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