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