1*5b133f3fSguenther /* $OpenBSD: ext2fs_readwrite.c,v 1.46 2023/03/08 04:43:09 guenther Exp $ */
24be9887dSart /* $NetBSD: ext2fs_readwrite.c,v 1.16 2001/02/27 04:37:47 chs Exp $ */
35ac2d602Sdownsj
45ac2d602Sdownsj /*-
51f3ff51cSdownsj * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj * Copyright (c) 1993
75ac2d602Sdownsj * The Regents of the University of California. All rights reserved.
85ac2d602Sdownsj *
95ac2d602Sdownsj * Redistribution and use in source and binary forms, with or without
105ac2d602Sdownsj * modification, are permitted provided that the following conditions
115ac2d602Sdownsj * are met:
125ac2d602Sdownsj * 1. Redistributions of source code must retain the above copyright
135ac2d602Sdownsj * notice, this list of conditions and the following disclaimer.
145ac2d602Sdownsj * 2. Redistributions in binary form must reproduce the above copyright
155ac2d602Sdownsj * notice, this list of conditions and the following disclaimer in the
165ac2d602Sdownsj * documentation and/or other materials provided with the distribution.
1729295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
185ac2d602Sdownsj * may be used to endorse or promote products derived from this software
195ac2d602Sdownsj * without specific prior written permission.
205ac2d602Sdownsj *
215ac2d602Sdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225ac2d602Sdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235ac2d602Sdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245ac2d602Sdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255ac2d602Sdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265ac2d602Sdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275ac2d602Sdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285ac2d602Sdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295ac2d602Sdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305ac2d602Sdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315ac2d602Sdownsj * SUCH DAMAGE.
325ac2d602Sdownsj *
335ac2d602Sdownsj * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
341f3ff51cSdownsj * Modified for ext2fs by Manuel Bouyer.
355ac2d602Sdownsj */
365ac2d602Sdownsj
375ac2d602Sdownsj #include <sys/param.h>
385ac2d602Sdownsj #include <sys/systm.h>
395ac2d602Sdownsj #include <sys/resourcevar.h>
405ac2d602Sdownsj #include <sys/kernel.h>
415ac2d602Sdownsj #include <sys/stat.h>
425ac2d602Sdownsj #include <sys/buf.h>
435ac2d602Sdownsj #include <sys/mount.h>
445ac2d602Sdownsj #include <sys/vnode.h>
455ac2d602Sdownsj #include <sys/malloc.h>
465ac2d602Sdownsj #include <sys/signalvar.h>
47b78c6981Smillert #include <sys/event.h>
485ac2d602Sdownsj
495ac2d602Sdownsj #include <ufs/ufs/quota.h>
50ea0c9824Spelikan #include <ufs/ufs/ufsmount.h>
515ac2d602Sdownsj #include <ufs/ufs/inode.h>
525ac2d602Sdownsj #include <ufs/ext2fs/ext2fs.h>
535ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_extern.h>
545ac2d602Sdownsj
555ac2d602Sdownsj
561217b0a4Spelikan static int ext2_ind_read(struct vnode *, struct inode *, struct m_ext2fs *, struct uio *);
57ea0c9824Spelikan static int ext4_ext_read(struct vnode *, struct inode *, struct m_ext2fs *, struct uio *);
581217b0a4Spelikan
595ac2d602Sdownsj /*
605ac2d602Sdownsj * Vnode op for reading.
615ac2d602Sdownsj */
625ac2d602Sdownsj int
ext2fs_read(void * v)635f64cd9cSjasper ext2fs_read(void *v)
645ac2d602Sdownsj {
6599bc9d31Sderaadt struct vop_read_args *ap = v;
664be9887dSart struct vnode *vp;
674be9887dSart struct inode *ip;
684be9887dSart struct uio *uio;
694be9887dSart struct m_ext2fs *fs;
701217b0a4Spelikan
711217b0a4Spelikan vp = ap->a_vp;
721217b0a4Spelikan ip = VTOI(vp);
731217b0a4Spelikan uio = ap->a_uio;
741217b0a4Spelikan fs = ip->i_e2fs;
751217b0a4Spelikan
76ea0c9824Spelikan if (ip->i_e2fs_flags & EXT4_EXTENTS)
77ea0c9824Spelikan return ext4_ext_read(vp, ip, fs, uio);
78ea0c9824Spelikan else
791217b0a4Spelikan return ext2_ind_read(vp, ip, fs, uio);
801217b0a4Spelikan }
811217b0a4Spelikan
821217b0a4Spelikan static int
ext2_ind_read(struct vnode * vp,struct inode * ip,struct m_ext2fs * fs,struct uio * uio)831217b0a4Spelikan ext2_ind_read(struct vnode *vp, struct inode *ip, struct m_ext2fs *fs,
841217b0a4Spelikan struct uio *uio)
851217b0a4Spelikan {
865ac2d602Sdownsj struct buf *bp;
871abdbfdeSderaadt daddr_t lbn, nextlbn;
885ac2d602Sdownsj off_t bytesinfile;
89dc30eddbSstefan int size, xfersize, blkoffset;
905ac2d602Sdownsj int error;
915ac2d602Sdownsj
925ac2d602Sdownsj #ifdef DIAGNOSTIC
935ac2d602Sdownsj if (uio->uio_rw != UIO_READ)
945ac2d602Sdownsj panic("%s: mode", "ext2fs_read");
955ac2d602Sdownsj
965ac2d602Sdownsj if (vp->v_type == VLNK) {
973484f39eSnatano if (ext2fs_size(ip) < EXT2_MAXSYMLINKLEN)
985ac2d602Sdownsj panic("%s: short symlink", "ext2fs_read");
995ac2d602Sdownsj } else if (vp->v_type != VREG && vp->v_type != VDIR)
1005ac2d602Sdownsj panic("%s: type %d", "ext2fs_read", vp->v_type);
1015ac2d602Sdownsj #endif
10213f30d2fSnatano if (uio->uio_offset < 0)
10313f30d2fSnatano return (EINVAL);
1041f3ff51cSdownsj if (uio->uio_resid == 0)
1051f3ff51cSdownsj return (0);
1065ac2d602Sdownsj
1075ac2d602Sdownsj for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1084dcbbbb0Sniallo if ((bytesinfile = ext2fs_size(ip) - uio->uio_offset) <= 0)
1095ac2d602Sdownsj break;
1105ac2d602Sdownsj lbn = lblkno(fs, uio->uio_offset);
1115ac2d602Sdownsj nextlbn = lbn + 1;
1125ac2d602Sdownsj size = fs->e2fs_bsize;
1135ac2d602Sdownsj blkoffset = blkoff(fs, uio->uio_offset);
1145ac2d602Sdownsj xfersize = fs->e2fs_bsize - blkoffset;
1155ac2d602Sdownsj if (uio->uio_resid < xfersize)
1165ac2d602Sdownsj xfersize = uio->uio_resid;
1175ac2d602Sdownsj if (bytesinfile < xfersize)
1185ac2d602Sdownsj xfersize = bytesinfile;
1195ac2d602Sdownsj
1204dcbbbb0Sniallo if (lblktosize(fs, nextlbn) >= ext2fs_size(ip))
12193f62a9eStedu error = bread(vp, lbn, size, &bp);
1225a0a814bScsapuntz else if (lbn - 1 == ip->i_ci.ci_lastr) {
1235ac2d602Sdownsj int nextsize = fs->e2fs_bsize;
12493f62a9eStedu error = breadn(vp, lbn, size, &nextlbn, &nextsize,
12593f62a9eStedu 1, &bp);
1265ac2d602Sdownsj } else
12793f62a9eStedu error = bread(vp, lbn, size, &bp);
1285ac2d602Sdownsj if (error)
1295ac2d602Sdownsj break;
1305a0a814bScsapuntz ip->i_ci.ci_lastr = lbn;
1315ac2d602Sdownsj
1325ac2d602Sdownsj /*
1335ac2d602Sdownsj * We should only get non-zero b_resid when an I/O error
1345ac2d602Sdownsj * has occurred, which should cause us to break above.
1355ac2d602Sdownsj * However, if the short read did not cause an error,
1365ac2d602Sdownsj * then we want to ensure that we do not uiomove bad
1375ac2d602Sdownsj * or uninitialized data.
1385ac2d602Sdownsj */
1395ac2d602Sdownsj size -= bp->b_resid;
1405ac2d602Sdownsj if (size < xfersize) {
1415ac2d602Sdownsj if (size == 0)
1425ac2d602Sdownsj break;
1435ac2d602Sdownsj xfersize = size;
1445ac2d602Sdownsj }
145dc30eddbSstefan error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
1465ac2d602Sdownsj if (error)
1475ac2d602Sdownsj break;
1485ac2d602Sdownsj brelse(bp);
1495ac2d602Sdownsj }
1505ac2d602Sdownsj if (bp != NULL)
1515ac2d602Sdownsj brelse(bp);
1524be9887dSart
1534be9887dSart if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
1545ac2d602Sdownsj ip->i_flag |= IN_ACCESS;
1554be9887dSart }
1565ac2d602Sdownsj return (error);
1575ac2d602Sdownsj }
1585ac2d602Sdownsj
159ea0c9824Spelikan int
ext4_ext_read(struct vnode * vp,struct inode * ip,struct m_ext2fs * fs,struct uio * uio)160ea0c9824Spelikan ext4_ext_read(struct vnode *vp, struct inode *ip, struct m_ext2fs *fs, struct uio *uio)
161ea0c9824Spelikan {
162ea0c9824Spelikan struct ext4_extent_path path;
163ea0c9824Spelikan struct ext4_extent nex, *ep;
164ea0c9824Spelikan struct buf *bp;
165ea0c9824Spelikan daddr_t lbn, pos;
166ea0c9824Spelikan off_t bytesinfile;
167dc30eddbSstefan int size, xfersize, blkoffset;
168ea0c9824Spelikan int error, cache_type;
169ea0c9824Spelikan
170ea0c9824Spelikan memset(&path, 0, sizeof path);
171ea0c9824Spelikan
17213f30d2fSnatano if (uio->uio_offset < 0)
17313f30d2fSnatano return (EINVAL);
17413f30d2fSnatano if (uio->uio_resid == 0)
175ea0c9824Spelikan return (0);
176ea0c9824Spelikan
177ea0c9824Spelikan while (uio->uio_resid > 0) {
178ea0c9824Spelikan if ((bytesinfile = ext2fs_size(ip) - uio->uio_offset) <= 0)
179ea0c9824Spelikan break;
180ea0c9824Spelikan lbn = lblkno(fs, uio->uio_offset);
181ea0c9824Spelikan size = fs->e2fs_bsize;
182ea0c9824Spelikan blkoffset = blkoff(fs, uio->uio_offset);
183ea0c9824Spelikan
184ea0c9824Spelikan xfersize = fs->e2fs_fsize - blkoffset;
185ea0c9824Spelikan xfersize = MIN(xfersize, uio->uio_resid);
186ea0c9824Spelikan xfersize = MIN(xfersize, bytesinfile);
187ea0c9824Spelikan
188ea0c9824Spelikan cache_type = ext4_ext_in_cache(ip, lbn, &nex);
189ea0c9824Spelikan switch (cache_type) {
190ea0c9824Spelikan case EXT4_EXT_CACHE_NO:
191ea0c9824Spelikan ext4_ext_find_extent(fs, ip, lbn, &path);
192ea0c9824Spelikan if ((ep = path.ep_ext) == NULL)
193ea0c9824Spelikan return (EIO);
194ea0c9824Spelikan ext4_ext_put_cache(ip, ep, EXT4_EXT_CACHE_IN);
195ea0c9824Spelikan
196ea0c9824Spelikan pos = lbn - ep->e_blk + (((daddr_t) ep->e_start_hi << 32) | ep->e_start_lo);
197ea0c9824Spelikan if (path.ep_bp != NULL) {
198ea0c9824Spelikan brelse(path.ep_bp);
199ea0c9824Spelikan path.ep_bp = NULL;
200ea0c9824Spelikan }
201ea0c9824Spelikan break;
202ea0c9824Spelikan case EXT4_EXT_CACHE_GAP:
203ea0c9824Spelikan /* block has not been allocated yet */
204ea0c9824Spelikan return (0);
205ea0c9824Spelikan case EXT4_EXT_CACHE_IN:
206ea0c9824Spelikan pos = lbn - nex.e_blk + (((daddr_t) nex.e_start_hi << 32) | nex.e_start_lo);
207ea0c9824Spelikan break;
208ea0c9824Spelikan }
209ea0c9824Spelikan error = bread(ip->i_devvp, fsbtodb(fs, pos), size, &bp);
210ea0c9824Spelikan if (error) {
211ea0c9824Spelikan brelse(bp);
212ea0c9824Spelikan return (error);
213ea0c9824Spelikan }
214ea0c9824Spelikan size -= bp->b_resid;
215ea0c9824Spelikan if (size < xfersize) {
216ea0c9824Spelikan if (size == 0) {
217ea0c9824Spelikan brelse(bp);
218ea0c9824Spelikan break;
219ea0c9824Spelikan }
220ea0c9824Spelikan xfersize = size;
221ea0c9824Spelikan }
222dc30eddbSstefan error = uiomove(bp->b_data + blkoffset, xfersize, uio);
223ea0c9824Spelikan brelse(bp);
224ea0c9824Spelikan if (error)
225ea0c9824Spelikan return (error);
226ea0c9824Spelikan }
227ea0c9824Spelikan return (0);
228ea0c9824Spelikan }
229ea0c9824Spelikan
2305ac2d602Sdownsj /*
2315ac2d602Sdownsj * Vnode op for writing.
2325ac2d602Sdownsj */
2335ac2d602Sdownsj int
ext2fs_write(void * v)2345f64cd9cSjasper ext2fs_write(void *v)
2355ac2d602Sdownsj {
23699bc9d31Sderaadt struct vop_write_args *ap = v;
2374be9887dSart struct vnode *vp;
2384be9887dSart struct uio *uio;
2394be9887dSart struct inode *ip;
2404be9887dSart struct m_ext2fs *fs;
2415ac2d602Sdownsj struct buf *bp;
2421dfb1939Spedro int32_t lbn;
2435ac2d602Sdownsj off_t osize;
244b78c6981Smillert int blkoffset, error, extended, flags, ioflag, size, xfersize;
245dc30eddbSstefan size_t resid;
246dc30eddbSstefan ssize_t overrun;
2475ac2d602Sdownsj
248b78c6981Smillert extended = 0;
2495ac2d602Sdownsj ioflag = ap->a_ioflag;
2505ac2d602Sdownsj uio = ap->a_uio;
2515ac2d602Sdownsj vp = ap->a_vp;
2525ac2d602Sdownsj ip = VTOI(vp);
2535ac2d602Sdownsj
2545ac2d602Sdownsj #ifdef DIAGNOSTIC
2555ac2d602Sdownsj if (uio->uio_rw != UIO_WRITE)
2565ac2d602Sdownsj panic("%s: mode", "ext2fs_write");
2575ac2d602Sdownsj #endif
2585ac2d602Sdownsj
2593c1ca557Stholo /*
2603c1ca557Stholo * If writing 0 bytes, succeed and do not change
2613c1ca557Stholo * update time or file offset (standards compliance)
2623c1ca557Stholo */
2633c1ca557Stholo if (uio->uio_resid == 0)
2643c1ca557Stholo return (0);
2653c1ca557Stholo
2665ac2d602Sdownsj switch (vp->v_type) {
2675ac2d602Sdownsj case VREG:
2685ac2d602Sdownsj if (ioflag & IO_APPEND)
2694dcbbbb0Sniallo uio->uio_offset = ext2fs_size(ip);
2705ac2d602Sdownsj if ((ip->i_e2fs_flags & EXT2_APPEND) &&
2714dcbbbb0Sniallo uio->uio_offset != ext2fs_size(ip))
2725ac2d602Sdownsj return (EPERM);
2735ac2d602Sdownsj /* FALLTHROUGH */
2745ac2d602Sdownsj case VLNK:
2755ac2d602Sdownsj break;
2765ac2d602Sdownsj case VDIR:
2775ac2d602Sdownsj if ((ioflag & IO_SYNC) == 0)
2785ac2d602Sdownsj panic("%s: nonsync dir write", "ext2fs_write");
2795ac2d602Sdownsj break;
2805ac2d602Sdownsj default:
2815ac2d602Sdownsj panic("%s: type", "ext2fs_write");
2825ac2d602Sdownsj }
2835ac2d602Sdownsj
2845ac2d602Sdownsj fs = ip->i_e2fs;
285c7fd3f62Spelikan if (e2fs_overflow(fs, uio->uio_resid, uio->uio_offset + uio->uio_resid))
2865ac2d602Sdownsj return (EFBIG);
287543c93a8Sguenther
288543c93a8Sguenther /* do the filesize rlimit check */
289543c93a8Sguenther if ((error = vn_fsizechk(vp, uio, ioflag, &overrun)))
290543c93a8Sguenther return (error);
2915ac2d602Sdownsj
2925ac2d602Sdownsj resid = uio->uio_resid;
2934dcbbbb0Sniallo osize = ext2fs_size(ip);
2945af79db2Sart flags = ioflag & IO_SYNC ? B_SYNC : 0;
2951414b0faSart
2965ac2d602Sdownsj for (error = 0; uio->uio_resid > 0;) {
2975ac2d602Sdownsj lbn = lblkno(fs, uio->uio_offset);
2985ac2d602Sdownsj blkoffset = blkoff(fs, uio->uio_offset);
2991414b0faSart xfersize = fs->e2fs_bsize - blkoffset;
3001414b0faSart if (uio->uio_resid < xfersize)
3011414b0faSart xfersize = uio->uio_resid;
3021414b0faSart if (fs->e2fs_bsize > xfersize)
3035ac2d602Sdownsj flags |= B_CLRBUF;
3045ac2d602Sdownsj else
3055ac2d602Sdownsj flags &= ~B_CLRBUF;
3061414b0faSart
307b080ad39Scsapuntz error = ext2fs_buf_alloc(ip,
3085ac2d602Sdownsj lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
3095ac2d602Sdownsj if (error)
3105ac2d602Sdownsj break;
3114dcbbbb0Sniallo if (uio->uio_offset + xfersize > ext2fs_size(ip)) {
3124dcbbbb0Sniallo error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
3134dcbbbb0Sniallo if (error)
3144dcbbbb0Sniallo break;
315392d10d7Smillert uvm_vnp_setsize(vp, ext2fs_size(ip));
316b78c6981Smillert extended = 1;
3175ac2d602Sdownsj }
3181414b0faSart uvm_vnp_uncache(vp);
3191414b0faSart
3201414b0faSart size = fs->e2fs_bsize - bp->b_resid;
3211414b0faSart if (size < xfersize)
3221414b0faSart xfersize = size;
3231414b0faSart
324b78c6981Smillert error = uiomove(bp->b_data + blkoffset, xfersize, uio);
325ad4c60c0Smillert /*
326ad4c60c0Smillert * If the buffer is not already filled and we encounter an
327ad4c60c0Smillert * error while trying to fill it, we have to clear out any
328ad4c60c0Smillert * garbage data from the pages instantiated for the buffer.
329ad4c60c0Smillert * If we do not, a failed uiomove() during a write can leave
330ad4c60c0Smillert * the prior contents of the pages exposed to a userland mmap.
331ad4c60c0Smillert *
332ad4c60c0Smillert * Note that we don't need to clear buffers that were
333ad4c60c0Smillert * allocated with the B_CLRBUF flag set.
334ad4c60c0Smillert */
335ad4c60c0Smillert if (error != 0 && !(flags & B_CLRBUF))
336ad4c60c0Smillert memset(bp->b_data + blkoffset, 0, xfersize);
337f62e8f74Sbeck
338e1405772Sbeck if (ioflag & IO_NOCACHE)
339e1405772Sbeck bp->b_flags |= B_NOCACHE;
340f62e8f74Sbeck
3415ac2d602Sdownsj if (ioflag & IO_SYNC)
3425ac2d602Sdownsj (void)bwrite(bp);
343cfc6a9edSmpi else if (xfersize + blkoffset == fs->e2fs_bsize)
3445ac2d602Sdownsj bawrite(bp);
345cfc6a9edSmpi else
3465ac2d602Sdownsj bdwrite(bp);
3475ac2d602Sdownsj if (error || xfersize == 0)
3485ac2d602Sdownsj break;
3491414b0faSart ip->i_flag |= IN_CHANGE | IN_UPDATE;
3505ac2d602Sdownsj }
3515ac2d602Sdownsj /*
3525ac2d602Sdownsj * If we successfully wrote any data, and we are not the superuser
3535ac2d602Sdownsj * we clear the setuid and setgid bits as a precaution against
3545ac2d602Sdownsj * tampering.
3555ac2d602Sdownsj */
3565ac2d602Sdownsj if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
3575ac2d602Sdownsj ip->i_e2fs_mode &= ~(ISUID | ISGID);
358b78c6981Smillert if (resid > uio->uio_resid)
359b78c6981Smillert VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
3605ac2d602Sdownsj if (error) {
3615ac2d602Sdownsj if (ioflag & IO_UNIT) {
362b080ad39Scsapuntz (void)ext2fs_truncate(ip, osize,
363b080ad39Scsapuntz ioflag & IO_SYNC, ap->a_cred);
3645ac2d602Sdownsj uio->uio_offset -= resid - uio->uio_resid;
3655ac2d602Sdownsj uio->uio_resid = resid;
3665ac2d602Sdownsj }
3671414b0faSart } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
368d4fc1c49Sguenther error = ext2fs_update(ip, 1);
3691414b0faSart }
370543c93a8Sguenther /* correct the result for writes clamped by vn_fsizechk() */
371543c93a8Sguenther uio->uio_resid += overrun;
3725ac2d602Sdownsj return (error);
3735ac2d602Sdownsj }
374