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