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