tools/llvm: Do not build with symbols
[minix3.git] / sys / ufs / ext2fs / ext2fs_readwrite.c
blob4c9dd98b5ea850ae46a7918f8050fbcdc31d334b
1 /* $NetBSD: ext2fs_readwrite.c,v 1.64 2013/06/23 07:28:37 dholland Exp $ */
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
31 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
32 * Modified for ext2fs by Manuel Bouyer.
35 /*-
36 * Copyright (c) 1997 Manuel Bouyer.
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.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
59 * Modified for ext2fs by Manuel Bouyer.
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.64 2013/06/23 07:28:37 dholland Exp $");
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/resourcevar.h>
68 #include <sys/kernel.h>
69 #include <sys/file.h>
70 #include <sys/stat.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/mount.h>
74 #include <sys/vnode.h>
75 #include <sys/malloc.h>
76 #include <sys/signalvar.h>
77 #include <sys/kauth.h>
79 #include <ufs/ufs/inode.h>
80 #include <ufs/ufs/ufsmount.h>
81 #include <ufs/ufs/ufs_extern.h>
82 #include <ufs/ext2fs/ext2fs.h>
83 #include <ufs/ext2fs/ext2fs_extern.h>
86 #define doclusterread 0 /* XXX underway */
87 #define doclusterwrite 0
90 * Vnode op for reading.
92 /* ARGSUSED */
93 int
94 ext2fs_read(void *v)
96 struct vop_read_args /* {
97 struct vnode *a_vp;
98 struct uio *a_uio;
99 int a_ioflag;
100 kauth_cred_t a_cred;
101 } */ *ap = v;
102 struct vnode *vp;
103 struct inode *ip;
104 struct uio *uio;
105 struct m_ext2fs *fs;
106 struct buf *bp;
107 struct ufsmount *ump;
108 vsize_t bytelen;
109 daddr_t lbn, nextlbn;
110 off_t bytesinfile;
111 long size, xfersize, blkoffset;
112 int error;
114 vp = ap->a_vp;
115 ip = VTOI(vp);
116 ump = ip->i_ump;
117 uio = ap->a_uio;
118 error = 0;
120 #ifdef DIAGNOSTIC
121 if (uio->uio_rw != UIO_READ)
122 panic("%s: mode", "ext2fs_read");
124 if (vp->v_type == VLNK) {
125 if (ext2fs_size(ip) < ump->um_maxsymlinklen ||
126 (ump->um_maxsymlinklen == 0 && ext2fs_nblock(ip) == 0))
127 panic("%s: short symlink", "ext2fs_read");
128 } else if (vp->v_type != VREG && vp->v_type != VDIR)
129 panic("%s: type %d", "ext2fs_read", vp->v_type);
130 #endif
131 fs = ip->i_e2fs;
132 if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
133 return (EFBIG);
134 if (uio->uio_resid == 0)
135 return (0);
136 if (uio->uio_offset >= ext2fs_size(ip))
137 goto out;
139 if (vp->v_type == VREG) {
140 const int advice = IO_ADV_DECODE(ap->a_ioflag);
142 while (uio->uio_resid > 0) {
143 bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
144 uio->uio_resid);
145 if (bytelen == 0)
146 break;
148 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
149 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
150 if (error)
151 break;
153 goto out;
156 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
157 bytesinfile = ext2fs_size(ip) - uio->uio_offset;
158 if (bytesinfile <= 0)
159 break;
160 lbn = ext2_lblkno(fs, uio->uio_offset);
161 nextlbn = lbn + 1;
162 size = fs->e2fs_bsize;
163 blkoffset = ext2_blkoff(fs, uio->uio_offset);
164 xfersize = fs->e2fs_bsize - blkoffset;
165 if (uio->uio_resid < xfersize)
166 xfersize = uio->uio_resid;
167 if (bytesinfile < xfersize)
168 xfersize = bytesinfile;
170 if (ext2_lblktosize(fs, nextlbn) >= ext2fs_size(ip))
171 error = bread(vp, lbn, size, NOCRED, 0, &bp);
172 else {
173 int nextsize = fs->e2fs_bsize;
174 error = breadn(vp, lbn,
175 size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
177 if (error)
178 break;
181 * We should only get non-zero b_resid when an I/O error
182 * has occurred, which should cause us to break above.
183 * However, if the short read did not cause an error,
184 * then we want to ensure that we do not uiomove bad
185 * or uninitialized data.
187 size -= bp->b_resid;
188 if (size < xfersize) {
189 if (size == 0)
190 break;
191 xfersize = size;
193 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
194 if (error)
195 break;
196 brelse(bp, 0);
198 if (bp != NULL)
199 brelse(bp, 0);
201 out:
202 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
203 ip->i_flag |= IN_ACCESS;
204 if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
205 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
207 return (error);
211 * Vnode op for writing.
214 ext2fs_write(void *v)
216 struct vop_write_args /* {
217 struct vnode *a_vp;
218 struct uio *a_uio;
219 int a_ioflag;
220 kauth_cred_t a_cred;
221 } */ *ap = v;
222 struct vnode *vp;
223 struct uio *uio;
224 struct inode *ip;
225 struct m_ext2fs *fs;
226 struct buf *bp;
227 struct ufsmount *ump;
228 daddr_t lbn;
229 off_t osize;
230 int blkoffset, error, flags, ioflag, resid, xfersize;
231 vsize_t bytelen;
232 off_t oldoff = 0; /* XXX */
233 bool async;
234 int extended = 0;
235 int advice;
237 ioflag = ap->a_ioflag;
238 advice = IO_ADV_DECODE(ioflag);
239 uio = ap->a_uio;
240 vp = ap->a_vp;
241 ip = VTOI(vp);
242 ump = ip->i_ump;
243 error = 0;
245 #ifdef DIAGNOSTIC
246 if (uio->uio_rw != UIO_WRITE)
247 panic("%s: mode", "ext2fs_write");
248 #endif
250 switch (vp->v_type) {
251 case VREG:
252 if (ioflag & IO_APPEND)
253 uio->uio_offset = ext2fs_size(ip);
254 if ((ip->i_e2fs_flags & EXT2_APPEND) &&
255 uio->uio_offset != ext2fs_size(ip))
256 return (EPERM);
257 /* FALLTHROUGH */
258 case VLNK:
259 break;
260 case VDIR:
261 if ((ioflag & IO_SYNC) == 0)
262 panic("%s: nonsync dir write", "ext2fs_write");
263 break;
264 default:
265 panic("%s: type", "ext2fs_write");
268 fs = ip->i_e2fs;
269 if (uio->uio_offset < 0 ||
270 (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
271 return (EFBIG);
272 if (uio->uio_resid == 0)
273 return (0);
275 async = vp->v_mount->mnt_flag & MNT_ASYNC;
276 resid = uio->uio_resid;
277 osize = ext2fs_size(ip);
279 if (vp->v_type == VREG) {
280 while (uio->uio_resid > 0) {
281 oldoff = uio->uio_offset;
282 blkoffset = ext2_blkoff(fs, uio->uio_offset);
283 bytelen = MIN(fs->e2fs_bsize - blkoffset,
284 uio->uio_resid);
286 if (vp->v_size < oldoff + bytelen) {
287 uvm_vnp_setwritesize(vp, oldoff + bytelen);
289 error = ufs_balloc_range(vp, uio->uio_offset,
290 bytelen, ap->a_cred, 0);
291 if (error)
292 break;
293 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
294 UBC_WRITE | UBC_UNMAP_FLAG(vp));
295 if (error)
296 break;
299 * update UVM's notion of the size now that we've
300 * copied the data into the vnode's pages.
303 if (vp->v_size < uio->uio_offset) {
304 uvm_vnp_setsize(vp, uio->uio_offset);
305 extended = 1;
309 * flush what we just wrote if necessary.
310 * XXXUBC simplistic async flushing.
313 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
314 mutex_enter(vp->v_interlock);
315 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
316 (uio->uio_offset >> 16) << 16,
317 PGO_CLEANIT | PGO_LAZY);
320 if (error == 0 && ioflag & IO_SYNC) {
321 mutex_enter(vp->v_interlock);
322 error = VOP_PUTPAGES(vp, trunc_page(oldoff),
323 round_page(ext2_blkroundup(fs, uio->uio_offset)),
324 PGO_CLEANIT | PGO_SYNCIO);
327 goto out;
330 flags = ioflag & IO_SYNC ? B_SYNC : 0;
331 for (error = 0; uio->uio_resid > 0;) {
332 lbn = ext2_lblkno(fs, uio->uio_offset);
333 blkoffset = ext2_blkoff(fs, uio->uio_offset);
334 xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
335 if (xfersize < fs->e2fs_bsize)
336 flags |= B_CLRBUF;
337 else
338 flags &= ~B_CLRBUF;
339 error = ext2fs_balloc(ip,
340 lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
341 if (error)
342 break;
343 if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
344 error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
345 if (error)
346 break;
348 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
351 * update UVM's notion of the size now that we've
352 * copied the data into the vnode's pages.
355 if (vp->v_size < uio->uio_offset) {
356 uvm_vnp_setsize(vp, uio->uio_offset);
357 extended = 1;
360 if (ioflag & IO_SYNC)
361 (void)bwrite(bp);
362 else if (xfersize + blkoffset == fs->e2fs_bsize)
363 bawrite(bp);
364 else
365 bdwrite(bp);
366 if (error || xfersize == 0)
367 break;
371 * If we successfully wrote any data, and we are not the superuser
372 * we clear the setuid and setgid bits as a precaution against
373 * tampering.
376 out:
377 ip->i_flag |= IN_CHANGE | IN_UPDATE;
378 if (vp->v_mount->mnt_flag & MNT_RELATIME)
379 ip->i_flag |= IN_ACCESS;
380 if (resid > uio->uio_resid && ap->a_cred) {
381 if (ip->i_e2fs_mode & ISUID) {
382 if (kauth_authorize_vnode(ap->a_cred,
383 KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
384 ip->i_e2fs_mode &= ISUID;
387 if (ip->i_e2fs_mode & ISGID) {
388 if (kauth_authorize_vnode(ap->a_cred,
389 KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
390 ip->i_e2fs_mode &= ~ISGID;
393 if (resid > uio->uio_resid)
394 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
395 if (error) {
396 (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
397 uio->uio_offset -= resid - uio->uio_resid;
398 uio->uio_resid = resid;
399 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
400 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
401 KASSERT(vp->v_size == ext2fs_size(ip));
402 return (error);