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