[PATCH] fs/ext2/bitmap.c: ext2_count_free() is only required #ifdef EXT2FS_DEBUG
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / bfs / file.c
blob807723b65daf3b1c93830325e441bf4b03bc81ef
1 /*
2 * fs/bfs/file.c
3 * BFS file operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 */
7 #include <linux/fs.h>
8 #include <linux/buffer_head.h>
9 #include <linux/smp_lock.h>
10 #include "bfs.h"
12 #undef DEBUG
14 #ifdef DEBUG
15 #define dprintf(x...) printf(x)
16 #else
17 #define dprintf(x...)
18 #endif
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);
33 if (!bh)
34 return -EIO;
35 new = sb_getblk(sb, to);
36 memcpy(new->b_data, bh->b_data, bh->b_size);
37 mark_buffer_dirty(new);
38 bforget(bh);
39 brelse(new);
40 return 0;
43 static int bfs_move_blocks(struct super_block *sb, unsigned long start,
44 unsigned long end, unsigned long where)
46 unsigned long i;
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);
52 return -EIO;
54 return 0;
57 static int bfs_get_block(struct inode * inode, sector_t block,
58 struct buffer_head * bh_result, int create)
60 unsigned long phys;
61 int err;
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)
68 return -EIO;
70 phys = bi->i_sblock + block;
71 if (!create) {
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);
77 return 0;
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);
86 return 0;
89 /* the rest has to be protected against itself */
90 lock_kernel();
92 /* if the last data block for this file is the last allocated
93 block, we can extend the file trivially, without moving it
94 anywhere */
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);
103 err = 0;
104 goto out;
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,
111 bi->i_eblock, phys);
112 if (err) {
113 dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
114 goto out;
116 } else
117 err = 0;
119 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
120 create, (unsigned long)block, phys);
121 bi->i_sblock = phys;
122 phys += block;
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);
131 out:
132 unlock_kernel();
133 return err;
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,
162 .bmap = bfs_bmap,
165 struct inode_operations bfs_file_inops;