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