ulfs_readwrite.c revision 1.7.8.2 1 /* $NetBSD: ulfs_readwrite.c,v 1.7.8.2 2015/06/06 14:40:30 skrll Exp $ */
2 /* from NetBSD: ufs_readwrite.c,v 1.105 2013/01/22 09:39:18 dholland Exp */
3
4 /*-
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.7.8.2 2015/06/06 14:40:30 skrll Exp $");
37
38 #ifdef LFS_READWRITE
39 #define FS struct lfs
40 #define I_FS i_lfs
41 #define READ lfs_read
42 #define READ_S "lfs_read"
43 #define WRITE lfs_write
44 #define WRITE_S "lfs_write"
45 #define BUFRD lfs_bufrd
46 #define BUFWR lfs_bufwr
47 #define fs_bsize lfs_bsize
48 #define fs_bmask lfs_bmask
49 #else
50 #define FS struct fs
51 #define I_FS i_fs
52 #define READ ffs_read
53 #define READ_S "ffs_read"
54 #define WRITE ffs_write
55 #define WRITE_S "ffs_write"
56 #define BUFRD ffs_bufrd
57 #define BUFWR ffs_bufwr
58 #endif
59
60 static int ulfs_post_read_update(struct vnode *, int, int);
61 static int ulfs_post_write_update(struct vnode *, struct uio *, int,
62 kauth_cred_t, off_t, int, int, int);
63
64 /*
65 * Vnode op for reading.
66 */
67 /* ARGSUSED */
68 int
69 READ(void *v)
70 {
71 struct vop_read_args /* {
72 struct vnode *a_vp;
73 struct uio *a_uio;
74 int a_ioflag;
75 kauth_cred_t a_cred;
76 } */ *ap = v;
77 struct vnode *vp;
78 struct inode *ip;
79 struct uio *uio;
80 FS *fs;
81 vsize_t bytelen;
82 int error, ioflag, advice;
83
84 vp = ap->a_vp;
85 ip = VTOI(vp);
86 fs = ip->I_FS;
87 uio = ap->a_uio;
88 ioflag = ap->a_ioflag;
89 error = 0;
90
91 KASSERT(uio->uio_rw == UIO_READ);
92 KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
93
94 /* XXX Eliminate me by refusing directory reads from userland. */
95 if (vp->v_type == VDIR)
96 return BUFRD(vp, uio, ioflag, ap->a_cred);
97 #ifdef LFS_READWRITE
98 /* XXX Eliminate me by using ufs_bufio in lfs. */
99 if (vp->v_type == VREG && ip->i_number == LFS_IFILE_INUM)
100 return BUFRD(vp, uio, ioflag, ap->a_cred);
101 #endif
102 if ((u_int64_t)uio->uio_offset > fs->um_maxfilesize)
103 return (EFBIG);
104 if (uio->uio_resid == 0)
105 return (0);
106
107 #ifndef LFS_READWRITE
108 if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT)
109 return ffs_snapshot_read(vp, uio, ioflag);
110 #endif /* !LFS_READWRITE */
111
112 fstrans_start(vp->v_mount, FSTRANS_SHARED);
113
114 if (uio->uio_offset >= ip->i_size)
115 goto out;
116
117 KASSERT(vp->v_type == VREG);
118 advice = IO_ADV_DECODE(ap->a_ioflag);
119 while (uio->uio_resid > 0) {
120 if (ioflag & IO_DIRECT) {
121 genfs_directio(vp, uio, ioflag);
122 }
123 bytelen = MIN(ip->i_size - uio->uio_offset, uio->uio_resid);
124 if (bytelen == 0)
125 break;
126 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
127 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
128 if (error)
129 break;
130 }
131
132 out:
133 error = ulfs_post_read_update(vp, ap->a_ioflag, error);
134 fstrans_done(vp->v_mount);
135 return (error);
136 }
137
138 /*
139 * UFS op for reading via the buffer cache
140 */
141 int
142 BUFRD(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
143 {
144 struct inode *ip;
145 FS *fs;
146 struct buf *bp;
147 daddr_t lbn, nextlbn;
148 off_t bytesinfile;
149 long size, xfersize, blkoffset;
150 int error;
151
152 KASSERT(VOP_ISLOCKED(vp));
153 KASSERT(vp->v_type == VDIR || vp->v_type == VLNK ||
154 vp->v_type == VREG);
155 KASSERT(uio->uio_rw == UIO_READ);
156
157 ip = VTOI(vp);
158 fs = ip->I_FS;
159 error = 0;
160
161 KASSERT(vp->v_type != VLNK || ip->i_size < fs->um_maxsymlinklen);
162 KASSERT(vp->v_type != VLNK || fs->um_maxsymlinklen != 0 ||
163 DIP(ip, blocks) == 0);
164 KASSERT(vp->v_type != VREG || vp == fs->lfs_ivnode);
165 KASSERT(vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM);
166
167 if (uio->uio_offset > fs->um_maxfilesize)
168 return EFBIG;
169 if (uio->uio_resid == 0)
170 return 0;
171
172 #ifndef LFS_READWRITE
173 KASSERT(!ISSET(ip->i_flags, (SF_SNAPSHOT | SF_SNAPINVAL)));
174 #endif
175
176 fstrans_start(vp->v_mount, FSTRANS_SHARED);
177
178 if (uio->uio_offset >= ip->i_size)
179 goto out;
180
181 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
182 bytesinfile = ip->i_size - uio->uio_offset;
183 if (bytesinfile <= 0)
184 break;
185 lbn = lfs_lblkno(fs, uio->uio_offset);
186 nextlbn = lbn + 1;
187 size = lfs_blksize(fs, ip, lbn);
188 blkoffset = lfs_blkoff(fs, uio->uio_offset);
189 xfersize = MIN(MIN(fs->fs_bsize - blkoffset, uio->uio_resid),
190 bytesinfile);
191
192 if (lfs_lblktosize(fs, nextlbn) >= ip->i_size)
193 error = bread(vp, lbn, size, 0, &bp);
194 else {
195 int nextsize = lfs_blksize(fs, ip, nextlbn);
196 error = breadn(vp, lbn,
197 size, &nextlbn, &nextsize, 1, 0, &bp);
198 }
199 if (error)
200 break;
201
202 /*
203 * We should only get non-zero b_resid when an I/O error
204 * has occurred, which should cause us to break above.
205 * However, if the short read did not cause an error,
206 * then we want to ensure that we do not uiomove bad
207 * or uninitialized data.
208 */
209 size -= bp->b_resid;
210 if (size < xfersize) {
211 if (size == 0)
212 break;
213 xfersize = size;
214 }
215 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
216 if (error)
217 break;
218 brelse(bp, 0);
219 }
220 if (bp != NULL)
221 brelse(bp, 0);
222
223 out:
224 error = ulfs_post_read_update(vp, ioflag, error);
225 fstrans_done(vp->v_mount);
226 return (error);
227 }
228
229 static int
230 ulfs_post_read_update(struct vnode *vp, int ioflag, int oerror)
231 {
232 struct inode *ip = VTOI(vp);
233 int error = oerror;
234
235 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
236 ip->i_flag |= IN_ACCESS;
237 if ((ioflag & IO_SYNC) == IO_SYNC) {
238 error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
239 }
240 }
241
242 /* Read error overrides any inode update error. */
243 if (oerror)
244 error = oerror;
245 return error;
246 }
247
248 /*
249 * Vnode op for writing.
250 */
251 int
252 WRITE(void *v)
253 {
254 struct vop_write_args /* {
255 struct vnode *a_vp;
256 struct uio *a_uio;
257 int a_ioflag;
258 kauth_cred_t a_cred;
259 } */ *ap = v;
260 struct vnode *vp;
261 struct uio *uio;
262 struct inode *ip;
263 FS *fs;
264 kauth_cred_t cred;
265 off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
266 int blkoffset, error, flags, ioflag, resid;
267 int aflag;
268 int extended=0;
269 vsize_t bytelen;
270 bool async;
271
272 cred = ap->a_cred;
273 ioflag = ap->a_ioflag;
274 uio = ap->a_uio;
275 vp = ap->a_vp;
276 ip = VTOI(vp);
277
278 KASSERT(vp->v_size == ip->i_size);
279 KASSERT(uio->uio_rw == UIO_WRITE);
280 KASSERT(vp->v_type == VREG);
281
282 if (ioflag & IO_APPEND)
283 uio->uio_offset = ip->i_size;
284 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
285 return (EPERM);
286
287 fs = ip->I_FS;
288 if (uio->uio_offset < 0 ||
289 (u_int64_t)uio->uio_offset + uio->uio_resid > fs->um_maxfilesize)
290 return (EFBIG);
291 #ifdef LFS_READWRITE
292 /* Disallow writes to the Ifile, even if noschg flag is removed */
293 /* XXX can this go away when the Ifile is no longer in the namespace? */
294 if (vp == fs->lfs_ivnode)
295 return (EPERM);
296 #endif
297 if (uio->uio_resid == 0)
298 return (0);
299
300 fstrans_start(vp->v_mount, FSTRANS_SHARED);
301
302 flags = ioflag & IO_SYNC ? B_SYNC : 0;
303 async = vp->v_mount->mnt_flag & MNT_ASYNC;
304 origoff = uio->uio_offset;
305 resid = uio->uio_resid;
306 osize = ip->i_size;
307 error = 0;
308
309 KASSERT(vp->v_type == VREG);
310
311 #ifdef LFS_READWRITE
312 async = true;
313 lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
314 lfs_check(vp, LFS_UNUSED_LBN, 0);
315 #endif /* !LFS_READWRITE */
316
317 preallocoff = round_page(lfs_blkroundup(fs, MAX(osize, uio->uio_offset)));
318 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
319 nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
320 endallocoff = nsize - lfs_blkoff(fs, nsize);
321
322 /*
323 * if we're increasing the file size, deal with expanding
324 * the fragment if there is one.
325 */
326
327 if (nsize > osize && lfs_lblkno(fs, osize) < ULFS_NDADDR &&
328 lfs_lblkno(fs, osize) != lfs_lblkno(fs, nsize) &&
329 lfs_blkroundup(fs, osize) != osize) {
330 off_t eob;
331
332 eob = lfs_blkroundup(fs, osize);
333 uvm_vnp_setwritesize(vp, eob);
334 error = ulfs_balloc_range(vp, osize, eob - osize, cred, aflag);
335 if (error)
336 goto out;
337 if (flags & B_SYNC) {
338 mutex_enter(vp->v_interlock);
339 VOP_PUTPAGES(vp, trunc_page(osize & fs->fs_bmask),
340 round_page(eob),
341 PGO_CLEANIT | PGO_SYNCIO);
342 }
343 }
344
345 while (uio->uio_resid > 0) {
346 int ubc_flags = UBC_WRITE;
347 bool overwrite; /* if we're overwrite a whole block */
348 off_t newoff;
349
350 if (ioflag & IO_DIRECT) {
351 genfs_directio(vp, uio, ioflag);
352 }
353
354 oldoff = uio->uio_offset;
355 blkoffset = lfs_blkoff(fs, uio->uio_offset);
356 bytelen = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
357 if (bytelen == 0) {
358 break;
359 }
360
361 /*
362 * if we're filling in a hole, allocate the blocks now and
363 * initialize the pages first. if we're extending the file,
364 * we can safely allocate blocks without initializing pages
365 * since the new blocks will be inaccessible until the write
366 * is complete.
367 */
368 overwrite = uio->uio_offset >= preallocoff &&
369 uio->uio_offset < endallocoff;
370 if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
371 lfs_blkoff(fs, uio->uio_offset) == 0 &&
372 (uio->uio_offset & PAGE_MASK) == 0) {
373 vsize_t len;
374
375 len = trunc_page(bytelen);
376 len -= lfs_blkoff(fs, len);
377 if (len > 0) {
378 overwrite = true;
379 bytelen = len;
380 }
381 }
382
383 newoff = oldoff + bytelen;
384 if (vp->v_size < newoff) {
385 uvm_vnp_setwritesize(vp, newoff);
386 }
387
388 if (!overwrite) {
389 error = ulfs_balloc_range(vp, uio->uio_offset, bytelen,
390 cred, aflag);
391 if (error)
392 break;
393 } else {
394 genfs_node_wrlock(vp);
395 error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
396 aflag, cred);
397 genfs_node_unlock(vp);
398 if (error)
399 break;
400 ubc_flags |= UBC_FAULTBUSY;
401 }
402
403 /*
404 * copy the data.
405 */
406
407 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
408 IO_ADV_DECODE(ioflag), ubc_flags | UBC_UNMAP_FLAG(vp));
409
410 /*
411 * update UVM's notion of the size now that we've
412 * copied the data into the vnode's pages.
413 *
414 * we should update the size even when uiomove failed.
415 */
416
417 if (vp->v_size < newoff) {
418 uvm_vnp_setsize(vp, newoff);
419 extended = 1;
420 }
421
422 if (error)
423 break;
424
425 /*
426 * flush what we just wrote if necessary.
427 * XXXUBC simplistic async flushing.
428 */
429
430 #ifndef LFS_READWRITE
431 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
432 mutex_enter(vp->v_interlock);
433 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
434 (uio->uio_offset >> 16) << 16,
435 PGO_CLEANIT | PGO_LAZY);
436 if (error)
437 break;
438 }
439 #else
440 __USE(async);
441 #endif
442 }
443 if (error == 0 && ioflag & IO_SYNC) {
444 mutex_enter(vp->v_interlock);
445 error = VOP_PUTPAGES(vp, trunc_page(origoff & fs->fs_bmask),
446 round_page(lfs_blkroundup(fs, uio->uio_offset)),
447 PGO_CLEANIT | PGO_SYNCIO);
448 }
449
450 out:
451 error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
452 extended, error);
453 fstrans_done(vp->v_mount);
454
455 return (error);
456 }
457
458 /*
459 * UFS op for writing via the buffer cache
460 */
461 int
462 BUFWR(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
463 {
464 struct inode *ip;
465 FS *fs;
466 int flags;
467 struct buf *bp;
468 off_t osize;
469 int resid, xfersize, size, blkoffset;
470 daddr_t lbn;
471 int extended=0;
472 int error;
473 #ifdef LFS_READWRITE
474 bool need_unreserve = false;
475 #endif
476
477 KASSERT(ISSET(ioflag, IO_NODELOCKED));
478 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
479 KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
480 KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
481 KASSERT(uio->uio_rw == UIO_WRITE);
482
483 ip = VTOI(vp);
484 fs = ip->I_FS;
485
486 KASSERT(vp->v_size == ip->i_size);
487
488 if (uio->uio_offset < 0 ||
489 uio->uio_resid > fs->um_maxfilesize ||
490 uio->uio_offset > (fs->um_maxfilesize - uio->uio_resid))
491 return EFBIG;
492 #ifdef LFS_READWRITE
493 KASSERT(vp != fs->lfs_ivnode);
494 #endif
495 if (uio->uio_resid == 0)
496 return 0;
497
498 fstrans_start(vp->v_mount, FSTRANS_SHARED);
499
500 flags = ioflag & IO_SYNC ? B_SYNC : 0;
501 resid = uio->uio_resid;
502 osize = ip->i_size;
503 error = 0;
504
505 KASSERT(vp->v_type != VREG);
506
507 #ifdef LFS_READWRITE
508 lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
509 lfs_check(vp, LFS_UNUSED_LBN, 0);
510 #endif /* !LFS_READWRITE */
511
512 /* XXX Should never have pages cached here. */
513 KASSERT(vp->v_uobj.uo_npages == 0);
514 while (uio->uio_resid > 0) {
515 lbn = lfs_lblkno(fs, uio->uio_offset);
516 blkoffset = lfs_blkoff(fs, uio->uio_offset);
517 xfersize = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
518 if (fs->fs_bsize > xfersize)
519 flags |= B_CLRBUF;
520 else
521 flags &= ~B_CLRBUF;
522
523 #ifdef LFS_READWRITE
524 error = lfs_reserve(fs, vp, NULL,
525 lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
526 if (error)
527 break;
528 need_unreserve = true;
529 #endif
530 error = lfs_balloc(vp, uio->uio_offset, xfersize, cred, flags,
531 &bp);
532
533 if (error)
534 break;
535 if (uio->uio_offset + xfersize > ip->i_size) {
536 ip->i_size = uio->uio_offset + xfersize;
537 DIP_ASSIGN(ip, size, ip->i_size);
538 uvm_vnp_setsize(vp, ip->i_size);
539 extended = 1;
540 }
541 size = lfs_blksize(fs, ip, lbn) - bp->b_resid;
542 if (xfersize > size)
543 xfersize = size;
544
545 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
546
547 /*
548 * if we didn't clear the block and the uiomove failed,
549 * the buf will now contain part of some other file,
550 * so we need to invalidate it.
551 */
552 if (error && (flags & B_CLRBUF) == 0) {
553 brelse(bp, BC_INVAL);
554 break;
555 }
556 #ifdef LFS_READWRITE
557 (void)VOP_BWRITE(bp->b_vp, bp);
558 lfs_reserve(fs, vp, NULL,
559 -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
560 need_unreserve = false;
561 #else
562 if (ioflag & IO_SYNC)
563 (void)bwrite(bp);
564 else if (xfersize + blkoffset == fs->fs_bsize)
565 bawrite(bp);
566 else
567 bdwrite(bp);
568 #endif
569 if (error || xfersize == 0)
570 break;
571 }
572 #ifdef LFS_READWRITE
573 if (need_unreserve) {
574 lfs_reserve(fs, vp, NULL,
575 -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
576 }
577 #endif
578
579 error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
580 extended, error);
581 fstrans_done(vp->v_mount);
582
583 return (error);
584 }
585
586 static int
587 ulfs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
588 kauth_cred_t cred, off_t osize, int resid, int extended, int oerror)
589 {
590 struct inode *ip = VTOI(vp);
591 int error = oerror;
592
593 /* Trigger ctime and mtime updates, and atime if MNT_RELATIME. */
594 ip->i_flag |= IN_CHANGE | IN_UPDATE;
595 if (vp->v_mount->mnt_flag & MNT_RELATIME)
596 ip->i_flag |= IN_ACCESS;
597
598 /*
599 * If we successfully wrote any data and we are not the superuser,
600 * we clear the setuid and setgid bits as a precaution against
601 * tampering.
602 */
603 if (resid > uio->uio_resid && cred) {
604 if (ip->i_mode & ISUID) {
605 if (kauth_authorize_vnode(cred,
606 KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
607 ip->i_mode &= ~ISUID;
608 DIP_ASSIGN(ip, mode, ip->i_mode);
609 }
610 }
611
612 if (ip->i_mode & ISGID) {
613 if (kauth_authorize_vnode(cred,
614 KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
615 ip->i_mode &= ~ISGID;
616 DIP_ASSIGN(ip, mode, ip->i_mode);
617 }
618 }
619 }
620
621 /* If we successfully wrote anything, notify kevent listeners. */
622 if (resid > uio->uio_resid)
623 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
624
625 /*
626 * Update the size on disk: truncate back to original size on
627 * error, or reflect the new size on success.
628 */
629 if (error) {
630 (void) lfs_truncate(vp, osize, ioflag & IO_SYNC, cred);
631 uio->uio_offset -= resid - uio->uio_resid;
632 uio->uio_resid = resid;
633 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
634 error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
635 } else {
636 /* nothing */
637 }
638
639 /* Make sure the vnode uvm size matches the inode file size. */
640 KASSERT(vp->v_size == ip->i_size);
641
642 /* Write error overrides any inode update error. */
643 if (oerror)
644 error = oerror;
645 return error;
646 }
647