1 /* $NetBSD: ffs_inode.c,v 1.116 2013/10/20 00:29:10 htodd Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Wasabi Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.116 2013/10/20 00:29:10 htodd Exp $"); 65 66 #if defined(_KERNEL_OPT) 67 #include "opt_ffs.h" 68 #include "opt_quota.h" 69 #endif 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/buf.h> 74 #include <sys/file.h> 75 #include <sys/fstrans.h> 76 #include <sys/kauth.h> 77 #include <sys/kernel.h> 78 #include <sys/kmem.h> 79 #include <sys/mount.h> 80 #include <sys/proc.h> 81 #include <sys/resourcevar.h> 82 #include <sys/trace.h> 83 #include <sys/vnode.h> 84 #include <sys/wapbl.h> 85 86 #include <ufs/ufs/quota.h> 87 #include <ufs/ufs/inode.h> 88 #include <ufs/ufs/ufsmount.h> 89 #include <ufs/ufs/ufs_extern.h> 90 #include <ufs/ufs/ufs_bswap.h> 91 #include <ufs/ufs/ufs_wapbl.h> 92 93 #include <ufs/ffs/fs.h> 94 #include <ufs/ffs/ffs_extern.h> 95 96 static int ffs_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int, 97 int64_t *); 98 99 /* 100 * Update the access, modified, and inode change times as specified 101 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. 102 * The IN_MODIFIED flag is used to specify that the inode needs to be 103 * updated but that the times have already been set. The access 104 * and modified times are taken from the second and third parameters; 105 * the inode change time is always taken from the current time. If 106 * UPDATE_WAIT flag is set, or UPDATE_DIROP is set then wait for the 107 * disk write of the inode to complete. 108 */ 109 110 int 111 ffs_update(struct vnode *vp, const struct timespec *acc, 112 const struct timespec *mod, int updflags) 113 { 114 struct fs *fs; 115 struct buf *bp; 116 struct inode *ip; 117 int error; 118 void *cp; 119 int waitfor, flags; 120 121 if (vp->v_mount->mnt_flag & MNT_RDONLY) 122 return (0); 123 ip = VTOI(vp); 124 FFS_ITIMES(ip, acc, mod, NULL); 125 if (updflags & UPDATE_CLOSE) 126 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED); 127 else 128 flags = ip->i_flag & IN_MODIFIED; 129 if (flags == 0) 130 return (0); 131 fs = ip->i_fs; 132 133 if ((flags & IN_MODIFIED) != 0 && 134 (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) { 135 waitfor = updflags & UPDATE_WAIT; 136 if ((updflags & UPDATE_DIROP) != 0) 137 waitfor |= UPDATE_WAIT; 138 } else 139 waitfor = 0; 140 141 /* 142 * Ensure that uid and gid are correct. This is a temporary 143 * fix until fsck has been changed to do the update. 144 */ 145 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */ 146 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */ 147 ip->i_ffs1_ouid = ip->i_uid; /* XXX */ 148 ip->i_ffs1_ogid = ip->i_gid; /* XXX */ 149 } /* XXX */ 150 error = bread(ip->i_devvp, 151 FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)), 152 (int)fs->fs_bsize, NOCRED, B_MODIFY, &bp); 153 if (error) { 154 return (error); 155 } 156 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED); 157 /* Keep unlinked inode list up to date */ 158 KDASSERTMSG(DIP(ip, nlink) == ip->i_nlink, 159 "DIP(ip, nlink) [%d] == ip->i_nlink [%d]", 160 DIP(ip, nlink), ip->i_nlink); 161 if (ip->i_mode) { 162 if (ip->i_nlink > 0) { 163 UFS_WAPBL_UNREGISTER_INODE(ip->i_ump->um_mountp, 164 ip->i_number, ip->i_mode); 165 } else { 166 UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp, 167 ip->i_number, ip->i_mode); 168 } 169 } 170 if (fs->fs_magic == FS_UFS1_MAGIC) { 171 cp = (char *)bp->b_data + 172 (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE); 173 #ifdef FFS_EI 174 if (UFS_FSNEEDSWAP(fs)) 175 ffs_dinode1_swap(ip->i_din.ffs1_din, 176 (struct ufs1_dinode *)cp); 177 else 178 #endif 179 memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE); 180 } else { 181 cp = (char *)bp->b_data + 182 (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE); 183 #ifdef FFS_EI 184 if (UFS_FSNEEDSWAP(fs)) 185 ffs_dinode2_swap(ip->i_din.ffs2_din, 186 (struct ufs2_dinode *)cp); 187 else 188 #endif 189 memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE); 190 } 191 if (waitfor) { 192 return (bwrite(bp)); 193 } else { 194 bdwrite(bp); 195 return (0); 196 } 197 } 198 199 #define SINGLE 0 /* index of single indirect block */ 200 #define DOUBLE 1 /* index of double indirect block */ 201 #define TRIPLE 2 /* index of triple indirect block */ 202 /* 203 * Truncate the inode oip to at most length size, freeing the 204 * disk blocks. 205 */ 206 int 207 ffs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred) 208 { 209 daddr_t lastblock; 210 struct inode *oip = VTOI(ovp); 211 daddr_t bn, lastiblock[UFS_NIADDR], indir_lbn[UFS_NIADDR]; 212 daddr_t blks[UFS_NDADDR + UFS_NIADDR]; 213 struct fs *fs; 214 int offset, pgoffset, level; 215 int64_t count, blocksreleased = 0; 216 int i, aflag, nblocks; 217 int error, allerror = 0; 218 off_t osize; 219 int sync; 220 struct ufsmount *ump = oip->i_ump; 221 222 if (ovp->v_type == VCHR || ovp->v_type == VBLK || 223 ovp->v_type == VFIFO || ovp->v_type == VSOCK) { 224 KASSERT(oip->i_size == 0); 225 return 0; 226 } 227 228 if (length < 0) 229 return (EINVAL); 230 231 if (ovp->v_type == VLNK && 232 (oip->i_size < ump->um_maxsymlinklen || 233 (ump->um_maxsymlinklen == 0 && DIP(oip, blocks) == 0))) { 234 KDASSERT(length == 0); 235 memset(SHORTLINK(oip), 0, (size_t)oip->i_size); 236 oip->i_size = 0; 237 DIP_ASSIGN(oip, size, 0); 238 oip->i_flag |= IN_CHANGE | IN_UPDATE; 239 return (ffs_update(ovp, NULL, NULL, 0)); 240 } 241 if (oip->i_size == length) { 242 /* still do a uvm_vnp_setsize() as writesize may be larger */ 243 uvm_vnp_setsize(ovp, length); 244 oip->i_flag |= IN_CHANGE | IN_UPDATE; 245 return (ffs_update(ovp, NULL, NULL, 0)); 246 } 247 fs = oip->i_fs; 248 if (length > ump->um_maxfilesize) 249 return (EFBIG); 250 251 if ((oip->i_flags & SF_SNAPSHOT) != 0) 252 ffs_snapremove(ovp); 253 254 osize = oip->i_size; 255 aflag = ioflag & IO_SYNC ? B_SYNC : 0; 256 257 /* 258 * Lengthen the size of the file. We must ensure that the 259 * last byte of the file is allocated. Since the smallest 260 * value of osize is 0, length will be at least 1. 261 */ 262 263 if (osize < length) { 264 if (ffs_lblkno(fs, osize) < UFS_NDADDR && 265 ffs_lblkno(fs, osize) != ffs_lblkno(fs, length) && 266 ffs_blkroundup(fs, osize) != osize) { 267 off_t eob; 268 269 eob = ffs_blkroundup(fs, osize); 270 uvm_vnp_setwritesize(ovp, eob); 271 error = ufs_balloc_range(ovp, osize, eob - osize, 272 cred, aflag); 273 if (error) { 274 (void) ffs_truncate(ovp, osize, 275 ioflag & IO_SYNC, cred); 276 return error; 277 } 278 if (ioflag & IO_SYNC) { 279 mutex_enter(ovp->v_interlock); 280 VOP_PUTPAGES(ovp, 281 trunc_page(osize & fs->fs_bmask), 282 round_page(eob), PGO_CLEANIT | PGO_SYNCIO | 283 PGO_JOURNALLOCKED); 284 } 285 } 286 uvm_vnp_setwritesize(ovp, length); 287 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag); 288 if (error) { 289 (void) ffs_truncate(ovp, osize, ioflag & IO_SYNC, cred); 290 return (error); 291 } 292 uvm_vnp_setsize(ovp, length); 293 oip->i_flag |= IN_CHANGE | IN_UPDATE; 294 KASSERT(ovp->v_size == oip->i_size); 295 return (ffs_update(ovp, NULL, NULL, 0)); 296 } 297 298 /* 299 * When truncating a regular file down to a non-block-aligned size, 300 * we must zero the part of last block which is past the new EOF. 301 * We must synchronously flush the zeroed pages to disk 302 * since the new pages will be invalidated as soon as we 303 * inform the VM system of the new, smaller size. 304 * We must do this before acquiring the GLOCK, since fetching 305 * the pages will acquire the GLOCK internally. 306 * So there is a window where another thread could see a whole 307 * zeroed page past EOF, but that's life. 308 */ 309 310 offset = ffs_blkoff(fs, length); 311 pgoffset = length & PAGE_MASK; 312 if (ovp->v_type == VREG && (pgoffset != 0 || offset != 0) && 313 osize > length) { 314 daddr_t lbn; 315 voff_t eoz; 316 int size; 317 318 if (offset != 0) { 319 error = ufs_balloc_range(ovp, length - 1, 1, cred, 320 aflag); 321 if (error) 322 return error; 323 } 324 lbn = ffs_lblkno(fs, length); 325 size = ffs_blksize(fs, oip, lbn); 326 eoz = MIN(MAX(ffs_lblktosize(fs, lbn) + size, round_page(pgoffset)), 327 osize); 328 ubc_zerorange(&ovp->v_uobj, length, eoz - length, 329 UBC_UNMAP_FLAG(ovp)); 330 if (round_page(eoz) > round_page(length)) { 331 mutex_enter(ovp->v_interlock); 332 error = VOP_PUTPAGES(ovp, round_page(length), 333 round_page(eoz), 334 PGO_CLEANIT | PGO_DEACTIVATE | PGO_JOURNALLOCKED | 335 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0)); 336 if (error) 337 return error; 338 } 339 } 340 341 genfs_node_wrlock(ovp); 342 oip->i_size = length; 343 DIP_ASSIGN(oip, size, length); 344 uvm_vnp_setsize(ovp, length); 345 /* 346 * Calculate index into inode's block list of 347 * last direct and indirect blocks (if any) 348 * which we want to keep. Lastblock is -1 when 349 * the file is truncated to 0. 350 */ 351 lastblock = ffs_lblkno(fs, length + fs->fs_bsize - 1) - 1; 352 lastiblock[SINGLE] = lastblock - UFS_NDADDR; 353 lastiblock[DOUBLE] = lastiblock[SINGLE] - FFS_NINDIR(fs); 354 lastiblock[TRIPLE] = lastiblock[DOUBLE] - FFS_NINDIR(fs) * FFS_NINDIR(fs); 355 nblocks = btodb(fs->fs_bsize); 356 /* 357 * Update file and block pointers on disk before we start freeing 358 * blocks. If we crash before free'ing blocks below, the blocks 359 * will be returned to the free list. lastiblock values are also 360 * normalized to -1 for calls to ffs_indirtrunc below. 361 */ 362 sync = 0; 363 for (level = TRIPLE; level >= SINGLE; level--) { 364 blks[UFS_NDADDR + level] = DIP(oip, ib[level]); 365 if (lastiblock[level] < 0 && blks[UFS_NDADDR + level] != 0) { 366 sync = 1; 367 DIP_ASSIGN(oip, ib[level], 0); 368 lastiblock[level] = -1; 369 } 370 } 371 for (i = 0; i < UFS_NDADDR; i++) { 372 blks[i] = DIP(oip, db[i]); 373 if (i > lastblock && blks[i] != 0) { 374 sync = 1; 375 DIP_ASSIGN(oip, db[i], 0); 376 } 377 } 378 oip->i_flag |= IN_CHANGE | IN_UPDATE; 379 if (sync) { 380 error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT); 381 if (error && !allerror) 382 allerror = error; 383 } 384 385 /* 386 * Having written the new inode to disk, save its new configuration 387 * and put back the old block pointers long enough to process them. 388 * Note that we save the new block configuration so we can check it 389 * when we are done. 390 */ 391 for (i = 0; i < UFS_NDADDR; i++) { 392 bn = DIP(oip, db[i]); 393 DIP_ASSIGN(oip, db[i], blks[i]); 394 blks[i] = bn; 395 } 396 for (i = 0; i < UFS_NIADDR; i++) { 397 bn = DIP(oip, ib[i]); 398 DIP_ASSIGN(oip, ib[i], blks[UFS_NDADDR + i]); 399 blks[UFS_NDADDR + i] = bn; 400 } 401 402 oip->i_size = osize; 403 DIP_ASSIGN(oip, size, osize); 404 error = vtruncbuf(ovp, lastblock + 1, 0, 0); 405 if (error && !allerror) 406 allerror = error; 407 408 /* 409 * Indirect blocks first. 410 */ 411 indir_lbn[SINGLE] = -UFS_NDADDR; 412 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - FFS_NINDIR(fs) - 1; 413 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - FFS_NINDIR(fs) * FFS_NINDIR(fs) - 1; 414 for (level = TRIPLE; level >= SINGLE; level--) { 415 if (oip->i_ump->um_fstype == UFS1) 416 bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs)); 417 else 418 bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs)); 419 if (bn != 0) { 420 error = ffs_indirtrunc(oip, indir_lbn[level], 421 FFS_FSBTODB(fs, bn), lastiblock[level], level, &count); 422 if (error) 423 allerror = error; 424 blocksreleased += count; 425 if (lastiblock[level] < 0) { 426 DIP_ASSIGN(oip, ib[level], 0); 427 if (oip->i_ump->um_mountp->mnt_wapbl) { 428 UFS_WAPBL_REGISTER_DEALLOCATION( 429 oip->i_ump->um_mountp, 430 FFS_FSBTODB(fs, bn), fs->fs_bsize); 431 } else 432 ffs_blkfree(fs, oip->i_devvp, bn, 433 fs->fs_bsize, oip->i_number); 434 blocksreleased += nblocks; 435 } 436 } 437 if (lastiblock[level] >= 0) 438 goto done; 439 } 440 441 /* 442 * All whole direct blocks or frags. 443 */ 444 for (i = UFS_NDADDR - 1; i > lastblock; i--) { 445 long bsize; 446 447 if (oip->i_ump->um_fstype == UFS1) 448 bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs)); 449 else 450 bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs)); 451 if (bn == 0) 452 continue; 453 DIP_ASSIGN(oip, db[i], 0); 454 bsize = ffs_blksize(fs, oip, i); 455 if ((oip->i_ump->um_mountp->mnt_wapbl) && 456 (ovp->v_type != VREG)) { 457 UFS_WAPBL_REGISTER_DEALLOCATION(oip->i_ump->um_mountp, 458 FFS_FSBTODB(fs, bn), bsize); 459 } else 460 ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number); 461 blocksreleased += btodb(bsize); 462 } 463 if (lastblock < 0) 464 goto done; 465 466 /* 467 * Finally, look for a change in size of the 468 * last direct block; release any frags. 469 */ 470 if (oip->i_ump->um_fstype == UFS1) 471 bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs)); 472 else 473 bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs)); 474 if (bn != 0) { 475 long oldspace, newspace; 476 477 /* 478 * Calculate amount of space we're giving 479 * back as old block size minus new block size. 480 */ 481 oldspace = ffs_blksize(fs, oip, lastblock); 482 oip->i_size = length; 483 DIP_ASSIGN(oip, size, length); 484 newspace = ffs_blksize(fs, oip, lastblock); 485 if (newspace == 0) 486 panic("itrunc: newspace"); 487 if (oldspace - newspace > 0) { 488 /* 489 * Block number of space to be free'd is 490 * the old block # plus the number of frags 491 * required for the storage we're keeping. 492 */ 493 bn += ffs_numfrags(fs, newspace); 494 if ((oip->i_ump->um_mountp->mnt_wapbl) && 495 (ovp->v_type != VREG)) { 496 UFS_WAPBL_REGISTER_DEALLOCATION( 497 oip->i_ump->um_mountp, FFS_FSBTODB(fs, bn), 498 oldspace - newspace); 499 } else 500 ffs_blkfree(fs, oip->i_devvp, bn, 501 oldspace - newspace, oip->i_number); 502 blocksreleased += btodb(oldspace - newspace); 503 } 504 } 505 506 done: 507 #ifdef DIAGNOSTIC 508 for (level = SINGLE; level <= TRIPLE; level++) 509 if (blks[UFS_NDADDR + level] != DIP(oip, ib[level])) 510 panic("itrunc1"); 511 for (i = 0; i < UFS_NDADDR; i++) 512 if (blks[i] != DIP(oip, db[i])) 513 panic("itrunc2"); 514 if (length == 0 && 515 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd))) 516 panic("itrunc3"); 517 #endif /* DIAGNOSTIC */ 518 /* 519 * Put back the real size. 520 */ 521 oip->i_size = length; 522 DIP_ASSIGN(oip, size, length); 523 DIP_ADD(oip, blocks, -blocksreleased); 524 genfs_node_unlock(ovp); 525 oip->i_flag |= IN_CHANGE; 526 UFS_WAPBL_UPDATE(ovp, NULL, NULL, 0); 527 #if defined(QUOTA) || defined(QUOTA2) 528 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 529 #endif 530 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size); 531 return (allerror); 532 } 533 534 /* 535 * Release blocks associated with the inode ip and stored in the indirect 536 * block bn. Blocks are free'd in LIFO order up to (but not including) 537 * lastbn. If level is greater than SINGLE, the block is an indirect block 538 * and recursive calls to indirtrunc must be used to cleanse other indirect 539 * blocks. 540 * 541 * NB: triple indirect blocks are untested. 542 */ 543 static int 544 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn, 545 int level, int64_t *countp) 546 { 547 int i; 548 struct buf *bp; 549 struct fs *fs = ip->i_fs; 550 int32_t *bap1 = NULL; 551 int64_t *bap2 = NULL; 552 struct vnode *vp; 553 daddr_t nb, nlbn, last; 554 char *copy = NULL; 555 int64_t blkcount, factor, blocksreleased = 0; 556 int nblocks; 557 int error = 0, allerror = 0; 558 const int needswap = UFS_FSNEEDSWAP(fs); 559 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \ 560 ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap)) 561 #define BAP_ASSIGN(ip, i, value) \ 562 do { \ 563 if ((ip)->i_ump->um_fstype == UFS1) \ 564 bap1[i] = (value); \ 565 else \ 566 bap2[i] = (value); \ 567 } while(0) 568 569 /* 570 * Calculate index in current block of last 571 * block to be kept. -1 indicates the entire 572 * block so we need not calculate the index. 573 */ 574 factor = 1; 575 for (i = SINGLE; i < level; i++) 576 factor *= FFS_NINDIR(fs); 577 last = lastbn; 578 if (lastbn > 0) 579 last /= factor; 580 nblocks = btodb(fs->fs_bsize); 581 /* 582 * Get buffer of block pointers, zero those entries corresponding 583 * to blocks to be free'd, and update on disk copy first. Since 584 * double(triple) indirect before single(double) indirect, calls 585 * to bmap on these blocks will fail. However, we already have 586 * the on disk address, so we have to set the b_blkno field 587 * explicitly instead of letting bread do everything for us. 588 */ 589 vp = ITOV(ip); 590 error = ffs_getblk(vp, lbn, FFS_NOBLK, fs->fs_bsize, false, &bp); 591 if (error) { 592 *countp = 0; 593 return error; 594 } 595 if (bp->b_oflags & (BO_DONE | BO_DELWRI)) { 596 /* Braces must be here in case trace evaluates to nothing. */ 597 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn); 598 } else { 599 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn); 600 curlwp->l_ru.ru_inblock++; /* pay for read */ 601 bp->b_flags |= B_READ; 602 bp->b_flags &= ~B_COWDONE; /* we change blkno below */ 603 if (bp->b_bcount > bp->b_bufsize) 604 panic("ffs_indirtrunc: bad buffer size"); 605 bp->b_blkno = dbn; 606 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL); 607 VOP_STRATEGY(vp, bp); 608 error = biowait(bp); 609 if (error == 0) 610 error = fscow_run(bp, true); 611 } 612 if (error) { 613 brelse(bp, 0); 614 *countp = 0; 615 return (error); 616 } 617 618 if (ip->i_ump->um_fstype == UFS1) 619 bap1 = (int32_t *)bp->b_data; 620 else 621 bap2 = (int64_t *)bp->b_data; 622 if (lastbn >= 0) { 623 copy = kmem_alloc(fs->fs_bsize, KM_SLEEP); 624 memcpy((void *)copy, bp->b_data, (u_int)fs->fs_bsize); 625 for (i = last + 1; i < FFS_NINDIR(fs); i++) 626 BAP_ASSIGN(ip, i, 0); 627 error = bwrite(bp); 628 if (error) 629 allerror = error; 630 if (ip->i_ump->um_fstype == UFS1) 631 bap1 = (int32_t *)copy; 632 else 633 bap2 = (int64_t *)copy; 634 } 635 636 /* 637 * Recursively free totally unused blocks. 638 */ 639 for (i = FFS_NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 640 i--, nlbn += factor) { 641 nb = RBAP(ip, i); 642 if (nb == 0) 643 continue; 644 if (level > SINGLE) { 645 error = ffs_indirtrunc(ip, nlbn, FFS_FSBTODB(fs, nb), 646 (daddr_t)-1, level - 1, 647 &blkcount); 648 if (error) 649 allerror = error; 650 blocksreleased += blkcount; 651 } 652 if ((ip->i_ump->um_mountp->mnt_wapbl) && 653 ((level > SINGLE) || (ITOV(ip)->v_type != VREG))) { 654 UFS_WAPBL_REGISTER_DEALLOCATION(ip->i_ump->um_mountp, 655 FFS_FSBTODB(fs, nb), fs->fs_bsize); 656 } else 657 ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, 658 ip->i_number); 659 blocksreleased += nblocks; 660 } 661 662 /* 663 * Recursively free last partial block. 664 */ 665 if (level > SINGLE && lastbn >= 0) { 666 last = lastbn % factor; 667 nb = RBAP(ip, i); 668 if (nb != 0) { 669 error = ffs_indirtrunc(ip, nlbn, FFS_FSBTODB(fs, nb), 670 last, level - 1, &blkcount); 671 if (error) 672 allerror = error; 673 blocksreleased += blkcount; 674 } 675 } 676 677 if (copy != NULL) { 678 kmem_free(copy, fs->fs_bsize); 679 } else { 680 brelse(bp, BC_INVAL); 681 } 682 683 *countp = blocksreleased; 684 return (allerror); 685 } 686 687 void 688 ffs_itimes(struct inode *ip, const struct timespec *acc, 689 const struct timespec *mod, const struct timespec *cre) 690 { 691 struct timespec now; 692 693 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) { 694 return; 695 } 696 697 vfs_timestamp(&now); 698 if (ip->i_flag & IN_ACCESS) { 699 if (acc == NULL) 700 acc = &now; 701 DIP_ASSIGN(ip, atime, acc->tv_sec); 702 DIP_ASSIGN(ip, atimensec, acc->tv_nsec); 703 } 704 if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) { 705 if ((ip->i_flags & SF_SNAPSHOT) == 0) { 706 if (mod == NULL) 707 mod = &now; 708 DIP_ASSIGN(ip, mtime, mod->tv_sec); 709 DIP_ASSIGN(ip, mtimensec, mod->tv_nsec); 710 } 711 ip->i_modrev++; 712 } 713 if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) { 714 if (cre == NULL) 715 cre = &now; 716 DIP_ASSIGN(ip, ctime, cre->tv_sec); 717 DIP_ASSIGN(ip, ctimensec, cre->tv_nsec); 718 } 719 if (ip->i_flag & (IN_ACCESS | IN_MODIFY)) 720 ip->i_flag |= IN_ACCESSED; 721 if (ip->i_flag & (IN_UPDATE | IN_CHANGE)) 722 ip->i_flag |= IN_MODIFIED; 723 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY); 724 } 725