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_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
150 return block_prepare_write(page, from, to, bfs_get_block);
153 static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
155 return generic_block_bmap(mapping, block, bfs_get_block);
158 const struct address_space_operations bfs_aops = {
159 .readpage = bfs_readpage,
160 .writepage = bfs_writepage,
161 .sync_page = block_sync_page,
162 .prepare_write = bfs_prepare_write,
163 .commit_write = generic_commit_write,
167 const struct inode_operations bfs_file_inops;