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 int affs_file_open(struct inode
*inode
, struct file
*filp
);
26 static int affs_file_release(struct inode
*inode
, struct file
*filp
);
28 const struct file_operations affs_file_operations
= {
29 .llseek
= generic_file_llseek
,
30 .read
= generic_file_read
,
31 .write
= generic_file_write
,
32 .mmap
= generic_file_mmap
,
33 .open
= affs_file_open
,
34 .release
= affs_file_release
,
36 .sendfile
= generic_file_sendfile
,
39 struct inode_operations affs_file_inode_operations
= {
40 .truncate
= affs_truncate
,
41 .setattr
= affs_notify_change
,
45 affs_file_open(struct inode
*inode
, struct file
*filp
)
47 if (atomic_read(&filp
->f_count
) != 1)
49 pr_debug("AFFS: open(%d)\n", AFFS_I(inode
)->i_opencnt
);
50 AFFS_I(inode
)->i_opencnt
++;
55 affs_file_release(struct inode
*inode
, struct file
*filp
)
57 if (atomic_read(&filp
->f_count
) != 0)
59 pr_debug("AFFS: release(%d)\n", AFFS_I(inode
)->i_opencnt
);
60 AFFS_I(inode
)->i_opencnt
--;
61 if (!AFFS_I(inode
)->i_opencnt
)
62 affs_free_prealloc(inode
);
68 affs_grow_extcache(struct inode
*inode
, u32 lc_idx
)
70 struct super_block
*sb
= inode
->i_sb
;
71 struct buffer_head
*bh
;
75 if (!AFFS_I(inode
)->i_lc
) {
76 char *ptr
= (char *)get_zeroed_page(GFP_NOFS
);
79 AFFS_I(inode
)->i_lc
= (u32
*)ptr
;
80 AFFS_I(inode
)->i_ac
= (struct affs_ext_key
*)(ptr
+ AFFS_CACHE_SIZE
/ 2);
83 lc_max
= AFFS_LC_SIZE
<< AFFS_I(inode
)->i_lc_shift
;
85 if (AFFS_I(inode
)->i_extcnt
> lc_max
) {
86 u32 lc_shift
, lc_mask
, tmp
, off
;
88 /* need to recalculate linear cache, start from old size */
89 lc_shift
= AFFS_I(inode
)->i_lc_shift
;
90 tmp
= (AFFS_I(inode
)->i_extcnt
/ AFFS_LC_SIZE
) >> lc_shift
;
91 for (; tmp
; tmp
>>= 1)
93 lc_mask
= (1 << lc_shift
) - 1;
95 /* fix idx and old size to new shift */
96 lc_idx
>>= (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
97 AFFS_I(inode
)->i_lc_size
>>= (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
99 /* first shrink old cache to make more space */
100 off
= 1 << (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
101 for (i
= 1, j
= off
; j
< AFFS_LC_SIZE
; i
++, j
+= off
)
102 AFFS_I(inode
)->i_ac
[i
] = AFFS_I(inode
)->i_ac
[j
];
104 AFFS_I(inode
)->i_lc_shift
= lc_shift
;
105 AFFS_I(inode
)->i_lc_mask
= lc_mask
;
108 /* fill cache to the needed index */
109 i
= AFFS_I(inode
)->i_lc_size
;
110 AFFS_I(inode
)->i_lc_size
= lc_idx
+ 1;
111 for (; i
<= lc_idx
; i
++) {
113 AFFS_I(inode
)->i_lc
[0] = inode
->i_ino
;
116 key
= AFFS_I(inode
)->i_lc
[i
- 1];
117 j
= AFFS_I(inode
)->i_lc_mask
+ 1;
120 bh
= affs_bread(sb
, key
);
123 key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
127 AFFS_I(inode
)->i_lc
[i
] = key
;
137 static struct buffer_head
*
138 affs_alloc_extblock(struct inode
*inode
, struct buffer_head
*bh
, u32 ext
)
140 struct super_block
*sb
= inode
->i_sb
;
141 struct buffer_head
*new_bh
;
144 blocknr
= affs_alloc_block(inode
, bh
->b_blocknr
);
146 return ERR_PTR(-ENOSPC
);
148 new_bh
= affs_getzeroblk(sb
, blocknr
);
150 affs_free_block(sb
, blocknr
);
151 return ERR_PTR(-EIO
);
154 AFFS_HEAD(new_bh
)->ptype
= cpu_to_be32(T_LIST
);
155 AFFS_HEAD(new_bh
)->key
= cpu_to_be32(blocknr
);
156 AFFS_TAIL(sb
, new_bh
)->stype
= cpu_to_be32(ST_FILE
);
157 AFFS_TAIL(sb
, new_bh
)->parent
= cpu_to_be32(inode
->i_ino
);
158 affs_fix_checksum(sb
, new_bh
);
160 mark_buffer_dirty_inode(new_bh
, inode
);
162 tmp
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
164 affs_warning(sb
, "alloc_ext", "previous extension set (%x)", tmp
);
165 AFFS_TAIL(sb
, bh
)->extension
= cpu_to_be32(blocknr
);
166 affs_adjust_checksum(bh
, blocknr
- tmp
);
167 mark_buffer_dirty_inode(bh
, inode
);
169 AFFS_I(inode
)->i_extcnt
++;
170 mark_inode_dirty(inode
);
175 static inline struct buffer_head
*
176 affs_get_extblock(struct inode
*inode
, u32 ext
)
178 /* inline the simplest case: same extended block as last time */
179 struct buffer_head
*bh
= AFFS_I(inode
)->i_ext_bh
;
180 if (ext
== AFFS_I(inode
)->i_ext_last
)
181 atomic_inc(&bh
->b_count
);
183 /* we have to do more (not inlined) */
184 bh
= affs_get_extblock_slow(inode
, ext
);
189 static struct buffer_head
*
190 affs_get_extblock_slow(struct inode
*inode
, u32 ext
)
192 struct super_block
*sb
= inode
->i_sb
;
193 struct buffer_head
*bh
;
195 u32 lc_idx
, lc_off
, ac_idx
;
198 if (ext
== AFFS_I(inode
)->i_ext_last
+ 1) {
199 /* read the next extended block from the current one */
200 bh
= AFFS_I(inode
)->i_ext_bh
;
201 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
202 if (ext
< AFFS_I(inode
)->i_extcnt
)
204 if (ext
> AFFS_I(inode
)->i_extcnt
)
206 bh
= affs_alloc_extblock(inode
, bh
, ext
);
213 /* we seek back to the file header block */
214 ext_key
= inode
->i_ino
;
218 if (ext
>= AFFS_I(inode
)->i_extcnt
) {
219 struct buffer_head
*prev_bh
;
221 /* allocate a new extended block */
222 if (ext
> AFFS_I(inode
)->i_extcnt
)
225 /* get previous extended block */
226 prev_bh
= affs_get_extblock(inode
, ext
- 1);
229 bh
= affs_alloc_extblock(inode
, prev_bh
, ext
);
230 affs_brelse(prev_bh
);
237 /* check if there is an extended cache and whether it's large enough */
238 lc_idx
= ext
>> AFFS_I(inode
)->i_lc_shift
;
239 lc_off
= ext
& AFFS_I(inode
)->i_lc_mask
;
241 if (lc_idx
>= AFFS_I(inode
)->i_lc_size
) {
244 err
= affs_grow_extcache(inode
, lc_idx
);
250 /* every n'th key we find in the linear cache */
252 ext_key
= AFFS_I(inode
)->i_lc
[lc_idx
];
256 /* maybe it's still in the associative cache */
257 ac_idx
= (ext
- lc_idx
- 1) & AFFS_AC_MASK
;
258 if (AFFS_I(inode
)->i_ac
[ac_idx
].ext
== ext
) {
259 ext_key
= AFFS_I(inode
)->i_ac
[ac_idx
].key
;
263 /* try to find one of the previous extended blocks */
266 while (--tmp
, --lc_off
> 0) {
267 idx
= (idx
- 1) & AFFS_AC_MASK
;
268 if (AFFS_I(inode
)->i_ac
[idx
].ext
== tmp
) {
269 ext_key
= AFFS_I(inode
)->i_ac
[idx
].key
;
274 /* fall back to the linear cache */
275 ext_key
= AFFS_I(inode
)->i_lc
[lc_idx
];
277 /* read all extended blocks until we find the one we need */
280 bh
= affs_bread(sb
, ext_key
);
283 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
289 /* store it in the associative cache */
290 // recalculate ac_idx?
291 AFFS_I(inode
)->i_ac
[ac_idx
].ext
= ext
;
292 AFFS_I(inode
)->i_ac
[ac_idx
].key
= ext_key
;
295 /* finally read the right extended block */
297 bh
= affs_bread(sb
, ext_key
);
303 /* release old cached extended block and store the new one */
304 affs_brelse(AFFS_I(inode
)->i_ext_bh
);
305 AFFS_I(inode
)->i_ext_last
= ext
;
306 AFFS_I(inode
)->i_ext_bh
= bh
;
307 atomic_inc(&bh
->b_count
);
313 return ERR_PTR(-EIO
);
317 affs_get_block(struct inode
*inode
, sector_t block
, struct buffer_head
*bh_result
, int create
)
319 struct super_block
*sb
= inode
->i_sb
;
320 struct buffer_head
*ext_bh
;
323 pr_debug("AFFS: get_block(%u, %lu)\n", (u32
)inode
->i_ino
, (unsigned long)block
);
326 if (block
> (sector_t
)0x7fffffffUL
)
329 if (block
>= AFFS_I(inode
)->i_blkcnt
) {
330 if (block
> AFFS_I(inode
)->i_blkcnt
|| !create
)
336 affs_lock_ext(inode
);
338 ext
= (u32
)block
/ AFFS_SB(sb
)->s_hashsize
;
339 block
-= ext
* AFFS_SB(sb
)->s_hashsize
;
340 ext_bh
= affs_get_extblock(inode
, ext
);
343 map_bh(bh_result
, sb
, (sector_t
)be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, block
)));
346 u32 blocknr
= affs_alloc_block(inode
, ext_bh
->b_blocknr
);
349 set_buffer_new(bh_result
);
350 AFFS_I(inode
)->mmu_private
+= AFFS_SB(sb
)->s_data_blksize
;
351 AFFS_I(inode
)->i_blkcnt
++;
353 /* store new block */
354 if (bh_result
->b_blocknr
)
355 affs_warning(sb
, "get_block", "block already set (%x)", bh_result
->b_blocknr
);
356 AFFS_BLOCK(sb
, ext_bh
, block
) = cpu_to_be32(blocknr
);
357 AFFS_HEAD(ext_bh
)->block_count
= cpu_to_be32(block
+ 1);
358 affs_adjust_checksum(ext_bh
, blocknr
- bh_result
->b_blocknr
+ 1);
359 bh_result
->b_blocknr
= blocknr
;
362 /* insert first block into header block */
363 u32 tmp
= be32_to_cpu(AFFS_HEAD(ext_bh
)->first_data
);
365 affs_warning(sb
, "get_block", "first block already set (%d)", tmp
);
366 AFFS_HEAD(ext_bh
)->first_data
= cpu_to_be32(blocknr
);
367 affs_adjust_checksum(ext_bh
, blocknr
- tmp
);
373 affs_unlock_ext(inode
);
377 affs_error(inode
->i_sb
,"get_block","strange block request %d", block
);
381 affs_unlock_ext(inode
);
382 return PTR_ERR(ext_bh
);
385 clear_buffer_mapped(bh_result
);
386 bh_result
->b_bdev
= NULL
;
388 affs_unlock_ext(inode
);
392 static int affs_writepage(struct page
*page
, struct writeback_control
*wbc
)
394 return block_write_full_page(page
, affs_get_block
, wbc
);
396 static int affs_readpage(struct file
*file
, struct page
*page
)
398 return block_read_full_page(page
, affs_get_block
);
400 static int affs_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
402 return cont_prepare_write(page
, from
, to
, affs_get_block
,
403 &AFFS_I(page
->mapping
->host
)->mmu_private
);
405 static sector_t
_affs_bmap(struct address_space
*mapping
, sector_t block
)
407 return generic_block_bmap(mapping
,block
,affs_get_block
);
409 struct address_space_operations affs_aops
= {
410 .readpage
= affs_readpage
,
411 .writepage
= affs_writepage
,
412 .sync_page
= block_sync_page
,
413 .prepare_write
= affs_prepare_write
,
414 .commit_write
= generic_commit_write
,
418 static inline struct buffer_head
*
419 affs_bread_ino(struct inode
*inode
, int block
, int create
)
421 struct buffer_head
*bh
, tmp_bh
;
425 err
= affs_get_block(inode
, block
, &tmp_bh
, create
);
427 bh
= affs_bread(inode
->i_sb
, tmp_bh
.b_blocknr
);
429 bh
->b_state
|= tmp_bh
.b_state
;
437 static inline struct buffer_head
*
438 affs_getzeroblk_ino(struct inode
*inode
, int block
)
440 struct buffer_head
*bh
, tmp_bh
;
444 err
= affs_get_block(inode
, block
, &tmp_bh
, 1);
446 bh
= affs_getzeroblk(inode
->i_sb
, tmp_bh
.b_blocknr
);
448 bh
->b_state
|= tmp_bh
.b_state
;
456 static inline struct buffer_head
*
457 affs_getemptyblk_ino(struct inode
*inode
, int block
)
459 struct buffer_head
*bh
, tmp_bh
;
463 err
= affs_get_block(inode
, block
, &tmp_bh
, 1);
465 bh
= affs_getemptyblk(inode
->i_sb
, tmp_bh
.b_blocknr
);
467 bh
->b_state
|= tmp_bh
.b_state
;
476 affs_do_readpage_ofs(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
478 struct inode
*inode
= page
->mapping
->host
;
479 struct super_block
*sb
= inode
->i_sb
;
480 struct buffer_head
*bh
;
482 u32 bidx
, boff
, bsize
;
485 pr_debug("AFFS: read_page(%u, %ld, %d, %d)\n", (u32
)inode
->i_ino
, page
->index
, from
, to
);
486 if (from
> to
|| to
> PAGE_CACHE_SIZE
)
489 data
= page_address(page
);
490 bsize
= AFFS_SB(sb
)->s_data_blksize
;
491 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
496 bh
= affs_bread_ino(inode
, bidx
, 0);
499 tmp
= min(bsize
- boff
, to
- from
);
500 if (from
+ tmp
> to
|| tmp
> bsize
)
502 memcpy(data
+ from
, AFFS_DATA(bh
) + boff
, tmp
);
508 flush_dcache_page(page
);
514 affs_extent_file_ofs(struct inode
*inode
, u32 newsize
)
516 struct super_block
*sb
= inode
->i_sb
;
517 struct buffer_head
*bh
, *prev_bh
;
522 pr_debug("AFFS: extent_file(%u, %d)\n", (u32
)inode
->i_ino
, newsize
);
523 bsize
= AFFS_SB(sb
)->s_data_blksize
;
525 size
= AFFS_I(inode
)->mmu_private
;
529 bh
= affs_bread_ino(inode
, bidx
, 0);
532 tmp
= min(bsize
- boff
, newsize
- size
);
533 if (boff
+ tmp
> bsize
|| tmp
> bsize
)
535 memset(AFFS_DATA(bh
) + boff
, 0, tmp
);
536 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh
)->size
) + tmp
);
537 affs_fix_checksum(sb
, bh
);
538 mark_buffer_dirty_inode(bh
, inode
);
542 bh
= affs_bread_ino(inode
, bidx
- 1, 0);
547 while (size
< newsize
) {
549 bh
= affs_getzeroblk_ino(inode
, bidx
);
552 tmp
= min(bsize
, newsize
- size
);
555 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
556 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
557 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
558 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
559 affs_fix_checksum(sb
, bh
);
560 bh
->b_state
&= ~(1UL << BH_New
);
561 mark_buffer_dirty_inode(bh
, inode
);
563 u32 tmp
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
565 affs_warning(sb
, "extent_file_ofs", "next block already set for %d (%d)", bidx
, tmp
);
566 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
567 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp
);
568 mark_buffer_dirty_inode(prev_bh
, inode
);
569 affs_brelse(prev_bh
);
575 inode
->i_size
= AFFS_I(inode
)->mmu_private
= newsize
;
579 inode
->i_size
= AFFS_I(inode
)->mmu_private
= newsize
;
584 affs_readpage_ofs(struct file
*file
, struct page
*page
)
586 struct inode
*inode
= page
->mapping
->host
;
590 pr_debug("AFFS: read_page(%u, %ld)\n", (u32
)inode
->i_ino
, page
->index
);
591 to
= PAGE_CACHE_SIZE
;
592 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > inode
->i_size
) {
593 to
= inode
->i_size
& ~PAGE_CACHE_MASK
;
594 memset(page_address(page
) + to
, 0, PAGE_CACHE_SIZE
- to
);
597 err
= affs_do_readpage_ofs(file
, page
, 0, to
);
599 SetPageUptodate(page
);
604 static int affs_prepare_write_ofs(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
606 struct inode
*inode
= page
->mapping
->host
;
611 pr_debug("AFFS: prepare_write(%u, %ld, %d, %d)\n", (u32
)inode
->i_ino
, page
->index
, from
, to
);
612 offset
= page
->index
<< PAGE_CACHE_SHIFT
;
613 if (offset
+ from
> AFFS_I(inode
)->mmu_private
) {
614 err
= affs_extent_file_ofs(inode
, offset
+ from
);
618 size
= inode
->i_size
;
620 if (PageUptodate(page
))
624 err
= affs_do_readpage_ofs(file
, page
, 0, from
);
628 if (to
< PAGE_CACHE_SIZE
) {
629 char *kaddr
= kmap_atomic(page
, KM_USER0
);
631 memset(kaddr
+ to
, 0, PAGE_CACHE_SIZE
- to
);
632 flush_dcache_page(page
);
633 kunmap_atomic(kaddr
, KM_USER0
);
634 if (size
> offset
+ to
) {
635 if (size
< offset
+ PAGE_CACHE_SIZE
)
636 tmp
= size
& ~PAGE_CACHE_MASK
;
638 tmp
= PAGE_CACHE_SIZE
;
639 err
= affs_do_readpage_ofs(file
, page
, to
, tmp
);
645 static int affs_commit_write_ofs(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
647 struct inode
*inode
= page
->mapping
->host
;
648 struct super_block
*sb
= inode
->i_sb
;
649 struct buffer_head
*bh
, *prev_bh
;
651 u32 bidx
, boff
, bsize
;
655 pr_debug("AFFS: commit_write(%u, %ld, %d, %d)\n", (u32
)inode
->i_ino
, page
->index
, from
, to
);
656 bsize
= AFFS_SB(sb
)->s_data_blksize
;
657 data
= page_address(page
);
661 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
665 bh
= affs_bread_ino(inode
, bidx
, 0);
668 tmp
= min(bsize
- boff
, to
- from
);
669 if (boff
+ tmp
> bsize
|| tmp
> bsize
)
671 memcpy(AFFS_DATA(bh
) + boff
, data
+ from
, tmp
);
672 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(be32_to_cpu(AFFS_DATA_HEAD(bh
)->size
) + tmp
);
673 affs_fix_checksum(sb
, bh
);
674 mark_buffer_dirty_inode(bh
, inode
);
679 bh
= affs_bread_ino(inode
, bidx
- 1, 0);
683 while (from
+ bsize
<= to
) {
685 bh
= affs_getemptyblk_ino(inode
, bidx
);
688 memcpy(AFFS_DATA(bh
), data
+ from
, bsize
);
689 if (buffer_new(bh
)) {
690 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
691 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
692 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
693 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(bsize
);
694 AFFS_DATA_HEAD(bh
)->next
= 0;
695 bh
->b_state
&= ~(1UL << BH_New
);
697 u32 tmp
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
699 affs_warning(sb
, "commit_write_ofs", "next block already set for %d (%d)", bidx
, tmp
);
700 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
701 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp
);
702 mark_buffer_dirty_inode(prev_bh
, inode
);
705 affs_brelse(prev_bh
);
706 affs_fix_checksum(sb
, bh
);
707 mark_buffer_dirty_inode(bh
, inode
);
714 bh
= affs_bread_ino(inode
, bidx
, 1);
717 tmp
= min(bsize
, to
- from
);
720 memcpy(AFFS_DATA(bh
), data
+ from
, tmp
);
721 if (buffer_new(bh
)) {
722 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
723 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
724 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
725 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
726 AFFS_DATA_HEAD(bh
)->next
= 0;
727 bh
->b_state
&= ~(1UL << BH_New
);
729 u32 tmp
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
731 affs_warning(sb
, "commit_write_ofs", "next block already set for %d (%d)", bidx
, tmp
);
732 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
733 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp
);
734 mark_buffer_dirty_inode(prev_bh
, inode
);
736 } else if (be32_to_cpu(AFFS_DATA_HEAD(bh
)->size
) < tmp
)
737 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
738 affs_brelse(prev_bh
);
739 affs_fix_checksum(sb
, bh
);
740 mark_buffer_dirty_inode(bh
, inode
);
745 SetPageUptodate(page
);
749 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
750 if (tmp
> inode
->i_size
)
751 inode
->i_size
= AFFS_I(inode
)->mmu_private
= tmp
;
758 written
= PTR_ERR(bh
);
762 struct address_space_operations affs_aops_ofs
= {
763 .readpage
= affs_readpage_ofs
,
764 //.writepage = affs_writepage_ofs,
765 //.sync_page = affs_sync_page_ofs,
766 .prepare_write
= affs_prepare_write_ofs
,
767 .commit_write
= affs_commit_write_ofs
770 /* Free any preallocated blocks. */
773 affs_free_prealloc(struct inode
*inode
)
775 struct super_block
*sb
= inode
->i_sb
;
777 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode
->i_ino
);
779 while (AFFS_I(inode
)->i_pa_cnt
) {
780 AFFS_I(inode
)->i_pa_cnt
--;
781 affs_free_block(sb
, ++AFFS_I(inode
)->i_lastalloc
);
785 /* Truncate (or enlarge) a file to the requested size. */
788 affs_truncate(struct inode
*inode
)
790 struct super_block
*sb
= inode
->i_sb
;
792 u32 last_blk
, blkcnt
, blk
;
794 struct buffer_head
*ext_bh
;
797 pr_debug("AFFS: truncate(inode=%d, oldsize=%u, newsize=%u)\n",
798 (u32
)inode
->i_ino
, (u32
)AFFS_I(inode
)->mmu_private
, (u32
)inode
->i_size
);
803 last_blk
= ((u32
)inode
->i_size
- 1) / AFFS_SB(sb
)->s_data_blksize
;
804 ext
= last_blk
/ AFFS_SB(sb
)->s_hashsize
;
807 if (inode
->i_size
> AFFS_I(inode
)->mmu_private
) {
808 struct address_space
*mapping
= inode
->i_mapping
;
810 u32 size
= inode
->i_size
- 1;
813 page
= grab_cache_page(mapping
, size
>> PAGE_CACHE_SHIFT
);
816 size
= (size
& (PAGE_CACHE_SIZE
- 1)) + 1;
817 res
= mapping
->a_ops
->prepare_write(NULL
, page
, size
, size
);
819 res
= mapping
->a_ops
->commit_write(NULL
, page
, size
, size
);
821 page_cache_release(page
);
822 mark_inode_dirty(inode
);
824 } else if (inode
->i_size
== AFFS_I(inode
)->mmu_private
)
828 ext_bh
= affs_get_extblock(inode
, ext
);
829 if (IS_ERR(ext_bh
)) {
830 affs_warning(sb
, "truncate", "unexpected read error for ext block %u (%d)",
831 ext
, PTR_ERR(ext_bh
));
834 if (AFFS_I(inode
)->i_lc
) {
835 /* clear linear cache */
836 i
= (ext
+ 1) >> AFFS_I(inode
)->i_lc_shift
;
837 if (AFFS_I(inode
)->i_lc_size
> i
) {
838 AFFS_I(inode
)->i_lc_size
= i
;
839 for (; i
< AFFS_LC_SIZE
; i
++)
840 AFFS_I(inode
)->i_lc
[i
] = 0;
842 /* clear associative cache */
843 for (i
= 0; i
< AFFS_AC_SIZE
; i
++)
844 if (AFFS_I(inode
)->i_ac
[i
].ext
>= ext
)
845 AFFS_I(inode
)->i_ac
[i
].ext
= 0;
847 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, ext_bh
)->extension
);
849 blkcnt
= AFFS_I(inode
)->i_blkcnt
;
853 i
= last_blk
% AFFS_SB(sb
)->s_hashsize
+ 1;
856 AFFS_HEAD(ext_bh
)->first_data
= 0;
857 size
= AFFS_SB(sb
)->s_hashsize
;
858 if (size
> blkcnt
- blk
+ i
)
859 size
= blkcnt
- blk
+ i
;
860 for (; i
< size
; i
++, blk
++) {
861 affs_free_block(sb
, be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, i
)));
862 AFFS_BLOCK(sb
, ext_bh
, i
) = 0;
864 AFFS_TAIL(sb
, ext_bh
)->extension
= 0;
865 affs_fix_checksum(sb
, ext_bh
);
866 mark_buffer_dirty_inode(ext_bh
, inode
);
870 AFFS_I(inode
)->i_blkcnt
= last_blk
+ 1;
871 AFFS_I(inode
)->i_extcnt
= ext
+ 1;
872 if (AFFS_SB(sb
)->s_flags
& SF_OFS
) {
873 struct buffer_head
*bh
= affs_bread_ino(inode
, last_blk
, 0);
875 if (IS_ERR(ext_bh
)) {
876 affs_warning(sb
, "truncate", "unexpected read error for last block %u (%d)",
877 ext
, PTR_ERR(ext_bh
));
880 tmp
= be32_to_cpu(AFFS_DATA_HEAD(bh
)->next
);
881 AFFS_DATA_HEAD(bh
)->next
= 0;
882 affs_adjust_checksum(bh
, -tmp
);
886 AFFS_I(inode
)->i_blkcnt
= 0;
887 AFFS_I(inode
)->i_extcnt
= 1;
889 AFFS_I(inode
)->mmu_private
= inode
->i_size
;
893 ext_bh
= affs_bread(sb
, ext_key
);
894 size
= AFFS_SB(sb
)->s_hashsize
;
895 if (size
> blkcnt
- blk
)
897 for (i
= 0; i
< size
; i
++, blk
++)
898 affs_free_block(sb
, be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, i
)));
899 affs_free_block(sb
, ext_key
);
900 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, ext_bh
)->extension
);
903 affs_free_prealloc(inode
);