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 struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
24 affs_file_open(struct inode *inode, struct file *filp)
26 pr_debug("open(%lu,%d)\n",
27 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
28 atomic_inc(&AFFS_I(inode)->i_opencnt);
33 affs_file_release(struct inode *inode, struct file *filp)
35 pr_debug("release(%lu, %d)\n",
36 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
38 if (atomic_dec_and_test(&AFFS_I(inode)->i_opencnt)) {
39 mutex_lock(&inode->i_mutex);
40 if (inode->i_size != AFFS_I(inode)->mmu_private)
42 affs_free_prealloc(inode);
43 mutex_unlock(&inode->i_mutex);
50 affs_grow_extcache(struct inode *inode, u32 lc_idx)
52 struct super_block *sb = inode->i_sb;
53 struct buffer_head *bh;
57 if (!AFFS_I(inode)->i_lc) {
58 char *ptr = (char *)get_zeroed_page(GFP_NOFS);
61 AFFS_I(inode)->i_lc = (u32 *)ptr;
62 AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
65 lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
67 if (AFFS_I(inode)->i_extcnt > lc_max) {
68 u32 lc_shift, lc_mask, tmp, off;
70 /* need to recalculate linear cache, start from old size */
71 lc_shift = AFFS_I(inode)->i_lc_shift;
72 tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
73 for (; tmp; tmp >>= 1)
75 lc_mask = (1 << lc_shift) - 1;
77 /* fix idx and old size to new shift */
78 lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
79 AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
81 /* first shrink old cache to make more space */
82 off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
83 for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
84 AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
86 AFFS_I(inode)->i_lc_shift = lc_shift;
87 AFFS_I(inode)->i_lc_mask = lc_mask;
90 /* fill cache to the needed index */
91 i = AFFS_I(inode)->i_lc_size;
92 AFFS_I(inode)->i_lc_size = lc_idx + 1;
93 for (; i <= lc_idx; i++) {
95 AFFS_I(inode)->i_lc[0] = inode->i_ino;
98 key = AFFS_I(inode)->i_lc[i - 1];
99 j = AFFS_I(inode)->i_lc_mask + 1;
102 bh = affs_bread(sb, key);
105 key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
109 AFFS_I(inode)->i_lc[i] = key;
119 static struct buffer_head *
120 affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
122 struct super_block *sb = inode->i_sb;
123 struct buffer_head *new_bh;
126 blocknr = affs_alloc_block(inode, bh->b_blocknr);
128 return ERR_PTR(-ENOSPC);
130 new_bh = affs_getzeroblk(sb, blocknr);
132 affs_free_block(sb, blocknr);
133 return ERR_PTR(-EIO);
136 AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
137 AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
138 AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
139 AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
140 affs_fix_checksum(sb, new_bh);
142 mark_buffer_dirty_inode(new_bh, inode);
144 tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
146 affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
147 AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
148 affs_adjust_checksum(bh, blocknr - tmp);
149 mark_buffer_dirty_inode(bh, inode);
151 AFFS_I(inode)->i_extcnt++;
152 mark_inode_dirty(inode);
157 static inline struct buffer_head *
158 affs_get_extblock(struct inode *inode, u32 ext)
160 /* inline the simplest case: same extended block as last time */
161 struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
162 if (ext == AFFS_I(inode)->i_ext_last)
165 /* we have to do more (not inlined) */
166 bh = affs_get_extblock_slow(inode, ext);
171 static struct buffer_head *
172 affs_get_extblock_slow(struct inode *inode, u32 ext)
174 struct super_block *sb = inode->i_sb;
175 struct buffer_head *bh;
177 u32 lc_idx, lc_off, ac_idx;
180 if (ext == AFFS_I(inode)->i_ext_last + 1) {
181 /* read the next extended block from the current one */
182 bh = AFFS_I(inode)->i_ext_bh;
183 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
184 if (ext < AFFS_I(inode)->i_extcnt)
186 if (ext > AFFS_I(inode)->i_extcnt)
188 bh = affs_alloc_extblock(inode, bh, ext);
195 /* we seek back to the file header block */
196 ext_key = inode->i_ino;
200 if (ext >= AFFS_I(inode)->i_extcnt) {
201 struct buffer_head *prev_bh;
203 /* allocate a new extended block */
204 if (ext > AFFS_I(inode)->i_extcnt)
207 /* get previous extended block */
208 prev_bh = affs_get_extblock(inode, ext - 1);
211 bh = affs_alloc_extblock(inode, prev_bh, ext);
212 affs_brelse(prev_bh);
219 /* check if there is an extended cache and whether it's large enough */
220 lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
221 lc_off = ext & AFFS_I(inode)->i_lc_mask;
223 if (lc_idx >= AFFS_I(inode)->i_lc_size) {
226 err = affs_grow_extcache(inode, lc_idx);
232 /* every n'th key we find in the linear cache */
234 ext_key = AFFS_I(inode)->i_lc[lc_idx];
238 /* maybe it's still in the associative cache */
239 ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
240 if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
241 ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
245 /* try to find one of the previous extended blocks */
248 while (--tmp, --lc_off > 0) {
249 idx = (idx - 1) & AFFS_AC_MASK;
250 if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
251 ext_key = AFFS_I(inode)->i_ac[idx].key;
256 /* fall back to the linear cache */
257 ext_key = AFFS_I(inode)->i_lc[lc_idx];
259 /* read all extended blocks until we find the one we need */
262 bh = affs_bread(sb, ext_key);
265 ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
271 /* store it in the associative cache */
272 // recalculate ac_idx?
273 AFFS_I(inode)->i_ac[ac_idx].ext = ext;
274 AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
277 /* finally read the right extended block */
279 bh = affs_bread(sb, ext_key);
285 /* release old cached extended block and store the new one */
286 affs_brelse(AFFS_I(inode)->i_ext_bh);
287 AFFS_I(inode)->i_ext_last = ext;
288 AFFS_I(inode)->i_ext_bh = bh;
295 return ERR_PTR(-EIO);
299 affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
301 struct super_block *sb = inode->i_sb;
302 struct buffer_head *ext_bh;
305 pr_debug("%s(%u, %lu)\n",
306 __func__, (u32)inode->i_ino, (unsigned long)block);
308 BUG_ON(block > (sector_t)0x7fffffffUL);
310 if (block >= AFFS_I(inode)->i_blkcnt) {
311 if (block > AFFS_I(inode)->i_blkcnt || !create)
317 affs_lock_ext(inode);
319 ext = (u32)block / AFFS_SB(sb)->s_hashsize;
320 block -= ext * AFFS_SB(sb)->s_hashsize;
321 ext_bh = affs_get_extblock(inode, ext);
324 map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
327 u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
330 set_buffer_new(bh_result);
331 AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
332 AFFS_I(inode)->i_blkcnt++;
334 /* store new block */
335 if (bh_result->b_blocknr)
336 affs_warning(sb, "get_block", "block already set (%lx)",
337 (unsigned long)bh_result->b_blocknr);
338 AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
339 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
340 affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
341 bh_result->b_blocknr = blocknr;
344 /* insert first block into header block */
345 u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
347 affs_warning(sb, "get_block", "first block already set (%d)", tmp);
348 AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
349 affs_adjust_checksum(ext_bh, blocknr - tmp);
355 affs_unlock_ext(inode);
359 affs_error(inode->i_sb, "get_block", "strange block request %d",
364 affs_unlock_ext(inode);
365 return PTR_ERR(ext_bh);
368 clear_buffer_mapped(bh_result);
369 bh_result->b_bdev = NULL;
371 affs_unlock_ext(inode);
375 static int affs_writepage(struct page *page, struct writeback_control *wbc)
377 return block_write_full_page(page, affs_get_block, wbc);
380 static int affs_readpage(struct file *file, struct page *page)
382 return block_read_full_page(page, affs_get_block);
385 static void affs_write_failed(struct address_space *mapping, loff_t to)
387 struct inode *inode = mapping->host;
389 if (to > inode->i_size) {
390 truncate_pagecache(inode, inode->i_size);
391 affs_truncate(inode);
395 static int affs_write_begin(struct file *file, struct address_space *mapping,
396 loff_t pos, unsigned len, unsigned flags,
397 struct page **pagep, void **fsdata)
402 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
404 &AFFS_I(mapping->host)->mmu_private);
406 affs_write_failed(mapping, pos + len);
411 static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
413 return generic_block_bmap(mapping,block,affs_get_block);
416 const struct address_space_operations affs_aops = {
417 .readpage = affs_readpage,
418 .writepage = affs_writepage,
419 .write_begin = affs_write_begin,
420 .write_end = generic_write_end,
424 static inline struct buffer_head *
425 affs_bread_ino(struct inode *inode, int block, int create)
427 struct buffer_head *bh, tmp_bh;
431 err = affs_get_block(inode, block, &tmp_bh, create);
433 bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
435 bh->b_state |= tmp_bh.b_state;
443 static inline struct buffer_head *
444 affs_getzeroblk_ino(struct inode *inode, int block)
446 struct buffer_head *bh, tmp_bh;
450 err = affs_get_block(inode, block, &tmp_bh, 1);
452 bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
454 bh->b_state |= tmp_bh.b_state;
462 static inline struct buffer_head *
463 affs_getemptyblk_ino(struct inode *inode, int block)
465 struct buffer_head *bh, tmp_bh;
469 err = affs_get_block(inode, block, &tmp_bh, 1);
471 bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
473 bh->b_state |= tmp_bh.b_state;
482 affs_do_readpage_ofs(struct page *page, unsigned to)
484 struct inode *inode = page->mapping->host;
485 struct super_block *sb = inode->i_sb;
486 struct buffer_head *bh;
489 u32 bidx, boff, bsize;
492 pr_debug("%s(%u, %ld, 0, %d)\n", __func__, (u32)inode->i_ino,
494 BUG_ON(to > PAGE_CACHE_SIZE);
496 data = page_address(page);
497 bsize = AFFS_SB(sb)->s_data_blksize;
498 tmp = page->index << PAGE_CACHE_SHIFT;
503 bh = affs_bread_ino(inode, bidx, 0);
506 tmp = min(bsize - boff, to - pos);
507 BUG_ON(pos + tmp > to || tmp > bsize);
508 memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
514 flush_dcache_page(page);
520 affs_extent_file_ofs(struct inode *inode, u32 newsize)
522 struct super_block *sb = inode->i_sb;
523 struct buffer_head *bh, *prev_bh;
528 pr_debug("%s(%u, %d)\n", __func__, (u32)inode->i_ino, newsize);
529 bsize = AFFS_SB(sb)->s_data_blksize;
531 size = AFFS_I(inode)->mmu_private;
535 bh = affs_bread_ino(inode, bidx, 0);
538 tmp = min(bsize - boff, newsize - size);
539 BUG_ON(boff + tmp > bsize || tmp > bsize);
540 memset(AFFS_DATA(bh) + boff, 0, tmp);
541 be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
542 affs_fix_checksum(sb, bh);
543 mark_buffer_dirty_inode(bh, inode);
547 bh = affs_bread_ino(inode, bidx - 1, 0);
552 while (size < newsize) {
554 bh = affs_getzeroblk_ino(inode, bidx);
557 tmp = min(bsize, newsize - size);
559 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
560 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
561 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
562 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
563 affs_fix_checksum(sb, bh);
564 bh->b_state &= ~(1UL << BH_New);
565 mark_buffer_dirty_inode(bh, inode);
567 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
570 affs_warning(sb, "extent_file_ofs",
571 "next block already set for %d (%d)",
573 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
574 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
575 mark_buffer_dirty_inode(prev_bh, inode);
576 affs_brelse(prev_bh);
582 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
586 inode->i_size = AFFS_I(inode)->mmu_private = newsize;
591 affs_readpage_ofs(struct file *file, struct page *page)
593 struct inode *inode = page->mapping->host;
597 pr_debug("%s(%u, %ld)\n", __func__, (u32)inode->i_ino, page->index);
598 to = PAGE_CACHE_SIZE;
599 if (((page->index + 1) << PAGE_CACHE_SHIFT) > inode->i_size) {
600 to = inode->i_size & ~PAGE_CACHE_MASK;
601 memset(page_address(page) + to, 0, PAGE_CACHE_SIZE - to);
604 err = affs_do_readpage_ofs(page, to);
606 SetPageUptodate(page);
611 static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
612 loff_t pos, unsigned len, unsigned flags,
613 struct page **pagep, void **fsdata)
615 struct inode *inode = mapping->host;
620 pr_debug("%s(%u, %llu, %llu)\n", __func__, (u32)inode->i_ino,
621 (unsigned long long)pos, (unsigned long long)pos + len);
622 if (pos > AFFS_I(inode)->mmu_private) {
623 /* XXX: this probably leaves a too-big i_size in case of
624 * failure. Should really be updating i_size at write_end time
626 err = affs_extent_file_ofs(inode, pos);
631 index = pos >> PAGE_CACHE_SHIFT;
632 page = grab_cache_page_write_begin(mapping, index, flags);
637 if (PageUptodate(page))
640 /* XXX: inefficient but safe in the face of short writes */
641 err = affs_do_readpage_ofs(page, PAGE_CACHE_SIZE);
644 page_cache_release(page);
649 static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
650 loff_t pos, unsigned len, unsigned copied,
651 struct page *page, void *fsdata)
653 struct inode *inode = mapping->host;
654 struct super_block *sb = inode->i_sb;
655 struct buffer_head *bh, *prev_bh;
657 u32 bidx, boff, bsize;
662 from = pos & (PAGE_CACHE_SIZE - 1);
665 * XXX: not sure if this can handle short copies (len < copied), but
666 * we don't have to, because the page should always be uptodate here,
667 * due to write_begin.
670 pr_debug("%s(%u, %llu, %llu)\n",
671 __func__, (u32)inode->i_ino, (unsigned long long)pos,
672 (unsigned long long)pos + len);
673 bsize = AFFS_SB(sb)->s_data_blksize;
674 data = page_address(page);
678 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
682 bh = affs_bread_ino(inode, bidx, 0);
685 tmp = min(bsize - boff, to - from);
686 BUG_ON(boff + tmp > bsize || tmp > bsize);
687 memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
688 be32_add_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_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
716 affs_warning(sb, "commit_write_ofs",
717 "next block already set for %d (%d)",
719 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
720 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
721 mark_buffer_dirty_inode(prev_bh, inode);
724 affs_brelse(prev_bh);
725 affs_fix_checksum(sb, bh);
726 mark_buffer_dirty_inode(bh, inode);
733 bh = affs_bread_ino(inode, bidx, 1);
736 tmp = min(bsize, to - from);
738 memcpy(AFFS_DATA(bh), data + from, tmp);
739 if (buffer_new(bh)) {
740 AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
741 AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
742 AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
743 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
744 AFFS_DATA_HEAD(bh)->next = 0;
745 bh->b_state &= ~(1UL << BH_New);
747 u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
750 affs_warning(sb, "commit_write_ofs",
751 "next block already set for %d (%d)",
753 AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
754 affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
755 mark_buffer_dirty_inode(prev_bh, inode);
757 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
758 AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
759 affs_brelse(prev_bh);
760 affs_fix_checksum(sb, bh);
761 mark_buffer_dirty_inode(bh, inode);
766 SetPageUptodate(page);
770 tmp = (page->index << PAGE_CACHE_SHIFT) + from;
771 if (tmp > inode->i_size)
772 inode->i_size = AFFS_I(inode)->mmu_private = tmp;
775 page_cache_release(page);
782 written = PTR_ERR(bh);
786 const struct address_space_operations affs_aops_ofs = {
787 .readpage = affs_readpage_ofs,
788 //.writepage = affs_writepage_ofs,
789 .write_begin = affs_write_begin_ofs,
790 .write_end = affs_write_end_ofs
793 /* Free any preallocated blocks. */
796 affs_free_prealloc(struct inode *inode)
798 struct super_block *sb = inode->i_sb;
800 pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
802 while (AFFS_I(inode)->i_pa_cnt) {
803 AFFS_I(inode)->i_pa_cnt--;
804 affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
808 /* Truncate (or enlarge) a file to the requested size. */
811 affs_truncate(struct inode *inode)
813 struct super_block *sb = inode->i_sb;
815 u32 last_blk, blkcnt, blk;
817 struct buffer_head *ext_bh;
820 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
821 (u32)inode->i_ino, (u32)AFFS_I(inode)->mmu_private, (u32)inode->i_size);
826 last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
827 ext = last_blk / AFFS_SB(sb)->s_hashsize;
830 if (inode->i_size > AFFS_I(inode)->mmu_private) {
831 struct address_space *mapping = inode->i_mapping;
834 loff_t isize = inode->i_size;
837 res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
839 res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
841 inode->i_size = AFFS_I(inode)->mmu_private;
842 mark_inode_dirty(inode);
844 } else if (inode->i_size == AFFS_I(inode)->mmu_private)
848 ext_bh = affs_get_extblock(inode, ext);
849 if (IS_ERR(ext_bh)) {
850 affs_warning(sb, "truncate",
851 "unexpected read error for ext block %u (%ld)",
852 (unsigned int)ext, PTR_ERR(ext_bh));
855 if (AFFS_I(inode)->i_lc) {
856 /* clear linear cache */
857 i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
858 if (AFFS_I(inode)->i_lc_size > i) {
859 AFFS_I(inode)->i_lc_size = i;
860 for (; i < AFFS_LC_SIZE; i++)
861 AFFS_I(inode)->i_lc[i] = 0;
863 /* clear associative cache */
864 for (i = 0; i < AFFS_AC_SIZE; i++)
865 if (AFFS_I(inode)->i_ac[i].ext >= ext)
866 AFFS_I(inode)->i_ac[i].ext = 0;
868 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
870 blkcnt = AFFS_I(inode)->i_blkcnt;
874 i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
877 AFFS_HEAD(ext_bh)->first_data = 0;
878 AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
879 size = AFFS_SB(sb)->s_hashsize;
880 if (size > blkcnt - blk + i)
881 size = blkcnt - blk + i;
882 for (; i < size; i++, blk++) {
883 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
884 AFFS_BLOCK(sb, ext_bh, i) = 0;
886 AFFS_TAIL(sb, ext_bh)->extension = 0;
887 affs_fix_checksum(sb, ext_bh);
888 mark_buffer_dirty_inode(ext_bh, inode);
892 AFFS_I(inode)->i_blkcnt = last_blk + 1;
893 AFFS_I(inode)->i_extcnt = ext + 1;
894 if (AFFS_SB(sb)->s_flags & SF_OFS) {
895 struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
898 affs_warning(sb, "truncate",
899 "unexpected read error for last block %u (%ld)",
900 (unsigned int)ext, PTR_ERR(bh));
903 tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
904 AFFS_DATA_HEAD(bh)->next = 0;
905 affs_adjust_checksum(bh, -tmp);
909 AFFS_I(inode)->i_blkcnt = 0;
910 AFFS_I(inode)->i_extcnt = 1;
912 AFFS_I(inode)->mmu_private = inode->i_size;
916 ext_bh = affs_bread(sb, ext_key);
917 size = AFFS_SB(sb)->s_hashsize;
918 if (size > blkcnt - blk)
920 for (i = 0; i < size; i++, blk++)
921 affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
922 affs_free_block(sb, ext_key);
923 ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
926 affs_free_prealloc(inode);
929 int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
931 struct inode *inode = filp->f_mapping->host;
934 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
938 mutex_lock(&inode->i_mutex);
939 ret = write_inode_now(inode, 0);
940 err = sync_blockdev(inode->i_sb->s_bdev);
943 mutex_unlock(&inode->i_mutex);
946 const struct file_operations affs_file_operations = {
947 .llseek = generic_file_llseek,
948 .read = new_sync_read,
949 .read_iter = generic_file_read_iter,
950 .write = new_sync_write,
951 .write_iter = generic_file_write_iter,
952 .mmap = generic_file_mmap,
953 .open = affs_file_open,
954 .release = affs_file_release,
955 .fsync = affs_file_fsync,
956 .splice_read = generic_file_splice_read,
959 const struct inode_operations affs_file_inode_operations = {
960 .setattr = affs_notify_change,