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