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 <linux/aio.h>
18 static struct buffer_head
*affs_get_extblock_slow(struct inode
*inode
, u32 ext
);
21 affs_file_open(struct inode
*inode
, struct file
*filp
)
23 pr_debug("open(%lu,%d)\n",
24 inode
->i_ino
, atomic_read(&AFFS_I(inode
)->i_opencnt
));
25 atomic_inc(&AFFS_I(inode
)->i_opencnt
);
30 affs_file_release(struct inode
*inode
, struct file
*filp
)
32 pr_debug("release(%lu, %d)\n",
33 inode
->i_ino
, atomic_read(&AFFS_I(inode
)->i_opencnt
));
35 if (atomic_dec_and_test(&AFFS_I(inode
)->i_opencnt
)) {
36 mutex_lock(&inode
->i_mutex
);
37 if (inode
->i_size
!= AFFS_I(inode
)->mmu_private
)
39 affs_free_prealloc(inode
);
40 mutex_unlock(&inode
->i_mutex
);
47 affs_grow_extcache(struct inode
*inode
, u32 lc_idx
)
49 struct super_block
*sb
= inode
->i_sb
;
50 struct buffer_head
*bh
;
54 if (!AFFS_I(inode
)->i_lc
) {
55 char *ptr
= (char *)get_zeroed_page(GFP_NOFS
);
58 AFFS_I(inode
)->i_lc
= (u32
*)ptr
;
59 AFFS_I(inode
)->i_ac
= (struct affs_ext_key
*)(ptr
+ AFFS_CACHE_SIZE
/ 2);
62 lc_max
= AFFS_LC_SIZE
<< AFFS_I(inode
)->i_lc_shift
;
64 if (AFFS_I(inode
)->i_extcnt
> lc_max
) {
65 u32 lc_shift
, lc_mask
, tmp
, off
;
67 /* need to recalculate linear cache, start from old size */
68 lc_shift
= AFFS_I(inode
)->i_lc_shift
;
69 tmp
= (AFFS_I(inode
)->i_extcnt
/ AFFS_LC_SIZE
) >> lc_shift
;
70 for (; tmp
; tmp
>>= 1)
72 lc_mask
= (1 << lc_shift
) - 1;
74 /* fix idx and old size to new shift */
75 lc_idx
>>= (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
76 AFFS_I(inode
)->i_lc_size
>>= (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
78 /* first shrink old cache to make more space */
79 off
= 1 << (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
80 for (i
= 1, j
= off
; j
< AFFS_LC_SIZE
; i
++, j
+= off
)
81 AFFS_I(inode
)->i_ac
[i
] = AFFS_I(inode
)->i_ac
[j
];
83 AFFS_I(inode
)->i_lc_shift
= lc_shift
;
84 AFFS_I(inode
)->i_lc_mask
= lc_mask
;
87 /* fill cache to the needed index */
88 i
= AFFS_I(inode
)->i_lc_size
;
89 AFFS_I(inode
)->i_lc_size
= lc_idx
+ 1;
90 for (; i
<= lc_idx
; i
++) {
92 AFFS_I(inode
)->i_lc
[0] = inode
->i_ino
;
95 key
= AFFS_I(inode
)->i_lc
[i
- 1];
96 j
= AFFS_I(inode
)->i_lc_mask
+ 1;
99 bh
= affs_bread(sb
, key
);
102 key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
106 AFFS_I(inode
)->i_lc
[i
] = key
;
116 static struct buffer_head
*
117 affs_alloc_extblock(struct inode
*inode
, struct buffer_head
*bh
, u32 ext
)
119 struct super_block
*sb
= inode
->i_sb
;
120 struct buffer_head
*new_bh
;
123 blocknr
= affs_alloc_block(inode
, bh
->b_blocknr
);
125 return ERR_PTR(-ENOSPC
);
127 new_bh
= affs_getzeroblk(sb
, blocknr
);
129 affs_free_block(sb
, blocknr
);
130 return ERR_PTR(-EIO
);
133 AFFS_HEAD(new_bh
)->ptype
= cpu_to_be32(T_LIST
);
134 AFFS_HEAD(new_bh
)->key
= cpu_to_be32(blocknr
);
135 AFFS_TAIL(sb
, new_bh
)->stype
= cpu_to_be32(ST_FILE
);
136 AFFS_TAIL(sb
, new_bh
)->parent
= cpu_to_be32(inode
->i_ino
);
137 affs_fix_checksum(sb
, new_bh
);
139 mark_buffer_dirty_inode(new_bh
, inode
);
141 tmp
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
143 affs_warning(sb
, "alloc_ext", "previous extension set (%x)", tmp
);
144 AFFS_TAIL(sb
, bh
)->extension
= cpu_to_be32(blocknr
);
145 affs_adjust_checksum(bh
, blocknr
- tmp
);
146 mark_buffer_dirty_inode(bh
, inode
);
148 AFFS_I(inode
)->i_extcnt
++;
149 mark_inode_dirty(inode
);
154 static inline struct buffer_head
*
155 affs_get_extblock(struct inode
*inode
, u32 ext
)
157 /* inline the simplest case: same extended block as last time */
158 struct buffer_head
*bh
= AFFS_I(inode
)->i_ext_bh
;
159 if (ext
== AFFS_I(inode
)->i_ext_last
)
162 /* we have to do more (not inlined) */
163 bh
= affs_get_extblock_slow(inode
, ext
);
168 static struct buffer_head
*
169 affs_get_extblock_slow(struct inode
*inode
, u32 ext
)
171 struct super_block
*sb
= inode
->i_sb
;
172 struct buffer_head
*bh
;
174 u32 lc_idx
, lc_off
, ac_idx
;
177 if (ext
== AFFS_I(inode
)->i_ext_last
+ 1) {
178 /* read the next extended block from the current one */
179 bh
= AFFS_I(inode
)->i_ext_bh
;
180 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
181 if (ext
< AFFS_I(inode
)->i_extcnt
)
183 if (ext
> AFFS_I(inode
)->i_extcnt
)
185 bh
= affs_alloc_extblock(inode
, bh
, ext
);
192 /* we seek back to the file header block */
193 ext_key
= inode
->i_ino
;
197 if (ext
>= AFFS_I(inode
)->i_extcnt
) {
198 struct buffer_head
*prev_bh
;
200 /* allocate a new extended block */
201 if (ext
> AFFS_I(inode
)->i_extcnt
)
204 /* get previous extended block */
205 prev_bh
= affs_get_extblock(inode
, ext
- 1);
208 bh
= affs_alloc_extblock(inode
, prev_bh
, ext
);
209 affs_brelse(prev_bh
);
216 /* check if there is an extended cache and whether it's large enough */
217 lc_idx
= ext
>> AFFS_I(inode
)->i_lc_shift
;
218 lc_off
= ext
& AFFS_I(inode
)->i_lc_mask
;
220 if (lc_idx
>= AFFS_I(inode
)->i_lc_size
) {
223 err
= affs_grow_extcache(inode
, lc_idx
);
229 /* every n'th key we find in the linear cache */
231 ext_key
= AFFS_I(inode
)->i_lc
[lc_idx
];
235 /* maybe it's still in the associative cache */
236 ac_idx
= (ext
- lc_idx
- 1) & AFFS_AC_MASK
;
237 if (AFFS_I(inode
)->i_ac
[ac_idx
].ext
== ext
) {
238 ext_key
= AFFS_I(inode
)->i_ac
[ac_idx
].key
;
242 /* try to find one of the previous extended blocks */
245 while (--tmp
, --lc_off
> 0) {
246 idx
= (idx
- 1) & AFFS_AC_MASK
;
247 if (AFFS_I(inode
)->i_ac
[idx
].ext
== tmp
) {
248 ext_key
= AFFS_I(inode
)->i_ac
[idx
].key
;
253 /* fall back to the linear cache */
254 ext_key
= AFFS_I(inode
)->i_lc
[lc_idx
];
256 /* read all extended blocks until we find the one we need */
259 bh
= affs_bread(sb
, ext_key
);
262 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
268 /* store it in the associative cache */
269 // recalculate ac_idx?
270 AFFS_I(inode
)->i_ac
[ac_idx
].ext
= ext
;
271 AFFS_I(inode
)->i_ac
[ac_idx
].key
= ext_key
;
274 /* finally read the right extended block */
276 bh
= affs_bread(sb
, ext_key
);
282 /* release old cached extended block and store the new one */
283 affs_brelse(AFFS_I(inode
)->i_ext_bh
);
284 AFFS_I(inode
)->i_ext_last
= ext
;
285 AFFS_I(inode
)->i_ext_bh
= bh
;
292 return ERR_PTR(-EIO
);
296 affs_get_block(struct inode
*inode
, sector_t block
, struct buffer_head
*bh_result
, int create
)
298 struct super_block
*sb
= inode
->i_sb
;
299 struct buffer_head
*ext_bh
;
302 pr_debug("%s(%u, %lu)\n",
303 __func__
, (u32
)inode
->i_ino
, (unsigned long)block
);
305 BUG_ON(block
> (sector_t
)0x7fffffffUL
);
307 if (block
>= AFFS_I(inode
)->i_blkcnt
) {
308 if (block
> AFFS_I(inode
)->i_blkcnt
|| !create
)
314 affs_lock_ext(inode
);
316 ext
= (u32
)block
/ AFFS_SB(sb
)->s_hashsize
;
317 block
-= ext
* AFFS_SB(sb
)->s_hashsize
;
318 ext_bh
= affs_get_extblock(inode
, ext
);
321 map_bh(bh_result
, sb
, (sector_t
)be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, block
)));
324 u32 blocknr
= affs_alloc_block(inode
, ext_bh
->b_blocknr
);
327 set_buffer_new(bh_result
);
328 AFFS_I(inode
)->mmu_private
+= AFFS_SB(sb
)->s_data_blksize
;
329 AFFS_I(inode
)->i_blkcnt
++;
331 /* store new block */
332 if (bh_result
->b_blocknr
)
333 affs_warning(sb
, "get_block", "block already set (%lx)",
334 (unsigned long)bh_result
->b_blocknr
);
335 AFFS_BLOCK(sb
, ext_bh
, block
) = cpu_to_be32(blocknr
);
336 AFFS_HEAD(ext_bh
)->block_count
= cpu_to_be32(block
+ 1);
337 affs_adjust_checksum(ext_bh
, blocknr
- bh_result
->b_blocknr
+ 1);
338 bh_result
->b_blocknr
= blocknr
;
341 /* insert first block into header block */
342 u32 tmp
= be32_to_cpu(AFFS_HEAD(ext_bh
)->first_data
);
344 affs_warning(sb
, "get_block", "first block already set (%d)", tmp
);
345 AFFS_HEAD(ext_bh
)->first_data
= cpu_to_be32(blocknr
);
346 affs_adjust_checksum(ext_bh
, blocknr
- tmp
);
352 affs_unlock_ext(inode
);
356 affs_error(inode
->i_sb
, "get_block", "strange block request %d",
361 affs_unlock_ext(inode
);
362 return PTR_ERR(ext_bh
);
365 clear_buffer_mapped(bh_result
);
366 bh_result
->b_bdev
= NULL
;
368 affs_unlock_ext(inode
);
372 static int affs_writepage(struct page
*page
, struct writeback_control
*wbc
)
374 return block_write_full_page(page
, affs_get_block
, wbc
);
377 static int affs_readpage(struct file
*file
, struct page
*page
)
379 return block_read_full_page(page
, affs_get_block
);
382 static void affs_write_failed(struct address_space
*mapping
, loff_t to
)
384 struct inode
*inode
= mapping
->host
;
386 if (to
> inode
->i_size
) {
387 truncate_pagecache(inode
, inode
->i_size
);
388 affs_truncate(inode
);
393 affs_direct_IO(int rw
, struct kiocb
*iocb
, struct iov_iter
*iter
,
396 struct file
*file
= iocb
->ki_filp
;
397 struct address_space
*mapping
= file
->f_mapping
;
398 struct inode
*inode
= mapping
->host
;
399 size_t count
= iov_iter_count(iter
);
402 ret
= blockdev_direct_IO(rw
, iocb
, inode
, iter
, offset
, affs_get_block
);
403 if (ret
< 0 && (rw
& WRITE
))
404 affs_write_failed(mapping
, offset
+ count
);
408 static int affs_write_begin(struct file
*file
, struct address_space
*mapping
,
409 loff_t pos
, unsigned len
, unsigned flags
,
410 struct page
**pagep
, void **fsdata
)
415 ret
= cont_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
417 &AFFS_I(mapping
->host
)->mmu_private
);
419 affs_write_failed(mapping
, pos
+ len
);
424 static sector_t
_affs_bmap(struct address_space
*mapping
, sector_t block
)
426 return generic_block_bmap(mapping
,block
,affs_get_block
);
429 const struct address_space_operations affs_aops
= {
430 .readpage
= affs_readpage
,
431 .writepage
= affs_writepage
,
432 .write_begin
= affs_write_begin
,
433 .write_end
= generic_write_end
,
434 .direct_IO
= affs_direct_IO
,
438 static inline struct buffer_head
*
439 affs_bread_ino(struct inode
*inode
, int block
, int create
)
441 struct buffer_head
*bh
, tmp_bh
;
445 err
= affs_get_block(inode
, block
, &tmp_bh
, create
);
447 bh
= affs_bread(inode
->i_sb
, tmp_bh
.b_blocknr
);
449 bh
->b_state
|= tmp_bh
.b_state
;
457 static inline struct buffer_head
*
458 affs_getzeroblk_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_getzeroblk(inode
->i_sb
, tmp_bh
.b_blocknr
);
468 bh
->b_state
|= tmp_bh
.b_state
;
476 static inline struct buffer_head
*
477 affs_getemptyblk_ino(struct inode
*inode
, int block
)
479 struct buffer_head
*bh
, tmp_bh
;
483 err
= affs_get_block(inode
, block
, &tmp_bh
, 1);
485 bh
= affs_getemptyblk(inode
->i_sb
, tmp_bh
.b_blocknr
);
487 bh
->b_state
|= tmp_bh
.b_state
;
496 affs_do_readpage_ofs(struct page
*page
, unsigned to
)
498 struct inode
*inode
= page
->mapping
->host
;
499 struct super_block
*sb
= inode
->i_sb
;
500 struct buffer_head
*bh
;
503 u32 bidx
, boff
, bsize
;
506 pr_debug("%s(%u, %ld, 0, %d)\n", __func__
, (u32
)inode
->i_ino
,
508 BUG_ON(to
> PAGE_CACHE_SIZE
);
510 data
= page_address(page
);
511 bsize
= AFFS_SB(sb
)->s_data_blksize
;
512 tmp
= page
->index
<< PAGE_CACHE_SHIFT
;
517 bh
= affs_bread_ino(inode
, bidx
, 0);
520 tmp
= min(bsize
- boff
, to
- pos
);
521 BUG_ON(pos
+ tmp
> to
|| tmp
> bsize
);
522 memcpy(data
+ pos
, AFFS_DATA(bh
) + boff
, tmp
);
528 flush_dcache_page(page
);
534 affs_extent_file_ofs(struct inode
*inode
, u32 newsize
)
536 struct super_block
*sb
= inode
->i_sb
;
537 struct buffer_head
*bh
, *prev_bh
;
542 pr_debug("%s(%u, %d)\n", __func__
, (u32
)inode
->i_ino
, newsize
);
543 bsize
= AFFS_SB(sb
)->s_data_blksize
;
545 size
= AFFS_I(inode
)->mmu_private
;
549 bh
= affs_bread_ino(inode
, bidx
, 0);
552 tmp
= min(bsize
- boff
, newsize
- size
);
553 BUG_ON(boff
+ tmp
> bsize
|| tmp
> bsize
);
554 memset(AFFS_DATA(bh
) + boff
, 0, tmp
);
555 be32_add_cpu(&AFFS_DATA_HEAD(bh
)->size
, tmp
);
556 affs_fix_checksum(sb
, bh
);
557 mark_buffer_dirty_inode(bh
, inode
);
561 bh
= affs_bread_ino(inode
, bidx
- 1, 0);
566 while (size
< newsize
) {
568 bh
= affs_getzeroblk_ino(inode
, bidx
);
571 tmp
= min(bsize
, newsize
- size
);
573 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
574 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
575 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
576 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
577 affs_fix_checksum(sb
, bh
);
578 bh
->b_state
&= ~(1UL << BH_New
);
579 mark_buffer_dirty_inode(bh
, inode
);
581 u32 tmp_next
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
584 affs_warning(sb
, "extent_file_ofs",
585 "next block already set for %d (%d)",
587 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
588 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp_next
);
589 mark_buffer_dirty_inode(prev_bh
, inode
);
590 affs_brelse(prev_bh
);
596 inode
->i_size
= AFFS_I(inode
)->mmu_private
= newsize
;
600 inode
->i_size
= AFFS_I(inode
)->mmu_private
= newsize
;
605 affs_readpage_ofs(struct file
*file
, struct page
*page
)
607 struct inode
*inode
= page
->mapping
->host
;
611 pr_debug("%s(%u, %ld)\n", __func__
, (u32
)inode
->i_ino
, page
->index
);
612 to
= PAGE_CACHE_SIZE
;
613 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > inode
->i_size
) {
614 to
= inode
->i_size
& ~PAGE_CACHE_MASK
;
615 memset(page_address(page
) + to
, 0, PAGE_CACHE_SIZE
- to
);
618 err
= affs_do_readpage_ofs(page
, to
);
620 SetPageUptodate(page
);
625 static int affs_write_begin_ofs(struct file
*file
, struct address_space
*mapping
,
626 loff_t pos
, unsigned len
, unsigned flags
,
627 struct page
**pagep
, void **fsdata
)
629 struct inode
*inode
= mapping
->host
;
634 pr_debug("%s(%u, %llu, %llu)\n", __func__
, (u32
)inode
->i_ino
,
635 (unsigned long long)pos
, (unsigned long long)pos
+ len
);
636 if (pos
> AFFS_I(inode
)->mmu_private
) {
637 /* XXX: this probably leaves a too-big i_size in case of
638 * failure. Should really be updating i_size at write_end time
640 err
= affs_extent_file_ofs(inode
, pos
);
645 index
= pos
>> PAGE_CACHE_SHIFT
;
646 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
651 if (PageUptodate(page
))
654 /* XXX: inefficient but safe in the face of short writes */
655 err
= affs_do_readpage_ofs(page
, PAGE_CACHE_SIZE
);
658 page_cache_release(page
);
663 static int affs_write_end_ofs(struct file
*file
, struct address_space
*mapping
,
664 loff_t pos
, unsigned len
, unsigned copied
,
665 struct page
*page
, void *fsdata
)
667 struct inode
*inode
= mapping
->host
;
668 struct super_block
*sb
= inode
->i_sb
;
669 struct buffer_head
*bh
, *prev_bh
;
671 u32 bidx
, boff
, bsize
;
676 from
= pos
& (PAGE_CACHE_SIZE
- 1);
679 * XXX: not sure if this can handle short copies (len < copied), but
680 * we don't have to, because the page should always be uptodate here,
681 * due to write_begin.
684 pr_debug("%s(%u, %llu, %llu)\n",
685 __func__
, (u32
)inode
->i_ino
, (unsigned long long)pos
,
686 (unsigned long long)pos
+ len
);
687 bsize
= AFFS_SB(sb
)->s_data_blksize
;
688 data
= page_address(page
);
692 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
696 bh
= affs_bread_ino(inode
, bidx
, 0);
699 tmp
= min(bsize
- boff
, to
- from
);
700 BUG_ON(boff
+ tmp
> bsize
|| tmp
> bsize
);
701 memcpy(AFFS_DATA(bh
) + boff
, data
+ from
, tmp
);
702 be32_add_cpu(&AFFS_DATA_HEAD(bh
)->size
, tmp
);
703 affs_fix_checksum(sb
, bh
);
704 mark_buffer_dirty_inode(bh
, inode
);
709 bh
= affs_bread_ino(inode
, bidx
- 1, 0);
713 while (from
+ bsize
<= to
) {
715 bh
= affs_getemptyblk_ino(inode
, bidx
);
718 memcpy(AFFS_DATA(bh
), data
+ from
, bsize
);
719 if (buffer_new(bh
)) {
720 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
721 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
722 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
723 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(bsize
);
724 AFFS_DATA_HEAD(bh
)->next
= 0;
725 bh
->b_state
&= ~(1UL << BH_New
);
727 u32 tmp_next
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
730 affs_warning(sb
, "commit_write_ofs",
731 "next block already set for %d (%d)",
733 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
734 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp_next
);
735 mark_buffer_dirty_inode(prev_bh
, inode
);
738 affs_brelse(prev_bh
);
739 affs_fix_checksum(sb
, bh
);
740 mark_buffer_dirty_inode(bh
, inode
);
747 bh
= affs_bread_ino(inode
, bidx
, 1);
750 tmp
= min(bsize
, to
- from
);
752 memcpy(AFFS_DATA(bh
), data
+ from
, tmp
);
753 if (buffer_new(bh
)) {
754 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
755 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
756 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
757 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
758 AFFS_DATA_HEAD(bh
)->next
= 0;
759 bh
->b_state
&= ~(1UL << BH_New
);
761 u32 tmp_next
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
764 affs_warning(sb
, "commit_write_ofs",
765 "next block already set for %d (%d)",
767 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
768 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp_next
);
769 mark_buffer_dirty_inode(prev_bh
, inode
);
771 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh
)->size
) < tmp
)
772 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
773 affs_brelse(prev_bh
);
774 affs_fix_checksum(sb
, bh
);
775 mark_buffer_dirty_inode(bh
, inode
);
780 SetPageUptodate(page
);
784 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
785 if (tmp
> inode
->i_size
)
786 inode
->i_size
= AFFS_I(inode
)->mmu_private
= tmp
;
789 page_cache_release(page
);
796 written
= PTR_ERR(bh
);
800 const struct address_space_operations affs_aops_ofs
= {
801 .readpage
= affs_readpage_ofs
,
802 //.writepage = affs_writepage_ofs,
803 .write_begin
= affs_write_begin_ofs
,
804 .write_end
= affs_write_end_ofs
807 /* Free any preallocated blocks. */
810 affs_free_prealloc(struct inode
*inode
)
812 struct super_block
*sb
= inode
->i_sb
;
814 pr_debug("free_prealloc(ino=%lu)\n", inode
->i_ino
);
816 while (AFFS_I(inode
)->i_pa_cnt
) {
817 AFFS_I(inode
)->i_pa_cnt
--;
818 affs_free_block(sb
, ++AFFS_I(inode
)->i_lastalloc
);
822 /* Truncate (or enlarge) a file to the requested size. */
825 affs_truncate(struct inode
*inode
)
827 struct super_block
*sb
= inode
->i_sb
;
829 u32 last_blk
, blkcnt
, blk
;
831 struct buffer_head
*ext_bh
;
834 pr_debug("truncate(inode=%d, oldsize=%u, newsize=%u)\n",
835 (u32
)inode
->i_ino
, (u32
)AFFS_I(inode
)->mmu_private
, (u32
)inode
->i_size
);
840 last_blk
= ((u32
)inode
->i_size
- 1) / AFFS_SB(sb
)->s_data_blksize
;
841 ext
= last_blk
/ AFFS_SB(sb
)->s_hashsize
;
844 if (inode
->i_size
> AFFS_I(inode
)->mmu_private
) {
845 struct address_space
*mapping
= inode
->i_mapping
;
848 loff_t isize
= inode
->i_size
;
851 res
= mapping
->a_ops
->write_begin(NULL
, mapping
, isize
, 0, 0, &page
, &fsdata
);
853 res
= mapping
->a_ops
->write_end(NULL
, mapping
, isize
, 0, 0, page
, fsdata
);
855 inode
->i_size
= AFFS_I(inode
)->mmu_private
;
856 mark_inode_dirty(inode
);
858 } else if (inode
->i_size
== AFFS_I(inode
)->mmu_private
)
862 ext_bh
= affs_get_extblock(inode
, ext
);
863 if (IS_ERR(ext_bh
)) {
864 affs_warning(sb
, "truncate",
865 "unexpected read error for ext block %u (%ld)",
866 (unsigned int)ext
, PTR_ERR(ext_bh
));
869 if (AFFS_I(inode
)->i_lc
) {
870 /* clear linear cache */
871 i
= (ext
+ 1) >> AFFS_I(inode
)->i_lc_shift
;
872 if (AFFS_I(inode
)->i_lc_size
> i
) {
873 AFFS_I(inode
)->i_lc_size
= i
;
874 for (; i
< AFFS_LC_SIZE
; i
++)
875 AFFS_I(inode
)->i_lc
[i
] = 0;
877 /* clear associative cache */
878 for (i
= 0; i
< AFFS_AC_SIZE
; i
++)
879 if (AFFS_I(inode
)->i_ac
[i
].ext
>= ext
)
880 AFFS_I(inode
)->i_ac
[i
].ext
= 0;
882 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, ext_bh
)->extension
);
884 blkcnt
= AFFS_I(inode
)->i_blkcnt
;
888 i
= last_blk
% AFFS_SB(sb
)->s_hashsize
+ 1;
891 AFFS_HEAD(ext_bh
)->first_data
= 0;
892 AFFS_HEAD(ext_bh
)->block_count
= cpu_to_be32(i
);
893 size
= AFFS_SB(sb
)->s_hashsize
;
894 if (size
> blkcnt
- blk
+ i
)
895 size
= blkcnt
- blk
+ i
;
896 for (; i
< size
; i
++, blk
++) {
897 affs_free_block(sb
, be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, i
)));
898 AFFS_BLOCK(sb
, ext_bh
, i
) = 0;
900 AFFS_TAIL(sb
, ext_bh
)->extension
= 0;
901 affs_fix_checksum(sb
, ext_bh
);
902 mark_buffer_dirty_inode(ext_bh
, inode
);
906 AFFS_I(inode
)->i_blkcnt
= last_blk
+ 1;
907 AFFS_I(inode
)->i_extcnt
= ext
+ 1;
908 if (AFFS_SB(sb
)->s_flags
& SF_OFS
) {
909 struct buffer_head
*bh
= affs_bread_ino(inode
, last_blk
, 0);
912 affs_warning(sb
, "truncate",
913 "unexpected read error for last block %u (%ld)",
914 (unsigned int)ext
, PTR_ERR(bh
));
917 tmp
= be32_to_cpu(AFFS_DATA_HEAD(bh
)->next
);
918 AFFS_DATA_HEAD(bh
)->next
= 0;
919 affs_adjust_checksum(bh
, -tmp
);
923 AFFS_I(inode
)->i_blkcnt
= 0;
924 AFFS_I(inode
)->i_extcnt
= 1;
926 AFFS_I(inode
)->mmu_private
= inode
->i_size
;
930 ext_bh
= affs_bread(sb
, ext_key
);
931 size
= AFFS_SB(sb
)->s_hashsize
;
932 if (size
> blkcnt
- blk
)
934 for (i
= 0; i
< size
; i
++, blk
++)
935 affs_free_block(sb
, be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, i
)));
936 affs_free_block(sb
, ext_key
);
937 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, ext_bh
)->extension
);
940 affs_free_prealloc(inode
);
943 int affs_file_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
945 struct inode
*inode
= filp
->f_mapping
->host
;
948 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
952 mutex_lock(&inode
->i_mutex
);
953 ret
= write_inode_now(inode
, 0);
954 err
= sync_blockdev(inode
->i_sb
->s_bdev
);
957 mutex_unlock(&inode
->i_mutex
);
960 const struct file_operations affs_file_operations
= {
961 .llseek
= generic_file_llseek
,
962 .read
= new_sync_read
,
963 .read_iter
= generic_file_read_iter
,
964 .write
= new_sync_write
,
965 .write_iter
= generic_file_write_iter
,
966 .mmap
= generic_file_mmap
,
967 .open
= affs_file_open
,
968 .release
= affs_file_release
,
969 .fsync
= affs_file_fsync
,
970 .splice_read
= generic_file_splice_read
,
973 const struct inode_operations affs_file_inode_operations
= {
974 .setattr
= affs_notify_change
,