ext2fs_readwrite.c revision 1.46 1 /* $NetBSD: ext2fs_readwrite.c,v 1.46 2007/04/19 11:05:14 yamt 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
64 * Modified for ext2fs by Manuel Bouyer.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.46 2007/04/19 11:05:14 yamt Exp $");
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/resourcevar.h>
73 #include <sys/kernel.h>
74 #include <sys/file.h>
75 #include <sys/stat.h>
76 #include <sys/buf.h>
77 #include <sys/proc.h>
78 #include <sys/mount.h>
79 #include <sys/vnode.h>
80 #include <sys/malloc.h>
81 #include <sys/signalvar.h>
82 #include <sys/kauth.h>
83
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufsmount.h>
86 #include <ufs/ufs/ufs_extern.h>
87 #include <ufs/ext2fs/ext2fs.h>
88 #include <ufs/ext2fs/ext2fs_extern.h>
89
90
91 #define doclusterread 0 /* XXX underway */
92 #define doclusterwrite 0
93
94 /*
95 * Vnode op for reading.
96 */
97 /* ARGSUSED */
98 int
99 ext2fs_read(void *v)
100 {
101 struct vop_read_args /* {
102 struct vnode *a_vp;
103 struct uio *a_uio;
104 int a_ioflag;
105 kauth_cred_t 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 struct ufsmount *ump;
113 void *win;
114 vsize_t bytelen;
115 daddr_t lbn, nextlbn;
116 off_t bytesinfile;
117 long size, xfersize, blkoffset;
118 int error, flags;
119
120 vp = ap->a_vp;
121 ip = VTOI(vp);
122 ump = ip->i_ump;
123 uio = ap->a_uio;
124 error = 0;
125
126 #ifdef DIAGNOSTIC
127 if (uio->uio_rw != UIO_READ)
128 panic("%s: mode", "ext2fs_read");
129
130 if (vp->v_type == VLNK) {
131 if (ext2fs_size(ip) < ump->um_maxsymlinklen ||
132 (ump->um_maxsymlinklen == 0 && 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 > ump->um_maxfilesize)
139 return (EFBIG);
140 if (uio->uio_resid == 0)
141 return (0);
142 if (uio->uio_offset >= ext2fs_size(ip))
143 goto out;
144
145 if (vp->v_type == VREG) {
146 const int advice = IO_ADV_DECODE(ap->a_ioflag);
147
148 while (uio->uio_resid > 0) {
149 bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
150 uio->uio_resid);
151 if (bytelen == 0)
152 break;
153
154 win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
155 &bytelen, advice, UBC_READ);
156 error = uiomove(win, bytelen, uio);
157 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
158 ubc_release(win, flags);
159 if (error)
160 break;
161 }
162 goto out;
163 }
164
165 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
166 bytesinfile = ext2fs_size(ip) - uio->uio_offset;
167 if (bytesinfile <= 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) >= ext2fs_size(ip))
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 = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
215 }
216 return (error);
217 }
218
219 /*
220 * Vnode op for writing.
221 */
222 int
223 ext2fs_write(void *v)
224 {
225 struct vop_write_args /* {
226 struct vnode *a_vp;
227 struct uio *a_uio;
228 int a_ioflag;
229 kauth_cred_t a_cred;
230 } */ *ap = v;
231 struct vnode *vp;
232 struct uio *uio;
233 struct inode *ip;
234 struct m_ext2fs *fs;
235 struct buf *bp;
236 struct proc *p;
237 struct ufsmount *ump;
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 = 0; /* XXX */
244 bool 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 ump = ip->i_ump;
252 error = 0;
253
254 #ifdef DIAGNOSTIC
255 if (uio->uio_rw != UIO_WRITE)
256 panic("%s: mode", "ext2fs_write");
257 #endif
258
259 switch (vp->v_type) {
260 case VREG:
261 if (ioflag & IO_APPEND)
262 uio->uio_offset = ext2fs_size(ip);
263 if ((ip->i_e2fs_flags & EXT2_APPEND) &&
264 uio->uio_offset != ext2fs_size(ip))
265 return (EPERM);
266 /* FALLTHROUGH */
267 case VLNK:
268 break;
269 case VDIR:
270 if ((ioflag & IO_SYNC) == 0)
271 panic("%s: nonsync dir write", "ext2fs_write");
272 break;
273 default:
274 panic("%s: type", "ext2fs_write");
275 }
276
277 fs = ip->i_e2fs;
278 if (uio->uio_offset < 0 ||
279 (u_int64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
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 = curproc;
286 if (vp->v_type == VREG && p &&
287 uio->uio_offset + uio->uio_resid >
288 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
289 mutex_enter(&proclist_mutex);
290 psignal(p, SIGXFSZ);
291 mutex_exit(&proclist_mutex);
292 return (EFBIG);
293 }
294 if (uio->uio_resid == 0)
295 return (0);
296
297 async = vp->v_mount->mnt_flag & MNT_ASYNC;
298 resid = uio->uio_resid;
299 osize = ext2fs_size(ip);
300
301 if (vp->v_type == VREG) {
302 while (uio->uio_resid > 0) {
303 oldoff = uio->uio_offset;
304 blkoffset = blkoff(fs, uio->uio_offset);
305 bytelen = MIN(fs->e2fs_bsize - blkoffset,
306 uio->uio_resid);
307
308 error = ufs_balloc_range(vp, uio->uio_offset,
309 bytelen, ap->a_cred, 0);
310 if (error)
311 break;
312 win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
313 &bytelen, UVM_ADV_NORMAL, UBC_WRITE);
314 error = uiomove(win, bytelen, uio);
315 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
316 ubc_release(win, flags);
317 if (error)
318 break;
319
320 /*
321 * update UVM's notion of the size now that we've
322 * copied the data into the vnode's pages.
323 */
324
325 if (vp->v_size < uio->uio_offset) {
326 uvm_vnp_setsize(vp, uio->uio_offset);
327 extended = 1;
328 }
329
330 /*
331 * flush what we just wrote if necessary.
332 * XXXUBC simplistic async flushing.
333 */
334
335 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
336 simple_lock(&vp->v_interlock);
337 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
338 (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
339 }
340 }
341 if (error == 0 && ioflag & IO_SYNC) {
342 simple_lock(&vp->v_interlock);
343 error = VOP_PUTPAGES(vp, trunc_page(oldoff),
344 round_page(blkroundup(fs, uio->uio_offset)),
345 PGO_CLEANIT | PGO_SYNCIO);
346 }
347
348 goto out;
349 }
350
351 flags = ioflag & IO_SYNC ? B_SYNC : 0;
352 for (error = 0; uio->uio_resid > 0;) {
353 lbn = lblkno(fs, uio->uio_offset);
354 blkoffset = blkoff(fs, uio->uio_offset);
355 xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
356 if (xfersize < fs->e2fs_bsize)
357 flags |= B_CLRBUF;
358 else
359 flags &= ~B_CLRBUF;
360 error = ext2fs_balloc(ip,
361 lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
362 if (error)
363 break;
364 if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
365 error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
366 if (error)
367 break;
368 }
369 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
370
371 /*
372 * update UVM's notion of the size now that we've
373 * copied the data into the vnode's pages.
374 */
375
376 if (vp->v_size < uio->uio_offset) {
377 uvm_vnp_setsize(vp, uio->uio_offset);
378 extended = 1;
379 }
380
381 if (ioflag & IO_SYNC)
382 (void)bwrite(bp);
383 else if (xfersize + blkoffset == fs->e2fs_bsize)
384 bawrite(bp);
385 else
386 bdwrite(bp);
387 if (error || xfersize == 0)
388 break;
389 }
390
391 /*
392 * If we successfully wrote any data, and we are not the superuser
393 * we clear the setuid and setgid bits as a precaution against
394 * tampering.
395 */
396
397 out:
398 ip->i_flag |= IN_CHANGE | IN_UPDATE;
399 if (resid > uio->uio_resid && ap->a_cred &&
400 kauth_authorize_generic(ap->a_cred, KAUTH_GENERIC_ISSUSER, NULL))
401 ip->i_e2fs_mode &= ~(ISUID | ISGID);
402 if (resid > uio->uio_resid)
403 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
404 if (error) {
405 (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred,
406 p);
407 uio->uio_offset -= resid - uio->uio_resid;
408 uio->uio_resid = resid;
409 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
410 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
411 KASSERT(vp->v_size == ext2fs_size(ip));
412 return (error);
413 }
414