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