lfs_inode.c revision 1.44 1 /* $NetBSD: lfs_inode.c,v 1.44 2000/09/09 04:49:55 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
233 if (length < 0)
234 return (EINVAL);
235 oip = VTOI(ovp);
236
237 /*
238 * Just return and not update modification times.
239 */
240 if (oip->i_ffs_size == length)
241 return (0);
242
243 if (ovp->v_type == VLNK &&
244 (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
245 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
246 oip->i_din.ffs_din.di_blocks == 0))) {
247 #ifdef DIAGNOSTIC
248 if (length != 0)
249 panic("lfs_truncate: partial truncate of symlink");
250 #endif
251 memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
252 oip->i_ffs_size = 0;
253 oip->i_flag |= IN_CHANGE | IN_UPDATE;
254 return (VOP_UPDATE(ovp, NULL, NULL, 0));
255 }
256 if (oip->i_ffs_size == length) {
257 oip->i_flag |= IN_CHANGE | IN_UPDATE;
258 return (VOP_UPDATE(ovp, NULL, NULL, 0));
259 }
260 #ifdef QUOTA
261 if ((error = getinoquota(oip)) != 0)
262 return (error);
263 #endif
264 fs = oip->i_lfs;
265 lfs_imtime(fs);
266 osize = oip->i_ffs_size;
267 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
268
269 /*
270 * Lengthen the size of the file. We must ensure that the
271 * last byte of the file is allocated. Since the smallest
272 * value of osize is 0, length will be at least 1.
273 */
274 if (osize < length) {
275 if (length > fs->lfs_maxfilesize)
276 return (EFBIG);
277 aflags = B_CLRBUF;
278 if (ap->a_flags & IO_SYNC)
279 aflags |= B_SYNC;
280 error = lfs_reserve(fs, ovp, fsbtodb(fs, NIADDR + 2));
281 if (error)
282 return (error);
283 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
284 lfs_reserve(fs, ovp, -fsbtodb(fs, NIADDR + 2));
285 if (error)
286 return (error);
287 oip->i_ffs_size = length;
288 uvm_vnp_setsize(ovp, length);
289 (void) uvm_vnp_uncache(ovp);
290 (void) VOP_BWRITE(bp);
291 oip->i_flag |= IN_CHANGE | IN_UPDATE;
292 return (VOP_UPDATE(ovp, NULL, NULL, 0));
293 }
294
295 if ((error = lfs_reserve(fs, ovp, fsbtodb(fs, 2 * NIADDR + 3))) != 0)
296 return (error);
297 /*
298 * Make sure no writes to this inode can happen while we're
299 * truncating. Otherwise, blocks which are accounted for on the
300 * inode *and* which have been created for cleaning can coexist,
301 * and cause an overcounting.
302 *
303 * (We don't need to *hold* the seglock, though, because we already
304 * hold the inode lock; draining the seglock is sufficient.)
305 */
306 if (ovp != fs->lfs_unlockvp) {
307 while(fs->lfs_seglock) {
308 tsleep(&fs->lfs_seglock, PRIBIO+1, "lfs_truncate", 0);
309 }
310 }
311
312 /*
313 * Shorten the size of the file. If the file is not being
314 * truncated to a block boundary, the contents of the
315 * partial block following the end of the file must be
316 * zero'ed in case it ever becomes accessible again because
317 * of subsequent file growth. Directories however are not
318 * zero'ed as they should grow back initialized to empty.
319 */
320 offset = blkoff(fs, length);
321 lastseg = -1;
322 bc = 0;
323 if (offset == 0) {
324 oip->i_ffs_size = length;
325 } else {
326 lbn = lblkno(fs, length);
327 aflags = B_CLRBUF;
328 if (ap->a_flags & IO_SYNC)
329 aflags |= B_SYNC;
330 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
331 if (error) {
332 lfs_reserve(fs, ovp, -fsbtodb(fs, 2 * NIADDR + 3));
333 return (error);
334 }
335 oip->i_ffs_size = length;
336 size = blksize(fs, oip, lbn);
337 (void) uvm_vnp_uncache(ovp);
338 if (ovp->v_type != VDIR)
339 memset((char *)bp->b_data + offset, 0,
340 (u_int)(size - offset));
341 allocbuf(bp, size);
342 (void) VOP_BWRITE(bp);
343 }
344 uvm_vnp_setsize(ovp, length);
345 /*
346 * Calculate index into inode's block list of
347 * last direct and indirect blocks (if any)
348 * which we want to keep. Lastblock is -1 when
349 * the file is truncated to 0.
350 */
351 lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
352 lastiblock[SINGLE] = lastblock - NDADDR;
353 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
354 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
355 nblocks = btodb(fs->lfs_bsize);
356 /*
357 * Record changed file and block pointers before we start
358 * freeing blocks. lastiblock values are also normalized to -1
359 * for calls to lfs_indirtrunc below.
360 */
361 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
362 for (level = TRIPLE; level >= SINGLE; level--)
363 if (lastiblock[level] < 0) {
364 newblks[NDADDR+level] = 0;
365 lastiblock[level] = -1;
366 }
367 for (i = NDADDR - 1; i > lastblock; i--)
368 newblks[i] = 0;
369
370 oip->i_ffs_size = osize;
371 error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
372 if (error && !allerror)
373 allerror = error;
374
375 /*
376 * Indirect blocks first.
377 */
378 indir_lbn[SINGLE] = -NDADDR;
379 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
380 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
381 for (level = TRIPLE; level >= SINGLE; level--) {
382 bn = oip->i_ffs_ib[level];
383 if (bn != 0) {
384 error = lfs_indirtrunc(oip, indir_lbn[level],
385 bn, lastiblock[level],
386 level, &count, &rcount,
387 &lastseg, &bc);
388 if (error)
389 allerror = error;
390 real_released += rcount;
391 blocksreleased += count;
392 if (lastiblock[level] < 0) {
393 if (oip->i_ffs_ib[level] > 0)
394 real_released += nblocks;
395 blocksreleased += nblocks;
396 oip->i_ffs_ib[level] = 0;
397 lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
398 }
399 }
400 if (lastiblock[level] >= 0)
401 goto done;
402 }
403
404 /*
405 * All whole direct blocks or frags.
406 */
407 for (i = NDADDR - 1; i > lastblock; i--) {
408 long bsize;
409
410 bn = oip->i_ffs_db[i];
411 if (bn == 0)
412 continue;
413 bsize = blksize(fs, oip, i);
414 if (oip->i_ffs_db[i] > 0)
415 real_released += btodb(bsize);
416 blocksreleased += btodb(bsize);
417 oip->i_ffs_db[i] = 0;
418 lfs_blkfree(fs, bn, bsize, &lastseg, &bc);
419 }
420 if (lastblock < 0)
421 goto done;
422
423 /*
424 * Finally, look for a change in size of the
425 * last direct block; release any frags.
426 */
427 bn = oip->i_ffs_db[lastblock];
428 if (bn != 0) {
429 long oldspace, newspace;
430
431 /*
432 * Calculate amount of space we're giving
433 * back as old block size minus new block size.
434 */
435 oldspace = blksize(fs, oip, lastblock);
436 oip->i_ffs_size = length;
437 newspace = blksize(fs, oip, lastblock);
438 if (newspace == 0)
439 panic("itrunc: newspace");
440 if (oldspace - newspace > 0) {
441 lfs_blkfree(fs, bn, oldspace - newspace, &lastseg, &bc);
442 real_released += btodb(oldspace - newspace);
443 blocksreleased += btodb(oldspace - newspace);
444 }
445 }
446
447 done:
448 /* Finish segment accounting corrections */
449 lfs_update_seguse(fs, lastseg, bc);
450 #ifdef DIAGNOSTIC
451 for (level = SINGLE; level <= TRIPLE; level++)
452 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
453 panic("lfs itrunc1");
454 for (i = 0; i < NDADDR; i++)
455 if (newblks[i] != oip->i_ffs_db[i])
456 panic("lfs itrunc2");
457 if (length == 0 &&
458 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
459 panic("lfs itrunc3");
460 #endif /* DIAGNOSTIC */
461 /*
462 * Put back the real size.
463 */
464 oip->i_ffs_size = length;
465 oip->i_lfs_effnblks -= blocksreleased;
466 oip->i_ffs_blocks -= real_released;
467 fs->lfs_bfree += blocksreleased;
468 #ifdef DIAGNOSTIC
469 if (oip->i_ffs_size == 0 && oip->i_ffs_blocks > 0) {
470 printf("lfs_tuncate: truncate to 0 but %d blocks on inode\n",
471 oip->i_ffs_blocks);
472 panic("lfs_truncate: persistent blocks\n");
473 }
474 #endif
475 if (oip->i_ffs_blocks < 0) {
476 #ifdef DIAGNOSTIC
477 panic("lfs_truncate: negative block count\n");
478 #endif
479 oip->i_ffs_blocks = 0;
480 }
481 oip->i_flag |= IN_CHANGE;
482 #ifdef QUOTA
483 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
484 #endif
485 lfs_reserve(fs, ovp, -fsbtodb(fs, 2 * NIADDR + 3));
486 return (allerror);
487 }
488
489 /* Update segment usage information when removing a block. */
490 static int
491 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
492 size_t *num)
493 {
494 long seg;
495 int error = 0;
496
497 bsize = fragroundup(fs, bsize);
498 if (daddr > 0) {
499 if (*lastseg != (seg = datosn(fs, daddr))) {
500 error = lfs_update_seguse(fs, *lastseg, *num);
501 *num = bsize;
502 *lastseg = seg;
503 } else
504 *num += bsize;
505 }
506 return error;
507 }
508
509 /* Finish the accounting updates for a segment. */
510 static int
511 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
512 {
513 SEGUSE *sup;
514 struct buf *bp;
515
516 if (lastseg < 0 || num == 0)
517 return 0;
518
519
520 LFS_SEGENTRY(sup, fs, lastseg, bp);
521 if (num > sup->su_nbytes) {
522 printf("lfs_truncate: segment %ld short by %ld\n",
523 lastseg, (long)num - sup->su_nbytes);
524 panic("lfs_truncate: negative bytes");
525 sup->su_nbytes = num;
526 }
527 sup->su_nbytes -= num;
528 return (VOP_BWRITE(bp)); /* Ifile */
529 }
530
531 /*
532 * Release blocks associated with the inode ip and stored in the indirect
533 * block bn. Blocks are free'd in LIFO order up to (but not including)
534 * lastbn. If level is greater than SINGLE, the block is an indirect block
535 * and recursive calls to indirtrunc must be used to cleanse other indirect
536 * blocks.
537 *
538 * NB: triple indirect blocks are untested.
539 */
540 static int
541 lfs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, daddr_t dbn,
542 ufs_daddr_t lastbn, int level, long *countp,
543 long *rcountp, long *lastsegp, size_t *bcp)
544 {
545 int i;
546 struct buf *bp;
547 struct lfs *fs = ip->i_lfs;
548 ufs_daddr_t *bap;
549 struct vnode *vp;
550 ufs_daddr_t *copy = NULL, nb, nlbn, last;
551 long blkcount, rblkcount, factor;
552 int nblocks, blocksreleased = 0, real_released = 0;
553 int error = 0, allerror = 0;
554
555 /*
556 * Calculate index in current block of last
557 * block to be kept. -1 indicates the entire
558 * block so we need not calculate the index.
559 */
560 factor = 1;
561 for (i = SINGLE; i < level; i++)
562 factor *= NINDIR(fs);
563 last = lastbn;
564 if (lastbn > 0)
565 last /= factor;
566 nblocks = btodb(fs->lfs_bsize);
567 /*
568 * Get buffer of block pointers, zero those entries corresponding
569 * to blocks to be free'd, and update on disk copy first. Since
570 * double(triple) indirect before single(double) indirect, calls
571 * to bmap on these blocks will fail. However, we already have
572 * the on disk address, so we have to set the b_blkno field
573 * explicitly instead of letting bread do everything for us.
574 */
575 vp = ITOV(ip);
576 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
577 if (bp->b_flags & (B_DONE | B_DELWRI)) {
578 /* Braces must be here in case trace evaluates to nothing. */
579 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
580 } else {
581 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
582 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
583 bp->b_flags |= B_READ;
584 if (bp->b_bcount > bp->b_bufsize)
585 panic("lfs_indirtrunc: bad buffer size");
586 bp->b_blkno = dbn;
587 VOP_STRATEGY(bp);
588 error = biowait(bp);
589 }
590 if (error) {
591 brelse(bp);
592 *countp = *rcountp = 0;
593 return (error);
594 }
595
596 bap = (ufs_daddr_t *)bp->b_data;
597 if (lastbn >= 0) {
598 MALLOC(copy, ufs_daddr_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
599 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
600 memset((caddr_t)&bap[last + 1], 0,
601 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
602 error = VOP_BWRITE(bp);
603 if (error)
604 allerror = error;
605 bap = copy;
606 }
607
608 /*
609 * Recursively free totally unused blocks.
610 */
611 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
612 i--, nlbn += factor) {
613 nb = bap[i];
614 if (nb == 0)
615 continue;
616 if (level > SINGLE) {
617 error = lfs_indirtrunc(ip, nlbn, nb,
618 (ufs_daddr_t)-1, level - 1,
619 &blkcount, &rblkcount,
620 lastsegp, bcp);
621 if (error)
622 allerror = error;
623 blocksreleased += blkcount;
624 real_released += rblkcount;
625 }
626 lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
627 if (bap[i] > 0)
628 real_released += nblocks;
629 blocksreleased += nblocks;
630 }
631
632 /*
633 * Recursively free last partial block.
634 */
635 if (level > SINGLE && lastbn >= 0) {
636 last = lastbn % factor;
637 nb = bap[i];
638 if (nb != 0) {
639 error = lfs_indirtrunc(ip, nlbn, nb,
640 last, level - 1, &blkcount,
641 &rblkcount, lastsegp, bcp);
642 if (error)
643 allerror = error;
644 real_released += rblkcount;
645 blocksreleased += blkcount;
646 }
647 }
648
649 if (copy != NULL) {
650 FREE(copy, M_TEMP);
651 } else {
652 bp->b_flags |= B_INVAL;
653 brelse(bp);
654 }
655
656 *countp = blocksreleased;
657 *rcountp = real_released;
658 return (allerror);
659 }
660
661 /*
662 * Destroy any in core blocks past the truncation length.
663 * Inlined from vtruncbuf, so that lfs_avail could be updated.
664 */
665 static int
666 lfs_vtruncbuf(vp, lbn, slpflag, slptimeo)
667 struct vnode *vp;
668 daddr_t lbn;
669 int slpflag, slptimeo;
670 {
671 struct buf *bp, *nbp;
672 int s, error;
673 struct lfs *fs;
674
675 fs = VTOI(vp)->i_lfs;
676 s = splbio();
677
678 restart:
679 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
680 nbp = LIST_NEXT(bp, b_vnbufs);
681 if (bp->b_lblkno < lbn)
682 continue;
683 if (bp->b_flags & B_BUSY) {
684 bp->b_flags |= B_WANTED;
685 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
686 "lfs_vtruncbuf", slptimeo);
687 if (error) {
688 splx(s);
689 return (error);
690 }
691 goto restart;
692 }
693 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
694 if (bp->b_flags & B_DELWRI) {
695 bp->b_flags &= ~B_DELWRI;
696 fs->lfs_avail += btodb(bp->b_bcount);
697 }
698 if (bp->b_flags & B_LOCKED) {
699 bp->b_flags &= ~B_LOCKED;
700 --locked_queue_count;
701 locked_queue_bytes -= bp->b_bcount;
702 }
703 brelse(bp);
704 }
705
706 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
707 nbp = LIST_NEXT(bp, b_vnbufs);
708 if (bp->b_lblkno < lbn)
709 continue;
710 if (bp->b_flags & B_BUSY) {
711 bp->b_flags |= B_WANTED;
712 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
713 "lfs_vtruncbuf", slptimeo);
714 if (error) {
715 splx(s);
716 return (error);
717 }
718 goto restart;
719 }
720 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
721 if (bp->b_flags & B_DELWRI) {
722 bp->b_flags &= ~B_DELWRI;
723 fs->lfs_avail += btodb(bp->b_bcount);
724 }
725 if (bp->b_flags & B_LOCKED) {
726 bp->b_flags &= ~B_LOCKED;
727 --locked_queue_count;
728 locked_queue_bytes -= bp->b_bcount;
729 }
730 brelse(bp);
731 }
732
733 splx(s);
734
735 return (0);
736 }
737
738