lfs_inode.c revision 1.90 1 /* $NetBSD: lfs_inode.c,v 1.90 2005/03/08 00:18:19 perseant 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.90 2005/03/08 00:18:19 perseant 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/malloc.h>
85 #include <sys/trace.h>
86 #include <sys/resourcevar.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 proc *);
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 /*
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 timespec ts;
142 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
143 int s;
144 int flags;
145
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 while ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT &&
159 WRITEINPROG(vp)) {
160 DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d"
161 " (in progress)\n", ip->i_number));
162 tsleep(vp, (PRIBIO+1), "lfs_update", 0);
163 }
164 splx(s);
165 TIMEVAL_TO_TIMESPEC(&time, &ts);
166 LFS_ITIMES(ip,
167 ap->a_access ? ap->a_access : &ts,
168 ap->a_modify ? ap->a_modify : &ts, &ts);
169 if (ap->a_flags & UPDATE_CLOSE)
170 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING);
171 else
172 flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING);
173 if (flags == 0)
174 return (0);
175
176 /* If sync, push back the vnode and any dirty blocks it may have. */
177 if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) {
178 /* Avoid flushing VDIROP. */
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 tsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync",
189 0);
190 /* XXX KS - by falling out here, are we writing the vn
191 twice? */
192 }
193 --fs->lfs_diropwait;
194 return lfs_vflush(vp);
195 }
196 return 0;
197 }
198
199 #define SINGLE 0 /* index of single indirect block */
200 #define DOUBLE 1 /* index of double indirect block */
201 #define TRIPLE 2 /* index of triple indirect block */
202 /*
203 * Truncate the inode oip to at most length size, freeing the
204 * disk blocks.
205 */
206 /* VOP_BWRITE 1 + NIADDR + VOP_BALLOC == 2 + 2*NIADDR times */
207
208 int
209 lfs_truncate(void *v)
210 {
211 struct vop_truncate_args /* {
212 struct vnode *a_vp;
213 off_t a_length;
214 int a_flags;
215 struct ucred *a_cred;
216 struct proc *a_p;
217 } */ *ap = v;
218 struct vnode *ovp = ap->a_vp;
219 struct genfs_node *gp = VTOG(ovp);
220 daddr_t lastblock;
221 struct inode *oip = VTOI(ovp);
222 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
223 /* XXX ondisk32 */
224 int32_t newblks[NDADDR + NIADDR];
225 off_t length = ap->a_length;
226 struct lfs *fs;
227 struct buf *bp;
228 int offset, size, level;
229 long count, rcount, blocksreleased = 0, real_released = 0;
230 int i, ioflag, nblocks;
231 int aflags, error, allerror = 0;
232 off_t osize;
233 long lastseg;
234 size_t bc;
235 int obufsize, odb;
236 int usepc;
237 struct ufsmount *ump = oip->i_ump;
238
239 if (length < 0)
240 return (EINVAL);
241
242 /*
243 * Just return and not update modification times.
244 */
245 if (oip->i_size == length)
246 return (0);
247
248 if (ovp->v_type == VLNK &&
249 (oip->i_size < ump->um_maxsymlinklen ||
250 (ump->um_maxsymlinklen == 0 &&
251 oip->i_ffs1_blocks == 0))) {
252 #ifdef DIAGNOSTIC
253 if (length != 0)
254 panic("lfs_truncate: partial truncate of symlink");
255 #endif
256 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
257 oip->i_size = oip->i_ffs1_size = 0;
258 oip->i_flag |= IN_CHANGE | IN_UPDATE;
259 return (VOP_UPDATE(ovp, NULL, NULL, 0));
260 }
261 if (oip->i_size == length) {
262 oip->i_flag |= IN_CHANGE | IN_UPDATE;
263 return (VOP_UPDATE(ovp, NULL, NULL, 0));
264 }
265 #ifdef QUOTA
266 if ((error = getinoquota(oip)) != 0)
267 return (error);
268 #endif
269 fs = oip->i_lfs;
270 lfs_imtime(fs);
271 osize = oip->i_size;
272 ioflag = ap->a_flags;
273 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode);
274
275 /*
276 * Lengthen the size of the file. We must ensure that the
277 * last byte of the file is allocated. Since the smallest
278 * value of osize is 0, length will be at least 1.
279 */
280 if (osize < length) {
281 if (length > ump->um_maxfilesize)
282 return (EFBIG);
283 aflags = B_CLRBUF;
284 if (ioflag & IO_SYNC)
285 aflags |= B_SYNC;
286 if (usepc) {
287 if (lblkno(fs, osize) < NDADDR &&
288 lblkno(fs, osize) != lblkno(fs, length) &&
289 blkroundup(fs, osize) != osize) {
290 off_t eob;
291
292 eob = blkroundup(fs, osize);
293 error = ufs_balloc_range(ovp, osize,
294 eob - osize, ap->a_cred, aflags);
295 if (error)
296 return error;
297 if (ioflag & IO_SYNC) {
298 ovp->v_size = eob;
299 simple_lock(&ovp->v_interlock);
300 VOP_PUTPAGES(ovp,
301 trunc_page(osize & fs->lfs_bmask),
302 round_page(eob),
303 PGO_CLEANIT | PGO_SYNCIO);
304 }
305 }
306 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
307 aflags);
308 if (error) {
309 (void) VOP_TRUNCATE(ovp, osize,
310 ioflag & IO_SYNC,
311 ap->a_cred, ap->a_p);
312 return error;
313 }
314 uvm_vnp_setsize(ovp, length);
315 oip->i_flag |= IN_CHANGE | IN_UPDATE;
316 KASSERT(ovp->v_size == oip->i_size);
317 return (VOP_UPDATE(ovp, NULL, NULL, 0));
318 } else {
319 error = lfs_reserve(fs, ovp, NULL,
320 btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
321 if (error)
322 return (error);
323 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred,
324 aflags, &bp);
325 lfs_reserve(fs, ovp, NULL,
326 -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift));
327 if (error)
328 return (error);
329 oip->i_ffs1_size = oip->i_size = length;
330 uvm_vnp_setsize(ovp, length);
331 (void) VOP_BWRITE(bp);
332 oip->i_flag |= IN_CHANGE | IN_UPDATE;
333 return (VOP_UPDATE(ovp, NULL, NULL, 0));
334 }
335 }
336
337 if ((error = lfs_reserve(fs, ovp, NULL,
338 btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0)
339 return (error);
340
341 /*
342 * Shorten the size of the file. If the file is not being
343 * truncated to a block boundary, the contents of the
344 * partial block following the end of the file must be
345 * zero'ed in case it ever becomes accessible again because
346 * of subsequent file growth. Directories however are not
347 * zero'ed as they should grow back initialized to empty.
348 */
349 offset = blkoff(fs, length);
350 lastseg = -1;
351 bc = 0;
352
353 lfs_seglock(fs, SEGM_PROT);
354 if (offset == 0) {
355 oip->i_size = oip->i_ffs1_size = length;
356 } else if (!usepc) {
357 lbn = lblkno(fs, length);
358 aflags = B_CLRBUF;
359 if (ioflag & IO_SYNC)
360 aflags |= B_SYNC;
361 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
362 if (error) {
363 lfs_reserve(fs, ovp, NULL,
364 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
365 goto errout;
366 }
367 obufsize = bp->b_bufsize;
368 odb = btofsb(fs, bp->b_bcount);
369 oip->i_size = oip->i_ffs1_size = length;
370 size = blksize(fs, oip, lbn);
371 if (ovp->v_type != VDIR)
372 memset((char *)bp->b_data + offset, 0,
373 (u_int)(size - offset));
374 allocbuf(bp, size, 1);
375 if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
376 locked_queue_bytes -= obufsize - bp->b_bufsize;
377 if (bp->b_flags & B_DELWRI)
378 fs->lfs_avail += odb - btofsb(fs, size);
379 (void) VOP_BWRITE(bp);
380 } else { /* vp->v_type == VREG && length < osize && offset != 0 */
381 /*
382 * When truncating a regular file down to a non-block-aligned
383 * size, we must zero the part of last block which is past
384 * the new EOF. We must synchronously flush the zeroed pages
385 * to disk since the new pages will be invalidated as soon
386 * as we inform the VM system of the new, smaller size.
387 * We must do this before acquiring the GLOCK, since fetching
388 * the pages will acquire the GLOCK internally.
389 * So there is a window where another thread could see a whole
390 * zeroed page past EOF, but that's life.
391 */
392 daddr_t lbn;
393 voff_t eoz;
394
395 aflags = ioflag & IO_SYNC ? B_SYNC : 0;
396 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
397 aflags);
398 if (error) {
399 lfs_reserve(fs, ovp, NULL,
400 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
401 goto errout;
402 }
403 lbn = lblkno(fs, length);
404 size = blksize(fs, oip, lbn);
405 eoz = MIN(lblktosize(fs, lbn) + size, osize);
406 uvm_vnp_zerorange(ovp, length, eoz - length);
407 if (round_page(eoz) > round_page(length)) {
408 simple_lock(&ovp->v_interlock);
409 error = VOP_PUTPAGES(ovp, round_page(length),
410 round_page(eoz),
411 PGO_CLEANIT | PGO_DEACTIVATE |
412 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
413 if (error) {
414 lfs_reserve(fs, ovp, NULL,
415 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
416 goto errout;
417 }
418 }
419 }
420
421 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
422
423 oip->i_size = oip->i_ffs1_size = length;
424 uvm_vnp_setsize(ovp, length);
425 /*
426 * Calculate index into inode's block list of
427 * last direct and indirect blocks (if any)
428 * which we want to keep. Lastblock is -1 when
429 * the file is truncated to 0.
430 */
431 lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1;
432 lastiblock[SINGLE] = lastblock - NDADDR;
433 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
434 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
435 nblocks = btofsb(fs, fs->lfs_bsize);
436 /*
437 * Record changed file and block pointers before we start
438 * freeing blocks. lastiblock values are also normalized to -1
439 * for calls to lfs_indirtrunc below.
440 */
441 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs1_db[0], sizeof newblks);
442 for (level = TRIPLE; level >= SINGLE; level--)
443 if (lastiblock[level] < 0) {
444 newblks[NDADDR+level] = 0;
445 lastiblock[level] = -1;
446 }
447 for (i = NDADDR - 1; i > lastblock; i--)
448 newblks[i] = 0;
449
450 oip->i_size = oip->i_ffs1_size = osize;
451 error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0);
452 if (error && !allerror)
453 allerror = error;
454
455 /*
456 * Indirect blocks first.
457 */
458 indir_lbn[SINGLE] = -NDADDR;
459 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
460 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
461 for (level = TRIPLE; level >= SINGLE; level--) {
462 bn = oip->i_ffs1_ib[level];
463 if (bn != 0) {
464 error = lfs_indirtrunc(oip, indir_lbn[level],
465 bn, lastiblock[level],
466 level, &count, &rcount,
467 &lastseg, &bc, ap->a_p);
468 if (error)
469 allerror = error;
470 real_released += rcount;
471 blocksreleased += count;
472 if (lastiblock[level] < 0) {
473 if (oip->i_ffs1_ib[level] > 0)
474 real_released += nblocks;
475 blocksreleased += nblocks;
476 oip->i_ffs1_ib[level] = 0;
477 lfs_blkfree(fs, bn, fs->lfs_bsize, &lastseg, &bc);
478 }
479 }
480 if (lastiblock[level] >= 0)
481 goto done;
482 }
483
484 /*
485 * All whole direct blocks or frags.
486 */
487 for (i = NDADDR - 1; i > lastblock; i--) {
488 long bsize, obsize;
489
490 bn = oip->i_ffs1_db[i];
491 if (bn == 0)
492 continue;
493 bsize = blksize(fs, oip, i);
494 if (oip->i_ffs1_db[i] > 0) {
495 /* Check for fragment size changes */
496 obsize = oip->i_lfs_fragsize[i];
497 real_released += btofsb(fs, obsize);
498 oip->i_lfs_fragsize[i] = 0;
499 } else
500 obsize = 0;
501 blocksreleased += btofsb(fs, bsize);
502 oip->i_ffs1_db[i] = 0;
503 lfs_blkfree(fs, bn, obsize, &lastseg, &bc);
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 fs->lfs_bfree += blocksreleased;
567 #ifdef DIAGNOSTIC
568 if (oip->i_size == 0 &&
569 (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) {
570 printf("lfs_truncate: truncate to 0 but %d blks/%d effblks\n",
571 oip->i_ffs1_blocks, oip->i_lfs_effnblks);
572 panic("lfs_truncate: persistent blocks");
573 }
574 #endif
575 oip->i_flag |= IN_CHANGE;
576 #ifdef QUOTA
577 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
578 #endif
579 lfs_reserve(fs, ovp, NULL,
580 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift));
581 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
582 errout:
583 lfs_segunlock(fs);
584 return (allerror ? allerror : error);
585 }
586
587 /* Update segment usage information when removing a block. */
588 static int
589 lfs_blkfree(struct lfs *fs, daddr_t daddr, size_t bsize, long *lastseg,
590 size_t *num)
591 {
592 long seg;
593 int error = 0;
594
595 bsize = fragroundup(fs, bsize);
596 if (daddr > 0) {
597 if (*lastseg != (seg = dtosn(fs, daddr))) {
598 error = lfs_update_seguse(fs, *lastseg, *num);
599 *num = bsize;
600 *lastseg = seg;
601 } else
602 *num += bsize;
603 }
604 return error;
605 }
606
607 /* Finish the accounting updates for a segment. */
608 static int
609 lfs_update_seguse(struct lfs *fs, long lastseg, size_t num)
610 {
611 SEGUSE *sup;
612 struct buf *bp;
613
614 if (lastseg < 0 || num == 0)
615 return 0;
616
617 LFS_SEGENTRY(sup, fs, lastseg, bp);
618 if (num > sup->su_nbytes) {
619 printf("lfs_truncate: segment %ld short by %ld\n",
620 lastseg, (long)num - sup->su_nbytes);
621 panic("lfs_truncate: negative bytes");
622 sup->su_nbytes = num;
623 }
624 sup->su_nbytes -= num;
625 LFS_WRITESEGENTRY(sup, fs, lastseg, bp);
626
627 return 0;
628 }
629
630 /*
631 * Release blocks associated with the inode ip and stored in the indirect
632 * block bn. Blocks are free'd in LIFO order up to (but not including)
633 * lastbn. If level is greater than SINGLE, the block is an indirect block
634 * and recursive calls to indirtrunc must be used to cleanse other indirect
635 * blocks.
636 *
637 * NB: triple indirect blocks are untested.
638 */
639 static int
640 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
641 daddr_t lastbn, int level, long *countp,
642 long *rcountp, long *lastsegp, size_t *bcp, struct proc *p)
643 {
644 int i;
645 struct buf *bp;
646 struct lfs *fs = ip->i_lfs;
647 int32_t *bap; /* XXX ondisk32 */
648 struct vnode *vp;
649 daddr_t nb, nlbn, last;
650 int32_t *copy = NULL; /* XXX ondisk32 */
651 long blkcount, rblkcount, factor;
652 int nblocks, blocksreleased = 0, real_released = 0;
653 int error = 0, allerror = 0;
654
655 /*
656 * Calculate index in current block of last
657 * block to be kept. -1 indicates the entire
658 * block so we need not calculate the index.
659 */
660 factor = 1;
661 for (i = SINGLE; i < level; i++)
662 factor *= NINDIR(fs);
663 last = lastbn;
664 if (lastbn > 0)
665 last /= factor;
666 nblocks = btofsb(fs, fs->lfs_bsize);
667 /*
668 * Get buffer of block pointers, zero those entries corresponding
669 * to blocks to be free'd, and update on disk copy first. Since
670 * double(triple) indirect before single(double) indirect, calls
671 * to bmap on these blocks will fail. However, we already have
672 * the on disk address, so we have to set the b_blkno field
673 * explicitly instead of letting bread do everything for us.
674 */
675 vp = ITOV(ip);
676 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0);
677 if (bp->b_flags & (B_DONE | B_DELWRI)) {
678 /* Braces must be here in case trace evaluates to nothing. */
679 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn);
680 } else {
681 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn);
682 p->p_stats->p_ru.ru_inblock++; /* pay for read */
683 bp->b_flags |= B_READ;
684 if (bp->b_bcount > bp->b_bufsize)
685 panic("lfs_indirtrunc: bad buffer size");
686 bp->b_blkno = fsbtodb(fs, dbn);
687 VOP_STRATEGY(vp, bp);
688 error = biowait(bp);
689 }
690 if (error) {
691 brelse(bp);
692 *countp = *rcountp = 0;
693 return (error);
694 }
695
696 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
697 if (lastbn >= 0) {
698 MALLOC(copy, int32_t *, fs->lfs_bsize, M_TEMP, M_WAITOK);
699 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize);
700 memset((caddr_t)&bap[last + 1], 0,
701 /* XXX ondisk32 */
702 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
703 error = VOP_BWRITE(bp);
704 if (error)
705 allerror = error;
706 bap = copy;
707 }
708
709 /*
710 * Recursively free totally unused blocks.
711 */
712 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
713 i--, nlbn += factor) {
714 nb = bap[i];
715 if (nb == 0)
716 continue;
717 if (level > SINGLE) {
718 error = lfs_indirtrunc(ip, nlbn, nb,
719 (daddr_t)-1, level - 1,
720 &blkcount, &rblkcount,
721 lastsegp, bcp, p);
722 if (error)
723 allerror = error;
724 blocksreleased += blkcount;
725 real_released += rblkcount;
726 }
727 lfs_blkfree(fs, nb, fs->lfs_bsize, lastsegp, bcp);
728 if (bap[i] > 0)
729 real_released += nblocks;
730 blocksreleased += nblocks;
731 }
732
733 /*
734 * Recursively free last partial block.
735 */
736 if (level > SINGLE && lastbn >= 0) {
737 last = lastbn % factor;
738 nb = bap[i];
739 if (nb != 0) {
740 error = lfs_indirtrunc(ip, nlbn, nb,
741 last, level - 1, &blkcount,
742 &rblkcount, lastsegp, bcp, p);
743 if (error)
744 allerror = error;
745 real_released += rblkcount;
746 blocksreleased += blkcount;
747 }
748 }
749
750 if (copy != NULL) {
751 FREE(copy, M_TEMP);
752 } else {
753 if (bp->b_flags & B_DELWRI) {
754 LFS_UNLOCK_BUF(bp);
755 fs->lfs_avail += btofsb(fs, bp->b_bcount);
756 wakeup(&fs->lfs_avail);
757 }
758 bp->b_flags |= B_INVAL;
759 brelse(bp);
760 }
761
762 *countp = blocksreleased;
763 *rcountp = real_released;
764 return (allerror);
765 }
766
767 /*
768 * Destroy any in core blocks past the truncation length.
769 * Inlined from vtruncbuf, so that lfs_avail could be updated.
770 * We take the seglock to prevent cleaning from occurring while we are
771 * invalidating blocks.
772 */
773 static int
774 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo)
775 {
776 struct buf *bp, *nbp;
777 int s, error;
778 struct lfs *fs;
779 voff_t off;
780
781 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
782 simple_lock(&vp->v_interlock);
783 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
784 if (error)
785 return error;
786
787 fs = VTOI(vp)->i_lfs;
788 s = splbio();
789
790 restart:
791 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
792 nbp = LIST_NEXT(bp, b_vnbufs);
793 if (bp->b_lblkno < lbn)
794 continue;
795 simple_lock(&bp->b_interlock);
796 if (bp->b_flags & B_BUSY) {
797 bp->b_flags |= B_WANTED;
798 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
799 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
800 if (error) {
801 splx(s);
802 return (error);
803 }
804 goto restart;
805 }
806 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
807 if (bp->b_flags & B_DELWRI) {
808 bp->b_flags &= ~B_DELWRI;
809 fs->lfs_avail += btofsb(fs, bp->b_bcount);
810 wakeup(&fs->lfs_avail);
811 }
812 LFS_UNLOCK_BUF(bp);
813 simple_unlock(&bp->b_interlock);
814 brelse(bp);
815 }
816
817 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
818 nbp = LIST_NEXT(bp, b_vnbufs);
819 if (bp->b_lblkno < lbn)
820 continue;
821 simple_lock(&bp->b_interlock);
822 if (bp->b_flags & B_BUSY) {
823 bp->b_flags |= B_WANTED;
824 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK,
825 "lfs_vtruncbuf", slptimeo, &bp->b_interlock);
826 if (error) {
827 splx(s);
828 return (error);
829 }
830 goto restart;
831 }
832 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH;
833 if (bp->b_flags & B_DELWRI) {
834 bp->b_flags &= ~B_DELWRI;
835 fs->lfs_avail += btofsb(fs, bp->b_bcount);
836 wakeup(&fs->lfs_avail);
837 }
838 LFS_UNLOCK_BUF(bp);
839 simple_unlock(&bp->b_interlock);
840 brelse(bp);
841 }
842
843 splx(s);
844
845 return (0);
846 }
847
848