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