4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
6 * Make the file block allocation algorithm understand the size
7 * of the underlying block device.
8 * Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
13 #include <linux/buffer_head.h>
19 #define dprintf(x...) printf(x)
24 const struct file_operations bfs_file_operations
= {
25 .llseek
= generic_file_llseek
,
27 .aio_read
= generic_file_aio_read
,
28 .write
= do_sync_write
,
29 .aio_write
= generic_file_aio_write
,
30 .mmap
= generic_file_mmap
,
31 .splice_read
= generic_file_splice_read
,
34 static int bfs_move_block(unsigned long from
, unsigned long to
,
35 struct super_block
*sb
)
37 struct buffer_head
*bh
, *new;
39 bh
= sb_bread(sb
, from
);
42 new = sb_getblk(sb
, to
);
43 memcpy(new->b_data
, bh
->b_data
, bh
->b_size
);
44 mark_buffer_dirty(new);
50 static int bfs_move_blocks(struct super_block
*sb
, unsigned long start
,
51 unsigned long end
, unsigned long where
)
55 dprintf("%08lx-%08lx->%08lx\n", start
, end
, where
);
56 for (i
= start
; i
<= end
; i
++)
57 if(bfs_move_block(i
, where
+ i
, sb
)) {
58 dprintf("failed to move block %08lx -> %08lx\n", i
,
65 static int bfs_get_block(struct inode
*inode
, sector_t block
,
66 struct buffer_head
*bh_result
, int create
)
70 struct super_block
*sb
= inode
->i_sb
;
71 struct bfs_sb_info
*info
= BFS_SB(sb
);
72 struct bfs_inode_info
*bi
= BFS_I(inode
);
73 struct buffer_head
*sbh
= info
->si_sbh
;
75 phys
= bi
->i_sblock
+ block
;
77 if (phys
<= bi
->i_eblock
) {
78 dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
79 create
, (unsigned long)block
, phys
);
80 map_bh(bh_result
, sb
, phys
);
86 * If the file is not empty and the requested block is within the
87 * range of blocks allocated for this file, we can grant it.
89 if (bi
->i_sblock
&& (phys
<= bi
->i_eblock
)) {
90 dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
91 create
, (unsigned long)block
, phys
);
92 map_bh(bh_result
, sb
, phys
);
96 /* The file will be extended, so let's see if there is enough space. */
97 if (phys
>= info
->si_blocks
)
100 /* The rest has to be protected against itself. */
101 mutex_lock(&info
->bfs_lock
);
104 * If the last data block for this file is the last allocated
105 * block, we can extend the file trivially, without moving it
108 if (bi
->i_eblock
== info
->si_lf_eblk
) {
109 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
110 create
, (unsigned long)block
, phys
);
111 map_bh(bh_result
, sb
, phys
);
112 info
->si_freeb
-= phys
- bi
->i_eblock
;
113 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
114 mark_inode_dirty(inode
);
115 mark_buffer_dirty(sbh
);
120 /* Ok, we have to move this entire file to the next free block. */
121 phys
= info
->si_lf_eblk
+ 1;
122 if (phys
+ block
>= info
->si_blocks
) {
128 err
= bfs_move_blocks(inode
->i_sb
, bi
->i_sblock
,
131 dprintf("failed to move ino=%08lx -> fs corruption\n",
138 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
139 create
, (unsigned long)block
, phys
);
142 info
->si_lf_eblk
= bi
->i_eblock
= phys
;
145 * This assumes nothing can write the inode back while we are here
146 * and thus update inode->i_blocks! (XXX)
148 info
->si_freeb
-= bi
->i_eblock
- bi
->i_sblock
+ 1 - inode
->i_blocks
;
149 mark_inode_dirty(inode
);
150 mark_buffer_dirty(sbh
);
151 map_bh(bh_result
, sb
, phys
);
153 mutex_unlock(&info
->bfs_lock
);
157 static int bfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
159 return block_write_full_page(page
, bfs_get_block
, wbc
);
162 static int bfs_readpage(struct file
*file
, struct page
*page
)
164 return block_read_full_page(page
, bfs_get_block
);
167 static int bfs_write_begin(struct file
*file
, struct address_space
*mapping
,
168 loff_t pos
, unsigned len
, unsigned flags
,
169 struct page
**pagep
, void **fsdata
)
172 return block_write_begin(file
, mapping
, pos
, len
, flags
,
173 pagep
, fsdata
, bfs_get_block
);
176 static sector_t
bfs_bmap(struct address_space
*mapping
, sector_t block
)
178 return generic_block_bmap(mapping
, block
, bfs_get_block
);
181 const struct address_space_operations bfs_aops
= {
182 .readpage
= bfs_readpage
,
183 .writepage
= bfs_writepage
,
184 .sync_page
= block_sync_page
,
185 .write_begin
= bfs_write_begin
,
186 .write_end
= generic_write_end
,
190 const struct inode_operations bfs_file_inops
;