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