1 /* $NetBSD: ext2fs_readwrite.c,v 1.58 2011/11/18 21:18:51 christos Exp $ */
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
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
31 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
32 * Modified for ext2fs by Manuel Bouyer.
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
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.58 2011/11/18 21:18:51 christos Exp $");
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/resourcevar.h>
68 #include <sys/kernel.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.
96 struct vop_read_args
/* {
107 struct ufsmount
*ump
;
109 daddr_t lbn
, nextlbn
;
111 long size
, xfersize
, blkoffset
;
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 && ip
->i_e2fs_nblock
== 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
);
132 if ((uint64_t)uio
->uio_offset
> ump
->um_maxfilesize
)
134 if (uio
->uio_resid
== 0)
136 if (uio
->uio_offset
>= ext2fs_size(ip
))
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
,
148 error
= ubc_uiomove(&vp
->v_uobj
, uio
, bytelen
, advice
,
149 UBC_READ
| UBC_PARTIALOK
| UBC_UNMAP_FLAG(vp
));
156 for (error
= 0, bp
= NULL
; uio
->uio_resid
> 0; bp
= NULL
) {
157 bytesinfile
= ext2fs_size(ip
) - uio
->uio_offset
;
158 if (bytesinfile
<= 0)
160 lbn
= lblkno(fs
, uio
->uio_offset
);
162 size
= fs
->e2fs_bsize
;
163 blkoffset
= 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 (lblktosize(fs
, nextlbn
) >= ext2fs_size(ip
))
171 error
= bread(vp
, lbn
, size
, NOCRED
, 0, &bp
);
173 int nextsize
= fs
->e2fs_bsize
;
174 error
= breadn(vp
, lbn
,
175 size
, &nextlbn
, &nextsize
, 1, NOCRED
, 0, &bp
);
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.
188 if (size
< xfersize
) {
193 error
= uiomove((char *)bp
->b_data
+ blkoffset
, xfersize
, uio
);
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
);
211 * Vnode op for writing.
214 ext2fs_write(void *v
)
216 struct vop_write_args
/* {
227 struct ufsmount
*ump
;
230 int blkoffset
, error
, flags
, ioflag
, resid
, xfersize
;
232 off_t oldoff
= 0; /* XXX */
237 ioflag
= ap
->a_ioflag
;
238 advice
= IO_ADV_DECODE(ioflag
);
246 if (uio
->uio_rw
!= UIO_WRITE
)
247 panic("%s: mode", "ext2fs_write");
250 switch (vp
->v_type
) {
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
))
261 if ((ioflag
& IO_SYNC
) == 0)
262 panic("%s: nonsync dir write", "ext2fs_write");
265 panic("%s: type", "ext2fs_write");
269 if (uio
->uio_offset
< 0 ||
270 (uint64_t)uio
->uio_offset
+ uio
->uio_resid
> ump
->um_maxfilesize
)
272 if (uio
->uio_resid
== 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
= blkoff(fs
, uio
->uio_offset
);
283 bytelen
= MIN(fs
->e2fs_bsize
- blkoffset
,
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);
293 error
= ubc_uiomove(&vp
->v_uobj
, uio
, bytelen
, advice
,
294 UBC_WRITE
| UBC_UNMAP_FLAG(vp
));
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
);
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, PGO_CLEANIT
);
319 if (error
== 0 && ioflag
& IO_SYNC
) {
320 mutex_enter(vp
->v_interlock
);
321 error
= VOP_PUTPAGES(vp
, trunc_page(oldoff
),
322 round_page(blkroundup(fs
, uio
->uio_offset
)),
323 PGO_CLEANIT
| PGO_SYNCIO
);
329 flags
= ioflag
& IO_SYNC
? B_SYNC
: 0;
330 for (error
= 0; uio
->uio_resid
> 0;) {
331 lbn
= lblkno(fs
, uio
->uio_offset
);
332 blkoffset
= blkoff(fs
, uio
->uio_offset
);
333 xfersize
= MIN(fs
->e2fs_bsize
- blkoffset
, uio
->uio_resid
);
334 if (xfersize
< fs
->e2fs_bsize
)
338 error
= ext2fs_balloc(ip
,
339 lbn
, blkoffset
+ xfersize
, ap
->a_cred
, &bp
, flags
);
342 if (ext2fs_size(ip
) < uio
->uio_offset
+ xfersize
) {
343 error
= ext2fs_setsize(ip
, uio
->uio_offset
+ xfersize
);
347 error
= uiomove((char *)bp
->b_data
+ blkoffset
, xfersize
, uio
);
350 * update UVM's notion of the size now that we've
351 * copied the data into the vnode's pages.
354 if (vp
->v_size
< uio
->uio_offset
) {
355 uvm_vnp_setsize(vp
, uio
->uio_offset
);
359 if (ioflag
& IO_SYNC
)
361 else if (xfersize
+ blkoffset
== fs
->e2fs_bsize
)
365 if (error
|| xfersize
== 0)
370 * If we successfully wrote any data, and we are not the superuser
371 * we clear the setuid and setgid bits as a precaution against
376 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
377 if (vp
->v_mount
->mnt_flag
& MNT_RELATIME
)
378 ip
->i_flag
|= IN_ACCESS
;
379 if (resid
> uio
->uio_resid
&& ap
->a_cred
&&
380 kauth_authorize_generic(ap
->a_cred
, KAUTH_GENERIC_ISSUSER
, NULL
))
381 ip
->i_e2fs_mode
&= ~(ISUID
| ISGID
);
382 if (resid
> uio
->uio_resid
)
383 VN_KNOTE(vp
, NOTE_WRITE
| (extended
? NOTE_EXTEND
: 0));
385 (void) ext2fs_truncate(vp
, osize
, ioflag
& IO_SYNC
, ap
->a_cred
);
386 uio
->uio_offset
-= resid
- uio
->uio_resid
;
387 uio
->uio_resid
= resid
;
388 } else if (resid
> uio
->uio_resid
&& (ioflag
& IO_SYNC
) == IO_SYNC
)
389 error
= ext2fs_update(vp
, NULL
, NULL
, UPDATE_WAIT
);
390 KASSERT(vp
->v_size
== ext2fs_size(ip
));