4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
8 #include <linux/buffer_head.h>
9 #include <linux/smp_lock.h>
15 #define dprintf(x...) printf(x)
20 struct file_operations bfs_file_operations
= {
21 .llseek
= generic_file_llseek
,
22 .read
= generic_file_read
,
23 .write
= generic_file_write
,
24 .mmap
= generic_file_mmap
,
25 .sendfile
= generic_file_sendfile
,
28 static int bfs_move_block(unsigned long from
, unsigned long to
, struct super_block
*sb
)
30 struct buffer_head
*bh
, *new;
32 bh
= sb_bread(sb
, from
);
35 new = sb_getblk(sb
, to
);
36 memcpy(new->b_data
, bh
->b_data
, bh
->b_size
);
37 mark_buffer_dirty(new);
43 static int bfs_move_blocks(struct super_block
*sb
, unsigned long start
, unsigned long end
,
48 dprintf("%08lx-%08lx->%08lx\n", start
, end
, where
);
49 for (i
= start
; i
<= end
; i
++)
50 if(bfs_move_block(i
, where
+ i
, sb
)) {
51 dprintf("failed to move block %08lx -> %08lx\n", i
, where
+ i
);
57 static int bfs_get_block(struct inode
* inode
, sector_t block
,
58 struct buffer_head
* bh_result
, int create
)
62 struct super_block
*sb
= inode
->i_sb
;
63 struct bfs_sb_info
*info
= BFS_SB(sb
);
64 struct bfs_inode_info
*bi
= BFS_I(inode
);
65 struct buffer_head
*sbh
= info
->si_sbh
;
67 if (block
< 0 || block
> info
->si_blocks
)
70 phys
= bi
->i_sblock
+ block
;
72 if (phys
<= bi
->i_eblock
) {
73 dprintf("c=%d, b=%08lx, phys=%08lx (granted)\n", create
, block
, phys
);
74 map_bh(bh_result
, sb
, phys
);
79 /* if the file is not empty and the requested block is within the range
80 of blocks allocated for this file, we can grant it */
81 if (inode
->i_size
&& phys
<= bi
->i_eblock
) {
82 dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
84 map_bh(bh_result
, sb
, phys
);
88 /* the rest has to be protected against itself */
91 /* if the last data block for this file is the last allocated block, we can
92 extend the file trivially, without moving it anywhere */
93 if (bi
->i_eblock
== info
->si_lf_eblk
) {
94 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
96 map_bh(bh_result
, sb
, phys
);
97 info
->si_freeb
-= phys
- bi
->i_eblock
;
98 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
99 mark_inode_dirty(inode
);
100 mark_buffer_dirty(sbh
);
105 /* Ok, we have to move this entire file to the next free block */
106 phys
= info
->si_lf_eblk
+ 1;
107 if (bi
->i_sblock
) { /* if data starts on block 0 then there is no data */
108 err
= bfs_move_blocks(inode
->i_sb
, bi
->i_sblock
,
111 dprintf("failed to move ino=%08lx -> fs corruption\n", inode
->i_ino
);
117 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n", create
, block
, phys
);
120 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
122 /* this assumes nothing can write the inode back while we are here
123 * and thus update inode->i_blocks! (XXX)*/
124 info
->si_freeb
-= bi
->i_eblock
- bi
->i_sblock
+ 1 - inode
->i_blocks
;
125 mark_inode_dirty(inode
);
126 mark_buffer_dirty(sbh
);
127 map_bh(bh_result
, sb
, phys
);
133 static int bfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
135 return block_write_full_page(page
, bfs_get_block
, wbc
);
138 static int bfs_readpage(struct file
*file
, struct page
*page
)
140 return block_read_full_page(page
, bfs_get_block
);
143 static int bfs_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
145 return block_prepare_write(page
, from
, to
, bfs_get_block
);
148 static sector_t
bfs_bmap(struct address_space
*mapping
, sector_t block
)
150 return generic_block_bmap(mapping
, block
, bfs_get_block
);
153 struct address_space_operations bfs_aops
= {
154 .readpage
= bfs_readpage
,
155 .writepage
= bfs_writepage
,
156 .sync_page
= block_sync_page
,
157 .prepare_write
= bfs_prepare_write
,
158 .commit_write
= generic_commit_write
,
162 struct inode_operations bfs_file_inops
;