ffs_inode.c revision 1.73 1 /* $NetBSD: ffs_inode.c,v 1.73 2005/09/12 16:24:41 christos 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.73 2005/09/12 16:24:41 christos 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(struct inode *, daddr_t, daddr_t, daddr_t, int,
64 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(void *v)
79 {
80 struct vop_update_args /* {
81 struct vnode *a_vp;
82 struct timespec *a_access;
83 struct timespec *a_modify;
84 int a_flags;
85 } */ *ap = v;
86 struct fs *fs;
87 struct buf *bp;
88 struct inode *ip;
89 int error;
90 caddr_t cp;
91 int waitfor, flags;
92
93 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
94 return (0);
95 ip = VTOI(ap->a_vp);
96 FFS_ITIMES(ip, ap->a_access, ap->a_modify, NULL);
97 if (ap->a_flags & UPDATE_CLOSE)
98 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
99 else
100 flags = ip->i_flag & IN_MODIFIED;
101 if (flags == 0)
102 return (0);
103 fs = ip->i_fs;
104
105 if ((flags & IN_MODIFIED) != 0 &&
106 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
107 waitfor = ap->a_flags & UPDATE_WAIT;
108 if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
109 waitfor |= UPDATE_WAIT;
110 } else
111 waitfor = 0;
112
113 /*
114 * Ensure that uid and gid are correct. This is a temporary
115 * fix until fsck has been changed to do the update.
116 */
117 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
118 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
119 ip->i_ffs1_ouid = ip->i_uid; /* XXX */
120 ip->i_ffs1_ogid = ip->i_gid; /* XXX */
121 } /* XXX */
122 error = bread(ip->i_devvp,
123 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
124 (int)fs->fs_bsize, NOCRED, &bp);
125 if (error) {
126 brelse(bp);
127 return (error);
128 }
129 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
130 if (DOINGSOFTDEP(ap->a_vp))
131 softdep_update_inodeblock(ip, bp, waitfor);
132 else if (ip->i_ffs_effnlink != ip->i_nlink)
133 panic("ffs_update: bad link cnt");
134 if (fs->fs_magic == FS_UFS1_MAGIC) {
135 cp = (caddr_t)bp->b_data +
136 (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
137 #ifdef FFS_EI
138 if (UFS_FSNEEDSWAP(fs))
139 ffs_dinode1_swap(ip->i_din.ffs1_din,
140 (struct ufs1_dinode *)cp);
141 else
142 #endif
143 memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
144 } else {
145 cp = (caddr_t)bp->b_data +
146 (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
147 #ifdef FFS_EI
148 if (UFS_FSNEEDSWAP(fs))
149 ffs_dinode2_swap(ip->i_din.ffs2_din,
150 (struct ufs2_dinode *)cp);
151 else
152 #endif
153 memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
154 }
155 if (waitfor) {
156 return (bwrite(bp));
157 } else {
158 bdwrite(bp);
159 return (0);
160 }
161 }
162
163 #define SINGLE 0 /* index of single indirect block */
164 #define DOUBLE 1 /* index of double indirect block */
165 #define TRIPLE 2 /* index of triple indirect block */
166 /*
167 * Truncate the inode oip to at most length size, freeing the
168 * disk blocks.
169 */
170 int
171 ffs_truncate(void *v)
172 {
173 struct vop_truncate_args /* {
174 struct vnode *a_vp;
175 off_t a_length;
176 int a_flags;
177 struct ucred *a_cred;
178 struct proc *a_p;
179 } */ *ap = v;
180 struct vnode *ovp = ap->a_vp;
181 struct genfs_node *gp = VTOG(ovp);
182 daddr_t lastblock;
183 struct inode *oip = VTOI(ovp);
184 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
185 daddr_t blks[NDADDR + NIADDR];
186 off_t length = ap->a_length;
187 struct fs *fs;
188 int offset, size, level;
189 int64_t count, blocksreleased = 0;
190 int i, ioflag, aflag, nblocks;
191 int error, allerror = 0;
192 off_t osize;
193 int sync;
194 struct ufsmount *ump = oip->i_ump;
195
196 if (length < 0)
197 return (EINVAL);
198
199 if (ovp->v_type == VLNK &&
200 (oip->i_size < ump->um_maxsymlinklen ||
201 (ump->um_maxsymlinklen == 0 && DIP(oip, blocks) == 0))) {
202 KDASSERT(length == 0);
203 memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
204 oip->i_size = 0;
205 DIP_ASSIGN(oip, size, 0);
206 oip->i_flag |= IN_CHANGE | IN_UPDATE;
207 return (VOP_UPDATE(ovp, NULL, NULL, 0));
208 }
209 if (oip->i_size == length) {
210 oip->i_flag |= IN_CHANGE | IN_UPDATE;
211 return (VOP_UPDATE(ovp, NULL, NULL, 0));
212 }
213 #ifdef QUOTA
214 if ((error = getinoquota(oip)) != 0)
215 return (error);
216 #endif
217 fs = oip->i_fs;
218 if (length > ump->um_maxfilesize)
219 return (EFBIG);
220
221 if ((oip->i_flags & SF_SNAPSHOT) != 0)
222 ffs_snapremove(ovp);
223
224 osize = oip->i_size;
225 ioflag = ap->a_flags;
226 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
227
228 /*
229 * Lengthen the size of the file. We must ensure that the
230 * last byte of the file is allocated. Since the smallest
231 * value of osize is 0, length will be at least 1.
232 */
233
234 if (osize < length) {
235 if (lblkno(fs, osize) < NDADDR &&
236 lblkno(fs, osize) != lblkno(fs, length) &&
237 blkroundup(fs, osize) != osize) {
238 off_t eob;
239
240 eob = blkroundup(fs, osize);
241 error = ufs_balloc_range(ovp, osize, eob - osize,
242 ap->a_cred, aflag);
243 if (error)
244 return error;
245 if (ioflag & IO_SYNC) {
246 ovp->v_size = eob;
247 simple_lock(&ovp->v_interlock);
248 VOP_PUTPAGES(ovp,
249 trunc_page(osize & fs->fs_bmask),
250 round_page(eob), PGO_CLEANIT | PGO_SYNCIO);
251 }
252 }
253 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
254 aflag);
255 if (error) {
256 (void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
257 ap->a_cred, ap->a_p);
258 return (error);
259 }
260 uvm_vnp_setsize(ovp, length);
261 oip->i_flag |= IN_CHANGE | IN_UPDATE;
262 KASSERT(ovp->v_size == oip->i_size);
263 return (VOP_UPDATE(ovp, NULL, NULL, 0));
264 }
265
266 /*
267 * When truncating a regular file down to a non-block-aligned size,
268 * we must zero the part of last block which is past the new EOF.
269 * We must synchronously flush the zeroed pages to disk
270 * since the new pages will be invalidated as soon as we
271 * inform the VM system of the new, smaller size.
272 * We must do this before acquiring the GLOCK, since fetching
273 * the pages will acquire the GLOCK internally.
274 * So there is a window where another thread could see a whole
275 * zeroed page past EOF, but that's life.
276 */
277
278 offset = blkoff(fs, length);
279 if (ovp->v_type == VREG && offset != 0 && osize > length) {
280 daddr_t lbn;
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 lbn = lblkno(fs, length);
288 size = blksize(fs, oip, lbn);
289 eoz = MIN(lblktosize(fs, lbn) + size, osize);
290 uvm_vnp_zerorange(ovp, length, eoz - length);
291 if (round_page(eoz) > round_page(length)) {
292 simple_lock(&ovp->v_interlock);
293 error = VOP_PUTPAGES(ovp, round_page(length),
294 round_page(eoz),
295 PGO_CLEANIT | PGO_DEACTIVATE |
296 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
297 if (error)
298 return error;
299 }
300 }
301
302 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
303
304 if (DOINGSOFTDEP(ovp)) {
305 if (length > 0) {
306 /*
307 * If a file is only partially truncated, then
308 * we have to clean up the data structures
309 * describing the allocation past the truncation
310 * point. Finding and deallocating those structures
311 * is a lot of work. Since partial truncation occurs
312 * rarely, we solve the problem by syncing the file
313 * so that it will have no data structures left.
314 */
315 if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
316 0, 0, ap->a_p)) != 0) {
317 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
318 return (error);
319 }
320 if (oip->i_flag & IN_SPACECOUNTED)
321 fs->fs_pendingblocks -= DIP(oip, blocks);
322 } else {
323 uvm_vnp_setsize(ovp, length);
324 #ifdef QUOTA
325 (void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
326 #endif
327 softdep_setup_freeblocks(oip, length, 0);
328 (void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
329 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
330 oip->i_flag |= IN_CHANGE | IN_UPDATE;
331 return (VOP_UPDATE(ovp, NULL, NULL, 0));
332 }
333 }
334 oip->i_size = length;
335 DIP_ASSIGN(oip, size, length);
336 uvm_vnp_setsize(ovp, length);
337 /*
338 * Calculate index into inode's block list of
339 * last direct and indirect blocks (if any)
340 * which we want to keep. Lastblock is -1 when
341 * the file is truncated to 0.
342 */
343 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
344 lastiblock[SINGLE] = lastblock - NDADDR;
345 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
346 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
347 nblocks = btodb(fs->fs_bsize);
348 /*
349 * Update file and block pointers on disk before we start freeing
350 * blocks. If we crash before free'ing blocks below, the blocks
351 * will be returned to the free list. lastiblock values are also
352 * normalized to -1 for calls to ffs_indirtrunc below.
353 */
354 sync = 0;
355 for (level = TRIPLE; level >= SINGLE; level--) {
356 blks[NDADDR + level] = DIP(oip, ib[level]);
357 if (lastiblock[level] < 0 && blks[NDADDR + level] != 0) {
358 sync = 1;
359 DIP_ASSIGN(oip, ib[level], 0);
360 lastiblock[level] = -1;
361 }
362 }
363 for (i = 0; i < NDADDR; i++) {
364 blks[i] = DIP(oip, db[i]);
365 if (i > lastblock && blks[i] != 0) {
366 sync = 1;
367 DIP_ASSIGN(oip, db[i], 0);
368 }
369 }
370 oip->i_flag |= IN_CHANGE | IN_UPDATE;
371 if (sync) {
372 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
373 if (error && !allerror)
374 allerror = error;
375 }
376
377 /*
378 * Having written the new inode to disk, save its new configuration
379 * and put back the old block pointers long enough to process them.
380 * Note that we save the new block configuration so we can check it
381 * when we are done.
382 */
383 for (i = 0; i < NDADDR; i++) {
384 bn = DIP(oip, db[i]);
385 DIP_ASSIGN(oip, db[i], blks[i]);
386 blks[i] = bn;
387 }
388 for (i = 0; i < NIADDR; i++) {
389 bn = DIP(oip, ib[i]);
390 DIP_ASSIGN(oip, ib[i], blks[NDADDR + i]);
391 blks[NDADDR + i] = bn;
392 }
393
394 oip->i_size = osize;
395 DIP_ASSIGN(oip, size, osize);
396 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
397 if (error && !allerror)
398 allerror = error;
399
400 /*
401 * Indirect blocks first.
402 */
403 indir_lbn[SINGLE] = -NDADDR;
404 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
405 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
406 for (level = TRIPLE; level >= SINGLE; level--) {
407 if (oip->i_ump->um_fstype == UFS1)
408 bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
409 else
410 bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
411 if (bn != 0) {
412 error = ffs_indirtrunc(oip, indir_lbn[level],
413 fsbtodb(fs, bn), lastiblock[level], level, &count);
414 if (error)
415 allerror = error;
416 blocksreleased += count;
417 if (lastiblock[level] < 0) {
418 DIP_ASSIGN(oip, ib[level], 0);
419 ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
420 oip->i_number);
421 blocksreleased += nblocks;
422 }
423 }
424 if (lastiblock[level] >= 0)
425 goto done;
426 }
427
428 /*
429 * All whole direct blocks or frags.
430 */
431 for (i = NDADDR - 1; i > lastblock; i--) {
432 long bsize;
433
434 if (oip->i_ump->um_fstype == UFS1)
435 bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
436 else
437 bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
438 if (bn == 0)
439 continue;
440 DIP_ASSIGN(oip, db[i], 0);
441 bsize = blksize(fs, oip, i);
442 ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
443 blocksreleased += btodb(bsize);
444 }
445 if (lastblock < 0)
446 goto done;
447
448 /*
449 * Finally, look for a change in size of the
450 * last direct block; release any frags.
451 */
452 if (oip->i_ump->um_fstype == UFS1)
453 bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
454 else
455 bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
456 if (bn != 0) {
457 long oldspace, newspace;
458
459 /*
460 * Calculate amount of space we're giving
461 * back as old block size minus new block size.
462 */
463 oldspace = blksize(fs, oip, lastblock);
464 oip->i_size = length;
465 DIP_ASSIGN(oip, size, length);
466 newspace = blksize(fs, oip, lastblock);
467 if (newspace == 0)
468 panic("itrunc: newspace");
469 if (oldspace - newspace > 0) {
470 /*
471 * Block number of space to be free'd is
472 * the old block # plus the number of frags
473 * required for the storage we're keeping.
474 */
475 bn += numfrags(fs, newspace);
476 ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
477 oip->i_number);
478 blocksreleased += btodb(oldspace - newspace);
479 }
480 }
481
482 done:
483 #ifdef DIAGNOSTIC
484 for (level = SINGLE; level <= TRIPLE; level++)
485 if (blks[NDADDR + level] != DIP(oip, ib[level]))
486 panic("itrunc1");
487 for (i = 0; i < NDADDR; i++)
488 if (blks[i] != DIP(oip, db[i]))
489 panic("itrunc2");
490 if (length == 0 &&
491 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
492 panic("itrunc3");
493 #endif /* DIAGNOSTIC */
494 /*
495 * Put back the real size.
496 */
497 oip->i_size = length;
498 DIP_ASSIGN(oip, size, length);
499 DIP_ADD(oip, blocks, -blocksreleased);
500 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
501 oip->i_flag |= IN_CHANGE;
502 #ifdef QUOTA
503 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
504 #endif
505 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
506 return (allerror);
507 }
508
509 /*
510 * Release blocks associated with the inode ip and stored in the indirect
511 * block bn. Blocks are free'd in LIFO order up to (but not including)
512 * lastbn. If level is greater than SINGLE, the block is an indirect block
513 * and recursive calls to indirtrunc must be used to cleanse other indirect
514 * blocks.
515 *
516 * NB: triple indirect blocks are untested.
517 */
518 static int
519 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
520 int level, int64_t *countp)
521 {
522 int i;
523 struct buf *bp;
524 struct fs *fs = ip->i_fs;
525 int32_t *bap1 = NULL;
526 int64_t *bap2 = NULL;
527 struct vnode *vp;
528 daddr_t nb, nlbn, last;
529 char *copy = NULL;
530 int64_t blkcount, factor, blocksreleased = 0;
531 int nblocks;
532 int error = 0, allerror = 0;
533 #ifdef FFS_EI
534 const int needswap = UFS_FSNEEDSWAP(fs);
535 #endif
536 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
537 ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
538 #define BAP_ASSIGN(ip, i, value) \
539 do { \
540 if ((ip)->i_ump->um_fstype == UFS1) \
541 bap1[i] = (value); \
542 else \
543 bap2[i] = (value); \
544 } while(0)
545
546 /*
547 * Calculate index in current block of last
548 * block to be kept. -1 indicates the entire
549 * block so we need not calculate the index.
550 */
551 factor = 1;
552 for (i = SINGLE; i < level; i++)
553 factor *= NINDIR(fs);
554 last = lastbn;
555 if (lastbn > 0)
556 last /= factor;
557 nblocks = btodb(fs->fs_bsize);
558 /*
559 * Get buffer of block pointers, zero those entries corresponding
560 * to blocks to be free'd, and update on disk copy first. Since
561 * double(triple) indirect before single(double) indirect, calls
562 * to bmap on these blocks will fail. However, we already have
563 * the on disk address, so we have to set the b_blkno field
564 * explicitly instead of letting bread do everything for us.
565 */
566 vp = ITOV(ip);
567 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
568 if (bp->b_flags & (B_DONE | B_DELWRI)) {
569 /* Braces must be here in case trace evaluates to nothing. */
570 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
571 } else {
572 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
573 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
574 bp->b_flags |= B_READ;
575 if (bp->b_bcount > bp->b_bufsize)
576 panic("ffs_indirtrunc: bad buffer size");
577 bp->b_blkno = dbn;
578 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
579 VOP_STRATEGY(vp, bp);
580 error = biowait(bp);
581 }
582 if (error) {
583 brelse(bp);
584 *countp = 0;
585 return (error);
586 }
587
588 if (ip->i_ump->um_fstype == UFS1)
589 bap1 = (int32_t *)bp->b_data;
590 else
591 bap2 = (int64_t *)bp->b_data;
592 if (lastbn >= 0) {
593 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
594 memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
595 for (i = last + 1; i < NINDIR(fs); i++)
596 BAP_ASSIGN(ip, i, 0);
597 error = bwrite(bp);
598 if (error)
599 allerror = error;
600 if (ip->i_ump->um_fstype == UFS1)
601 bap1 = (int32_t *)copy;
602 else
603 bap2 = (int64_t *)copy;
604 }
605
606 /*
607 * Recursively free totally unused blocks.
608 */
609 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
610 i--, nlbn += factor) {
611 nb = RBAP(ip, i);
612 if (nb == 0)
613 continue;
614 if (level > SINGLE) {
615 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
616 (daddr_t)-1, level - 1,
617 &blkcount);
618 if (error)
619 allerror = error;
620 blocksreleased += blkcount;
621 }
622 ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
623 blocksreleased += nblocks;
624 }
625
626 /*
627 * Recursively free last partial block.
628 */
629 if (level > SINGLE && lastbn >= 0) {
630 last = lastbn % factor;
631 nb = RBAP(ip, i);
632 if (nb != 0) {
633 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
634 last, level - 1, &blkcount);
635 if (error)
636 allerror = error;
637 blocksreleased += blkcount;
638 }
639 }
640
641 if (copy != NULL) {
642 FREE(copy, M_TEMP);
643 } else {
644 bp->b_flags |= B_INVAL;
645 brelse(bp);
646 }
647
648 *countp = blocksreleased;
649 return (allerror);
650 }
651