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
,
31 .aio_read
= generic_file_aio_read
,
32 .write
= do_sync_write
,
33 .aio_write
= generic_file_aio_write
,
34 .mmap
= generic_file_mmap
,
35 .open
= affs_file_open
,
36 .release
= affs_file_release
,
38 .splice_read
= generic_file_splice_read
,
41 const struct inode_operations affs_file_inode_operations
= {
42 .truncate
= affs_truncate
,
43 .setattr
= affs_notify_change
,
47 affs_file_open(struct inode
*inode
, struct file
*filp
)
49 if (atomic_read(&filp
->f_count
) != 1)
51 pr_debug("AFFS: open(%lu,%d)\n",
52 inode
->i_ino
, atomic_read(&AFFS_I(inode
)->i_opencnt
));
53 atomic_inc(&AFFS_I(inode
)->i_opencnt
);
58 affs_file_release(struct inode
*inode
, struct file
*filp
)
60 if (atomic_read(&filp
->f_count
) != 0)
62 pr_debug("AFFS: release(%lu, %d)\n",
63 inode
->i_ino
, atomic_read(&AFFS_I(inode
)->i_opencnt
));
65 if (atomic_dec_and_test(&AFFS_I(inode
)->i_opencnt
)) {
66 mutex_lock(&inode
->i_mutex
);
67 if (inode
->i_size
!= AFFS_I(inode
)->mmu_private
)
69 affs_free_prealloc(inode
);
70 mutex_unlock(&inode
->i_mutex
);
77 affs_grow_extcache(struct inode
*inode
, u32 lc_idx
)
79 struct super_block
*sb
= inode
->i_sb
;
80 struct buffer_head
*bh
;
84 if (!AFFS_I(inode
)->i_lc
) {
85 char *ptr
= (char *)get_zeroed_page(GFP_NOFS
);
88 AFFS_I(inode
)->i_lc
= (u32
*)ptr
;
89 AFFS_I(inode
)->i_ac
= (struct affs_ext_key
*)(ptr
+ AFFS_CACHE_SIZE
/ 2);
92 lc_max
= AFFS_LC_SIZE
<< AFFS_I(inode
)->i_lc_shift
;
94 if (AFFS_I(inode
)->i_extcnt
> lc_max
) {
95 u32 lc_shift
, lc_mask
, tmp
, off
;
97 /* need to recalculate linear cache, start from old size */
98 lc_shift
= AFFS_I(inode
)->i_lc_shift
;
99 tmp
= (AFFS_I(inode
)->i_extcnt
/ AFFS_LC_SIZE
) >> lc_shift
;
100 for (; tmp
; tmp
>>= 1)
102 lc_mask
= (1 << lc_shift
) - 1;
104 /* fix idx and old size to new shift */
105 lc_idx
>>= (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
106 AFFS_I(inode
)->i_lc_size
>>= (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
108 /* first shrink old cache to make more space */
109 off
= 1 << (lc_shift
- AFFS_I(inode
)->i_lc_shift
);
110 for (i
= 1, j
= off
; j
< AFFS_LC_SIZE
; i
++, j
+= off
)
111 AFFS_I(inode
)->i_ac
[i
] = AFFS_I(inode
)->i_ac
[j
];
113 AFFS_I(inode
)->i_lc_shift
= lc_shift
;
114 AFFS_I(inode
)->i_lc_mask
= lc_mask
;
117 /* fill cache to the needed index */
118 i
= AFFS_I(inode
)->i_lc_size
;
119 AFFS_I(inode
)->i_lc_size
= lc_idx
+ 1;
120 for (; i
<= lc_idx
; i
++) {
122 AFFS_I(inode
)->i_lc
[0] = inode
->i_ino
;
125 key
= AFFS_I(inode
)->i_lc
[i
- 1];
126 j
= AFFS_I(inode
)->i_lc_mask
+ 1;
129 bh
= affs_bread(sb
, key
);
132 key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
136 AFFS_I(inode
)->i_lc
[i
] = key
;
146 static struct buffer_head
*
147 affs_alloc_extblock(struct inode
*inode
, struct buffer_head
*bh
, u32 ext
)
149 struct super_block
*sb
= inode
->i_sb
;
150 struct buffer_head
*new_bh
;
153 blocknr
= affs_alloc_block(inode
, bh
->b_blocknr
);
155 return ERR_PTR(-ENOSPC
);
157 new_bh
= affs_getzeroblk(sb
, blocknr
);
159 affs_free_block(sb
, blocknr
);
160 return ERR_PTR(-EIO
);
163 AFFS_HEAD(new_bh
)->ptype
= cpu_to_be32(T_LIST
);
164 AFFS_HEAD(new_bh
)->key
= cpu_to_be32(blocknr
);
165 AFFS_TAIL(sb
, new_bh
)->stype
= cpu_to_be32(ST_FILE
);
166 AFFS_TAIL(sb
, new_bh
)->parent
= cpu_to_be32(inode
->i_ino
);
167 affs_fix_checksum(sb
, new_bh
);
169 mark_buffer_dirty_inode(new_bh
, inode
);
171 tmp
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
173 affs_warning(sb
, "alloc_ext", "previous extension set (%x)", tmp
);
174 AFFS_TAIL(sb
, bh
)->extension
= cpu_to_be32(blocknr
);
175 affs_adjust_checksum(bh
, blocknr
- tmp
);
176 mark_buffer_dirty_inode(bh
, inode
);
178 AFFS_I(inode
)->i_extcnt
++;
179 mark_inode_dirty(inode
);
184 static inline struct buffer_head
*
185 affs_get_extblock(struct inode
*inode
, u32 ext
)
187 /* inline the simplest case: same extended block as last time */
188 struct buffer_head
*bh
= AFFS_I(inode
)->i_ext_bh
;
189 if (ext
== AFFS_I(inode
)->i_ext_last
)
192 /* we have to do more (not inlined) */
193 bh
= affs_get_extblock_slow(inode
, ext
);
198 static struct buffer_head
*
199 affs_get_extblock_slow(struct inode
*inode
, u32 ext
)
201 struct super_block
*sb
= inode
->i_sb
;
202 struct buffer_head
*bh
;
204 u32 lc_idx
, lc_off
, ac_idx
;
207 if (ext
== AFFS_I(inode
)->i_ext_last
+ 1) {
208 /* read the next extended block from the current one */
209 bh
= AFFS_I(inode
)->i_ext_bh
;
210 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
211 if (ext
< AFFS_I(inode
)->i_extcnt
)
213 if (ext
> AFFS_I(inode
)->i_extcnt
)
215 bh
= affs_alloc_extblock(inode
, bh
, ext
);
222 /* we seek back to the file header block */
223 ext_key
= inode
->i_ino
;
227 if (ext
>= AFFS_I(inode
)->i_extcnt
) {
228 struct buffer_head
*prev_bh
;
230 /* allocate a new extended block */
231 if (ext
> AFFS_I(inode
)->i_extcnt
)
234 /* get previous extended block */
235 prev_bh
= affs_get_extblock(inode
, ext
- 1);
238 bh
= affs_alloc_extblock(inode
, prev_bh
, ext
);
239 affs_brelse(prev_bh
);
246 /* check if there is an extended cache and whether it's large enough */
247 lc_idx
= ext
>> AFFS_I(inode
)->i_lc_shift
;
248 lc_off
= ext
& AFFS_I(inode
)->i_lc_mask
;
250 if (lc_idx
>= AFFS_I(inode
)->i_lc_size
) {
253 err
= affs_grow_extcache(inode
, lc_idx
);
259 /* every n'th key we find in the linear cache */
261 ext_key
= AFFS_I(inode
)->i_lc
[lc_idx
];
265 /* maybe it's still in the associative cache */
266 ac_idx
= (ext
- lc_idx
- 1) & AFFS_AC_MASK
;
267 if (AFFS_I(inode
)->i_ac
[ac_idx
].ext
== ext
) {
268 ext_key
= AFFS_I(inode
)->i_ac
[ac_idx
].key
;
272 /* try to find one of the previous extended blocks */
275 while (--tmp
, --lc_off
> 0) {
276 idx
= (idx
- 1) & AFFS_AC_MASK
;
277 if (AFFS_I(inode
)->i_ac
[idx
].ext
== tmp
) {
278 ext_key
= AFFS_I(inode
)->i_ac
[idx
].key
;
283 /* fall back to the linear cache */
284 ext_key
= AFFS_I(inode
)->i_lc
[lc_idx
];
286 /* read all extended blocks until we find the one we need */
289 bh
= affs_bread(sb
, ext_key
);
292 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, bh
)->extension
);
298 /* store it in the associative cache */
299 // recalculate ac_idx?
300 AFFS_I(inode
)->i_ac
[ac_idx
].ext
= ext
;
301 AFFS_I(inode
)->i_ac
[ac_idx
].key
= ext_key
;
304 /* finally read the right extended block */
306 bh
= affs_bread(sb
, ext_key
);
312 /* release old cached extended block and store the new one */
313 affs_brelse(AFFS_I(inode
)->i_ext_bh
);
314 AFFS_I(inode
)->i_ext_last
= ext
;
315 AFFS_I(inode
)->i_ext_bh
= bh
;
322 return ERR_PTR(-EIO
);
326 affs_get_block(struct inode
*inode
, sector_t block
, struct buffer_head
*bh_result
, int create
)
328 struct super_block
*sb
= inode
->i_sb
;
329 struct buffer_head
*ext_bh
;
332 pr_debug("AFFS: get_block(%u, %lu)\n", (u32
)inode
->i_ino
, (unsigned long)block
);
334 BUG_ON(block
> (sector_t
)0x7fffffffUL
);
336 if (block
>= AFFS_I(inode
)->i_blkcnt
) {
337 if (block
> AFFS_I(inode
)->i_blkcnt
|| !create
)
343 affs_lock_ext(inode
);
345 ext
= (u32
)block
/ AFFS_SB(sb
)->s_hashsize
;
346 block
-= ext
* AFFS_SB(sb
)->s_hashsize
;
347 ext_bh
= affs_get_extblock(inode
, ext
);
350 map_bh(bh_result
, sb
, (sector_t
)be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, block
)));
353 u32 blocknr
= affs_alloc_block(inode
, ext_bh
->b_blocknr
);
356 set_buffer_new(bh_result
);
357 AFFS_I(inode
)->mmu_private
+= AFFS_SB(sb
)->s_data_blksize
;
358 AFFS_I(inode
)->i_blkcnt
++;
360 /* store new block */
361 if (bh_result
->b_blocknr
)
362 affs_warning(sb
, "get_block", "block already set (%x)", bh_result
->b_blocknr
);
363 AFFS_BLOCK(sb
, ext_bh
, block
) = cpu_to_be32(blocknr
);
364 AFFS_HEAD(ext_bh
)->block_count
= cpu_to_be32(block
+ 1);
365 affs_adjust_checksum(ext_bh
, blocknr
- bh_result
->b_blocknr
+ 1);
366 bh_result
->b_blocknr
= blocknr
;
369 /* insert first block into header block */
370 u32 tmp
= be32_to_cpu(AFFS_HEAD(ext_bh
)->first_data
);
372 affs_warning(sb
, "get_block", "first block already set (%d)", tmp
);
373 AFFS_HEAD(ext_bh
)->first_data
= cpu_to_be32(blocknr
);
374 affs_adjust_checksum(ext_bh
, blocknr
- tmp
);
380 affs_unlock_ext(inode
);
384 affs_error(inode
->i_sb
,"get_block","strange block request %d", block
);
388 affs_unlock_ext(inode
);
389 return PTR_ERR(ext_bh
);
392 clear_buffer_mapped(bh_result
);
393 bh_result
->b_bdev
= NULL
;
395 affs_unlock_ext(inode
);
399 static int affs_writepage(struct page
*page
, struct writeback_control
*wbc
)
401 return block_write_full_page(page
, affs_get_block
, wbc
);
404 static int affs_readpage(struct file
*file
, struct page
*page
)
406 return block_read_full_page(page
, affs_get_block
);
409 static int affs_write_begin(struct file
*file
, struct address_space
*mapping
,
410 loff_t pos
, unsigned len
, unsigned flags
,
411 struct page
**pagep
, void **fsdata
)
414 return cont_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
416 &AFFS_I(mapping
->host
)->mmu_private
);
419 static sector_t
_affs_bmap(struct address_space
*mapping
, sector_t block
)
421 return generic_block_bmap(mapping
,block
,affs_get_block
);
424 const struct address_space_operations affs_aops
= {
425 .readpage
= affs_readpage
,
426 .writepage
= affs_writepage
,
427 .sync_page
= block_sync_page
,
428 .write_begin
= affs_write_begin
,
429 .write_end
= generic_write_end
,
433 static inline struct buffer_head
*
434 affs_bread_ino(struct inode
*inode
, int block
, int create
)
436 struct buffer_head
*bh
, tmp_bh
;
440 err
= affs_get_block(inode
, block
, &tmp_bh
, create
);
442 bh
= affs_bread(inode
->i_sb
, tmp_bh
.b_blocknr
);
444 bh
->b_state
|= tmp_bh
.b_state
;
452 static inline struct buffer_head
*
453 affs_getzeroblk_ino(struct inode
*inode
, int block
)
455 struct buffer_head
*bh
, tmp_bh
;
459 err
= affs_get_block(inode
, block
, &tmp_bh
, 1);
461 bh
= affs_getzeroblk(inode
->i_sb
, tmp_bh
.b_blocknr
);
463 bh
->b_state
|= tmp_bh
.b_state
;
471 static inline struct buffer_head
*
472 affs_getemptyblk_ino(struct inode
*inode
, int block
)
474 struct buffer_head
*bh
, tmp_bh
;
478 err
= affs_get_block(inode
, block
, &tmp_bh
, 1);
480 bh
= affs_getemptyblk(inode
->i_sb
, tmp_bh
.b_blocknr
);
482 bh
->b_state
|= tmp_bh
.b_state
;
491 affs_do_readpage_ofs(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
493 struct inode
*inode
= page
->mapping
->host
;
494 struct super_block
*sb
= inode
->i_sb
;
495 struct buffer_head
*bh
;
497 u32 bidx
, boff
, bsize
;
500 pr_debug("AFFS: read_page(%u, %ld, %d, %d)\n", (u32
)inode
->i_ino
, page
->index
, from
, to
);
501 BUG_ON(from
> to
|| to
> PAGE_CACHE_SIZE
);
503 data
= page_address(page
);
504 bsize
= AFFS_SB(sb
)->s_data_blksize
;
505 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
510 bh
= affs_bread_ino(inode
, bidx
, 0);
513 tmp
= min(bsize
- boff
, to
- from
);
514 BUG_ON(from
+ tmp
> to
|| tmp
> bsize
);
515 memcpy(data
+ from
, AFFS_DATA(bh
) + boff
, tmp
);
521 flush_dcache_page(page
);
527 affs_extent_file_ofs(struct inode
*inode
, u32 newsize
)
529 struct super_block
*sb
= inode
->i_sb
;
530 struct buffer_head
*bh
, *prev_bh
;
535 pr_debug("AFFS: extent_file(%u, %d)\n", (u32
)inode
->i_ino
, newsize
);
536 bsize
= AFFS_SB(sb
)->s_data_blksize
;
538 size
= AFFS_I(inode
)->mmu_private
;
542 bh
= affs_bread_ino(inode
, bidx
, 0);
545 tmp
= min(bsize
- boff
, newsize
- size
);
546 BUG_ON(boff
+ tmp
> bsize
|| tmp
> bsize
);
547 memset(AFFS_DATA(bh
) + boff
, 0, tmp
);
548 be32_add_cpu(&AFFS_DATA_HEAD(bh
)->size
, tmp
);
549 affs_fix_checksum(sb
, bh
);
550 mark_buffer_dirty_inode(bh
, inode
);
554 bh
= affs_bread_ino(inode
, bidx
- 1, 0);
559 while (size
< newsize
) {
561 bh
= affs_getzeroblk_ino(inode
, bidx
);
564 tmp
= min(bsize
, newsize
- size
);
566 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
567 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
568 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
569 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(tmp
);
570 affs_fix_checksum(sb
, bh
);
571 bh
->b_state
&= ~(1UL << BH_New
);
572 mark_buffer_dirty_inode(bh
, inode
);
574 u32 tmp
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
576 affs_warning(sb
, "extent_file_ofs", "next block already set for %d (%d)", bidx
, tmp
);
577 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
578 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp
);
579 mark_buffer_dirty_inode(prev_bh
, inode
);
580 affs_brelse(prev_bh
);
586 inode
->i_size
= AFFS_I(inode
)->mmu_private
= newsize
;
590 inode
->i_size
= AFFS_I(inode
)->mmu_private
= newsize
;
595 affs_readpage_ofs(struct file
*file
, struct page
*page
)
597 struct inode
*inode
= page
->mapping
->host
;
601 pr_debug("AFFS: read_page(%u, %ld)\n", (u32
)inode
->i_ino
, page
->index
);
602 to
= PAGE_CACHE_SIZE
;
603 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > inode
->i_size
) {
604 to
= inode
->i_size
& ~PAGE_CACHE_MASK
;
605 memset(page_address(page
) + to
, 0, PAGE_CACHE_SIZE
- to
);
608 err
= affs_do_readpage_ofs(file
, page
, 0, to
);
610 SetPageUptodate(page
);
615 static int affs_write_begin_ofs(struct file
*file
, struct address_space
*mapping
,
616 loff_t pos
, unsigned len
, unsigned flags
,
617 struct page
**pagep
, void **fsdata
)
619 struct inode
*inode
= mapping
->host
;
624 pr_debug("AFFS: write_begin(%u, %llu, %llu)\n", (u32
)inode
->i_ino
, (unsigned long long)pos
, (unsigned long long)pos
+ len
);
625 if (pos
> AFFS_I(inode
)->mmu_private
) {
626 /* XXX: this probably leaves a too-big i_size in case of
627 * failure. Should really be updating i_size at write_end time
629 err
= affs_extent_file_ofs(inode
, pos
);
634 index
= pos
>> PAGE_CACHE_SHIFT
;
635 page
= __grab_cache_page(mapping
, index
);
640 if (PageUptodate(page
))
643 /* XXX: inefficient but safe in the face of short writes */
644 err
= affs_do_readpage_ofs(file
, page
, 0, PAGE_CACHE_SIZE
);
647 page_cache_release(page
);
652 static int affs_write_end_ofs(struct file
*file
, struct address_space
*mapping
,
653 loff_t pos
, unsigned len
, unsigned copied
,
654 struct page
*page
, void *fsdata
)
656 struct inode
*inode
= mapping
->host
;
657 struct super_block
*sb
= inode
->i_sb
;
658 struct buffer_head
*bh
, *prev_bh
;
660 u32 bidx
, boff
, bsize
;
665 from
= pos
& (PAGE_CACHE_SIZE
- 1);
668 * XXX: not sure if this can handle short copies (len < copied), but
669 * we don't have to, because the page should always be uptodate here,
670 * due to write_begin.
673 pr_debug("AFFS: write_begin(%u, %llu, %llu)\n", (u32
)inode
->i_ino
, (unsigned long long)pos
, (unsigned long long)pos
+ len
);
674 bsize
= AFFS_SB(sb
)->s_data_blksize
;
675 data
= page_address(page
);
679 tmp
= (page
->index
<< PAGE_CACHE_SHIFT
) + from
;
683 bh
= affs_bread_ino(inode
, bidx
, 0);
686 tmp
= min(bsize
- boff
, to
- from
);
687 BUG_ON(boff
+ tmp
> bsize
|| tmp
> bsize
);
688 memcpy(AFFS_DATA(bh
) + boff
, data
+ from
, tmp
);
689 be32_add_cpu(&AFFS_DATA_HEAD(bh
)->size
, tmp
);
690 affs_fix_checksum(sb
, bh
);
691 mark_buffer_dirty_inode(bh
, inode
);
696 bh
= affs_bread_ino(inode
, bidx
- 1, 0);
700 while (from
+ bsize
<= to
) {
702 bh
= affs_getemptyblk_ino(inode
, bidx
);
705 memcpy(AFFS_DATA(bh
), data
+ from
, bsize
);
706 if (buffer_new(bh
)) {
707 AFFS_DATA_HEAD(bh
)->ptype
= cpu_to_be32(T_DATA
);
708 AFFS_DATA_HEAD(bh
)->key
= cpu_to_be32(inode
->i_ino
);
709 AFFS_DATA_HEAD(bh
)->sequence
= cpu_to_be32(bidx
);
710 AFFS_DATA_HEAD(bh
)->size
= cpu_to_be32(bsize
);
711 AFFS_DATA_HEAD(bh
)->next
= 0;
712 bh
->b_state
&= ~(1UL << BH_New
);
714 u32 tmp
= be32_to_cpu(AFFS_DATA_HEAD(prev_bh
)->next
);
716 affs_warning(sb
, "commit_write_ofs", "next block already set for %d (%d)", bidx
, tmp
);
717 AFFS_DATA_HEAD(prev_bh
)->next
= cpu_to_be32(bh
->b_blocknr
);
718 affs_adjust_checksum(prev_bh
, bh
->b_blocknr
- tmp
);
719 mark_buffer_dirty_inode(prev_bh
, inode
);
722 affs_brelse(prev_bh
);
723 affs_fix_checksum(sb
, bh
);
724 mark_buffer_dirty_inode(bh
, inode
);
731 bh
= affs_bread_ino(inode
, bidx
, 1);
734 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
;
770 page_cache_release(page
);
777 written
= PTR_ERR(bh
);
781 const struct address_space_operations affs_aops_ofs
= {
782 .readpage
= affs_readpage_ofs
,
783 //.writepage = affs_writepage_ofs,
784 //.sync_page = affs_sync_page_ofs,
785 .write_begin
= affs_write_begin_ofs
,
786 .write_end
= affs_write_end_ofs
789 /* Free any preallocated blocks. */
792 affs_free_prealloc(struct inode
*inode
)
794 struct super_block
*sb
= inode
->i_sb
;
796 pr_debug("AFFS: free_prealloc(ino=%lu)\n", inode
->i_ino
);
798 while (AFFS_I(inode
)->i_pa_cnt
) {
799 AFFS_I(inode
)->i_pa_cnt
--;
800 affs_free_block(sb
, ++AFFS_I(inode
)->i_lastalloc
);
804 /* Truncate (or enlarge) a file to the requested size. */
807 affs_truncate(struct inode
*inode
)
809 struct super_block
*sb
= inode
->i_sb
;
811 u32 last_blk
, blkcnt
, blk
;
813 struct buffer_head
*ext_bh
;
816 pr_debug("AFFS: truncate(inode=%d, oldsize=%u, newsize=%u)\n",
817 (u32
)inode
->i_ino
, (u32
)AFFS_I(inode
)->mmu_private
, (u32
)inode
->i_size
);
822 last_blk
= ((u32
)inode
->i_size
- 1) / AFFS_SB(sb
)->s_data_blksize
;
823 ext
= last_blk
/ AFFS_SB(sb
)->s_hashsize
;
826 if (inode
->i_size
> AFFS_I(inode
)->mmu_private
) {
827 struct address_space
*mapping
= inode
->i_mapping
;
830 u32 size
= inode
->i_size
;
833 res
= mapping
->a_ops
->write_begin(NULL
, mapping
, size
, 0, 0, &page
, &fsdata
);
835 res
= mapping
->a_ops
->write_end(NULL
, mapping
, size
, 0, 0, page
, fsdata
);
837 inode
->i_size
= AFFS_I(inode
)->mmu_private
;
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 AFFS_HEAD(ext_bh
)->block_count
= cpu_to_be32(i
);
874 size
= AFFS_SB(sb
)->s_hashsize
;
875 if (size
> blkcnt
- blk
+ i
)
876 size
= blkcnt
- blk
+ i
;
877 for (; i
< size
; i
++, blk
++) {
878 affs_free_block(sb
, be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, i
)));
879 AFFS_BLOCK(sb
, ext_bh
, i
) = 0;
881 AFFS_TAIL(sb
, ext_bh
)->extension
= 0;
882 affs_fix_checksum(sb
, ext_bh
);
883 mark_buffer_dirty_inode(ext_bh
, inode
);
887 AFFS_I(inode
)->i_blkcnt
= last_blk
+ 1;
888 AFFS_I(inode
)->i_extcnt
= ext
+ 1;
889 if (AFFS_SB(sb
)->s_flags
& SF_OFS
) {
890 struct buffer_head
*bh
= affs_bread_ino(inode
, last_blk
, 0);
892 if (IS_ERR(ext_bh
)) {
893 affs_warning(sb
, "truncate", "unexpected read error for last block %u (%d)",
894 ext
, PTR_ERR(ext_bh
));
897 tmp
= be32_to_cpu(AFFS_DATA_HEAD(bh
)->next
);
898 AFFS_DATA_HEAD(bh
)->next
= 0;
899 affs_adjust_checksum(bh
, -tmp
);
903 AFFS_I(inode
)->i_blkcnt
= 0;
904 AFFS_I(inode
)->i_extcnt
= 1;
906 AFFS_I(inode
)->mmu_private
= inode
->i_size
;
910 ext_bh
= affs_bread(sb
, ext_key
);
911 size
= AFFS_SB(sb
)->s_hashsize
;
912 if (size
> blkcnt
- blk
)
914 for (i
= 0; i
< size
; i
++, blk
++)
915 affs_free_block(sb
, be32_to_cpu(AFFS_BLOCK(sb
, ext_bh
, i
)));
916 affs_free_block(sb
, ext_key
);
917 ext_key
= be32_to_cpu(AFFS_TAIL(sb
, ext_bh
)->extension
);
920 affs_free_prealloc(inode
);