]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/ufs/ffs/ffs_inode.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / ufs / ffs / ffs_inode.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_quota.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/proc.h>
41 #include <sys/bio.h>
42 #include <sys/buf.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/resourcevar.h>
46 #include <sys/vmmeter.h>
47 #include <sys/stat.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_object.h>
52
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/inode.h>
57 #include <ufs/ufs/ufs_extern.h>
58
59 #include <ufs/ffs/fs.h>
60 #include <ufs/ffs/ffs_extern.h>
61
62 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
63             ufs2_daddr_t, int, ufs2_daddr_t *);
64
65 /*
66  * Update the access, modified, and inode change times as specified by the
67  * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.  Write the inode
68  * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
69  * the timestamp update).  The IN_LAZYMOD flag is set to force a write
70  * later if not now.  The IN_LAZYACCESS is set instead of IN_MODIFIED if the fs
71  * is currently being suspended (or is suspended) and vnode has been accessed.
72  * If we write now, then clear IN_MODIFIED, IN_LAZYACCESS and IN_LAZYMOD to
73  * reflect the presumably successful write, and if waitfor is set, then wait
74  * for the write to complete.
75  */
76 int
77 ffs_update(vp, waitfor)
78         struct vnode *vp;
79         int waitfor;
80 {
81         struct fs *fs;
82         struct buf *bp;
83         struct inode *ip;
84         int error;
85
86         ASSERT_VOP_ELOCKED(vp, "ffs_update");
87         ufs_itimes(vp);
88         ip = VTOI(vp);
89         if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
90                 return (0);
91         ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
92         fs = ip->i_fs;
93         if (fs->fs_ronly)
94                 return (0);
95         /*
96          * Ensure that uid and gid are correct. This is a temporary
97          * fix until fsck has been changed to do the update.
98          */
99         if (fs->fs_magic == FS_UFS1_MAGIC &&            /* XXX */
100             fs->fs_old_inodefmt < FS_44INODEFMT) {      /* XXX */
101                 ip->i_din1->di_ouid = ip->i_uid;        /* XXX */
102                 ip->i_din1->di_ogid = ip->i_gid;        /* XXX */
103         }                                               /* XXX */
104         error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
105                 (int)fs->fs_bsize, NOCRED, &bp);
106         if (error) {
107                 brelse(bp);
108                 return (error);
109         }
110         if (DOINGSOFTDEP(vp))
111                 softdep_update_inodeblock(ip, bp, waitfor);
112         else if (ip->i_effnlink != ip->i_nlink)
113                 panic("ffs_update: bad link cnt");
114         if (ip->i_ump->um_fstype == UFS1)
115                 *((struct ufs1_dinode *)bp->b_data +
116                     ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
117         else
118                 *((struct ufs2_dinode *)bp->b_data +
119                     ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
120         if (waitfor && !DOINGASYNC(vp)) {
121                 return (bwrite(bp));
122         } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
123                 return (bwrite(bp));
124         } else {
125                 if (bp->b_bufsize == fs->fs_bsize)
126                         bp->b_flags |= B_CLUSTEROK;
127                 bdwrite(bp);
128                 return (0);
129         }
130 }
131
132 static void
133 ffs_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end)
134 {
135         vm_object_t object;
136
137         if ((object = vp->v_object) == NULL)
138                 return;
139         VM_OBJECT_LOCK(object);
140         vm_object_page_remove(object, start, end, FALSE);
141         VM_OBJECT_UNLOCK(object);
142 }
143
144 #define SINGLE  0       /* index of single indirect block */
145 #define DOUBLE  1       /* index of double indirect block */
146 #define TRIPLE  2       /* index of triple indirect block */
147 /*
148  * Truncate the inode ip to at most length size, freeing the
149  * disk blocks.
150  */
151 int
152 ffs_truncate(vp, length, flags, cred, td)
153         struct vnode *vp;
154         off_t length;
155         int flags;
156         struct ucred *cred;
157         struct thread *td;
158 {
159         struct inode *ip;
160         ufs2_daddr_t bn, lbn, lastblock, lastiblock[NIADDR], indir_lbn[NIADDR];
161         ufs2_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
162         ufs2_daddr_t count, blocksreleased = 0, datablocks;
163         struct fs *fs;
164         struct buf *bp;
165         struct ufsmount *ump;
166         int needextclean, softdepslowdown, extblocks;
167         int offset, size, level, nblocks;
168         int i, error, allerror;
169         off_t osize;
170
171         ip = VTOI(vp);
172         fs = ip->i_fs;
173         ump = ip->i_ump;
174
175         ASSERT_VOP_LOCKED(vp, "ffs_truncate");
176
177         if (length < 0)
178                 return (EINVAL);
179         /*
180          * Historically clients did not have to specify which data
181          * they were truncating. So, if not specified, we assume
182          * traditional behavior, e.g., just the normal data.
183          */
184         if ((flags & (IO_EXT | IO_NORMAL)) == 0)
185                 flags |= IO_NORMAL;
186         /*
187          * If we are truncating the extended-attributes, and cannot
188          * do it with soft updates, then do it slowly here. If we are
189          * truncating both the extended attributes and the file contents
190          * (e.g., the file is being unlinked), then pick it off with
191          * soft updates below.
192          */
193         needextclean = 0;
194         softdepslowdown = DOINGSOFTDEP(vp) && softdep_slowdown(vp);
195         extblocks = 0;
196         datablocks = DIP(ip, i_blocks);
197         if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
198                 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
199                 datablocks -= extblocks;
200         }
201         if ((flags & IO_EXT) && extblocks > 0) {
202                 if (DOINGSOFTDEP(vp) && softdepslowdown == 0 && length == 0) {
203                         if ((flags & IO_NORMAL) == 0) {
204                                 softdep_setup_freeblocks(ip, length, IO_EXT);
205                                 return (0);
206                         }
207                         needextclean = 1;
208                 } else {
209                         if (length != 0)
210                                 panic("ffs_truncate: partial trunc of extdata");
211                         if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
212                                 return (error);
213                         osize = ip->i_din2->di_extsize;
214                         ip->i_din2->di_blocks -= extblocks;
215 #ifdef QUOTA
216                         (void) chkdq(ip, -extblocks, NOCRED, 0);
217 #endif
218                         vinvalbuf(vp, V_ALT, td, 0, 0);
219                         ffs_pages_remove(vp,
220                             OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
221                         ip->i_din2->di_extsize = 0;
222                         for (i = 0; i < NXADDR; i++) {
223                                 oldblks[i] = ip->i_din2->di_extb[i];
224                                 ip->i_din2->di_extb[i] = 0;
225                         }
226                         ip->i_flag |= IN_CHANGE;
227                         if ((error = ffs_update(vp, 1)))
228                                 return (error);
229                         for (i = 0; i < NXADDR; i++) {
230                                 if (oldblks[i] == 0)
231                                         continue;
232                                 ffs_blkfree(ump, fs, ip->i_devvp, oldblks[i],
233                                     sblksize(fs, osize, i), ip->i_number);
234                         }
235                 }
236         }
237         if ((flags & IO_NORMAL) == 0)
238                 return (0);
239         if (length > fs->fs_maxfilesize)
240                 return (EFBIG);
241         if (vp->v_type == VLNK &&
242             (ip->i_size < vp->v_mount->mnt_maxsymlinklen ||
243              datablocks == 0)) {
244 #ifdef INVARIANTS
245                 if (length != 0)
246                         panic("ffs_truncate: partial truncate of symlink");
247 #endif
248                 bzero(SHORTLINK(ip), (u_int)ip->i_size);
249                 ip->i_size = 0;
250                 DIP_SET(ip, i_size, 0);
251                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
252                 if (needextclean)
253                         softdep_setup_freeblocks(ip, length, IO_EXT);
254                 return (ffs_update(vp, 1));
255         }
256         if (ip->i_size == length) {
257                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
258                 if (needextclean)
259                         softdep_setup_freeblocks(ip, length, IO_EXT);
260                 return (ffs_update(vp, 0));
261         }
262         if (fs->fs_ronly)
263                 panic("ffs_truncate: read-only filesystem");
264 #ifdef QUOTA
265         error = getinoquota(ip);
266         if (error)
267                 return (error);
268 #endif
269         if ((ip->i_flags & SF_SNAPSHOT) != 0)
270                 ffs_snapremove(vp);
271         vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
272         if (DOINGSOFTDEP(vp)) {
273                 if (length > 0 || softdepslowdown) {
274                         /*
275                          * If a file is only partially truncated, then
276                          * we have to clean up the data structures
277                          * describing the allocation past the truncation
278                          * point. Finding and deallocating those structures
279                          * is a lot of work. Since partial truncation occurs
280                          * rarely, we solve the problem by syncing the file
281                          * so that it will have no data structures left.
282                          */
283                         if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
284                                 return (error);
285                         UFS_LOCK(ump);
286                         if (ip->i_flag & IN_SPACECOUNTED)
287                                 fs->fs_pendingblocks -= datablocks;
288                         UFS_UNLOCK(ump);
289                 } else {
290 #ifdef QUOTA
291                         (void) chkdq(ip, -datablocks, NOCRED, 0);
292 #endif
293                         softdep_setup_freeblocks(ip, length, needextclean ?
294                             IO_EXT | IO_NORMAL : IO_NORMAL);
295                         ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
296                         vinvalbuf(vp, needextclean ? 0 : V_NORMAL, td, 0, 0);
297                         if (!needextclean)
298                                 ffs_pages_remove(vp, 0,
299                                     OFF_TO_IDX(lblktosize(fs, -extblocks)));
300                         vnode_pager_setsize(vp, 0);
301                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
302                         return (ffs_update(vp, 0));
303                 }
304         }
305         osize = ip->i_size;
306         /*
307          * Lengthen the size of the file. We must ensure that the
308          * last byte of the file is allocated. Since the smallest
309          * value of osize is 0, length will be at least 1.
310          */
311         if (osize < length) {
312                 vnode_pager_setsize(vp, length);
313                 flags |= BA_CLRBUF;
314                 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
315                 if (error) {
316                         vnode_pager_setsize(vp, osize);
317                         return (error);
318                 }
319                 ip->i_size = length;
320                 DIP_SET(ip, i_size, length);
321                 if (bp->b_bufsize == fs->fs_bsize)
322                         bp->b_flags |= B_CLUSTEROK;
323                 if (flags & IO_SYNC)
324                         bwrite(bp);
325                 else
326                         bawrite(bp);
327                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
328                 return (ffs_update(vp, 1));
329         }
330         /*
331          * Shorten the size of the file. If the file is not being
332          * truncated to a block boundary, the contents of the
333          * partial block following the end of the file must be
334          * zero'ed in case it ever becomes accessible again because
335          * of subsequent file growth. Directories however are not
336          * zero'ed as they should grow back initialized to empty.
337          */
338         offset = blkoff(fs, length);
339         if (offset == 0) {
340                 ip->i_size = length;
341                 DIP_SET(ip, i_size, length);
342         } else {
343                 lbn = lblkno(fs, length);
344                 flags |= BA_CLRBUF;
345                 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
346                 if (error) {
347                         return (error);
348                 }
349                 /*
350                  * When we are doing soft updates and the UFS_BALLOC
351                  * above fills in a direct block hole with a full sized
352                  * block that will be truncated down to a fragment below,
353                  * we must flush out the block dependency with an FSYNC
354                  * so that we do not get a soft updates inconsistency
355                  * when we create the fragment below.
356                  */
357                 if (DOINGSOFTDEP(vp) && lbn < NDADDR &&
358                     fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
359                     (error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
360                         return (error);
361                 ip->i_size = length;
362                 DIP_SET(ip, i_size, length);
363                 size = blksize(fs, ip, lbn);
364                 if (vp->v_type != VDIR)
365                         bzero((char *)bp->b_data + offset,
366                             (u_int)(size - offset));
367                 /* Kirk's code has reallocbuf(bp, size, 1) here */
368                 allocbuf(bp, size);
369                 if (bp->b_bufsize == fs->fs_bsize)
370                         bp->b_flags |= B_CLUSTEROK;
371                 if (flags & IO_SYNC)
372                         bwrite(bp);
373                 else
374                         bawrite(bp);
375         }
376         /*
377          * Calculate index into inode's block list of
378          * last direct and indirect blocks (if any)
379          * which we want to keep.  Lastblock is -1 when
380          * the file is truncated to 0.
381          */
382         lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
383         lastiblock[SINGLE] = lastblock - NDADDR;
384         lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
385         lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
386         nblocks = btodb(fs->fs_bsize);
387         /*
388          * Update file and block pointers on disk before we start freeing
389          * blocks.  If we crash before free'ing blocks below, the blocks
390          * will be returned to the free list.  lastiblock values are also
391          * normalized to -1 for calls to ffs_indirtrunc below.
392          */
393         for (level = TRIPLE; level >= SINGLE; level--) {
394                 oldblks[NDADDR + level] = DIP(ip, i_ib[level]);
395                 if (lastiblock[level] < 0) {
396                         DIP_SET(ip, i_ib[level], 0);
397                         lastiblock[level] = -1;
398                 }
399         }
400         for (i = 0; i < NDADDR; i++) {
401                 oldblks[i] = DIP(ip, i_db[i]);
402                 if (i > lastblock)
403                         DIP_SET(ip, i_db[i], 0);
404         }
405         ip->i_flag |= IN_CHANGE | IN_UPDATE;
406         allerror = ffs_update(vp, 1);
407         
408         /*
409          * Having written the new inode to disk, save its new configuration
410          * and put back the old block pointers long enough to process them.
411          * Note that we save the new block configuration so we can check it
412          * when we are done.
413          */
414         for (i = 0; i < NDADDR; i++) {
415                 newblks[i] = DIP(ip, i_db[i]);
416                 DIP_SET(ip, i_db[i], oldblks[i]);
417         }
418         for (i = 0; i < NIADDR; i++) {
419                 newblks[NDADDR + i] = DIP(ip, i_ib[i]);
420                 DIP_SET(ip, i_ib[i], oldblks[NDADDR + i]);
421         }
422         ip->i_size = osize;
423         DIP_SET(ip, i_size, osize);
424
425         error = vtruncbuf(vp, cred, td, length, fs->fs_bsize);
426         if (error && (allerror == 0))
427                 allerror = error;
428
429         /*
430          * Indirect blocks first.
431          */
432         indir_lbn[SINGLE] = -NDADDR;
433         indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
434         indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
435         for (level = TRIPLE; level >= SINGLE; level--) {
436                 bn = DIP(ip, i_ib[level]);
437                 if (bn != 0) {
438                         error = ffs_indirtrunc(ip, indir_lbn[level],
439                             fsbtodb(fs, bn), lastiblock[level], level, &count);
440                         if (error)
441                                 allerror = error;
442                         blocksreleased += count;
443                         if (lastiblock[level] < 0) {
444                                 DIP_SET(ip, i_ib[level], 0);
445                                 ffs_blkfree(ump, fs, ip->i_devvp, bn,
446                                     fs->fs_bsize, ip->i_number);
447                                 blocksreleased += nblocks;
448                         }
449                 }
450                 if (lastiblock[level] >= 0)
451                         goto done;
452         }
453
454         /*
455          * All whole direct blocks or frags.
456          */
457         for (i = NDADDR - 1; i > lastblock; i--) {
458                 long bsize;
459
460                 bn = DIP(ip, i_db[i]);
461                 if (bn == 0)
462                         continue;
463                 DIP_SET(ip, i_db[i], 0);
464                 bsize = blksize(fs, ip, i);
465                 ffs_blkfree(ump, fs, ip->i_devvp, bn, bsize, ip->i_number);
466                 blocksreleased += btodb(bsize);
467         }
468         if (lastblock < 0)
469                 goto done;
470
471         /*
472          * Finally, look for a change in size of the
473          * last direct block; release any frags.
474          */
475         bn = DIP(ip, i_db[lastblock]);
476         if (bn != 0) {
477                 long oldspace, newspace;
478
479                 /*
480                  * Calculate amount of space we're giving
481                  * back as old block size minus new block size.
482                  */
483                 oldspace = blksize(fs, ip, lastblock);
484                 ip->i_size = length;
485                 DIP_SET(ip, i_size, length);
486                 newspace = blksize(fs, ip, lastblock);
487                 if (newspace == 0)
488                         panic("ffs_truncate: newspace");
489                 if (oldspace - newspace > 0) {
490                         /*
491                          * Block number of space to be free'd is
492                          * the old block # plus the number of frags
493                          * required for the storage we're keeping.
494                          */
495                         bn += numfrags(fs, newspace);
496                         ffs_blkfree(ump, fs, ip->i_devvp, bn,
497                             oldspace - newspace, ip->i_number);
498                         blocksreleased += btodb(oldspace - newspace);
499                 }
500         }
501 done:
502 #ifdef INVARIANTS
503         for (level = SINGLE; level <= TRIPLE; level++)
504                 if (newblks[NDADDR + level] != DIP(ip, i_ib[level]))
505                         panic("ffs_truncate1");
506         for (i = 0; i < NDADDR; i++)
507                 if (newblks[i] != DIP(ip, i_db[i]))
508                         panic("ffs_truncate2");
509         VI_LOCK(vp);
510         if (length == 0 &&
511             (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
512             (vp->v_bufobj.bo_dirty.bv_cnt > 0 ||
513              vp->v_bufobj.bo_clean.bv_cnt > 0))
514                 panic("ffs_truncate3");
515         VI_UNLOCK(vp);
516 #endif /* INVARIANTS */
517         /*
518          * Put back the real size.
519          */
520         ip->i_size = length;
521         DIP_SET(ip, i_size, length);
522         DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
523
524         if (DIP(ip, i_blocks) < 0)                      /* sanity */
525                 DIP_SET(ip, i_blocks, 0);
526         ip->i_flag |= IN_CHANGE;
527 #ifdef QUOTA
528         (void) chkdq(ip, -blocksreleased, NOCRED, 0);
529 #endif
530         return (allerror);
531 }
532
533 /*
534  * Release blocks associated with the inode ip and stored in the indirect
535  * block bn.  Blocks are free'd in LIFO order up to (but not including)
536  * lastbn.  If level is greater than SINGLE, the block is an indirect block
537  * and recursive calls to indirtrunc must be used to cleanse other indirect
538  * blocks.
539  */
540 static int
541 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
542         struct inode *ip;
543         ufs2_daddr_t lbn, lastbn;
544         ufs2_daddr_t dbn;
545         int level;
546         ufs2_daddr_t *countp;
547 {
548         struct buf *bp;
549         struct fs *fs = ip->i_fs;
550         struct vnode *vp;
551         caddr_t copy = NULL;
552         int i, nblocks, error = 0, allerror = 0;
553         ufs2_daddr_t nb, nlbn, last;
554         ufs2_daddr_t blkcount, factor, blocksreleased = 0;
555         ufs1_daddr_t *bap1 = NULL;
556         ufs2_daddr_t *bap2 = NULL;
557 #       define BAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? bap1[i] : bap2[i])
558
559         /*
560          * Calculate index in current block of last
561          * block to be kept.  -1 indicates the entire
562          * block so we need not calculate the index.
563          */
564         factor = 1;
565         for (i = SINGLE; i < level; i++)
566                 factor *= NINDIR(fs);
567         last = lastbn;
568         if (lastbn > 0)
569                 last /= factor;
570         nblocks = btodb(fs->fs_bsize);
571         /*
572          * Get buffer of block pointers, zero those entries corresponding
573          * to blocks to be free'd, and update on disk copy first.  Since
574          * double(triple) indirect before single(double) indirect, calls
575          * to bmap on these blocks will fail.  However, we already have
576          * the on disk address, so we have to set the b_blkno field
577          * explicitly instead of letting bread do everything for us.
578          */
579         vp = ITOV(ip);
580         bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, 0);
581         if ((bp->b_flags & B_CACHE) == 0) {
582                 curthread->td_ru.ru_inblock++;  /* pay for read */
583                 bp->b_iocmd = BIO_READ;
584                 bp->b_flags &= ~B_INVAL;
585                 bp->b_ioflags &= ~BIO_ERROR;
586                 if (bp->b_bcount > bp->b_bufsize)
587                         panic("ffs_indirtrunc: bad buffer size");
588                 bp->b_blkno = dbn;
589                 vfs_busy_pages(bp, 0);
590                 bp->b_iooffset = dbtob(bp->b_blkno);
591                 bstrategy(bp);
592                 error = bufwait(bp);
593         }
594         if (error) {
595                 brelse(bp);
596                 *countp = 0;
597                 return (error);
598         }
599
600         if (ip->i_ump->um_fstype == UFS1)
601                 bap1 = (ufs1_daddr_t *)bp->b_data;
602         else
603                 bap2 = (ufs2_daddr_t *)bp->b_data;
604         if (lastbn != -1) {
605                 MALLOC(copy, caddr_t, fs->fs_bsize, M_TEMP, M_WAITOK);
606                 bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
607                 for (i = last + 1; i < NINDIR(fs); i++)
608                         if (ip->i_ump->um_fstype == UFS1)
609                                 bap1[i] = 0;
610                         else
611                                 bap2[i] = 0;
612                 if (DOINGASYNC(vp)) {
613                         bawrite(bp);
614                 } else {
615                         error = bwrite(bp);
616                         if (error)
617                                 allerror = error;
618                 }
619                 if (ip->i_ump->um_fstype == UFS1)
620                         bap1 = (ufs1_daddr_t *)copy;
621                 else
622                         bap2 = (ufs2_daddr_t *)copy;
623         }
624
625         /*
626          * Recursively free totally unused blocks.
627          */
628         for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
629             i--, nlbn += factor) {
630                 nb = BAP(ip, i);
631                 if (nb == 0)
632                         continue;
633                 if (level > SINGLE) {
634                         if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
635                             (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
636                                 allerror = error;
637                         blocksreleased += blkcount;
638                 }
639                 ffs_blkfree(ip->i_ump, fs, ip->i_devvp, nb, fs->fs_bsize,
640                     ip->i_number);
641                 blocksreleased += nblocks;
642         }
643
644         /*
645          * Recursively free last partial block.
646          */
647         if (level > SINGLE && lastbn >= 0) {
648                 last = lastbn % factor;
649                 nb = BAP(ip, i);
650                 if (nb != 0) {
651                         error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
652                             last, level - 1, &blkcount);
653                         if (error)
654                                 allerror = error;
655                         blocksreleased += blkcount;
656                 }
657         }
658         if (copy != NULL) {
659                 FREE(copy, M_TEMP);
660         } else {
661                 bp->b_flags |= B_INVAL | B_NOCACHE;
662                 brelse(bp);
663         }
664
665         *countp = blocksreleased;
666         return (allerror);
667 }
668
669 int
670 ffs_rdonly(struct inode *ip)
671 {
672
673         return (ip->i_ump->um_fs->fs_ronly != 0);
674 }
675