Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ufs/truncate.c |
| 3 | * |
| 4 | * Copyright (C) 1998 |
| 5 | * Daniel Pirkl <daniel.pirkl@email.cz> |
| 6 | * Charles University, Faculty of Mathematics and Physics |
| 7 | * |
| 8 | * from |
| 9 | * |
| 10 | * linux/fs/ext2/truncate.c |
| 11 | * |
| 12 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 13 | * Remy Card (card@masi.ibp.fr) |
| 14 | * Laboratoire MASI - Institut Blaise Pascal |
| 15 | * Universite Pierre et Marie Curie (Paris VI) |
| 16 | * |
| 17 | * from |
| 18 | * |
| 19 | * linux/fs/minix/truncate.c |
| 20 | * |
| 21 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 22 | * |
| 23 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 24 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * Real random numbers for secure rm added 94/02/18 |
| 29 | * Idea from Pierre del Perugia <delperug@gla.ecoledoc.ibp.fr> |
| 30 | */ |
| 31 | |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 32 | /* |
| 33 | * Modified to avoid infinite loop on 2006 by |
| 34 | * Evgeniy Dushistov <dushistov@mail.ru> |
| 35 | */ |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/errno.h> |
| 38 | #include <linux/fs.h> |
| 39 | #include <linux/ufs_fs.h> |
| 40 | #include <linux/fcntl.h> |
| 41 | #include <linux/time.h> |
| 42 | #include <linux/stat.h> |
| 43 | #include <linux/string.h> |
| 44 | #include <linux/smp_lock.h> |
| 45 | #include <linux/buffer_head.h> |
| 46 | #include <linux/blkdev.h> |
| 47 | #include <linux/sched.h> |
| 48 | |
| 49 | #include "swab.h" |
| 50 | #include "util.h" |
| 51 | |
| 52 | #undef UFS_TRUNCATE_DEBUG |
| 53 | |
| 54 | #ifdef UFS_TRUNCATE_DEBUG |
| 55 | #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x; |
| 56 | #else |
| 57 | #define UFSD(x) |
| 58 | #endif |
| 59 | |
| 60 | /* |
| 61 | * Secure deletion currently doesn't work. It interacts very badly |
| 62 | * with buffers shared with memory mappings, and for that reason |
| 63 | * can't be done in the truncate() routines. It should instead be |
| 64 | * done separately in "release()" before calling the truncate routines |
| 65 | * that will release the actual file blocks. |
| 66 | * |
| 67 | * Linus |
| 68 | */ |
| 69 | |
| 70 | #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift) |
| 71 | #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift) |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | static int ufs_trunc_direct (struct inode * inode) |
| 75 | { |
| 76 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 77 | struct super_block * sb; |
| 78 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | __fs32 * p; |
| 80 | unsigned frag1, frag2, frag3, frag4, block1, block2; |
| 81 | unsigned frag_to_free, free_count; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 82 | unsigned i, tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int retry; |
| 84 | |
| 85 | UFSD(("ENTER\n")) |
| 86 | |
| 87 | sb = inode->i_sb; |
| 88 | uspi = UFS_SB(sb)->s_uspi; |
| 89 | |
| 90 | frag_to_free = 0; |
| 91 | free_count = 0; |
| 92 | retry = 0; |
| 93 | |
| 94 | frag1 = DIRECT_FRAGMENT; |
| 95 | frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag); |
| 96 | frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1); |
| 97 | frag3 = frag4 & ~uspi->s_fpbmask; |
| 98 | block1 = block2 = 0; |
| 99 | if (frag2 > frag3) { |
| 100 | frag2 = frag4; |
| 101 | frag3 = frag4 = 0; |
| 102 | } |
| 103 | else if (frag2 < frag3) { |
| 104 | block1 = ufs_fragstoblks (frag2); |
| 105 | block2 = ufs_fragstoblks (frag3); |
| 106 | } |
| 107 | |
| 108 | UFSD(("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4)) |
| 109 | |
| 110 | if (frag1 >= frag2) |
| 111 | goto next1; |
| 112 | |
| 113 | /* |
| 114 | * Free first free fragments |
| 115 | */ |
| 116 | p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1); |
| 117 | tmp = fs32_to_cpu(sb, *p); |
| 118 | if (!tmp ) |
| 119 | ufs_panic (sb, "ufs_trunc_direct", "internal error"); |
| 120 | frag1 = ufs_fragnum (frag1); |
| 121 | frag2 = ufs_fragnum (frag2); |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | inode->i_blocks -= (frag2-frag1) << uspi->s_nspfshift; |
| 124 | mark_inode_dirty(inode); |
| 125 | ufs_free_fragments (inode, tmp + frag1, frag2 - frag1); |
| 126 | frag_to_free = tmp + frag1; |
| 127 | |
| 128 | next1: |
| 129 | /* |
| 130 | * Free whole blocks |
| 131 | */ |
| 132 | for (i = block1 ; i < block2; i++) { |
| 133 | p = ufsi->i_u1.i_data + i; |
| 134 | tmp = fs32_to_cpu(sb, *p); |
| 135 | if (!tmp) |
| 136 | continue; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | *p = 0; |
| 139 | inode->i_blocks -= uspi->s_nspb; |
| 140 | mark_inode_dirty(inode); |
| 141 | if (free_count == 0) { |
| 142 | frag_to_free = tmp; |
| 143 | free_count = uspi->s_fpb; |
| 144 | } else if (free_count > 0 && frag_to_free == tmp - free_count) |
| 145 | free_count += uspi->s_fpb; |
| 146 | else { |
| 147 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 148 | frag_to_free = tmp; |
| 149 | free_count = uspi->s_fpb; |
| 150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (free_count > 0) |
| 154 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 155 | |
| 156 | if (frag3 >= frag4) |
| 157 | goto next3; |
| 158 | |
| 159 | /* |
| 160 | * Free last free fragments |
| 161 | */ |
| 162 | p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3); |
| 163 | tmp = fs32_to_cpu(sb, *p); |
| 164 | if (!tmp ) |
| 165 | ufs_panic(sb, "ufs_truncate_direct", "internal error"); |
| 166 | frag4 = ufs_fragnum (frag4); |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | *p = 0; |
| 169 | inode->i_blocks -= frag4 << uspi->s_nspfshift; |
| 170 | mark_inode_dirty(inode); |
| 171 | ufs_free_fragments (inode, tmp, frag4); |
| 172 | next3: |
| 173 | |
| 174 | UFSD(("EXIT\n")) |
| 175 | return retry; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p) |
| 180 | { |
| 181 | struct super_block * sb; |
| 182 | struct ufs_sb_private_info * uspi; |
| 183 | struct ufs_buffer_head * ind_ubh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | __fs32 * ind; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 185 | unsigned indirect_block, i, tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | unsigned frag_to_free, free_count; |
| 187 | int retry; |
| 188 | |
| 189 | UFSD(("ENTER\n")) |
| 190 | |
| 191 | sb = inode->i_sb; |
| 192 | uspi = UFS_SB(sb)->s_uspi; |
| 193 | |
| 194 | frag_to_free = 0; |
| 195 | free_count = 0; |
| 196 | retry = 0; |
| 197 | |
| 198 | tmp = fs32_to_cpu(sb, *p); |
| 199 | if (!tmp) |
| 200 | return 0; |
| 201 | ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize); |
| 202 | if (tmp != fs32_to_cpu(sb, *p)) { |
| 203 | ubh_brelse (ind_ubh); |
| 204 | return 1; |
| 205 | } |
| 206 | if (!ind_ubh) { |
| 207 | *p = 0; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0; |
| 212 | for (i = indirect_block; i < uspi->s_apb; i++) { |
| 213 | ind = ubh_get_addr32 (ind_ubh, i); |
| 214 | tmp = fs32_to_cpu(sb, *ind); |
| 215 | if (!tmp) |
| 216 | continue; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | *ind = 0; |
| 219 | ubh_mark_buffer_dirty(ind_ubh); |
| 220 | if (free_count == 0) { |
| 221 | frag_to_free = tmp; |
| 222 | free_count = uspi->s_fpb; |
| 223 | } else if (free_count > 0 && frag_to_free == tmp - free_count) |
| 224 | free_count += uspi->s_fpb; |
| 225 | else { |
| 226 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 227 | frag_to_free = tmp; |
| 228 | free_count = uspi->s_fpb; |
| 229 | } |
| 230 | inode->i_blocks -= uspi->s_nspb; |
| 231 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | if (free_count > 0) { |
| 235 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 236 | } |
| 237 | for (i = 0; i < uspi->s_apb; i++) |
| 238 | if (*ubh_get_addr32(ind_ubh,i)) |
| 239 | break; |
| 240 | if (i >= uspi->s_apb) { |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 241 | tmp = fs32_to_cpu(sb, *p); |
| 242 | *p = 0; |
| 243 | inode->i_blocks -= uspi->s_nspb; |
| 244 | mark_inode_dirty(inode); |
| 245 | ufs_free_blocks (inode, tmp, uspi->s_fpb); |
| 246 | ubh_bforget(ind_ubh); |
| 247 | ind_ubh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) { |
Jan Kara | 096125f3 | 2005-09-06 15:19:15 -0700 | [diff] [blame] | 250 | ubh_ll_rw_block (SWRITE, 1, &ind_ubh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | ubh_wait_on_buffer (ind_ubh); |
| 252 | } |
| 253 | ubh_brelse (ind_ubh); |
| 254 | |
| 255 | UFSD(("EXIT\n")) |
| 256 | |
| 257 | return retry; |
| 258 | } |
| 259 | |
| 260 | static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p) |
| 261 | { |
| 262 | struct super_block * sb; |
| 263 | struct ufs_sb_private_info * uspi; |
| 264 | struct ufs_buffer_head * dind_bh; |
| 265 | unsigned i, tmp, dindirect_block; |
| 266 | __fs32 * dind; |
| 267 | int retry = 0; |
| 268 | |
| 269 | UFSD(("ENTER\n")) |
| 270 | |
| 271 | sb = inode->i_sb; |
| 272 | uspi = UFS_SB(sb)->s_uspi; |
| 273 | |
| 274 | dindirect_block = (DIRECT_BLOCK > offset) |
| 275 | ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0; |
| 276 | retry = 0; |
| 277 | |
| 278 | tmp = fs32_to_cpu(sb, *p); |
| 279 | if (!tmp) |
| 280 | return 0; |
| 281 | dind_bh = ubh_bread(sb, tmp, uspi->s_bsize); |
| 282 | if (tmp != fs32_to_cpu(sb, *p)) { |
| 283 | ubh_brelse (dind_bh); |
| 284 | return 1; |
| 285 | } |
| 286 | if (!dind_bh) { |
| 287 | *p = 0; |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | for (i = dindirect_block ; i < uspi->s_apb ; i++) { |
| 292 | dind = ubh_get_addr32 (dind_bh, i); |
| 293 | tmp = fs32_to_cpu(sb, *dind); |
| 294 | if (!tmp) |
| 295 | continue; |
| 296 | retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind); |
| 297 | ubh_mark_buffer_dirty(dind_bh); |
| 298 | } |
| 299 | |
| 300 | for (i = 0; i < uspi->s_apb; i++) |
| 301 | if (*ubh_get_addr32 (dind_bh, i)) |
| 302 | break; |
| 303 | if (i >= uspi->s_apb) { |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 304 | tmp = fs32_to_cpu(sb, *p); |
| 305 | *p = 0; |
| 306 | inode->i_blocks -= uspi->s_nspb; |
| 307 | mark_inode_dirty(inode); |
| 308 | ufs_free_blocks (inode, tmp, uspi->s_fpb); |
| 309 | ubh_bforget(dind_bh); |
| 310 | dind_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) { |
Jan Kara | 096125f3 | 2005-09-06 15:19:15 -0700 | [diff] [blame] | 313 | ubh_ll_rw_block (SWRITE, 1, &dind_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | ubh_wait_on_buffer (dind_bh); |
| 315 | } |
| 316 | ubh_brelse (dind_bh); |
| 317 | |
| 318 | UFSD(("EXIT\n")) |
| 319 | |
| 320 | return retry; |
| 321 | } |
| 322 | |
| 323 | static int ufs_trunc_tindirect (struct inode * inode) |
| 324 | { |
| 325 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 326 | struct super_block * sb; |
| 327 | struct ufs_sb_private_info * uspi; |
| 328 | struct ufs_buffer_head * tind_bh; |
| 329 | unsigned tindirect_block, tmp, i; |
| 330 | __fs32 * tind, * p; |
| 331 | int retry; |
| 332 | |
| 333 | UFSD(("ENTER\n")) |
| 334 | |
| 335 | sb = inode->i_sb; |
| 336 | uspi = UFS_SB(sb)->s_uspi; |
| 337 | retry = 0; |
| 338 | |
| 339 | tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb)) |
| 340 | ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0; |
| 341 | p = ufsi->i_u1.i_data + UFS_TIND_BLOCK; |
| 342 | if (!(tmp = fs32_to_cpu(sb, *p))) |
| 343 | return 0; |
| 344 | tind_bh = ubh_bread (sb, tmp, uspi->s_bsize); |
| 345 | if (tmp != fs32_to_cpu(sb, *p)) { |
| 346 | ubh_brelse (tind_bh); |
| 347 | return 1; |
| 348 | } |
| 349 | if (!tind_bh) { |
| 350 | *p = 0; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | for (i = tindirect_block ; i < uspi->s_apb ; i++) { |
| 355 | tind = ubh_get_addr32 (tind_bh, i); |
| 356 | retry |= ufs_trunc_dindirect(inode, UFS_NDADDR + |
| 357 | uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind); |
| 358 | ubh_mark_buffer_dirty(tind_bh); |
| 359 | } |
| 360 | for (i = 0; i < uspi->s_apb; i++) |
| 361 | if (*ubh_get_addr32 (tind_bh, i)) |
| 362 | break; |
| 363 | if (i >= uspi->s_apb) { |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 364 | tmp = fs32_to_cpu(sb, *p); |
| 365 | *p = 0; |
| 366 | inode->i_blocks -= uspi->s_nspb; |
| 367 | mark_inode_dirty(inode); |
| 368 | ufs_free_blocks (inode, tmp, uspi->s_fpb); |
| 369 | ubh_bforget(tind_bh); |
| 370 | tind_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) { |
Jan Kara | 096125f3 | 2005-09-06 15:19:15 -0700 | [diff] [blame] | 373 | ubh_ll_rw_block (SWRITE, 1, &tind_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | ubh_wait_on_buffer (tind_bh); |
| 375 | } |
| 376 | ubh_brelse (tind_bh); |
| 377 | |
| 378 | UFSD(("EXIT\n")) |
| 379 | return retry; |
| 380 | } |
| 381 | |
| 382 | void ufs_truncate (struct inode * inode) |
| 383 | { |
| 384 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 385 | struct super_block * sb; |
| 386 | struct ufs_sb_private_info * uspi; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 387 | int retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | UFSD(("ENTER\n")) |
| 390 | sb = inode->i_sb; |
| 391 | uspi = UFS_SB(sb)->s_uspi; |
| 392 | |
| 393 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) |
| 394 | return; |
| 395 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 396 | return; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 397 | |
| 398 | block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block); |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | lock_kernel(); |
| 401 | while (1) { |
| 402 | retry = ufs_trunc_direct(inode); |
| 403 | retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK, |
| 404 | (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]); |
| 405 | retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb, |
| 406 | (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]); |
| 407 | retry |= ufs_trunc_tindirect (inode); |
| 408 | if (!retry) |
| 409 | break; |
| 410 | if (IS_SYNC(inode) && (inode->i_state & I_DIRTY)) |
| 411 | ufs_sync_inode (inode); |
| 412 | blk_run_address_space(inode->i_mapping); |
| 413 | yield(); |
| 414 | } |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; |
| 417 | ufsi->i_lastfrag = DIRECT_FRAGMENT; |
| 418 | unlock_kernel(); |
| 419 | mark_inode_dirty(inode); |
| 420 | UFSD(("EXIT\n")) |
| 421 | } |