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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Secure deletion currently doesn't work. It interacts very badly |
| 54 | * with buffers shared with memory mappings, and for that reason |
| 55 | * can't be done in the truncate() routines. It should instead be |
| 56 | * done separately in "release()" before calling the truncate routines |
| 57 | * that will release the actual file blocks. |
| 58 | * |
| 59 | * Linus |
| 60 | */ |
| 61 | |
| 62 | #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift) |
| 63 | #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift) |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | static int ufs_trunc_direct (struct inode * inode) |
| 67 | { |
| 68 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 69 | struct super_block * sb; |
| 70 | struct ufs_sb_private_info * uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | __fs32 * p; |
| 72 | unsigned frag1, frag2, frag3, frag4, block1, block2; |
| 73 | unsigned frag_to_free, free_count; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 74 | unsigned i, tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int retry; |
| 76 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 77 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | sb = inode->i_sb; |
| 80 | uspi = UFS_SB(sb)->s_uspi; |
| 81 | |
| 82 | frag_to_free = 0; |
| 83 | free_count = 0; |
| 84 | retry = 0; |
| 85 | |
| 86 | frag1 = DIRECT_FRAGMENT; |
| 87 | frag4 = min_t(u32, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag); |
| 88 | frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1); |
| 89 | frag3 = frag4 & ~uspi->s_fpbmask; |
| 90 | block1 = block2 = 0; |
| 91 | if (frag2 > frag3) { |
| 92 | frag2 = frag4; |
| 93 | frag3 = frag4 = 0; |
| 94 | } |
| 95 | else if (frag2 < frag3) { |
| 96 | block1 = ufs_fragstoblks (frag2); |
| 97 | block2 = ufs_fragstoblks (frag3); |
| 98 | } |
| 99 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 100 | UFSD("frag1 %u, frag2 %u, block1 %u, block2 %u, frag3 %u, frag4 %u\n", frag1, frag2, block1, block2, frag3, frag4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | if (frag1 >= frag2) |
| 103 | goto next1; |
| 104 | |
| 105 | /* |
| 106 | * Free first free fragments |
| 107 | */ |
| 108 | p = ufsi->i_u1.i_data + ufs_fragstoblks (frag1); |
| 109 | tmp = fs32_to_cpu(sb, *p); |
| 110 | if (!tmp ) |
| 111 | ufs_panic (sb, "ufs_trunc_direct", "internal error"); |
| 112 | frag1 = ufs_fragnum (frag1); |
| 113 | frag2 = ufs_fragnum (frag2); |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | ufs_free_fragments (inode, tmp + frag1, frag2 - frag1); |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 116 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | frag_to_free = tmp + frag1; |
| 118 | |
| 119 | next1: |
| 120 | /* |
| 121 | * Free whole blocks |
| 122 | */ |
| 123 | for (i = block1 ; i < block2; i++) { |
| 124 | p = ufsi->i_u1.i_data + i; |
| 125 | tmp = fs32_to_cpu(sb, *p); |
| 126 | if (!tmp) |
| 127 | continue; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | *p = 0; |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (free_count == 0) { |
| 132 | frag_to_free = tmp; |
| 133 | free_count = uspi->s_fpb; |
| 134 | } else if (free_count > 0 && frag_to_free == tmp - free_count) |
| 135 | free_count += uspi->s_fpb; |
| 136 | else { |
| 137 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 138 | frag_to_free = tmp; |
| 139 | free_count = uspi->s_fpb; |
| 140 | } |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 141 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (free_count > 0) |
| 145 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 146 | |
| 147 | if (frag3 >= frag4) |
| 148 | goto next3; |
| 149 | |
| 150 | /* |
| 151 | * Free last free fragments |
| 152 | */ |
| 153 | p = ufsi->i_u1.i_data + ufs_fragstoblks (frag3); |
| 154 | tmp = fs32_to_cpu(sb, *p); |
| 155 | if (!tmp ) |
| 156 | ufs_panic(sb, "ufs_truncate_direct", "internal error"); |
| 157 | frag4 = ufs_fragnum (frag4); |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | *p = 0; |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | ufs_free_fragments (inode, tmp, frag4); |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 162 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | next3: |
| 164 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 165 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return retry; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static int ufs_trunc_indirect (struct inode * inode, unsigned offset, __fs32 *p) |
| 171 | { |
| 172 | struct super_block * sb; |
| 173 | struct ufs_sb_private_info * uspi; |
| 174 | struct ufs_buffer_head * ind_ubh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | __fs32 * ind; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 176 | unsigned indirect_block, i, tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | unsigned frag_to_free, free_count; |
| 178 | int retry; |
| 179 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 180 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | sb = inode->i_sb; |
| 183 | uspi = UFS_SB(sb)->s_uspi; |
| 184 | |
| 185 | frag_to_free = 0; |
| 186 | free_count = 0; |
| 187 | retry = 0; |
| 188 | |
| 189 | tmp = fs32_to_cpu(sb, *p); |
| 190 | if (!tmp) |
| 191 | return 0; |
| 192 | ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize); |
| 193 | if (tmp != fs32_to_cpu(sb, *p)) { |
| 194 | ubh_brelse (ind_ubh); |
| 195 | return 1; |
| 196 | } |
| 197 | if (!ind_ubh) { |
| 198 | *p = 0; |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | indirect_block = (DIRECT_BLOCK > offset) ? (DIRECT_BLOCK - offset) : 0; |
| 203 | for (i = indirect_block; i < uspi->s_apb; i++) { |
| 204 | ind = ubh_get_addr32 (ind_ubh, i); |
| 205 | tmp = fs32_to_cpu(sb, *ind); |
| 206 | if (!tmp) |
| 207 | continue; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | *ind = 0; |
| 210 | ubh_mark_buffer_dirty(ind_ubh); |
| 211 | if (free_count == 0) { |
| 212 | frag_to_free = tmp; |
| 213 | free_count = uspi->s_fpb; |
| 214 | } else if (free_count > 0 && frag_to_free == tmp - free_count) |
| 215 | free_count += uspi->s_fpb; |
| 216 | else { |
| 217 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 218 | frag_to_free = tmp; |
| 219 | free_count = uspi->s_fpb; |
| 220 | } |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (free_count > 0) { |
| 226 | ufs_free_blocks (inode, frag_to_free, free_count); |
| 227 | } |
| 228 | for (i = 0; i < uspi->s_apb; i++) |
| 229 | if (*ubh_get_addr32(ind_ubh,i)) |
| 230 | break; |
| 231 | if (i >= uspi->s_apb) { |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 232 | tmp = fs32_to_cpu(sb, *p); |
| 233 | *p = 0; |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 234 | |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 235 | ufs_free_blocks (inode, tmp, uspi->s_fpb); |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 236 | mark_inode_dirty(inode); |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 237 | ubh_bforget(ind_ubh); |
| 238 | ind_ubh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | if (IS_SYNC(inode) && ind_ubh && ubh_buffer_dirty(ind_ubh)) { |
Evgeniy Dushistov | 098d5af | 2006-06-25 05:47:31 -0700 | [diff] [blame] | 241 | ubh_ll_rw_block(SWRITE, ind_ubh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ubh_wait_on_buffer (ind_ubh); |
| 243 | } |
| 244 | ubh_brelse (ind_ubh); |
| 245 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 246 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | return retry; |
| 249 | } |
| 250 | |
| 251 | static int ufs_trunc_dindirect (struct inode *inode, unsigned offset, __fs32 *p) |
| 252 | { |
| 253 | struct super_block * sb; |
| 254 | struct ufs_sb_private_info * uspi; |
| 255 | struct ufs_buffer_head * dind_bh; |
| 256 | unsigned i, tmp, dindirect_block; |
| 257 | __fs32 * dind; |
| 258 | int retry = 0; |
| 259 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 260 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | sb = inode->i_sb; |
| 263 | uspi = UFS_SB(sb)->s_uspi; |
| 264 | |
| 265 | dindirect_block = (DIRECT_BLOCK > offset) |
| 266 | ? ((DIRECT_BLOCK - offset) >> uspi->s_apbshift) : 0; |
| 267 | retry = 0; |
| 268 | |
| 269 | tmp = fs32_to_cpu(sb, *p); |
| 270 | if (!tmp) |
| 271 | return 0; |
| 272 | dind_bh = ubh_bread(sb, tmp, uspi->s_bsize); |
| 273 | if (tmp != fs32_to_cpu(sb, *p)) { |
| 274 | ubh_brelse (dind_bh); |
| 275 | return 1; |
| 276 | } |
| 277 | if (!dind_bh) { |
| 278 | *p = 0; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | for (i = dindirect_block ; i < uspi->s_apb ; i++) { |
| 283 | dind = ubh_get_addr32 (dind_bh, i); |
| 284 | tmp = fs32_to_cpu(sb, *dind); |
| 285 | if (!tmp) |
| 286 | continue; |
| 287 | retry |= ufs_trunc_indirect (inode, offset + (i << uspi->s_apbshift), dind); |
| 288 | ubh_mark_buffer_dirty(dind_bh); |
| 289 | } |
| 290 | |
| 291 | for (i = 0; i < uspi->s_apb; i++) |
| 292 | if (*ubh_get_addr32 (dind_bh, i)) |
| 293 | break; |
| 294 | if (i >= uspi->s_apb) { |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 295 | tmp = fs32_to_cpu(sb, *p); |
| 296 | *p = 0; |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 297 | |
| 298 | ufs_free_blocks(inode, tmp, uspi->s_fpb); |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 299 | mark_inode_dirty(inode); |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 300 | ubh_bforget(dind_bh); |
| 301 | dind_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | if (IS_SYNC(inode) && dind_bh && ubh_buffer_dirty(dind_bh)) { |
Evgeniy Dushistov | 098d5af | 2006-06-25 05:47:31 -0700 | [diff] [blame] | 304 | ubh_ll_rw_block(SWRITE, dind_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | ubh_wait_on_buffer (dind_bh); |
| 306 | } |
| 307 | ubh_brelse (dind_bh); |
| 308 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 309 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | return retry; |
| 312 | } |
| 313 | |
| 314 | static int ufs_trunc_tindirect (struct inode * inode) |
| 315 | { |
| 316 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 317 | struct super_block * sb; |
| 318 | struct ufs_sb_private_info * uspi; |
| 319 | struct ufs_buffer_head * tind_bh; |
| 320 | unsigned tindirect_block, tmp, i; |
| 321 | __fs32 * tind, * p; |
| 322 | int retry; |
| 323 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 324 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | sb = inode->i_sb; |
| 327 | uspi = UFS_SB(sb)->s_uspi; |
| 328 | retry = 0; |
| 329 | |
| 330 | tindirect_block = (DIRECT_BLOCK > (UFS_NDADDR + uspi->s_apb + uspi->s_2apb)) |
| 331 | ? ((DIRECT_BLOCK - UFS_NDADDR - uspi->s_apb - uspi->s_2apb) >> uspi->s_2apbshift) : 0; |
| 332 | p = ufsi->i_u1.i_data + UFS_TIND_BLOCK; |
| 333 | if (!(tmp = fs32_to_cpu(sb, *p))) |
| 334 | return 0; |
| 335 | tind_bh = ubh_bread (sb, tmp, uspi->s_bsize); |
| 336 | if (tmp != fs32_to_cpu(sb, *p)) { |
| 337 | ubh_brelse (tind_bh); |
| 338 | return 1; |
| 339 | } |
| 340 | if (!tind_bh) { |
| 341 | *p = 0; |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | for (i = tindirect_block ; i < uspi->s_apb ; i++) { |
| 346 | tind = ubh_get_addr32 (tind_bh, i); |
| 347 | retry |= ufs_trunc_dindirect(inode, UFS_NDADDR + |
| 348 | uspi->s_apb + ((i + 1) << uspi->s_2apbshift), tind); |
| 349 | ubh_mark_buffer_dirty(tind_bh); |
| 350 | } |
| 351 | for (i = 0; i < uspi->s_apb; i++) |
| 352 | if (*ubh_get_addr32 (tind_bh, i)) |
| 353 | break; |
| 354 | if (i >= uspi->s_apb) { |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 355 | tmp = fs32_to_cpu(sb, *p); |
| 356 | *p = 0; |
Evgeniy Dushistov | 50aa4eb | 2006-06-25 05:47:26 -0700 | [diff] [blame] | 357 | |
| 358 | ufs_free_blocks(inode, tmp, uspi->s_fpb); |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 359 | mark_inode_dirty(inode); |
Evgeniy Dushistov | 2061df0 | 2006-06-25 05:47:18 -0700 | [diff] [blame] | 360 | ubh_bforget(tind_bh); |
| 361 | tind_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | if (IS_SYNC(inode) && tind_bh && ubh_buffer_dirty(tind_bh)) { |
Evgeniy Dushistov | 098d5af | 2006-06-25 05:47:31 -0700 | [diff] [blame] | 364 | ubh_ll_rw_block(SWRITE, tind_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | ubh_wait_on_buffer (tind_bh); |
| 366 | } |
| 367 | ubh_brelse (tind_bh); |
| 368 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 369 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return retry; |
| 371 | } |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 372 | |
| 373 | static int ufs_alloc_lastblock(struct inode *inode) |
| 374 | { |
| 375 | int err = 0; |
| 376 | struct address_space *mapping = inode->i_mapping; |
| 377 | struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 378 | unsigned lastfrag, i, end; |
| 379 | struct page *lastpage; |
| 380 | struct buffer_head *bh; |
| 381 | |
| 382 | lastfrag = (i_size_read(inode) + uspi->s_fsize - 1) >> uspi->s_fshift; |
| 383 | |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 384 | if (!lastfrag) |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 385 | goto out; |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 386 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 387 | lastfrag--; |
| 388 | |
| 389 | lastpage = ufs_get_locked_page(mapping, lastfrag >> |
| 390 | (PAGE_CACHE_SHIFT - inode->i_blkbits)); |
| 391 | if (IS_ERR(lastpage)) { |
| 392 | err = -EIO; |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1); |
| 397 | bh = page_buffers(lastpage); |
| 398 | for (i = 0; i < end; ++i) |
| 399 | bh = bh->b_this_page; |
| 400 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 401 | |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 402 | err = ufs_getfrag_block(inode, lastfrag, bh, 1); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 403 | |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 404 | if (unlikely(err)) |
| 405 | goto out_unlock; |
| 406 | |
| 407 | if (buffer_new(bh)) { |
| 408 | clear_buffer_new(bh); |
| 409 | unmap_underlying_metadata(bh->b_bdev, |
| 410 | bh->b_blocknr); |
| 411 | /* |
| 412 | * we do not zeroize fragment, because of |
| 413 | * if it maped to hole, it already contains zeroes |
| 414 | */ |
| 415 | set_buffer_uptodate(bh); |
| 416 | mark_buffer_dirty(bh); |
| 417 | set_page_dirty(lastpage); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 418 | } |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 419 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 420 | out_unlock: |
| 421 | ufs_put_locked_page(lastpage); |
| 422 | out: |
| 423 | return err; |
| 424 | } |
| 425 | |
| 426 | int ufs_truncate(struct inode *inode, loff_t old_i_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
| 428 | struct ufs_inode_info *ufsi = UFS_I(inode); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 429 | struct super_block *sb = inode->i_sb; |
| 430 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
| 431 | int retry, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 433 | UFSD("ENTER\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 435 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 436 | S_ISLNK(inode->i_mode))) |
| 437 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 439 | return -EPERM; |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 440 | |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 441 | err = ufs_alloc_lastblock(inode); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 442 | |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 443 | if (err) { |
| 444 | i_size_write(inode, old_i_size); |
| 445 | goto out; |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | block_truncate_page(inode->i_mapping, inode->i_size, ufs_getfrag_block); |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | lock_kernel(); |
| 451 | while (1) { |
| 452 | retry = ufs_trunc_direct(inode); |
| 453 | retry |= ufs_trunc_indirect (inode, UFS_IND_BLOCK, |
| 454 | (__fs32 *) &ufsi->i_u1.i_data[UFS_IND_BLOCK]); |
| 455 | retry |= ufs_trunc_dindirect (inode, UFS_IND_BLOCK + uspi->s_apb, |
| 456 | (__fs32 *) &ufsi->i_u1.i_data[UFS_DIND_BLOCK]); |
| 457 | retry |= ufs_trunc_tindirect (inode); |
| 458 | if (!retry) |
| 459 | break; |
| 460 | if (IS_SYNC(inode) && (inode->i_state & I_DIRTY)) |
| 461 | ufs_sync_inode (inode); |
| 462 | blk_run_address_space(inode->i_mapping); |
| 463 | yield(); |
| 464 | } |
Evgeniy Dushistov | 09114eb | 2006-02-03 03:04:06 -0800 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; |
Evgeniy Dushistov | ecdc639 | 2006-08-27 01:23:46 -0700 | [diff] [blame] | 467 | ufsi->i_lastfrag = DIRECT_FRAGMENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | unlock_kernel(); |
| 469 | mark_inode_dirty(inode); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 470 | out: |
| 471 | UFSD("EXIT: err %d\n", err); |
| 472 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 474 | |
| 475 | |
| 476 | /* |
| 477 | * We don't define our `inode->i_op->truncate', and call it here, |
| 478 | * because of: |
| 479 | * - there is no way to know old size |
| 480 | * - there is no way inform user about error, if it happens in `truncate' |
| 481 | */ |
| 482 | static int ufs_setattr(struct dentry *dentry, struct iattr *attr) |
| 483 | { |
| 484 | struct inode *inode = dentry->d_inode; |
| 485 | unsigned int ia_valid = attr->ia_valid; |
| 486 | int error; |
| 487 | |
| 488 | error = inode_change_ok(inode, attr); |
| 489 | if (error) |
| 490 | return error; |
| 491 | |
| 492 | if (ia_valid & ATTR_SIZE && |
| 493 | attr->ia_size != i_size_read(inode)) { |
| 494 | loff_t old_i_size = inode->i_size; |
| 495 | error = vmtruncate(inode, attr->ia_size); |
| 496 | if (error) |
| 497 | return error; |
| 498 | error = ufs_truncate(inode, old_i_size); |
| 499 | if (error) |
| 500 | return error; |
| 501 | } |
| 502 | return inode_setattr(inode, attr); |
| 503 | } |
| 504 | |
| 505 | struct inode_operations ufs_file_inode_operations = { |
| 506 | .setattr = ufs_setattr, |
| 507 | }; |