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