xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_readwrite.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: ext2fs_readwrite.c,v 1.31 2003/10/05 17:48:49 bouyer 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  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by Manuel Bouyer.
49  * 4. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
65  * Modified for ext2fs by Manuel Bouyer.
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.31 2003/10/05 17:48:49 bouyer Exp $");
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/resourcevar.h>
74 #include <sys/kernel.h>
75 #include <sys/file.h>
76 #include <sys/stat.h>
77 #include <sys/buf.h>
78 #include <sys/proc.h>
79 #include <sys/mount.h>
80 #include <sys/vnode.h>
81 #include <sys/malloc.h>
82 #include <sys/signalvar.h>
83 
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufs_extern.h>
86 #include <ufs/ext2fs/ext2fs.h>
87 #include <ufs/ext2fs/ext2fs_extern.h>
88 
89 
90 #define doclusterread 0 /* XXX underway */
91 #define doclusterwrite 0
92 
93 /*
94  * Vnode op for reading.
95  */
96 /* ARGSUSED */
97 int
98 ext2fs_read(v)
99 	void *v;
100 {
101 	struct vop_read_args /* {
102 		struct vnode *a_vp;
103 		struct uio *a_uio;
104 		int a_ioflag;
105 		struct ucred *a_cred;
106 	} */ *ap = v;
107 	struct vnode *vp;
108 	struct inode *ip;
109 	struct uio *uio;
110 	struct m_ext2fs *fs;
111 	struct buf *bp;
112 	void *win;
113 	vsize_t bytelen;
114 	daddr_t lbn, nextlbn;
115 	off_t bytesinfile;
116 	long size, xfersize, blkoffset;
117 	int error;
118 
119 	vp = ap->a_vp;
120 	ip = VTOI(vp);
121 	uio = ap->a_uio;
122 
123 #ifdef DIAGNOSTIC
124 	if (uio->uio_rw != UIO_READ)
125 		panic("%s: mode", "ext2fs_read");
126 
127 	if (vp->v_type == VLNK) {
128 		if ((int)ip->i_e2fs_size < vp->v_mount->mnt_maxsymlinklen ||
129 			(vp->v_mount->mnt_maxsymlinklen == 0 &&
130 			 ip->i_e2fs_nblock == 0))
131 			panic("%s: short symlink", "ext2fs_read");
132 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
133 		panic("%s: type %d", "ext2fs_read", vp->v_type);
134 #endif
135 	fs = ip->i_e2fs;
136 	if ((u_int64_t)uio->uio_offset >
137 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
138 		return (EFBIG);
139 	if (uio->uio_resid == 0)
140 		return (0);
141 	if (uio->uio_offset >= ip->i_e2fs_size)
142 		return (0);
143 
144 	if (vp->v_type == VREG) {
145 		error = 0;
146 		while (uio->uio_resid > 0) {
147 			bytelen = MIN(ip->i_e2fs_size - uio->uio_offset,
148 			    uio->uio_resid);
149 
150 			if (bytelen == 0) {
151 				break;
152 			}
153 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
154 			    &bytelen, UBC_READ);
155 			error = uiomove(win, bytelen, uio);
156 			ubc_release(win, 0);
157 			if (error) {
158 				break;
159 			}
160 		}
161 		goto out;
162 	}
163 
164 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
165 		if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
166 			break;
167 		lbn = lblkno(fs, uio->uio_offset);
168 		nextlbn = lbn + 1;
169 		size = fs->e2fs_bsize;
170 		blkoffset = blkoff(fs, uio->uio_offset);
171 		xfersize = fs->e2fs_bsize - blkoffset;
172 		if (uio->uio_resid < xfersize)
173 			xfersize = uio->uio_resid;
174 		if (bytesinfile < xfersize)
175 			xfersize = bytesinfile;
176 
177 		if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
178 			error = bread(vp, lbn, size, NOCRED, &bp);
179 		else {
180 			int nextsize = fs->e2fs_bsize;
181 			error = breadn(vp, lbn,
182 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
183 		}
184 		if (error)
185 			break;
186 
187 		/*
188 		 * We should only get non-zero b_resid when an I/O error
189 		 * has occurred, which should cause us to break above.
190 		 * However, if the short read did not cause an error,
191 		 * then we want to ensure that we do not uiomove bad
192 		 * or uninitialized data.
193 		 */
194 		size -= bp->b_resid;
195 		if (size < xfersize) {
196 			if (size == 0)
197 				break;
198 			xfersize = size;
199 		}
200 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
201 		if (error)
202 			break;
203 		brelse(bp);
204 	}
205 	if (bp != NULL)
206 		brelse(bp);
207 
208 out:
209 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
210 		ip->i_flag |= IN_ACCESS;
211 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
212 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
213 	}
214 	return (error);
215 }
216 
217 /*
218  * Vnode op for writing.
219  */
220 int
221 ext2fs_write(v)
222 	void *v;
223 {
224 	struct vop_write_args /* {
225 		struct vnode *a_vp;
226 		struct uio *a_uio;
227 		int a_ioflag;
228 		struct ucred *a_cred;
229 	} */ *ap = v;
230 	struct vnode *vp;
231 	struct uio *uio;
232 	struct inode *ip;
233 	struct m_ext2fs *fs;
234 	struct buf *bp;
235 	struct proc *p;
236 	daddr_t lbn;
237 	off_t osize;
238 	int blkoffset, error, flags, ioflag, resid, xfersize;
239 	vsize_t bytelen;
240 	void *win;
241 	off_t oldoff;
242 	boolean_t async;
243 	int extended=0;
244 
245 	ioflag = ap->a_ioflag;
246 	uio = ap->a_uio;
247 	vp = ap->a_vp;
248 	ip = VTOI(vp);
249 	error = 0;
250 
251 #ifdef DIAGNOSTIC
252 	if (uio->uio_rw != UIO_WRITE)
253 		panic("%s: mode", "ext2fs_write");
254 #endif
255 
256 	switch (vp->v_type) {
257 	case VREG:
258 		if (ioflag & IO_APPEND)
259 			uio->uio_offset = ip->i_e2fs_size;
260 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
261 			uio->uio_offset != ip->i_e2fs_size)
262 			return (EPERM);
263 		/* FALLTHROUGH */
264 	case VLNK:
265 		break;
266 	case VDIR:
267 		if ((ioflag & IO_SYNC) == 0)
268 			panic("%s: nonsync dir write", "ext2fs_write");
269 		break;
270 	default:
271 		panic("%s: type", "ext2fs_write");
272 	}
273 
274 	fs = ip->i_e2fs;
275 	if (uio->uio_offset < 0 ||
276 		(u_int64_t)uio->uio_offset + uio->uio_resid >
277 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
278 		return (EFBIG);
279 	/*
280 	 * Maybe this should be above the vnode op call, but so long as
281 	 * file servers have no limits, I don't think it matters.
282 	 */
283 	p = uio->uio_procp;
284 	if (vp->v_type == VREG && p &&
285 		uio->uio_offset + uio->uio_resid >
286 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
287 		psignal(p, SIGXFSZ);
288 		return (EFBIG);
289 	}
290 	if (uio->uio_resid == 0)
291 		return (0);
292 
293 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
294 	resid = uio->uio_resid;
295 	osize = ip->i_e2fs_size;
296 
297 	if (vp->v_type == VREG) {
298 		while (uio->uio_resid > 0) {
299 			oldoff = uio->uio_offset;
300 			blkoffset = blkoff(fs, uio->uio_offset);
301 			bytelen = MIN(fs->e2fs_bsize - blkoffset,
302 			    uio->uio_resid);
303 
304 			error = ufs_balloc_range(vp, uio->uio_offset,
305 			    bytelen, ap->a_cred, 0);
306 			if (error) {
307 				break;
308 			}
309 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
310 			    &bytelen, UBC_WRITE);
311 			error = uiomove(win, bytelen, uio);
312 			ubc_release(win, 0);
313 			if (error) {
314 				break;
315 			}
316 
317 			/*
318 			 * update UVM's notion of the size now that we've
319 			 * copied the data into the vnode's pages.
320 			 */
321 
322 			if (vp->v_size < uio->uio_offset) {
323 				uvm_vnp_setsize(vp, uio->uio_offset);
324 				extended = 1;
325 			}
326 
327 			/*
328 			 * flush what we just wrote if necessary.
329 			 * XXXUBC simplistic async flushing.
330 			 */
331 
332 			if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
333 				simple_lock(&vp->v_interlock);
334 				error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
335 				    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
336 			}
337 		}
338 		if (error == 0 && ioflag & IO_SYNC) {
339 			simple_lock(&vp->v_interlock);
340 			error = VOP_PUTPAGES(vp, trunc_page(oldoff),
341 			    round_page(blkroundup(fs, uio->uio_offset)),
342 			    PGO_CLEANIT | PGO_SYNCIO);
343 		}
344 
345 		goto out;
346 	}
347 
348 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
349 	for (error = 0; uio->uio_resid > 0;) {
350 		lbn = lblkno(fs, uio->uio_offset);
351 		blkoffset = blkoff(fs, uio->uio_offset);
352 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
353 		if (xfersize < fs->e2fs_bsize)
354 			flags |= B_CLRBUF;
355 		else
356 			flags &= ~B_CLRBUF;
357 		error = ext2fs_balloc(ip,
358 		    lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
359 		if (error)
360 			break;
361 		if (ip->i_e2fs_size < uio->uio_offset + xfersize) {
362 			ip->i_e2fs_size = uio->uio_offset + xfersize;
363 		}
364 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
365 
366 		/*
367 		 * update UVM's notion of the size now that we've
368 		 * copied the data into the vnode's pages.
369 		 */
370 
371 		if (vp->v_size < uio->uio_offset) {
372 			uvm_vnp_setsize(vp, uio->uio_offset);
373 			extended = 1;
374 		}
375 
376 		if (ioflag & IO_SYNC)
377 			(void)bwrite(bp);
378 		else if (xfersize + blkoffset == fs->e2fs_bsize)
379 			bawrite(bp);
380 		else
381 			bdwrite(bp);
382 		if (error || xfersize == 0)
383 			break;
384 	}
385 
386 	/*
387 	 * If we successfully wrote any data, and we are not the superuser
388 	 * we clear the setuid and setgid bits as a precaution against
389 	 * tampering.
390 	 */
391 
392 out:
393 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
394 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
395 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
396 	if (resid > uio->uio_resid)
397 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
398 	if (error) {
399 		(void) VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred,
400 		    uio->uio_procp);
401 		uio->uio_offset -= resid - uio->uio_resid;
402 		uio->uio_resid = resid;
403 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
404 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
405 	KASSERT(vp->v_size == ip->i_e2fs_size);
406 	return (error);
407 }
408