lfs_inode.c revision 1.148 1 /* $NetBSD: lfs_inode.c,v 1.148 2017/03/13 13:45:53 riastradh 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.148 2017/03/13 13:45:53 riastradh 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 /* note: newblks is set but only actually used if DIAGNOSTIC */
208 daddr_t newblks[ULFS_NDADDR + ULFS_NIADDR] __diagused;
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 lfs_dino_getblocks(fs, oip->i_din) == 0))) {
246 KASSERTMSG((length == 0),
247 "partial truncate of symlink: %jd", (intmax_t)length);
248 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size);
249 oip->i_size = 0;
250 lfs_dino_setsize(fs, oip->i_din, 0);
251 oip->i_flag |= IN_CHANGE | IN_UPDATE;
252 return (lfs_update(ovp, NULL, NULL, 0));
253 }
254 if (oip->i_size == length) {
255 oip->i_flag |= IN_CHANGE | IN_UPDATE;
256 return (lfs_update(ovp, NULL, NULL, 0));
257 }
258 lfs_imtime(fs);
259 osize = oip->i_size;
260 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode);
261
262 ASSERT_NO_SEGLOCK(fs);
263 /*
264 * Lengthen the size of the file. We must ensure that the
265 * last byte of the file is allocated. Since the smallest
266 * value of osize is 0, length will be at least 1.
267 */
268 if (osize < length) {
269 if (length > fs->um_maxfilesize)
270 return (EFBIG);
271 aflags = B_CLRBUF;
272 if (ioflag & IO_SYNC)
273 aflags |= B_SYNC;
274 if (usepc) {
275 if (lfs_lblkno(fs, osize) < ULFS_NDADDR &&
276 lfs_lblkno(fs, osize) != lfs_lblkno(fs, length) &&
277 lfs_blkroundup(fs, osize) != osize) {
278 off_t eob;
279
280 eob = lfs_blkroundup(fs, osize);
281 uvm_vnp_setwritesize(ovp, eob);
282 error = ulfs_balloc_range(ovp, osize,
283 eob - osize, cred, aflags);
284 if (error) {
285 (void) lfs_truncate(ovp, osize,
286 ioflag & IO_SYNC, cred);
287 return error;
288 }
289 if (ioflag & IO_SYNC) {
290 mutex_enter(ovp->v_interlock);
291 VOP_PUTPAGES(ovp,
292 trunc_page(osize & lfs_sb_getbmask(fs)),
293 round_page(eob),
294 PGO_CLEANIT | PGO_SYNCIO);
295 }
296 }
297 uvm_vnp_setwritesize(ovp, length);
298 error = ulfs_balloc_range(ovp, length - 1, 1, cred,
299 aflags);
300 if (error) {
301 (void) lfs_truncate(ovp, osize,
302 ioflag & IO_SYNC, cred);
303 return error;
304 }
305 uvm_vnp_setsize(ovp, length);
306 oip->i_flag |= IN_CHANGE | IN_UPDATE;
307 KASSERT(ovp->v_size == oip->i_size);
308 oip->i_lfs_hiblk = lfs_lblkno(fs, oip->i_size + lfs_sb_getbsize(fs) - 1) - 1;
309 return (lfs_update(ovp, NULL, NULL, 0));
310 } else {
311 error = lfs_reserve(fs, ovp, NULL,
312 lfs_btofsb(fs, (ULFS_NIADDR + 2) << lfs_sb_getbshift(fs)));
313 if (error)
314 return (error);
315 error = lfs_balloc(ovp, length - 1, 1, cred,
316 aflags, &bp);
317 lfs_reserve(fs, ovp, NULL,
318 -lfs_btofsb(fs, (ULFS_NIADDR + 2) << lfs_sb_getbshift(fs)));
319 if (error)
320 return (error);
321 oip->i_size = length;
322 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
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 = length;
351 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
352 } else if (!usepc) {
353 lbn = lfs_lblkno(fs, length);
354 aflags = B_CLRBUF;
355 if (ioflag & IO_SYNC)
356 aflags |= B_SYNC;
357 error = lfs_balloc(ovp, length - 1, 1, cred, aflags, &bp);
358 if (error) {
359 lfs_reserve(fs, ovp, NULL,
360 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
361 goto errout;
362 }
363 obufsize = bp->b_bufsize;
364 odb = lfs_btofsb(fs, bp->b_bcount);
365 oip->i_size = length;
366 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
367 size = lfs_blksize(fs, oip, lbn);
368 if (ovp->v_type != VDIR)
369 memset((char *)bp->b_data + offset, 0,
370 (u_int)(size - offset));
371 allocbuf(bp, size, 1);
372 if ((bp->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {
373 mutex_enter(&lfs_lock);
374 locked_queue_bytes -= obufsize - bp->b_bufsize;
375 mutex_exit(&lfs_lock);
376 }
377 if (bp->b_oflags & BO_DELWRI) {
378 lfs_sb_addavail(fs, odb - lfs_btofsb(fs, size));
379 /* XXX shouldn't this wake up on lfs_availsleep? */
380 }
381 (void) VOP_BWRITE(bp->b_vp, bp);
382 } else { /* vp->v_type == VREG && length < osize && offset != 0 */
383 /*
384 * When truncating a regular file down to a non-block-aligned
385 * size, we must zero the part of last block which is past
386 * the new EOF. We must synchronously flush the zeroed pages
387 * to disk since the new pages will be invalidated as soon
388 * as we inform the VM system of the new, smaller size.
389 * We must do this before acquiring the GLOCK, since fetching
390 * the pages will acquire the GLOCK internally.
391 * So there is a window where another thread could see a whole
392 * zeroed page past EOF, but that's life.
393 */
394 daddr_t xlbn;
395 voff_t eoz;
396
397 aflags = ioflag & IO_SYNC ? B_SYNC : 0;
398 error = ulfs_balloc_range(ovp, length - 1, 1, cred, aflags);
399 if (error) {
400 lfs_reserve(fs, ovp, NULL,
401 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
402 goto errout;
403 }
404 xlbn = lfs_lblkno(fs, length);
405 size = lfs_blksize(fs, oip, xlbn);
406 eoz = MIN(lfs_lblktosize(fs, xlbn) + size, osize);
407 ubc_zerorange(&ovp->v_uobj, length, eoz - length,
408 UBC_UNMAP_FLAG(ovp));
409 if (round_page(eoz) > round_page(length)) {
410 mutex_enter(ovp->v_interlock);
411 error = VOP_PUTPAGES(ovp, round_page(length),
412 round_page(eoz),
413 PGO_CLEANIT | PGO_DEACTIVATE |
414 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
415 if (error) {
416 lfs_reserve(fs, ovp, NULL,
417 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
418 goto errout;
419 }
420 }
421 }
422
423 genfs_node_wrlock(ovp);
424
425 oip->i_size = length;
426 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
427 uvm_vnp_setsize(ovp, length);
428
429 /*
430 * Calculate index into inode's block list of
431 * last direct and indirect blocks (if any)
432 * which we want to keep. Lastblock is -1 when
433 * the file is truncated to 0.
434 */
435 /* Avoid sign overflow - XXX assumes that off_t is a quad_t. */
436 if (length > QUAD_MAX - lfs_sb_getbsize(fs))
437 lastblock = lfs_lblkno(fs, QUAD_MAX - lfs_sb_getbsize(fs));
438 else
439 lastblock = lfs_lblkno(fs, length + lfs_sb_getbsize(fs) - 1) - 1;
440 lastiblock[SINGLE] = lastblock - ULFS_NDADDR;
441 lastiblock[DOUBLE] = lastiblock[SINGLE] - LFS_NINDIR(fs);
442 lastiblock[TRIPLE] = lastiblock[DOUBLE] - LFS_NINDIR(fs) * LFS_NINDIR(fs);
443 nblocks = lfs_btofsb(fs, lfs_sb_getbsize(fs));
444 /*
445 * Record changed file and block pointers before we start
446 * freeing blocks. lastiblock values are also normalized to -1
447 * for calls to lfs_indirtrunc below.
448 */
449 for (i=0; i<ULFS_NDADDR; i++) {
450 newblks[i] = lfs_dino_getdb(fs, oip->i_din, i);
451 }
452 for (i=0; i<ULFS_NIADDR; i++) {
453 newblks[ULFS_NDADDR + i] = lfs_dino_getib(fs, oip->i_din, i);
454 }
455 for (level = TRIPLE; level >= SINGLE; level--)
456 if (lastiblock[level] < 0) {
457 newblks[ULFS_NDADDR+level] = 0;
458 lastiblock[level] = -1;
459 }
460 for (i = ULFS_NDADDR - 1; i > lastblock; i--)
461 newblks[i] = 0;
462
463 oip->i_size = osize;
464 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
465 error = lfs_vtruncbuf(ovp, lastblock + 1, false, 0);
466 if (error && !allerror)
467 allerror = error;
468
469 /*
470 * Indirect blocks first.
471 */
472 indir_lbn[SINGLE] = -ULFS_NDADDR;
473 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - LFS_NINDIR(fs) - 1;
474 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - LFS_NINDIR(fs) * LFS_NINDIR(fs) - 1;
475 for (level = TRIPLE; level >= SINGLE; level--) {
476 bn = lfs_dino_getib(fs, oip->i_din, level);
477 if (bn != 0) {
478 error = lfs_indirtrunc(oip, indir_lbn[level],
479 bn, lastiblock[level],
480 level, &count, &rcount,
481 &lastseg, &bc);
482 if (error)
483 allerror = error;
484 real_released += rcount;
485 blocksreleased += count;
486 if (lastiblock[level] < 0) {
487 if (lfs_dino_getib(fs, oip->i_din, level) > 0)
488 real_released += nblocks;
489 blocksreleased += nblocks;
490 lfs_dino_setib(fs, oip->i_din, level, 0);
491 lfs_blkfree(fs, oip, bn, lfs_sb_getbsize(fs),
492 &lastseg, &bc);
493 lfs_deregister_block(ovp, bn);
494 }
495 }
496 if (lastiblock[level] >= 0)
497 goto done;
498 }
499
500 /*
501 * All whole direct blocks or frags.
502 */
503 for (i = ULFS_NDADDR - 1; i > lastblock; i--) {
504 long bsize, obsize;
505
506 bn = lfs_dino_getdb(fs, oip->i_din, i);
507 if (bn == 0)
508 continue;
509 bsize = lfs_blksize(fs, oip, i);
510 if (lfs_dino_getdb(fs, oip->i_din, i) > 0) {
511 /* Check for fragment size changes */
512 obsize = oip->i_lfs_fragsize[i];
513 real_released += lfs_btofsb(fs, obsize);
514 oip->i_lfs_fragsize[i] = 0;
515 } else
516 obsize = 0;
517 blocksreleased += lfs_btofsb(fs, bsize);
518 lfs_dino_setdb(fs, oip->i_din, i, 0);
519 lfs_blkfree(fs, oip, bn, obsize, &lastseg, &bc);
520 lfs_deregister_block(ovp, bn);
521 }
522 if (lastblock < 0)
523 goto done;
524
525 /*
526 * Finally, look for a change in size of the
527 * last direct block; release any frags.
528 */
529 bn = lfs_dino_getdb(fs, oip->i_din, lastblock);
530 if (bn != 0) {
531 long oldspace, newspace;
532 #if 0
533 long olddspace;
534 #endif
535
536 /*
537 * Calculate amount of space we're giving
538 * back as old block size minus new block size.
539 */
540 oldspace = lfs_blksize(fs, oip, lastblock);
541 #if 0
542 olddspace = oip->i_lfs_fragsize[lastblock];
543 #endif
544
545 oip->i_size = length;
546 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
547 newspace = lfs_blksize(fs, oip, lastblock);
548 if (newspace == 0)
549 panic("itrunc: newspace");
550 if (oldspace - newspace > 0) {
551 blocksreleased += lfs_btofsb(fs, oldspace - newspace);
552 }
553 #if 0
554 if (bn > 0 && olddspace - newspace > 0) {
555 /* No segment accounting here, just vnode */
556 real_released += lfs_btofsb(fs, olddspace - newspace);
557 }
558 #endif
559 }
560
561 done:
562 /* Finish segment accounting corrections */
563 lfs_update_seguse(fs, oip, lastseg, bc);
564 #ifdef DIAGNOSTIC
565 for (level = SINGLE; level <= TRIPLE; level++)
566 if ((newblks[ULFS_NDADDR + level] == 0) !=
567 (lfs_dino_getib(fs, oip->i_din, level) == 0)) {
568 panic("lfs itrunc1");
569 }
570 for (i = 0; i < ULFS_NDADDR; i++)
571 if ((newblks[i] == 0) !=
572 (lfs_dino_getdb(fs, oip->i_din, i) == 0)) {
573 panic("lfs itrunc2");
574 }
575 if (length == 0 &&
576 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
577 panic("lfs itrunc3");
578 #endif /* DIAGNOSTIC */
579 /*
580 * Put back the real size.
581 */
582 oip->i_size = length;
583 lfs_dino_setsize(fs, oip->i_din, oip->i_size);
584 oip->i_lfs_effnblks -= blocksreleased;
585 lfs_dino_setblocks(fs, oip->i_din,
586 lfs_dino_getblocks(fs, oip->i_din) - real_released);
587 mutex_enter(&lfs_lock);
588 lfs_sb_addbfree(fs, blocksreleased);
589 mutex_exit(&lfs_lock);
590
591 KASSERTMSG((oip->i_size != 0 ||
592 lfs_dino_getblocks(fs, oip->i_din) == 0),
593 "truncate to 0 but %jd blks/%jd effblks",
594 lfs_dino_getblocks(fs, oip->i_din),
595 oip->i_lfs_effnblks);
596 KASSERTMSG((oip->i_size != 0 || oip->i_lfs_effnblks == 0),
597 "truncate to 0 but %jd blks/%jd effblks",
598 lfs_dino_getblocks(fs, oip->i_din),
599 oip->i_lfs_effnblks);
600
601 /*
602 * If we truncated to zero, take us off the paging queue.
603 */
604 mutex_enter(&lfs_lock);
605 if (oip->i_size == 0 && oip->i_flags & IN_PAGING) {
606 oip->i_flags &= ~IN_PAGING;
607 TAILQ_REMOVE(&fs->lfs_pchainhd, oip, i_lfs_pchain);
608 }
609 mutex_exit(&lfs_lock);
610
611 oip->i_flag |= IN_CHANGE;
612 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
613 (void) lfs_chkdq(oip, -blocksreleased, NOCRED, 0);
614 #endif
615 lfs_reserve(fs, ovp, NULL,
616 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(fs)));
617 genfs_node_unlock(ovp);
618 errout:
619 oip->i_lfs_hiblk = lfs_lblkno(fs, oip->i_size + lfs_sb_getbsize(fs) - 1) - 1;
620 if (ovp != fs->lfs_ivnode)
621 lfs_segunlock(fs);
622 return (allerror ? allerror : error);
623 }
624
625 /* Update segment and avail usage information when removing a block. */
626 static int
627 lfs_blkfree(struct lfs *fs, struct inode *ip, daddr_t daddr,
628 size_t bsize, long *lastseg, size_t *num)
629 {
630 long seg;
631 int error = 0;
632
633 ASSERT_SEGLOCK(fs);
634 bsize = lfs_fragroundup(fs, bsize);
635 if (daddr > 0) {
636 if (*lastseg != (seg = lfs_dtosn(fs, daddr))) {
637 error = lfs_update_seguse(fs, ip, *lastseg, *num);
638 *num = bsize;
639 *lastseg = seg;
640 } else
641 *num += bsize;
642 }
643
644 return error;
645 }
646
647 /* Finish the accounting updates for a segment. */
648 static int
649 lfs_update_seguse(struct lfs *fs, struct inode *ip, long lastseg, size_t num)
650 {
651 struct segdelta *sd;
652
653 ASSERT_SEGLOCK(fs);
654 if (lastseg < 0 || num == 0)
655 return 0;
656
657 LIST_FOREACH(sd, &ip->i_lfs_segdhd, list)
658 if (sd->segnum == lastseg)
659 break;
660 if (sd == NULL) {
661 sd = malloc(sizeof(*sd), M_SEGMENT, M_WAITOK);
662 sd->segnum = lastseg;
663 sd->num = 0;
664 LIST_INSERT_HEAD(&ip->i_lfs_segdhd, sd, list);
665 }
666 sd->num += num;
667
668 return 0;
669 }
670
671 static void
672 lfs_finalize_seguse(struct lfs *fs, void *v)
673 {
674 SEGUSE *sup;
675 struct buf *bp;
676 struct segdelta *sd;
677 LIST_HEAD(, segdelta) *hd = v;
678
679 ASSERT_SEGLOCK(fs);
680 while((sd = LIST_FIRST(hd)) != NULL) {
681 LIST_REMOVE(sd, list);
682 LFS_SEGENTRY(sup, fs, sd->segnum, bp);
683 if (sd->num > sup->su_nbytes) {
684 printf("lfs_finalize_seguse: segment %ld short by %ld\n",
685 sd->segnum, (long)(sd->num - sup->su_nbytes));
686 panic("lfs_finalize_seguse: negative bytes");
687 sup->su_nbytes = sd->num;
688 }
689 sup->su_nbytes -= sd->num;
690 LFS_WRITESEGENTRY(sup, fs, sd->segnum, bp);
691 free(sd, M_SEGMENT);
692 }
693 }
694
695 /* Finish the accounting updates for a segment. */
696 void
697 lfs_finalize_ino_seguse(struct lfs *fs, struct inode *ip)
698 {
699 ASSERT_SEGLOCK(fs);
700 lfs_finalize_seguse(fs, &ip->i_lfs_segdhd);
701 }
702
703 /* Finish the accounting updates for a segment. */
704 void
705 lfs_finalize_fs_seguse(struct lfs *fs)
706 {
707 ASSERT_SEGLOCK(fs);
708 lfs_finalize_seguse(fs, &fs->lfs_segdhd);
709 }
710
711 /*
712 * Release blocks associated with the inode ip and stored in the indirect
713 * block bn. Blocks are free'd in LIFO order up to (but not including)
714 * lastbn. If level is greater than SINGLE, the block is an indirect block
715 * and recursive calls to indirtrunc must be used to cleanse other indirect
716 * blocks.
717 *
718 * NB: triple indirect blocks are untested.
719 */
720 static int
721 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
722 daddr_t lastbn, int level, daddr_t *countp,
723 daddr_t *rcountp, long *lastsegp, size_t *bcp)
724 {
725 int i;
726 struct buf *bp;
727 struct lfs *fs = ip->i_lfs;
728 void *bap;
729 bool bap_needs_free;
730 struct vnode *vp;
731 daddr_t nb, nlbn, last;
732 daddr_t blkcount, rblkcount, factor;
733 int nblocks;
734 daddr_t blocksreleased = 0, real_released = 0;
735 int error = 0, allerror = 0;
736
737 ASSERT_SEGLOCK(fs);
738 /*
739 * Calculate index in current block of last
740 * block to be kept. -1 indicates the entire
741 * block so we need not calculate the index.
742 */
743 factor = 1;
744 for (i = SINGLE; i < level; i++)
745 factor *= LFS_NINDIR(fs);
746 last = lastbn;
747 if (lastbn > 0)
748 last /= factor;
749 nblocks = lfs_btofsb(fs, lfs_sb_getbsize(fs));
750 /*
751 * Get buffer of block pointers, zero those entries corresponding
752 * to blocks to be free'd, and update on disk copy first. Since
753 * double(triple) indirect before single(double) indirect, calls
754 * to bmap on these blocks will fail. However, we already have
755 * the on disk address, so we have to set the b_blkno field
756 * explicitly instead of letting bread do everything for us.
757 */
758 vp = ITOV(ip);
759 bp = getblk(vp, lbn, lfs_sb_getbsize(fs), 0, 0);
760 if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
761 /* Braces must be here in case trace evaluates to nothing. */
762 trace(TR_BREADHIT, pack(vp, lfs_sb_getbsize(fs)), lbn);
763 } else {
764 trace(TR_BREADMISS, pack(vp, lfs_sb_getbsize(fs)), lbn);
765 curlwp->l_ru.ru_inblock++; /* pay for read */
766 bp->b_flags |= B_READ;
767 if (bp->b_bcount > bp->b_bufsize)
768 panic("lfs_indirtrunc: bad buffer size");
769 bp->b_blkno = LFS_FSBTODB(fs, dbn);
770 VOP_STRATEGY(vp, bp);
771 error = biowait(bp);
772 }
773 if (error) {
774 brelse(bp, 0);
775 *countp = *rcountp = 0;
776 return (error);
777 }
778
779 if (lastbn >= 0) {
780 /*
781 * We still need this block, so copy the data for
782 * subsequent processing; then in the original block,
783 * zero out the dying block pointers and send it off.
784 */
785 bap = lfs_malloc(fs, lfs_sb_getbsize(fs), LFS_NB_IBLOCK);
786 memcpy(bap, bp->b_data, lfs_sb_getbsize(fs));
787 bap_needs_free = true;
788
789 for (i = last + 1; i < LFS_NINDIR(fs); i++) {
790 lfs_iblock_set(fs, bp->b_data, i, 0);
791 }
792 error = VOP_BWRITE(bp->b_vp, bp);
793 if (error)
794 allerror = error;
795 } else {
796 bap = bp->b_data;
797 bap_needs_free = false;
798 }
799
800 /*
801 * Recursively free totally unused blocks.
802 */
803 for (i = LFS_NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
804 i--, nlbn += factor) {
805 nb = lfs_iblock_get(fs, bap, i);
806 if (nb == 0)
807 continue;
808 if (level > SINGLE) {
809 error = lfs_indirtrunc(ip, nlbn, nb,
810 (daddr_t)-1, level - 1,
811 &blkcount, &rblkcount,
812 lastsegp, bcp);
813 if (error)
814 allerror = error;
815 blocksreleased += blkcount;
816 real_released += rblkcount;
817 }
818 lfs_blkfree(fs, ip, nb, lfs_sb_getbsize(fs), lastsegp, bcp);
819 if (lfs_iblock_get(fs, bap, i) > 0)
820 real_released += nblocks;
821 blocksreleased += nblocks;
822 }
823
824 /*
825 * Recursively free last partial block.
826 */
827 if (level > SINGLE && lastbn >= 0) {
828 last = lastbn % factor;
829 nb = lfs_iblock_get(fs, bap, i);
830 if (nb != 0) {
831 error = lfs_indirtrunc(ip, nlbn, nb,
832 last, level - 1, &blkcount,
833 &rblkcount, lastsegp, bcp);
834 if (error)
835 allerror = error;
836 real_released += rblkcount;
837 blocksreleased += blkcount;
838 }
839 }
840
841 if (bap_needs_free) {
842 lfs_free(fs, bap, LFS_NB_IBLOCK);
843 } else {
844 mutex_enter(&bufcache_lock);
845 if (bp->b_oflags & BO_DELWRI) {
846 LFS_UNLOCK_BUF(bp);
847 lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
848 wakeup(&fs->lfs_availsleep);
849 }
850 brelsel(bp, BC_INVAL);
851 mutex_exit(&bufcache_lock);
852 }
853
854 *countp = blocksreleased;
855 *rcountp = real_released;
856 return (allerror);
857 }
858
859 /*
860 * Destroy any in core blocks past the truncation length.
861 * Inlined from vtruncbuf, so that lfs_avail could be updated.
862 * We take the seglock to prevent cleaning from occurring while we are
863 * invalidating blocks.
864 */
865 static int
866 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, bool catch, int slptimeo)
867 {
868 struct buf *bp, *nbp;
869 int error;
870 struct lfs *fs;
871 voff_t off;
872
873 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
874 mutex_enter(vp->v_interlock);
875 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
876 if (error)
877 return error;
878
879 fs = VTOI(vp)->i_lfs;
880
881 ASSERT_SEGLOCK(fs);
882
883 mutex_enter(&bufcache_lock);
884 restart:
885 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
886 nbp = LIST_NEXT(bp, b_vnbufs);
887 if (bp->b_lblkno < lbn)
888 continue;
889 error = bbusy(bp, catch, slptimeo, NULL);
890 if (error == EPASSTHROUGH)
891 goto restart;
892 if (error != 0) {
893 mutex_exit(&bufcache_lock);
894 return (error);
895 }
896 mutex_enter(bp->b_objlock);
897 if (bp->b_oflags & BO_DELWRI) {
898 bp->b_oflags &= ~BO_DELWRI;
899 lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
900 wakeup(&fs->lfs_availsleep);
901 }
902 mutex_exit(bp->b_objlock);
903 LFS_UNLOCK_BUF(bp);
904 brelsel(bp, BC_INVAL | BC_VFLUSH);
905 }
906
907 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
908 nbp = LIST_NEXT(bp, b_vnbufs);
909 if (bp->b_lblkno < lbn)
910 continue;
911 error = bbusy(bp, catch, slptimeo, NULL);
912 if (error == EPASSTHROUGH)
913 goto restart;
914 if (error != 0) {
915 mutex_exit(&bufcache_lock);
916 return (error);
917 }
918 mutex_enter(bp->b_objlock);
919 if (bp->b_oflags & BO_DELWRI) {
920 bp->b_oflags &= ~BO_DELWRI;
921 lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
922 wakeup(&fs->lfs_availsleep);
923 }
924 mutex_exit(bp->b_objlock);
925 LFS_UNLOCK_BUF(bp);
926 brelsel(bp, BC_INVAL | BC_VFLUSH);
927 }
928 mutex_exit(&bufcache_lock);
929
930 return (0);
931 }
932
933