1 /* $NetBSD: ext2fs_readwrite.c,v 1.54 2009/09/12 11:27:39 tsutsui 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.54 2009/09/12 11:27:39 tsutsui 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
/* {
228 struct ufsmount
*ump
;
231 int blkoffset
, error
, flags
, ioflag
, resid
, xfersize
;
233 off_t oldoff
= 0; /* XXX */
238 ioflag
= ap
->a_ioflag
;
239 advice
= IO_ADV_DECODE(ioflag
);
247 if (uio
->uio_rw
!= UIO_WRITE
)
248 panic("%s: mode", "ext2fs_write");
251 switch (vp
->v_type
) {
253 if (ioflag
& IO_APPEND
)
254 uio
->uio_offset
= ext2fs_size(ip
);
255 if ((ip
->i_e2fs_flags
& EXT2_APPEND
) &&
256 uio
->uio_offset
!= ext2fs_size(ip
))
262 if ((ioflag
& IO_SYNC
) == 0)
263 panic("%s: nonsync dir write", "ext2fs_write");
266 panic("%s: type", "ext2fs_write");
270 if (uio
->uio_offset
< 0 ||
271 (uint64_t)uio
->uio_offset
+ uio
->uio_resid
> ump
->um_maxfilesize
)
274 * Maybe this should be above the vnode op call, but so long as
275 * file servers have no limits, I don't think it matters.
278 if (vp
->v_type
== VREG
&& p
&&
279 uio
->uio_offset
+ uio
->uio_resid
>
280 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
281 mutex_enter(proc_lock
);
283 mutex_exit(proc_lock
);
286 if (uio
->uio_resid
== 0)
289 async
= vp
->v_mount
->mnt_flag
& MNT_ASYNC
;
290 resid
= uio
->uio_resid
;
291 osize
= ext2fs_size(ip
);
293 if (vp
->v_type
== VREG
) {
294 while (uio
->uio_resid
> 0) {
295 oldoff
= uio
->uio_offset
;
296 blkoffset
= blkoff(fs
, uio
->uio_offset
);
297 bytelen
= MIN(fs
->e2fs_bsize
- blkoffset
,
300 if (vp
->v_size
< oldoff
+ bytelen
) {
301 uvm_vnp_setwritesize(vp
, oldoff
+ bytelen
);
303 error
= ufs_balloc_range(vp
, uio
->uio_offset
,
304 bytelen
, ap
->a_cred
, 0);
307 error
= ubc_uiomove(&vp
->v_uobj
, uio
, bytelen
, advice
,
308 UBC_WRITE
| UBC_UNMAP_FLAG(vp
));
313 * update UVM's notion of the size now that we've
314 * copied the data into the vnode's pages.
317 if (vp
->v_size
< uio
->uio_offset
) {
318 uvm_vnp_setsize(vp
, uio
->uio_offset
);
323 * flush what we just wrote if necessary.
324 * XXXUBC simplistic async flushing.
327 if (!async
&& oldoff
>> 16 != uio
->uio_offset
>> 16) {
328 mutex_enter(&vp
->v_interlock
);
329 error
= VOP_PUTPAGES(vp
, (oldoff
>> 16) << 16,
330 (uio
->uio_offset
>> 16) << 16, PGO_CLEANIT
);
333 if (error
== 0 && ioflag
& IO_SYNC
) {
334 mutex_enter(&vp
->v_interlock
);
335 error
= VOP_PUTPAGES(vp
, trunc_page(oldoff
),
336 round_page(blkroundup(fs
, uio
->uio_offset
)),
337 PGO_CLEANIT
| PGO_SYNCIO
);
343 flags
= ioflag
& IO_SYNC
? B_SYNC
: 0;
344 for (error
= 0; uio
->uio_resid
> 0;) {
345 lbn
= lblkno(fs
, uio
->uio_offset
);
346 blkoffset
= blkoff(fs
, uio
->uio_offset
);
347 xfersize
= MIN(fs
->e2fs_bsize
- blkoffset
, uio
->uio_resid
);
348 if (xfersize
< fs
->e2fs_bsize
)
352 error
= ext2fs_balloc(ip
,
353 lbn
, blkoffset
+ xfersize
, ap
->a_cred
, &bp
, flags
);
356 if (ext2fs_size(ip
) < uio
->uio_offset
+ xfersize
) {
357 error
= ext2fs_setsize(ip
, uio
->uio_offset
+ xfersize
);
361 error
= uiomove((char *)bp
->b_data
+ blkoffset
, xfersize
, uio
);
364 * update UVM's notion of the size now that we've
365 * copied the data into the vnode's pages.
368 if (vp
->v_size
< uio
->uio_offset
) {
369 uvm_vnp_setsize(vp
, uio
->uio_offset
);
373 if (ioflag
& IO_SYNC
)
375 else if (xfersize
+ blkoffset
== fs
->e2fs_bsize
)
379 if (error
|| xfersize
== 0)
384 * If we successfully wrote any data, and we are not the superuser
385 * we clear the setuid and setgid bits as a precaution against
390 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
391 if (resid
> uio
->uio_resid
&& ap
->a_cred
&&
392 kauth_authorize_generic(ap
->a_cred
, KAUTH_GENERIC_ISSUSER
, NULL
))
393 ip
->i_e2fs_mode
&= ~(ISUID
| ISGID
);
394 if (resid
> uio
->uio_resid
)
395 VN_KNOTE(vp
, NOTE_WRITE
| (extended
? NOTE_EXTEND
: 0));
397 (void) ext2fs_truncate(vp
, osize
, ioflag
& IO_SYNC
, ap
->a_cred
);
398 uio
->uio_offset
-= resid
- uio
->uio_resid
;
399 uio
->uio_resid
= resid
;
400 } else if (resid
> uio
->uio_resid
&& (ioflag
& IO_SYNC
) == IO_SYNC
)
401 error
= ext2fs_update(vp
, NULL
, NULL
, UPDATE_WAIT
);
402 KASSERT(vp
->v_size
== ext2fs_size(ip
));