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