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 const struct file_operations bfs_file_operations
= {
21 .llseek
= generic_file_llseek
,
23 .aio_read
= generic_file_aio_read
,
24 .write
= do_sync_write
,
25 .aio_write
= generic_file_aio_write
,
26 .mmap
= generic_file_mmap
,
27 .splice_read
= generic_file_splice_read
,
30 static int bfs_move_block(unsigned long from
, unsigned long to
, struct super_block
*sb
)
32 struct buffer_head
*bh
, *new;
34 bh
= sb_bread(sb
, from
);
37 new = sb_getblk(sb
, to
);
38 memcpy(new->b_data
, bh
->b_data
, bh
->b_size
);
39 mark_buffer_dirty(new);
45 static int bfs_move_blocks(struct super_block
*sb
, unsigned long start
,
46 unsigned long end
, unsigned long where
)
50 dprintf("%08lx-%08lx->%08lx\n", start
, end
, where
);
51 for (i
= start
; i
<= end
; i
++)
52 if(bfs_move_block(i
, where
+ i
, sb
)) {
53 dprintf("failed to move block %08lx -> %08lx\n", i
, where
+ i
);
59 static int bfs_get_block(struct inode
* inode
, sector_t block
,
60 struct buffer_head
* bh_result
, int create
)
64 struct super_block
*sb
= inode
->i_sb
;
65 struct bfs_sb_info
*info
= BFS_SB(sb
);
66 struct bfs_inode_info
*bi
= BFS_I(inode
);
67 struct buffer_head
*sbh
= info
->si_sbh
;
69 if (block
> info
->si_blocks
)
72 phys
= bi
->i_sblock
+ block
;
74 if (phys
<= bi
->i_eblock
) {
75 dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
76 create
, (unsigned long)block
, phys
);
77 map_bh(bh_result
, sb
, phys
);
82 /* if the file is not empty and the requested block is within the range
83 of blocks allocated for this file, we can grant it */
84 if (inode
->i_size
&& phys
<= bi
->i_eblock
) {
85 dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
86 create
, (unsigned long)block
, phys
);
87 map_bh(bh_result
, sb
, phys
);
91 /* the rest has to be protected against itself */
94 /* if the last data block for this file is the last allocated
95 block, we can extend the file trivially, without moving it
97 if (bi
->i_eblock
== info
->si_lf_eblk
) {
98 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
99 create
, (unsigned long)block
, phys
);
100 map_bh(bh_result
, sb
, phys
);
101 info
->si_freeb
-= phys
- bi
->i_eblock
;
102 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
103 mark_inode_dirty(inode
);
104 mark_buffer_dirty(sbh
);
109 /* Ok, we have to move this entire file to the next free block */
110 phys
= info
->si_lf_eblk
+ 1;
111 if (bi
->i_sblock
) { /* if data starts on block 0 then there is no data */
112 err
= bfs_move_blocks(inode
->i_sb
, bi
->i_sblock
,
115 dprintf("failed to move ino=%08lx -> fs corruption\n", inode
->i_ino
);
121 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
122 create
, (unsigned long)block
, phys
);
125 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
127 /* this assumes nothing can write the inode back while we are here
128 * and thus update inode->i_blocks! (XXX)*/
129 info
->si_freeb
-= bi
->i_eblock
- bi
->i_sblock
+ 1 - inode
->i_blocks
;
130 mark_inode_dirty(inode
);
131 mark_buffer_dirty(sbh
);
132 map_bh(bh_result
, sb
, phys
);
138 static int bfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
140 return block_write_full_page(page
, bfs_get_block
, wbc
);
143 static int bfs_readpage(struct file
*file
, struct page
*page
)
145 return block_read_full_page(page
, bfs_get_block
);
148 static int bfs_write_begin(struct file
*file
, struct address_space
*mapping
,
149 loff_t pos
, unsigned len
, unsigned flags
,
150 struct page
**pagep
, void **fsdata
)
153 return block_write_begin(file
, mapping
, pos
, len
, flags
,
154 pagep
, fsdata
, bfs_get_block
);
157 static sector_t
bfs_bmap(struct address_space
*mapping
, sector_t block
)
159 return generic_block_bmap(mapping
, block
, bfs_get_block
);
162 const struct address_space_operations bfs_aops
= {
163 .readpage
= bfs_readpage
,
164 .writepage
= bfs_writepage
,
165 .sync_page
= block_sync_page
,
166 .write_begin
= bfs_write_begin
,
167 .write_end
= generic_write_end
,
171 const struct inode_operations bfs_file_inops
;