lfs_inode.c revision 1.46 1 /* $NetBSD: lfs_inode.c,v 1.46 2000/11/17 19:14:41 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 if (bn > 0)
443 real_released += btodb(oldspace - newspace);
444 blocksreleased += btodb(oldspace - newspace);
445 }
446 }
447
448 done:
449 /* Finish segment accounting corrections */
450 lfs_update_seguse(fs, lastseg, bc);
451 #ifdef DIAGNOSTIC
452 for (level = SINGLE; level <= TRIPLE; level++)
453 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
454 panic("lfs itrunc1");
455 for (i = 0; i < NDADDR; i++)
456 if (newblks[i] != oip->i_ffs_db[i])
457 panic("lfs itrunc2");
458 if (length == 0 &&
459 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
460 panic("lfs itrunc3");
461 #endif /* DIAGNOSTIC */
462 /*
463 * Put back the real size.
464 */
465 oip->i_ffs_size = length;
466 oip->i_lfs_effnblks -= blocksreleased;
467 oip->i_ffs_blocks -= real_released;
468 fs->lfs_bfree += blocksreleased;
469 #ifdef DIAGNOSTIC
470 if (oip->i_ffs_size == 0 && oip->i_ffs_blocks != 0) {
471 printf("lfs_truncate: truncate to 0 but %d blocks on inode\n",
472 oip->i_ffs_blocks);
473 panic("lfs_truncate: persistent blocks\n");
474 }
475 #endif
476 oip->i_flag |= IN_CHANGE;
477 #ifdef QUOTA
478 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
479 #endif
480 lfs_reserve(fs, ovp, -fsbtodb(fs, 2 * NIADDR + 3));
481 return (allerror);
482 }
483
484 /* Update segment usage information when removing a block. */
485 static int
486 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
487 size_t *num)
488 {
489 long seg;
490 int error = 0;
491
492 bsize = fragroundup(fs, bsize);
493 if (daddr > 0) {
494 if (*lastseg != (seg = datosn(fs, daddr))) {
495 error = lfs_update_seguse(fs, *lastseg, *num);
496 *num = bsize;
497 *lastseg = seg;
498 } else
499 *num += bsize;
500 }
501 return error;
502 }
503
504 /* Finish the accounting updates for a segment. */
505 static int
506 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
507 {
508 SEGUSE *sup;
509 struct buf *bp;
510
511 if (lastseg < 0 || num == 0)
512 return 0;
513
514
515 LFS_SEGENTRY(sup, fs, lastseg, bp);
516 if (num > sup->su_nbytes) {
517 printf("lfs_truncate: segment %ld short by %ld\n",
518 lastseg, (long)num - sup->su_nbytes);
519 panic("lfs_truncate: negative bytes");
520 sup->su_nbytes = num;
521 }
522 sup->su_nbytes -= num;
523 return (VOP_BWRITE(bp)); /* Ifile */
524 }
525
526 /*
527 * Release blocks associated with the inode ip and stored in the indirect
528 * block bn. Blocks are free'd in LIFO order up to (but not including)
529 * lastbn. If level is greater than SINGLE, the block is an indirect block
530 * and recursive calls to indirtrunc must be used to cleanse other indirect
531 * blocks.
532 *
533 * NB: triple indirect blocks are untested.
534 */
535 static int
536 lfs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, daddr_t dbn,
537 ufs_daddr_t lastbn, int level, long *countp,
538 long *rcountp, long *lastsegp, size_t *bcp)
539 {
540 int i;
541 struct buf *bp;
542 struct lfs *fs = ip->i_lfs;
543 ufs_daddr_t *bap;
544 struct vnode *vp;
545 ufs_daddr_t *copy = NULL, nb, nlbn, last;
546 long blkcount, rblkcount, factor;
547 int nblocks, blocksreleased = 0, real_released = 0;
548 int error = 0, allerror = 0;
549
550 /*
551 * Calculate index in current block of last
552 * block to be kept. -1 indicates the entire
553 * block so we need not calculate the index.
554 */
555 factor = 1;
556 for (i = SINGLE; i < level; i++)
557 factor *= NINDIR(fs);
558 last = lastbn;
559 if (lastbn > 0)
560 last /= factor;
561 nblocks = btodb(fs->lfs_bsize);
562 /*
563 * Get buffer of block pointers, zero those entries corresponding
564 * to blocks to be free'd, and update on disk copy first. Since
565 * double(triple) indirect before single(double) indirect, calls
566 * to bmap on these blocks will fail. However, we already have
567 * the on disk address, so we have to set the b_blkno field
568 * explicitly instead of letting bread do everything for us.
569 */
570 vp = ITOV(ip);
571 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
572 if (bp->b_flags & (B_DONE | B_DELWRI)) {
573 /* Braces must be here in case trace evaluates to nothing. */
574 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
575 } else {
576 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
577 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
578 bp->b_flags |= B_READ;
579 if (bp->b_bcount > bp->b_bufsize)
580 panic("lfs_indirtrunc: bad buffer size");
581 bp->b_blkno = dbn;
582 VOP_STRATEGY(bp);
583 error = biowait(bp);
584 }
585 if (error) {
586 brelse(bp);
587 *countp = *rcountp = 0;
588 return (error);
589 }
590
591 bap = (ufs_daddr_t *)bp->b_data;
592 if (lastbn >= 0) {
593 MALLOC(copy, ufs_daddr_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
594 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
595 memset((caddr_t)&bap[last + 1], 0,
596 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
597 error = VOP_BWRITE(bp);
598 if (error)
599 allerror = error;
600 bap = copy;
601 }
602
603 /*
604 * Recursively free totally unused blocks.
605 */
606 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
607 i--, nlbn += factor) {
608 nb = bap[i];
609 if (nb == 0)
610 continue;
611 if (level > SINGLE) {
612 error = lfs_indirtrunc(ip, nlbn, nb,
613 (ufs_daddr_t)-1, level - 1,
614 &blkcount, &rblkcount,
615 lastsegp, bcp);
616 if (error)
617 allerror = error;
618 blocksreleased += blkcount;
619 real_released += rblkcount;
620 }
621 lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
622 if (bap[i] > 0)
623 real_released += nblocks;
624 blocksreleased += nblocks;
625 }
626
627 /*
628 * Recursively free last partial block.
629 */
630 if (level > SINGLE && lastbn >= 0) {
631 last = lastbn % factor;
632 nb = bap[i];
633 if (nb != 0) {
634 error = lfs_indirtrunc(ip, nlbn, nb,
635 last, level - 1, &blkcount,
636 &rblkcount, lastsegp, bcp);
637 if (error)
638 allerror = error;
639 real_released += rblkcount;
640 blocksreleased += blkcount;
641 }
642 }
643
644 if (copy != NULL) {
645 FREE(copy, M_TEMP);
646 } else {
647 bp->b_flags |= B_INVAL;
648 brelse(bp);
649 }
650
651 *countp = blocksreleased;
652 *rcountp = real_released;
653 return (allerror);
654 }
655
656 /*
657 * Destroy any in core blocks past the truncation length.
658 * Inlined from vtruncbuf, so that lfs_avail could be updated.
659 */
660 static int
661 lfs_vtruncbuf(vp, lbn, slpflag, slptimeo)
662 struct vnode *vp;
663 daddr_t lbn;
664 int slpflag, slptimeo;
665 {
666 struct buf *bp, *nbp;
667 int s, error;
668 struct lfs *fs;
669
670 fs = VTOI(vp)->i_lfs;
671 s = splbio();
672
673 restart:
674 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
675 nbp = LIST_NEXT(bp, b_vnbufs);
676 if (bp->b_lblkno < lbn)
677 continue;
678 if (bp->b_flags & B_BUSY) {
679 bp->b_flags |= B_WANTED;
680 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
681 "lfs_vtruncbuf", slptimeo);
682 if (error) {
683 splx(s);
684 return (error);
685 }
686 goto restart;
687 }
688 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
689 if (bp->b_flags & B_DELWRI) {
690 bp->b_flags &= ~B_DELWRI;
691 fs->lfs_avail += btodb(bp->b_bcount);
692 }
693 LFS_UNLOCK_BUF(bp);
694 brelse(bp);
695 }
696
697 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
698 nbp = LIST_NEXT(bp, b_vnbufs);
699 if (bp->b_lblkno < lbn)
700 continue;
701 if (bp->b_flags & B_BUSY) {
702 bp->b_flags |= B_WANTED;
703 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
704 "lfs_vtruncbuf", slptimeo);
705 if (error) {
706 splx(s);
707 return (error);
708 }
709 goto restart;
710 }
711 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
712 if (bp->b_flags & B_DELWRI) {
713 bp->b_flags &= ~B_DELWRI;
714 fs->lfs_avail += btodb(bp->b_bcount);
715 }
716 LFS_UNLOCK_BUF(bp);
717 brelse(bp);
718 }
719
720 splx(s);
721
722 return (0);
723 }
724
725