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