lfs_inode.c revision 1.112 1 /* $NetBSD: lfs_inode.c,v 1.112 2007/10/08 18:01:30 ad 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.112 2007/10/08 18:01:30 ad 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/malloc.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/trace.h>
86 #include <sys/resourcevar.h>
87 #include <sys/kauth.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 static int lfs_update_seguse(struct lfs *, struct inode *ip, long, size_t);
98 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t,
99 daddr_t, int, long *, long *, long *, size_t *,
100 struct lwp *);
101 static int lfs_blkfree (struct lfs *, struct inode *, daddr_t, size_t, long *, size_t *);
102 static int lfs_vtruncbuf(struct vnode *, daddr_t, int, int);
103
104 /* Search a block for a specific dinode. */
105 struct ufs1_dinode *
106 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp)
107 {
108 struct ufs1_dinode *dip = (struct ufs1_dinode *)bp->b_data;
109 struct ufs1_dinode *ldip, *fin;
110
111 ASSERT_NO_SEGLOCK(fs);
112 /*
113 * Read the inode block backwards, since later versions of the
114 * inode will supercede earlier ones. Though it is unlikely, it is
115 * possible that the same inode will appear in the same inode block.
116 */
117 fin = dip + INOPB(fs);
118 for (ldip = fin - 1; ldip >= dip; --ldip)
119 if (ldip->di_inumber == ino)
120 return (ldip);
121
122 printf("searched %d entries\n", (int)(fin - dip));
123 printf("offset is 0x%x (seg %d)\n", fs->lfs_offset,
124 dtosn(fs, fs->lfs_offset));
125 printf("block is 0x%llx (seg %lld)\n",
126 (unsigned long long)dbtofsb(fs, bp->b_blkno),
127 (long long)dtosn(fs, dbtofsb(fs, bp->b_blkno)));
128
129 return NULL;
130 }
131
132 int
133 lfs_update(struct vnode *vp, const struct timespec *acc,
134 const struct timespec *mod, int updflags)
135 {
136 struct inode *ip;
137 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
138 int s;
139 int flags;
140
141 ASSERT_NO_SEGLOCK(fs);
142 if (vp->v_mount->mnt_flag & MNT_RDONLY)
143 return (0);
144 ip = VTOI(vp);
145
146 /*
147 * If we are called from vinvalbuf, and the file's blocks have
148 * already been scheduled for writing, but the writes have not
149 * yet completed, lfs_vflush will not be called, and vinvalbuf
150 * will cause a panic. So, we must wait until any pending write
151 * for our inode completes, if we are called with UPDATE_WAIT set.
152 */
153 s = splbio();
154 simple_lock(&vp->v_interlock);
155 while ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
156 WRITEINPROG(vp)) {
157 DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d"
158 " (in progress)\n", ip->i_number));
159 ltsleep(vp, (PRIBIO+1), "lfs_update", 0, &vp->v_interlock);
160 }
161 simple_unlock(&vp->v_interlock);
162 splx(s);
163 LFS_ITIMES(ip, acc, mod, NULL);
164 if (updflags & UPDATE_CLOSE)
165 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING);
166 else
167 flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING);
168 if (flags == 0)
169 return (0);
170
171 /* If sync, push back the vnode and any dirty blocks it may have. */
172 if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) {
173 /* Avoid flushing VDIROP. */
174 simple_lock(&fs->lfs_interlock);
175 ++fs->lfs_diropwait;
176 while (vp->v_flag & VDIROP) {
177 DLOG((DLOG_DIROP, "lfs_update: sleeping on inode %d"
178 " (dirops)\n", ip->i_number));
179 DLOG((DLOG_DIROP, "lfs_update: vflags 0x%x, iflags"
180 " 0x%x\n", vp->v_flag, ip->i_flag));
181 if (fs->lfs_dirops == 0)
182 lfs_flush_fs(fs, SEGM_SYNC);
183 else
184 ltsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
185 0, &fs->lfs_interlock);
186 /* XXX KS - by falling out here, are we writing the vn
187 twice? */
188 }
189 --fs->lfs_diropwait;
190 simple_unlock(&fs->lfs_interlock);
191 return lfs_vflush(vp);
192 }
193 return 0;
194 }
195
196 #define SINGLE 0 /* index of single indirect block */
197 #define DOUBLE 1 /* index of double indirect block */
198 #define TRIPLE 2 /* index of triple indirect block */
199 /*
200 * Truncate the inode oip to at most length size, freeing the
201 * disk blocks.
202 */
203 /* VOP_BWRITE 1 + NIADDR + lfs_balloc == 2 + 2*NIADDR times */
204
205 int
206 lfs_truncate(struct vnode *ovp, off_t length, int ioflag,
207 kauth_cred_t cred, struct lwp *l)
208 {
209 daddr_t lastblock;
210 struct inode *oip = VTOI(ovp);
211 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
212 /* XXX ondisk32 */
213 int32_t newblks[NDADDR + NIADDR];
214 struct lfs *fs;
215 struct buf *bp;
216 int offset, size, level;
217 long count, rcount, blocksreleased = 0, real_released = 0;
218 int i, nblocks;
219 int aflags, error, allerror = 0;
220 off_t osize;
221 long lastseg;
222 size_t bc;
223 int obufsize, odb;
224 int usepc;
225 struct ufsmount *ump = oip->i_ump;
226
227 if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
228 ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
229 KASSERT(oip->i_size == 0);
230 return 0;
231 }
232
233 if (length < 0)
234 return (EINVAL);
235
236 /*
237 * Just return and not update modification times.
238 */
239 if (oip->i_size == length)
240 return (0);
241
242 if (ovp->v_type == VLNK &&
243 (oip->i_size < ump->um_maxsymlinklen ||
244 (ump->um_maxsymlinklen == 0 &&
245 oip->i_ffs1_blocks == 0))) {
246 #ifdef DIAGNOSTIC
247 if (length != 0)
248 panic("lfs_truncate: partial truncate of symlink");
249 #endif
250 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
251 oip->i_size = oip->i_ffs1_size = 0;
252 oip->i_flag |= IN_CHANGE | IN_UPDATE;
253 return (lfs_update(ovp, NULL, NULL, 0));
254 }
255 if (oip->i_size == length) {
256 oip->i_flag |= IN_CHANGE | IN_UPDATE;
257 return (lfs_update(ovp, NULL, NULL, 0));
258 }
259 fs = oip->i_lfs;
260 lfs_imtime(fs);
261 osize = oip->i_size;
262 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode);
263
264 ASSERT_NO_SEGLOCK(fs);
265 /*
266 * Lengthen the size of the file. We must ensure that the
267 * last byte of the file is allocated. Since the smallest
268 * value of osize is 0, length will be at least 1.
269 */
270 if (osize < length) {
271 if (length > ump->um_maxfilesize)
272 return (EFBIG);
273 aflags = B_CLRBUF;
274 if (ioflag & IO_SYNC)
275 aflags |= B_SYNC;
276 if (usepc) {
277 if (lblkno(fs, osize) < NDADDR &&
278 lblkno(fs, osize) != lblkno(fs, length) &&
279 blkroundup(fs, osize) != osize) {
280 off_t eob;
281
282 eob = blkroundup(fs, osize);
283 uvm_vnp_setwritesize(ovp, eob);
284 error = ufs_balloc_range(ovp, osize,
285 eob - osize, cred, aflags);
286 if (error)
287 return error;
288 if (ioflag & IO_SYNC) {
289 simple_lock(&ovp->v_interlock);
290 VOP_PUTPAGES(ovp,
291 trunc_page(osize & fs->lfs_bmask),
292 round_page(eob),
293 PGO_CLEANIT | PGO_SYNCIO);
294 }
295 }
296 uvm_vnp_setwritesize(ovp, length);
297 error = ufs_balloc_range(ovp, length - 1, 1, cred,
298 aflags);
299 if (error) {
300 (void) lfs_truncate(ovp, osize,
301 ioflag & IO_SYNC, cred, l);
302 return error;
303 }
304 uvm_vnp_setsize(ovp, length);
305 oip->i_flag |= IN_CHANGE | IN_UPDATE;
306 KASSERT(ovp->v_size == oip->i_size);
307 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
308 return (lfs_update(ovp, NULL, NULL, 0));
309 } else {
310 error = lfs_reserve(fs, ovp, NULL,
311 btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
312 if (error)
313 return (error);
314 error = lfs_balloc(ovp, length - 1, 1, cred,
315 aflags, &bp);
316 lfs_reserve(fs, ovp, NULL,
317 -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
318 if (error)
319 return (error);
320 oip->i_ffs1_size = oip->i_size = length;
321 uvm_vnp_setsize(ovp, length);
322 (void) VOP_BWRITE(bp);
323 oip->i_flag |= IN_CHANGE | IN_UPDATE;
324 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
325 return (lfs_update(ovp, NULL, NULL, 0));
326 }
327 }
328
329 if ((error = lfs_reserve(fs, ovp, NULL,
330 btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
331 return (error);
332
333 /*
334 * Shorten the size of the file. If the file is not being
335 * truncated to a block boundary, the contents of the
336 * partial block following the end of the file must be
337 * zero'ed in case it ever becomes accessible again because
338 * of subsequent file growth. Directories however are not
339 * zero'ed as they should grow back initialized to empty.
340 */
341 offset = blkoff(fs, length);
342 lastseg = -1;
343 bc = 0;
344
345 if (ovp != fs->lfs_ivnode)
346 lfs_seglock(fs, SEGM_PROT);
347 if (offset == 0) {
348 oip->i_size = oip->i_ffs1_size = length;
349 } else if (!usepc) {
350 lbn = lblkno(fs, length);
351 aflags = B_CLRBUF;
352 if (ioflag & IO_SYNC)
353 aflags |= B_SYNC;
354 error = lfs_balloc(ovp, length - 1, 1, cred, aflags, &bp);
355 if (error) {
356 lfs_reserve(fs, ovp, NULL,
357 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
358 goto errout;
359 }
360 obufsize = bp->b_bufsize;
361 odb = btofsb(fs, bp->b_bcount);
362 oip->i_size = oip->i_ffs1_size = length;
363 size = blksize(fs, oip, lbn);
364 if (ovp->v_type != VDIR)
365 memset((char *)bp->b_data + offset, 0,
366 (u_int)(size - offset));
367 allocbuf(bp, size, 1);
368 if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) {
369 simple_lock(&lfs_subsys_lock);
370 locked_queue_bytes -= obufsize - bp->b_bufsize;
371 simple_unlock(&lfs_subsys_lock);
372 }
373 if (bp->b_flags & B_DELWRI)
374 fs->lfs_avail += odb - btofsb(fs, size);
375 (void) VOP_BWRITE(bp);
376 } else { /* vp->v_type == VREG && length < osize && offset != 0 */
377 /*
378 * When truncating a regular file down to a non-block-aligned
379 * size, we must zero the part of last block which is past
380 * the new EOF. We must synchronously flush the zeroed pages
381 * to disk since the new pages will be invalidated as soon
382 * as we inform the VM system of the new, smaller size.
383 * We must do this before acquiring the GLOCK, since fetching
384 * the pages will acquire the GLOCK internally.
385 * So there is a window where another thread could see a whole
386 * zeroed page past EOF, but that's life.
387 */
388 daddr_t xlbn;
389 voff_t eoz;
390
391 aflags = ioflag & IO_SYNC ? B_SYNC : 0;
392 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflags);
393 if (error) {
394 lfs_reserve(fs, ovp, NULL,
395 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
396 goto errout;
397 }
398 xlbn = lblkno(fs, length);
399 size = blksize(fs, oip, xlbn);
400 eoz = MIN(lblktosize(fs, xlbn) + size, osize);
401 uvm_vnp_zerorange(ovp, length, eoz - length);
402 if (round_page(eoz) > round_page(length)) {
403 simple_lock(&ovp->v_interlock);
404 error = VOP_PUTPAGES(ovp, round_page(length),
405 round_page(eoz),
406 PGO_CLEANIT | PGO_DEACTIVATE |
407 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
408 if (error) {
409 lfs_reserve(fs, ovp, NULL,
410 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
411 goto errout;
412 }
413 }
414 }
415
416 genfs_node_wrlock(ovp);
417
418 oip->i_size = oip->i_ffs1_size = length;
419 uvm_vnp_setsize(ovp, length);
420 /*
421 * Calculate index into inode's block list of
422 * last direct and indirect blocks (if any)
423 * which we want to keep. Lastblock is -1 when
424 * the file is truncated to 0.
425 */
426 /* Avoid sign overflow - XXX assumes that off_t is a quad_t. */
427 if (length > QUAD_MAX - fs->lfs_bsize)
428 lastblock = lblkno(fs, QUAD_MAX - fs->lfs_bsize);
429 else
430 lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
431 lastiblock[SINGLE] = lastblock - NDADDR;
432 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
433 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
434 nblocks = btofsb(fs, fs->lfs_bsize);
435 /*
436 * Record changed file and block pointers before we start
437 * freeing blocks. lastiblock values are also normalized to -1
438 * for calls to lfs_indirtrunc below.
439 */
440 memcpy((void *)newblks, (void *)&oip->i_ffs1_db[0], sizeof newblks);
441 for (level = TRIPLE; level >= SINGLE; level--)
442 if (lastiblock[level] < 0) {
443 newblks[NDADDR+level] = 0;
444 lastiblock[level] = -1;
445 }
446 for (i = NDADDR - 1; i > lastblock; i--)
447 newblks[i] = 0;
448
449 oip->i_size = oip->i_ffs1_size = osize;
450 error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
451 if (error && !allerror)
452 allerror = error;
453
454 /*
455 * Indirect blocks first.
456 */
457 indir_lbn[SINGLE] = -NDADDR;
458 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
459 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
460 for (level = TRIPLE; level >= SINGLE; level--) {
461 bn = oip->i_ffs1_ib[level];
462 if (bn != 0) {
463 error = lfs_indirtrunc(oip, indir_lbn[level],
464 bn, lastiblock[level],
465 level, &count, &rcount,
466 &lastseg, &bc, l);
467 if (error)
468 allerror = error;
469 real_released += rcount;
470 blocksreleased += count;
471 if (lastiblock[level] < 0) {
472 if (oip->i_ffs1_ib[level] > 0)
473 real_released += nblocks;
474 blocksreleased += nblocks;
475 oip->i_ffs1_ib[level] = 0;
476 lfs_blkfree(fs, oip, bn, fs->lfs_bsize,
477 &lastseg, &bc);
478 lfs_deregister_block(ovp, bn);
479 }
480 }
481 if (lastiblock[level] >= 0)
482 goto done;
483 }
484
485 /*
486 * All whole direct blocks or frags.
487 */
488 for (i = NDADDR - 1; i > lastblock; i--) {
489 long bsize, obsize;
490
491 bn = oip->i_ffs1_db[i];
492 if (bn == 0)
493 continue;
494 bsize = blksize(fs, oip, i);
495 if (oip->i_ffs1_db[i] > 0) {
496 /* Check for fragment size changes */
497 obsize = oip->i_lfs_fragsize[i];
498 real_released += btofsb(fs, obsize);
499 oip->i_lfs_fragsize[i] = 0;
500 } else
501 obsize = 0;
502 blocksreleased += btofsb(fs, bsize);
503 oip->i_ffs1_db[i] = 0;
504 lfs_blkfree(fs, oip, bn, obsize, &lastseg, &bc);
505 lfs_deregister_block(ovp, bn);
506 }
507 if (lastblock < 0)
508 goto done;
509
510 /*
511 * Finally, look for a change in size of the
512 * last direct block; release any frags.
513 */
514 bn = oip->i_ffs1_db[lastblock];
515 if (bn != 0) {
516 long oldspace, newspace;
517 #if 0
518 long olddspace;
519 #endif
520
521 /*
522 * Calculate amount of space we're giving
523 * back as old block size minus new block size.
524 */
525 oldspace = blksize(fs, oip, lastblock);
526 #if 0
527 olddspace = oip->i_lfs_fragsize[lastblock];
528 #endif
529
530 oip->i_size = oip->i_ffs1_size = length;
531 newspace = blksize(fs, oip, lastblock);
532 if (newspace == 0)
533 panic("itrunc: newspace");
534 if (oldspace - newspace > 0) {
535 blocksreleased += btofsb(fs, oldspace - newspace);
536 }
537 #if 0
538 if (bn > 0 && olddspace - newspace > 0) {
539 /* No segment accounting here, just vnode */
540 real_released += btofsb(fs, olddspace - newspace);
541 }
542 #endif
543 }
544
545 done:
546 /* Finish segment accounting corrections */
547 lfs_update_seguse(fs, oip, lastseg, bc);
548 #ifdef DIAGNOSTIC
549 for (level = SINGLE; level <= TRIPLE; level++)
550 if ((newblks[NDADDR + level] == 0) !=
551 ((oip->i_ffs1_ib[level]) == 0)) {
552 panic("lfs itrunc1");
553 }
554 for (i = 0; i < NDADDR; i++)
555 if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) {
556 panic("lfs itrunc2");
557 }
558 if (length == 0 &&
559 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
560 panic("lfs itrunc3");
561 #endif /* DIAGNOSTIC */
562 /*
563 * Put back the real size.
564 */
565 oip->i_size = oip->i_ffs1_size = length;
566 oip->i_lfs_effnblks -= blocksreleased;
567 oip->i_ffs1_blocks -= real_released;
568 simple_lock(&fs->lfs_interlock);
569 fs->lfs_bfree += blocksreleased;
570 simple_unlock(&fs->lfs_interlock);
571 #ifdef DIAGNOSTIC
572 if (oip->i_size == 0 &&
573 (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) {
574 printf("lfs_truncate: truncate to 0 but %d blks/%d effblks\n",
575 oip->i_ffs1_blocks, oip->i_lfs_effnblks);
576 panic("lfs_truncate: persistent blocks");
577 }
578 #endif
579
580 /*
581 * If we truncated to zero, take us off the paging queue.
582 */
583 simple_lock(&fs->lfs_interlock);
584 if (oip->i_size == 0 && oip->i_flags & IN_PAGING) {
585 oip->i_flags &= ~IN_PAGING;
586 TAILQ_REMOVE(&fs->lfs_pchainhd, oip, i_lfs_pchain);
587 }
588 simple_unlock(&fs->lfs_interlock);
589
590 oip->i_flag |= IN_CHANGE;
591 #ifdef QUOTA
592 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
593 #endif
594 lfs_reserve(fs, ovp, NULL,
595 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
596 genfs_node_unlock(ovp);
597 errout:
598 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1;
599 if (ovp != fs->lfs_ivnode)
600 lfs_segunlock(fs);
601 return (allerror ? allerror : error);
602 }
603
604 /* Update segment and avail usage information when removing a block. */
605 static int
606 lfs_blkfree(struct lfs *fs, struct inode *ip, daddr_t daddr,
607 size_t bsize, long *lastseg, size_t *num)
608 {
609 long seg;
610 int error = 0;
611
612 ASSERT_SEGLOCK(fs);
613 bsize = fragroundup(fs, bsize);
614 if (daddr > 0) {
615 if (*lastseg != (seg = dtosn(fs, daddr))) {
616 error = lfs_update_seguse(fs, ip, *lastseg, *num);
617 *num = bsize;
618 *lastseg = seg;
619 } else
620 *num += bsize;
621 }
622
623 return error;
624 }
625
626 /* Finish the accounting updates for a segment. */
627 static int
628 lfs_update_seguse(struct lfs *fs, struct inode *ip, long lastseg, size_t num)
629 {
630 struct segdelta *sd;
631 struct vnode *vp;
632
633 ASSERT_SEGLOCK(fs);
634 if (lastseg < 0 || num == 0)
635 return 0;
636
637 vp = ITOV(ip);
638 LIST_FOREACH(sd, &ip->i_lfs_segdhd, list)
639 if (sd->segnum == lastseg)
640 break;
641 if (sd == NULL) {
642 sd = malloc(sizeof(*sd), M_SEGMENT, M_WAITOK);
643 sd->segnum = lastseg;
644 sd->num = 0;
645 LIST_INSERT_HEAD(&ip->i_lfs_segdhd, sd, list);
646 }
647 sd->num += num;
648
649 return 0;
650 }
651
652 static void
653 lfs_finalize_seguse(struct lfs *fs, void *v)
654 {
655 SEGUSE *sup;
656 struct buf *bp;
657 struct segdelta *sd;
658 LIST_HEAD(, segdelta) *hd = v;
659
660 ASSERT_SEGLOCK(fs);
661 while((sd = LIST_FIRST(hd)) != NULL) {
662 LIST_REMOVE(sd, list);
663 LFS_SEGENTRY(sup, fs, sd->segnum, bp);
664 if (sd->num > sup->su_nbytes) {
665 printf("lfs_finalize_seguse: segment %ld short by %ld\n",
666 sd->segnum, (long)(sd->num - sup->su_nbytes));
667 panic("lfs_finalize_seguse: negative bytes");
668 sup->su_nbytes = sd->num;
669 }
670 sup->su_nbytes -= sd->num;
671 LFS_WRITESEGENTRY(sup, fs, sd->segnum, bp);
672 free(sd, M_SEGMENT);
673 }
674 }
675
676 /* Finish the accounting updates for a segment. */
677 void
678 lfs_finalize_ino_seguse(struct lfs *fs, struct inode *ip)
679 {
680 ASSERT_SEGLOCK(fs);
681 lfs_finalize_seguse(fs, &ip->i_lfs_segdhd);
682 }
683
684 /* Finish the accounting updates for a segment. */
685 void
686 lfs_finalize_fs_seguse(struct lfs *fs)
687 {
688 ASSERT_SEGLOCK(fs);
689 lfs_finalize_seguse(fs, &fs->lfs_segdhd);
690 }
691
692 /*
693 * Release blocks associated with the inode ip and stored in the indirect
694 * block bn. Blocks are free'd in LIFO order up to (but not including)
695 * lastbn. If level is greater than SINGLE, the block is an indirect block
696 * and recursive calls to indirtrunc must be used to cleanse other indirect
697 * blocks.
698 *
699 * NB: triple indirect blocks are untested.
700 */
701 static int
702 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
703 daddr_t lastbn, int level, long *countp,
704 long *rcountp, long *lastsegp, size_t *bcp, struct lwp *l)
705 {
706 int i;
707 struct buf *bp;
708 struct lfs *fs = ip->i_lfs;
709 int32_t *bap; /* XXX ondisk32 */
710 struct vnode *vp;
711 daddr_t nb, nlbn, last;
712 int32_t *copy = NULL; /* XXX ondisk32 */
713 long blkcount, rblkcount, factor;
714 int nblocks, blocksreleased = 0, real_released = 0;
715 int error = 0, allerror = 0;
716
717 ASSERT_SEGLOCK(fs);
718 /*
719 * Calculate index in current block of last
720 * block to be kept. -1 indicates the entire
721 * block so we need not calculate the index.
722 */
723 factor = 1;
724 for (i = SINGLE; i < level; i++)
725 factor *= NINDIR(fs);
726 last = lastbn;
727 if (lastbn > 0)
728 last /= factor;
729 nblocks = btofsb(fs, fs->lfs_bsize);
730 /*
731 * Get buffer of block pointers, zero those entries corresponding
732 * to blocks to be free'd, and update on disk copy first. Since
733 * double(triple) indirect before single(double) indirect, calls
734 * to bmap on these blocks will fail. However, we already have
735 * the on disk address, so we have to set the b_blkno field
736 * explicitly instead of letting bread do everything for us.
737 */
738 vp = ITOV(ip);
739 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
740 if (bp->b_flags & (B_DONE | B_DELWRI)) {
741 /* Braces must be here in case trace evaluates to nothing. */
742 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
743 } else {
744 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
745 l->l_proc->p_stats->p_ru.ru_inblock++; /* pay for read */
746 bp->b_flags |= B_READ;
747 if (bp->b_bcount > bp->b_bufsize)
748 panic("lfs_indirtrunc: bad buffer size");
749 bp->b_blkno = fsbtodb(fs, dbn);
750 VOP_STRATEGY(vp, bp);
751 error = biowait(bp);
752 }
753 if (error) {
754 brelse(bp, 0);
755 *countp = *rcountp = 0;
756 return (error);
757 }
758
759 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
760 if (lastbn >= 0) {
761 copy = (int32_t *)lfs_malloc(fs, fs->lfs_bsize, LFS_NB_IBLOCK);
762 memcpy((void *)copy, (void *)bap, (u_int)fs->lfs_bsize);
763 memset((void *)&bap[last + 1], 0,
764 /* XXX ondisk32 */
765 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
766 error = VOP_BWRITE(bp);
767 if (error)
768 allerror = error;
769 bap = copy;
770 }
771
772 /*
773 * Recursively free totally unused blocks.
774 */
775 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
776 i--, nlbn += factor) {
777 nb = bap[i];
778 if (nb == 0)
779 continue;
780 if (level > SINGLE) {
781 error = lfs_indirtrunc(ip, nlbn, nb,
782 (daddr_t)-1, level - 1,
783 &blkcount, &rblkcount,
784 lastsegp, bcp, l);
785 if (error)
786 allerror = error;
787 blocksreleased += blkcount;
788 real_released += rblkcount;
789 }
790 lfs_blkfree(fs, ip, nb, fs->lfs_bsize, lastsegp, bcp);
791 if (bap[i] > 0)
792 real_released += nblocks;
793 blocksreleased += nblocks;
794 }
795
796 /*
797 * Recursively free last partial block.
798 */
799 if (level > SINGLE && lastbn >= 0) {
800 last = lastbn % factor;
801 nb = bap[i];
802 if (nb != 0) {
803 error = lfs_indirtrunc(ip, nlbn, nb,
804 last, level - 1, &blkcount,
805 &rblkcount, lastsegp, bcp, l);
806 if (error)
807 allerror = error;
808 real_released += rblkcount;
809 blocksreleased += blkcount;
810 }
811 }
812
813 if (copy != NULL) {
814 lfs_free(fs, copy, LFS_NB_IBLOCK);
815 } else {
816 if (bp->b_flags & B_DELWRI) {
817 LFS_UNLOCK_BUF(bp);
818 fs->lfs_avail += btofsb(fs, bp->b_bcount);
819 wakeup(&fs->lfs_avail);
820 }
821 brelse(bp, BC_INVAL);
822 }
823
824 *countp = blocksreleased;
825 *rcountp = real_released;
826 return (allerror);
827 }
828
829 /*
830 * Destroy any in core blocks past the truncation length.
831 * Inlined from vtruncbuf, so that lfs_avail could be updated.
832 * We take the seglock to prevent cleaning from occurring while we are
833 * invalidating blocks.
834 */
835 static int
836 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
837 {
838 struct buf *bp, *nbp;
839 int s, error;
840 struct lfs *fs;
841 voff_t off;
842
843 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
844 simple_lock(&vp->v_interlock);
845 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
846 if (error)
847 return error;
848
849 fs = VTOI(vp)->i_lfs;
850 s = splbio();
851
852 ASSERT_SEGLOCK(fs);
853 restart:
854 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
855 nbp = LIST_NEXT(bp, b_vnbufs);
856 if (bp->b_lblkno < lbn)
857 continue;
858 simple_lock(&bp->b_interlock);
859 if (bp->b_flags & B_BUSY) {
860 bp->b_flags |= B_WANTED;
861 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
862 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
863 if (error) {
864 splx(s);
865 return (error);
866 }
867 goto restart;
868 }
869 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
870 if (bp->b_flags & B_DELWRI) {
871 bp->b_flags &= ~B_DELWRI;
872 fs->lfs_avail += btofsb(fs, bp->b_bcount);
873 wakeup(&fs->lfs_avail);
874 }
875 LFS_UNLOCK_BUF(bp);
876 simple_unlock(&bp->b_interlock);
877 brelse(bp, 0);
878 }
879
880 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
881 nbp = LIST_NEXT(bp, b_vnbufs);
882 if (bp->b_lblkno < lbn)
883 continue;
884 simple_lock(&bp->b_interlock);
885 if (bp->b_flags & B_BUSY) {
886 bp->b_flags |= B_WANTED;
887 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
888 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
889 if (error) {
890 splx(s);
891 return (error);
892 }
893 goto restart;
894 }
895 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
896 if (bp->b_flags & B_DELWRI) {
897 bp->b_flags &= ~B_DELWRI;
898 fs->lfs_avail += btofsb(fs, bp->b_bcount);
899 wakeup(&fs->lfs_avail);
900 }
901 LFS_UNLOCK_BUF(bp);
902 simple_unlock(&bp->b_interlock);
903 brelse(bp, 0);
904 }
905
906 splx(s);
907
908 return (0);
909 }
910
911