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
15 #include <asm/div64.h>
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/sched.h>
19 #include <linux/affs_fs.h>
20 #include <linux/fcntl.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/stat.h>
25 #include <linux/locks.h>
26 #include <linux/smp_lock.h>
27 #include <linux/dirent.h>
29 #include <linux/amigaffs.h>
31 #include <linux/pagemap.h>
34 #error PAGE_SIZE must be at least 4096
37 static int affs_grow_extcache(struct inode *inode, u32 lc_idx);
38 static struct buffer_head *affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext);
39 static inline struct buffer_head *affs_get_extblock(struct inode *inode, u32 ext);
40 static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
41 static int affs_get_block(struct inode *inode, long block, struct buffer_head *bh_result, int create);
43 static ssize_t affs_file_write(struct file *filp, const char *buf, size_t count, loff_t *ppos);
44 static int affs_file_open(struct inode *inode, struct file *filp);
45 static int affs_file_release(struct inode *inode, struct file *filp);
47 struct file_operations affs_file_operations = {
48 llseek: generic_file_llseek,
49 read: generic_file_read,
50 write: affs_file_write,
51 mmap: generic_file_mmap,
53 release: affs_file_release,
57 struct inode_operations affs_file_inode_operations = {
58 truncate: affs_truncate,
59 setattr: affs_notify_change,
63 affs_file_open(struct inode *inode, struct file *filp)
65 if (atomic_read(&filp->f_count) != 1)
67 pr_debug("AFFS: open(%d)\n", AFFS_INODE->i_opencnt);
68 AFFS_INODE->i_opencnt++;
73 affs_file_release(struct inode *inode, struct file *filp)
75 if (atomic_read(&filp->f_count) != 0)
77 pr_debug("AFFS: release(%d)\n", AFFS_INODE->i_opencnt);
78 AFFS_INODE->i_opencnt--;
79 if (!AFFS_INODE->i_opencnt)
80 affs_free_prealloc(inode);
86 affs_grow_extcache(struct inode *inode, u32 lc_idx)
88 struct super_block *sb = inode->i_sb;
89 struct buffer_head *bh;
93 if (!AFFS_INODE->i_lc) {
94 char *ptr = (char *)get_zeroed_page(GFP_NOFS);
97 AFFS_INODE->i_lc = (u32 *)ptr;
98 AFFS_INODE->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
101 lc_max = AFFS_LC_SIZE << AFFS_INODE->i_lc_shift;
103 if (AFFS_INODE->i_extcnt > lc_max) {
104 u32 lc_shift, lc_mask, tmp, off;
106 /* need to recalculate linear cache, start from old size */
107 lc_shift = AFFS_INODE->i_lc_shift;
108 tmp = (AFFS_INODE->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
109 for (; tmp; tmp >>= 1)
111 lc_mask = (1 << lc_shift) - 1;
113 /* fix idx and old size to new shift */
114 lc_idx >>= (lc_shift - AFFS_INODE->i_lc_shift);
115 AFFS_INODE->i_lc_size >>= (lc_shift - AFFS_INODE->i_lc_shift);
117 /* first shrink old cache to make more space */
118 off = 1 << (lc_shift - AFFS_INODE->i_lc_shift);
119 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
120 AFFS_INODE->i_ac[i] = AFFS_INODE->i_ac[j];
122 AFFS_INODE->i_lc_shift = lc_shift;
123 AFFS_INODE->i_lc_mask = lc_mask;
126 /* fill cache to the needed index */
127 i = AFFS_INODE->i_lc_size;
128 AFFS_INODE->i_lc_size = lc_idx + 1;
129 for (; i <= lc_idx; i++) {
131 AFFS_INODE->i_lc[0] = inode->i_ino;
134 key = AFFS_INODE->i_lc[i - 1];
135 j = AFFS_INODE->i_lc_mask + 1;
138 bh = affs_bread(sb, key);
141 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
145 AFFS_INODE->i_lc[i] = key;
155 static struct buffer_head *
156 affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
158 struct super_block *sb = inode->i_sb;
159 struct buffer_head *new_bh;
162 blocknr = affs_alloc_block(inode, bh->b_blocknr);
164 return ERR_PTR(-ENOSPC);
166 new_bh = affs_getzeroblk(sb, blocknr);
168 affs_free_block(sb, blocknr);
169 return ERR_PTR(-EIO);
172 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
173 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
174 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
175 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
176 affs_fix_checksum(sb, new_bh);
178 mark_buffer_dirty_inode(new_bh, inode);
180 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
182 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
183 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
184 affs_adjust_checksum(bh, blocknr - tmp);
185 mark_buffer_dirty_inode(bh, inode);
187 AFFS_INODE->i_extcnt++;
188 mark_inode_dirty(inode);
193 static inline struct buffer_head *
194 affs_get_extblock(struct inode *inode, u32 ext)
196 /* inline the simplest case: same extended block as last time */
197 struct buffer_head *bh = AFFS_INODE->i_ext_bh;
198 if (ext == AFFS_INODE->i_ext_last)
199 atomic_inc(&bh->b_count);
201 /* we have to do more (not inlined) */
202 bh = affs_get_extblock_slow(inode, ext);
207 static struct buffer_head *
208 affs_get_extblock_slow(struct inode *inode, u32 ext)
210 struct super_block *sb = inode->i_sb;
211 struct buffer_head *bh;
213 u32 lc_idx, lc_off, ac_idx;
216 if (ext == AFFS_INODE->i_ext_last + 1) {
217 /* read the next extended block from the current one */
218 bh = AFFS_INODE->i_ext_bh;
219 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
220 if (ext < AFFS_INODE->i_extcnt)
222 if (ext > AFFS_INODE->i_extcnt)
224 bh = affs_alloc_extblock(inode, bh, ext);
231 /* we seek back to the file header block */
232 ext_key = inode->i_ino;
236 if (ext >= AFFS_INODE->i_extcnt) {
237 struct buffer_head *prev_bh;
239 /* allocate a new extended block */
240 if (ext > AFFS_INODE->i_extcnt)
243 /* get previous extended block */
244 prev_bh = affs_get_extblock(inode, ext - 1);
247 bh = affs_alloc_extblock(inode, prev_bh, ext);
248 affs_brelse(prev_bh);
255 /* check if there is an extended cache and whether it's large enough */
256 lc_idx = ext >> AFFS_INODE->i_lc_shift;
257 lc_off = ext & AFFS_INODE->i_lc_mask;
259 if (lc_idx >= AFFS_INODE->i_lc_size) {
262 err = affs_grow_extcache(inode, lc_idx);
268 /* every n'th key we find in the linear cache */
270 ext_key = AFFS_INODE->i_lc[lc_idx];
274 /* maybe it's still in the associative cache */
275 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
276 if (AFFS_INODE->i_ac[ac_idx].ext == ext) {
277 ext_key = AFFS_INODE->i_ac[ac_idx].key;
281 /* try to find one of the previous extended blocks */
284 while (--tmp, --lc_off > 0) {
285 idx = (idx - 1) & AFFS_AC_MASK;
286 if (AFFS_INODE->i_ac[idx].ext == tmp) {
287 ext_key = AFFS_INODE->i_ac[idx].key;
292 /* fall back to the linear cache */
293 ext_key = AFFS_INODE->i_lc[lc_idx];
295 /* read all extended blocks until we find the one we need */
298 bh = affs_bread(sb, ext_key);
301 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
307 /* store it in the associative cache */
308 // recalculate ac_idx?
309 AFFS_INODE->i_ac[ac_idx].ext = ext;
310 AFFS_INODE->i_ac[ac_idx].key = ext_key;
313 /* finally read the right extended block */
315 bh = affs_bread(sb, ext_key);
321 /* release old cached extended block and store the new one */
322 affs_brelse(AFFS_INODE->i_ext_bh);
323 AFFS_INODE->i_ext_last = ext;
324 AFFS_INODE->i_ext_bh = bh;
325 atomic_inc(&bh->b_count);
331 return ERR_PTR(-EIO);
335 affs_get_block(struct inode *inode, long block, struct buffer_head *bh_result, int create)
337 struct super_block *sb = inode->i_sb;
338 struct buffer_head *ext_bh;
341 pr_debug("AFFS: get_block(%u, %ld)\n", (u32)inode->i_ino, block);
346 if (block >= AFFS_INODE->i_blkcnt) {
347 if (block > AFFS_INODE->i_blkcnt || !create)
353 affs_lock_ext(inode);
355 ext = block / AFFS_SB->s_hashsize;
356 block -= ext * AFFS_SB->s_hashsize;
357 ext_bh = affs_get_extblock(inode, ext);
360 bh_result->b_blocknr = be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block));
361 bh_result->b_dev = inode->i_dev;
362 bh_result->b_state |= (1UL << BH_Mapped);
365 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
368 bh_result->b_state |= (1UL << BH_New);
369 AFFS_INODE->mmu_private += AFFS_SB->s_data_blksize;
370 AFFS_INODE->i_blkcnt++;
372 /* store new block */
373 if (bh_result->b_blocknr)
374 affs_warning(sb, "get_block", "block already set (%x)", bh_result->b_blocknr);
375 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
376 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
377 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
378 bh_result->b_blocknr = blocknr;
381 /* insert first block into header block */
382 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
384 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
385 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
386 affs_adjust_checksum(ext_bh, blocknr - tmp);
392 affs_unlock_ext(inode);
396 affs_error(inode->i_sb,"get_block","Block < 0");
399 affs_error(inode->i_sb,"get_block","strange block request %d", block);
403 affs_unlock_ext(inode);
404 return PTR_ERR(ext_bh);
407 bh_result->b_state &= ~(1UL << BH_Mapped);
409 affs_unlock_ext(inode);
413 static int affs_writepage(struct page *page)
415 return block_write_full_page(page, affs_get_block);
417 static int affs_readpage(struct file *file, struct page *page)
419 return block_read_full_page(page, affs_get_block);
421 static int affs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
423 return cont_prepare_write(page, from, to, affs_get_block,
424 &page->mapping->host->u.affs_i.mmu_private);
426 static int _affs_bmap(struct address_space *mapping, long block)
428 return generic_block_bmap(mapping,block,affs_get_block);
430 struct address_space_operations affs_aops = {
431 readpage: affs_readpage,
432 writepage: affs_writepage,
433 sync_page: block_sync_page,
434 prepare_write: affs_prepare_write,
435 commit_write: generic_commit_write,
439 static inline struct buffer_head *
440 affs_bread_ino(struct inode *inode, int block, int create)
442 struct buffer_head *bh, tmp_bh;
446 err = affs_get_block(inode, block, &tmp_bh, create);
448 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
450 bh->b_state |= tmp_bh.b_state;
458 static inline struct buffer_head *
459 affs_getzeroblk_ino(struct inode *inode, int block)
461 struct buffer_head *bh, tmp_bh;
465 err = affs_get_block(inode, block, &tmp_bh, 1);
467 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
469 bh->b_state |= tmp_bh.b_state;
477 static inline struct buffer_head *
478 affs_getemptyblk_ino(struct inode *inode, int block)
480 struct buffer_head *bh, tmp_bh;
484 err = affs_get_block(inode, block, &tmp_bh, 1);
486 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
488 bh->b_state |= tmp_bh.b_state;
497 affs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
501 retval = generic_file_write (file, buf, count, ppos);
503 struct inode *inode = file->f_dentry->d_inode;
504 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
505 mark_inode_dirty(inode);
511 affs_do_readpage_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
513 struct inode *inode = page->mapping->host;
514 struct super_block *sb = inode->i_sb;
515 struct buffer_head *bh;
517 u32 bidx, boff, bsize;
520 pr_debug("AFFS: read_page(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
521 if (from > to || to > PAGE_CACHE_SIZE)
523 data = page_address(page);
524 bsize = AFFS_SB->s_data_blksize;
525 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
530 bh = affs_bread_ino(inode, bidx, 0);
533 tmp = min(bsize - boff, to - from);
534 if (from + tmp > to || tmp > bsize)
536 memcpy(data + from, AFFS_DATA(bh) + boff, tmp);
546 affs_extent_file_ofs(struct inode *inode, u32 newsize)
548 struct super_block *sb = inode->i_sb;
549 struct buffer_head *bh, *prev_bh;
554 pr_debug("AFFS: extent_file(%u, %d)\n", (u32)inode->i_ino, newsize);
555 bsize = AFFS_SB->s_data_blksize;
557 size = AFFS_INODE->mmu_private;
561 bh = affs_bread_ino(inode, bidx, 0);
564 tmp = min(bsize - boff, newsize - size);
565 if (boff + tmp > bsize || tmp > bsize)
567 memset(AFFS_DATA(bh) + boff, 0, tmp);
568 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh)->size) + tmp);
569 affs_fix_checksum(sb, bh);
570 mark_buffer_dirty_inode(bh, inode);
574 bh = affs_bread_ino(inode, bidx - 1, 0);
579 while (size < newsize) {
581 bh = affs_getzeroblk_ino(inode, bidx);
584 tmp = min(bsize, newsize - size);
587 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
588 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
589 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
590 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
591 affs_fix_checksum(sb, bh);
592 bh->b_state &= ~(1UL << BH_New);
593 mark_buffer_dirty_inode(bh, inode);
595 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
597 affs_warning(sb, "extent_file_ofs", "next block already set for %d (%d)", bidx, tmp);
598 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
599 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
600 mark_buffer_dirty_inode(prev_bh, inode);
601 affs_brelse(prev_bh);
607 inode->i_size = AFFS_INODE->mmu_private = newsize;
611 inode->i_size = AFFS_INODE->mmu_private = size;
616 affs_readpage_ofs(struct file *file, struct page *page)
618 struct inode *inode = page->mapping->host;
622 pr_debug("AFFS: read_page(%u, %ld)\n", (u32)inode->i_ino, page->index);
623 to = PAGE_CACHE_SIZE;
624 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
625 to = inode->i_size & ~PAGE_CACHE_MASK;
626 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
629 err = affs_do_readpage_ofs(file, page, 0, to);
631 SetPageUptodate(page);
636 static int affs_prepare_write_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
638 struct inode *inode = page->mapping->host;
643 pr_debug("AFFS: prepare_write(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
644 offset = page->index << PAGE_CACHE_SHIFT;
645 if (offset + from > AFFS_INODE->mmu_private) {
646 err = affs_extent_file_ofs(inode, offset + from);
650 size = inode->i_size;
652 if (Page_Uptodate(page))
656 err = affs_do_readpage_ofs(file, page, 0, from);
660 if (to < PAGE_CACHE_SIZE) {
661 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
662 if (size > offset + to) {
663 if (size < offset + PAGE_CACHE_SIZE)
664 tmp = size & ~PAGE_CACHE_MASK;
666 tmp = PAGE_CACHE_SIZE;
667 err = affs_do_readpage_ofs(file, page, to, tmp);
673 static int affs_commit_write_ofs(struct file *file, struct page *page, unsigned from, unsigned to)
675 struct inode *inode = page->mapping->host;
676 struct super_block *sb = inode->i_sb;
677 struct buffer_head *bh, *prev_bh;
679 u32 bidx, boff, bsize;
683 pr_debug("AFFS: commit_write(%u, %ld, %d, %d)\n", (u32)inode->i_ino, page->index, from, to);
684 bsize = AFFS_SB->s_data_blksize;
685 data = page_address(page);
689 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
693 bh = affs_bread_ino(inode, bidx, 0);
696 tmp = min(bsize - boff, to - from);
697 if (boff + tmp > bsize || tmp > bsize)
699 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
700 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh)->size) + tmp);
701 affs_fix_checksum(sb, bh);
702 mark_buffer_dirty_inode(bh, inode);
707 bh = affs_bread_ino(inode, bidx - 1, 0);
711 while (from + bsize <= to) {
713 bh = affs_getemptyblk_ino(inode, bidx);
716 memcpy(AFFS_DATA(bh), data + from, bsize);
717 if (bh->b_state & (1UL << BH_New)) {
718 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
719 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
720 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
721 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
722 AFFS_DATA_HEAD(bh)->next = 0;
723 bh->b_state &= ~(1UL << BH_New);
725 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
727 affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp);
728 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
729 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
730 mark_buffer_dirty_inode(prev_bh, inode);
733 affs_brelse(prev_bh);
734 affs_fix_checksum(sb, bh);
735 mark_buffer_dirty_inode(bh, inode);
742 bh = affs_bread_ino(inode, bidx, 1);
745 tmp = min(bsize, to - from);
748 memcpy(AFFS_DATA(bh), data + from, tmp);
749 if (bh->b_state & (1UL << BH_New)) {
750 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
751 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
752 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
753 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
754 AFFS_DATA_HEAD(bh)->next = 0;
755 bh->b_state &= ~(1UL << BH_New);
757 u32 tmp = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
759 affs_warning(sb, "commit_write_ofs", "next block already set for %d (%d)", bidx, tmp);
760 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
761 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp);
762 mark_buffer_dirty_inode(prev_bh, inode);
764 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
765 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
766 affs_brelse(prev_bh);
767 affs_fix_checksum(sb, bh);
768 mark_buffer_dirty_inode(bh, inode);
773 SetPageUptodate(page);
777 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
778 if (tmp > inode->i_size)
779 inode->i_size = AFFS_INODE->mmu_private = tmp;
786 written = PTR_ERR(bh);
790 struct address_space_operations affs_aops_ofs = {
791 readpage: affs_readpage_ofs,
792 //writepage: affs_writepage_ofs,
793 //sync_page: affs_sync_page_ofs,
794 prepare_write: affs_prepare_write_ofs,
795 commit_write: affs_commit_write_ofs
798 /* Free any preallocated blocks. */
801 affs_free_prealloc(struct inode *inode)
803 struct super_block *sb = inode->i_sb;
805 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode->i_ino);
807 while (inode->u.affs_i.i_pa_cnt) {
808 inode->u.affs_i.i_pa_cnt--;
809 affs_free_block(sb, ++inode->u.affs_i.i_lastalloc);
813 /* Truncate (or enlarge) a file to the requested size. */
816 affs_truncate(struct inode *inode)
818 struct super_block *sb = inode->i_sb;
820 u32 last_blk, blkcnt, blk;
822 struct buffer_head *ext_bh;
825 pr_debug("AFFS: truncate(inode=%d, oldsize=%u, newsize=%u)\n",
826 (u32)inode->i_ino, (u32)AFFS_INODE->mmu_private, (u32)inode->i_size);
831 last_blk = ((u32)inode->i_size - 1) / AFFS_SB->s_data_blksize;
832 ext = last_blk / AFFS_SB->s_hashsize;
835 if (inode->i_size > AFFS_INODE->mmu_private) {
836 struct address_space *mapping = inode->i_mapping;
838 u32 size = inode->i_size - 1;
841 page = grab_cache_page(mapping, size >> PAGE_CACHE_SHIFT);
844 size = (size & (PAGE_CACHE_SIZE - 1)) + 1;
845 res = mapping->a_ops->prepare_write(NULL, page, size, size);
847 res = mapping->a_ops->commit_write(NULL, page, size, size);
849 page_cache_release(page);
850 mark_inode_dirty(inode);
852 } else if (inode->i_size == AFFS_INODE->mmu_private)
856 ext_bh = affs_get_extblock(inode, ext);
857 if (IS_ERR(ext_bh)) {
858 affs_warning(sb, "truncate", "unexpected read error for ext block %u (%d)",
859 ext, PTR_ERR(ext_bh));
862 if (AFFS_INODE->i_lc) {
863 /* clear linear cache */
864 i = (ext + 1) >> AFFS_INODE->i_lc_shift;
865 if (AFFS_INODE->i_lc_size > i) {
866 AFFS_INODE->i_lc_size = i;
867 for (; i < AFFS_LC_SIZE; i++)
868 AFFS_INODE->i_lc[i] = 0;
870 /* clear associative cache */
871 for (i = 0; i < AFFS_AC_SIZE; i++)
872 if (AFFS_INODE->i_ac[i].ext >= ext)
873 AFFS_INODE->i_ac[i].ext = 0;
875 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
877 blkcnt = AFFS_INODE->i_blkcnt;
881 i = last_blk % AFFS_SB->s_hashsize + 1;
884 AFFS_HEAD(ext_bh)->first_data = 0;
885 size = AFFS_SB->s_hashsize;
886 if (size > blkcnt - blk + i)
887 size = blkcnt - blk + i;
888 for (; i < size; i++, blk++) {
889 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
890 AFFS_BLOCK(sb, ext_bh, i) = 0;
892 AFFS_TAIL(sb, ext_bh)->extension = 0;
893 affs_fix_checksum(sb, ext_bh);
894 mark_buffer_dirty_inode(ext_bh, inode);
898 AFFS_INODE->i_blkcnt = last_blk + 1;
899 AFFS_INODE->i_extcnt = ext + 1;
900 if (AFFS_SB->s_flags & SF_OFS) {
901 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
904 affs_warning(sb, "truncate", "unexpected read error for last block %u (%d)",
905 last_blk, PTR_ERR(bh));
908 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
909 AFFS_DATA_HEAD(bh)->next = 0;
910 affs_adjust_checksum(bh, -tmp);
914 AFFS_INODE->i_blkcnt = 0;
915 AFFS_INODE->i_extcnt = 1;
917 AFFS_INODE->mmu_private = inode->i_size;
921 ext_bh = affs_bread(sb, ext_key);
922 size = AFFS_SB->s_hashsize;
923 if (size > blkcnt - blk)
925 for (i = 0; i < size; i++, blk++)
926 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
927 affs_free_block(sb, ext_key);
928 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
931 affs_free_prealloc(inode);