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