1/* 2 * modified for Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7/* 8 * Copyright (c) 1982, 1986, 1989, 1993 9 * The Regents of the University of California. All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)ffs_inode.c 8.5 (Berkeley) 12/30/93
| 1/* 2 * modified for Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7/* 8 * Copyright (c) 1982, 1986, 1989, 1993 9 * The Regents of the University of California. All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)ffs_inode.c 8.5 (Berkeley) 12/30/93
|
40 * $FreeBSD: head/sys/gnu/fs/ext2fs/ext2_inode.c 96752 2002-05-16 19:43:28Z iedowse $
| 40 * $FreeBSD: head/sys/gnu/fs/ext2fs/ext2_inode.c 96877 2002-05-18 19:12:38Z iedowse $
|
41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/mount.h> 46#include <sys/bio.h> 47#include <sys/buf.h> 48#include <sys/vnode.h> 49#include <sys/malloc.h> 50 51#include <vm/vm.h> 52#include <vm/vm_extern.h> 53 54#include <gnu/ext2fs/inode.h> 55#include <gnu/ext2fs/ext2_mount.h> 56#include <gnu/ext2fs/ext2_fs.h> 57#include <gnu/ext2fs/ext2_fs_sb.h> 58#include <gnu/ext2fs/fs.h> 59#include <gnu/ext2fs/ext2_extern.h> 60
| 41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/mount.h> 46#include <sys/bio.h> 47#include <sys/buf.h> 48#include <sys/vnode.h> 49#include <sys/malloc.h> 50 51#include <vm/vm.h> 52#include <vm/vm_extern.h> 53 54#include <gnu/ext2fs/inode.h> 55#include <gnu/ext2fs/ext2_mount.h> 56#include <gnu/ext2fs/ext2_fs.h> 57#include <gnu/ext2fs/ext2_fs_sb.h> 58#include <gnu/ext2fs/fs.h> 59#include <gnu/ext2fs/ext2_extern.h> 60
|
61static int ext2_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int,
| 61static int ext2_indirtrunc(struct inode *, int32_t, int32_t, int32_t, int,
|
62 long *); 63 64/* 65 * Update the access, modified, and inode change times as specified by the 66 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode 67 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by 68 * the timestamp update). The IN_LAZYMOD flag is set to force a write 69 * later if not now. If we write now, then clear both IN_MODIFIED and 70 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is 71 * set, then wait for the write to complete. 72 */ 73int 74ext2_update(vp, waitfor) 75 struct vnode *vp; 76 int waitfor; 77{ 78 struct ext2_sb_info *fs; 79 struct buf *bp; 80 struct inode *ip; 81 int error; 82 83 ext2_itimes(vp); 84 ip = VTOI(vp); 85 if ((ip->i_flag & IN_MODIFIED) == 0) 86 return (0); 87 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); 88 if (vp->v_mount->mnt_flag & MNT_RDONLY) 89 return (0); 90 fs = ip->i_e2fs; 91 if ((error = bread(ip->i_devvp, 92 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 93 (int)fs->s_blocksize, NOCRED, &bp)) != 0) { 94 brelse(bp); 95 return (error); 96 } 97 ext2_i2ei(ip, (struct ext2_inode *)((char *)bp->b_data + 98 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number))); 99/* 100 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 101 return (bwrite(bp)); 102 else { 103*/ 104 bdwrite(bp); 105 return (0); 106/* 107 } 108*/ 109} 110 111#define SINGLE 0 /* index of single indirect block */ 112#define DOUBLE 1 /* index of double indirect block */ 113#define TRIPLE 2 /* index of triple indirect block */ 114/* 115 * Truncate the inode oip to at most length size, freeing the 116 * disk blocks. 117 */ 118int 119ext2_truncate(vp, length, flags, cred, td) 120 struct vnode *vp; 121 off_t length; 122 int flags; 123 struct ucred *cred; 124 struct thread *td; 125{ 126 struct vnode *ovp = vp;
| 62 long *); 63 64/* 65 * Update the access, modified, and inode change times as specified by the 66 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode 67 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by 68 * the timestamp update). The IN_LAZYMOD flag is set to force a write 69 * later if not now. If we write now, then clear both IN_MODIFIED and 70 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is 71 * set, then wait for the write to complete. 72 */ 73int 74ext2_update(vp, waitfor) 75 struct vnode *vp; 76 int waitfor; 77{ 78 struct ext2_sb_info *fs; 79 struct buf *bp; 80 struct inode *ip; 81 int error; 82 83 ext2_itimes(vp); 84 ip = VTOI(vp); 85 if ((ip->i_flag & IN_MODIFIED) == 0) 86 return (0); 87 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED); 88 if (vp->v_mount->mnt_flag & MNT_RDONLY) 89 return (0); 90 fs = ip->i_e2fs; 91 if ((error = bread(ip->i_devvp, 92 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 93 (int)fs->s_blocksize, NOCRED, &bp)) != 0) { 94 brelse(bp); 95 return (error); 96 } 97 ext2_i2ei(ip, (struct ext2_inode *)((char *)bp->b_data + 98 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number))); 99/* 100 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 101 return (bwrite(bp)); 102 else { 103*/ 104 bdwrite(bp); 105 return (0); 106/* 107 } 108*/ 109} 110 111#define SINGLE 0 /* index of single indirect block */ 112#define DOUBLE 1 /* index of double indirect block */ 113#define TRIPLE 2 /* index of triple indirect block */ 114/* 115 * Truncate the inode oip to at most length size, freeing the 116 * disk blocks. 117 */ 118int 119ext2_truncate(vp, length, flags, cred, td) 120 struct vnode *vp; 121 off_t length; 122 int flags; 123 struct ucred *cred; 124 struct thread *td; 125{ 126 struct vnode *ovp = vp;
|
127 daddr_t lastblock;
| 127 int32_t lastblock;
|
128 struct inode *oip;
| 128 struct inode *oip;
|
129 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 130 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
| 129 int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 130 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
|
131 struct ext2_sb_info *fs; 132 struct buf *bp; 133 int offset, size, level; 134 long count, nblocks, blocksreleased = 0; 135 int aflags, error, i, allerror; 136 off_t osize; 137/* 138printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length); 139*/ /* 140 * negative file sizes will totally break the code below and 141 * are not meaningful anyways. 142 */ 143 if (length < 0) 144 return EFBIG; 145 146 oip = VTOI(ovp); 147 if (ovp->v_type == VLNK && 148 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) { 149#if DIAGNOSTIC 150 if (length != 0) 151 panic("ext2_truncate: partial truncate of symlink"); 152#endif 153 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 154 oip->i_size = 0; 155 oip->i_flag |= IN_CHANGE | IN_UPDATE; 156 return (ext2_update(ovp, 1)); 157 } 158 if (oip->i_size == length) { 159 oip->i_flag |= IN_CHANGE | IN_UPDATE; 160 return (ext2_update(ovp, 0)); 161 } 162 fs = oip->i_e2fs; 163 osize = oip->i_size; 164 ext2_discard_prealloc(oip); 165 /* 166 * Lengthen the size of the file. We must ensure that the 167 * last byte of the file is allocated. Since the smallest 168 * value of oszie is 0, length will be at least 1. 169 */ 170 if (osize < length) { 171 offset = blkoff(fs, length - 1); 172 lbn = lblkno(fs, length - 1); 173 aflags = B_CLRBUF; 174 if (flags & IO_SYNC) 175 aflags |= B_SYNC; 176 vnode_pager_setsize(ovp, length); 177 if ((error = ext2_balloc(oip, lbn, offset + 1, cred, &bp, 178 aflags)) != 0) 179 return (error); 180 oip->i_size = length; 181 if (aflags & IO_SYNC) 182 bwrite(bp); 183 else 184 bawrite(bp); 185 oip->i_flag |= IN_CHANGE | IN_UPDATE; 186 return (ext2_update(ovp, 1)); 187 } 188 /* 189 * Shorten the size of the file. If the file is not being 190 * truncated to a block boundry, the contents of the 191 * partial block following the end of the file must be 192 * zero'ed in case it ever become accessible again because 193 * of subsequent file growth. 194 */ 195 /* I don't understand the comment above */ 196 offset = blkoff(fs, length); 197 if (offset == 0) { 198 oip->i_size = length; 199 } else { 200 lbn = lblkno(fs, length); 201 aflags = B_CLRBUF; 202 if (flags & IO_SYNC) 203 aflags |= B_SYNC; 204 if ((error = ext2_balloc(oip, lbn, offset, cred, &bp, 205 aflags)) != 0) 206 return (error); 207 oip->i_size = length; 208 size = blksize(fs, oip, lbn); 209 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 210 allocbuf(bp, size); 211 if (aflags & IO_SYNC) 212 bwrite(bp); 213 else 214 bawrite(bp); 215 } 216 /* 217 * Calculate index into inode's block list of 218 * last direct and indirect blocks (if any) 219 * which we want to keep. Lastblock is -1 when 220 * the file is truncated to 0. 221 */ 222 lastblock = lblkno(fs, length + fs->s_blocksize - 1) - 1; 223 lastiblock[SINGLE] = lastblock - NDADDR; 224 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 225 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 226 nblocks = btodb(fs->s_blocksize); 227 /* 228 * Update file and block pointers on disk before we start freeing 229 * blocks. If we crash before free'ing blocks below, the blocks 230 * will be returned to the free list. lastiblock values are also 231 * normalized to -1 for calls to ext2_indirtrunc below. 232 */ 233 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); 234 for (level = TRIPLE; level >= SINGLE; level--) 235 if (lastiblock[level] < 0) { 236 oip->i_ib[level] = 0; 237 lastiblock[level] = -1; 238 } 239 for (i = NDADDR - 1; i > lastblock; i--) 240 oip->i_db[i] = 0; 241 oip->i_flag |= IN_CHANGE | IN_UPDATE; 242 allerror = ext2_update(ovp, 1); 243 244 /* 245 * Having written the new inode to disk, save its new configuration 246 * and put back the old block pointers long enough to process them. 247 * Note that we save the new block configuration so we can check it 248 * when we are done. 249 */ 250 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); 251 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); 252 oip->i_size = osize; 253 error = vtruncbuf(ovp, cred, td, length, (int)fs->s_blocksize); 254 if (error && (allerror == 0)) 255 allerror = error; 256 257 /* 258 * Indirect blocks first. 259 */ 260 indir_lbn[SINGLE] = -NDADDR; 261 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 262 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 263 for (level = TRIPLE; level >= SINGLE; level--) { 264 bn = oip->i_ib[level]; 265 if (bn != 0) { 266 error = ext2_indirtrunc(oip, indir_lbn[level], 267 fsbtodb(fs, bn), lastiblock[level], level, &count); 268 if (error) 269 allerror = error; 270 blocksreleased += count; 271 if (lastiblock[level] < 0) { 272 oip->i_ib[level] = 0; 273 ext2_blkfree(oip, bn, fs->s_frag_size); 274 blocksreleased += nblocks; 275 } 276 } 277 if (lastiblock[level] >= 0) 278 goto done; 279 } 280 281 /* 282 * All whole direct blocks or frags. 283 */ 284 for (i = NDADDR - 1; i > lastblock; i--) { 285 long bsize; 286 287 bn = oip->i_db[i]; 288 if (bn == 0) 289 continue; 290 oip->i_db[i] = 0; 291 bsize = blksize(fs, oip, i); 292 ext2_blkfree(oip, bn, bsize); 293 blocksreleased += btodb(bsize); 294 } 295 if (lastblock < 0) 296 goto done; 297 298 /* 299 * Finally, look for a change in size of the 300 * last direct block; release any frags. 301 */ 302 bn = oip->i_db[lastblock]; 303 if (bn != 0) { 304 long oldspace, newspace; 305 306 /* 307 * Calculate amount of space we're giving 308 * back as old block size minus new block size. 309 */ 310 oldspace = blksize(fs, oip, lastblock); 311 oip->i_size = length; 312 newspace = blksize(fs, oip, lastblock); 313 if (newspace == 0) 314 panic("itrunc: newspace"); 315 if (oldspace - newspace > 0) { 316 /* 317 * Block number of space to be free'd is 318 * the old block # plus the number of frags 319 * required for the storage we're keeping. 320 */ 321 bn += numfrags(fs, newspace); 322 ext2_blkfree(oip, bn, oldspace - newspace); 323 blocksreleased += btodb(oldspace - newspace); 324 } 325 } 326done: 327#if DIAGNOSTIC 328 for (level = SINGLE; level <= TRIPLE; level++) 329 if (newblks[NDADDR + level] != oip->i_ib[level]) 330 panic("itrunc1"); 331 for (i = 0; i < NDADDR; i++) 332 if (newblks[i] != oip->i_db[i]) 333 panic("itrunc2"); 334 if (length == 0 && (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) || 335 !TAILQ_EMPTY(&ovp->v_cleanblkhd))) 336 panic("itrunc3"); 337#endif /* DIAGNOSTIC */ 338 /* 339 * Put back the real size. 340 */ 341 oip->i_size = length; 342 oip->i_blocks -= blocksreleased; 343 if (oip->i_blocks < 0) /* sanity */ 344 oip->i_blocks = 0; 345 oip->i_flag |= IN_CHANGE; 346 vnode_pager_setsize(ovp, length); 347 return (allerror); 348} 349 350/* 351 * Release blocks associated with the inode ip and stored in the indirect 352 * block bn. Blocks are free'd in LIFO order up to (but not including) 353 * lastbn. If level is greater than SINGLE, the block is an indirect block 354 * and recursive calls to indirtrunc must be used to cleanse other indirect 355 * blocks. 356 * 357 * NB: triple indirect blocks are untested. 358 */ 359 360static int 361ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 362 struct inode *ip;
| 131 struct ext2_sb_info *fs; 132 struct buf *bp; 133 int offset, size, level; 134 long count, nblocks, blocksreleased = 0; 135 int aflags, error, i, allerror; 136 off_t osize; 137/* 138printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length); 139*/ /* 140 * negative file sizes will totally break the code below and 141 * are not meaningful anyways. 142 */ 143 if (length < 0) 144 return EFBIG; 145 146 oip = VTOI(ovp); 147 if (ovp->v_type == VLNK && 148 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) { 149#if DIAGNOSTIC 150 if (length != 0) 151 panic("ext2_truncate: partial truncate of symlink"); 152#endif 153 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); 154 oip->i_size = 0; 155 oip->i_flag |= IN_CHANGE | IN_UPDATE; 156 return (ext2_update(ovp, 1)); 157 } 158 if (oip->i_size == length) { 159 oip->i_flag |= IN_CHANGE | IN_UPDATE; 160 return (ext2_update(ovp, 0)); 161 } 162 fs = oip->i_e2fs; 163 osize = oip->i_size; 164 ext2_discard_prealloc(oip); 165 /* 166 * Lengthen the size of the file. We must ensure that the 167 * last byte of the file is allocated. Since the smallest 168 * value of oszie is 0, length will be at least 1. 169 */ 170 if (osize < length) { 171 offset = blkoff(fs, length - 1); 172 lbn = lblkno(fs, length - 1); 173 aflags = B_CLRBUF; 174 if (flags & IO_SYNC) 175 aflags |= B_SYNC; 176 vnode_pager_setsize(ovp, length); 177 if ((error = ext2_balloc(oip, lbn, offset + 1, cred, &bp, 178 aflags)) != 0) 179 return (error); 180 oip->i_size = length; 181 if (aflags & IO_SYNC) 182 bwrite(bp); 183 else 184 bawrite(bp); 185 oip->i_flag |= IN_CHANGE | IN_UPDATE; 186 return (ext2_update(ovp, 1)); 187 } 188 /* 189 * Shorten the size of the file. If the file is not being 190 * truncated to a block boundry, the contents of the 191 * partial block following the end of the file must be 192 * zero'ed in case it ever become accessible again because 193 * of subsequent file growth. 194 */ 195 /* I don't understand the comment above */ 196 offset = blkoff(fs, length); 197 if (offset == 0) { 198 oip->i_size = length; 199 } else { 200 lbn = lblkno(fs, length); 201 aflags = B_CLRBUF; 202 if (flags & IO_SYNC) 203 aflags |= B_SYNC; 204 if ((error = ext2_balloc(oip, lbn, offset, cred, &bp, 205 aflags)) != 0) 206 return (error); 207 oip->i_size = length; 208 size = blksize(fs, oip, lbn); 209 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 210 allocbuf(bp, size); 211 if (aflags & IO_SYNC) 212 bwrite(bp); 213 else 214 bawrite(bp); 215 } 216 /* 217 * Calculate index into inode's block list of 218 * last direct and indirect blocks (if any) 219 * which we want to keep. Lastblock is -1 when 220 * the file is truncated to 0. 221 */ 222 lastblock = lblkno(fs, length + fs->s_blocksize - 1) - 1; 223 lastiblock[SINGLE] = lastblock - NDADDR; 224 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 225 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 226 nblocks = btodb(fs->s_blocksize); 227 /* 228 * Update file and block pointers on disk before we start freeing 229 * blocks. If we crash before free'ing blocks below, the blocks 230 * will be returned to the free list. lastiblock values are also 231 * normalized to -1 for calls to ext2_indirtrunc below. 232 */ 233 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); 234 for (level = TRIPLE; level >= SINGLE; level--) 235 if (lastiblock[level] < 0) { 236 oip->i_ib[level] = 0; 237 lastiblock[level] = -1; 238 } 239 for (i = NDADDR - 1; i > lastblock; i--) 240 oip->i_db[i] = 0; 241 oip->i_flag |= IN_CHANGE | IN_UPDATE; 242 allerror = ext2_update(ovp, 1); 243 244 /* 245 * Having written the new inode to disk, save its new configuration 246 * and put back the old block pointers long enough to process them. 247 * Note that we save the new block configuration so we can check it 248 * when we are done. 249 */ 250 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); 251 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); 252 oip->i_size = osize; 253 error = vtruncbuf(ovp, cred, td, length, (int)fs->s_blocksize); 254 if (error && (allerror == 0)) 255 allerror = error; 256 257 /* 258 * Indirect blocks first. 259 */ 260 indir_lbn[SINGLE] = -NDADDR; 261 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 262 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 263 for (level = TRIPLE; level >= SINGLE; level--) { 264 bn = oip->i_ib[level]; 265 if (bn != 0) { 266 error = ext2_indirtrunc(oip, indir_lbn[level], 267 fsbtodb(fs, bn), lastiblock[level], level, &count); 268 if (error) 269 allerror = error; 270 blocksreleased += count; 271 if (lastiblock[level] < 0) { 272 oip->i_ib[level] = 0; 273 ext2_blkfree(oip, bn, fs->s_frag_size); 274 blocksreleased += nblocks; 275 } 276 } 277 if (lastiblock[level] >= 0) 278 goto done; 279 } 280 281 /* 282 * All whole direct blocks or frags. 283 */ 284 for (i = NDADDR - 1; i > lastblock; i--) { 285 long bsize; 286 287 bn = oip->i_db[i]; 288 if (bn == 0) 289 continue; 290 oip->i_db[i] = 0; 291 bsize = blksize(fs, oip, i); 292 ext2_blkfree(oip, bn, bsize); 293 blocksreleased += btodb(bsize); 294 } 295 if (lastblock < 0) 296 goto done; 297 298 /* 299 * Finally, look for a change in size of the 300 * last direct block; release any frags. 301 */ 302 bn = oip->i_db[lastblock]; 303 if (bn != 0) { 304 long oldspace, newspace; 305 306 /* 307 * Calculate amount of space we're giving 308 * back as old block size minus new block size. 309 */ 310 oldspace = blksize(fs, oip, lastblock); 311 oip->i_size = length; 312 newspace = blksize(fs, oip, lastblock); 313 if (newspace == 0) 314 panic("itrunc: newspace"); 315 if (oldspace - newspace > 0) { 316 /* 317 * Block number of space to be free'd is 318 * the old block # plus the number of frags 319 * required for the storage we're keeping. 320 */ 321 bn += numfrags(fs, newspace); 322 ext2_blkfree(oip, bn, oldspace - newspace); 323 blocksreleased += btodb(oldspace - newspace); 324 } 325 } 326done: 327#if DIAGNOSTIC 328 for (level = SINGLE; level <= TRIPLE; level++) 329 if (newblks[NDADDR + level] != oip->i_ib[level]) 330 panic("itrunc1"); 331 for (i = 0; i < NDADDR; i++) 332 if (newblks[i] != oip->i_db[i]) 333 panic("itrunc2"); 334 if (length == 0 && (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) || 335 !TAILQ_EMPTY(&ovp->v_cleanblkhd))) 336 panic("itrunc3"); 337#endif /* DIAGNOSTIC */ 338 /* 339 * Put back the real size. 340 */ 341 oip->i_size = length; 342 oip->i_blocks -= blocksreleased; 343 if (oip->i_blocks < 0) /* sanity */ 344 oip->i_blocks = 0; 345 oip->i_flag |= IN_CHANGE; 346 vnode_pager_setsize(ovp, length); 347 return (allerror); 348} 349 350/* 351 * Release blocks associated with the inode ip and stored in the indirect 352 * block bn. Blocks are free'd in LIFO order up to (but not including) 353 * lastbn. If level is greater than SINGLE, the block is an indirect block 354 * and recursive calls to indirtrunc must be used to cleanse other indirect 355 * blocks. 356 * 357 * NB: triple indirect blocks are untested. 358 */ 359 360static int 361ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp) 362 struct inode *ip;
|
363 daddr_t lbn, lastbn; 364 daddr_t dbn;
| 363 int32_t lbn, lastbn; 364 int32_t dbn;
|
365 int level; 366 long *countp; 367{ 368 struct buf *bp; 369 struct ext2_sb_info *fs = ip->i_e2fs; 370 struct vnode *vp;
| 365 int level; 366 long *countp; 367{ 368 struct buf *bp; 369 struct ext2_sb_info *fs = ip->i_e2fs; 370 struct vnode *vp;
|
371 daddr_t *bap, *copy, nb, nlbn, last;
| 371 int32_t *bap, *copy, nb, nlbn, last;
|
372 long blkcount, factor; 373 int i, nblocks, blocksreleased = 0; 374 int error = 0, allerror = 0; 375 376 /* 377 * Calculate index in current block of last 378 * block to be kept. -1 indicates the entire 379 * block so we need not calculate the index. 380 */ 381 factor = 1; 382 for (i = SINGLE; i < level; i++) 383 factor *= NINDIR(fs); 384 last = lastbn; 385 if (lastbn > 0) 386 last /= factor; 387 nblocks = btodb(fs->s_blocksize); 388 /* 389 * Get buffer of block pointers, zero those entries corresponding 390 * to blocks to be free'd, and update on disk copy first. Since 391 * double(triple) indirect before single(double) indirect, calls 392 * to bmap on these blocks will fail. However, we already have 393 * the on disk address, so we have to set the b_blkno field 394 * explicitly instead of letting bread do everything for us. 395 */ 396 vp = ITOV(ip); 397 bp = getblk(vp, lbn, (int)fs->s_blocksize, 0, 0); 398 if (bp->b_flags & (B_DONE | B_DELWRI)) { 399 } else { 400 bp->b_iocmd = BIO_READ; 401 if (bp->b_bcount > bp->b_bufsize) 402 panic("ext2_indirtrunc: bad buffer size"); 403 bp->b_blkno = dbn; 404 vfs_busy_pages(bp, 0); 405 VOP_STRATEGY(vp, bp); 406 error = bufwait(bp); 407 } 408 if (error) { 409 brelse(bp); 410 *countp = 0; 411 return (error); 412 } 413
| 372 long blkcount, factor; 373 int i, nblocks, blocksreleased = 0; 374 int error = 0, allerror = 0; 375 376 /* 377 * Calculate index in current block of last 378 * block to be kept. -1 indicates the entire 379 * block so we need not calculate the index. 380 */ 381 factor = 1; 382 for (i = SINGLE; i < level; i++) 383 factor *= NINDIR(fs); 384 last = lastbn; 385 if (lastbn > 0) 386 last /= factor; 387 nblocks = btodb(fs->s_blocksize); 388 /* 389 * Get buffer of block pointers, zero those entries corresponding 390 * to blocks to be free'd, and update on disk copy first. Since 391 * double(triple) indirect before single(double) indirect, calls 392 * to bmap on these blocks will fail. However, we already have 393 * the on disk address, so we have to set the b_blkno field 394 * explicitly instead of letting bread do everything for us. 395 */ 396 vp = ITOV(ip); 397 bp = getblk(vp, lbn, (int)fs->s_blocksize, 0, 0); 398 if (bp->b_flags & (B_DONE | B_DELWRI)) { 399 } else { 400 bp->b_iocmd = BIO_READ; 401 if (bp->b_bcount > bp->b_bufsize) 402 panic("ext2_indirtrunc: bad buffer size"); 403 bp->b_blkno = dbn; 404 vfs_busy_pages(bp, 0); 405 VOP_STRATEGY(vp, bp); 406 error = bufwait(bp); 407 } 408 if (error) { 409 brelse(bp); 410 *countp = 0; 411 return (error); 412 } 413
|
414 bap = (daddr_t *)bp->b_data; 415 MALLOC(copy, daddr_t *, fs->s_blocksize, M_TEMP, M_WAITOK);
| 414 bap = (int32_t *)bp->b_data; 415 MALLOC(copy, int32_t *, fs->s_blocksize, M_TEMP, M_WAITOK);
|
416 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->s_blocksize); 417 bzero((caddr_t)&bap[last + 1],
| 416 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->s_blocksize); 417 bzero((caddr_t)&bap[last + 1],
|
418 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
| 418 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
|
419 if (last == -1) 420 bp->b_flags |= B_INVAL; 421 error = bwrite(bp); 422 if (error) 423 allerror = error; 424 bap = copy; 425 426 /* 427 * Recursively free totally unused blocks. 428 */ 429 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 430 i--, nlbn += factor) { 431 nb = bap[i]; 432 if (nb == 0) 433 continue; 434 if (level > SINGLE) { 435 if ((error = ext2_indirtrunc(ip, nlbn,
| 419 if (last == -1) 420 bp->b_flags |= B_INVAL; 421 error = bwrite(bp); 422 if (error) 423 allerror = error; 424 bap = copy; 425 426 /* 427 * Recursively free totally unused blocks. 428 */ 429 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 430 i--, nlbn += factor) { 431 nb = bap[i]; 432 if (nb == 0) 433 continue; 434 if (level > SINGLE) { 435 if ((error = ext2_indirtrunc(ip, nlbn,
|
436 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount)) != 0)
| 436 fsbtodb(fs, nb), (int32_t)-1, level - 1, &blkcount)) != 0)
|
437 allerror = error; 438 blocksreleased += blkcount; 439 } 440 ext2_blkfree(ip, nb, fs->s_blocksize); 441 blocksreleased += nblocks; 442 } 443 444 /* 445 * Recursively free last partial block. 446 */ 447 if (level > SINGLE && lastbn >= 0) { 448 last = lastbn % factor; 449 nb = bap[i]; 450 if (nb != 0) { 451 if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 452 last, level - 1, &blkcount)) != 0) 453 allerror = error; 454 blocksreleased += blkcount; 455 } 456 } 457 FREE(copy, M_TEMP); 458 *countp = blocksreleased; 459 return (allerror); 460} 461 462/* 463 * discard preallocated blocks 464 */ 465int 466ext2_inactive(ap) 467 struct vop_inactive_args /* { 468 struct vnode *a_vp; 469 struct thread *a_td; 470 } */ *ap; 471{ 472 struct vnode *vp = ap->a_vp; 473 struct inode *ip = VTOI(vp); 474 struct thread *td = ap->a_td; 475 int mode, error = 0; 476 477 ext2_discard_prealloc(ip); 478 if (prtactive && vp->v_usecount != 0) 479 vprint("ext2_inactive: pushing active", vp); 480 481 /* 482 * Ignore inodes related to stale file handles. 483 */ 484 if (ip->i_mode == 0) 485 goto out; 486 if (ip->i_nlink <= 0) { 487 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 488 error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td); 489 ip->i_rdev = 0; 490 mode = ip->i_mode; 491 ip->i_mode = 0; 492 ip->i_flag |= IN_CHANGE | IN_UPDATE; 493 ext2_vfree(vp, ip->i_number, mode); 494 } 495 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 496 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 && 497 vn_write_suspend_wait(vp, NULL, V_NOWAIT)) { 498 ip->i_flag &= ~IN_ACCESS; 499 } else { 500 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 501 ext2_update(vp, 0); 502 } 503 } 504out: 505 VOP_UNLOCK(vp, 0, td); 506 /* 507 * If we are done with the inode, reclaim it 508 * so that it can be reused immediately. 509 */ 510 if (ip->i_mode == 0) 511 vrecycle(vp, NULL, td); 512 return (error); 513} 514 515/* 516 * Reclaim an inode so that it can be used for other purposes. 517 */ 518int 519ext2_reclaim(ap) 520 struct vop_reclaim_args /* { 521 struct vnode *a_vp; 522 struct thread *a_td; 523 } */ *ap; 524{ 525 struct inode *ip; 526 struct vnode *vp = ap->a_vp; 527 528 if (prtactive && vp->v_usecount != 0) 529 vprint("ufs_reclaim: pushing active", vp); 530 ip = VTOI(vp); 531 if (ip->i_flag & IN_LAZYMOD) { 532 ip->i_flag |= IN_MODIFIED; 533 ext2_update(vp, 0); 534 } 535 /* 536 * Remove the inode from its hash chain. 537 */ 538 ext2_ihashrem(ip); 539 /* 540 * Purge old data structures associated with the inode. 541 */ 542 cache_purge(vp); 543 if (ip->i_devvp) { 544 vrele(ip->i_devvp); 545 ip->i_devvp = 0; 546 } 547 lockdestroy(&vp->v_lock); 548 FREE(vp->v_data, M_EXT2NODE); 549 vp->v_data = 0; 550 return (0); 551}
| 437 allerror = error; 438 blocksreleased += blkcount; 439 } 440 ext2_blkfree(ip, nb, fs->s_blocksize); 441 blocksreleased += nblocks; 442 } 443 444 /* 445 * Recursively free last partial block. 446 */ 447 if (level > SINGLE && lastbn >= 0) { 448 last = lastbn % factor; 449 nb = bap[i]; 450 if (nb != 0) { 451 if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb), 452 last, level - 1, &blkcount)) != 0) 453 allerror = error; 454 blocksreleased += blkcount; 455 } 456 } 457 FREE(copy, M_TEMP); 458 *countp = blocksreleased; 459 return (allerror); 460} 461 462/* 463 * discard preallocated blocks 464 */ 465int 466ext2_inactive(ap) 467 struct vop_inactive_args /* { 468 struct vnode *a_vp; 469 struct thread *a_td; 470 } */ *ap; 471{ 472 struct vnode *vp = ap->a_vp; 473 struct inode *ip = VTOI(vp); 474 struct thread *td = ap->a_td; 475 int mode, error = 0; 476 477 ext2_discard_prealloc(ip); 478 if (prtactive && vp->v_usecount != 0) 479 vprint("ext2_inactive: pushing active", vp); 480 481 /* 482 * Ignore inodes related to stale file handles. 483 */ 484 if (ip->i_mode == 0) 485 goto out; 486 if (ip->i_nlink <= 0) { 487 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 488 error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td); 489 ip->i_rdev = 0; 490 mode = ip->i_mode; 491 ip->i_mode = 0; 492 ip->i_flag |= IN_CHANGE | IN_UPDATE; 493 ext2_vfree(vp, ip->i_number, mode); 494 } 495 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 496 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 && 497 vn_write_suspend_wait(vp, NULL, V_NOWAIT)) { 498 ip->i_flag &= ~IN_ACCESS; 499 } else { 500 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 501 ext2_update(vp, 0); 502 } 503 } 504out: 505 VOP_UNLOCK(vp, 0, td); 506 /* 507 * If we are done with the inode, reclaim it 508 * so that it can be reused immediately. 509 */ 510 if (ip->i_mode == 0) 511 vrecycle(vp, NULL, td); 512 return (error); 513} 514 515/* 516 * Reclaim an inode so that it can be used for other purposes. 517 */ 518int 519ext2_reclaim(ap) 520 struct vop_reclaim_args /* { 521 struct vnode *a_vp; 522 struct thread *a_td; 523 } */ *ap; 524{ 525 struct inode *ip; 526 struct vnode *vp = ap->a_vp; 527 528 if (prtactive && vp->v_usecount != 0) 529 vprint("ufs_reclaim: pushing active", vp); 530 ip = VTOI(vp); 531 if (ip->i_flag & IN_LAZYMOD) { 532 ip->i_flag |= IN_MODIFIED; 533 ext2_update(vp, 0); 534 } 535 /* 536 * Remove the inode from its hash chain. 537 */ 538 ext2_ihashrem(ip); 539 /* 540 * Purge old data structures associated with the inode. 541 */ 542 cache_purge(vp); 543 if (ip->i_devvp) { 544 vrele(ip->i_devvp); 545 ip->i_devvp = 0; 546 } 547 lockdestroy(&vp->v_lock); 548 FREE(vp->v_data, M_EXT2NODE); 549 vp->v_data = 0; 550 return (0); 551}
|