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