lfs_inode.c revision 1.78 1 /* $NetBSD: lfs_inode.c,v 1.78 2003/07/12 16:17:07 yamt 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. 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 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_inode.c,v 1.78 2003/07/12 16:17:07 yamt Exp $");
75
76 #if defined(_KERNEL_OPT)
77 #include "opt_quota.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/mount.h>
83 #include <sys/proc.h>
84 #include <sys/file.h>
85 #include <sys/buf.h>
86 #include <sys/vnode.h>
87 #include <sys/kernel.h>
88 #include <sys/malloc.h>
89 #include <sys/trace.h>
90 #include <sys/resourcevar.h>
91
92 #include <ufs/ufs/quota.h>
93 #include <ufs/ufs/inode.h>
94 #include <ufs/ufs/ufsmount.h>
95 #include <ufs/ufs/ufs_extern.h>
96
97 #include <ufs/lfs/lfs.h>
98 #include <ufs/lfs/lfs_extern.h>
99
100 static int lfs_update_seguse(struct lfs *, long, size_t);
101 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t,
102 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 ufs1_dinode *
109 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp)
110 {
111 struct ufs1_dinode *dip = (struct ufs1_dinode *)bp->b_data;
112 struct ufs1_dinode *ldip, *fin;
113
114 #ifdef LFS_IFILE_FRAG_ADDRESSING
115 if (fs->lfs_version == 1)
116 fin = dip + INOPB(fs);
117 else
118 fin = dip + INOPF(fs);
119 #else
120 fin = dip + INOPB(fs);
121 #endif
122
123 /*
124 * Read the inode block backwards, since later versions of the
125 * inode will supercede earlier ones. Though it is unlikely, it is
126 * possible that the same inode will appear in the same inode block.
127 */
128 for (ldip = fin - 1; ldip >= dip; --ldip)
129 if (ldip->di_inumber == ino)
130 return (ldip);
131
132 printf("searched %d entries\n", (int)(fin - dip));
133 printf("offset is 0x%x (seg %d)\n", fs->lfs_offset,
134 dtosn(fs, fs->lfs_offset));
135 printf("block is 0x%llx (seg %lld)\n",
136 (unsigned long long)dbtofsb(fs, bp->b_blkno),
137 (long long)dtosn(fs, dbtofsb(fs, bp->b_blkno)));
138
139 return NULL;
140 }
141
142 int
143 lfs_update(void *v)
144 {
145 struct vop_update_args /* {
146 struct vnode *a_vp;
147 struct timespec *a_access;
148 struct timespec *a_modify;
149 int a_flags;
150 } */ *ap = v;
151 struct inode *ip;
152 struct vnode *vp = ap->a_vp;
153 struct timespec ts;
154 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
155 int s;
156
157 if (vp->v_mount->mnt_flag & MNT_RDONLY)
158 return (0);
159 ip = VTOI(vp);
160
161 /*
162 * If we are called from vinvalbuf, and the file's blocks have
163 * already been scheduled for writing, but the writes have not
164 * yet completed, lfs_vflush will not be called, and vinvalbuf
165 * will cause a panic. So, we must wait until any pending write
166 * for our inode completes, if we are called with UPDATE_WAIT set.
167 */
168 s = splbio();
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 splx(s);
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%x, 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
220 int
221 lfs_truncate(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 struct genfs_node *gp = VTOG(ovp);
232 daddr_t lastblock;
233 struct inode *oip;
234 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
235 /* XXX ondisk32 */
236 int32_t newblks[NDADDR + NIADDR];
237 off_t length = ap->a_length;
238 struct lfs *fs;
239 struct buf *bp;
240 int offset, size, level;
241 long count, rcount, nblocks, blocksreleased = 0, real_released = 0;
242 int i;
243 int aflags, error, allerror = 0;
244 off_t osize;
245 voff_t eoz;
246 long lastseg;
247 size_t bc;
248 int obufsize, odb;
249 int usepc;
250
251 if (length < 0)
252 return (EINVAL);
253 oip = VTOI(ovp);
254
255 /*
256 * Just return and not update modification times.
257 */
258 if (oip->i_size == length)
259 return (0);
260
261 if (ovp->v_type == VLNK &&
262 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen ||
263 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
264 oip->i_ffs1_blocks == 0))) {
265 #ifdef DIAGNOSTIC
266 if (length != 0)
267 panic("lfs_truncate: partial truncate of symlink");
268 #endif
269 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
270 oip->i_size = oip->i_ffs1_size = 0;
271 oip->i_flag |= IN_CHANGE | IN_UPDATE;
272 return (VOP_UPDATE(ovp, NULL, NULL, 0));
273 }
274 if (oip->i_size == length) {
275 oip->i_flag |= IN_CHANGE | IN_UPDATE;
276 return (VOP_UPDATE(ovp, NULL, NULL, 0));
277 }
278 #ifdef QUOTA
279 if ((error = getinoquota(oip)) != 0)
280 return (error);
281 #endif
282 fs = oip->i_lfs;
283 lfs_imtime(fs);
284 osize = oip->i_size;
285 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode);
286
287 /*
288 * Lengthen the size of the file. We must ensure that the
289 * last byte of the file is allocated. Since the smallest
290 * value of osize is 0, length will be at least 1.
291 */
292 if (osize < length) {
293 if (length > fs->lfs_maxfilesize)
294 return (EFBIG);
295 aflags = B_CLRBUF;
296 if (ap->a_flags & IO_SYNC)
297 aflags |= B_SYNC;
298 if (usepc) {
299 if (lblkno(fs, osize) < NDADDR &&
300 lblkno(fs, osize) != lblkno(fs, length) &&
301 blkroundup(fs, osize) != osize) {
302 error = ufs_balloc_range(ovp, osize,
303 blkroundup(fs, osize) -
304 osize, ap->a_cred,
305 aflags);
306 if (error) {
307 return error;
308 }
309 if (ap->a_flags & IO_SYNC) {
310 ovp->v_size = blkroundup(fs, osize);
311 simple_lock(&ovp->v_interlock);
312 VOP_PUTPAGES(ovp,
313 trunc_page(osize & fs->lfs_bmask),
314 round_page(ovp->v_size),
315 PGO_CLEANIT | PGO_SYNCIO);
316 }
317 }
318 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
319 aflags);
320 if (error) {
321 (void) VOP_TRUNCATE(ovp, osize,
322 ap->a_flags & IO_SYNC,
323 ap->a_cred, ap->a_p);
324 return error;
325 }
326 uvm_vnp_setsize(ovp, length);
327 oip->i_flag |= IN_CHANGE | IN_UPDATE;
328 KASSERT(ovp->v_size == oip->i_size);
329 return (VOP_UPDATE(ovp, NULL, NULL, 0));
330 } else {
331 error = lfs_reserve(fs, ovp, NULL,
332 btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
333 if (error)
334 return (error);
335 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred,
336 aflags, &bp);
337 lfs_reserve(fs, ovp, NULL,
338 -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
339 if (error)
340 return (error);
341 oip->i_ffs1_size = oip->i_size = length;
342 uvm_vnp_setsize(ovp, length);
343 (void) VOP_BWRITE(bp);
344 oip->i_flag |= IN_CHANGE | IN_UPDATE;
345 return (VOP_UPDATE(ovp, NULL, NULL, 0));
346 }
347 }
348
349 if ((error = lfs_reserve(fs, ovp, NULL,
350 btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
351 return (error);
352
353 /*
354 * Shorten the size of the file. If the file is not being
355 * truncated to a block boundary, the contents of the
356 * partial block following the end of the file must be
357 * zero'ed in case it ever becomes accessible again because
358 * of subsequent file growth. Directories however are not
359 * zero'ed as they should grow back initialized to empty.
360 */
361 offset = blkoff(fs, length);
362 lastseg = -1;
363 bc = 0;
364
365 lfs_seglock(fs, SEGM_PROT);
366 if (offset == 0) {
367 oip->i_size = oip->i_ffs1_size = length;
368 } else if (!usepc) {
369 lbn = lblkno(fs, length);
370 aflags = B_CLRBUF;
371 if (ap->a_flags & IO_SYNC)
372 aflags |= B_SYNC;
373 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
374 if (error) {
375 lfs_reserve(fs, ovp, NULL,
376 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
377 goto errout;
378 }
379 obufsize = bp->b_bufsize;
380 odb = btofsb(fs, bp->b_bcount);
381 oip->i_size = oip->i_ffs1_size = length;
382 size = blksize(fs, oip, lbn);
383 if (ovp->v_type != VDIR)
384 memset((char *)bp->b_data + offset, 0,
385 (u_int)(size - offset));
386 allocbuf(bp, size);
387 if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
388 locked_queue_bytes -= obufsize - bp->b_bufsize;
389 if (bp->b_flags & B_DELWRI)
390 fs->lfs_avail += odb - btofsb(fs, size);
391 (void) VOP_BWRITE(bp);
392 } else { /* vp->v_type == VREG && length < osize && offset != 0 */
393 /*
394 * When truncating a regular file down to a non-block-aligned
395 * size, we must zero the part of last block which is past
396 * the new EOF. We must synchronously flush the zeroed pages
397 * to disk since the new pages will be invalidated as soon
398 * as we inform the VM system of the new, smaller size.
399 * We must do this before acquiring the GLOCK, since fetching
400 * the pages will acquire the GLOCK internally.
401 * So there is a window where another thread could see a whole
402 * zeroed page past EOF, but that's life.
403 */
404 aflags = ap->a_flags & IO_SYNC ? B_SYNC : 0;
405 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
406 aflags);
407 if (error) {
408 goto errout;
409 }
410 eoz = blkroundup(fs, length);
411 uvm_vnp_zerorange(ovp, length, eoz - length);
412 simple_lock(&ovp->v_interlock);
413 error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
414 PGO_CLEANIT | PGO_DEACTIVATE | (aflags ? PGO_SYNCIO : 0));
415 if (error) {
416 goto errout;
417 }
418 }
419
420 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
421
422 oip->i_size = oip->i_ffs1_size = length;
423 uvm_vnp_setsize(ovp, length);
424 /*
425 * Calculate index into inode's block list of
426 * last direct and indirect blocks (if any)
427 * which we want to keep. Lastblock is -1 when
428 * the file is truncated to 0.
429 */
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((caddr_t)newblks, (caddr_t)&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, ap->a_p);
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, bn, fs->lfs_bsize, &lastseg, &bc);
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 }
504 if (lastblock < 0)
505 goto done;
506
507 /*
508 * Finally, look for a change in size of the
509 * last direct block; release any frags.
510 */
511 bn = oip->i_ffs1_db[lastblock];
512 if (bn != 0) {
513 long oldspace, newspace;
514 #if 0
515 long olddspace;
516 #endif
517
518 /*
519 * Calculate amount of space we're giving
520 * back as old block size minus new block size.
521 */
522 oldspace = blksize(fs, oip, lastblock);
523 #if 0
524 olddspace = oip->i_lfs_fragsize[lastblock];
525 #endif
526
527 oip->i_size = oip->i_ffs1_size = length;
528 newspace = blksize(fs, oip, lastblock);
529 if (newspace == 0)
530 panic("itrunc: newspace");
531 if (oldspace - newspace > 0) {
532 blocksreleased += btofsb(fs, oldspace - newspace);
533 }
534 #if 0
535 if (bn > 0 && olddspace - newspace > 0) {
536 /* No segment accounting here, just vnode */
537 real_released += btofsb(fs, olddspace - newspace);
538 }
539 #endif
540 }
541
542 done:
543 /* Finish segment accounting corrections */
544 lfs_update_seguse(fs, lastseg, bc);
545 #ifdef DIAGNOSTIC
546 for (level = SINGLE; level <= TRIPLE; level++)
547 if ((newblks[NDADDR + level] == 0) !=
548 (oip->i_ffs1_ib[level]) == 0) {
549 panic("lfs itrunc1");
550 }
551 for (i = 0; i < NDADDR; i++)
552 if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) {
553 panic("lfs itrunc2");
554 }
555 if (length == 0 &&
556 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
557 panic("lfs itrunc3");
558 #endif /* DIAGNOSTIC */
559 /*
560 * Put back the real size.
561 */
562 oip->i_size = oip->i_ffs1_size = length;
563 oip->i_lfs_effnblks -= blocksreleased;
564 oip->i_ffs1_blocks -= real_released;
565 fs->lfs_bfree += blocksreleased;
566 #ifdef DIAGNOSTIC
567 if (oip->i_size == 0 && oip->i_ffs1_blocks != 0) {
568 printf("lfs_truncate: truncate to 0 but %d blocks on inode\n",
569 oip->i_ffs1_blocks);
570 panic("lfs_truncate: persistent blocks");
571 }
572 #endif
573 oip->i_flag |= IN_CHANGE;
574 #ifdef QUOTA
575 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
576 #endif
577 lfs_reserve(fs, ovp, NULL,
578 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
579 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
580 errout:
581 lfs_segunlock(fs);
582 return (allerror ? allerror : error);
583 }
584
585 /* Update segment usage information when removing a block. */
586 static int
587 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
588 size_t *num)
589 {
590 long seg;
591 int error = 0;
592
593 bsize = fragroundup(fs, bsize);
594 if (daddr > 0) {
595 if (*lastseg != (seg = dtosn(fs, daddr))) {
596 error = lfs_update_seguse(fs, *lastseg, *num);
597 *num = bsize;
598 *lastseg = seg;
599 } else
600 *num += bsize;
601 }
602 return error;
603 }
604
605 /* Finish the accounting updates for a segment. */
606 static int
607 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
608 {
609 SEGUSE *sup;
610 struct buf *bp;
611
612 if (lastseg < 0 || num == 0)
613 return 0;
614
615 LFS_SEGENTRY(sup, fs, lastseg, bp);
616 if (num > sup->su_nbytes) {
617 printf("lfs_truncate: segment %ld short by %ld\n",
618 lastseg, (long)num - sup->su_nbytes);
619 panic("lfs_truncate: negative bytes");
620 sup->su_nbytes = num;
621 }
622 sup->su_nbytes -= num;
623 LFS_WRITESEGENTRY(sup, fs, lastseg, bp);
624
625 return 0;
626 }
627
628 /*
629 * Release blocks associated with the inode ip and stored in the indirect
630 * block bn. Blocks are free'd in LIFO order up to (but not including)
631 * lastbn. If level is greater than SINGLE, the block is an indirect block
632 * and recursive calls to indirtrunc must be used to cleanse other indirect
633 * blocks.
634 *
635 * NB: triple indirect blocks are untested.
636 */
637 static int
638 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
639 daddr_t lastbn, int level, long *countp,
640 long *rcountp, long *lastsegp, size_t *bcp, struct proc *p)
641 {
642 int i;
643 struct buf *bp;
644 struct lfs *fs = ip->i_lfs;
645 int32_t *bap; /* XXX ondisk32 */
646 struct vnode *vp;
647 daddr_t nb, nlbn, last;
648 int32_t *copy = NULL; /* XXX ondisk32 */
649 long blkcount, rblkcount, factor;
650 int nblocks, blocksreleased = 0, real_released = 0;
651 int error = 0, allerror = 0;
652
653 /*
654 * Calculate index in current block of last
655 * block to be kept. -1 indicates the entire
656 * block so we need not calculate the index.
657 */
658 factor = 1;
659 for (i = SINGLE; i < level; i++)
660 factor *= NINDIR(fs);
661 last = lastbn;
662 if (lastbn > 0)
663 last /= factor;
664 nblocks = btofsb(fs, fs->lfs_bsize);
665 /*
666 * Get buffer of block pointers, zero those entries corresponding
667 * to blocks to be free'd, and update on disk copy first. Since
668 * double(triple) indirect before single(double) indirect, calls
669 * to bmap on these blocks will fail. However, we already have
670 * the on disk address, so we have to set the b_blkno field
671 * explicitly instead of letting bread do everything for us.
672 */
673 vp = ITOV(ip);
674 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
675 if (bp->b_flags & (B_DONE | B_DELWRI)) {
676 /* Braces must be here in case trace evaluates to nothing. */
677 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
678 } else {
679 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
680 p->p_stats->p_ru.ru_inblock++; /* pay for read */
681 bp->b_flags |= B_READ;
682 if (bp->b_bcount > bp->b_bufsize)
683 panic("lfs_indirtrunc: bad buffer size");
684 bp->b_blkno = fsbtodb(fs, dbn);
685 VOP_STRATEGY(bp);
686 error = biowait(bp);
687 }
688 if (error) {
689 brelse(bp);
690 *countp = *rcountp = 0;
691 return (error);
692 }
693
694 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
695 if (lastbn >= 0) {
696 MALLOC(copy, int32_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
697 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
698 memset((caddr_t)&bap[last + 1], 0,
699 /* XXX ondisk32 */
700 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
701 error = VOP_BWRITE(bp);
702 if (error)
703 allerror = error;
704 bap = copy;
705 }
706
707 /*
708 * Recursively free totally unused blocks.
709 */
710 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
711 i--, nlbn += factor) {
712 nb = bap[i];
713 if (nb == 0)
714 continue;
715 if (level > SINGLE) {
716 error = lfs_indirtrunc(ip, nlbn, nb,
717 (daddr_t)-1, level - 1,
718 &blkcount, &rblkcount,
719 lastsegp, bcp, p);
720 if (error)
721 allerror = error;
722 blocksreleased += blkcount;
723 real_released += rblkcount;
724 }
725 lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
726 if (bap[i] > 0)
727 real_released += nblocks;
728 blocksreleased += nblocks;
729 }
730
731 /*
732 * Recursively free last partial block.
733 */
734 if (level > SINGLE && lastbn >= 0) {
735 last = lastbn % factor;
736 nb = bap[i];
737 if (nb != 0) {
738 error = lfs_indirtrunc(ip, nlbn, nb,
739 last, level - 1, &blkcount,
740 &rblkcount, lastsegp, bcp, p);
741 if (error)
742 allerror = error;
743 real_released += rblkcount;
744 blocksreleased += blkcount;
745 }
746 }
747
748 if (copy != NULL) {
749 FREE(copy, M_TEMP);
750 } else {
751 if (bp->b_flags & B_DELWRI) {
752 LFS_UNLOCK_BUF(bp);
753 fs->lfs_avail += btofsb(fs, bp->b_bcount);
754 wakeup(&fs->lfs_avail);
755 }
756 bp->b_flags |= B_INVAL;
757 brelse(bp);
758 }
759
760 *countp = blocksreleased;
761 *rcountp = real_released;
762 return (allerror);
763 }
764
765 /*
766 * Destroy any in core blocks past the truncation length.
767 * Inlined from vtruncbuf, so that lfs_avail could be updated.
768 * We take the seglock to prevent cleaning from occurring while we are
769 * invalidating blocks.
770 */
771 static int
772 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
773 {
774 struct buf *bp, *nbp;
775 int s, error;
776 struct lfs *fs;
777 voff_t off;
778
779 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
780 simple_lock(&vp->v_interlock);
781 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
782 if (error) {
783 return error;
784 }
785
786 fs = VTOI(vp)->i_lfs;
787 s = splbio();
788
789 restart:
790 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
791 nbp = LIST_NEXT(bp, b_vnbufs);
792 if (bp->b_lblkno < lbn)
793 continue;
794 simple_lock(&bp->b_interlock);
795 if (bp->b_flags & B_BUSY) {
796 bp->b_flags |= B_WANTED;
797 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
798 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
799 if (error) {
800 splx(s);
801 return (error);
802 }
803 goto restart;
804 }
805 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
806 if (bp->b_flags & B_DELWRI) {
807 bp->b_flags &= ~B_DELWRI;
808 fs->lfs_avail += btofsb(fs, bp->b_bcount);
809 wakeup(&fs->lfs_avail);
810 }
811 LFS_UNLOCK_BUF(bp);
812 simple_unlock(&bp->b_interlock);
813 brelse(bp);
814 }
815
816 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
817 nbp = LIST_NEXT(bp, b_vnbufs);
818 if (bp->b_lblkno < lbn)
819 continue;
820 simple_lock(&bp->b_interlock);
821 if (bp->b_flags & B_BUSY) {
822 bp->b_flags |= B_WANTED;
823 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
824 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
825 if (error) {
826 splx(s);
827 return (error);
828 }
829 goto restart;
830 }
831 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
832 if (bp->b_flags & B_DELWRI) {
833 bp->b_flags &= ~B_DELWRI;
834 fs->lfs_avail += btofsb(fs, bp->b_bcount);
835 wakeup(&fs->lfs_avail);
836 }
837 LFS_UNLOCK_BUF(bp);
838 simple_unlock(&bp->b_interlock);
839 brelse(bp);
840 }
841
842 splx(s);
843
844 return (0);
845 }
846
847