1 /* $NetBSD: ext2fs_inode.c,v 1.62 2007/12/08 19:29:53 pooka Exp $ */ 2 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.8 (Berkeley) 10/19/94 32 * Modified for ext2fs by Manuel Bouyer. 33 */ 34 35 /* 36 * Copyright (c) 1997 Manuel Bouyer. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by Manuel Bouyer. 49 * 4. The name of the author may not be used to endorse or promote products 50 * derived from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 * 63 * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94 64 * Modified for ext2fs by Manuel Bouyer. 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.62 2007/12/08 19:29:53 pooka Exp $"); 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/mount.h> 73 #include <sys/proc.h> 74 #include <sys/file.h> 75 #include <sys/buf.h> 76 #include <sys/vnode.h> 77 #include <sys/kernel.h> 78 #include <sys/malloc.h> 79 #include <sys/trace.h> 80 #include <sys/resourcevar.h> 81 #include <sys/kauth.h> 82 83 #include <ufs/ufs/inode.h> 84 #include <ufs/ufs/ufsmount.h> 85 #include <ufs/ufs/ufs_extern.h> 86 87 #include <ufs/ext2fs/ext2fs.h> 88 #include <ufs/ext2fs/ext2fs_extern.h> 89 90 extern int prtactive; 91 92 static int ext2fs_indirtrunc(struct inode *, daddr_t, daddr_t, 93 daddr_t, int, long *); 94 95 /* 96 * Get the size of an inode. 97 */ 98 u_int64_t 99 ext2fs_size(struct inode *ip) 100 { 101 u_int64_t size = ip->i_e2fs_size; 102 103 if ((ip->i_e2fs_mode & IFMT) == IFREG) 104 size |= (u_int64_t)ip->i_e2fs_dacl << 32; 105 return size; 106 } 107 108 int 109 ext2fs_setsize(struct inode *ip, u_int64_t size) 110 { 111 if ((ip->i_e2fs_mode & IFMT) == IFREG || 112 ip->i_e2fs_mode == 0) { 113 ip->i_e2fs_dacl = size >> 32; 114 if (size >= 0x80000000U) { 115 struct m_ext2fs *fs = ip->i_e2fs; 116 117 if (fs->e2fs.e2fs_rev <= E2FS_REV0) { 118 /* Linux automagically upgrades to REV1 here! */ 119 return EFBIG; 120 } 121 if (!(fs->e2fs.e2fs_features_rocompat 122 & EXT2F_ROCOMPAT_LARGEFILE)) { 123 fs->e2fs.e2fs_features_rocompat |= 124 EXT2F_ROCOMPAT_LARGEFILE; 125 fs->e2fs_fmod = 1; 126 } 127 } 128 } else if (size >= 0x80000000U) 129 return EFBIG; 130 131 ip->i_e2fs_size = size; 132 133 return 0; 134 } 135 136 /* 137 * Last reference to an inode. If necessary, write or delete it. 138 */ 139 int 140 ext2fs_inactive(void *v) 141 { 142 struct vop_inactive_args /* { 143 struct vnode *a_vp; 144 } */ *ap = v; 145 struct vnode *vp = ap->a_vp; 146 struct inode *ip = VTOI(vp); 147 int error = 0; 148 149 if (prtactive && vp->v_usecount != 0) 150 vprint("ext2fs_inactive: pushing active", vp); 151 /* Get rid of inodes related to stale file handles. */ 152 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0) 153 goto out; 154 155 error = 0; 156 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 157 if (ext2fs_size(ip) != 0) { 158 error = ext2fs_truncate(vp, (off_t)0, 0, NOCRED); 159 } 160 ip->i_e2fs_dtime = time_second; 161 ip->i_flag |= IN_CHANGE | IN_UPDATE; 162 ext2fs_vfree(vp, ip->i_number, ip->i_e2fs_mode); 163 } 164 if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) { 165 ext2fs_update(vp, NULL, NULL, 0); 166 } 167 out: 168 VOP_UNLOCK(vp, 0); 169 /* 170 * If we are done with the inode, reclaim it 171 * so that it can be reused immediately. 172 */ 173 if (ip->i_e2fs_dtime != 0) 174 vrecycle(vp, NULL, curlwp); 175 return (error); 176 } 177 178 179 /* 180 * Update the access, modified, and inode change times as specified by the 181 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is 182 * used to specify that the inode needs to be updated but that the times have 183 * already been set. The access and modified times are taken from the second 184 * and third parameters; the inode change time is always taken from the current 185 * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk 186 * write of the inode to complete. 187 */ 188 int 189 ext2fs_update(struct vnode *vp, const struct timespec *acc, 190 const struct timespec *mod, int updflags) 191 { 192 struct m_ext2fs *fs; 193 struct buf *bp; 194 struct inode *ip; 195 int error; 196 void *cp; 197 int flags; 198 199 if (vp->v_mount->mnt_flag & MNT_RDONLY) 200 return (0); 201 ip = VTOI(vp); 202 EXT2FS_ITIMES(ip, acc, mod, NULL); 203 if (updflags & UPDATE_CLOSE) 204 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED); 205 else 206 flags = ip->i_flag & IN_MODIFIED; 207 if (flags == 0) 208 return (0); 209 fs = ip->i_e2fs; 210 211 error = bread(ip->i_devvp, 212 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 213 (int)fs->e2fs_bsize, NOCRED, &bp); 214 if (error) { 215 brelse(bp, 0); 216 return (error); 217 } 218 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED); 219 cp = (char *)bp->b_data + 220 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE); 221 e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp); 222 if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 && 223 (flags & IN_MODIFIED) != 0 && 224 (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 225 return (bwrite(bp)); 226 else { 227 bdwrite(bp); 228 return (0); 229 } 230 } 231 232 #define SINGLE 0 /* index of single indirect block */ 233 #define DOUBLE 1 /* index of double indirect block */ 234 #define TRIPLE 2 /* index of triple indirect block */ 235 /* 236 * Truncate the inode oip to at most length size, freeing the 237 * disk blocks. 238 */ 239 int 240 ext2fs_truncate(struct vnode *ovp, off_t length, int ioflag, 241 kauth_cred_t cred) 242 { 243 daddr_t lastblock; 244 struct inode *oip = VTOI(ovp); 245 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR]; 246 /* XXX ondisk32 */ 247 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; 248 struct m_ext2fs *fs; 249 int offset, size, level; 250 long count, blocksreleased = 0; 251 int i, nblocks; 252 int error, allerror = 0; 253 off_t osize; 254 int sync; 255 struct ufsmount *ump = oip->i_ump; 256 257 if (ovp->v_type == VCHR || ovp->v_type == VBLK || 258 ovp->v_type == VFIFO || ovp->v_type == VSOCK) { 259 return 0; 260 } 261 262 if (length < 0) 263 return (EINVAL); 264 265 if (ovp->v_type == VLNK && 266 (ext2fs_size(oip) < ump->um_maxsymlinklen || 267 (ump->um_maxsymlinklen == 0 && oip->i_e2fs_nblock == 0))) { 268 KDASSERT(length == 0); 269 memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0, 270 (u_int)ext2fs_size(oip)); 271 (void)ext2fs_setsize(oip, 0); 272 oip->i_flag |= IN_CHANGE | IN_UPDATE; 273 return (ext2fs_update(ovp, NULL, NULL, 0)); 274 } 275 if (ext2fs_size(oip) == length) { 276 oip->i_flag |= IN_CHANGE | IN_UPDATE; 277 return (ext2fs_update(ovp, NULL, NULL, 0)); 278 } 279 fs = oip->i_e2fs; 280 if (length > ump->um_maxfilesize) 281 return (EFBIG); 282 283 osize = ext2fs_size(oip); 284 285 /* 286 * Lengthen the size of the file. We must ensure that the 287 * last byte of the file is allocated. Since the smallest 288 * value of osize is 0, length will be at least 1. 289 */ 290 if (osize < length) { 291 uvm_vnp_setwritesize(ovp, length); 292 error = ufs_balloc_range(ovp, length - 1, 1, cred, 293 ioflag & IO_SYNC ? B_SYNC : 0); 294 if (error) { 295 (void) ext2fs_truncate(ovp, osize, ioflag & IO_SYNC, 296 cred); 297 return (error); 298 } 299 uvm_vnp_setsize(ovp, length); 300 oip->i_flag |= IN_CHANGE | IN_UPDATE; 301 KASSERT(error || ovp->v_size == ext2fs_size(oip)); 302 return (ext2fs_update(ovp, NULL, NULL, 0)); 303 } 304 /* 305 * Shorten the size of the file. If the file is not being 306 * truncated to a block boundry, the contents of the 307 * partial block following the end of the file must be 308 * zero'ed in case it ever become accessible again because 309 * of subsequent file growth. 310 */ 311 offset = blkoff(fs, length); 312 if (offset != 0) { 313 size = fs->e2fs_bsize; 314 315 /* XXXUBC we should handle more than just VREG */ 316 uvm_vnp_zerorange(ovp, length, size - offset); 317 } 318 (void)ext2fs_setsize(oip, length); 319 uvm_vnp_setsize(ovp, length); 320 /* 321 * Calculate index into inode's block list of 322 * last direct and indirect blocks (if any) 323 * which we want to keep. Lastblock is -1 when 324 * the file is truncated to 0. 325 */ 326 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1; 327 lastiblock[SINGLE] = lastblock - NDADDR; 328 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 329 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 330 nblocks = btodb(fs->e2fs_bsize); 331 /* 332 * Update file and block pointers on disk before we start freeing 333 * blocks. If we crash before free'ing blocks below, the blocks 334 * will be returned to the free list. lastiblock values are also 335 * normalized to -1 for calls to ext2fs_indirtrunc below. 336 */ 337 memcpy((void *)oldblks, (void *)&oip->i_e2fs_blocks[0], sizeof oldblks); 338 sync = 0; 339 for (level = TRIPLE; level >= SINGLE; level--) { 340 if (lastiblock[level] < 0 && oldblks[NDADDR + level] != 0) { 341 sync = 1; 342 oip->i_e2fs_blocks[NDADDR + level] = 0; 343 lastiblock[level] = -1; 344 } 345 } 346 for (i = 0; i < NDADDR; i++) { 347 if (i > lastblock && oldblks[i] != 0) { 348 sync = 1; 349 oip->i_e2fs_blocks[i] = 0; 350 } 351 } 352 oip->i_flag |= IN_CHANGE | IN_UPDATE; 353 if (sync) { 354 error = ext2fs_update(ovp, NULL, NULL, UPDATE_WAIT); 355 if (error && !allerror) 356 allerror = error; 357 } 358 359 /* 360 * Having written the new inode to disk, save its new configuration 361 * and put back the old block pointers long enough to process them. 362 * Note that we save the new block configuration so we can check it 363 * when we are done. 364 */ 365 memcpy((void *)newblks, (void *)&oip->i_e2fs_blocks[0], sizeof newblks); 366 memcpy((void *)&oip->i_e2fs_blocks[0], (void *)oldblks, sizeof oldblks); 367 368 (void)ext2fs_setsize(oip, osize); 369 error = vtruncbuf(ovp, lastblock + 1, 0, 0); 370 if (error && !allerror) 371 allerror = error; 372 373 /* 374 * Indirect blocks first. 375 */ 376 indir_lbn[SINGLE] = -NDADDR; 377 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1; 378 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 379 for (level = TRIPLE; level >= SINGLE; level--) { 380 /* XXX ondisk32 */ 381 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]); 382 if (bn != 0) { 383 error = ext2fs_indirtrunc(oip, indir_lbn[level], 384 fsbtodb(fs, bn), lastiblock[level], level, &count); 385 if (error) 386 allerror = error; 387 blocksreleased += count; 388 if (lastiblock[level] < 0) { 389 oip->i_e2fs_blocks[NDADDR + level] = 0; 390 ext2fs_blkfree(oip, bn); 391 blocksreleased += nblocks; 392 } 393 } 394 if (lastiblock[level] >= 0) 395 goto done; 396 } 397 398 /* 399 * All whole direct blocks or frags. 400 */ 401 for (i = NDADDR - 1; i > lastblock; i--) { 402 /* XXX ondisk32 */ 403 bn = fs2h32(oip->i_e2fs_blocks[i]); 404 if (bn == 0) 405 continue; 406 oip->i_e2fs_blocks[i] = 0; 407 ext2fs_blkfree(oip, bn); 408 blocksreleased += btodb(fs->e2fs_bsize); 409 } 410 411 done: 412 #ifdef DIAGNOSTIC 413 for (level = SINGLE; level <= TRIPLE; level++) 414 if (newblks[NDADDR + level] != 415 oip->i_e2fs_blocks[NDADDR + level]) 416 panic("ext2fs_truncate1"); 417 for (i = 0; i < NDADDR; i++) 418 if (newblks[i] != oip->i_e2fs_blocks[i]) 419 panic("ext2fs_truncate2"); 420 if (length == 0 && 421 (!LIST_EMPTY(&ovp->v_cleanblkhd) || 422 !LIST_EMPTY(&ovp->v_dirtyblkhd))) 423 panic("ext2fs_truncate3"); 424 #endif /* DIAGNOSTIC */ 425 /* 426 * Put back the real size. 427 */ 428 (void)ext2fs_setsize(oip, length); 429 oip->i_e2fs_nblock -= blocksreleased; 430 oip->i_flag |= IN_CHANGE; 431 KASSERT(ovp->v_type != VREG || ovp->v_size == ext2fs_size(oip)); 432 return (allerror); 433 } 434 435 /* 436 * Release blocks associated with the inode ip and stored in the indirect 437 * block bn. Blocks are free'd in LIFO order up to (but not including) 438 * lastbn. If level is greater than SINGLE, the block is an indirect block 439 * and recursive calls to indirtrunc must be used to cleanse other indirect 440 * blocks. 441 * 442 * NB: triple indirect blocks are untested. 443 */ 444 static int 445 ext2fs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn, 446 int level, long *countp) 447 { 448 int i; 449 struct buf *bp; 450 struct m_ext2fs *fs = ip->i_e2fs; 451 int32_t *bap; /* XXX ondisk32 */ 452 struct vnode *vp; 453 daddr_t nb, nlbn, last; 454 int32_t *copy = NULL; /* XXX ondisk32 */ 455 long blkcount, factor; 456 int nblocks, blocksreleased = 0; 457 int error = 0, allerror = 0; 458 459 /* 460 * Calculate index in current block of last 461 * block to be kept. -1 indicates the entire 462 * block so we need not calculate the index. 463 */ 464 factor = 1; 465 for (i = SINGLE; i < level; i++) 466 factor *= NINDIR(fs); 467 last = lastbn; 468 if (lastbn > 0) 469 last /= factor; 470 nblocks = btodb(fs->e2fs_bsize); 471 /* 472 * Get buffer of block pointers, zero those entries corresponding 473 * to blocks to be free'd, and update on disk copy first. Since 474 * double(triple) indirect before single(double) indirect, calls 475 * to bmap on these blocks will fail. However, we already have 476 * the on disk address, so we have to set the b_blkno field 477 * explicitly instead of letting bread do everything for us. 478 */ 479 vp = ITOV(ip); 480 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0); 481 if (bp->b_flags & (B_DONE | B_DELWRI)) { 482 /* Braces must be here in case trace evaluates to nothing. */ 483 trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn); 484 } else { 485 trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn); 486 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */ 487 bp->b_flags |= B_READ; 488 if (bp->b_bcount > bp->b_bufsize) 489 panic("ext2fs_indirtrunc: bad buffer size"); 490 bp->b_blkno = dbn; 491 VOP_STRATEGY(vp, bp); 492 error = biowait(bp); 493 } 494 if (error) { 495 brelse(bp, 0); 496 *countp = 0; 497 return (error); 498 } 499 500 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */ 501 if (lastbn >= 0) { 502 /* XXX ondisk32 */ 503 copy = malloc(fs->e2fs_bsize, M_TEMP, M_WAITOK); 504 memcpy((void *)copy, (void *)bap, (u_int)fs->e2fs_bsize); 505 memset((void *)&bap[last + 1], 0, 506 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t)); 507 error = bwrite(bp); 508 if (error) 509 allerror = error; 510 bap = copy; 511 } 512 513 /* 514 * Recursively free totally unused blocks. 515 */ 516 for (i = NINDIR(fs) - 1, 517 nlbn = lbn + 1 - i * factor; i > last; 518 i--, nlbn += factor) { 519 /* XXX ondisk32 */ 520 nb = fs2h32(bap[i]); 521 if (nb == 0) 522 continue; 523 if (level > SINGLE) { 524 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 525 (daddr_t)-1, level - 1, 526 &blkcount); 527 if (error) 528 allerror = error; 529 blocksreleased += blkcount; 530 } 531 ext2fs_blkfree(ip, nb); 532 blocksreleased += nblocks; 533 } 534 535 /* 536 * Recursively free last partial block. 537 */ 538 if (level > SINGLE && lastbn >= 0) { 539 last = lastbn % factor; 540 /* XXX ondisk32 */ 541 nb = fs2h32(bap[i]); 542 if (nb != 0) { 543 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 544 last, level - 1, &blkcount); 545 if (error) 546 allerror = error; 547 blocksreleased += blkcount; 548 } 549 } 550 551 if (copy != NULL) { 552 FREE(copy, M_TEMP); 553 } else { 554 brelse(bp, BC_INVAL); 555 } 556 557 *countp = blocksreleased; 558 return (allerror); 559 } 560