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