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