ext2fs_readwrite.c revision 1.6 1 /* $NetBSD: ext2fs_readwrite.c,v 1.6 1998/03/01 02:23:46 fvdl 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_uvm.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 #if defined(UVM)
58 #include <uvm/uvm_extern.h>
59 #endif
60
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ext2fs/ext2fs.h>
64 #include <ufs/ext2fs/ext2fs_extern.h>
65
66
67 #define doclusterread 0 /* XXX underway */
68 #define doclusterwrite 0
69
70 /*
71 * Vnode op for reading.
72 */
73 /* ARGSUSED */
74 int
75 ext2fs_read(v)
76 void *v;
77 {
78 struct vop_read_args /* {
79 struct vnode *a_vp;
80 struct uio *a_uio;
81 int a_ioflag;
82 struct ucred *a_cred;
83 } */ *ap = v;
84 register struct vnode *vp;
85 register struct inode *ip;
86 register struct uio *uio;
87 register struct m_ext2fs *fs;
88 struct buf *bp;
89 ufs_daddr_t lbn, nextlbn;
90 off_t bytesinfile;
91 long size, xfersize, blkoffset;
92 int error;
93
94 vp = ap->a_vp;
95 ip = VTOI(vp);
96 uio = ap->a_uio;
97
98 #ifdef DIAGNOSTIC
99 if (uio->uio_rw != UIO_READ)
100 panic("%s: mode", "ext2fs_read");
101
102 if (vp->v_type == VLNK) {
103 if ((int)ip->i_e2fs_size < vp->v_mount->mnt_maxsymlinklen ||
104 (vp->v_mount->mnt_maxsymlinklen == 0 &&
105 ip->i_e2fs_nblock == 0))
106 panic("%s: short symlink", "ext2fs_read");
107 } else if (vp->v_type != VREG && vp->v_type != VDIR)
108 panic("%s: type %d", "ext2fs_read", vp->v_type);
109 #endif
110 fs = ip->i_e2fs;
111 if ((u_int64_t)uio->uio_offset >
112 ((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
113 return (EFBIG);
114 if (uio->uio_resid == 0)
115 return (0);
116
117 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
118 if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
119 break;
120 lbn = lblkno(fs, uio->uio_offset);
121 nextlbn = lbn + 1;
122 size = fs->e2fs_bsize;
123 blkoffset = blkoff(fs, uio->uio_offset);
124 xfersize = fs->e2fs_bsize - blkoffset;
125 if (uio->uio_resid < xfersize)
126 xfersize = uio->uio_resid;
127 if (bytesinfile < xfersize)
128 xfersize = bytesinfile;
129
130 if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
131 error = bread(vp, lbn, size, NOCRED, &bp);
132 else if (doclusterread)
133 error = cluster_read(vp,
134 ip->i_e2fs_size, lbn, size, NOCRED, &bp);
135 else if (lbn - 1 == vp->v_lastr) {
136 int nextsize = fs->e2fs_bsize;
137 error = breadn(vp, lbn,
138 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
139 } else
140 error = bread(vp, lbn, size, NOCRED, &bp);
141 if (error)
142 break;
143 vp->v_lastr = lbn;
144
145 /*
146 * We should only get non-zero b_resid when an I/O error
147 * has occurred, which should cause us to break above.
148 * However, if the short read did not cause an error,
149 * then we want to ensure that we do not uiomove bad
150 * or uninitialized data.
151 */
152 size -= bp->b_resid;
153 if (size < xfersize) {
154 if (size == 0)
155 break;
156 xfersize = size;
157 }
158 error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize,
159 uio);
160 if (error)
161 break;
162 brelse(bp);
163 }
164 if (bp != NULL)
165 brelse(bp);
166 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
167 ip->i_flag |= IN_ACCESS;
168 return (error);
169 }
170
171 /*
172 * Vnode op for writing.
173 */
174 int
175 ext2fs_write(v)
176 void *v;
177 {
178 struct vop_write_args /* {
179 struct vnode *a_vp;
180 struct uio *a_uio;
181 int a_ioflag;
182 struct ucred *a_cred;
183 } */ *ap = v;
184 register struct vnode *vp;
185 register struct uio *uio;
186 register struct inode *ip;
187 register struct m_ext2fs *fs;
188 struct buf *bp;
189 struct proc *p;
190 ufs_daddr_t lbn;
191 off_t osize;
192 int blkoffset, error, flags, ioflag, resid, size, xfersize;
193 struct timespec ts;
194
195 ioflag = ap->a_ioflag;
196 uio = ap->a_uio;
197 vp = ap->a_vp;
198 ip = VTOI(vp);
199
200 #ifdef DIAGNOSTIC
201 if (uio->uio_rw != UIO_WRITE)
202 panic("%s: mode", "ext2fs_write");
203 #endif
204
205 switch (vp->v_type) {
206 case VREG:
207 if (ioflag & IO_APPEND)
208 uio->uio_offset = ip->i_e2fs_size;
209 if ((ip->i_e2fs_flags & EXT2_APPEND) &&
210 uio->uio_offset != ip->i_e2fs_size)
211 return (EPERM);
212 /* FALLTHROUGH */
213 case VLNK:
214 break;
215 case VDIR:
216 if ((ioflag & IO_SYNC) == 0)
217 panic("%s: nonsync dir write", "ext2fs_write");
218 break;
219 default:
220 panic("%s: type", "ext2fs_write");
221 }
222
223 fs = ip->i_e2fs;
224 if (uio->uio_offset < 0 ||
225 (u_int64_t)uio->uio_offset + uio->uio_resid >
226 ((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
227 return (EFBIG);
228 /*
229 * Maybe this should be above the vnode op call, but so long as
230 * file servers have no limits, I don't think it matters.
231 */
232 p = uio->uio_procp;
233 if (vp->v_type == VREG && p &&
234 uio->uio_offset + uio->uio_resid >
235 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
236 psignal(p, SIGXFSZ);
237 return (EFBIG);
238 }
239
240 resid = uio->uio_resid;
241 osize = ip->i_e2fs_size;
242 flags = ioflag & IO_SYNC ? B_SYNC : 0;
243
244 for (error = 0; uio->uio_resid > 0;) {
245 lbn = lblkno(fs, uio->uio_offset);
246 blkoffset = blkoff(fs, uio->uio_offset);
247 xfersize = fs->e2fs_bsize - blkoffset;
248 if (uio->uio_resid < xfersize)
249 xfersize = uio->uio_resid;
250 if (fs->e2fs_bsize > xfersize)
251 flags |= B_CLRBUF;
252 else
253 flags &= ~B_CLRBUF;
254
255 error = ext2fs_balloc(ip,
256 lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
257 if (error)
258 break;
259 if (uio->uio_offset + xfersize > ip->i_e2fs_size) {
260 ip->i_e2fs_size = uio->uio_offset + xfersize;
261 #if defined(UVM)
262 uvm_vnp_setsize(vp, ip->i_e2fs_size);
263 #else
264 vnode_pager_setsize(vp, ip->i_e2fs_size);
265 #endif
266 }
267 #if defined(UVM)
268 (void)uvm_vnp_uncache(vp);
269 #else
270 (void)vnode_pager_uncache(vp);
271 #endif
272
273 size = fs->e2fs_bsize - bp->b_resid;
274 if (size < xfersize)
275 xfersize = size;
276
277 error =
278 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
279 if (ioflag & IO_SYNC)
280 (void)bwrite(bp);
281 else if (xfersize + blkoffset == fs->e2fs_bsize)
282 if (doclusterwrite)
283 cluster_write(bp, ip->i_e2fs_size);
284 else
285 bawrite(bp);
286 else
287 bdwrite(bp);
288 if (error || xfersize == 0)
289 break;
290 ip->i_flag |= IN_CHANGE | IN_UPDATE;
291 }
292 /*
293 * If we successfully wrote any data, and we are not the superuser
294 * we clear the setuid and setgid bits as a precaution against
295 * tampering.
296 */
297 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
298 ip->i_e2fs_mode &= ~(ISUID | ISGID);
299 if (error) {
300 if (ioflag & IO_UNIT) {
301 (void)VOP_TRUNCATE(vp, osize,
302 ioflag & IO_SYNC, ap->a_cred, uio->uio_procp);
303 uio->uio_offset -= resid - uio->uio_resid;
304 uio->uio_resid = resid;
305 }
306 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
307 TIMEVAL_TO_TIMESPEC(&time, &ts);
308 error = VOP_UPDATE(vp, &ts, &ts, 1);
309 }
310 return (error);
311 }
312