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