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