]> git.saurik.com Git - apple/xnu.git/blame - bsd/ufs/ffs/ffs_inode.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / ufs / ffs / ffs_inode.c
CommitLineData
1c79356b 1/*
8ad349bb 2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
1c79356b 3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31/*
32 * Copyright (c) 1982, 1986, 1989, 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. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
64 */
65
66#include <rev_endian_fs.h>
67#include <vm/vm_pager.h>
68
69#include <sys/param.h>
70#include <sys/systm.h>
91447636
A
71#include <sys/mount_internal.h>
72#include <sys/proc_internal.h> /* for accessing p_stats */
1c79356b 73#include <sys/file.h>
91447636
A
74#include <sys/buf_internal.h>
75#include <sys/vnode_internal.h>
1c79356b
A
76#include <sys/kernel.h>
77#include <sys/malloc.h>
78#include <sys/trace.h>
79#include <sys/resourcevar.h>
80#include <sys/ubc.h>
9bccf70c 81#include <sys/quota.h>
1c79356b
A
82
83#include <sys/vm.h>
84
85#include <ufs/ufs/quota.h>
86#include <ufs/ufs/inode.h>
87#include <ufs/ufs/ufsmount.h>
88#include <ufs/ufs/ufs_extern.h>
89
90#include <ufs/ffs/fs.h>
91#include <ufs/ffs/ffs_extern.h>
92
93#if REV_ENDIAN_FS
94#include <ufs/ufs/ufs_byte_order.h>
8ad349bb 95#include <architecture/byte_order.h>
1c79356b
A
96#endif /* REV_ENDIAN_FS */
97
91447636
A
98static int ffs_indirtrunc(struct inode *, ufs_daddr_t, ufs_daddr_t,
99 ufs_daddr_t, int, long *);
1c79356b
A
100
101/*
102 * Update the access, modified, and inode change times as specified by the
103 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
104 * used to specify that the inode needs to be updated but that the times have
105 * already been set. The access and modified times are taken from the second
106 * and third parameters; the inode change time is always taken from the current
107 * time. If waitfor is set, then wait for the disk write of the inode to
108 * complete.
109 */
110int
91447636 111ffs_update(struct vnode *vp, struct timeval *access, struct timeval *modify, int waitfor)
1c79356b
A
112{
113 register struct fs *fs;
114 struct buf *bp;
115 struct inode *ip;
91447636
A
116 struct timeval tv;
117 errno_t error;
1c79356b 118#if REV_ENDIAN_FS
91447636 119 struct mount *mp=(vp)->v_mount;
1c79356b
A
120 int rev_endian=(mp->mnt_flag & MNT_REVEND);
121#endif /* REV_ENDIAN_FS */
122
91447636
A
123 ip = VTOI(vp);
124 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1c79356b
A
125 ip->i_flag &=
126 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
127 return (0);
128 }
129 if ((ip->i_flag &
130 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
131 return (0);
132 if (ip->i_flag & IN_ACCESS)
91447636 133 ip->i_atime = access->tv_sec;
1c79356b 134 if (ip->i_flag & IN_UPDATE) {
91447636 135 ip->i_mtime = modify->tv_sec;
1c79356b
A
136 ip->i_modrev++;
137 }
91447636
A
138 if (ip->i_flag & IN_CHANGE) {
139 microtime(&tv);
140 ip->i_ctime = tv.tv_sec;
141 }
1c79356b
A
142 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
143 fs = ip->i_fs;
144 /*
145 * Ensure that uid and gid are correct. This is a temporary
146 * fix until fsck has been changed to do the update.
147 */
148 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
149 ip->i_din.di_ouid = ip->i_uid; /* XXX */
150 ip->i_din.di_ogid = ip->i_gid; /* XXX */
151 } /* XXX */
91447636
A
152 if (error = buf_bread(ip->i_devvp,
153 (daddr64_t)((unsigned)fsbtodb(fs, ino_to_fsba(fs, ip->i_number))),
1c79356b 154 (int)fs->fs_bsize, NOCRED, &bp)) {
91447636
A
155 buf_brelse(bp);
156 return ((int)error);
1c79356b
A
157 }
158#if REV_ENDIAN_FS
159 if (rev_endian)
91447636 160 byte_swap_inode_out(ip, ((struct dinode *)buf_dataptr(bp) + ino_to_fsbo(fs, ip->i_number)));
1c79356b
A
161 else {
162#endif /* REV_ENDIAN_FS */
91447636 163 *((struct dinode *)buf_dataptr(bp) + ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
1c79356b
A
164#if REV_ENDIAN_FS
165 }
166#endif /* REV_ENDIAN_FS */
167
91447636
A
168 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
169 return ((int)buf_bwrite(bp));
1c79356b 170 else {
91447636 171 buf_bdwrite(bp);
1c79356b
A
172 return (0);
173 }
174}
175
91447636 176
1c79356b
A
177#define SINGLE 0 /* index of single indirect block */
178#define DOUBLE 1 /* index of double indirect block */
179#define TRIPLE 2 /* index of triple indirect block */
91447636
A
180
181int
182ffs_truncate_internal(vnode_t ovp, off_t length, int flags, ucred_t cred)
1c79356b 183{
91447636
A
184 struct inode *oip;
185 struct fs *fs;
1c79356b 186 ufs_daddr_t lastblock;
1c79356b
A
187 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
188 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
91447636
A
189 buf_t bp;
190 int offset, size, level, i;
191 long count, nblocks, vflags, blocksreleased = 0;
192 struct timeval tv;
193 int aflags, error, allerror;
194 off_t osize;
195 int devBlockSize=0;
9bccf70c
A
196#if QUOTA
197 int64_t change; /* in bytes */
198#endif /* QUOTA */
1c79356b
A
199
200 if (length < 0)
201 return (EINVAL);
202
203 oip = VTOI(ovp);
204 fs = oip->i_fs;
205
206 if (length > fs->fs_maxfilesize)
207 return (EFBIG);
208
91447636 209 microtime(&tv);
1c79356b
A
210 if (ovp->v_type == VLNK &&
211 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
212#if DIAGNOSTIC
213 if (length != 0)
214 panic("ffs_truncate: partial truncate of symlink");
215#endif
216 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
217 oip->i_size = 0;
218 oip->i_flag |= IN_CHANGE | IN_UPDATE;
91447636 219 return (ffs_update(ovp, &tv, &tv, 1));
1c79356b
A
220 }
221
222 if (oip->i_size == length) {
223 oip->i_flag |= IN_CHANGE | IN_UPDATE;
91447636 224 return (ffs_update(ovp, &tv, &tv, 0));
1c79356b
A
225 }
226#if QUOTA
227 if (error = getinoquota(oip))
228 return (error);
229#endif
230 osize = oip->i_size;
231
232 /*
233 * Lengthen the size of the file. We must ensure that the
234 * last byte of the file is allocated. Since the smallest
235 * value of osize is 0, length will be at least 1.
236 */
237 if (osize < length) {
238 offset = blkoff(fs, length - 1);
239 lbn = lblkno(fs, length - 1);
240 aflags = B_CLRBUF;
91447636 241 if (flags & IO_SYNC)
1c79356b 242 aflags |= B_SYNC;
91447636 243 if (error = ffs_balloc(oip, lbn, offset + 1, cred, &bp, aflags, 0))
1c79356b
A
244 return (error);
245 oip->i_size = length;
246
247 if (UBCINFOEXISTS(ovp)) {
91447636
A
248 buf_markinvalid(bp);
249 buf_bwrite(bp);
1c79356b
A
250 ubc_setsize(ovp, (off_t)length);
251 } else {
252 if (aflags & B_SYNC)
91447636 253 buf_bwrite(bp);
1c79356b 254 else
91447636 255 buf_bawrite(bp);
1c79356b
A
256 }
257 oip->i_flag |= IN_CHANGE | IN_UPDATE;
91447636 258 return (ffs_update(ovp, &tv, &tv, 1));
1c79356b
A
259 }
260 /*
261 * Shorten the size of the file. If the file is not being
262 * truncated to a block boundry, the contents of the
263 * partial block following the end of the file must be
264 * zero'ed in case it ever become accessable again because
265 * of subsequent file growth.
266 */
267 if (UBCINFOEXISTS(ovp))
268 ubc_setsize(ovp, (off_t)length);
269
91447636 270 vflags = ((length > 0) ? BUF_WRITE_DATA : 0) | BUF_SKIP_META;
1c79356b 271
91447636
A
272 if (vflags & BUF_WRITE_DATA)
273 ffs_fsync_internal(ovp, MNT_WAIT);
274 allerror = buf_invalidateblks(ovp, vflags, 0, 0);
275
1c79356b
A
276 offset = blkoff(fs, length);
277 if (offset == 0) {
278 oip->i_size = length;
279 } else {
280 lbn = lblkno(fs, length);
281 aflags = B_CLRBUF;
91447636 282 if (flags & IO_SYNC)
1c79356b 283 aflags |= B_SYNC;
91447636 284 if (error = ffs_balloc(oip, lbn, offset, cred, &bp, aflags, 0))
1c79356b
A
285 return (error);
286 oip->i_size = length;
287 size = blksize(fs, oip, lbn);
91447636 288 bzero((char *)buf_dataptr(bp) + offset, (u_int)(size - offset));
1c79356b
A
289 allocbuf(bp, size);
290 if (UBCINFOEXISTS(ovp)) {
91447636
A
291 buf_markinvalid(bp);
292 buf_bwrite(bp);
1c79356b
A
293 } else {
294 if (aflags & B_SYNC)
91447636 295 buf_bwrite(bp);
1c79356b 296 else
91447636 297 buf_bawrite(bp);
1c79356b
A
298 }
299 }
300 /*
301 * Calculate index into inode's block list of
302 * last direct and indirect blocks (if any)
303 * which we want to keep. Lastblock is -1 when
304 * the file is truncated to 0.
305 */
306 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
307 lastiblock[SINGLE] = lastblock - NDADDR;
308 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
309 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
91447636
A
310
311 devBlockSize = vfs_devblocksize(vnode_mount(ovp));
1c79356b
A
312 nblocks = btodb(fs->fs_bsize, devBlockSize);
313
314 /*
315 * Update file and block pointers on disk before we start freeing
316 * blocks. If we crash before free'ing blocks below, the blocks
317 * will be returned to the free list. lastiblock values are also
318 * normalized to -1 for calls to ffs_indirtrunc below.
319 */
320 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
321 for (level = TRIPLE; level >= SINGLE; level--)
322 if (lastiblock[level] < 0) {
323 oip->i_ib[level] = 0;
324 lastiblock[level] = -1;
325 }
326 for (i = NDADDR - 1; i > lastblock; i--)
327 oip->i_db[i] = 0;
328 oip->i_flag |= IN_CHANGE | IN_UPDATE;
91447636 329 if (error = ffs_update(ovp, &tv, &tv, MNT_WAIT))
1c79356b
A
330 allerror = error;
331 /*
332 * Having written the new inode to disk, save its new configuration
333 * and put back the old block pointers long enough to process them.
334 * Note that we save the new block configuration so we can check it
335 * when we are done.
336 */
337 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
338 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
339 oip->i_size = osize;
91447636
A
340
341 vflags = ((length > 0) ? BUF_WRITE_DATA : 0) | BUF_SKIP_META;
342
343 if (vflags & BUF_WRITE_DATA)
344 ffs_fsync_internal(ovp, MNT_WAIT);
345 allerror = buf_invalidateblks(ovp, vflags, 0, 0);
1c79356b
A
346
347 /*
348 * Indirect blocks first.
349 */
350 indir_lbn[SINGLE] = -NDADDR;
351 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
352 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
353 for (level = TRIPLE; level >= SINGLE; level--) {
354 bn = oip->i_ib[level];
355 if (bn != 0) {
356 error = ffs_indirtrunc(oip, indir_lbn[level],
357 fsbtodb(fs, bn), lastiblock[level], level, &count);
358 if (error)
359 allerror = error;
360 blocksreleased += count;
361 if (lastiblock[level] < 0) {
362 oip->i_ib[level] = 0;
363 ffs_blkfree(oip, bn, fs->fs_bsize);
364 blocksreleased += nblocks;
365 }
366 }
367 if (lastiblock[level] >= 0)
368 goto done;
369 }
370
371 /*
372 * All whole direct blocks or frags.
373 */
374 for (i = NDADDR - 1; i > lastblock; i--) {
375 register long bsize;
376
377 bn = oip->i_db[i];
378 if (bn == 0)
379 continue;
380 oip->i_db[i] = 0;
381 bsize = blksize(fs, oip, i);
382 ffs_blkfree(oip, bn, bsize);
383 blocksreleased += btodb(bsize, devBlockSize);
384 }
385 if (lastblock < 0)
386 goto done;
387
388 /*
389 * Finally, look for a change in size of the
390 * last direct block; release any frags.
391 */
392 bn = oip->i_db[lastblock];
393 if (bn != 0) {
394 long oldspace, newspace;
395
396 /*
397 * Calculate amount of space we're giving
398 * back as old block size minus new block size.
399 */
400 oldspace = blksize(fs, oip, lastblock);
401 oip->i_size = length;
402 newspace = blksize(fs, oip, lastblock);
403 if (newspace == 0)
404 panic("itrunc: newspace");
405 if (oldspace - newspace > 0) {
406 /*
407 * Block number of space to be free'd is
408 * the old block # plus the number of frags
409 * required for the storage we're keeping.
410 */
411 bn += numfrags(fs, newspace);
412 ffs_blkfree(oip, bn, oldspace - newspace);
413 blocksreleased += btodb(oldspace - newspace, devBlockSize);
414 }
415 }
416done:
417#if DIAGNOSTIC
418 for (level = SINGLE; level <= TRIPLE; level++)
419 if (newblks[NDADDR + level] != oip->i_ib[level])
420 panic("itrunc1");
421 for (i = 0; i < NDADDR; i++)
422 if (newblks[i] != oip->i_db[i])
423 panic("itrunc2");
424 if (length == 0 &&
91447636 425 (vnode_hasdirtyblks(ovp) || vnode_hascleanblks(ovp)))
1c79356b
A
426 panic("itrunc3");
427#endif /* DIAGNOSTIC */
428 /*
429 * Put back the real size.
430 */
431 oip->i_size = length;
432 oip->i_blocks -= blocksreleased;
433 if (oip->i_blocks < 0) /* sanity */
434 oip->i_blocks = 0;
435 oip->i_flag |= IN_CHANGE;
436#if QUOTA
9bccf70c
A
437 change = dbtob((int64_t)blocksreleased,devBlockSize);
438 (void) chkdq(oip, -change, NOCRED, 0);
1c79356b
A
439#endif
440 return (allerror);
441}
442
443/*
444 * Release blocks associated with the inode ip and stored in the indirect
445 * block bn. Blocks are free'd in LIFO order up to (but not including)
446 * lastbn. If level is greater than SINGLE, the block is an indirect block
447 * and recursive calls to indirtrunc must be used to cleanse other indirect
448 * blocks.
449 *
450 * NB: triple indirect blocks are untested.
451 */
452static int
453ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
454 register struct inode *ip;
455 ufs_daddr_t lbn, lastbn;
456 ufs_daddr_t dbn;
457 int level;
458 long *countp;
459{
460 register int i;
461 struct buf *bp;
462 struct buf *tbp;
463 register struct fs *fs = ip->i_fs;
464 register ufs_daddr_t *bap;
465 struct vnode *vp=ITOV(ip);
466 ufs_daddr_t *copy, nb, nlbn, last;
467 long blkcount, factor;
468 int nblocks, blocksreleased = 0;
91447636 469 errno_t error = 0, allerror = 0;
1c79356b 470 int devBlockSize=0;
1c79356b 471 struct mount *mp=vp->v_mount;
91447636 472#if REV_ENDIAN_FS
1c79356b
A
473 int rev_endian=(mp->mnt_flag & MNT_REVEND);
474#endif /* REV_ENDIAN_FS */
475
476 /*
477 * Calculate index in current block of last
478 * block to be kept. -1 indicates the entire
479 * block so we need not calculate the index.
480 */
481 factor = 1;
482 for (i = SINGLE; i < level; i++)
483 factor *= NINDIR(fs);
484 last = lastbn;
485 if (lastbn > 0)
486 last /= factor;
91447636
A
487
488 devBlockSize = vfs_devblocksize(mp);
1c79356b
A
489 nblocks = btodb(fs->fs_bsize, devBlockSize);
490
491 /* Doing a MALLOC here is asking for trouble. We can still
492 * deadlock on pagerfile lock, in case we are running
493 * low on memory and block in MALLOC
494 */
495
91447636
A
496 tbp = buf_geteblk(fs->fs_bsize);
497 copy = (ufs_daddr_t *)buf_dataptr(tbp);
1c79356b
A
498
499 /*
500 * Get buffer of block pointers, zero those entries corresponding
501 * to blocks to be free'd, and update on disk copy first. Since
502 * double(triple) indirect before single(double) indirect, calls
503 * to bmap on these blocks will fail. However, we already have
91447636
A
504 * the on disk address, so we have to set the blkno field
505 * explicitly instead of letting buf_bread do everything for us.
1c79356b
A
506 */
507
508 vp = ITOV(ip);
91447636
A
509 bp = buf_getblk(vp, (daddr64_t)((unsigned)lbn), (int)fs->fs_bsize, 0, 0, BLK_META);
510
511 if (buf_valid(bp)) {
1c79356b
A
512 /* Braces must be here in case trace evaluates to nothing. */
513 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
514 } else {
515 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
516 current_proc()->p_stats->p_ru.ru_inblock++; /* pay for read */
91447636
A
517 buf_setflags(bp, B_READ);
518 if (buf_count(bp) > buf_size(bp))
1c79356b 519 panic("ffs_indirtrunc: bad buffer size");
91447636
A
520 buf_setblkno(bp, (daddr64_t)((unsigned)dbn));
521 VNOP_STRATEGY(bp);
522 error = buf_biowait(bp);
1c79356b
A
523 }
524 if (error) {
91447636 525 buf_brelse(bp);
1c79356b 526 *countp = 0;
91447636
A
527 buf_brelse(tbp);
528 return ((int)error);
1c79356b
A
529 }
530
91447636 531 bap = (ufs_daddr_t *)buf_dataptr(bp);
1c79356b
A
532 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
533 bzero((caddr_t)&bap[last + 1],
534 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
535 if (last == -1)
91447636 536 buf_markinvalid(bp);
55e303ae
A
537 if (last != -1 && (vp)->v_mount->mnt_flag & MNT_ASYNC) {
538 error = 0;
91447636 539 buf_bdwrite(bp);
55e303ae 540 } else {
91447636 541 error = buf_bwrite(bp);
55e303ae
A
542 if (error)
543 allerror = error;
544 }
1c79356b
A
545 bap = copy;
546
547 /*
548 * Recursively free totally unused blocks.
549 */
550 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
551 i--, nlbn += factor) {
552#if REV_ENDIAN_FS
553 if (rev_endian)
8ad349bb 554 nb = NXSwapLong(bap[i]);
1c79356b
A
555 else {
556#endif /* REV_ENDIAN_FS */
557 nb = bap[i];
558#if REV_ENDIAN_FS
559 }
560#endif /* REV_ENDIAN_FS */
561 if (nb == 0)
562 continue;
563 if (level > SINGLE) {
564 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
565 (ufs_daddr_t)-1, level - 1, &blkcount))
566 allerror = error;
567 blocksreleased += blkcount;
568 }
569 ffs_blkfree(ip, nb, fs->fs_bsize);
570 blocksreleased += nblocks;
571 }
572
573 /*
574 * Recursively free last partial block.
575 */
576 if (level > SINGLE && lastbn >= 0) {
577 last = lastbn % factor;
578#if REV_ENDIAN_FS
579 if (rev_endian)
8ad349bb 580 nb = NXSwapLong(bap[i]);
1c79356b
A
581 else {
582#endif /* REV_ENDIAN_FS */
583 nb = bap[i];
584#if REV_ENDIAN_FS
585 }
586#endif /* REV_ENDIAN_FS */
587 if (nb != 0) {
588 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
589 last, level - 1, &blkcount))
590 allerror = error;
591 blocksreleased += blkcount;
592 }
593 }
91447636 594 buf_brelse(tbp);
1c79356b 595 *countp = blocksreleased;
91447636 596 return ((int)allerror);
1c79356b
A
597}
598