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