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