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