]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/ufs/ffs/ffs_inode.c
MFC r226155:
[FreeBSD/stable/8.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 #define SINGLE  0       /* index of single indirect block */
133 #define DOUBLE  1       /* index of double indirect block */
134 #define TRIPLE  2       /* index of triple indirect block */
135 /*
136  * Truncate the inode ip to at most length size, freeing the
137  * disk blocks.
138  */
139 int
140 ffs_truncate(vp, length, flags, cred, td)
141         struct vnode *vp;
142         off_t length;
143         int flags;
144         struct ucred *cred;
145         struct thread *td;
146 {
147         struct inode *ip;
148         ufs2_daddr_t bn, lbn, lastblock, lastiblock[NIADDR], indir_lbn[NIADDR];
149         ufs2_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
150         ufs2_daddr_t count, blocksreleased = 0, datablocks;
151         struct bufobj *bo;
152         struct fs *fs;
153         struct buf *bp;
154         struct ufsmount *ump;
155         int needextclean, softdepslowdown, extblocks;
156         int offset, size, level, nblocks;
157         int i, error, allerror;
158         off_t osize;
159
160         ip = VTOI(vp);
161         fs = ip->i_fs;
162         ump = ip->i_ump;
163         bo = &vp->v_bufobj;
164
165         ASSERT_VOP_LOCKED(vp, "ffs_truncate");
166
167         if (length < 0)
168                 return (EINVAL);
169         /*
170          * Historically clients did not have to specify which data
171          * they were truncating. So, if not specified, we assume
172          * traditional behavior, e.g., just the normal data.
173          */
174         if ((flags & (IO_EXT | IO_NORMAL)) == 0)
175                 flags |= IO_NORMAL;
176         /*
177          * If we are truncating the extended-attributes, and cannot
178          * do it with soft updates, then do it slowly here. If we are
179          * truncating both the extended attributes and the file contents
180          * (e.g., the file is being unlinked), then pick it off with
181          * soft updates below.
182          */
183         needextclean = 0;
184         softdepslowdown = DOINGSOFTDEP(vp) && softdep_slowdown(vp);
185         extblocks = 0;
186         datablocks = DIP(ip, i_blocks);
187         if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
188                 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
189                 datablocks -= extblocks;
190         }
191         if ((flags & IO_EXT) && extblocks > 0) {
192                 if (DOINGSOFTDEP(vp) && softdepslowdown == 0 && length == 0) {
193                         if ((flags & IO_NORMAL) == 0) {
194                                 softdep_setup_freeblocks(ip, length, IO_EXT);
195                                 return (0);
196                         }
197                         needextclean = 1;
198                 } else {
199                         if (length != 0)
200                                 panic("ffs_truncate: partial trunc of extdata");
201                         if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
202                                 return (error);
203                         osize = ip->i_din2->di_extsize;
204                         ip->i_din2->di_blocks -= extblocks;
205 #ifdef QUOTA
206                         (void) chkdq(ip, -extblocks, NOCRED, 0);
207 #endif
208                         vinvalbuf(vp, V_ALT, 0, 0);
209                         vn_pages_remove(vp,
210                             OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
211                         ip->i_din2->di_extsize = 0;
212                         for (i = 0; i < NXADDR; i++) {
213                                 oldblks[i] = ip->i_din2->di_extb[i];
214                                 ip->i_din2->di_extb[i] = 0;
215                         }
216                         ip->i_flag |= IN_CHANGE;
217                         if ((error = ffs_update(vp, 1)))
218                                 return (error);
219                         for (i = 0; i < NXADDR; i++) {
220                                 if (oldblks[i] == 0)
221                                         continue;
222                                 ffs_blkfree(ump, fs, ip->i_devvp, oldblks[i],
223                                     sblksize(fs, osize, i), ip->i_number);
224                         }
225                 }
226         }
227         if ((flags & IO_NORMAL) == 0)
228                 return (0);
229         if (length > fs->fs_maxfilesize)
230                 return (EFBIG);
231         if (vp->v_type == VLNK &&
232             (ip->i_size < vp->v_mount->mnt_maxsymlinklen ||
233              datablocks == 0)) {
234 #ifdef INVARIANTS
235                 if (length != 0)
236                         panic("ffs_truncate: partial truncate of symlink");
237 #endif
238                 bzero(SHORTLINK(ip), (u_int)ip->i_size);
239                 ip->i_size = 0;
240                 DIP_SET(ip, i_size, 0);
241                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
242                 if (needextclean)
243                         softdep_setup_freeblocks(ip, length, IO_EXT);
244                 return (ffs_update(vp, 1));
245         }
246         if (ip->i_size == length) {
247                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
248                 if (needextclean)
249                         softdep_setup_freeblocks(ip, length, IO_EXT);
250                 return (ffs_update(vp, 0));
251         }
252         if (fs->fs_ronly)
253                 panic("ffs_truncate: read-only filesystem");
254 #ifdef QUOTA
255         error = getinoquota(ip);
256         if (error)
257                 return (error);
258 #endif
259         if ((ip->i_flags & SF_SNAPSHOT) != 0)
260                 ffs_snapremove(vp);
261         vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
262         if (DOINGSOFTDEP(vp)) {
263                 if (length > 0 || softdepslowdown) {
264                         /*
265                          * If a file is only partially truncated, then
266                          * we have to clean up the data structures
267                          * describing the allocation past the truncation
268                          * point. Finding and deallocating those structures
269                          * is a lot of work. Since partial truncation occurs
270                          * rarely, we solve the problem by syncing the file
271                          * so that it will have no data structures left.
272                          */
273                         if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
274                                 return (error);
275                         UFS_LOCK(ump);
276                         if (ip->i_flag & IN_SPACECOUNTED)
277                                 fs->fs_pendingblocks -= datablocks;
278                         UFS_UNLOCK(ump);
279                 } else {
280 #ifdef QUOTA
281                         (void) chkdq(ip, -datablocks, NOCRED, 0);
282 #endif
283                         softdep_setup_freeblocks(ip, length, needextclean ?
284                             IO_EXT | IO_NORMAL : IO_NORMAL);
285                         ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
286                         vinvalbuf(vp, needextclean ? 0 : V_NORMAL, 0, 0);
287                         if (!needextclean)
288                                 vn_pages_remove(vp, 0,
289                                     OFF_TO_IDX(lblktosize(fs, -extblocks)));
290                         vnode_pager_setsize(vp, 0);
291                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
292                         return (ffs_update(vp, 0));
293                 }
294         }
295         osize = ip->i_size;
296         /*
297          * Lengthen the size of the file. We must ensure that the
298          * last byte of the file is allocated. Since the smallest
299          * value of osize is 0, length will be at least 1.
300          */
301         if (osize < length) {
302                 vnode_pager_setsize(vp, length);
303                 flags |= BA_CLRBUF;
304                 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
305                 if (error) {
306                         vnode_pager_setsize(vp, osize);
307                         return (error);
308                 }
309                 ip->i_size = length;
310                 DIP_SET(ip, i_size, length);
311                 if (bp->b_bufsize == fs->fs_bsize)
312                         bp->b_flags |= B_CLUSTEROK;
313                 if (flags & IO_SYNC)
314                         bwrite(bp);
315                 else
316                         bawrite(bp);
317                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
318                 return (ffs_update(vp, 1));
319         }
320         /*
321          * Shorten the size of the file. If the file is not being
322          * truncated to a block boundary, the contents of the
323          * partial block following the end of the file must be
324          * zero'ed in case it ever becomes accessible again because
325          * of subsequent file growth. Directories however are not
326          * zero'ed as they should grow back initialized to empty.
327          */
328         offset = blkoff(fs, length);
329         if (offset == 0) {
330                 ip->i_size = length;
331                 DIP_SET(ip, i_size, length);
332         } else {
333                 lbn = lblkno(fs, length);
334                 flags |= BA_CLRBUF;
335                 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
336                 if (error) {
337                         return (error);
338                 }
339                 /*
340                  * When we are doing soft updates and the UFS_BALLOC
341                  * above fills in a direct block hole with a full sized
342                  * block that will be truncated down to a fragment below,
343                  * we must flush out the block dependency with an FSYNC
344                  * so that we do not get a soft updates inconsistency
345                  * when we create the fragment below.
346                  */
347                 if (DOINGSOFTDEP(vp) && lbn < NDADDR &&
348                     fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
349                     (error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
350                         return (error);
351                 ip->i_size = length;
352                 DIP_SET(ip, i_size, length);
353                 size = blksize(fs, ip, lbn);
354                 if (vp->v_type != VDIR)
355                         bzero((char *)bp->b_data + offset,
356                             (u_int)(size - offset));
357                 /* Kirk's code has reallocbuf(bp, size, 1) here */
358                 allocbuf(bp, size);
359                 if (bp->b_bufsize == fs->fs_bsize)
360                         bp->b_flags |= B_CLUSTEROK;
361                 if (flags & IO_SYNC)
362                         bwrite(bp);
363                 else
364                         bawrite(bp);
365         }
366         /*
367          * Calculate index into inode's block list of
368          * last direct and indirect blocks (if any)
369          * which we want to keep.  Lastblock is -1 when
370          * the file is truncated to 0.
371          */
372         lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
373         lastiblock[SINGLE] = lastblock - NDADDR;
374         lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
375         lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
376         nblocks = btodb(fs->fs_bsize);
377         /*
378          * Update file and block pointers on disk before we start freeing
379          * blocks.  If we crash before free'ing blocks below, the blocks
380          * will be returned to the free list.  lastiblock values are also
381          * normalized to -1 for calls to ffs_indirtrunc below.
382          */
383         for (level = TRIPLE; level >= SINGLE; level--) {
384                 oldblks[NDADDR + level] = DIP(ip, i_ib[level]);
385                 if (lastiblock[level] < 0) {
386                         DIP_SET(ip, i_ib[level], 0);
387                         lastiblock[level] = -1;
388                 }
389         }
390         for (i = 0; i < NDADDR; i++) {
391                 oldblks[i] = DIP(ip, i_db[i]);
392                 if (i > lastblock)
393                         DIP_SET(ip, i_db[i], 0);
394         }
395         ip->i_flag |= IN_CHANGE | IN_UPDATE;
396         allerror = ffs_update(vp, 1);
397         
398         /*
399          * Having written the new inode to disk, save its new configuration
400          * and put back the old block pointers long enough to process them.
401          * Note that we save the new block configuration so we can check it
402          * when we are done.
403          */
404         for (i = 0; i < NDADDR; i++) {
405                 newblks[i] = DIP(ip, i_db[i]);
406                 DIP_SET(ip, i_db[i], oldblks[i]);
407         }
408         for (i = 0; i < NIADDR; i++) {
409                 newblks[NDADDR + i] = DIP(ip, i_ib[i]);
410                 DIP_SET(ip, i_ib[i], oldblks[NDADDR + i]);
411         }
412         ip->i_size = osize;
413         DIP_SET(ip, i_size, osize);
414
415         error = vtruncbuf(vp, cred, td, length, fs->fs_bsize);
416         if (error && (allerror == 0))
417                 allerror = error;
418
419         /*
420          * Indirect blocks first.
421          */
422         indir_lbn[SINGLE] = -NDADDR;
423         indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
424         indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
425         for (level = TRIPLE; level >= SINGLE; level--) {
426                 bn = DIP(ip, i_ib[level]);
427                 if (bn != 0) {
428                         error = ffs_indirtrunc(ip, indir_lbn[level],
429                             fsbtodb(fs, bn), lastiblock[level], level, &count);
430                         if (error)
431                                 allerror = error;
432                         blocksreleased += count;
433                         if (lastiblock[level] < 0) {
434                                 DIP_SET(ip, i_ib[level], 0);
435                                 ffs_blkfree(ump, fs, ip->i_devvp, bn,
436                                     fs->fs_bsize, ip->i_number);
437                                 blocksreleased += nblocks;
438                         }
439                 }
440                 if (lastiblock[level] >= 0)
441                         goto done;
442         }
443
444         /*
445          * All whole direct blocks or frags.
446          */
447         for (i = NDADDR - 1; i > lastblock; i--) {
448                 long bsize;
449
450                 bn = DIP(ip, i_db[i]);
451                 if (bn == 0)
452                         continue;
453                 DIP_SET(ip, i_db[i], 0);
454                 bsize = blksize(fs, ip, i);
455                 ffs_blkfree(ump, fs, ip->i_devvp, bn, bsize, ip->i_number);
456                 blocksreleased += btodb(bsize);
457         }
458         if (lastblock < 0)
459                 goto done;
460
461         /*
462          * Finally, look for a change in size of the
463          * last direct block; release any frags.
464          */
465         bn = DIP(ip, i_db[lastblock]);
466         if (bn != 0) {
467                 long oldspace, newspace;
468
469                 /*
470                  * Calculate amount of space we're giving
471                  * back as old block size minus new block size.
472                  */
473                 oldspace = blksize(fs, ip, lastblock);
474                 ip->i_size = length;
475                 DIP_SET(ip, i_size, length);
476                 newspace = blksize(fs, ip, lastblock);
477                 if (newspace == 0)
478                         panic("ffs_truncate: newspace");
479                 if (oldspace - newspace > 0) {
480                         /*
481                          * Block number of space to be free'd is
482                          * the old block # plus the number of frags
483                          * required for the storage we're keeping.
484                          */
485                         bn += numfrags(fs, newspace);
486                         ffs_blkfree(ump, fs, ip->i_devvp, bn,
487                             oldspace - newspace, ip->i_number);
488                         blocksreleased += btodb(oldspace - newspace);
489                 }
490         }
491 done:
492 #ifdef INVARIANTS
493         for (level = SINGLE; level <= TRIPLE; level++)
494                 if (newblks[NDADDR + level] != DIP(ip, i_ib[level]))
495                         panic("ffs_truncate1");
496         for (i = 0; i < NDADDR; i++)
497                 if (newblks[i] != DIP(ip, i_db[i]))
498                         panic("ffs_truncate2");
499         BO_LOCK(bo);
500         if (length == 0 &&
501             (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
502             (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
503                 panic("ffs_truncate3");
504         BO_UNLOCK(bo);
505 #endif /* INVARIANTS */
506         /*
507          * Put back the real size.
508          */
509         ip->i_size = length;
510         DIP_SET(ip, i_size, length);
511         DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
512
513         if (DIP(ip, i_blocks) < 0)                      /* sanity */
514                 DIP_SET(ip, i_blocks, 0);
515         ip->i_flag |= IN_CHANGE;
516 #ifdef QUOTA
517         (void) chkdq(ip, -blocksreleased, NOCRED, 0);
518 #endif
519         return (allerror);
520 }
521
522 /*
523  * Release blocks associated with the inode ip and stored in the indirect
524  * block bn.  Blocks are free'd in LIFO order up to (but not including)
525  * lastbn.  If level is greater than SINGLE, the block is an indirect block
526  * and recursive calls to indirtrunc must be used to cleanse other indirect
527  * blocks.
528  */
529 static int
530 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
531         struct inode *ip;
532         ufs2_daddr_t lbn, lastbn;
533         ufs2_daddr_t dbn;
534         int level;
535         ufs2_daddr_t *countp;
536 {
537         struct buf *bp;
538         struct fs *fs = ip->i_fs;
539         struct vnode *vp;
540         caddr_t copy = NULL;
541         int i, nblocks, error = 0, allerror = 0;
542         ufs2_daddr_t nb, nlbn, last;
543         ufs2_daddr_t blkcount, factor, blocksreleased = 0;
544         ufs1_daddr_t *bap1 = NULL;
545         ufs2_daddr_t *bap2 = NULL;
546 #       define BAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? bap1[i] : bap2[i])
547
548         /*
549          * Calculate index in current block of last
550          * block to be kept.  -1 indicates the entire
551          * block so we need not calculate the index.
552          */
553         factor = 1;
554         for (i = SINGLE; i < level; i++)
555                 factor *= NINDIR(fs);
556         last = lastbn;
557         if (lastbn > 0)
558                 last /= factor;
559         nblocks = btodb(fs->fs_bsize);
560         /*
561          * Get buffer of block pointers, zero those entries corresponding
562          * to blocks to be free'd, and update on disk copy first.  Since
563          * double(triple) indirect before single(double) indirect, calls
564          * to bmap on these blocks will fail.  However, we already have
565          * the on disk address, so we have to set the b_blkno field
566          * explicitly instead of letting bread do everything for us.
567          */
568         vp = ITOV(ip);
569         bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, 0);
570         if ((bp->b_flags & B_CACHE) == 0) {
571                 curthread->td_ru.ru_inblock++;  /* pay for read */
572                 bp->b_iocmd = BIO_READ;
573                 bp->b_flags &= ~B_INVAL;
574                 bp->b_ioflags &= ~BIO_ERROR;
575                 if (bp->b_bcount > bp->b_bufsize)
576                         panic("ffs_indirtrunc: bad buffer size");
577                 bp->b_blkno = dbn;
578                 vfs_busy_pages(bp, 0);
579                 bp->b_iooffset = dbtob(bp->b_blkno);
580                 bstrategy(bp);
581                 error = bufwait(bp);
582         }
583         if (error) {
584                 brelse(bp);
585                 *countp = 0;
586                 return (error);
587         }
588
589         if (ip->i_ump->um_fstype == UFS1)
590                 bap1 = (ufs1_daddr_t *)bp->b_data;
591         else
592                 bap2 = (ufs2_daddr_t *)bp->b_data;
593         if (lastbn != -1) {
594                 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
595                 bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
596                 for (i = last + 1; i < NINDIR(fs); i++)
597                         if (ip->i_ump->um_fstype == UFS1)
598                                 bap1[i] = 0;
599                         else
600                                 bap2[i] = 0;
601                 if (DOINGASYNC(vp)) {
602                         bawrite(bp);
603                 } else {
604                         error = bwrite(bp);
605                         if (error)
606                                 allerror = error;
607                 }
608                 if (ip->i_ump->um_fstype == UFS1)
609                         bap1 = (ufs1_daddr_t *)copy;
610                 else
611                         bap2 = (ufs2_daddr_t *)copy;
612         }
613
614         /*
615          * Recursively free totally unused blocks.
616          */
617         for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
618             i--, nlbn += factor) {
619                 nb = BAP(ip, i);
620                 if (nb == 0)
621                         continue;
622                 if (level > SINGLE) {
623                         if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
624                             (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
625                                 allerror = error;
626                         blocksreleased += blkcount;
627                 }
628                 ffs_blkfree(ip->i_ump, fs, ip->i_devvp, nb, fs->fs_bsize,
629                     ip->i_number);
630                 blocksreleased += nblocks;
631         }
632
633         /*
634          * Recursively free last partial block.
635          */
636         if (level > SINGLE && lastbn >= 0) {
637                 last = lastbn % factor;
638                 nb = BAP(ip, i);
639                 if (nb != 0) {
640                         error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
641                             last, level - 1, &blkcount);
642                         if (error)
643                                 allerror = error;
644                         blocksreleased += blkcount;
645                 }
646         }
647         if (copy != NULL) {
648                 free(copy, M_TEMP);
649         } else {
650                 bp->b_flags |= B_INVAL | B_NOCACHE;
651                 brelse(bp);
652         }
653
654         *countp = blocksreleased;
655         return (allerror);
656 }
657
658 int
659 ffs_rdonly(struct inode *ip)
660 {
661
662         return (ip->i_ump->um_fs->fs_ronly != 0);
663 }
664