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
,
44 unsigned long end
, unsigned long where
)
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
> info
->si_blocks
)
70 phys
= bi
->i_sblock
+ block
;
72 if (phys
<= bi
->i_eblock
) {
73 dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
74 create
, (unsigned long)block
, phys
);
75 map_bh(bh_result
, sb
, phys
);
80 /* if the file is not empty and the requested block is within the range
81 of blocks allocated for this file, we can grant it */
82 if (inode
->i_size
&& phys
<= bi
->i_eblock
) {
83 dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
84 create
, (unsigned long)block
, phys
);
85 map_bh(bh_result
, sb
, phys
);
89 /* the rest has to be protected against itself */
92 /* if the last data block for this file is the last allocated
93 block, we can extend the file trivially, without moving it
95 if (bi
->i_eblock
== info
->si_lf_eblk
) {
96 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
97 create
, (unsigned long)block
, phys
);
98 map_bh(bh_result
, sb
, phys
);
99 info
->si_freeb
-= phys
- bi
->i_eblock
;
100 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
101 mark_inode_dirty(inode
);
102 mark_buffer_dirty(sbh
);
107 /* Ok, we have to move this entire file to the next free block */
108 phys
= info
->si_lf_eblk
+ 1;
109 if (bi
->i_sblock
) { /* if data starts on block 0 then there is no data */
110 err
= bfs_move_blocks(inode
->i_sb
, bi
->i_sblock
,
113 dprintf("failed to move ino=%08lx -> fs corruption\n", inode
->i_ino
);
119 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
120 create
, (unsigned long)block
, phys
);
123 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
125 /* this assumes nothing can write the inode back while we are here
126 * and thus update inode->i_blocks! (XXX)*/
127 info
->si_freeb
-= bi
->i_eblock
- bi
->i_sblock
+ 1 - inode
->i_blocks
;
128 mark_inode_dirty(inode
);
129 mark_buffer_dirty(sbh
);
130 map_bh(bh_result
, sb
, phys
);
136 static int bfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
138 return block_write_full_page(page
, bfs_get_block
, wbc
);
141 static int bfs_readpage(struct file
*file
, struct page
*page
)
143 return block_read_full_page(page
, bfs_get_block
);
146 static int bfs_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
148 return block_prepare_write(page
, from
, to
, bfs_get_block
);
151 static sector_t
bfs_bmap(struct address_space
*mapping
, sector_t block
)
153 return generic_block_bmap(mapping
, block
, bfs_get_block
);
156 struct address_space_operations bfs_aops
= {
157 .readpage
= bfs_readpage
,
158 .writepage
= bfs_writepage
,
159 .sync_page
= block_sync_page
,
160 .prepare_write
= bfs_prepare_write
,
161 .commit_write
= generic_commit_write
,
165 struct inode_operations bfs_file_inops
;