4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
10 * (C) 1991 Linus Torvalds - minix filesystem
12 * affs regular file handling primitives
18 #error PAGE_SIZE must be at least 4096
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 ssize_t affs_file_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos);
26 static int affs_file_open(struct inode *inode, struct file *filp);
27 static int affs_file_release(struct inode *inode, struct file *filp);
29 struct file_operations affs_file_operations = {
30 .llseek = generic_file_llseek,
31 .read = generic_file_read,
32 .write = affs_file_write,
33 .mmap = generic_file_mmap,
34 .open = affs_file_open,
35 .release = affs_file_release,
37 .sendfile = generic_file_sendfile,
40 struct inode_operations affs_file_inode_operations = {
41 .truncate = affs_truncate,
42 .setattr = affs_notify_change,
46 affs_file_open(struct inode *inode, struct file *filp)
48 if (atomic_read(&filp->f_count) != 1)
50 pr_debug("AFFS: open(%d)\n", AFFS_I(inode)->i_opencnt);
51 AFFS_I(inode)->i_opencnt++;
56 affs_file_release(struct inode *inode, struct file *filp)
58 if (atomic_read(&filp->f_count) != 0)
60 pr_debug("AFFS: release(%d)\n", AFFS_I(inode)->i_opencnt);
61 AFFS_I(inode)->i_opencnt--;
62 if (!AFFS_I(inode)->i_opencnt)
63 affs_free_prealloc(inode);
69 affs_grow_extcache(struct inode *inode, u32 lc_idx)
71 struct super_block *sb = inode->i_sb;
72 struct buffer_head *bh;
76 if (!AFFS_I(inode)->i_lc) {
77 char *ptr = (char *)get_zeroed_page(GFP_NOFS);
80 AFFS_I(inode)->i_lc = (u32 *)ptr;
81 AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
84 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
86 if (AFFS_I(inode)->i_extcnt > lc_max) {
87 u32 lc_shift, lc_mask, tmp, off;
89 /* need to recalculate linear cache, start from old size */
90 lc_shift = AFFS_I(inode)->i_lc_shift;
91 tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
92 for (; tmp; tmp >>= 1)
94 lc_mask = (1 << lc_shift) - 1;
96 /* fix idx and old size to new shift */
97 lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
98 AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
100 /* first shrink old cache to make more space */
101 off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
102 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
103 AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
105 AFFS_I(inode)->i_lc_shift = lc_shift;
106 AFFS_I(inode)->i_lc_mask = lc_mask;
109 /* fill cache to the needed index */
110 i = AFFS_I(inode)->i_lc_size;
111 AFFS_I(inode)->i_lc_size = lc_idx + 1;
112 for (; i <= lc_idx; i++) {
114 AFFS_I(inode)->i_lc[0] = inode->i_ino;
117 key = AFFS_I(inode)->i_lc[i - 1];
118 j = AFFS_I(inode)->i_lc_mask + 1;
121 bh = affs_bread(sb, key);
124 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
128 AFFS_I(inode)->i_lc[i] = key;
138 static struct buffer_head *
139 affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
141 struct super_block *sb = inode->i_sb;
142 struct buffer_head *new_bh;
145 blocknr = affs_alloc_block(inode, bh->b_blocknr);
147 return ERR_PTR(-ENOSPC);
149 new_bh = affs_getzeroblk(sb, blocknr);
151 affs_free_block(sb, blocknr);
152 return ERR_PTR(-EIO);
155 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
156 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
157 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
158 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
159 affs_fix_checksum(sb, new_bh);
161 mark_buffer_dirty_inode(new_bh, inode);
163 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
165 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
166 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
167 affs_adjust_checksum(bh, blocknr - tmp);
168 mark_buffer_dirty_inode(bh, inode);
170 AFFS_I(inode)->i_extcnt++;
171 mark_inode_dirty(inode);
176 static inline struct buffer_head *
177 affs_get_extblock(struct inode *inode, u32 ext)
179 /* inline the simplest case: same extended block as last time */
180 struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
181 if (ext == AFFS_I(inode)->i_ext_last)
182 atomic_inc(&bh->b_count);
184 /* we have to do more (not inlined) */
185 bh = affs_get_extblock_slow(inode, ext);
190 static struct buffer_head *
191 affs_get_extblock_slow(struct inode *inode, u32 ext)
193 struct super_block *sb = inode->i_sb;
194 struct buffer_head *bh;
196 u32 lc_idx, lc_off, ac_idx;
199 if (ext == AFFS_I(inode)->i_ext_last + 1) {
200 /* read the next extended block from the current one */
201 bh = AFFS_I(inode)->i_ext_bh;
202 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
203 if (ext < AFFS_I(inode)->i_extcnt)
205 if (ext > AFFS_I(inode)->i_extcnt)
207 bh = affs_alloc_extblock(inode, bh, ext);
214 /* we seek back to the file header block */
215 ext_key = inode->i_ino;
219 if (ext >= AFFS_I(inode)->i_extcnt) {
220 struct buffer_head *prev_bh;
222 /* allocate a new extended block */
223 if (ext > AFFS_I(inode)->i_extcnt)
226 /* get previous extended block */
227 prev_bh = affs_get_extblock(inode, ext - 1);
230 bh = affs_alloc_extblock(inode, prev_bh, ext);
231 affs_brelse(prev_bh);
238 /* check if there is an extended cache and whether it's large enough */
239 lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
240 lc_off = ext & AFFS_I(inode)->i_lc_mask;
242 if (lc_idx >= AFFS_I(inode)->i_lc_size) {
245 err = affs_grow_extcache(inode, lc_idx);
251 /* every n'th key we find in the linear cache */
253 ext_key = AFFS_I(inode)->i_lc[lc_idx];
257 /* maybe it's still in the associative cache */
258 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
259 if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
260 ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
264 /* try to find one of the previous extended blocks */
267 while (--tmp, --lc_off > 0) {
268 idx = (idx - 1) & AFFS_AC_MASK;
269 if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
270 ext_key = AFFS_I(inode)->i_ac[idx].key;
275 /* fall back to the linear cache */
276 ext_key = AFFS_I(inode)->i_lc[lc_idx];
278 /* read all extended blocks until we find the one we need */
281 bh = affs_bread(sb, ext_key);
284 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
290 /* store it in the associative cache */
291 // recalculate ac_idx?
292 AFFS_I(inode)->i_ac[ac_idx].ext = ext;
293 AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
296 /* finally read the right extended block */
298 bh = affs_bread(sb, ext_key);
304 /* release old cached extended block and store the new one */
305 affs_brelse(AFFS_I(inode)->i_ext_bh);
306 AFFS_I(inode)->i_ext_last = ext;
307 AFFS_I(inode)->i_ext_bh = bh;
308 atomic_inc(&bh->b_count);
314 return ERR_PTR(-EIO);
318 affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
320 struct super_block *sb = inode->i_sb;
321 struct buffer_head *ext_bh;
324 pr_debug("AFFS: get_block(%u, %lu)\n", (u32)inode->i_ino, (unsigned long)block);
327 if (block > (sector_t)0x7fffffffUL)
330 if (block >= AFFS_I(inode)->i_blkcnt) {
331 if (block > AFFS_I(inode)->i_blkcnt || !create)
337 affs_lock_ext(inode);
339 ext = (u32)block / AFFS_SB(sb)->s_hashsize;
340 block -= ext * AFFS_SB(sb)->s_hashsize;
341 ext_bh = affs_get_extblock(inode, ext);
344 map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
347 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
350 set_buffer_new(bh_result);
351 AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
352 AFFS_I(inode)->i_blkcnt++;
354 /* store new block */
355 if (bh_result->b_blocknr)
356 affs_warning(sb, "get_block", "block already set (%x)", bh_result->b_blocknr);
357 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
358 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
359 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
360 bh_result->b_blocknr = blocknr;
363 /* insert first block into header block */
364 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
366 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
367 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
368 affs_adjust_checksum(ext_bh, blocknr - tmp);
374 affs_unlock_ext(inode);
378 affs_error(inode->i_sb,"get_block","strange block request %d", block);
382 affs_unlock_ext(inode);
383 return PTR_ERR(ext_bh);
386 clear_buffer_mapped(bh_result);
387 bh_result->b_bdev = NULL;
389 affs_unlock_ext(inode);
393 static int affs_writepage(struct page *page, struct writeback_control *wbc)
395 return block_write_full_page(page, affs_get_block, wbc);
397 static int affs_readpage(struct file *file, struct page *page)
399 return block_read_full_page(page, affs_get_block);
401 static int affs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
403 return cont_prepare_write(page, from, to, affs_get_block,
404 &AFFS_I(page->mapping->host)->mmu_private);
406 static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
408 return generic_block_bmap(mapping,block,affs_get_block);
410 struct address_space_operations affs_aops = {
411 .readpage = affs_readpage,
412 .writepage = affs_writepage,
413 .sync_page = block_sync_page,
414 .prepare_write = affs_prepare_write,
415 .commit_write = generic_commit_write,
419 static inline struct buffer_head *
420 affs_bread_ino(struct inode *inode, int block, int create)
422 struct buffer_head *bh, tmp_bh;
426 err = affs_get_block(inode, block, &tmp_bh, create);
428 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
430 bh->b_state |= tmp_bh.b_state;
438 static inline struct buffer_head *
439 affs_getzeroblk_ino(struct inode *inode, int block)
441 struct buffer_head *bh, tmp_bh;
445 err = affs_get_block(inode, block, &tmp_bh, 1);
447 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
449 bh->b_state |= tmp_bh.b_state;
457 static inline struct buffer_head *
458 affs_getemptyblk_ino(struct inode *inode, int block)
460 struct buffer_head *bh, tmp_bh;
464 err = affs_get_block(inode, block, &tmp_bh, 1);
466 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
468 bh->b_state |= tmp_bh.b_state;
477 affs_file_write(struct file *file, const char __user *buf,
478 size_t count, loff_t *ppos)
482 retval = generic_file_write (file, buf, count, ppos);
484 struct inode *inode = file->f_dentry->d_inode;
485 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
486 mark_inode_dirty(inode);
492 affs_do_readpage_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
494 struct inode *inode = page->mapping->host;
495 struct super_block *sb = inode->i_sb;
496 struct buffer_head *bh;
498 u32 bidx, boff, bsize;
501 pr_debug("AFFS: read_page(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
502 if (from > to || to > PAGE_CACHE_SIZE)
505 data = page_address(page);
506 bsize = AFFS_SB(sb)->s_data_blksize;
507 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
512 bh = affs_bread_ino(inode, bidx, 0);
515 tmp = min(bsize - boff, to - from);
516 if (from + tmp > to || tmp > bsize)
518 memcpy(data + from, AFFS_DATA(bh) + boff, tmp);
524 flush_dcache_page(page);
530 affs_extent_file_ofs(struct inode *inode, u32 newsize)
532 struct super_block *sb = inode->i_sb;
533 struct buffer_head *bh, *prev_bh;
538 pr_debug("AFFS: extent_file(%u, %d)\n", (u32)inode->i_ino, newsize);
539 bsize = AFFS_SB(sb)->s_data_blksize;
541 size = AFFS_I(inode)->mmu_private;
545 bh = affs_bread_ino(inode, bidx, 0);
548 tmp = min(bsize - boff, newsize - size);
549 if (boff + tmp > bsize || tmp > bsize)
551 memset(AFFS_DATA(bh) + boff, 0, tmp);
552 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh)->size) + tmp);
553 affs_fix_checksum(sb, bh);
554 mark_buffer_dirty_inode(bh, inode);
558 bh = affs_bread_ino(inode, bidx - 1, 0);
563 while (size < newsize) {
565 bh = affs_getzeroblk_ino(inode, bidx);
568 tmp = min(bsize, newsize - size);
571 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
572 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
573 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
574 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
575 affs_fix_checksum(sb, bh);
576 bh->b_state &= ~(1UL << BH_New);
577 mark_buffer_dirty_inode(bh, inode);
579 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
581 affs_warning(sb, "extent_file_ofs", "next block already set for %d (%d)", bidx, tmp);
582 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
583 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
584 mark_buffer_dirty_inode(prev_bh, inode);
585 affs_brelse(prev_bh);
591 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
595 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
600 affs_readpage_ofs(struct file *file, struct page *page)
602 struct inode *inode = page->mapping->host;
606 pr_debug("AFFS: read_page(%u, %ld)\n", (u32)inode->i_ino, page->index);
607 to = PAGE_CACHE_SIZE;
608 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
609 to = inode->i_size & ~PAGE_CACHE_MASK;
610 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
613 err = affs_do_readpage_ofs(file, page, 0, to);
615 SetPageUptodate(page);
620 static int affs_prepare_write_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
622 struct inode *inode = page->mapping->host;
627 pr_debug("AFFS: prepare_write(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
628 offset = page->index << PAGE_CACHE_SHIFT;
629 if (offset + from > AFFS_I(inode)->mmu_private) {
630 err = affs_extent_file_ofs(inode, offset + from);
634 size = inode->i_size;
636 if (PageUptodate(page))
640 err = affs_do_readpage_ofs(file, page, 0, from);
644 if (to < PAGE_CACHE_SIZE) {
645 char *kaddr = kmap_atomic(page, KM_USER0);
647 memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
648 flush_dcache_page(page);
649 kunmap_atomic(kaddr, KM_USER0);
650 if (size > offset + to) {
651 if (size < offset + PAGE_CACHE_SIZE)
652 tmp = size & ~PAGE_CACHE_MASK;
654 tmp = PAGE_CACHE_SIZE;
655 err = affs_do_readpage_ofs(file, page, to, tmp);
661 static int affs_commit_write_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
663 struct inode *inode = page->mapping->host;
664 struct super_block *sb = inode->i_sb;
665 struct buffer_head *bh, *prev_bh;
667 u32 bidx, boff, bsize;
671 pr_debug("AFFS: commit_write(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
672 bsize = AFFS_SB(sb)->s_data_blksize;
673 data = page_address(page);
677 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
681 bh = affs_bread_ino(inode, bidx, 0);
684 tmp = min(bsize - boff, to - from);
685 if (boff + tmp > bsize || tmp > bsize)
687 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
688 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh)->size) + tmp);
689 affs_fix_checksum(sb, bh);
690 mark_buffer_dirty_inode(bh, inode);
695 bh = affs_bread_ino(inode, bidx - 1, 0);
699 while (from + bsize <= to) {
701 bh = affs_getemptyblk_ino(inode, bidx);
704 memcpy(AFFS_DATA(bh), data + from, bsize);
705 if (buffer_new(bh)) {
706 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
707 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
708 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
709 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
710 AFFS_DATA_HEAD(bh)->next = 0;
711 bh->b_state &= ~(1UL << BH_New);
713 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
715 affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp);
716 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
717 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
718 mark_buffer_dirty_inode(prev_bh, inode);
721 affs_brelse(prev_bh);
722 affs_fix_checksum(sb, bh);
723 mark_buffer_dirty_inode(bh, inode);
730 bh = affs_bread_ino(inode, bidx, 1);
733 tmp = min(bsize, to - from);
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);
745 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
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);
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);
761 SetPageUptodate(page);
765 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
766 if (tmp > inode->i_size)
767 inode->i_size = AFFS_I(inode)->mmu_private = tmp;
774 written = PTR_ERR(bh);
778 struct address_space_operations affs_aops_ofs = {
779 .readpage = affs_readpage_ofs,
780 //.writepage = affs_writepage_ofs,
781 //.sync_page = affs_sync_page_ofs,
782 .prepare_write = affs_prepare_write_ofs,
783 .commit_write = affs_commit_write_ofs
786 /* Free any preallocated blocks. */
789 affs_free_prealloc(struct inode *inode)
791 struct super_block *sb = inode->i_sb;
793 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode->i_ino);
795 while (AFFS_I(inode)->i_pa_cnt) {
796 AFFS_I(inode)->i_pa_cnt--;
797 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
801 /* Truncate (or enlarge) a file to the requested size. */
804 affs_truncate(struct inode *inode)
806 struct super_block *sb = inode->i_sb;
808 u32 last_blk, blkcnt, blk;
810 struct buffer_head *ext_bh;
813 pr_debug("AFFS: truncate(inode=%d, oldsize=%u, newsize=%u)\n",
814 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
819 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
820 ext = last_blk / AFFS_SB(sb)->s_hashsize;
823 if (inode->i_size > AFFS_I(inode)->mmu_private) {
824 struct address_space *mapping = inode->i_mapping;
826 u32 size = inode->i_size - 1;
829 page = grab_cache_page(mapping, size >> PAGE_CACHE_SHIFT);
832 size = (size & (PAGE_CACHE_SIZE - 1)) + 1;
833 res = mapping->a_ops->prepare_write(NULL, page, size, size);
835 res = mapping->a_ops->commit_write(NULL, page, size, size);
837 page_cache_release(page);
838 mark_inode_dirty(inode);
840 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
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));
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;
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;
863 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
865 blkcnt = AFFS_I(inode)->i_blkcnt;
869 i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
872 AFFS_HEAD(ext_bh)->first_data = 0;
873 size = AFFS_SB(sb)->s_hashsize;
874 if (size > blkcnt - blk + i)
875 size = blkcnt - blk + i;
876 for (; i < size; i++, blk++) {
877 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
878 AFFS_BLOCK(sb, ext_bh, i) = 0;
880 AFFS_TAIL(sb, ext_bh)->extension = 0;
881 affs_fix_checksum(sb, ext_bh);
882 mark_buffer_dirty_inode(ext_bh, inode);
886 AFFS_I(inode)->i_blkcnt = last_blk + 1;
887 AFFS_I(inode)->i_extcnt = ext + 1;
888 if (AFFS_SB(sb)->s_flags & SF_OFS) {
889 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
891 if (IS_ERR(ext_bh)) {
892 affs_warning(sb, "truncate", "unexpected read error for last block %u (%d)",
893 ext, PTR_ERR(ext_bh));
896 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
897 AFFS_DATA_HEAD(bh)->next = 0;
898 affs_adjust_checksum(bh, -tmp);
902 AFFS_I(inode)->i_blkcnt = 0;
903 AFFS_I(inode)->i_extcnt = 1;
905 AFFS_I(inode)->mmu_private = inode->i_size;
909 ext_bh = affs_bread(sb, ext_key);
910 size = AFFS_SB(sb)->s_hashsize;
911 if (size > blkcnt - blk)
913 for (i = 0; i < size; i++, blk++)
914 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
915 affs_free_block(sb, ext_key);
916 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
919 affs_free_prealloc(inode);