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