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