1*0a6a1f1dSLionel Sambuc /* $NetBSD: ext2fs_readwrite.c,v 1.74 2015/03/28 19:24:04 maxv Exp $ */
2d65f6f70SBen Gras
3d65f6f70SBen Gras /*-
4d65f6f70SBen Gras * Copyright (c) 1993
5d65f6f70SBen Gras * The Regents of the University of California. All rights reserved.
6d65f6f70SBen Gras *
7d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
8d65f6f70SBen Gras * modification, are permitted provided that the following conditions
9d65f6f70SBen Gras * are met:
10d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
11d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
12d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
14d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
15d65f6f70SBen Gras * 3. Neither the name of the University nor the names of its contributors
16d65f6f70SBen Gras * may be used to endorse or promote products derived from this software
17d65f6f70SBen Gras * without specific prior written permission.
18d65f6f70SBen Gras *
19d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d65f6f70SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d65f6f70SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d65f6f70SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d65f6f70SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d65f6f70SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d65f6f70SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d65f6f70SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d65f6f70SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d65f6f70SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d65f6f70SBen Gras * SUCH DAMAGE.
30d65f6f70SBen Gras *
31d65f6f70SBen Gras * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
32d65f6f70SBen Gras * Modified for ext2fs by Manuel Bouyer.
33d65f6f70SBen Gras */
34d65f6f70SBen Gras
35d65f6f70SBen Gras /*-
36d65f6f70SBen Gras * Copyright (c) 1997 Manuel Bouyer.
37d65f6f70SBen Gras *
38d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
39d65f6f70SBen Gras * modification, are permitted provided that the following conditions
40d65f6f70SBen Gras * are met:
41d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
42d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
43d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
44d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
45d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
46d65f6f70SBen Gras *
47d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48d65f6f70SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49d65f6f70SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50d65f6f70SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51d65f6f70SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52d65f6f70SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53d65f6f70SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54d65f6f70SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55d65f6f70SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56d65f6f70SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57d65f6f70SBen Gras *
58d65f6f70SBen Gras * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
59d65f6f70SBen Gras * Modified for ext2fs by Manuel Bouyer.
60d65f6f70SBen Gras */
61d65f6f70SBen Gras
62d65f6f70SBen Gras #include <sys/cdefs.h>
63*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.74 2015/03/28 19:24:04 maxv Exp $");
64d65f6f70SBen Gras
65d65f6f70SBen Gras #include <sys/param.h>
66d65f6f70SBen Gras #include <sys/systm.h>
67d65f6f70SBen Gras #include <sys/resourcevar.h>
68d65f6f70SBen Gras #include <sys/kernel.h>
69d65f6f70SBen Gras #include <sys/file.h>
70d65f6f70SBen Gras #include <sys/stat.h>
71d65f6f70SBen Gras #include <sys/buf.h>
72d65f6f70SBen Gras #include <sys/proc.h>
73d65f6f70SBen Gras #include <sys/mount.h>
74d65f6f70SBen Gras #include <sys/vnode.h>
75d65f6f70SBen Gras #include <sys/signalvar.h>
76d65f6f70SBen Gras #include <sys/kauth.h>
77d65f6f70SBen Gras
78d65f6f70SBen Gras #include <ufs/ufs/inode.h>
79d65f6f70SBen Gras #include <ufs/ufs/ufsmount.h>
80d65f6f70SBen Gras #include <ufs/ufs/ufs_extern.h>
81d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs.h>
82d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs_extern.h>
83d65f6f70SBen Gras
84*0a6a1f1dSLionel Sambuc static int ext2fs_post_read_update(struct vnode *, int, int);
85*0a6a1f1dSLionel Sambuc static int ext2fs_post_write_update(struct vnode *, struct uio *, int,
86*0a6a1f1dSLionel Sambuc kauth_cred_t, off_t, int, int, int);
87d65f6f70SBen Gras
88d65f6f70SBen Gras /*
89d65f6f70SBen Gras * Vnode op for reading.
90d65f6f70SBen Gras */
91d65f6f70SBen Gras /* ARGSUSED */
92d65f6f70SBen Gras int
ext2fs_read(void * v)93d65f6f70SBen Gras ext2fs_read(void *v)
94d65f6f70SBen Gras {
95d65f6f70SBen Gras struct vop_read_args /* {
96d65f6f70SBen Gras struct vnode *a_vp;
97d65f6f70SBen Gras struct uio *a_uio;
98d65f6f70SBen Gras int a_ioflag;
99d65f6f70SBen Gras kauth_cred_t a_cred;
100d65f6f70SBen Gras } */ *ap = v;
101d65f6f70SBen Gras struct vnode *vp;
102d65f6f70SBen Gras struct inode *ip;
103d65f6f70SBen Gras struct uio *uio;
104d65f6f70SBen Gras struct ufsmount *ump;
105d65f6f70SBen Gras vsize_t bytelen;
106*0a6a1f1dSLionel Sambuc int advice;
107d65f6f70SBen Gras int error;
108d65f6f70SBen Gras
109d65f6f70SBen Gras vp = ap->a_vp;
110d65f6f70SBen Gras ip = VTOI(vp);
111d65f6f70SBen Gras ump = ip->i_ump;
112d65f6f70SBen Gras uio = ap->a_uio;
113d65f6f70SBen Gras error = 0;
114d65f6f70SBen Gras
115*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_READ);
116*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
117d65f6f70SBen Gras
118*0a6a1f1dSLionel Sambuc /* XXX Eliminate me by refusing directory reads from userland. */
119*0a6a1f1dSLionel Sambuc if (vp->v_type == VDIR)
120*0a6a1f1dSLionel Sambuc return ext2fs_bufrd(vp, uio, ap->a_ioflag, ap->a_cred);
121*0a6a1f1dSLionel Sambuc
122d65f6f70SBen Gras if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
123d65f6f70SBen Gras return (EFBIG);
124d65f6f70SBen Gras if (uio->uio_resid == 0)
125d65f6f70SBen Gras return (0);
126d65f6f70SBen Gras if (uio->uio_offset >= ext2fs_size(ip))
127d65f6f70SBen Gras goto out;
128d65f6f70SBen Gras
129*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG);
130*0a6a1f1dSLionel Sambuc advice = IO_ADV_DECODE(ap->a_ioflag);
131d65f6f70SBen Gras while (uio->uio_resid > 0) {
132d65f6f70SBen Gras bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
133d65f6f70SBen Gras uio->uio_resid);
134d65f6f70SBen Gras if (bytelen == 0)
135d65f6f70SBen Gras break;
136d65f6f70SBen Gras
137d65f6f70SBen Gras error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
138d65f6f70SBen Gras UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
139d65f6f70SBen Gras if (error)
140d65f6f70SBen Gras break;
141d65f6f70SBen Gras }
142*0a6a1f1dSLionel Sambuc
143*0a6a1f1dSLionel Sambuc out:
144*0a6a1f1dSLionel Sambuc error = ext2fs_post_read_update(vp, ap->a_ioflag, error);
145*0a6a1f1dSLionel Sambuc return (error);
146d65f6f70SBen Gras }
147d65f6f70SBen Gras
148*0a6a1f1dSLionel Sambuc /*
149*0a6a1f1dSLionel Sambuc * UFS op for reading via the buffer cache
150*0a6a1f1dSLionel Sambuc */
151*0a6a1f1dSLionel Sambuc int
ext2fs_bufrd(struct vnode * vp,struct uio * uio,int ioflag,kauth_cred_t cred)152*0a6a1f1dSLionel Sambuc ext2fs_bufrd(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
153*0a6a1f1dSLionel Sambuc {
154*0a6a1f1dSLionel Sambuc struct inode *ip;
155*0a6a1f1dSLionel Sambuc struct ufsmount *ump;
156*0a6a1f1dSLionel Sambuc struct m_ext2fs *fs;
157*0a6a1f1dSLionel Sambuc struct buf *bp;
158*0a6a1f1dSLionel Sambuc off_t bytesinfile;
159*0a6a1f1dSLionel Sambuc daddr_t lbn, nextlbn;
160*0a6a1f1dSLionel Sambuc long size, xfersize, blkoffset;
161*0a6a1f1dSLionel Sambuc int error;
162*0a6a1f1dSLionel Sambuc
163*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_READ);
164*0a6a1f1dSLionel Sambuc KASSERT(VOP_ISLOCKED(vp));
165*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc ip = VTOI(vp);
168*0a6a1f1dSLionel Sambuc ump = ip->i_ump;
169*0a6a1f1dSLionel Sambuc fs = ip->i_e2fs;
170*0a6a1f1dSLionel Sambuc error = 0;
171*0a6a1f1dSLionel Sambuc
172*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VLNK ||
173*0a6a1f1dSLionel Sambuc ext2fs_size(ip) >= ump->um_maxsymlinklen);
174*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VLNK || ump->um_maxsymlinklen != 0 ||
175*0a6a1f1dSLionel Sambuc ext2fs_nblock(ip) != 0);
176*0a6a1f1dSLionel Sambuc
177*0a6a1f1dSLionel Sambuc if (uio->uio_offset > ump->um_maxfilesize)
178*0a6a1f1dSLionel Sambuc return EFBIG;
179*0a6a1f1dSLionel Sambuc if (uio->uio_resid == 0)
180*0a6a1f1dSLionel Sambuc return 0;
181*0a6a1f1dSLionel Sambuc if (uio->uio_offset >= ext2fs_size(ip))
182*0a6a1f1dSLionel Sambuc goto out;
183*0a6a1f1dSLionel Sambuc
184d65f6f70SBen Gras for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
185d65f6f70SBen Gras bytesinfile = ext2fs_size(ip) - uio->uio_offset;
186d65f6f70SBen Gras if (bytesinfile <= 0)
187d65f6f70SBen Gras break;
18884d9c625SLionel Sambuc lbn = ext2_lblkno(fs, uio->uio_offset);
189d65f6f70SBen Gras nextlbn = lbn + 1;
190d65f6f70SBen Gras size = fs->e2fs_bsize;
19184d9c625SLionel Sambuc blkoffset = ext2_blkoff(fs, uio->uio_offset);
192d65f6f70SBen Gras xfersize = fs->e2fs_bsize - blkoffset;
193d65f6f70SBen Gras if (uio->uio_resid < xfersize)
194d65f6f70SBen Gras xfersize = uio->uio_resid;
195d65f6f70SBen Gras if (bytesinfile < xfersize)
196d65f6f70SBen Gras xfersize = bytesinfile;
197d65f6f70SBen Gras
19884d9c625SLionel Sambuc if (ext2_lblktosize(fs, nextlbn) >= ext2fs_size(ip))
199*0a6a1f1dSLionel Sambuc error = bread(vp, lbn, size, 0, &bp);
200d65f6f70SBen Gras else {
201d65f6f70SBen Gras int nextsize = fs->e2fs_bsize;
202d65f6f70SBen Gras error = breadn(vp, lbn,
203*0a6a1f1dSLionel Sambuc size, &nextlbn, &nextsize, 1, 0, &bp);
204d65f6f70SBen Gras }
205d65f6f70SBen Gras if (error)
206d65f6f70SBen Gras break;
207d65f6f70SBen Gras
208d65f6f70SBen Gras /*
209d65f6f70SBen Gras * We should only get non-zero b_resid when an I/O error
210d65f6f70SBen Gras * has occurred, which should cause us to break above.
211d65f6f70SBen Gras * However, if the short read did not cause an error,
212d65f6f70SBen Gras * then we want to ensure that we do not uiomove bad
213d65f6f70SBen Gras * or uninitialized data.
214d65f6f70SBen Gras */
215d65f6f70SBen Gras size -= bp->b_resid;
216d65f6f70SBen Gras if (size < xfersize) {
217d65f6f70SBen Gras if (size == 0)
218d65f6f70SBen Gras break;
219d65f6f70SBen Gras xfersize = size;
220d65f6f70SBen Gras }
221d65f6f70SBen Gras error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
222d65f6f70SBen Gras if (error)
223d65f6f70SBen Gras break;
224d65f6f70SBen Gras brelse(bp, 0);
225d65f6f70SBen Gras }
226d65f6f70SBen Gras if (bp != NULL)
227d65f6f70SBen Gras brelse(bp, 0);
228d65f6f70SBen Gras
229d65f6f70SBen Gras out:
230*0a6a1f1dSLionel Sambuc error = ext2fs_post_read_update(vp, ioflag, error);
231*0a6a1f1dSLionel Sambuc return (error);
232*0a6a1f1dSLionel Sambuc }
233*0a6a1f1dSLionel Sambuc
234*0a6a1f1dSLionel Sambuc static int
ext2fs_post_read_update(struct vnode * vp,int ioflag,int oerror)235*0a6a1f1dSLionel Sambuc ext2fs_post_read_update(struct vnode *vp, int ioflag, int oerror)
236*0a6a1f1dSLionel Sambuc {
237*0a6a1f1dSLionel Sambuc struct inode *ip = VTOI(vp);
238*0a6a1f1dSLionel Sambuc int error = oerror;
239*0a6a1f1dSLionel Sambuc
240d65f6f70SBen Gras if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
241d65f6f70SBen Gras ip->i_flag |= IN_ACCESS;
242*0a6a1f1dSLionel Sambuc if ((ioflag & IO_SYNC) == IO_SYNC)
243d65f6f70SBen Gras error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
244d65f6f70SBen Gras }
245*0a6a1f1dSLionel Sambuc
246*0a6a1f1dSLionel Sambuc /* Read error overrides any inode update error. */
247*0a6a1f1dSLionel Sambuc if (oerror)
248*0a6a1f1dSLionel Sambuc error = oerror;
249*0a6a1f1dSLionel Sambuc return error;
250d65f6f70SBen Gras }
251d65f6f70SBen Gras
252d65f6f70SBen Gras /*
253d65f6f70SBen Gras * Vnode op for writing.
254d65f6f70SBen Gras */
255d65f6f70SBen Gras int
ext2fs_write(void * v)256d65f6f70SBen Gras ext2fs_write(void *v)
257d65f6f70SBen Gras {
258d65f6f70SBen Gras struct vop_write_args /* {
259d65f6f70SBen Gras struct vnode *a_vp;
260d65f6f70SBen Gras struct uio *a_uio;
261d65f6f70SBen Gras int a_ioflag;
262d65f6f70SBen Gras kauth_cred_t a_cred;
263d65f6f70SBen Gras } */ *ap = v;
264d65f6f70SBen Gras struct vnode *vp;
265d65f6f70SBen Gras struct uio *uio;
266d65f6f70SBen Gras struct inode *ip;
267d65f6f70SBen Gras struct m_ext2fs *fs;
268d65f6f70SBen Gras struct ufsmount *ump;
269d65f6f70SBen Gras off_t osize;
270*0a6a1f1dSLionel Sambuc int blkoffset, error, ioflag, resid;
271d65f6f70SBen Gras vsize_t bytelen;
272d65f6f70SBen Gras off_t oldoff = 0; /* XXX */
273d65f6f70SBen Gras bool async;
274d65f6f70SBen Gras int extended = 0;
275d65f6f70SBen Gras int advice;
276d65f6f70SBen Gras
277d65f6f70SBen Gras ioflag = ap->a_ioflag;
278d65f6f70SBen Gras advice = IO_ADV_DECODE(ioflag);
279d65f6f70SBen Gras uio = ap->a_uio;
280d65f6f70SBen Gras vp = ap->a_vp;
281d65f6f70SBen Gras ip = VTOI(vp);
282d65f6f70SBen Gras ump = ip->i_ump;
283d65f6f70SBen Gras error = 0;
284d65f6f70SBen Gras
285*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_WRITE);
286*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG);
287d65f6f70SBen Gras
288d65f6f70SBen Gras if (ioflag & IO_APPEND)
289d65f6f70SBen Gras uio->uio_offset = ext2fs_size(ip);
290d65f6f70SBen Gras if ((ip->i_e2fs_flags & EXT2_APPEND) &&
291d65f6f70SBen Gras uio->uio_offset != ext2fs_size(ip))
292d65f6f70SBen Gras return (EPERM);
293d65f6f70SBen Gras
294d65f6f70SBen Gras fs = ip->i_e2fs;
295d65f6f70SBen Gras if (uio->uio_offset < 0 ||
296d65f6f70SBen Gras (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
297d65f6f70SBen Gras return (EFBIG);
298d65f6f70SBen Gras if (uio->uio_resid == 0)
299d65f6f70SBen Gras return (0);
300d65f6f70SBen Gras
301d65f6f70SBen Gras async = vp->v_mount->mnt_flag & MNT_ASYNC;
302d65f6f70SBen Gras resid = uio->uio_resid;
303d65f6f70SBen Gras osize = ext2fs_size(ip);
304d65f6f70SBen Gras
305*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG);
306d65f6f70SBen Gras while (uio->uio_resid > 0) {
307d65f6f70SBen Gras oldoff = uio->uio_offset;
30884d9c625SLionel Sambuc blkoffset = ext2_blkoff(fs, uio->uio_offset);
309*0a6a1f1dSLionel Sambuc bytelen = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
310d65f6f70SBen Gras
311d65f6f70SBen Gras if (vp->v_size < oldoff + bytelen) {
312d65f6f70SBen Gras uvm_vnp_setwritesize(vp, oldoff + bytelen);
313d65f6f70SBen Gras }
314*0a6a1f1dSLionel Sambuc error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
315*0a6a1f1dSLionel Sambuc ap->a_cred, 0);
316d65f6f70SBen Gras if (error)
317d65f6f70SBen Gras break;
318d65f6f70SBen Gras error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
319d65f6f70SBen Gras UBC_WRITE | UBC_UNMAP_FLAG(vp));
320d65f6f70SBen Gras if (error)
321d65f6f70SBen Gras break;
322d65f6f70SBen Gras
323d65f6f70SBen Gras /*
324d65f6f70SBen Gras * update UVM's notion of the size now that we've
325d65f6f70SBen Gras * copied the data into the vnode's pages.
326d65f6f70SBen Gras */
327d65f6f70SBen Gras
328d65f6f70SBen Gras if (vp->v_size < uio->uio_offset) {
329d65f6f70SBen Gras uvm_vnp_setsize(vp, uio->uio_offset);
330d65f6f70SBen Gras extended = 1;
331d65f6f70SBen Gras }
332d65f6f70SBen Gras
333d65f6f70SBen Gras /*
334d65f6f70SBen Gras * flush what we just wrote if necessary.
335d65f6f70SBen Gras * XXXUBC simplistic async flushing.
336d65f6f70SBen Gras */
337d65f6f70SBen Gras
338d65f6f70SBen Gras if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
339d65f6f70SBen Gras mutex_enter(vp->v_interlock);
340d65f6f70SBen Gras error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
34184d9c625SLionel Sambuc (uio->uio_offset >> 16) << 16,
34284d9c625SLionel Sambuc PGO_CLEANIT | PGO_LAZY);
343d65f6f70SBen Gras }
344d65f6f70SBen Gras }
345d65f6f70SBen Gras if (error == 0 && ioflag & IO_SYNC) {
346d65f6f70SBen Gras mutex_enter(vp->v_interlock);
347d65f6f70SBen Gras error = VOP_PUTPAGES(vp, trunc_page(oldoff),
34884d9c625SLionel Sambuc round_page(ext2_blkroundup(fs, uio->uio_offset)),
349d65f6f70SBen Gras PGO_CLEANIT | PGO_SYNCIO);
350d65f6f70SBen Gras }
351d65f6f70SBen Gras
352*0a6a1f1dSLionel Sambuc error = ext2fs_post_write_update(vp, uio, ioflag, ap->a_cred, osize,
353*0a6a1f1dSLionel Sambuc resid, extended, error);
354*0a6a1f1dSLionel Sambuc return (error);
355d65f6f70SBen Gras }
356d65f6f70SBen Gras
357*0a6a1f1dSLionel Sambuc /*
358*0a6a1f1dSLionel Sambuc * UFS op for writing via the buffer cache
359*0a6a1f1dSLionel Sambuc */
360*0a6a1f1dSLionel Sambuc int
ext2fs_bufwr(struct vnode * vp,struct uio * uio,int ioflag,kauth_cred_t cred)361*0a6a1f1dSLionel Sambuc ext2fs_bufwr(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
362*0a6a1f1dSLionel Sambuc {
363*0a6a1f1dSLionel Sambuc struct inode *ip;
364*0a6a1f1dSLionel Sambuc struct ufsmount *ump;
365*0a6a1f1dSLionel Sambuc struct m_ext2fs *fs;
366*0a6a1f1dSLionel Sambuc struct buf *bp;
367*0a6a1f1dSLionel Sambuc int flags;
368*0a6a1f1dSLionel Sambuc off_t osize;
369*0a6a1f1dSLionel Sambuc daddr_t lbn;
370*0a6a1f1dSLionel Sambuc int resid, blkoffset, xfersize;
371*0a6a1f1dSLionel Sambuc int extended = 0;
372*0a6a1f1dSLionel Sambuc int error;
373*0a6a1f1dSLionel Sambuc
374*0a6a1f1dSLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
375*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
376*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
377*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_WRITE);
378*0a6a1f1dSLionel Sambuc
379*0a6a1f1dSLionel Sambuc ip = VTOI(vp);
380*0a6a1f1dSLionel Sambuc ump = ip->i_ump;
381*0a6a1f1dSLionel Sambuc fs = ip->i_e2fs;
382*0a6a1f1dSLionel Sambuc error = 0;
383*0a6a1f1dSLionel Sambuc
384*0a6a1f1dSLionel Sambuc if (uio->uio_offset < 0 ||
385*0a6a1f1dSLionel Sambuc uio->uio_resid > ump->um_maxfilesize ||
386*0a6a1f1dSLionel Sambuc uio->uio_offset > (ump->um_maxfilesize - uio->uio_resid))
387*0a6a1f1dSLionel Sambuc return EFBIG;
388*0a6a1f1dSLionel Sambuc if (uio->uio_resid == 0)
389*0a6a1f1dSLionel Sambuc return 0;
390*0a6a1f1dSLionel Sambuc
391d65f6f70SBen Gras flags = ioflag & IO_SYNC ? B_SYNC : 0;
392*0a6a1f1dSLionel Sambuc resid = uio->uio_resid;
393*0a6a1f1dSLionel Sambuc osize = ext2fs_size(ip);
394*0a6a1f1dSLionel Sambuc
395d65f6f70SBen Gras for (error = 0; uio->uio_resid > 0;) {
39684d9c625SLionel Sambuc lbn = ext2_lblkno(fs, uio->uio_offset);
39784d9c625SLionel Sambuc blkoffset = ext2_blkoff(fs, uio->uio_offset);
398d65f6f70SBen Gras xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
399d65f6f70SBen Gras if (xfersize < fs->e2fs_bsize)
400d65f6f70SBen Gras flags |= B_CLRBUF;
401d65f6f70SBen Gras else
402d65f6f70SBen Gras flags &= ~B_CLRBUF;
403*0a6a1f1dSLionel Sambuc error = ext2fs_balloc(ip, lbn, blkoffset + xfersize, cred, &bp,
404*0a6a1f1dSLionel Sambuc flags);
405d65f6f70SBen Gras if (error)
406d65f6f70SBen Gras break;
407d65f6f70SBen Gras if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
408d65f6f70SBen Gras error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
409d65f6f70SBen Gras if (error)
410d65f6f70SBen Gras break;
411d65f6f70SBen Gras }
412d65f6f70SBen Gras error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
413d65f6f70SBen Gras
414d65f6f70SBen Gras /*
415d65f6f70SBen Gras * update UVM's notion of the size now that we've
416d65f6f70SBen Gras * copied the data into the vnode's pages.
417d65f6f70SBen Gras */
418d65f6f70SBen Gras
419d65f6f70SBen Gras if (vp->v_size < uio->uio_offset) {
420d65f6f70SBen Gras uvm_vnp_setsize(vp, uio->uio_offset);
421d65f6f70SBen Gras extended = 1;
422d65f6f70SBen Gras }
423d65f6f70SBen Gras
424d65f6f70SBen Gras if (ioflag & IO_SYNC)
425d65f6f70SBen Gras (void)bwrite(bp);
426d65f6f70SBen Gras else if (xfersize + blkoffset == fs->e2fs_bsize)
427d65f6f70SBen Gras bawrite(bp);
428d65f6f70SBen Gras else
429d65f6f70SBen Gras bdwrite(bp);
430d65f6f70SBen Gras if (error || xfersize == 0)
431d65f6f70SBen Gras break;
432d65f6f70SBen Gras }
433d65f6f70SBen Gras
434*0a6a1f1dSLionel Sambuc error = ext2fs_post_write_update(vp, uio, ioflag, cred, osize, resid,
435*0a6a1f1dSLionel Sambuc extended, error);
436*0a6a1f1dSLionel Sambuc return (error);
437*0a6a1f1dSLionel Sambuc }
438d65f6f70SBen Gras
439*0a6a1f1dSLionel Sambuc static int
ext2fs_post_write_update(struct vnode * vp,struct uio * uio,int ioflag,kauth_cred_t cred,off_t osize,int resid,int extended,int oerror)440*0a6a1f1dSLionel Sambuc ext2fs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
441*0a6a1f1dSLionel Sambuc kauth_cred_t cred, off_t osize, int resid, int extended, int oerror)
442*0a6a1f1dSLionel Sambuc {
443*0a6a1f1dSLionel Sambuc struct inode *ip = VTOI(vp);
444*0a6a1f1dSLionel Sambuc int error = oerror;
445*0a6a1f1dSLionel Sambuc
446*0a6a1f1dSLionel Sambuc /* Trigger ctime and mtime updates, and atime if MNT_RELATIME. */
447d65f6f70SBen Gras ip->i_flag |= IN_CHANGE | IN_UPDATE;
448d65f6f70SBen Gras if (vp->v_mount->mnt_flag & MNT_RELATIME)
449d65f6f70SBen Gras ip->i_flag |= IN_ACCESS;
450*0a6a1f1dSLionel Sambuc
451*0a6a1f1dSLionel Sambuc /*
452*0a6a1f1dSLionel Sambuc * If we successfully wrote any data and we are not the superuser,
453*0a6a1f1dSLionel Sambuc * we clear the setuid and setgid bits as a precaution against
454*0a6a1f1dSLionel Sambuc * tampering.
455*0a6a1f1dSLionel Sambuc */
456*0a6a1f1dSLionel Sambuc if (resid > uio->uio_resid && cred) {
45784d9c625SLionel Sambuc if (ip->i_e2fs_mode & ISUID) {
458*0a6a1f1dSLionel Sambuc if (kauth_authorize_vnode(cred,
45984d9c625SLionel Sambuc KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
46084d9c625SLionel Sambuc ip->i_e2fs_mode &= ISUID;
46184d9c625SLionel Sambuc }
46284d9c625SLionel Sambuc
46384d9c625SLionel Sambuc if (ip->i_e2fs_mode & ISGID) {
464*0a6a1f1dSLionel Sambuc if (kauth_authorize_vnode(cred,
46584d9c625SLionel Sambuc KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
46684d9c625SLionel Sambuc ip->i_e2fs_mode &= ~ISGID;
46784d9c625SLionel Sambuc }
46884d9c625SLionel Sambuc }
469*0a6a1f1dSLionel Sambuc
470*0a6a1f1dSLionel Sambuc /* If we successfully wrote anything, notify kevent listeners. */
471d65f6f70SBen Gras if (resid > uio->uio_resid)
472d65f6f70SBen Gras VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
473*0a6a1f1dSLionel Sambuc
474*0a6a1f1dSLionel Sambuc /*
475*0a6a1f1dSLionel Sambuc * Update the size on disk: truncate back to original size on
476*0a6a1f1dSLionel Sambuc * error, or reflect the new size on success.
477*0a6a1f1dSLionel Sambuc */
478d65f6f70SBen Gras if (error) {
479*0a6a1f1dSLionel Sambuc (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, cred);
480d65f6f70SBen Gras uio->uio_offset -= resid - uio->uio_resid;
481d65f6f70SBen Gras uio->uio_resid = resid;
482d65f6f70SBen Gras } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
483d65f6f70SBen Gras error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
484*0a6a1f1dSLionel Sambuc
485*0a6a1f1dSLionel Sambuc /* Make sure the vnode uvm size matches the inode file size. */
486d65f6f70SBen Gras KASSERT(vp->v_size == ext2fs_size(ip));
487*0a6a1f1dSLionel Sambuc
488*0a6a1f1dSLionel Sambuc /* Write error overrides any inode update error. */
489*0a6a1f1dSLionel Sambuc if (oerror)
490*0a6a1f1dSLionel Sambuc error = oerror;
491*0a6a1f1dSLionel Sambuc return error;
492d65f6f70SBen Gras }
493