ext2fs_readwrite.c revision 1.69 1 /* $NetBSD: ext2fs_readwrite.c,v 1.69 2015/03/28 03:49:41 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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.
18 *
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.
30 *
31 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
32 * Modified for ext2fs by Manuel Bouyer.
33 */
34
35 /*-
36 * Copyright (c) 1997 Manuel Bouyer.
37 *
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.
46 *
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.
57 *
58 * @(#)ufs_readwrite.c 8.8 (Berkeley) 8/4/94
59 * Modified for ext2fs by Manuel Bouyer.
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.69 2015/03/28 03:49:41 riastradh Exp $");
64
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/signalvar.h>
76 #include <sys/kauth.h>
77
78 #include <ufs/ufs/inode.h>
79 #include <ufs/ufs/ufsmount.h>
80 #include <ufs/ufs/ufs_extern.h>
81 #include <ufs/ext2fs/ext2fs.h>
82 #include <ufs/ext2fs/ext2fs_extern.h>
83
84 /*
85 * Vnode op for reading.
86 */
87 /* ARGSUSED */
88 int
89 ext2fs_read(void *v)
90 {
91 struct vop_read_args /* {
92 struct vnode *a_vp;
93 struct uio *a_uio;
94 int a_ioflag;
95 kauth_cred_t a_cred;
96 } */ *ap = v;
97 struct vnode *vp;
98 struct inode *ip;
99 struct uio *uio;
100 struct ufsmount *ump;
101 vsize_t bytelen;
102 int advice;
103 int error;
104
105 vp = ap->a_vp;
106 ip = VTOI(vp);
107 ump = ip->i_ump;
108 uio = ap->a_uio;
109 error = 0;
110
111 #ifdef DIAGNOSTIC
112 if (uio->uio_rw != UIO_READ)
113 panic("%s: mode", "ext2fs_read");
114
115 if (vp->v_type != VREG && vp->v_type != VDIR)
116 panic("%s: type %d", "ext2fs_read", vp->v_type);
117 #endif
118 /* XXX Eliminate me by refusing directory reads from userland. */
119 if (vp->v_type == VDIR)
120 return ext2fs_bufrd(vp, uio, ap->a_ioflag, ap->a_cred);
121
122 if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
123 return (EFBIG);
124 if (uio->uio_resid == 0)
125 return (0);
126 if (uio->uio_offset >= ext2fs_size(ip))
127 goto out;
128
129 KASSERT(vp->v_type == VREG);
130 advice = IO_ADV_DECODE(ap->a_ioflag);
131 while (uio->uio_resid > 0) {
132 bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
133 uio->uio_resid);
134 if (bytelen == 0)
135 break;
136
137 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
138 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
139 if (error)
140 break;
141 }
142
143 out:
144 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
145 ip->i_flag |= IN_ACCESS;
146 if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
147 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
148 }
149 return (error);
150 }
151
152 /*
153 * UFS op for reading via the buffer cache
154 */
155 int
156 ext2fs_bufrd(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
157 {
158 struct inode *ip;
159 struct ufsmount *ump;
160 struct m_ext2fs *fs;
161 struct buf *bp;
162 off_t bytesinfile;
163 daddr_t lbn, nextlbn;
164 long size, xfersize, blkoffset;
165 int error;
166
167 KASSERT(uio->uio_rw == UIO_READ);
168 KASSERT(VOP_ISLOCKED(vp));
169 KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
170
171 ip = VTOI(vp);
172 ump = ip->i_ump;
173 fs = ip->i_e2fs;
174 error = 0;
175
176 KASSERT(vp->v_type != VLNK ||
177 ext2fs_size(ip) >= ump->um_maxsymlinklen);
178 KASSERT(vp->v_type != VLNK || ump->um_maxsymlinklen != 0 ||
179 ext2fs_nblock(ip) != 0);
180
181 if (uio->uio_offset > ump->um_maxfilesize)
182 return EFBIG;
183 if (uio->uio_resid == 0)
184 return 0;
185 if (uio->uio_offset >= ext2fs_size(ip))
186 goto out;
187
188 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
189 bytesinfile = ext2fs_size(ip) - uio->uio_offset;
190 if (bytesinfile <= 0)
191 break;
192 lbn = ext2_lblkno(fs, uio->uio_offset);
193 nextlbn = lbn + 1;
194 size = fs->e2fs_bsize;
195 blkoffset = ext2_blkoff(fs, uio->uio_offset);
196 xfersize = fs->e2fs_bsize - blkoffset;
197 if (uio->uio_resid < xfersize)
198 xfersize = uio->uio_resid;
199 if (bytesinfile < xfersize)
200 xfersize = bytesinfile;
201
202 if (ext2_lblktosize(fs, nextlbn) >= ext2fs_size(ip))
203 error = bread(vp, lbn, size, NOCRED, 0, &bp);
204 else {
205 int nextsize = fs->e2fs_bsize;
206 error = breadn(vp, lbn,
207 size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
208 }
209 if (error)
210 break;
211
212 /*
213 * We should only get non-zero b_resid when an I/O error
214 * has occurred, which should cause us to break above.
215 * However, if the short read did not cause an error,
216 * then we want to ensure that we do not uiomove bad
217 * or uninitialized data.
218 */
219 size -= bp->b_resid;
220 if (size < xfersize) {
221 if (size == 0)
222 break;
223 xfersize = size;
224 }
225 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
226 if (error)
227 break;
228 brelse(bp, 0);
229 }
230 if (bp != NULL)
231 brelse(bp, 0);
232
233 out:
234 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
235 ip->i_flag |= IN_ACCESS;
236 if ((ioflag & IO_SYNC) == IO_SYNC)
237 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
238 }
239 return (error);
240 }
241
242 /*
243 * Vnode op for writing.
244 */
245 int
246 ext2fs_write(void *v)
247 {
248 struct vop_write_args /* {
249 struct vnode *a_vp;
250 struct uio *a_uio;
251 int a_ioflag;
252 kauth_cred_t a_cred;
253 } */ *ap = v;
254 struct vnode *vp;
255 struct uio *uio;
256 struct inode *ip;
257 struct m_ext2fs *fs;
258 struct ufsmount *ump;
259 off_t osize;
260 int blkoffset, error, ioflag, resid;
261 vsize_t bytelen;
262 off_t oldoff = 0; /* XXX */
263 bool async;
264 int extended = 0;
265 int advice;
266
267 ioflag = ap->a_ioflag;
268 advice = IO_ADV_DECODE(ioflag);
269 uio = ap->a_uio;
270 vp = ap->a_vp;
271 ip = VTOI(vp);
272 ump = ip->i_ump;
273 error = 0;
274
275 #ifdef DIAGNOSTIC
276 if (uio->uio_rw != UIO_WRITE)
277 panic("%s: mode", "ext2fs_write");
278 #endif
279
280 switch (vp->v_type) {
281 case VREG:
282 if (ioflag & IO_APPEND)
283 uio->uio_offset = ext2fs_size(ip);
284 if ((ip->i_e2fs_flags & EXT2_APPEND) &&
285 uio->uio_offset != ext2fs_size(ip))
286 return (EPERM);
287 break;
288 default:
289 panic("%s: type", "ext2fs_write");
290 }
291
292 fs = ip->i_e2fs;
293 if (uio->uio_offset < 0 ||
294 (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
295 return (EFBIG);
296 if (uio->uio_resid == 0)
297 return (0);
298
299 async = vp->v_mount->mnt_flag & MNT_ASYNC;
300 resid = uio->uio_resid;
301 osize = ext2fs_size(ip);
302
303 KASSERT(vp->v_type == VREG);
304 while (uio->uio_resid > 0) {
305 oldoff = uio->uio_offset;
306 blkoffset = ext2_blkoff(fs, uio->uio_offset);
307 bytelen = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
308
309 if (vp->v_size < oldoff + bytelen) {
310 uvm_vnp_setwritesize(vp, oldoff + bytelen);
311 }
312 error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
313 ap->a_cred, 0);
314 if (error)
315 break;
316 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
317 UBC_WRITE | UBC_UNMAP_FLAG(vp));
318 if (error)
319 break;
320
321 /*
322 * update UVM's notion of the size now that we've
323 * copied the data into the vnode's pages.
324 */
325
326 if (vp->v_size < uio->uio_offset) {
327 uvm_vnp_setsize(vp, uio->uio_offset);
328 extended = 1;
329 }
330
331 /*
332 * flush what we just wrote if necessary.
333 * XXXUBC simplistic async flushing.
334 */
335
336 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
337 mutex_enter(vp->v_interlock);
338 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
339 (uio->uio_offset >> 16) << 16,
340 PGO_CLEANIT | PGO_LAZY);
341 }
342 }
343 if (error == 0 && ioflag & IO_SYNC) {
344 mutex_enter(vp->v_interlock);
345 error = VOP_PUTPAGES(vp, trunc_page(oldoff),
346 round_page(ext2_blkroundup(fs, uio->uio_offset)),
347 PGO_CLEANIT | PGO_SYNCIO);
348 }
349
350 /*
351 * If we successfully wrote any data, and we are not the superuser
352 * we clear the setuid and setgid bits as a precaution against
353 * tampering.
354 */
355 ip->i_flag |= IN_CHANGE | IN_UPDATE;
356 if (vp->v_mount->mnt_flag & MNT_RELATIME)
357 ip->i_flag |= IN_ACCESS;
358 if (resid > uio->uio_resid && ap->a_cred) {
359 if (ip->i_e2fs_mode & ISUID) {
360 if (kauth_authorize_vnode(ap->a_cred,
361 KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
362 ip->i_e2fs_mode &= ISUID;
363 }
364
365 if (ip->i_e2fs_mode & ISGID) {
366 if (kauth_authorize_vnode(ap->a_cred,
367 KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
368 ip->i_e2fs_mode &= ~ISGID;
369 }
370 }
371 if (resid > uio->uio_resid)
372 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
373 if (error) {
374 (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
375 uio->uio_offset -= resid - uio->uio_resid;
376 uio->uio_resid = resid;
377 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
378 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
379 KASSERT(vp->v_size == ext2fs_size(ip));
380 return (error);
381 }
382
383 /*
384 * UFS op for writing via the buffer cache
385 */
386 int
387 ext2fs_bufwr(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
388 {
389 struct inode *ip;
390 struct ufsmount *ump;
391 struct m_ext2fs *fs;
392 struct buf *bp;
393 int flags;
394 off_t osize;
395 daddr_t lbn;
396 int resid, blkoffset, xfersize;
397 int extended = 0;
398 int error;
399
400 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
401 KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
402 KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
403 KASSERT(uio->uio_rw == UIO_WRITE);
404
405 ip = VTOI(vp);
406 ump = ip->i_ump;
407 fs = ip->i_e2fs;
408 error = 0;
409
410 if (uio->uio_offset < 0 ||
411 uio->uio_resid > ump->um_maxfilesize ||
412 uio->uio_offset > (ump->um_maxfilesize - uio->uio_resid))
413 return EFBIG;
414 if (uio->uio_resid == 0)
415 return 0;
416
417 flags = ioflag & IO_SYNC ? B_SYNC : 0;
418 resid = uio->uio_resid;
419 osize = ext2fs_size(ip);
420
421 for (error = 0; uio->uio_resid > 0;) {
422 lbn = ext2_lblkno(fs, uio->uio_offset);
423 blkoffset = ext2_blkoff(fs, uio->uio_offset);
424 xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
425 if (xfersize < fs->e2fs_bsize)
426 flags |= B_CLRBUF;
427 else
428 flags &= ~B_CLRBUF;
429 error = ext2fs_balloc(ip, lbn, blkoffset + xfersize, cred, &bp,
430 flags);
431 if (error)
432 break;
433 if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
434 error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
435 if (error)
436 break;
437 }
438 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
439
440 /*
441 * update UVM's notion of the size now that we've
442 * copied the data into the vnode's pages.
443 */
444
445 if (vp->v_size < uio->uio_offset) {
446 uvm_vnp_setsize(vp, uio->uio_offset);
447 extended = 1;
448 }
449
450 if (ioflag & IO_SYNC)
451 (void)bwrite(bp);
452 else if (xfersize + blkoffset == fs->e2fs_bsize)
453 bawrite(bp);
454 else
455 bdwrite(bp);
456 if (error || xfersize == 0)
457 break;
458 }
459
460 /*
461 * If we successfully wrote any data, and we are not the superuser
462 * we clear the setuid and setgid bits as a precaution against
463 * tampering.
464 */
465 ip->i_flag |= IN_CHANGE | IN_UPDATE;
466 if (vp->v_mount->mnt_flag & MNT_RELATIME)
467 ip->i_flag |= IN_ACCESS;
468 if (resid > uio->uio_resid && cred) {
469 if (ip->i_e2fs_mode & ISUID) {
470 if (kauth_authorize_vnode(cred,
471 KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
472 ip->i_e2fs_mode &= ISUID;
473 }
474
475 if (ip->i_e2fs_mode & ISGID) {
476 if (kauth_authorize_vnode(cred,
477 KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
478 ip->i_e2fs_mode &= ~ISGID;
479 }
480 }
481 if (resid > uio->uio_resid)
482 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
483 if (error) {
484 (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, cred);
485 uio->uio_offset -= resid - uio->uio_resid;
486 uio->uio_resid = resid;
487 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
488 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
489 KASSERT(vp->v_size == ext2fs_size(ip));
490 return (error);
491 }
492