lfs_inode.c revision 1.97 1 /* $NetBSD: lfs_inode.c,v 1.97 2005/09/12 16:24:41 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_inode.c 8.9 (Berkeley) 5/8/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_inode.c,v 1.97 2005/09/12 16:24:41 christos Exp $");
71
72 #if defined(_KERNEL_OPT)
73 #include "opt_quota.h"
74 #endif
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/mount.h>
79 #include <sys/proc.h>
80 #include <sys/file.h>
81 #include <sys/buf.h>
82 #include <sys/vnode.h>
83 #include <sys/kernel.h>
84 #include <sys/trace.h>
85 #include <sys/resourcevar.h>
86
87 #include <ufs/ufs/quota.h>
88 #include <ufs/ufs/inode.h>
89 #include <ufs/ufs/ufsmount.h>
90 #include <ufs/ufs/ufs_extern.h>
91
92 #include <ufs/lfs/lfs.h>
93 #include <ufs/lfs/lfs_extern.h>
94
95 static int lfs_update_seguse(struct lfs *, long, size_t);
96 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t,
97 daddr_t, int, long *, long *, long *, size_t *,
98 struct proc *);
99 static int lfs_blkfree (struct lfs *, daddr_t, size_t, long *, size_t *);
100 static int lfs_vtruncbuf(struct vnode *, daddr_t, int, int);
101
102 /* Search a block for a specific dinode. */
103 struct ufs1_dinode *
104 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp)
105 {
106 struct ufs1_dinode *dip = (struct ufs1_dinode *)bp->b_data;
107 struct ufs1_dinode *ldip, *fin;
108
109 ASSERT_NO_SEGLOCK(fs);
110 /*
111 * Read the inode block backwards, since later versions of the
112 * inode will supercede earlier ones. Though it is unlikely, it is
113 * possible that the same inode will appear in the same inode block.
114 */
115 fin = dip + INOPB(fs);
116 for (ldip = fin - 1; ldip >= dip; --ldip)
117 if (ldip->di_inumber == ino)
118 return (ldip);
119
120 printf("searched %d entries\n", (int)(fin - dip));
121 printf("offset is 0x%x (seg %d)\n", fs->lfs_offset,
122 dtosn(fs, fs->lfs_offset));
123 printf("block is 0x%llx (seg %lld)\n",
124 (unsigned long long)dbtofsb(fs, bp->b_blkno),
125 (long long)dtosn(fs, dbtofsb(fs, bp->b_blkno)));
126
127 return NULL;
128 }
129
130 int
131 lfs_update(void *v)
132 {
133 struct vop_update_args /* {
134 struct vnode *a_vp;
135 struct timespec *a_access;
136 struct timespec *a_modify;
137 int a_flags;
138 } */ *ap = v;
139 struct inode *ip;
140 struct vnode *vp = ap->a_vp;
141 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
142 int s;
143 int flags;
144
145 ASSERT_NO_SEGLOCK(fs);
146 if (vp->v_mount->mnt_flag & MNT_RDONLY)
147 return (0);
148 ip = VTOI(vp);
149
150 /*
151 * If we are called from vinvalbuf, and the file's blocks have
152 * already been scheduled for writing, but the writes have not
153 * yet completed, lfs_vflush will not be called, and vinvalbuf
154 * will cause a panic. So, we must wait until any pending write
155 * for our inode completes, if we are called with UPDATE_WAIT set.
156 */
157 s = splbio();
158 simple_lock(&vp->v_interlock);
159 while ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
160 WRITEINPROG(vp)) {
161 DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d"
162 " (in progress)\n", ip->i_number));
163 ltsleep(vp, (PRIBIO+1), "lfs_update", 0, &vp->v_interlock);
164 }
165 simple_unlock(&vp->v_interlock);
166 splx(s);
167 LFS_ITIMES(ip, ap->a_access, ap->a_modify, NULL);
168 if (ap->a_flags & UPDATE_CLOSE)
169 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING);
170 else
171 flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING);
172 if (flags == 0)
173 return (0);
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 simple_lock(&fs->lfs_interlock);
179 ++fs->lfs_diropwait;
180 while (vp->v_flag & VDIROP) {
181 DLOG((DLOG_DIROP, "lfs_update: sleeping on inode %d"
182 " (dirops)\n", ip->i_number));
183 DLOG((DLOG_DIROP, "lfs_update: vflags 0x%x, iflags"
184 " 0x%x\n", vp->v_flag, ip->i_flag));
185 if (fs->lfs_dirops == 0)
186 lfs_flush_fs(fs, SEGM_SYNC);
187 else
188 ltsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
189 0, &fs->lfs_interlock);
190 /* XXX KS - by falling out here, are we writing the vn
191 twice? */
192 }
193 --fs->lfs_diropwait;
194 simple_unlock(&fs->lfs_interlock);
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
209 int
210 lfs_truncate(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 struct genfs_node *gp = VTOG(ovp);
221 daddr_t lastblock;
222 struct inode *oip = VTOI(ovp);
223 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
224 /* XXX ondisk32 */
225 int32_t newblks[NDADDR + NIADDR];
226 off_t length = ap->a_length;
227 struct lfs *fs;
228 struct buf *bp;
229 int offset, size, level;
230 long count, rcount, blocksreleased = 0, real_released = 0;
231 int i, ioflag, nblocks;
232 int aflags, error, allerror = 0;
233 off_t osize;
234 long lastseg;
235 size_t bc;
236 int obufsize, odb;
237 int usepc;
238 struct ufsmount *ump = oip->i_ump;
239
240 if (length < 0)
241 return (EINVAL);
242
243 /*
244 * Just return and not update modification times.
245 */
246 if (oip->i_size == length)
247 return (0);
248
249 if (ovp->v_type == VLNK &&
250 (oip->i_size < ump->um_maxsymlinklen ||
251 (ump->um_maxsymlinklen == 0 &&
252 oip->i_ffs1_blocks == 0))) {
253 #ifdef DIAGNOSTIC
254 if (length != 0)
255 panic("lfs_truncate: partial truncate of symlink");
256 #endif
257 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
258 oip->i_size = oip->i_ffs1_size = 0;
259 oip->i_flag |= IN_CHANGE | IN_UPDATE;
260 return (VOP_UPDATE(ovp, NULL, NULL, 0));
261 }
262 if (oip->i_size == length) {
263 oip->i_flag |= IN_CHANGE | IN_UPDATE;
264 return (VOP_UPDATE(ovp, NULL, NULL, 0));
265 }
266 #ifdef QUOTA
267 if ((error = getinoquota(oip)) != 0)
268 return (error);
269 #endif
270 fs = oip->i_lfs;
271 lfs_imtime(fs);
272 osize = oip->i_size;
273 ioflag = ap->a_flags;
274 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode);
275
276 ASSERT_NO_SEGLOCK(fs);
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 > ump->um_maxfilesize)
284 return (EFBIG);
285 aflags = B_CLRBUF;
286 if (ioflag & IO_SYNC)
287 aflags |= B_SYNC;
288 if (usepc) {
289 if (lblkno(fs, osize) < NDADDR &&
290 lblkno(fs, osize) != lblkno(fs, length) &&
291 blkroundup(fs, osize) != osize) {
292 off_t eob;
293
294 eob = blkroundup(fs, osize);
295 error = ufs_balloc_range(ovp, osize,
296 eob - osize, ap->a_cred, aflags);
297 if (error)
298 return error;
299 if (ioflag & IO_SYNC) {
300 ovp->v_size = eob;
301 simple_lock(&ovp->v_interlock);
302 VOP_PUTPAGES(ovp,
303 trunc_page(osize & fs->lfs_bmask),
304 round_page(eob),
305 PGO_CLEANIT | PGO_SYNCIO);
306 }
307 }
308 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
309 aflags);
310 if (error) {
311 (void) VOP_TRUNCATE(ovp, osize,
312 ioflag & IO_SYNC,
313 ap->a_cred, ap->a_p);
314 return error;
315 }
316 uvm_vnp_setsize(ovp, length);
317 oip->i_flag |= IN_CHANGE | IN_UPDATE;
318 KASSERT(ovp->v_size == oip->i_size);
319 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
320 return (VOP_UPDATE(ovp, NULL, NULL, 0));
321 } else {
322 error = lfs_reserve(fs, ovp, NULL,
323 btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
324 if (error)
325 return (error);
326 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred,
327 aflags, &bp);
328 lfs_reserve(fs, ovp, NULL,
329 -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
330 if (error)
331 return (error);
332 oip->i_ffs1_size = oip->i_size = length;
333 uvm_vnp_setsize(ovp, length);
334 (void) VOP_BWRITE(bp);
335 oip->i_flag |= IN_CHANGE | IN_UPDATE;
336 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
337 return (VOP_UPDATE(ovp, NULL, NULL, 0));
338 }
339 }
340
341 if ((error = lfs_reserve(fs, ovp, NULL,
342 btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
343 return (error);
344
345 /*
346 * Shorten the size of the file. If the file is not being
347 * truncated to a block boundary, the contents of the
348 * partial block following the end of the file must be
349 * zero'ed in case it ever becomes accessible again because
350 * of subsequent file growth. Directories however are not
351 * zero'ed as they should grow back initialized to empty.
352 */
353 offset = blkoff(fs, length);
354 lastseg = -1;
355 bc = 0;
356
357 if (ovp != fs->lfs_ivnode)
358 lfs_seglock(fs, SEGM_PROT);
359 if (offset == 0) {
360 oip->i_size = oip->i_ffs1_size = length;
361 } else if (!usepc) {
362 lbn = lblkno(fs, length);
363 aflags = B_CLRBUF;
364 if (ioflag & IO_SYNC)
365 aflags |= B_SYNC;
366 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
367 if (error) {
368 lfs_reserve(fs, ovp, NULL,
369 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
370 goto errout;
371 }
372 obufsize = bp->b_bufsize;
373 odb = btofsb(fs, bp->b_bcount);
374 oip->i_size = oip->i_ffs1_size = length;
375 size = blksize(fs, oip, lbn);
376 if (ovp->v_type != VDIR)
377 memset((char *)bp->b_data + offset, 0,
378 (u_int)(size - offset));
379 allocbuf(bp, size, 1);
380 if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) {
381 simple_lock(&lfs_subsys_lock);
382 locked_queue_bytes -= obufsize - bp->b_bufsize;
383 simple_unlock(&lfs_subsys_lock);
384 }
385 if (bp->b_flags & B_DELWRI)
386 fs->lfs_avail += odb - btofsb(fs, size);
387 (void) VOP_BWRITE(bp);
388 } else { /* vp->v_type == VREG && length < osize && offset != 0 */
389 /*
390 * When truncating a regular file down to a non-block-aligned
391 * size, we must zero the part of last block which is past
392 * the new EOF. We must synchronously flush the zeroed pages
393 * to disk since the new pages will be invalidated as soon
394 * as we inform the VM system of the new, smaller size.
395 * We must do this before acquiring the GLOCK, since fetching
396 * the pages will acquire the GLOCK internally.
397 * So there is a window where another thread could see a whole
398 * zeroed page past EOF, but that's life.
399 */
400 daddr_t xlbn;
401 voff_t eoz;
402
403 aflags = ioflag & IO_SYNC ? B_SYNC : 0;
404 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
405 aflags);
406 if (error) {
407 lfs_reserve(fs, ovp, NULL,
408 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
409 goto errout;
410 }
411 xlbn = lblkno(fs, length);
412 size = blksize(fs, oip, xlbn);
413 eoz = MIN(lblktosize(fs, xlbn) + size, osize);
414 uvm_vnp_zerorange(ovp, length, eoz - length);
415 if (round_page(eoz) > round_page(length)) {
416 simple_lock(&ovp->v_interlock);
417 error = VOP_PUTPAGES(ovp, round_page(length),
418 round_page(eoz),
419 PGO_CLEANIT | PGO_DEACTIVATE |
420 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
421 if (error) {
422 lfs_reserve(fs, ovp, NULL,
423 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
424 goto errout;
425 }
426 }
427 }
428
429 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
430
431 oip->i_size = oip->i_ffs1_size = length;
432 uvm_vnp_setsize(ovp, length);
433 /*
434 * Calculate index into inode's block list of
435 * last direct and indirect blocks (if any)
436 * which we want to keep. Lastblock is -1 when
437 * the file is truncated to 0.
438 */
439 lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
440 lastiblock[SINGLE] = lastblock - NDADDR;
441 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
442 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
443 nblocks = btofsb(fs, fs->lfs_bsize);
444 /*
445 * Record changed file and block pointers before we start
446 * freeing blocks. lastiblock values are also normalized to -1
447 * for calls to lfs_indirtrunc below.
448 */
449 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs1_db[0], sizeof newblks);
450 for (level = TRIPLE; level >= SINGLE; level--)
451 if (lastiblock[level] < 0) {
452 newblks[NDADDR+level] = 0;
453 lastiblock[level] = -1;
454 }
455 for (i = NDADDR - 1; i > lastblock; i--)
456 newblks[i] = 0;
457
458 oip->i_size = oip->i_ffs1_size = osize;
459 error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
460 if (error && !allerror)
461 allerror = error;
462
463 /*
464 * Indirect blocks first.
465 */
466 indir_lbn[SINGLE] = -NDADDR;
467 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
468 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
469 for (level = TRIPLE; level >= SINGLE; level--) {
470 bn = oip->i_ffs1_ib[level];
471 if (bn != 0) {
472 error = lfs_indirtrunc(oip, indir_lbn[level],
473 bn, lastiblock[level],
474 level, &count, &rcount,
475 &lastseg, &bc, ap->a_p);
476 if (error)
477 allerror = error;
478 real_released += rcount;
479 blocksreleased += count;
480 if (lastiblock[level] < 0) {
481 if (oip->i_ffs1_ib[level] > 0)
482 real_released += nblocks;
483 blocksreleased += nblocks;
484 oip->i_ffs1_ib[level] = 0;
485 lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
486 lfs_deregister_block(ovp, bn);
487 }
488 }
489 if (lastiblock[level] >= 0)
490 goto done;
491 }
492
493 /*
494 * All whole direct blocks or frags.
495 */
496 for (i = NDADDR - 1; i > lastblock; i--) {
497 long bsize, obsize;
498
499 bn = oip->i_ffs1_db[i];
500 if (bn == 0)
501 continue;
502 bsize = blksize(fs, oip, i);
503 if (oip->i_ffs1_db[i] > 0) {
504 /* Check for fragment size changes */
505 obsize = oip->i_lfs_fragsize[i];
506 real_released += btofsb(fs, obsize);
507 oip->i_lfs_fragsize[i] = 0;
508 } else
509 obsize = 0;
510 blocksreleased += btofsb(fs, bsize);
511 oip->i_ffs1_db[i] = 0;
512 lfs_blkfree(fs, bn, obsize, &lastseg, &bc);
513 lfs_deregister_block(ovp, bn);
514 }
515 if (lastblock < 0)
516 goto done;
517
518 /*
519 * Finally, look for a change in size of the
520 * last direct block; release any frags.
521 */
522 bn = oip->i_ffs1_db[lastblock];
523 if (bn != 0) {
524 long oldspace, newspace;
525 #if 0
526 long olddspace;
527 #endif
528
529 /*
530 * Calculate amount of space we're giving
531 * back as old block size minus new block size.
532 */
533 oldspace = blksize(fs, oip, lastblock);
534 #if 0
535 olddspace = oip->i_lfs_fragsize[lastblock];
536 #endif
537
538 oip->i_size = oip->i_ffs1_size = length;
539 newspace = blksize(fs, oip, lastblock);
540 if (newspace == 0)
541 panic("itrunc: newspace");
542 if (oldspace - newspace > 0) {
543 blocksreleased += btofsb(fs, oldspace - newspace);
544 }
545 #if 0
546 if (bn > 0 && olddspace - newspace > 0) {
547 /* No segment accounting here, just vnode */
548 real_released += btofsb(fs, olddspace - newspace);
549 }
550 #endif
551 }
552
553 done:
554 /* Finish segment accounting corrections */
555 lfs_update_seguse(fs, lastseg, bc);
556 #ifdef DIAGNOSTIC
557 for (level = SINGLE; level <= TRIPLE; level++)
558 if ((newblks[NDADDR + level] == 0) !=
559 (oip->i_ffs1_ib[level]) == 0) {
560 panic("lfs itrunc1");
561 }
562 for (i = 0; i < NDADDR; i++)
563 if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) {
564 panic("lfs itrunc2");
565 }
566 if (length == 0 &&
567 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
568 panic("lfs itrunc3");
569 #endif /* DIAGNOSTIC */
570 /*
571 * Put back the real size.
572 */
573 oip->i_size = oip->i_ffs1_size = length;
574 oip->i_lfs_effnblks -= blocksreleased;
575 oip->i_ffs1_blocks -= real_released;
576 simple_lock(&fs->lfs_interlock);
577 fs->lfs_bfree += blocksreleased;
578 simple_unlock(&fs->lfs_interlock);
579 #ifdef DIAGNOSTIC
580 if (oip->i_size == 0 &&
581 (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) {
582 printf("lfs_truncate: truncate to 0 but %d blks/%d effblks\n",
583 oip->i_ffs1_blocks, oip->i_lfs_effnblks);
584 panic("lfs_truncate: persistent blocks");
585 }
586 #endif
587 oip->i_flag |= IN_CHANGE;
588 #ifdef QUOTA
589 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
590 #endif
591 lfs_reserve(fs, ovp, NULL,
592 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
593 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
594 errout:
595 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
596 if (ovp != fs->lfs_ivnode)
597 lfs_segunlock(fs);
598 return (allerror ? allerror : error);
599 }
600
601 /* Update segment and avail usage information when removing a block. */
602 static int
603 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
604 size_t *num)
605 {
606 long seg;
607 int error = 0;
608
609 ASSERT_SEGLOCK(fs);
610 bsize = fragroundup(fs, bsize);
611 if (daddr > 0) {
612 if (*lastseg != (seg = dtosn(fs, daddr))) {
613 error = lfs_update_seguse(fs, *lastseg, *num);
614 *num = bsize;
615 *lastseg = seg;
616 } else
617 *num += bsize;
618 }
619
620 return error;
621 }
622
623 /* Finish the accounting updates for a segment. */
624 static int
625 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
626 {
627 SEGUSE *sup;
628 struct buf *bp;
629
630 ASSERT_SEGLOCK(fs);
631 if (lastseg < 0 || num == 0)
632 return 0;
633
634 LFS_SEGENTRY(sup, fs, lastseg, bp);
635 if (num > sup->su_nbytes) {
636 printf("lfs_truncate: segment %ld short by %ld\n",
637 lastseg, (long)num - sup->su_nbytes);
638 panic("lfs_truncate: negative bytes");
639 sup->su_nbytes = num;
640 }
641 sup->su_nbytes -= num;
642 LFS_WRITESEGENTRY(sup, fs, lastseg, bp);
643
644 return 0;
645 }
646
647 /*
648 * Release blocks associated with the inode ip and stored in the indirect
649 * block bn. Blocks are free'd in LIFO order up to (but not including)
650 * lastbn. If level is greater than SINGLE, the block is an indirect block
651 * and recursive calls to indirtrunc must be used to cleanse other indirect
652 * blocks.
653 *
654 * NB: triple indirect blocks are untested.
655 */
656 static int
657 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
658 daddr_t lastbn, int level, long *countp,
659 long *rcountp, long *lastsegp, size_t *bcp, struct proc *p)
660 {
661 int i;
662 struct buf *bp;
663 struct lfs *fs = ip->i_lfs;
664 int32_t *bap; /* XXX ondisk32 */
665 struct vnode *vp;
666 daddr_t nb, nlbn, last;
667 int32_t *copy = NULL; /* XXX ondisk32 */
668 long blkcount, rblkcount, factor;
669 int nblocks, blocksreleased = 0, real_released = 0;
670 int error = 0, allerror = 0;
671
672 ASSERT_SEGLOCK(fs);
673 /*
674 * Calculate index in current block of last
675 * block to be kept. -1 indicates the entire
676 * block so we need not calculate the index.
677 */
678 factor = 1;
679 for (i = SINGLE; i < level; i++)
680 factor *= NINDIR(fs);
681 last = lastbn;
682 if (lastbn > 0)
683 last /= factor;
684 nblocks = btofsb(fs, fs->lfs_bsize);
685 /*
686 * Get buffer of block pointers, zero those entries corresponding
687 * to blocks to be free'd, and update on disk copy first. Since
688 * double(triple) indirect before single(double) indirect, calls
689 * to bmap on these blocks will fail. However, we already have
690 * the on disk address, so we have to set the b_blkno field
691 * explicitly instead of letting bread do everything for us.
692 */
693 vp = ITOV(ip);
694 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
695 if (bp->b_flags & (B_DONE | B_DELWRI)) {
696 /* Braces must be here in case trace evaluates to nothing. */
697 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
698 } else {
699 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
700 p->p_stats->p_ru.ru_inblock++; /* pay for read */
701 bp->b_flags |= B_READ;
702 if (bp->b_bcount > bp->b_bufsize)
703 panic("lfs_indirtrunc: bad buffer size");
704 bp->b_blkno = fsbtodb(fs, dbn);
705 VOP_STRATEGY(vp, bp);
706 error = biowait(bp);
707 }
708 if (error) {
709 brelse(bp);
710 *countp = *rcountp = 0;
711 return (error);
712 }
713
714 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
715 if (lastbn >= 0) {
716 copy = (int32_t *)lfs_malloc(fs, fs->lfs_bsize, LFS_NB_IBLOCK);
717 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
718 memset((caddr_t)&bap[last + 1], 0,
719 /* XXX ondisk32 */
720 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
721 error = VOP_BWRITE(bp);
722 if (error)
723 allerror = error;
724 bap = copy;
725 }
726
727 /*
728 * Recursively free totally unused blocks.
729 */
730 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
731 i--, nlbn += factor) {
732 nb = bap[i];
733 if (nb == 0)
734 continue;
735 if (level > SINGLE) {
736 error = lfs_indirtrunc(ip, nlbn, nb,
737 (daddr_t)-1, level - 1,
738 &blkcount, &rblkcount,
739 lastsegp, bcp, p);
740 if (error)
741 allerror = error;
742 blocksreleased += blkcount;
743 real_released += rblkcount;
744 }
745 lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
746 if (bap[i] > 0)
747 real_released += nblocks;
748 blocksreleased += nblocks;
749 }
750
751 /*
752 * Recursively free last partial block.
753 */
754 if (level > SINGLE && lastbn >= 0) {
755 last = lastbn % factor;
756 nb = bap[i];
757 if (nb != 0) {
758 error = lfs_indirtrunc(ip, nlbn, nb,
759 last, level - 1, &blkcount,
760 &rblkcount, lastsegp, bcp, p);
761 if (error)
762 allerror = error;
763 real_released += rblkcount;
764 blocksreleased += blkcount;
765 }
766 }
767
768 if (copy != NULL) {
769 lfs_free(fs, copy, LFS_NB_IBLOCK);
770 } else {
771 if (bp->b_flags & B_DELWRI) {
772 LFS_UNLOCK_BUF(bp);
773 fs->lfs_avail += btofsb(fs, bp->b_bcount);
774 wakeup(&fs->lfs_avail);
775 }
776 bp->b_flags |= B_INVAL;
777 brelse(bp);
778 }
779
780 *countp = blocksreleased;
781 *rcountp = real_released;
782 return (allerror);
783 }
784
785 /*
786 * Destroy any in core blocks past the truncation length.
787 * Inlined from vtruncbuf, so that lfs_avail could be updated.
788 * We take the seglock to prevent cleaning from occurring while we are
789 * invalidating blocks.
790 */
791 static int
792 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
793 {
794 struct buf *bp, *nbp;
795 int s, error;
796 struct lfs *fs;
797 voff_t off;
798
799 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
800 simple_lock(&vp->v_interlock);
801 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
802 if (error)
803 return error;
804
805 fs = VTOI(vp)->i_lfs;
806 s = splbio();
807
808 ASSERT_SEGLOCK(fs);
809 restart:
810 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
811 nbp = LIST_NEXT(bp, b_vnbufs);
812 if (bp->b_lblkno < lbn)
813 continue;
814 simple_lock(&bp->b_interlock);
815 if (bp->b_flags & B_BUSY) {
816 bp->b_flags |= B_WANTED;
817 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
818 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
819 if (error) {
820 splx(s);
821 return (error);
822 }
823 goto restart;
824 }
825 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
826 if (bp->b_flags & B_DELWRI) {
827 bp->b_flags &= ~B_DELWRI;
828 fs->lfs_avail += btofsb(fs, bp->b_bcount);
829 wakeup(&fs->lfs_avail);
830 }
831 LFS_UNLOCK_BUF(bp);
832 simple_unlock(&bp->b_interlock);
833 brelse(bp);
834 }
835
836 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
837 nbp = LIST_NEXT(bp, b_vnbufs);
838 if (bp->b_lblkno < lbn)
839 continue;
840 simple_lock(&bp->b_interlock);
841 if (bp->b_flags & B_BUSY) {
842 bp->b_flags |= B_WANTED;
843 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
844 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
845 if (error) {
846 splx(s);
847 return (error);
848 }
849 goto restart;
850 }
851 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
852 if (bp->b_flags & B_DELWRI) {
853 bp->b_flags &= ~B_DELWRI;
854 fs->lfs_avail += btofsb(fs, bp->b_bcount);
855 wakeup(&fs->lfs_avail);
856 }
857 LFS_UNLOCK_BUF(bp);
858 simple_unlock(&bp->b_interlock);
859 brelse(bp);
860 }
861
862 splx(s);
863
864 return (0);
865 }
866
867