lfs_inode.c revision 1.47 1 /* $NetBSD: lfs_inode.c,v 1.47 2000/11/21 00:00:31 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1986, 1989, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_inode.c 8.9 (Berkeley) 5/8/95
71 */
72
73 #if defined(_KERNEL) && !defined(_LKM)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/mount.h>
80 #include <sys/proc.h>
81 #include <sys/file.h>
82 #include <sys/buf.h>
83 #include <sys/vnode.h>
84 #include <sys/kernel.h>
85 #include <sys/malloc.h>
86 #include <sys/trace.h>
87 #include <sys/resourcevar.h>
88
89 #include <ufs/ufs/quota.h>
90 #include <ufs/ufs/inode.h>
91 #include <ufs/ufs/ufsmount.h>
92 #include <ufs/ufs/ufs_extern.h>
93
94 #include <ufs/lfs/lfs.h>
95 #include <ufs/lfs/lfs_extern.h>
96
97 extern int locked_queue_count;
98 extern long locked_queue_bytes;
99
100 static int lfs_update_seguse(struct lfs *, long, size_t);
101 static int lfs_indirtrunc (struct inode *, ufs_daddr_t, ufs_daddr_t,
102 ufs_daddr_t, int, long *, long *, long *, size_t *);
103 static int lfs_blkfree (struct lfs *, daddr_t, size_t, long *, size_t *);
104 static int lfs_vtruncbuf(struct vnode *, daddr_t, int, int);
105
106 /* Search a block for a specific dinode. */
107 struct dinode *
108 lfs_ifind(fs, ino, bp)
109 struct lfs *fs;
110 ino_t ino;
111 struct buf *bp;
112 {
113 int cnt;
114 struct dinode *dip = (struct dinode *)bp->b_data;
115 struct dinode *ldip;
116
117 for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
118 if (ldip->di_inumber == ino)
119 return (ldip);
120
121 printf("offset is 0x%x (seg %d)\n", fs->lfs_offset,
122 datosn(fs,fs->lfs_offset));
123 printf("block is 0x%x (seg %d)\n", bp->b_blkno,
124 datosn(fs,bp->b_blkno));
125 panic("lfs_ifind: dinode %u not found", ino);
126 /* NOTREACHED */
127 }
128
129 int
130 lfs_update(v)
131 void *v;
132 {
133 struct vop_update_args /* {
134 struct vnode *a_vp;
135 struct timespec *a_access;
136 struct timespec *a_modify;
137 int a_flags;
138 } */ *ap = v;
139 struct inode *ip;
140 struct vnode *vp = ap->a_vp;
141 int oflag;
142 struct timespec ts;
143 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
144
145 if (vp->v_mount->mnt_flag & MNT_RDONLY)
146 return (0);
147 ip = VTOI(vp);
148
149 /*
150 * If we are called from vinvalbuf, and the file's blocks have
151 * already been scheduled for writing, but the writes have not
152 * yet completed, lfs_vflush will not be called, and vinvalbuf
153 * will cause a panic. So, we must wait until any pending write
154 * for our inode completes, if we are called with UPDATE_WAIT set.
155 */
156 while((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
157 WRITEINPROG(vp)) {
158 #ifdef DEBUG_LFS
159 printf("lfs_update: sleeping on inode %d (in-progress)\n",
160 ip->i_number);
161 #endif
162 tsleep(vp, (PRIBIO+1), "lfs_update", 0);
163 }
164 oflag = ip->i_flag;
165 TIMEVAL_TO_TIMESPEC(&time, &ts);
166 LFS_ITIMES(ip,
167 ap->a_access ? ap->a_access : &ts,
168 ap->a_modify ? ap->a_modify : &ts, &ts);
169 if ((ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING)) == 0) {
170 return (0);
171 }
172
173 /* If sync, push back the vnode and any dirty blocks it may have. */
174 if((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP))==UPDATE_WAIT) {
175 /* Avoid flushing VDIROP. */
176 ++fs->lfs_diropwait;
177 while(vp->v_flag & VDIROP) {
178 #ifdef DEBUG_LFS
179 printf("lfs_update: sleeping on inode %d (dirops)\n",
180 ip->i_number);
181 printf("lfs_update: vflags 0x%lx, iflags 0x%x\n",
182 vp->v_flag, ip->i_flag);
183 #endif
184 if(fs->lfs_dirops == 0)
185 lfs_flush_fs(fs, SEGM_SYNC);
186 else
187 tsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
188 0);
189 /* XXX KS - by falling out here, are we writing the vn
190 twice? */
191 }
192 --fs->lfs_diropwait;
193 return lfs_vflush(vp);
194 }
195 return 0;
196 }
197
198 #define SINGLE 0 /* index of single indirect block */
199 #define DOUBLE 1 /* index of double indirect block */
200 #define TRIPLE 2 /* index of triple indirect block */
201 /*
202 * Truncate the inode oip to at most length size, freeing the
203 * disk blocks.
204 */
205 /* VOP_BWRITE 1 + NIADDR + VOP_BALLOC == 2 + 2*NIADDR times */
206 int
207 lfs_truncate(v)
208 void *v;
209 {
210 struct vop_truncate_args /* {
211 struct vnode *a_vp;
212 off_t a_length;
213 int a_flags;
214 struct ucred *a_cred;
215 struct proc *a_p;
216 } */ *ap = v;
217 struct vnode *ovp = ap->a_vp;
218 ufs_daddr_t lastblock;
219 struct inode *oip;
220 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
221 ufs_daddr_t newblks[NDADDR + NIADDR];
222 off_t length = ap->a_length;
223 struct lfs *fs;
224 struct buf *bp;
225 int offset, size, level;
226 long count, rcount, nblocks, blocksreleased = 0, real_released = 0;
227 int i;
228 int aflags, error, allerror = 0;
229 off_t osize;
230 long lastseg;
231 size_t bc;
232 int obufsize, odb;
233
234 if (length < 0)
235 return (EINVAL);
236 oip = VTOI(ovp);
237
238 /*
239 * Just return and not update modification times.
240 */
241 if (oip->i_ffs_size == length)
242 return (0);
243
244 if (ovp->v_type == VLNK &&
245 (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
246 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
247 oip->i_din.ffs_din.di_blocks == 0))) {
248 #ifdef DIAGNOSTIC
249 if (length != 0)
250 panic("lfs_truncate: partial truncate of symlink");
251 #endif
252 memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
253 oip->i_ffs_size = 0;
254 oip->i_flag |= IN_CHANGE | IN_UPDATE;
255 return (VOP_UPDATE(ovp, NULL, NULL, 0));
256 }
257 if (oip->i_ffs_size == length) {
258 oip->i_flag |= IN_CHANGE | IN_UPDATE;
259 return (VOP_UPDATE(ovp, NULL, NULL, 0));
260 }
261 #ifdef QUOTA
262 if ((error = getinoquota(oip)) != 0)
263 return (error);
264 #endif
265 fs = oip->i_lfs;
266 lfs_imtime(fs);
267 osize = oip->i_ffs_size;
268 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
269
270 /*
271 * Lengthen the size of the file. We must ensure that the
272 * last byte of the file is allocated. Since the smallest
273 * value of osize is 0, length will be at least 1.
274 */
275 if (osize < length) {
276 if (length > fs->lfs_maxfilesize)
277 return (EFBIG);
278 aflags = B_CLRBUF;
279 if (ap->a_flags & IO_SYNC)
280 aflags |= B_SYNC;
281 error = lfs_reserve(fs, ovp, fsbtodb(fs, NIADDR + 2));
282 if (error)
283 return (error);
284 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
285 lfs_reserve(fs, ovp, -fsbtodb(fs, NIADDR + 2));
286 if (error)
287 return (error);
288 oip->i_ffs_size = length;
289 uvm_vnp_setsize(ovp, length);
290 (void) uvm_vnp_uncache(ovp);
291 (void) VOP_BWRITE(bp);
292 oip->i_flag |= IN_CHANGE | IN_UPDATE;
293 return (VOP_UPDATE(ovp, NULL, NULL, 0));
294 }
295
296 if ((error = lfs_reserve(fs, ovp, fsbtodb(fs, 2 * NIADDR + 3))) != 0)
297 return (error);
298 /*
299 * Make sure no writes to this inode can happen while we're
300 * truncating. Otherwise, blocks which are accounted for on the
301 * inode *and* which have been created for cleaning can coexist,
302 * and cause an overcounting.
303 *
304 * (We don't need to *hold* the seglock, though, because we already
305 * hold the inode lock; draining the seglock is sufficient.)
306 */
307 if (ovp != fs->lfs_unlockvp) {
308 while(fs->lfs_seglock) {
309 tsleep(&fs->lfs_seglock, PRIBIO+1, "lfs_truncate", 0);
310 }
311 }
312
313 /*
314 * Shorten the size of the file. If the file is not being
315 * truncated to a block boundary, the contents of the
316 * partial block following the end of the file must be
317 * zero'ed in case it ever becomes accessible again because
318 * of subsequent file growth. Directories however are not
319 * zero'ed as they should grow back initialized to empty.
320 */
321 offset = blkoff(fs, length);
322 lastseg = -1;
323 bc = 0;
324 if (offset == 0) {
325 oip->i_ffs_size = length;
326 } else {
327 lbn = lblkno(fs, length);
328 aflags = B_CLRBUF;
329 if (ap->a_flags & IO_SYNC)
330 aflags |= B_SYNC;
331 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
332 if (error) {
333 lfs_reserve(fs, ovp, -fsbtodb(fs, 2 * NIADDR + 3));
334 return (error);
335 }
336 obufsize = bp->b_bufsize;
337 odb = btodb(bp->b_bcount);
338 oip->i_ffs_size = length;
339 size = blksize(fs, oip, lbn);
340 (void) uvm_vnp_uncache(ovp);
341 if (ovp->v_type != VDIR)
342 memset((char *)bp->b_data + offset, 0,
343 (u_int)(size - offset));
344 allocbuf(bp, size);
345 if (bp->b_flags & B_DELWRI) {
346 if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
347 locked_queue_bytes -= obufsize - bp->b_bufsize;
348 fs->lfs_avail += odb - btodb(size);
349 }
350 (void) VOP_BWRITE(bp);
351 }
352 uvm_vnp_setsize(ovp, length);
353 /*
354 * Calculate index into inode's block list of
355 * last direct and indirect blocks (if any)
356 * which we want to keep. Lastblock is -1 when
357 * the file is truncated to 0.
358 */
359 lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
360 lastiblock[SINGLE] = lastblock - NDADDR;
361 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
362 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
363 nblocks = btodb(fs->lfs_bsize);
364 /*
365 * Record changed file and block pointers before we start
366 * freeing blocks. lastiblock values are also normalized to -1
367 * for calls to lfs_indirtrunc below.
368 */
369 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
370 for (level = TRIPLE; level >= SINGLE; level--)
371 if (lastiblock[level] < 0) {
372 newblks[NDADDR+level] = 0;
373 lastiblock[level] = -1;
374 }
375 for (i = NDADDR - 1; i > lastblock; i--)
376 newblks[i] = 0;
377
378 oip->i_ffs_size = osize;
379 error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
380 if (error && !allerror)
381 allerror = error;
382
383 /*
384 * Indirect blocks first.
385 */
386 indir_lbn[SINGLE] = -NDADDR;
387 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
388 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
389 for (level = TRIPLE; level >= SINGLE; level--) {
390 bn = oip->i_ffs_ib[level];
391 if (bn != 0) {
392 error = lfs_indirtrunc(oip, indir_lbn[level],
393 bn, lastiblock[level],
394 level, &count, &rcount,
395 &lastseg, &bc);
396 if (error)
397 allerror = error;
398 real_released += rcount;
399 blocksreleased += count;
400 if (lastiblock[level] < 0) {
401 if (oip->i_ffs_ib[level] > 0)
402 real_released += nblocks;
403 blocksreleased += nblocks;
404 oip->i_ffs_ib[level] = 0;
405 lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
406 }
407 }
408 if (lastiblock[level] >= 0)
409 goto done;
410 }
411
412 /*
413 * All whole direct blocks or frags.
414 */
415 for (i = NDADDR - 1; i > lastblock; i--) {
416 long bsize;
417
418 bn = oip->i_ffs_db[i];
419 if (bn == 0)
420 continue;
421 bsize = blksize(fs, oip, i);
422 if (oip->i_ffs_db[i] > 0)
423 real_released += btodb(bsize);
424 blocksreleased += btodb(bsize);
425 oip->i_ffs_db[i] = 0;
426 lfs_blkfree(fs, bn, bsize, &lastseg, &bc);
427 }
428 if (lastblock < 0)
429 goto done;
430
431 /*
432 * Finally, look for a change in size of the
433 * last direct block; release any frags.
434 */
435 bn = oip->i_ffs_db[lastblock];
436 if (bn != 0) {
437 long oldspace, newspace;
438
439 /*
440 * Calculate amount of space we're giving
441 * back as old block size minus new block size.
442 */
443 oldspace = blksize(fs, oip, lastblock);
444 oip->i_ffs_size = length;
445 newspace = blksize(fs, oip, lastblock);
446 if (newspace == 0)
447 panic("itrunc: newspace");
448 if (oldspace - newspace > 0) {
449 lfs_blkfree(fs, bn, oldspace - newspace, &lastseg, &bc);
450 if (bn > 0)
451 real_released += btodb(oldspace - newspace);
452 blocksreleased += btodb(oldspace - newspace);
453 }
454 }
455
456 done:
457 /* Finish segment accounting corrections */
458 lfs_update_seguse(fs, lastseg, bc);
459 #ifdef DIAGNOSTIC
460 for (level = SINGLE; level <= TRIPLE; level++)
461 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
462 panic("lfs itrunc1");
463 for (i = 0; i < NDADDR; i++)
464 if (newblks[i] != oip->i_ffs_db[i])
465 panic("lfs itrunc2");
466 if (length == 0 &&
467 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
468 panic("lfs itrunc3");
469 #endif /* DIAGNOSTIC */
470 /*
471 * Put back the real size.
472 */
473 oip->i_ffs_size = length;
474 oip->i_lfs_effnblks -= blocksreleased;
475 oip->i_ffs_blocks -= real_released;
476 fs->lfs_bfree += blocksreleased;
477 #ifdef DIAGNOSTIC
478 if (oip->i_ffs_size == 0 && oip->i_ffs_blocks != 0) {
479 printf("lfs_truncate: truncate to 0 but %d blocks on inode\n",
480 oip->i_ffs_blocks);
481 panic("lfs_truncate: persistent blocks\n");
482 }
483 #endif
484 oip->i_flag |= IN_CHANGE;
485 #ifdef QUOTA
486 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
487 #endif
488 lfs_reserve(fs, ovp, -fsbtodb(fs, 2 * NIADDR + 3));
489 return (allerror);
490 }
491
492 /* Update segment usage information when removing a block. */
493 static int
494 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
495 size_t *num)
496 {
497 long seg;
498 int error = 0;
499
500 bsize = fragroundup(fs, bsize);
501 if (daddr > 0) {
502 if (*lastseg != (seg = datosn(fs, daddr))) {
503 error = lfs_update_seguse(fs, *lastseg, *num);
504 *num = bsize;
505 *lastseg = seg;
506 } else
507 *num += bsize;
508 }
509 return error;
510 }
511
512 /* Finish the accounting updates for a segment. */
513 static int
514 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
515 {
516 SEGUSE *sup;
517 struct buf *bp;
518
519 if (lastseg < 0 || num == 0)
520 return 0;
521
522
523 LFS_SEGENTRY(sup, fs, lastseg, bp);
524 if (num > sup->su_nbytes) {
525 printf("lfs_truncate: segment %ld short by %ld\n",
526 lastseg, (long)num - sup->su_nbytes);
527 panic("lfs_truncate: negative bytes");
528 sup->su_nbytes = num;
529 }
530 sup->su_nbytes -= num;
531 return (VOP_BWRITE(bp)); /* Ifile */
532 }
533
534 /*
535 * Release blocks associated with the inode ip and stored in the indirect
536 * block bn. Blocks are free'd in LIFO order up to (but not including)
537 * lastbn. If level is greater than SINGLE, the block is an indirect block
538 * and recursive calls to indirtrunc must be used to cleanse other indirect
539 * blocks.
540 *
541 * NB: triple indirect blocks are untested.
542 */
543 static int
544 lfs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, daddr_t dbn,
545 ufs_daddr_t lastbn, int level, long *countp,
546 long *rcountp, long *lastsegp, size_t *bcp)
547 {
548 int i;
549 struct buf *bp;
550 struct lfs *fs = ip->i_lfs;
551 ufs_daddr_t *bap;
552 struct vnode *vp;
553 ufs_daddr_t *copy = NULL, nb, nlbn, last;
554 long blkcount, rblkcount, factor;
555 int nblocks, blocksreleased = 0, real_released = 0;
556 int error = 0, allerror = 0;
557
558 /*
559 * Calculate index in current block of last
560 * block to be kept. -1 indicates the entire
561 * block so we need not calculate the index.
562 */
563 factor = 1;
564 for (i = SINGLE; i < level; i++)
565 factor *= NINDIR(fs);
566 last = lastbn;
567 if (lastbn > 0)
568 last /= factor;
569 nblocks = btodb(fs->lfs_bsize);
570 /*
571 * Get buffer of block pointers, zero those entries corresponding
572 * to blocks to be free'd, and update on disk copy first. Since
573 * double(triple) indirect before single(double) indirect, calls
574 * to bmap on these blocks will fail. However, we already have
575 * the on disk address, so we have to set the b_blkno field
576 * explicitly instead of letting bread do everything for us.
577 */
578 vp = ITOV(ip);
579 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
580 if (bp->b_flags & (B_DONE | B_DELWRI)) {
581 /* Braces must be here in case trace evaluates to nothing. */
582 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
583 } else {
584 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
585 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
586 bp->b_flags |= B_READ;
587 if (bp->b_bcount > bp->b_bufsize)
588 panic("lfs_indirtrunc: bad buffer size");
589 bp->b_blkno = dbn;
590 VOP_STRATEGY(bp);
591 error = biowait(bp);
592 }
593 if (error) {
594 brelse(bp);
595 *countp = *rcountp = 0;
596 return (error);
597 }
598
599 bap = (ufs_daddr_t *)bp->b_data;
600 if (lastbn >= 0) {
601 MALLOC(copy, ufs_daddr_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
602 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
603 memset((caddr_t)&bap[last + 1], 0,
604 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
605 error = VOP_BWRITE(bp);
606 if (error)
607 allerror = error;
608 bap = copy;
609 }
610
611 /*
612 * Recursively free totally unused blocks.
613 */
614 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
615 i--, nlbn += factor) {
616 nb = bap[i];
617 if (nb == 0)
618 continue;
619 if (level > SINGLE) {
620 error = lfs_indirtrunc(ip, nlbn, nb,
621 (ufs_daddr_t)-1, level - 1,
622 &blkcount, &rblkcount,
623 lastsegp, bcp);
624 if (error)
625 allerror = error;
626 blocksreleased += blkcount;
627 real_released += rblkcount;
628 }
629 lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
630 if (bap[i] > 0)
631 real_released += nblocks;
632 blocksreleased += nblocks;
633 }
634
635 /*
636 * Recursively free last partial block.
637 */
638 if (level > SINGLE && lastbn >= 0) {
639 last = lastbn % factor;
640 nb = bap[i];
641 if (nb != 0) {
642 error = lfs_indirtrunc(ip, nlbn, nb,
643 last, level - 1, &blkcount,
644 &rblkcount, lastsegp, bcp);
645 if (error)
646 allerror = error;
647 real_released += rblkcount;
648 blocksreleased += blkcount;
649 }
650 }
651
652 if (copy != NULL) {
653 FREE(copy, M_TEMP);
654 } else {
655 if (bp->b_flags & B_DELWRI) {
656 LFS_UNLOCK_BUF(bp);
657 fs->lfs_avail += btodb(bp->b_bcount);
658 wakeup(&fs->lfs_avail);
659 }
660 bp->b_flags |= B_INVAL;
661 brelse(bp);
662 }
663
664 *countp = blocksreleased;
665 *rcountp = real_released;
666 return (allerror);
667 }
668
669 /*
670 * Destroy any in core blocks past the truncation length.
671 * Inlined from vtruncbuf, so that lfs_avail could be updated.
672 */
673 static int
674 lfs_vtruncbuf(vp, lbn, slpflag, slptimeo)
675 struct vnode *vp;
676 daddr_t lbn;
677 int slpflag, slptimeo;
678 {
679 struct buf *bp, *nbp;
680 int s, error;
681 struct lfs *fs;
682
683 fs = VTOI(vp)->i_lfs;
684 s = splbio();
685
686 restart:
687 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
688 nbp = LIST_NEXT(bp, b_vnbufs);
689 if (bp->b_lblkno < lbn)
690 continue;
691 if (bp->b_flags & B_BUSY) {
692 bp->b_flags |= B_WANTED;
693 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
694 "lfs_vtruncbuf", slptimeo);
695 if (error) {
696 splx(s);
697 return (error);
698 }
699 goto restart;
700 }
701 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
702 if (bp->b_flags & B_DELWRI) {
703 bp->b_flags &= ~B_DELWRI;
704 fs->lfs_avail += btodb(bp->b_bcount);
705 wakeup(&fs->lfs_avail);
706 }
707 LFS_UNLOCK_BUF(bp);
708 brelse(bp);
709 }
710
711 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
712 nbp = LIST_NEXT(bp, b_vnbufs);
713 if (bp->b_lblkno < lbn)
714 continue;
715 if (bp->b_flags & B_BUSY) {
716 bp->b_flags |= B_WANTED;
717 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
718 "lfs_vtruncbuf", slptimeo);
719 if (error) {
720 splx(s);
721 return (error);
722 }
723 goto restart;
724 }
725 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
726 if (bp->b_flags & B_DELWRI) {
727 bp->b_flags &= ~B_DELWRI;
728 fs->lfs_avail += btodb(bp->b_bcount);
729 wakeup(&fs->lfs_avail);
730 }
731 LFS_UNLOCK_BUF(bp);
732 brelse(bp);
733 }
734
735 splx(s);
736
737 return (0);
738 }
739
740