ext2fs_readwrite.c revision 1.67 1 1.67 riastrad /* $NetBSD: ext2fs_readwrite.c,v 1.67 2015/03/27 17:27:56 riastradh 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.67 riastrad __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.67 2015/03/27 17:27:56 riastradh 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.1 bouyer /*
85 1.1 bouyer * Vnode op for reading.
86 1.1 bouyer */
87 1.1 bouyer /* ARGSUSED */
88 1.1 bouyer int
89 1.37 xtraeme ext2fs_read(void *v)
90 1.1 bouyer {
91 1.1 bouyer struct vop_read_args /* {
92 1.1 bouyer struct vnode *a_vp;
93 1.1 bouyer struct uio *a_uio;
94 1.1 bouyer int a_ioflag;
95 1.43 elad kauth_cred_t a_cred;
96 1.1 bouyer } */ *ap = v;
97 1.11 augustss struct vnode *vp;
98 1.11 augustss struct inode *ip;
99 1.11 augustss struct uio *uio;
100 1.35 chs struct ufsmount *ump;
101 1.14 chs vsize_t bytelen;
102 1.67 riastrad int advice;
103 1.53 pooka int error;
104 1.1 bouyer
105 1.1 bouyer vp = ap->a_vp;
106 1.1 bouyer ip = VTOI(vp);
107 1.33 mycroft ump = ip->i_ump;
108 1.1 bouyer uio = ap->a_uio;
109 1.33 mycroft error = 0;
110 1.1 bouyer
111 1.1 bouyer #ifdef DIAGNOSTIC
112 1.1 bouyer if (uio->uio_rw != UIO_READ)
113 1.1 bouyer panic("%s: mode", "ext2fs_read");
114 1.1 bouyer
115 1.1 bouyer if (vp->v_type == VLNK) {
116 1.36 ws if (ext2fs_size(ip) < ump->um_maxsymlinklen ||
117 1.62 jakllsch (ump->um_maxsymlinklen == 0 && ext2fs_nblock(ip) == 0))
118 1.1 bouyer panic("%s: short symlink", "ext2fs_read");
119 1.1 bouyer } else if (vp->v_type != VREG && vp->v_type != VDIR)
120 1.1 bouyer panic("%s: type %d", "ext2fs_read", vp->v_type);
121 1.1 bouyer #endif
122 1.67 riastrad /* XXX Eliminate me by refusing directory reads from userland. */
123 1.67 riastrad if (vp->v_type == VDIR)
124 1.67 riastrad return ext2fs_bufrd(vp, uio, ap->a_ioflag, ap->a_cred);
125 1.67 riastrad
126 1.54 tsutsui if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
127 1.1 bouyer return (EFBIG);
128 1.1 bouyer if (uio->uio_resid == 0)
129 1.18 chs return (0);
130 1.36 ws if (uio->uio_offset >= ext2fs_size(ip))
131 1.33 mycroft goto out;
132 1.1 bouyer
133 1.67 riastrad KASSERT(vp->v_type == VREG);
134 1.67 riastrad advice = IO_ADV_DECODE(ap->a_ioflag);
135 1.67 riastrad while (uio->uio_resid > 0) {
136 1.67 riastrad bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
137 1.67 riastrad uio->uio_resid);
138 1.67 riastrad if (bytelen == 0)
139 1.67 riastrad break;
140 1.67 riastrad
141 1.67 riastrad error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
142 1.67 riastrad UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
143 1.67 riastrad if (error)
144 1.67 riastrad break;
145 1.67 riastrad }
146 1.67 riastrad
147 1.67 riastrad out:
148 1.67 riastrad if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
149 1.67 riastrad ip->i_flag |= IN_ACCESS;
150 1.67 riastrad if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
151 1.67 riastrad error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
152 1.67 riastrad }
153 1.67 riastrad return (error);
154 1.67 riastrad }
155 1.67 riastrad
156 1.67 riastrad /*
157 1.67 riastrad * UFS op for reading via the buffer cache
158 1.67 riastrad */
159 1.67 riastrad int
160 1.67 riastrad ext2fs_bufrd(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
161 1.67 riastrad {
162 1.67 riastrad struct inode *ip;
163 1.67 riastrad struct ufsmount *ump;
164 1.67 riastrad struct m_ext2fs *fs;
165 1.67 riastrad struct buf *bp;
166 1.67 riastrad off_t bytesinfile;
167 1.67 riastrad daddr_t lbn, nextlbn;
168 1.67 riastrad long size, xfersize, blkoffset;
169 1.67 riastrad int error;
170 1.39 yamt
171 1.67 riastrad KASSERT(uio->uio_rw == UIO_READ);
172 1.67 riastrad KASSERT(VOP_ISLOCKED(vp));
173 1.67 riastrad KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
174 1.67 riastrad
175 1.67 riastrad ip = VTOI(vp);
176 1.67 riastrad ump = ip->i_ump;
177 1.67 riastrad fs = ip->i_e2fs;
178 1.67 riastrad error = 0;
179 1.67 riastrad
180 1.67 riastrad KASSERT(vp->v_type != VLNK ||
181 1.67 riastrad ext2fs_size(ip) >= ump->um_maxsymlinklen);
182 1.67 riastrad KASSERT(vp->v_type != VLNK || ump->um_maxsymlinklen != 0 ||
183 1.67 riastrad ext2fs_nblock(ip) != 0);
184 1.14 chs
185 1.67 riastrad if (uio->uio_offset > ump->um_maxfilesize)
186 1.67 riastrad return EFBIG;
187 1.67 riastrad if (uio->uio_resid == 0)
188 1.67 riastrad return 0;
189 1.67 riastrad if (uio->uio_offset >= ext2fs_size(ip))
190 1.14 chs goto out;
191 1.14 chs
192 1.1 bouyer for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
193 1.36 ws bytesinfile = ext2fs_size(ip) - uio->uio_offset;
194 1.33 mycroft if (bytesinfile <= 0)
195 1.1 bouyer break;
196 1.64 dholland lbn = ext2_lblkno(fs, uio->uio_offset);
197 1.1 bouyer nextlbn = lbn + 1;
198 1.1 bouyer size = fs->e2fs_bsize;
199 1.63 dholland blkoffset = ext2_blkoff(fs, uio->uio_offset);
200 1.1 bouyer xfersize = fs->e2fs_bsize - blkoffset;
201 1.1 bouyer if (uio->uio_resid < xfersize)
202 1.1 bouyer xfersize = uio->uio_resid;
203 1.1 bouyer if (bytesinfile < xfersize)
204 1.1 bouyer xfersize = bytesinfile;
205 1.1 bouyer
206 1.64 dholland if (ext2_lblktosize(fs, nextlbn) >= ext2fs_size(ip))
207 1.52 hannken error = bread(vp, lbn, size, NOCRED, 0, &bp);
208 1.17 chs else {
209 1.1 bouyer int nextsize = fs->e2fs_bsize;
210 1.1 bouyer error = breadn(vp, lbn,
211 1.52 hannken size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
212 1.17 chs }
213 1.1 bouyer if (error)
214 1.1 bouyer break;
215 1.1 bouyer
216 1.1 bouyer /*
217 1.1 bouyer * We should only get non-zero b_resid when an I/O error
218 1.1 bouyer * has occurred, which should cause us to break above.
219 1.1 bouyer * However, if the short read did not cause an error,
220 1.1 bouyer * then we want to ensure that we do not uiomove bad
221 1.1 bouyer * or uninitialized data.
222 1.1 bouyer */
223 1.1 bouyer size -= bp->b_resid;
224 1.1 bouyer if (size < xfersize) {
225 1.1 bouyer if (size == 0)
226 1.1 bouyer break;
227 1.1 bouyer xfersize = size;
228 1.1 bouyer }
229 1.14 chs error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
230 1.1 bouyer if (error)
231 1.1 bouyer break;
232 1.48 ad brelse(bp, 0);
233 1.1 bouyer }
234 1.1 bouyer if (bp != NULL)
235 1.48 ad brelse(bp, 0);
236 1.14 chs
237 1.14 chs out:
238 1.7 kleink if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
239 1.1 bouyer ip->i_flag |= IN_ACCESS;
240 1.67 riastrad if ((ioflag & IO_SYNC) == IO_SYNC)
241 1.38 yamt error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
242 1.7 kleink }
243 1.1 bouyer return (error);
244 1.1 bouyer }
245 1.1 bouyer
246 1.1 bouyer /*
247 1.1 bouyer * Vnode op for writing.
248 1.1 bouyer */
249 1.1 bouyer int
250 1.37 xtraeme ext2fs_write(void *v)
251 1.1 bouyer {
252 1.1 bouyer struct vop_write_args /* {
253 1.1 bouyer struct vnode *a_vp;
254 1.1 bouyer struct uio *a_uio;
255 1.1 bouyer int a_ioflag;
256 1.43 elad kauth_cred_t a_cred;
257 1.1 bouyer } */ *ap = v;
258 1.11 augustss struct vnode *vp;
259 1.11 augustss struct uio *uio;
260 1.11 augustss struct inode *ip;
261 1.11 augustss struct m_ext2fs *fs;
262 1.35 chs struct ufsmount *ump;
263 1.1 bouyer off_t osize;
264 1.67 riastrad int blkoffset, error, ioflag, resid;
265 1.14 chs vsize_t bytelen;
266 1.36 ws off_t oldoff = 0; /* XXX */
267 1.45 thorpej bool async;
268 1.33 mycroft int extended = 0;
269 1.53 pooka int advice;
270 1.1 bouyer
271 1.1 bouyer ioflag = ap->a_ioflag;
272 1.53 pooka advice = IO_ADV_DECODE(ioflag);
273 1.1 bouyer uio = ap->a_uio;
274 1.1 bouyer vp = ap->a_vp;
275 1.1 bouyer ip = VTOI(vp);
276 1.33 mycroft ump = ip->i_ump;
277 1.14 chs error = 0;
278 1.1 bouyer
279 1.1 bouyer #ifdef DIAGNOSTIC
280 1.1 bouyer if (uio->uio_rw != UIO_WRITE)
281 1.1 bouyer panic("%s: mode", "ext2fs_write");
282 1.1 bouyer #endif
283 1.1 bouyer
284 1.1 bouyer switch (vp->v_type) {
285 1.1 bouyer case VREG:
286 1.1 bouyer if (ioflag & IO_APPEND)
287 1.36 ws uio->uio_offset = ext2fs_size(ip);
288 1.1 bouyer if ((ip->i_e2fs_flags & EXT2_APPEND) &&
289 1.36 ws uio->uio_offset != ext2fs_size(ip))
290 1.1 bouyer return (EPERM);
291 1.1 bouyer /* FALLTHROUGH */
292 1.1 bouyer case VLNK:
293 1.1 bouyer break;
294 1.1 bouyer case VDIR:
295 1.1 bouyer if ((ioflag & IO_SYNC) == 0)
296 1.1 bouyer panic("%s: nonsync dir write", "ext2fs_write");
297 1.1 bouyer break;
298 1.1 bouyer default:
299 1.1 bouyer panic("%s: type", "ext2fs_write");
300 1.1 bouyer }
301 1.1 bouyer
302 1.1 bouyer fs = ip->i_e2fs;
303 1.1 bouyer if (uio->uio_offset < 0 ||
304 1.54 tsutsui (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
305 1.1 bouyer return (EFBIG);
306 1.24 chs if (uio->uio_resid == 0)
307 1.24 chs return (0);
308 1.1 bouyer
309 1.23 chs async = vp->v_mount->mnt_flag & MNT_ASYNC;
310 1.1 bouyer resid = uio->uio_resid;
311 1.36 ws osize = ext2fs_size(ip);
312 1.14 chs
313 1.67 riastrad KASSERT(vp->v_type == VREG);
314 1.67 riastrad while (uio->uio_resid > 0) {
315 1.67 riastrad oldoff = uio->uio_offset;
316 1.67 riastrad blkoffset = ext2_blkoff(fs, uio->uio_offset);
317 1.67 riastrad bytelen = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
318 1.67 riastrad
319 1.67 riastrad if (vp->v_size < oldoff + bytelen) {
320 1.67 riastrad uvm_vnp_setwritesize(vp, oldoff + bytelen);
321 1.67 riastrad }
322 1.67 riastrad error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
323 1.67 riastrad ap->a_cred, 0);
324 1.67 riastrad if (error)
325 1.67 riastrad break;
326 1.67 riastrad error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
327 1.67 riastrad UBC_WRITE | UBC_UNMAP_FLAG(vp));
328 1.67 riastrad if (error)
329 1.67 riastrad break;
330 1.14 chs
331 1.67 riastrad /*
332 1.67 riastrad * update UVM's notion of the size now that we've
333 1.67 riastrad * copied the data into the vnode's pages.
334 1.67 riastrad */
335 1.14 chs
336 1.67 riastrad if (vp->v_size < uio->uio_offset) {
337 1.67 riastrad uvm_vnp_setsize(vp, uio->uio_offset);
338 1.67 riastrad extended = 1;
339 1.14 chs }
340 1.67 riastrad
341 1.67 riastrad /*
342 1.67 riastrad * flush what we just wrote if necessary.
343 1.67 riastrad * XXXUBC simplistic async flushing.
344 1.67 riastrad */
345 1.67 riastrad
346 1.67 riastrad if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
347 1.57 rmind mutex_enter(vp->v_interlock);
348 1.67 riastrad error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
349 1.67 riastrad (uio->uio_offset >> 16) << 16,
350 1.67 riastrad PGO_CLEANIT | PGO_LAZY);
351 1.17 chs }
352 1.67 riastrad }
353 1.67 riastrad if (error == 0 && ioflag & IO_SYNC) {
354 1.67 riastrad mutex_enter(vp->v_interlock);
355 1.67 riastrad error = VOP_PUTPAGES(vp, trunc_page(oldoff),
356 1.67 riastrad round_page(ext2_blkroundup(fs, uio->uio_offset)),
357 1.67 riastrad PGO_CLEANIT | PGO_SYNCIO);
358 1.67 riastrad }
359 1.17 chs
360 1.67 riastrad /*
361 1.67 riastrad * If we successfully wrote any data, and we are not the superuser
362 1.67 riastrad * we clear the setuid and setgid bits as a precaution against
363 1.67 riastrad * tampering.
364 1.67 riastrad */
365 1.67 riastrad ip->i_flag |= IN_CHANGE | IN_UPDATE;
366 1.67 riastrad if (vp->v_mount->mnt_flag & MNT_RELATIME)
367 1.67 riastrad ip->i_flag |= IN_ACCESS;
368 1.67 riastrad if (resid > uio->uio_resid && ap->a_cred) {
369 1.67 riastrad if (ip->i_e2fs_mode & ISUID) {
370 1.67 riastrad if (kauth_authorize_vnode(ap->a_cred,
371 1.67 riastrad KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
372 1.67 riastrad ip->i_e2fs_mode &= ISUID;
373 1.67 riastrad }
374 1.67 riastrad
375 1.67 riastrad if (ip->i_e2fs_mode & ISGID) {
376 1.67 riastrad if (kauth_authorize_vnode(ap->a_cred,
377 1.67 riastrad KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
378 1.67 riastrad ip->i_e2fs_mode &= ~ISGID;
379 1.67 riastrad }
380 1.14 chs }
381 1.67 riastrad if (resid > uio->uio_resid)
382 1.67 riastrad VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
383 1.67 riastrad if (error) {
384 1.67 riastrad (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
385 1.67 riastrad uio->uio_offset -= resid - uio->uio_resid;
386 1.67 riastrad uio->uio_resid = resid;
387 1.67 riastrad } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
388 1.67 riastrad error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
389 1.67 riastrad KASSERT(vp->v_size == ext2fs_size(ip));
390 1.67 riastrad return (error);
391 1.67 riastrad }
392 1.67 riastrad
393 1.67 riastrad /*
394 1.67 riastrad * UFS op for writing via the buffer cache
395 1.67 riastrad */
396 1.67 riastrad int
397 1.67 riastrad ext2fs_bufwr(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
398 1.67 riastrad {
399 1.67 riastrad struct inode *ip;
400 1.67 riastrad struct ufsmount *ump;
401 1.67 riastrad struct m_ext2fs *fs;
402 1.67 riastrad struct buf *bp;
403 1.67 riastrad int flags;
404 1.67 riastrad off_t osize;
405 1.67 riastrad daddr_t lbn;
406 1.67 riastrad int resid, blkoffset, xfersize;
407 1.67 riastrad int extended = 0;
408 1.67 riastrad int error;
409 1.67 riastrad
410 1.67 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
411 1.67 riastrad KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
412 1.67 riastrad KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
413 1.67 riastrad KASSERT(uio->uio_rw == UIO_WRITE);
414 1.67 riastrad
415 1.67 riastrad ip = VTOI(vp);
416 1.67 riastrad ump = ip->i_ump;
417 1.67 riastrad fs = ip->i_e2fs;
418 1.67 riastrad error = 0;
419 1.67 riastrad
420 1.67 riastrad if (uio->uio_offset < 0 ||
421 1.67 riastrad uio->uio_resid > ump->um_maxfilesize ||
422 1.67 riastrad uio->uio_offset > (ump->um_maxfilesize - uio->uio_resid))
423 1.67 riastrad return EFBIG;
424 1.67 riastrad if (uio->uio_resid == 0)
425 1.67 riastrad return 0;
426 1.14 chs
427 1.1 bouyer flags = ioflag & IO_SYNC ? B_SYNC : 0;
428 1.67 riastrad resid = uio->uio_resid;
429 1.67 riastrad osize = ext2fs_size(ip);
430 1.67 riastrad
431 1.1 bouyer for (error = 0; uio->uio_resid > 0;) {
432 1.64 dholland lbn = ext2_lblkno(fs, uio->uio_offset);
433 1.63 dholland blkoffset = ext2_blkoff(fs, uio->uio_offset);
434 1.16 chs xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
435 1.14 chs if (xfersize < fs->e2fs_bsize)
436 1.1 bouyer flags |= B_CLRBUF;
437 1.1 bouyer else
438 1.1 bouyer flags &= ~B_CLRBUF;
439 1.67 riastrad error = ext2fs_balloc(ip, lbn, blkoffset + xfersize, cred, &bp,
440 1.67 riastrad flags);
441 1.1 bouyer if (error)
442 1.1 bouyer break;
443 1.36 ws if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
444 1.36 ws error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
445 1.36 ws if (error)
446 1.36 ws break;
447 1.1 bouyer }
448 1.14 chs error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
449 1.21 chs
450 1.21 chs /*
451 1.21 chs * update UVM's notion of the size now that we've
452 1.21 chs * copied the data into the vnode's pages.
453 1.21 chs */
454 1.21 chs
455 1.21 chs if (vp->v_size < uio->uio_offset) {
456 1.21 chs uvm_vnp_setsize(vp, uio->uio_offset);
457 1.26 jdolecek extended = 1;
458 1.21 chs }
459 1.21 chs
460 1.1 bouyer if (ioflag & IO_SYNC)
461 1.1 bouyer (void)bwrite(bp);
462 1.1 bouyer else if (xfersize + blkoffset == fs->e2fs_bsize)
463 1.17 chs bawrite(bp);
464 1.1 bouyer else
465 1.1 bouyer bdwrite(bp);
466 1.1 bouyer if (error || xfersize == 0)
467 1.1 bouyer break;
468 1.1 bouyer }
469 1.21 chs
470 1.1 bouyer /*
471 1.1 bouyer * If we successfully wrote any data, and we are not the superuser
472 1.1 bouyer * we clear the setuid and setgid bits as a precaution against
473 1.1 bouyer * tampering.
474 1.1 bouyer */
475 1.14 chs ip->i_flag |= IN_CHANGE | IN_UPDATE;
476 1.58 christos if (vp->v_mount->mnt_flag & MNT_RELATIME)
477 1.58 christos ip->i_flag |= IN_ACCESS;
478 1.67 riastrad if (resid > uio->uio_resid && cred) {
479 1.59 elad if (ip->i_e2fs_mode & ISUID) {
480 1.67 riastrad if (kauth_authorize_vnode(cred,
481 1.60 christos KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
482 1.59 elad ip->i_e2fs_mode &= ISUID;
483 1.59 elad }
484 1.59 elad
485 1.59 elad if (ip->i_e2fs_mode & ISGID) {
486 1.67 riastrad if (kauth_authorize_vnode(cred,
487 1.60 christos KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
488 1.59 elad ip->i_e2fs_mode &= ~ISGID;
489 1.59 elad }
490 1.59 elad }
491 1.26 jdolecek if (resid > uio->uio_resid)
492 1.26 jdolecek VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
493 1.1 bouyer if (error) {
494 1.67 riastrad (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, cred);
495 1.17 chs uio->uio_offset -= resid - uio->uio_resid;
496 1.17 chs uio->uio_resid = resid;
497 1.9 mycroft } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
498 1.38 yamt error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
499 1.36 ws KASSERT(vp->v_size == ext2fs_size(ip));
500 1.1 bouyer return (error);
501 1.1 bouyer }
502