ffs_inode.c revision 1.63 1 /* $NetBSD: ffs_inode.c,v 1.63 2004/05/25 14:54:59 hannken Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.63 2004/05/25 14:54:59 hannken Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_ffs.h"
39 #include "opt_quota.h"
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/buf.h>
48 #include <sys/vnode.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/trace.h>
52 #include <sys/resourcevar.h>
53
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
58 #include <ufs/ufs/ufs_bswap.h>
59
60 #include <ufs/ffs/fs.h>
61 #include <ufs/ffs/ffs_extern.h>
62
63 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t,
64 daddr_t, int, int64_t *));
65
66 /*
67 * Update the access, modified, and inode change times as specified
68 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
69 * The IN_MODIFIED flag is used to specify that the inode needs to be
70 * updated but that the times have already been set. The access
71 * and modified times are taken from the second and third parameters;
72 * the inode change time is always taken from the current time. If
73 * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
74 * softupdates, then wait for the disk write of the inode to complete.
75 */
76
77 int
78 ffs_update(v)
79 void *v;
80 {
81 struct vop_update_args /* {
82 struct vnode *a_vp;
83 struct timespec *a_access;
84 struct timespec *a_modify;
85 int a_flags;
86 } */ *ap = v;
87 struct fs *fs;
88 struct buf *bp;
89 struct inode *ip;
90 int error;
91 struct timespec ts;
92 caddr_t cp;
93 int waitfor, flags;
94
95 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
96 return (0);
97 ip = VTOI(ap->a_vp);
98 TIMEVAL_TO_TIMESPEC(&time, &ts);
99 FFS_ITIMES(ip,
100 ap->a_access ? ap->a_access : &ts,
101 ap->a_modify ? ap->a_modify : &ts, &ts);
102 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
103 if (flags == 0)
104 return (0);
105 fs = ip->i_fs;
106
107 if ((flags & IN_MODIFIED) != 0 &&
108 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
109 waitfor = ap->a_flags & UPDATE_WAIT;
110 if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
111 waitfor |= UPDATE_WAIT;
112 } else
113 waitfor = 0;
114
115 /*
116 * Ensure that uid and gid are correct. This is a temporary
117 * fix until fsck has been changed to do the update.
118 */
119 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
120 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
121 ip->i_ffs1_ouid = ip->i_uid; /* XXX */
122 ip->i_ffs1_ogid = ip->i_gid; /* XXX */
123 } /* XXX */
124 error = bread(ip->i_devvp,
125 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
126 (int)fs->fs_bsize, NOCRED, &bp);
127 if (error) {
128 brelse(bp);
129 return (error);
130 }
131 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
132 if (DOINGSOFTDEP(ap->a_vp))
133 softdep_update_inodeblock(ip, bp, waitfor);
134 else if (ip->i_ffs_effnlink != ip->i_nlink)
135 panic("ffs_update: bad link cnt");
136 if (fs->fs_magic == FS_UFS1_MAGIC) {
137 cp = (caddr_t)bp->b_data +
138 (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
139 #ifdef FFS_EI
140 if (UFS_FSNEEDSWAP(fs))
141 ffs_dinode1_swap(ip->i_din.ffs1_din,
142 (struct ufs1_dinode *)cp);
143 else
144 #endif
145 memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
146 } else {
147 cp = (caddr_t)bp->b_data +
148 (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
149 #ifdef FFS_EI
150 if (UFS_FSNEEDSWAP(fs))
151 ffs_dinode2_swap(ip->i_din.ffs2_din,
152 (struct ufs2_dinode *)cp);
153 else
154 #endif
155 memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
156 }
157 if (waitfor) {
158 return (bwrite(bp));
159 } else {
160 bdwrite(bp);
161 return (0);
162 }
163 }
164
165 #define SINGLE 0 /* index of single indirect block */
166 #define DOUBLE 1 /* index of double indirect block */
167 #define TRIPLE 2 /* index of triple indirect block */
168 /*
169 * Truncate the inode oip to at most length size, freeing the
170 * disk blocks.
171 */
172 int
173 ffs_truncate(v)
174 void *v;
175 {
176 struct vop_truncate_args /* {
177 struct vnode *a_vp;
178 off_t a_length;
179 int a_flags;
180 struct ucred *a_cred;
181 struct proc *a_p;
182 } */ *ap = v;
183 struct vnode *ovp = ap->a_vp;
184 struct genfs_node *gp = VTOG(ovp);
185 daddr_t lastblock;
186 struct inode *oip;
187 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
188 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
189 off_t length = ap->a_length;
190 struct fs *fs;
191 int offset, size, level;
192 int64_t count, blocksreleased = 0;
193 int i, ioflag, aflag, nblocks;
194 int error, allerror = 0;
195 off_t osize;
196
197 if (length < 0)
198 return (EINVAL);
199 oip = VTOI(ovp);
200 if (ovp->v_type == VLNK &&
201 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen ||
202 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
203 DIP(oip, blocks) == 0))) {
204 KDASSERT(length == 0);
205 memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
206 oip->i_size = 0;
207 DIP_ASSIGN(oip, size, 0);
208 oip->i_flag |= IN_CHANGE | IN_UPDATE;
209 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
210 }
211 if (oip->i_size == length) {
212 oip->i_flag |= IN_CHANGE | IN_UPDATE;
213 return (VOP_UPDATE(ovp, NULL, NULL, 0));
214 }
215 #ifdef QUOTA
216 if ((error = getinoquota(oip)) != 0)
217 return (error);
218 #endif
219 fs = oip->i_fs;
220 if (length > fs->fs_maxfilesize)
221 return (EFBIG);
222
223 if ((oip->i_flags & SF_SNAPSHOT) != 0)
224 ffs_snapremove(ovp);
225
226 osize = oip->i_size;
227 ioflag = ap->a_flags;
228 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
229
230 /*
231 * Lengthen the size of the file. We must ensure that the
232 * last byte of the file is allocated. Since the smallest
233 * value of osize is 0, length will be at least 1.
234 */
235
236 if (osize < length) {
237 if (lblkno(fs, osize) < NDADDR &&
238 lblkno(fs, osize) != lblkno(fs, length) &&
239 blkroundup(fs, osize) != osize) {
240 error = ufs_balloc_range(ovp, osize,
241 blkroundup(fs, osize) - osize, ap->a_cred, aflag);
242 if (error) {
243 return error;
244 }
245 if (ioflag & IO_SYNC) {
246 ovp->v_size = blkroundup(fs, osize);
247 simple_lock(&ovp->v_interlock);
248 VOP_PUTPAGES(ovp,
249 trunc_page(osize & ~(fs->fs_bsize - 1)),
250 round_page(ovp->v_size),
251 PGO_CLEANIT | PGO_SYNCIO);
252 }
253 }
254 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
255 aflag);
256 if (error) {
257 (void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
258 ap->a_cred, ap->a_p);
259 return error;
260 }
261 uvm_vnp_setsize(ovp, length);
262 oip->i_flag |= IN_CHANGE | IN_UPDATE;
263 KASSERT(ovp->v_size == oip->i_size);
264 return (VOP_UPDATE(ovp, NULL, NULL, 1));
265 }
266
267 /*
268 * When truncating a regular file down to a non-block-aligned size,
269 * we must zero the part of last block which is past the new EOF.
270 * We must synchronously flush the zeroed pages to disk
271 * since the new pages will be invalidated as soon as we
272 * inform the VM system of the new, smaller size.
273 * We must do this before acquiring the GLOCK, since fetching
274 * the pages will acquire the GLOCK internally.
275 * So there is a window where another thread could see a whole
276 * zeroed page past EOF, but that's life.
277 */
278
279 offset = blkoff(fs, length);
280 if (ovp->v_type == VREG && length < osize && offset != 0) {
281 voff_t eoz;
282
283 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
284 aflag);
285 if (error) {
286 return error;
287 }
288 size = blksize(fs, oip, lblkno(fs, length));
289 eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
290 uvm_vnp_zerorange(ovp, length, eoz - length);
291 simple_lock(&ovp->v_interlock);
292 error = VOP_PUTPAGES(ovp, trunc_page(length), round_page(eoz),
293 PGO_CLEANIT | PGO_DEACTIVATE | PGO_SYNCIO);
294 if (error) {
295 return error;
296 }
297 }
298
299 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
300
301 if (DOINGSOFTDEP(ovp)) {
302 if (length > 0) {
303 /*
304 * If a file is only partially truncated, then
305 * we have to clean up the data structures
306 * describing the allocation past the truncation
307 * point. Finding and deallocating those structures
308 * is a lot of work. Since partial truncation occurs
309 * rarely, we solve the problem by syncing the file
310 * so that it will have no data structures left.
311 */
312 if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
313 0, 0, ap->a_p)) != 0) {
314 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
315 return (error);
316 }
317 if (oip->i_flag & IN_SPACECOUNTED)
318 fs->fs_pendingblocks -= DIP(oip, blocks);
319 } else {
320 uvm_vnp_setsize(ovp, length);
321 #ifdef QUOTA
322 (void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
323 #endif
324 softdep_setup_freeblocks(oip, length, 0);
325 (void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
326 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
327 oip->i_flag |= IN_CHANGE | IN_UPDATE;
328 return (VOP_UPDATE(ovp, NULL, NULL, 0));
329 }
330 }
331 oip->i_size = length;
332 DIP_ASSIGN(oip, size, length);
333 uvm_vnp_setsize(ovp, length);
334 /*
335 * Calculate index into inode's block list of
336 * last direct and indirect blocks (if any)
337 * which we want to keep. Lastblock is -1 when
338 * the file is truncated to 0.
339 */
340 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
341 lastiblock[SINGLE] = lastblock - NDADDR;
342 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
343 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
344 nblocks = btodb(fs->fs_bsize);
345 /*
346 * Update file and block pointers on disk before we start freeing
347 * blocks. If we crash before free'ing blocks below, the blocks
348 * will be returned to the free list. lastiblock values are also
349 * normalized to -1 for calls to ffs_indirtrunc below.
350 */
351 for (level = TRIPLE; level >= SINGLE; level--) {
352 oldblks[NDADDR + level] = DIP(oip, ib[level]);
353 if (lastiblock[level] < 0) {
354 DIP_ASSIGN(oip, ib[level], 0);
355 lastiblock[level] = -1;
356 }
357 }
358 for (i = 0; i < NDADDR; i++) {
359 oldblks[i] = DIP(oip, db[i]);
360 if (i > lastblock)
361 DIP_ASSIGN(oip, db[i], 0);
362 }
363 oip->i_flag |= IN_CHANGE | IN_UPDATE;
364 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
365 if (error && !allerror)
366 allerror = error;
367
368 /*
369 * Having written the new inode to disk, save its new configuration
370 * and put back the old block pointers long enough to process them.
371 * Note that we save the new block configuration so we can check it
372 * when we are done.
373 */
374 for (i = 0; i < NDADDR; i++) {
375 newblks[i] = DIP(oip, db[i]);
376 DIP_ASSIGN(oip, db[i], oldblks[i]);
377 }
378 for (i = 0; i < NIADDR; i++) {
379 newblks[NDADDR + i] = DIP(oip, ib[i]);
380 DIP_ASSIGN(oip, ib[i], oldblks[NDADDR + i]);
381 }
382
383 oip->i_size = osize;
384 DIP_ASSIGN(oip, size, osize);
385 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
386 if (error && !allerror)
387 allerror = error;
388
389 /*
390 * Indirect blocks first.
391 */
392 indir_lbn[SINGLE] = -NDADDR;
393 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
394 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
395 for (level = TRIPLE; level >= SINGLE; level--) {
396 if (oip->i_ump->um_fstype == UFS1)
397 bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
398 else
399 bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
400 if (bn != 0) {
401 error = ffs_indirtrunc(oip, indir_lbn[level],
402 fsbtodb(fs, bn), lastiblock[level], level, &count);
403 if (error)
404 allerror = error;
405 blocksreleased += count;
406 if (lastiblock[level] < 0) {
407 DIP_ASSIGN(oip, ib[level], 0);
408 ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
409 oip->i_number);
410 blocksreleased += nblocks;
411 }
412 }
413 if (lastiblock[level] >= 0)
414 goto done;
415 }
416
417 /*
418 * All whole direct blocks or frags.
419 */
420 for (i = NDADDR - 1; i > lastblock; i--) {
421 long bsize;
422
423 if (oip->i_ump->um_fstype == UFS1)
424 bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
425 else
426 bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
427 if (bn == 0)
428 continue;
429 DIP_ASSIGN(oip, db[i], 0);
430 bsize = blksize(fs, oip, i);
431 ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
432 blocksreleased += btodb(bsize);
433 }
434 if (lastblock < 0)
435 goto done;
436
437 /*
438 * Finally, look for a change in size of the
439 * last direct block; release any frags.
440 */
441 if (oip->i_ump->um_fstype == UFS1)
442 bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
443 else
444 bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
445 if (bn != 0) {
446 long oldspace, newspace;
447
448 /*
449 * Calculate amount of space we're giving
450 * back as old block size minus new block size.
451 */
452 oldspace = blksize(fs, oip, lastblock);
453 oip->i_size = length;
454 DIP_ASSIGN(oip, size, length);
455 newspace = blksize(fs, oip, lastblock);
456 if (newspace == 0)
457 panic("itrunc: newspace");
458 if (oldspace - newspace > 0) {
459 /*
460 * Block number of space to be free'd is
461 * the old block # plus the number of frags
462 * required for the storage we're keeping.
463 */
464 bn += numfrags(fs, newspace);
465 ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
466 oip->i_number);
467 blocksreleased += btodb(oldspace - newspace);
468 }
469 }
470
471 done:
472 #ifdef DIAGNOSTIC
473 for (level = SINGLE; level <= TRIPLE; level++)
474 if (newblks[NDADDR + level] != DIP(oip, ib[level]))
475 panic("itrunc1");
476 for (i = 0; i < NDADDR; i++)
477 if (newblks[i] != DIP(oip, db[i]))
478 panic("itrunc2");
479 if (length == 0 &&
480 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
481 panic("itrunc3");
482 #endif /* DIAGNOSTIC */
483 /*
484 * Put back the real size.
485 */
486 oip->i_size = length;
487 DIP_ASSIGN(oip, size, length);
488 DIP_ADD(oip, blocks, -blocksreleased);
489 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
490 oip->i_flag |= IN_CHANGE;
491 #ifdef QUOTA
492 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
493 #endif
494 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
495 return (allerror);
496 }
497
498 /*
499 * Release blocks associated with the inode ip and stored in the indirect
500 * block bn. Blocks are free'd in LIFO order up to (but not including)
501 * lastbn. If level is greater than SINGLE, the block is an indirect block
502 * and recursive calls to indirtrunc must be used to cleanse other indirect
503 * blocks.
504 *
505 * NB: triple indirect blocks are untested.
506 */
507 static int
508 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
509 struct inode *ip;
510 daddr_t lbn, lastbn;
511 daddr_t dbn;
512 int level;
513 int64_t *countp;
514 {
515 int i;
516 struct buf *bp;
517 struct fs *fs = ip->i_fs;
518 int32_t *bap1 = NULL;
519 int64_t *bap2 = NULL;
520 struct vnode *vp;
521 daddr_t nb, nlbn, last;
522 char *copy = NULL;
523 int64_t blkcount, factor, blocksreleased = 0;
524 int nblocks;
525 int error = 0, allerror = 0;
526 #ifdef FFS_EI
527 const int needswap = UFS_FSNEEDSWAP(fs);
528 #endif
529 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
530 ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
531 #define BAP_ASSIGN(ip, i, value) \
532 do { \
533 if ((ip)->i_ump->um_fstype == UFS1) \
534 bap1[i] = (value); \
535 else \
536 bap2[i] = (value); \
537 } while(0)
538
539 /*
540 * Calculate index in current block of last
541 * block to be kept. -1 indicates the entire
542 * block so we need not calculate the index.
543 */
544 factor = 1;
545 for (i = SINGLE; i < level; i++)
546 factor *= NINDIR(fs);
547 last = lastbn;
548 if (lastbn > 0)
549 last /= factor;
550 nblocks = btodb(fs->fs_bsize);
551 /*
552 * Get buffer of block pointers, zero those entries corresponding
553 * to blocks to be free'd, and update on disk copy first. Since
554 * double(triple) indirect before single(double) indirect, calls
555 * to bmap on these blocks will fail. However, we already have
556 * the on disk address, so we have to set the b_blkno field
557 * explicitly instead of letting bread do everything for us.
558 */
559 vp = ITOV(ip);
560 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
561 if (bp->b_flags & (B_DONE | B_DELWRI)) {
562 /* Braces must be here in case trace evaluates to nothing. */
563 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
564 } else {
565 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
566 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
567 bp->b_flags |= B_READ;
568 if (bp->b_bcount > bp->b_bufsize)
569 panic("ffs_indirtrunc: bad buffer size");
570 bp->b_blkno = dbn;
571 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
572 VOP_STRATEGY(vp, bp);
573 error = biowait(bp);
574 }
575 if (error) {
576 brelse(bp);
577 *countp = 0;
578 return (error);
579 }
580
581 if (ip->i_ump->um_fstype == UFS1)
582 bap1 = (int32_t *)bp->b_data;
583 else
584 bap2 = (int64_t *)bp->b_data;
585 if (lastbn >= 0) {
586 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
587 memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
588 for (i = last + 1; i < NINDIR(fs); i++)
589 BAP_ASSIGN(ip, i, 0);
590 error = bwrite(bp);
591 if (error)
592 allerror = error;
593 if (ip->i_ump->um_fstype == UFS1)
594 bap1 = (int32_t *)copy;
595 else
596 bap2 = (int64_t *)copy;
597 }
598
599 /*
600 * Recursively free totally unused blocks.
601 */
602 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
603 i--, nlbn += factor) {
604 nb = RBAP(ip, i);
605 if (nb == 0)
606 continue;
607 if (level > SINGLE) {
608 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
609 (daddr_t)-1, level - 1,
610 &blkcount);
611 if (error)
612 allerror = error;
613 blocksreleased += blkcount;
614 }
615 ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
616 blocksreleased += nblocks;
617 }
618
619 /*
620 * Recursively free last partial block.
621 */
622 if (level > SINGLE && lastbn >= 0) {
623 last = lastbn % factor;
624 nb = RBAP(ip, i);
625 if (nb != 0) {
626 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
627 last, level - 1, &blkcount);
628 if (error)
629 allerror = error;
630 blocksreleased += blkcount;
631 }
632 }
633
634 if (copy != NULL) {
635 FREE(copy, M_TEMP);
636 } else {
637 bp->b_flags |= B_INVAL;
638 brelse(bp);
639 }
640
641 *countp = blocksreleased;
642 return (allerror);
643 }
644