Sync usage with man page.
[netbsd-mini2440.git] / sys / ufs / ext2fs / ext2fs_readwrite.c
blob9e8d93a01bfbb3178df93023f756e0b87241cebf
1 /* $NetBSD: ext2fs_readwrite.c,v 1.54 2009/09/12 11:27:39 tsutsui 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.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>
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 && 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);
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 = lblkno(fs, uio->uio_offset);
161 nextlbn = lbn + 1;
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);
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 proc *p;
228 struct ufsmount *ump;
229 daddr_t lbn;
230 off_t osize;
231 int blkoffset, error, flags, ioflag, resid, xfersize;
232 vsize_t bytelen;
233 off_t oldoff = 0; /* XXX */
234 bool async;
235 int extended = 0;
236 int advice;
238 ioflag = ap->a_ioflag;
239 advice = IO_ADV_DECODE(ioflag);
240 uio = ap->a_uio;
241 vp = ap->a_vp;
242 ip = VTOI(vp);
243 ump = ip->i_ump;
244 error = 0;
246 #ifdef DIAGNOSTIC
247 if (uio->uio_rw != UIO_WRITE)
248 panic("%s: mode", "ext2fs_write");
249 #endif
251 switch (vp->v_type) {
252 case VREG:
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))
257 return (EPERM);
258 /* FALLTHROUGH */
259 case VLNK:
260 break;
261 case VDIR:
262 if ((ioflag & IO_SYNC) == 0)
263 panic("%s: nonsync dir write", "ext2fs_write");
264 break;
265 default:
266 panic("%s: type", "ext2fs_write");
269 fs = ip->i_e2fs;
270 if (uio->uio_offset < 0 ||
271 (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
272 return (EFBIG);
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.
277 p = curproc;
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);
282 psignal(p, SIGXFSZ);
283 mutex_exit(proc_lock);
284 return (EFBIG);
286 if (uio->uio_resid == 0)
287 return (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,
298 uio->uio_resid);
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);
305 if (error)
306 break;
307 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
308 UBC_WRITE | UBC_UNMAP_FLAG(vp));
309 if (error)
310 break;
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);
319 extended = 1;
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);
340 goto out;
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)
349 flags |= B_CLRBUF;
350 else
351 flags &= ~B_CLRBUF;
352 error = ext2fs_balloc(ip,
353 lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
354 if (error)
355 break;
356 if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
357 error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
358 if (error)
359 break;
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);
370 extended = 1;
373 if (ioflag & IO_SYNC)
374 (void)bwrite(bp);
375 else if (xfersize + blkoffset == fs->e2fs_bsize)
376 bawrite(bp);
377 else
378 bdwrite(bp);
379 if (error || xfersize == 0)
380 break;
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
386 * tampering.
389 out:
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));
396 if (error) {
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));
403 return (error);