1 /* 2 * linux/fs/affs/file.c 3 * 4 * (c) 1996 Hans-Joachim Widmaier - Rewritten 5 * 6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem. 7 * 8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem. 9 * 10 * (C) 1991 Linus Torvalds - minix filesystem 11 * 12 * affs regular file handling primitives 13 */ 14 15 #include "affs.h" 16 17 #if PAGE_SIZE < 4096 18 #error PAGE_SIZE must be at least 4096 19 #endif 20 21 static int affs_grow_extcache(struct inode *inode, u32 lc_idx); 22 static struct buffer_head *affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext); 23 static inline struct buffer_head *affs_get_extblock(struct inode *inode, u32 ext); 24 static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext); 25 static int affs_file_open(struct inode *inode, struct file *filp); 26 static int affs_file_release(struct inode *inode, struct file *filp); 27 28 const struct file_operations affs_file_operations = { 29 .llseek = generic_file_llseek, 30 .read = do_sync_read, 31 .aio_read = generic_file_aio_read, 32 .write = do_sync_write, 33 .aio_write = generic_file_aio_write, 34 .mmap = generic_file_mmap, 35 .open = affs_file_open, 36 .release = affs_file_release, 37 .fsync = file_fsync, 38 .splice_read = generic_file_splice_read, 39 }; 40 41 const struct inode_operations affs_file_inode_operations = { 42 .truncate = affs_truncate, 43 .setattr = affs_notify_change, 44 }; 45 46 static int 47 affs_file_open(struct inode *inode, struct file *filp) 48 { 49 if (atomic_read(&filp->f_count) != 1) 50 return 0; 51 pr_debug("AFFS: open(%lu,%d)\n", 52 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt)); 53 atomic_inc(&AFFS_I(inode)->i_opencnt); 54 return 0; 55 } 56 57 static int 58 affs_file_release(struct inode *inode, struct file *filp) 59 { 60 if (atomic_read(&filp->f_count) != 0) 61 return 0; 62 pr_debug("AFFS: release(%lu, %d)\n", 63 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt)); 64 65 if (atomic_dec_and_test(&AFFS_I(inode)->i_opencnt)) { 66 mutex_lock(&inode->i_mutex); 67 if (inode->i_size != AFFS_I(inode)->mmu_private) 68 affs_truncate(inode); 69 affs_free_prealloc(inode); 70 mutex_unlock(&inode->i_mutex); 71 } 72 73 return 0; 74 } 75 76 static int 77 affs_grow_extcache(struct inode *inode, u32 lc_idx) 78 { 79 struct super_block *sb = inode->i_sb; 80 struct buffer_head *bh; 81 u32 lc_max; 82 int i, j, key; 83 84 if (!AFFS_I(inode)->i_lc) { 85 char *ptr = (char *)get_zeroed_page(GFP_NOFS); 86 if (!ptr) 87 return -ENOMEM; 88 AFFS_I(inode)->i_lc = (u32 *)ptr; 89 AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2); 90 } 91 92 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift; 93 94 if (AFFS_I(inode)->i_extcnt > lc_max) { 95 u32 lc_shift, lc_mask, tmp, off; 96 97 /* need to recalculate linear cache, start from old size */ 98 lc_shift = AFFS_I(inode)->i_lc_shift; 99 tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift; 100 for (; tmp; tmp >>= 1) 101 lc_shift++; 102 lc_mask = (1 << lc_shift) - 1; 103 104 /* fix idx and old size to new shift */ 105 lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift); 106 AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift); 107 108 /* first shrink old cache to make more space */ 109 off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift); 110 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off) 111 AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j]; 112 113 AFFS_I(inode)->i_lc_shift = lc_shift; 114 AFFS_I(inode)->i_lc_mask = lc_mask; 115 } 116 117 /* fill cache to the needed index */ 118 i = AFFS_I(inode)->i_lc_size; 119 AFFS_I(inode)->i_lc_size = lc_idx + 1; 120 for (; i <= lc_idx; i++) { 121 if (!i) { 122 AFFS_I(inode)->i_lc[0] = inode->i_ino; 123 continue; 124 } 125 key = AFFS_I(inode)->i_lc[i - 1]; 126 j = AFFS_I(inode)->i_lc_mask + 1; 127 // unlock cache 128 for (; j > 0; j--) { 129 bh = affs_bread(sb, key); 130 if (!bh) 131 goto err; 132 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension); 133 affs_brelse(bh); 134 } 135 // lock cache 136 AFFS_I(inode)->i_lc[i] = key; 137 } 138 139 return 0; 140 141 err: 142 // lock cache 143 return -EIO; 144 } 145 146 static struct buffer_head * 147 affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext) 148 { 149 struct super_block *sb = inode->i_sb; 150 struct buffer_head *new_bh; 151 u32 blocknr, tmp; 152 153 blocknr = affs_alloc_block(inode, bh->b_blocknr); 154 if (!blocknr) 155 return ERR_PTR(-ENOSPC); 156 157 new_bh = affs_getzeroblk(sb, blocknr); 158 if (!new_bh) { 159 affs_free_block(sb, blocknr); 160 return ERR_PTR(-EIO); 161 } 162 163 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST); 164 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr); 165 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE); 166 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino); 167 affs_fix_checksum(sb, new_bh); 168 169 mark_buffer_dirty_inode(new_bh, inode); 170 171 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension); 172 if (tmp) 173 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp); 174 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr); 175 affs_adjust_checksum(bh, blocknr - tmp); 176 mark_buffer_dirty_inode(bh, inode); 177 178 AFFS_I(inode)->i_extcnt++; 179 mark_inode_dirty(inode); 180 181 return new_bh; 182 } 183 184 static inline struct buffer_head * 185 affs_get_extblock(struct inode *inode, u32 ext) 186 { 187 /* inline the simplest case: same extended block as last time */ 188 struct buffer_head *bh = AFFS_I(inode)->i_ext_bh; 189 if (ext == AFFS_I(inode)->i_ext_last) 190 get_bh(bh); 191 else 192 /* we have to do more (not inlined) */ 193 bh = affs_get_extblock_slow(inode, ext); 194 195 return bh; 196 } 197 198 static struct buffer_head * 199 affs_get_extblock_slow(struct inode *inode, u32 ext) 200 { 201 struct super_block *sb = inode->i_sb; 202 struct buffer_head *bh; 203 u32 ext_key; 204 u32 lc_idx, lc_off, ac_idx; 205 u32 tmp, idx; 206 207 if (ext == AFFS_I(inode)->i_ext_last + 1) { 208 /* read the next extended block from the current one */ 209 bh = AFFS_I(inode)->i_ext_bh; 210 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension); 211 if (ext < AFFS_I(inode)->i_extcnt) 212 goto read_ext; 213 if (ext > AFFS_I(inode)->i_extcnt) 214 BUG(); 215 bh = affs_alloc_extblock(inode, bh, ext); 216 if (IS_ERR(bh)) 217 return bh; 218 goto store_ext; 219 } 220 221 if (ext == 0) { 222 /* we seek back to the file header block */ 223 ext_key = inode->i_ino; 224 goto read_ext; 225 } 226 227 if (ext >= AFFS_I(inode)->i_extcnt) { 228 struct buffer_head *prev_bh; 229 230 /* allocate a new extended block */ 231 if (ext > AFFS_I(inode)->i_extcnt) 232 BUG(); 233 234 /* get previous extended block */ 235 prev_bh = affs_get_extblock(inode, ext - 1); 236 if (IS_ERR(prev_bh)) 237 return prev_bh; 238 bh = affs_alloc_extblock(inode, prev_bh, ext); 239 affs_brelse(prev_bh); 240 if (IS_ERR(bh)) 241 return bh; 242 goto store_ext; 243 } 244 245 again: 246 /* check if there is an extended cache and whether it's large enough */ 247 lc_idx = ext >> AFFS_I(inode)->i_lc_shift; 248 lc_off = ext & AFFS_I(inode)->i_lc_mask; 249 250 if (lc_idx >= AFFS_I(inode)->i_lc_size) { 251 int err; 252 253 err = affs_grow_extcache(inode, lc_idx); 254 if (err) 255 return ERR_PTR(err); 256 goto again; 257 } 258 259 /* every n'th key we find in the linear cache */ 260 if (!lc_off) { 261 ext_key = AFFS_I(inode)->i_lc[lc_idx]; 262 goto read_ext; 263 } 264 265 /* maybe it's still in the associative cache */ 266 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK; 267 if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) { 268 ext_key = AFFS_I(inode)->i_ac[ac_idx].key; 269 goto read_ext; 270 } 271 272 /* try to find one of the previous extended blocks */ 273 tmp = ext; 274 idx = ac_idx; 275 while (--tmp, --lc_off > 0) { 276 idx = (idx - 1) & AFFS_AC_MASK; 277 if (AFFS_I(inode)->i_ac[idx].ext == tmp) { 278 ext_key = AFFS_I(inode)->i_ac[idx].key; 279 goto find_ext; 280 } 281 } 282 283 /* fall back to the linear cache */ 284 ext_key = AFFS_I(inode)->i_lc[lc_idx]; 285 find_ext: 286 /* read all extended blocks until we find the one we need */ 287 //unlock cache 288 do { 289 bh = affs_bread(sb, ext_key); 290 if (!bh) 291 goto err_bread; 292 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension); 293 affs_brelse(bh); 294 tmp++; 295 } while (tmp < ext); 296 //lock cache 297 298 /* store it in the associative cache */ 299 // recalculate ac_idx? 300 AFFS_I(inode)->i_ac[ac_idx].ext = ext; 301 AFFS_I(inode)->i_ac[ac_idx].key = ext_key; 302 303 read_ext: 304 /* finally read the right extended block */ 305 //unlock cache 306 bh = affs_bread(sb, ext_key); 307 if (!bh) 308 goto err_bread; 309 //lock cache 310 311 store_ext: 312 /* release old cached extended block and store the new one */ 313 affs_brelse(AFFS_I(inode)->i_ext_bh); 314 AFFS_I(inode)->i_ext_last = ext; 315 AFFS_I(inode)->i_ext_bh = bh; 316 get_bh(bh); 317 318 return bh; 319 320 err_bread: 321 affs_brelse(bh); 322 return ERR_PTR(-EIO); 323 } 324 325 static int 326 affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create) 327 { 328 struct super_block *sb = inode->i_sb; 329 struct buffer_head *ext_bh; 330 u32 ext; 331 332 pr_debug("AFFS: get_block(%u, %lu)\n", (u32)inode->i_ino, (unsigned long)block); 333 334 BUG_ON(block > (sector_t)0x7fffffffUL); 335 336 if (block >= AFFS_I(inode)->i_blkcnt) { 337 if (block > AFFS_I(inode)->i_blkcnt || !create) 338 goto err_big; 339 } else 340 create = 0; 341 342 //lock cache 343 affs_lock_ext(inode); 344 345 ext = (u32)block / AFFS_SB(sb)->s_hashsize; 346 block -= ext * AFFS_SB(sb)->s_hashsize; 347 ext_bh = affs_get_extblock(inode, ext); 348 if (IS_ERR(ext_bh)) 349 goto err_ext; 350 map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block))); 351 352 if (create) { 353 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr); 354 if (!blocknr) 355 goto err_alloc; 356 set_buffer_new(bh_result); 357 AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize; 358 AFFS_I(inode)->i_blkcnt++; 359 360 /* store new block */ 361 if (bh_result->b_blocknr) 362 affs_warning(sb, "get_block", "block already set (%x)", bh_result->b_blocknr); 363 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr); 364 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1); 365 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1); 366 bh_result->b_blocknr = blocknr; 367 368 if (!block) { 369 /* insert first block into header block */ 370 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data); 371 if (tmp) 372 affs_warning(sb, "get_block", "first block already set (%d)", tmp); 373 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr); 374 affs_adjust_checksum(ext_bh, blocknr - tmp); 375 } 376 } 377 378 affs_brelse(ext_bh); 379 //unlock cache 380 affs_unlock_ext(inode); 381 return 0; 382 383 err_big: 384 affs_error(inode->i_sb,"get_block","strange block request %d", block); 385 return -EIO; 386 err_ext: 387 // unlock cache 388 affs_unlock_ext(inode); 389 return PTR_ERR(ext_bh); 390 err_alloc: 391 brelse(ext_bh); 392 clear_buffer_mapped(bh_result); 393 bh_result->b_bdev = NULL; 394 // unlock cache 395 affs_unlock_ext(inode); 396 return -ENOSPC; 397 } 398 399 static int affs_writepage(struct page *page, struct writeback_control *wbc) 400 { 401 return block_write_full_page(page, affs_get_block, wbc); 402 } 403 404 static int affs_readpage(struct file *file, struct page *page) 405 { 406 return block_read_full_page(page, affs_get_block); 407 } 408 409 static int affs_write_begin(struct file *file, struct address_space *mapping, 410 loff_t pos, unsigned len, unsigned flags, 411 struct page **pagep, void **fsdata) 412 { 413 *pagep = NULL; 414 return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 415 affs_get_block, 416 &AFFS_I(mapping->host)->mmu_private); 417 } 418 419 static sector_t _affs_bmap(struct address_space *mapping, sector_t block) 420 { 421 return generic_block_bmap(mapping,block,affs_get_block); 422 } 423 424 const struct address_space_operations affs_aops = { 425 .readpage = affs_readpage, 426 .writepage = affs_writepage, 427 .sync_page = block_sync_page, 428 .write_begin = affs_write_begin, 429 .write_end = generic_write_end, 430 .bmap = _affs_bmap 431 }; 432 433 static inline struct buffer_head * 434 affs_bread_ino(struct inode *inode, int block, int create) 435 { 436 struct buffer_head *bh, tmp_bh; 437 int err; 438 439 tmp_bh.b_state = 0; 440 err = affs_get_block(inode, block, &tmp_bh, create); 441 if (!err) { 442 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr); 443 if (bh) { 444 bh->b_state |= tmp_bh.b_state; 445 return bh; 446 } 447 err = -EIO; 448 } 449 return ERR_PTR(err); 450 } 451 452 static inline struct buffer_head * 453 affs_getzeroblk_ino(struct inode *inode, int block) 454 { 455 struct buffer_head *bh, tmp_bh; 456 int err; 457 458 tmp_bh.b_state = 0; 459 err = affs_get_block(inode, block, &tmp_bh, 1); 460 if (!err) { 461 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr); 462 if (bh) { 463 bh->b_state |= tmp_bh.b_state; 464 return bh; 465 } 466 err = -EIO; 467 } 468 return ERR_PTR(err); 469 } 470 471 static inline struct buffer_head * 472 affs_getemptyblk_ino(struct inode *inode, int block) 473 { 474 struct buffer_head *bh, tmp_bh; 475 int err; 476 477 tmp_bh.b_state = 0; 478 err = affs_get_block(inode, block, &tmp_bh, 1); 479 if (!err) { 480 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr); 481 if (bh) { 482 bh->b_state |= tmp_bh.b_state; 483 return bh; 484 } 485 err = -EIO; 486 } 487 return ERR_PTR(err); 488 } 489 490 static int 491 affs_do_readpage_ofs(struct file *file, struct page *page, unsigned from, unsigned to) 492 { 493 struct inode *inode = page->mapping->host; 494 struct super_block *sb = inode->i_sb; 495 struct buffer_head *bh; 496 char *data; 497 u32 bidx, boff, bsize; 498 u32 tmp; 499 500 pr_debug("AFFS: read_page(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to); 501 BUG_ON(from > to || to > PAGE_CACHE_SIZE); 502 kmap(page); 503 data = page_address(page); 504 bsize = AFFS_SB(sb)->s_data_blksize; 505 tmp = (page->index << PAGE_CACHE_SHIFT) + from; 506 bidx = tmp / bsize; 507 boff = tmp % bsize; 508 509 while (from < to) { 510 bh = affs_bread_ino(inode, bidx, 0); 511 if (IS_ERR(bh)) 512 return PTR_ERR(bh); 513 tmp = min(bsize - boff, to - from); 514 BUG_ON(from + tmp > to || tmp > bsize); 515 memcpy(data + from, AFFS_DATA(bh) + boff, tmp); 516 affs_brelse(bh); 517 bidx++; 518 from += tmp; 519 boff = 0; 520 } 521 flush_dcache_page(page); 522 kunmap(page); 523 return 0; 524 } 525 526 static int 527 affs_extent_file_ofs(struct inode *inode, u32 newsize) 528 { 529 struct super_block *sb = inode->i_sb; 530 struct buffer_head *bh, *prev_bh; 531 u32 bidx, boff; 532 u32 size, bsize; 533 u32 tmp; 534 535 pr_debug("AFFS: extent_file(%u, %d)\n", (u32)inode->i_ino, newsize); 536 bsize = AFFS_SB(sb)->s_data_blksize; 537 bh = NULL; 538 size = AFFS_I(inode)->mmu_private; 539 bidx = size / bsize; 540 boff = size % bsize; 541 if (boff) { 542 bh = affs_bread_ino(inode, bidx, 0); 543 if (IS_ERR(bh)) 544 return PTR_ERR(bh); 545 tmp = min(bsize - boff, newsize - size); 546 BUG_ON(boff + tmp > bsize || tmp > bsize); 547 memset(AFFS_DATA(bh) + boff, 0, tmp); 548 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp); 549 affs_fix_checksum(sb, bh); 550 mark_buffer_dirty_inode(bh, inode); 551 size += tmp; 552 bidx++; 553 } else if (bidx) { 554 bh = affs_bread_ino(inode, bidx - 1, 0); 555 if (IS_ERR(bh)) 556 return PTR_ERR(bh); 557 } 558 559 while (size < newsize) { 560 prev_bh = bh; 561 bh = affs_getzeroblk_ino(inode, bidx); 562 if (IS_ERR(bh)) 563 goto out; 564 tmp = min(bsize, newsize - size); 565 BUG_ON(tmp > bsize); 566 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA); 567 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino); 568 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx); 569 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp); 570 affs_fix_checksum(sb, bh); 571 bh->b_state &= ~(1UL << BH_New); 572 mark_buffer_dirty_inode(bh, inode); 573 if (prev_bh) { 574 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next); 575 if (tmp) 576 affs_warning(sb, "extent_file_ofs", "next block already set for %d (%d)", bidx, tmp); 577 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr); 578 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp); 579 mark_buffer_dirty_inode(prev_bh, inode); 580 affs_brelse(prev_bh); 581 } 582 size += bsize; 583 bidx++; 584 } 585 affs_brelse(bh); 586 inode->i_size = AFFS_I(inode)->mmu_private = newsize; 587 return 0; 588 589 out: 590 inode->i_size = AFFS_I(inode)->mmu_private = newsize; 591 return PTR_ERR(bh); 592 } 593 594 static int 595 affs_readpage_ofs(struct file *file, struct page *page) 596 { 597 struct inode *inode = page->mapping->host; 598 u32 to; 599 int err; 600 601 pr_debug("AFFS: read_page(%u, %ld)\n", (u32)inode->i_ino, page->index); 602 to = PAGE_CACHE_SIZE; 603 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) { 604 to = inode->i_size & ~PAGE_CACHE_MASK; 605 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to); 606 } 607 608 err = affs_do_readpage_ofs(file, page, 0, to); 609 if (!err) 610 SetPageUptodate(page); 611 unlock_page(page); 612 return err; 613 } 614 615 static int affs_write_begin_ofs(struct file *file, struct address_space *mapping, 616 loff_t pos, unsigned len, unsigned flags, 617 struct page **pagep, void **fsdata) 618 { 619 struct inode *inode = mapping->host; 620 struct page *page; 621 pgoff_t index; 622 int err = 0; 623 624 pr_debug("AFFS: write_begin(%u, %llu, %llu)\n", (u32)inode->i_ino, (unsigned long long)pos, (unsigned long long)pos + len); 625 if (pos > AFFS_I(inode)->mmu_private) { 626 /* XXX: this probably leaves a too-big i_size in case of 627 * failure. Should really be updating i_size at write_end time 628 */ 629 err = affs_extent_file_ofs(inode, pos); 630 if (err) 631 return err; 632 } 633 634 index = pos >> PAGE_CACHE_SHIFT; 635 page = __grab_cache_page(mapping, index); 636 if (!page) 637 return -ENOMEM; 638 *pagep = page; 639 640 if (PageUptodate(page)) 641 return 0; 642 643 /* XXX: inefficient but safe in the face of short writes */ 644 err = affs_do_readpage_ofs(file, page, 0, PAGE_CACHE_SIZE); 645 if (err) { 646 unlock_page(page); 647 page_cache_release(page); 648 } 649 return err; 650 } 651 652 static int affs_write_end_ofs(struct file *file, struct address_space *mapping, 653 loff_t pos, unsigned len, unsigned copied, 654 struct page *page, void *fsdata) 655 { 656 struct inode *inode = mapping->host; 657 struct super_block *sb = inode->i_sb; 658 struct buffer_head *bh, *prev_bh; 659 char *data; 660 u32 bidx, boff, bsize; 661 unsigned from, to; 662 u32 tmp; 663 int written; 664 665 from = pos & (PAGE_CACHE_SIZE - 1); 666 to = pos + len; 667 /* 668 * XXX: not sure if this can handle short copies (len < copied), but 669 * we don't have to, because the page should always be uptodate here, 670 * due to write_begin. 671 */ 672 673 pr_debug("AFFS: write_begin(%u, %llu, %llu)\n", (u32)inode->i_ino, (unsigned long long)pos, (unsigned long long)pos + len); 674 bsize = AFFS_SB(sb)->s_data_blksize; 675 data = page_address(page); 676 677 bh = NULL; 678 written = 0; 679 tmp = (page->index << PAGE_CACHE_SHIFT) + from; 680 bidx = tmp / bsize; 681 boff = tmp % bsize; 682 if (boff) { 683 bh = affs_bread_ino(inode, bidx, 0); 684 if (IS_ERR(bh)) 685 return PTR_ERR(bh); 686 tmp = min(bsize - boff, to - from); 687 BUG_ON(boff + tmp > bsize || tmp > bsize); 688 memcpy(AFFS_DATA(bh) + boff, data + from, tmp); 689 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp); 690 affs_fix_checksum(sb, bh); 691 mark_buffer_dirty_inode(bh, inode); 692 written += tmp; 693 from += tmp; 694 bidx++; 695 } else if (bidx) { 696 bh = affs_bread_ino(inode, bidx - 1, 0); 697 if (IS_ERR(bh)) 698 return PTR_ERR(bh); 699 } 700 while (from + bsize <= to) { 701 prev_bh = bh; 702 bh = affs_getemptyblk_ino(inode, bidx); 703 if (IS_ERR(bh)) 704 goto out; 705 memcpy(AFFS_DATA(bh), data + from, bsize); 706 if (buffer_new(bh)) { 707 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA); 708 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino); 709 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx); 710 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize); 711 AFFS_DATA_HEAD(bh)->next = 0; 712 bh->b_state &= ~(1UL << BH_New); 713 if (prev_bh) { 714 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next); 715 if (tmp) 716 affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp); 717 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr); 718 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp); 719 mark_buffer_dirty_inode(prev_bh, inode); 720 } 721 } 722 affs_brelse(prev_bh); 723 affs_fix_checksum(sb, bh); 724 mark_buffer_dirty_inode(bh, inode); 725 written += bsize; 726 from += bsize; 727 bidx++; 728 } 729 if (from < to) { 730 prev_bh = bh; 731 bh = affs_bread_ino(inode, bidx, 1); 732 if (IS_ERR(bh)) 733 goto out; 734 tmp = min(bsize, to - from); 735 BUG_ON(tmp > bsize); 736 memcpy(AFFS_DATA(bh), data + from, tmp); 737 if (buffer_new(bh)) { 738 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA); 739 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino); 740 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx); 741 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp); 742 AFFS_DATA_HEAD(bh)->next = 0; 743 bh->b_state &= ~(1UL << BH_New); 744 if (prev_bh) { 745 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next); 746 if (tmp) 747 affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp); 748 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr); 749 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp); 750 mark_buffer_dirty_inode(prev_bh, inode); 751 } 752 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp) 753 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp); 754 affs_brelse(prev_bh); 755 affs_fix_checksum(sb, bh); 756 mark_buffer_dirty_inode(bh, inode); 757 written += tmp; 758 from += tmp; 759 bidx++; 760 } 761 SetPageUptodate(page); 762 763 done: 764 affs_brelse(bh); 765 tmp = (page->index << PAGE_CACHE_SHIFT) + from; 766 if (tmp > inode->i_size) 767 inode->i_size = AFFS_I(inode)->mmu_private = tmp; 768 769 unlock_page(page); 770 page_cache_release(page); 771 772 return written; 773 774 out: 775 bh = prev_bh; 776 if (!written) 777 written = PTR_ERR(bh); 778 goto done; 779 } 780 781 const struct address_space_operations affs_aops_ofs = { 782 .readpage = affs_readpage_ofs, 783 //.writepage = affs_writepage_ofs, 784 //.sync_page = affs_sync_page_ofs, 785 .write_begin = affs_write_begin_ofs, 786 .write_end = affs_write_end_ofs 787 }; 788 789 /* Free any preallocated blocks. */ 790 791 void 792 affs_free_prealloc(struct inode *inode) 793 { 794 struct super_block *sb = inode->i_sb; 795 796 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode->i_ino); 797 798 while (AFFS_I(inode)->i_pa_cnt) { 799 AFFS_I(inode)->i_pa_cnt--; 800 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc); 801 } 802 } 803 804 /* Truncate (or enlarge) a file to the requested size. */ 805 806 void 807 affs_truncate(struct inode *inode) 808 { 809 struct super_block *sb = inode->i_sb; 810 u32 ext, ext_key; 811 u32 last_blk, blkcnt, blk; 812 u32 size; 813 struct buffer_head *ext_bh; 814 int i; 815 816 pr_debug("AFFS: truncate(inode=%d, oldsize=%u, newsize=%u)\n", 817 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size); 818 819 last_blk = 0; 820 ext = 0; 821 if (inode->i_size) { 822 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize; 823 ext = last_blk / AFFS_SB(sb)->s_hashsize; 824 } 825 826 if (inode->i_size > AFFS_I(inode)->mmu_private) { 827 struct address_space *mapping = inode->i_mapping; 828 struct page *page; 829 void *fsdata; 830 u32 size = inode->i_size; 831 int res; 832 833 res = mapping->a_ops->write_begin(NULL, mapping, size, 0, 0, &page, &fsdata); 834 if (!res) 835 res = mapping->a_ops->write_end(NULL, mapping, size, 0, 0, page, fsdata); 836 else 837 inode->i_size = AFFS_I(inode)->mmu_private; 838 mark_inode_dirty(inode); 839 return; 840 } else if (inode->i_size == AFFS_I(inode)->mmu_private) 841 return; 842 843 // lock cache 844 ext_bh = affs_get_extblock(inode, ext); 845 if (IS_ERR(ext_bh)) { 846 affs_warning(sb, "truncate", "unexpected read error for ext block %u (%d)", 847 ext, PTR_ERR(ext_bh)); 848 return; 849 } 850 if (AFFS_I(inode)->i_lc) { 851 /* clear linear cache */ 852 i = (ext + 1) >> AFFS_I(inode)->i_lc_shift; 853 if (AFFS_I(inode)->i_lc_size > i) { 854 AFFS_I(inode)->i_lc_size = i; 855 for (; i < AFFS_LC_SIZE; i++) 856 AFFS_I(inode)->i_lc[i] = 0; 857 } 858 /* clear associative cache */ 859 for (i = 0; i < AFFS_AC_SIZE; i++) 860 if (AFFS_I(inode)->i_ac[i].ext >= ext) 861 AFFS_I(inode)->i_ac[i].ext = 0; 862 } 863 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension); 864 865 blkcnt = AFFS_I(inode)->i_blkcnt; 866 i = 0; 867 blk = last_blk; 868 if (inode->i_size) { 869 i = last_blk % AFFS_SB(sb)->s_hashsize + 1; 870 blk++; 871 } else 872 AFFS_HEAD(ext_bh)->first_data = 0; 873 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i); 874 size = AFFS_SB(sb)->s_hashsize; 875 if (size > blkcnt - blk + i) 876 size = blkcnt - blk + i; 877 for (; i < size; i++, blk++) { 878 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i))); 879 AFFS_BLOCK(sb, ext_bh, i) = 0; 880 } 881 AFFS_TAIL(sb, ext_bh)->extension = 0; 882 affs_fix_checksum(sb, ext_bh); 883 mark_buffer_dirty_inode(ext_bh, inode); 884 affs_brelse(ext_bh); 885 886 if (inode->i_size) { 887 AFFS_I(inode)->i_blkcnt = last_blk + 1; 888 AFFS_I(inode)->i_extcnt = ext + 1; 889 if (AFFS_SB(sb)->s_flags & SF_OFS) { 890 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0); 891 u32 tmp; 892 if (IS_ERR(ext_bh)) { 893 affs_warning(sb, "truncate", "unexpected read error for last block %u (%d)", 894 ext, PTR_ERR(ext_bh)); 895 return; 896 } 897 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next); 898 AFFS_DATA_HEAD(bh)->next = 0; 899 affs_adjust_checksum(bh, -tmp); 900 affs_brelse(bh); 901 } 902 } else { 903 AFFS_I(inode)->i_blkcnt = 0; 904 AFFS_I(inode)->i_extcnt = 1; 905 } 906 AFFS_I(inode)->mmu_private = inode->i_size; 907 // unlock cache 908 909 while (ext_key) { 910 ext_bh = affs_bread(sb, ext_key); 911 size = AFFS_SB(sb)->s_hashsize; 912 if (size > blkcnt - blk) 913 size = blkcnt - blk; 914 for (i = 0; i < size; i++, blk++) 915 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i))); 916 affs_free_block(sb, ext_key); 917 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension); 918 affs_brelse(ext_bh); 919 } 920 affs_free_prealloc(inode); 921 } 922