1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
32 */
33
34 #include <sys/cdefs.h>
35 #include "opt_ufs.h"
36 #include "opt_quota.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bio.h>
41 #include <sys/buf.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/proc.h>
45 #include <sys/racct.h>
46 #include <sys/random.h>
47 #include <sys/resourcevar.h>
48 #include <sys/rwlock.h>
49 #include <sys/stat.h>
50 #include <sys/vmmeter.h>
51 #include <sys/vnode.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_object.h>
56
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/ufsmount.h>
60 #include <ufs/ufs/inode.h>
61 #include <ufs/ufs/dir.h>
62 #ifdef UFS_DIRHASH
63 #include <ufs/ufs/dirhash.h>
64 #endif
65 #include <ufs/ufs/ufs_extern.h>
66
67 #include <ufs/ffs/fs.h>
68 #include <ufs/ffs/ffs_extern.h>
69
70 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
71 ufs2_daddr_t, int, ufs2_daddr_t *);
72
73 static void
ffs_inode_bwrite(struct vnode * vp,struct buf * bp,int flags)74 ffs_inode_bwrite(struct vnode *vp, struct buf *bp, int flags)
75 {
76 if ((flags & IO_SYNC) != 0)
77 bwrite(bp);
78 else if (DOINGASYNC(vp))
79 bdwrite(bp);
80 else
81 bawrite(bp);
82 }
83
84 /*
85 * Update the access, modified, and inode change times as specified by the
86 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
87 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
88 * the timestamp update). The IN_LAZYMOD flag is set to force a write
89 * later if not now. The IN_LAZYACCESS is set instead of IN_MODIFIED if the fs
90 * is currently being suspended (or is suspended) and vnode has been accessed.
91 * If we write now, then clear IN_MODIFIED, IN_LAZYACCESS and IN_LAZYMOD to
92 * reflect the presumably successful write, and if waitfor is set, then wait
93 * for the write to complete.
94 */
95 int
ffs_update(struct vnode * vp,int waitfor)96 ffs_update(struct vnode *vp, int waitfor)
97 {
98 struct fs *fs;
99 struct buf *bp;
100 struct inode *ip;
101 daddr_t bn;
102 int flags, error;
103
104 ASSERT_VOP_ELOCKED(vp, "ffs_update");
105 ufs_itimes(vp);
106 ip = VTOI(vp);
107 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
108 return (0);
109 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
110 /*
111 * The IN_SIZEMOD and IN_IBLKDATA flags indicate changes to the
112 * file size and block pointer fields in the inode. When these
113 * fields have been changed, the fsync() and fsyncdata() system
114 * calls must write the inode to ensure their semantics that the
115 * file is on stable store.
116 *
117 * The IN_SIZEMOD and IN_IBLKDATA flags cannot be cleared until
118 * a synchronous write of the inode is done. If they are cleared
119 * on an asynchronous write, then the inode may not yet have been
120 * written to the disk when an fsync() or fsyncdata() call is done.
121 * Absent these flags, these calls would not know that they needed
122 * to write the inode. Thus, these flags only can be cleared on
123 * synchronous writes of the inode. Since the inode will be locked
124 * for the duration of the I/O that writes it to disk, no fsync()
125 * or fsyncdata() will be able to run before the on-disk inode
126 * is complete.
127 */
128 if (waitfor)
129 ip->i_flag &= ~(IN_SIZEMOD | IN_IBLKDATA);
130 fs = ITOFS(ip);
131 if (fs->fs_ronly)
132 return (0);
133 /*
134 * If we are updating a snapshot and another process is currently
135 * writing the buffer containing the inode for this snapshot then
136 * a deadlock can occur when it tries to check the snapshot to see
137 * if that block needs to be copied. Thus when updating a snapshot
138 * we check to see if the buffer is already locked, and if it is
139 * we drop the snapshot lock until the buffer has been written
140 * and is available to us. We have to grab a reference to the
141 * snapshot vnode to prevent it from being removed while we are
142 * waiting for the buffer.
143 */
144 loop:
145 flags = 0;
146 if (IS_SNAPSHOT(ip))
147 flags = GB_LOCK_NOWAIT;
148 bn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
149 error = ffs_breadz(VFSTOUFS(vp->v_mount), ITODEVVP(ip), bn, bn,
150 (int) fs->fs_bsize, NULL, NULL, 0, NOCRED, flags, NULL, &bp);
151 if (error != 0) {
152 /*
153 * If EBUSY was returned without GB_LOCK_NOWAIT (which
154 * requests trylock for buffer lock), it is for some
155 * other reason and we should not handle it specially.
156 */
157 if (error != EBUSY || (flags & GB_LOCK_NOWAIT) == 0)
158 return (error);
159
160 /*
161 * Wait for our inode block to become available.
162 *
163 * Hold a reference to the vnode to protect against
164 * ffs_snapgone(). Since we hold a reference, it can only
165 * get reclaimed (VIRF_DOOMED flag) in a forcible downgrade
166 * or unmount. For an unmount, the entire filesystem will be
167 * gone, so we cannot attempt to touch anything associated
168 * with it while the vnode is unlocked; all we can do is
169 * pause briefly and try again. If when we relock the vnode
170 * we discover that it has been reclaimed, updating it is no
171 * longer necessary and we can just return an error.
172 */
173 vref(vp);
174 VOP_UNLOCK(vp);
175 pause("ffsupd", 1);
176 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
177 vrele(vp);
178 if (!IS_UFS(vp))
179 return (ENOENT);
180
181 /*
182 * Recalculate flags, because the vnode was relocked and
183 * could no longer be a snapshot.
184 */
185 goto loop;
186 }
187 if (DOINGSOFTDEP(vp))
188 softdep_update_inodeblock(ip, bp, waitfor);
189 else if (ip->i_effnlink != ip->i_nlink)
190 panic("ffs_update: bad link cnt");
191 if (I_IS_UFS1(ip)) {
192 *((struct ufs1_dinode *)bp->b_data +
193 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
194 /*
195 * XXX: FIX? The entropy here is desirable,
196 * but the harvesting may be expensive
197 */
198 random_harvest_queue(&(ip->i_din1), sizeof(ip->i_din1), RANDOM_FS_ATIME);
199 } else {
200 ffs_update_dinode_ckhash(fs, ip->i_din2);
201 *((struct ufs2_dinode *)bp->b_data +
202 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
203 /*
204 * XXX: FIX? The entropy here is desirable,
205 * but the harvesting may be expensive
206 */
207 random_harvest_queue(&(ip->i_din2), sizeof(ip->i_din2), RANDOM_FS_ATIME);
208 }
209 if (waitfor) {
210 error = bwrite(bp);
211 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
212 error = 0;
213 } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
214 bawrite(bp);
215 error = 0;
216 } else {
217 if (bp->b_bufsize == fs->fs_bsize)
218 bp->b_flags |= B_CLUSTEROK;
219 bdwrite(bp);
220 error = 0;
221 }
222 return (error);
223 }
224
225 #define SINGLE 0 /* index of single indirect block */
226 #define DOUBLE 1 /* index of double indirect block */
227 #define TRIPLE 2 /* index of triple indirect block */
228 /*
229 * Truncate the inode ip to at most length size, freeing the
230 * disk blocks.
231 */
232 int
ffs_truncate(struct vnode * vp,off_t length,int flags,struct ucred * cred)233 ffs_truncate(struct vnode *vp,
234 off_t length,
235 int flags,
236 struct ucred *cred)
237 {
238 struct inode *ip;
239 ufs2_daddr_t bn, lbn, lastblock, lastiblock[UFS_NIADDR];
240 ufs2_daddr_t indir_lbn[UFS_NIADDR], oldblks[UFS_NDADDR + UFS_NIADDR];
241 #ifdef INVARIANTS
242 ufs2_daddr_t newblks[UFS_NDADDR + UFS_NIADDR];
243 #endif
244 ufs2_daddr_t count, blocksreleased = 0, blkno;
245 struct bufobj *bo __diagused;
246 struct fs *fs;
247 struct buf *bp;
248 struct ufsmount *ump;
249 int softdeptrunc, journaltrunc;
250 int needextclean, extblocks;
251 int offset, size, level, nblocks;
252 int i, error, allerror, indiroff, waitforupdate;
253 uint64_t key;
254 off_t osize;
255
256 ip = VTOI(vp);
257 ump = VFSTOUFS(vp->v_mount);
258 fs = ump->um_fs;
259 bo = &vp->v_bufobj;
260
261 ASSERT_VOP_LOCKED(vp, "ffs_truncate");
262
263 if (length < 0)
264 return (EINVAL);
265 if (length > fs->fs_maxfilesize)
266 return (EFBIG);
267 #ifdef QUOTA
268 error = getinoquota(ip);
269 if (error)
270 return (error);
271 #endif
272 /*
273 * Historically clients did not have to specify which data
274 * they were truncating. So, if not specified, we assume
275 * traditional behavior, e.g., just the normal data.
276 */
277 if ((flags & (IO_EXT | IO_NORMAL)) == 0)
278 flags |= IO_NORMAL;
279 if (!DOINGSOFTDEP(vp) && !DOINGASYNC(vp))
280 flags |= IO_SYNC;
281 waitforupdate = (flags & IO_SYNC) != 0 || !DOINGASYNC(vp);
282 /*
283 * If we are truncating the extended-attributes, and cannot
284 * do it with soft updates, then do it slowly here. If we are
285 * truncating both the extended attributes and the file contents
286 * (e.g., the file is being unlinked), then pick it off with
287 * soft updates below.
288 */
289 allerror = 0;
290 needextclean = 0;
291 softdeptrunc = 0;
292 journaltrunc = DOINGSUJ(vp);
293 journaltrunc = 0; /* XXX temp patch until bug found */
294 if (journaltrunc == 0 && DOINGSOFTDEP(vp) && length == 0)
295 softdeptrunc = !softdep_slowdown(vp);
296 extblocks = 0;
297 if (fs->fs_magic == FS_UFS2_MAGIC && ip->i_din2->di_extsize > 0) {
298 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
299 }
300 if ((flags & IO_EXT) && extblocks > 0) {
301 if (length != 0)
302 panic("ffs_truncate: partial trunc of extdata");
303 if (softdeptrunc || journaltrunc) {
304 if ((flags & IO_NORMAL) == 0)
305 goto extclean;
306 needextclean = 1;
307 } else {
308 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
309 return (error);
310 #ifdef QUOTA
311 (void) chkdq(ip, -extblocks, NOCRED, FORCE);
312 #endif
313 vinvalbuf(vp, V_ALT, 0, 0);
314 vn_pages_remove(vp,
315 OFF_TO_IDX(lblktosize(fs, -extblocks)), 0);
316 osize = ip->i_din2->di_extsize;
317 ip->i_din2->di_blocks -= extblocks;
318 ip->i_din2->di_extsize = 0;
319 for (i = 0; i < UFS_NXADDR; i++) {
320 oldblks[i] = ip->i_din2->di_extb[i];
321 ip->i_din2->di_extb[i] = 0;
322 }
323 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
324 if ((error = ffs_update(vp, waitforupdate)))
325 return (error);
326 for (i = 0; i < UFS_NXADDR; i++) {
327 if (oldblks[i] == 0)
328 continue;
329 ffs_blkfree(ump, fs, ITODEVVP(ip), oldblks[i],
330 sblksize(fs, osize, i), ip->i_number,
331 vp->v_type, NULL, SINGLETON_KEY);
332 }
333 }
334 }
335 if ((flags & IO_NORMAL) == 0)
336 return (0);
337 if (vp->v_type == VLNK && ip->i_size < ump->um_maxsymlinklen) {
338 #ifdef INVARIANTS
339 if (length != 0)
340 panic("ffs_truncate: partial truncate of symlink");
341 #endif
342 bzero(DIP(ip, i_shortlink), (uint64_t)ip->i_size);
343 ip->i_size = 0;
344 DIP_SET(ip, i_size, 0);
345 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
346 if (needextclean)
347 goto extclean;
348 return (ffs_update(vp, waitforupdate));
349 }
350 if (ip->i_size == length) {
351 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
352 if (needextclean)
353 goto extclean;
354 return (ffs_update(vp, 0));
355 }
356 if (fs->fs_ronly)
357 panic("ffs_truncate: read-only filesystem");
358 if (IS_SNAPSHOT(ip))
359 ffs_snapremove(vp);
360 cluster_init_vn(&ip->i_clusterw);
361 osize = ip->i_size;
362 /*
363 * Lengthen the size of the file. We must ensure that the
364 * last byte of the file is allocated. Since the smallest
365 * value of osize is 0, length will be at least 1.
366 */
367 if (osize < length) {
368 vnode_pager_setsize(vp, length);
369 flags |= BA_CLRBUF;
370 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
371 if (error) {
372 vnode_pager_setsize(vp, osize);
373 return (error);
374 }
375 ip->i_size = length;
376 DIP_SET(ip, i_size, length);
377 if (bp->b_bufsize == fs->fs_bsize)
378 bp->b_flags |= B_CLUSTEROK;
379 ffs_inode_bwrite(vp, bp, flags);
380 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
381 return (ffs_update(vp, waitforupdate));
382 }
383 /*
384 * Lookup block number for a given offset. Zero length files
385 * have no blocks, so return a blkno of -1.
386 */
387 lbn = lblkno(fs, length - 1);
388 if (length == 0) {
389 blkno = -1;
390 } else if (lbn < UFS_NDADDR) {
391 blkno = DIP(ip, i_db[lbn]);
392 } else {
393 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn), fs->fs_bsize,
394 cred, BA_METAONLY, &bp);
395 if (error)
396 return (error);
397 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
398 if (I_IS_UFS1(ip))
399 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
400 else
401 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
402 /*
403 * If the block number is non-zero, then the indirect block
404 * must have been previously allocated and need not be written.
405 * If the block number is zero, then we may have allocated
406 * the indirect block and hence need to write it out.
407 */
408 if (blkno != 0)
409 brelse(bp);
410 else if (flags & IO_SYNC)
411 bwrite(bp);
412 else
413 bdwrite(bp);
414 }
415 /*
416 * If the block number at the new end of the file is zero,
417 * then we must allocate it to ensure that the last block of
418 * the file is allocated. Soft updates does not handle this
419 * case, so here we have to clean up the soft updates data
420 * structures describing the allocation past the truncation
421 * point. Finding and deallocating those structures is a lot of
422 * work. Since partial truncation with a hole at the end occurs
423 * rarely, we solve the problem by syncing the file so that it
424 * will have no soft updates data structures left.
425 */
426 if (blkno == 0 && (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
427 return (error);
428 if (blkno != 0 && DOINGSOFTDEP(vp)) {
429 if (softdeptrunc == 0 && journaltrunc == 0) {
430 /*
431 * If soft updates cannot handle this truncation,
432 * clean up soft dependency data structures and
433 * fall through to the synchronous truncation.
434 */
435 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
436 return (error);
437 } else {
438 flags = IO_NORMAL | (needextclean ? IO_EXT: 0);
439 if (journaltrunc)
440 softdep_journal_freeblocks(ip, cred, length,
441 flags);
442 else
443 softdep_setup_freeblocks(ip, length, flags);
444 ASSERT_VOP_LOCKED(vp, "ffs_truncate1");
445 if (journaltrunc == 0) {
446 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
447 error = ffs_update(vp, 0);
448 }
449 return (error);
450 }
451 }
452 /*
453 * Shorten the size of the file. If the last block of the
454 * shortened file is unallocated, we must allocate it.
455 * Additionally, if the file is not being truncated to a
456 * block boundary, the contents of the partial block
457 * following the end of the file must be zero'ed in
458 * case it ever becomes accessible again because of
459 * subsequent file growth. Directories however are not
460 * zero'ed as they should grow back initialized to empty.
461 */
462 offset = blkoff(fs, length);
463 if (blkno != 0 && offset == 0) {
464 ip->i_size = length;
465 DIP_SET(ip, i_size, length);
466 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
467 #ifdef UFS_DIRHASH
468 if (vp->v_type == VDIR && ip->i_dirhash != NULL)
469 ufsdirhash_dirtrunc(ip, length);
470 #endif
471 } else {
472 lbn = lblkno(fs, length);
473 flags |= BA_CLRBUF;
474 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
475 if (error)
476 return (error);
477 ffs_inode_bwrite(vp, bp, flags);
478
479 /*
480 * When we are doing soft updates and the UFS_BALLOC
481 * above fills in a direct block hole with a full sized
482 * block that will be truncated down to a fragment below,
483 * we must flush out the block dependency with an FSYNC
484 * so that we do not get a soft updates inconsistency
485 * when we create the fragment below.
486 */
487 if (DOINGSOFTDEP(vp) && lbn < UFS_NDADDR &&
488 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
489 (error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
490 return (error);
491
492 error = UFS_BALLOC(vp, length - 1, 1, cred, flags, &bp);
493 if (error)
494 return (error);
495 ip->i_size = length;
496 DIP_SET(ip, i_size, length);
497 #ifdef UFS_DIRHASH
498 if (vp->v_type == VDIR && ip->i_dirhash != NULL)
499 ufsdirhash_dirtrunc(ip, length);
500 #endif
501 size = blksize(fs, ip, lbn);
502 if (vp->v_type != VDIR && offset != 0)
503 bzero((char *)bp->b_data + offset,
504 (uint64_t)(size - offset));
505 /* Kirk's code has reallocbuf(bp, size, 1) here */
506 allocbuf(bp, size);
507 if (bp->b_bufsize == fs->fs_bsize)
508 bp->b_flags |= B_CLUSTEROK;
509 ffs_inode_bwrite(vp, bp, flags);
510 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
511 }
512 /*
513 * Calculate index into inode's block list of
514 * last direct and indirect blocks (if any)
515 * which we want to keep. Lastblock is -1 when
516 * the file is truncated to 0.
517 */
518 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
519 lastiblock[SINGLE] = lastblock - UFS_NDADDR;
520 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
521 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
522 nblocks = btodb(fs->fs_bsize);
523 /*
524 * Update file and block pointers on disk before we start freeing
525 * blocks. If we crash before free'ing blocks below, the blocks
526 * will be returned to the free list. lastiblock values are also
527 * normalized to -1 for calls to ffs_indirtrunc below.
528 */
529 for (level = TRIPLE; level >= SINGLE; level--) {
530 oldblks[UFS_NDADDR + level] = DIP(ip, i_ib[level]);
531 if (lastiblock[level] < 0) {
532 DIP_SET(ip, i_ib[level], 0);
533 lastiblock[level] = -1;
534 }
535 }
536 for (i = 0; i < UFS_NDADDR; i++) {
537 oldblks[i] = DIP(ip, i_db[i]);
538 if (i > lastblock)
539 DIP_SET(ip, i_db[i], 0);
540 }
541 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
542 allerror = ffs_update(vp, waitforupdate);
543
544 /*
545 * Having written the new inode to disk, save its new configuration
546 * and put back the old block pointers long enough to process them.
547 * Note that we save the new block configuration so we can check it
548 * when we are done.
549 */
550 for (i = 0; i < UFS_NDADDR; i++) {
551 #ifdef INVARIANTS
552 newblks[i] = DIP(ip, i_db[i]);
553 #endif
554 DIP_SET(ip, i_db[i], oldblks[i]);
555 }
556 for (i = 0; i < UFS_NIADDR; i++) {
557 #ifdef INVARIANTS
558 newblks[UFS_NDADDR + i] = DIP(ip, i_ib[i]);
559 #endif
560 DIP_SET(ip, i_ib[i], oldblks[UFS_NDADDR + i]);
561 }
562 ip->i_size = osize;
563 DIP_SET(ip, i_size, osize);
564 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
565
566 error = vtruncbuf(vp, length, fs->fs_bsize);
567 if (error && (allerror == 0))
568 allerror = error;
569
570 /*
571 * Indirect blocks first.
572 */
573 indir_lbn[SINGLE] = -UFS_NDADDR;
574 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
575 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
576 for (level = TRIPLE; level >= SINGLE; level--) {
577 bn = DIP(ip, i_ib[level]);
578 if (bn != 0) {
579 error = ffs_indirtrunc(ip, indir_lbn[level],
580 fsbtodb(fs, bn), lastiblock[level], level, &count);
581 if (error)
582 allerror = error;
583 blocksreleased += count;
584 if (lastiblock[level] < 0) {
585 DIP_SET(ip, i_ib[level], 0);
586 ffs_blkfree(ump, fs, ump->um_devvp, bn,
587 fs->fs_bsize, ip->i_number,
588 vp->v_type, NULL, SINGLETON_KEY);
589 blocksreleased += nblocks;
590 }
591 }
592 if (lastiblock[level] >= 0)
593 goto done;
594 }
595
596 /*
597 * All whole direct blocks or frags.
598 */
599 key = ffs_blkrelease_start(ump, ump->um_devvp, ip->i_number);
600 for (i = UFS_NDADDR - 1; i > lastblock; i--) {
601 long bsize;
602
603 bn = DIP(ip, i_db[i]);
604 if (bn == 0)
605 continue;
606 DIP_SET(ip, i_db[i], 0);
607 bsize = blksize(fs, ip, i);
608 ffs_blkfree(ump, fs, ump->um_devvp, bn, bsize, ip->i_number,
609 vp->v_type, NULL, key);
610 blocksreleased += btodb(bsize);
611 }
612 ffs_blkrelease_finish(ump, key);
613 if (lastblock < 0)
614 goto done;
615
616 /*
617 * Finally, look for a change in size of the
618 * last direct block; release any frags.
619 */
620 bn = DIP(ip, i_db[lastblock]);
621 if (bn != 0) {
622 long oldspace, newspace;
623
624 /*
625 * Calculate amount of space we're giving
626 * back as old block size minus new block size.
627 */
628 oldspace = blksize(fs, ip, lastblock);
629 ip->i_size = length;
630 DIP_SET(ip, i_size, length);
631 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
632 newspace = blksize(fs, ip, lastblock);
633 if (newspace == 0)
634 panic("ffs_truncate: newspace");
635 if (oldspace - newspace > 0) {
636 /*
637 * Block number of space to be free'd is
638 * the old block # plus the number of frags
639 * required for the storage we're keeping.
640 */
641 bn += numfrags(fs, newspace);
642 ffs_blkfree(ump, fs, ump->um_devvp, bn,
643 oldspace - newspace, ip->i_number, vp->v_type,
644 NULL, SINGLETON_KEY);
645 blocksreleased += btodb(oldspace - newspace);
646 }
647 }
648 done:
649 #ifdef INVARIANTS
650 for (level = SINGLE; level <= TRIPLE; level++)
651 if (newblks[UFS_NDADDR + level] != DIP(ip, i_ib[level]))
652 panic("ffs_truncate1: level %d newblks %jd != i_ib %jd",
653 level, (intmax_t)newblks[UFS_NDADDR + level],
654 (intmax_t)DIP(ip, i_ib[level]));
655 for (i = 0; i < UFS_NDADDR; i++)
656 if (newblks[i] != DIP(ip, i_db[i]))
657 panic("ffs_truncate2: blkno %d newblks %jd != i_db %jd",
658 i, (intmax_t)newblks[UFS_NDADDR + level],
659 (intmax_t)DIP(ip, i_ib[level]));
660 BO_LOCK(bo);
661 if (length == 0 &&
662 (fs->fs_magic != FS_UFS2_MAGIC || ip->i_din2->di_extsize == 0) &&
663 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
664 panic("ffs_truncate3: vp = %p, buffers: dirty = %d, clean = %d",
665 vp, bo->bo_dirty.bv_cnt, bo->bo_clean.bv_cnt);
666 BO_UNLOCK(bo);
667 #endif /* INVARIANTS */
668 /*
669 * Put back the real size.
670 */
671 ip->i_size = length;
672 DIP_SET(ip, i_size, length);
673 if (DIP(ip, i_blocks) >= blocksreleased)
674 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - blocksreleased);
675 else /* sanity */
676 DIP_SET(ip, i_blocks, 0);
677 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
678 #ifdef QUOTA
679 (void) chkdq(ip, -blocksreleased, NOCRED, FORCE);
680 #endif
681 return (allerror);
682
683 extclean:
684 if (journaltrunc)
685 softdep_journal_freeblocks(ip, cred, length, IO_EXT);
686 else
687 softdep_setup_freeblocks(ip, length, IO_EXT);
688 return (ffs_update(vp, waitforupdate));
689 }
690
691 /*
692 * Release blocks associated with the inode ip and stored in the indirect
693 * block bn. Blocks are free'd in LIFO order up to (but not including)
694 * lastbn. If level is greater than SINGLE, the block is an indirect block
695 * and recursive calls to indirtrunc must be used to cleanse other indirect
696 * blocks.
697 */
698 static int
ffs_indirtrunc(struct inode * ip,ufs2_daddr_t lbn,ufs2_daddr_t dbn,ufs2_daddr_t lastbn,int level,ufs2_daddr_t * countp)699 ffs_indirtrunc(struct inode *ip,
700 ufs2_daddr_t lbn,
701 ufs2_daddr_t dbn,
702 ufs2_daddr_t lastbn,
703 int level,
704 ufs2_daddr_t *countp)
705 {
706 struct buf *bp;
707 struct fs *fs;
708 struct ufsmount *ump;
709 struct vnode *vp;
710 caddr_t copy = NULL;
711 uint64_t key;
712 int i, nblocks, error = 0, allerror = 0;
713 ufs2_daddr_t nb, nlbn, last;
714 ufs2_daddr_t blkcount, factor, blocksreleased = 0;
715 ufs1_daddr_t *bap1 = NULL;
716 ufs2_daddr_t *bap2 = NULL;
717 #define BAP(ip, i) (I_IS_UFS1(ip) ? bap1[i] : bap2[i])
718
719 fs = ITOFS(ip);
720 ump = ITOUMP(ip);
721
722 /*
723 * Calculate index in current block of last
724 * block to be kept. -1 indicates the entire
725 * block so we need not calculate the index.
726 */
727 factor = lbn_offset(fs, level);
728 last = lastbn;
729 if (lastbn > 0)
730 last /= factor;
731 nblocks = btodb(fs->fs_bsize);
732 /*
733 * Get buffer of block pointers, zero those entries corresponding
734 * to blocks to be free'd, and update on disk copy first. Since
735 * double(triple) indirect before single(double) indirect, calls
736 * to VOP_BMAP() on these blocks will fail. However, we already
737 * have the on-disk address, so we just pass it to bread() instead
738 * of having bread() attempt to calculate it using VOP_BMAP().
739 */
740 vp = ITOV(ip);
741 error = ffs_breadz(ump, vp, lbn, dbn, (int)fs->fs_bsize, NULL, NULL, 0,
742 NOCRED, 0, NULL, &bp);
743 if (error) {
744 *countp = 0;
745 return (error);
746 }
747
748 if (I_IS_UFS1(ip))
749 bap1 = (ufs1_daddr_t *)bp->b_data;
750 else
751 bap2 = (ufs2_daddr_t *)bp->b_data;
752 if (lastbn != -1) {
753 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
754 bcopy((caddr_t)bp->b_data, copy, (uint64_t)fs->fs_bsize);
755 for (i = last + 1; i < NINDIR(fs); i++)
756 if (I_IS_UFS1(ip))
757 bap1[i] = 0;
758 else
759 bap2[i] = 0;
760 if (DOINGASYNC(vp)) {
761 bdwrite(bp);
762 } else {
763 error = bwrite(bp);
764 if (error)
765 allerror = error;
766 }
767 if (I_IS_UFS1(ip))
768 bap1 = (ufs1_daddr_t *)copy;
769 else
770 bap2 = (ufs2_daddr_t *)copy;
771 }
772
773 /*
774 * Recursively free totally unused blocks.
775 */
776 key = ffs_blkrelease_start(ump, ITODEVVP(ip), ip->i_number);
777 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
778 i--, nlbn += factor) {
779 nb = BAP(ip, i);
780 if (nb == 0)
781 continue;
782 if (level > SINGLE) {
783 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
784 (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
785 allerror = error;
786 blocksreleased += blkcount;
787 }
788 ffs_blkfree(ump, fs, ITODEVVP(ip), nb, fs->fs_bsize,
789 ip->i_number, vp->v_type, NULL, key);
790 blocksreleased += nblocks;
791 }
792 ffs_blkrelease_finish(ump, key);
793
794 /*
795 * Recursively free last partial block.
796 */
797 if (level > SINGLE && lastbn >= 0) {
798 last = lastbn % factor;
799 nb = BAP(ip, i);
800 if (nb != 0) {
801 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
802 last, level - 1, &blkcount);
803 if (error)
804 allerror = error;
805 blocksreleased += blkcount;
806 }
807 }
808 if (copy != NULL) {
809 free(copy, M_TEMP);
810 } else {
811 bp->b_flags |= B_INVAL | B_NOCACHE;
812 brelse(bp);
813 }
814
815 *countp = blocksreleased;
816 return (allerror);
817 }
818
819 int
ffs_rdonly(struct inode * ip)820 ffs_rdonly(struct inode *ip)
821 {
822
823 return (ITOFS(ip)->fs_ronly != 0);
824 }
825