blob: 3d5aca28a0a0980922c83c0b197d1cf52cb27593 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/bfs/file.c
3 * BFS file operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 */
6
7#include <linux/fs.h>
8#include <linux/buffer_head.h>
9#include <linux/smp_lock.h>
10#include "bfs.h"
11
12#undef DEBUG
13
14#ifdef DEBUG
15#define dprintf(x...) printf(x)
16#else
17#define dprintf(x...)
18#endif
19
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080020const struct file_operations bfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 .llseek = generic_file_llseek,
22 .read = generic_file_read,
23 .write = generic_file_write,
24 .mmap = generic_file_mmap,
25 .sendfile = generic_file_sendfile,
26};
27
28static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb)
29{
30 struct buffer_head *bh, *new;
31
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;
41}
42
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070043static int bfs_move_blocks(struct super_block *sb, unsigned long start,
44 unsigned long end, unsigned long where)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 unsigned long i;
47
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;
53 }
54 return 0;
55}
56
57static int bfs_get_block(struct inode * inode, sector_t block,
58 struct buffer_head * bh_result, int create)
59{
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070060 unsigned long phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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;
66
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070067 if (block > info->si_blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -EIO;
69
70 phys = bi->i_sblock + block;
71 if (!create) {
72 if (phys <= bi->i_eblock) {
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070073 dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
74 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 map_bh(bh_result, sb, phys);
76 }
77 return 0;
78 }
79
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",
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070084 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 map_bh(bh_result, sb, phys);
86 return 0;
87 }
88
89 /* the rest has to be protected against itself */
90 lock_kernel();
91
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070092 /* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (bi->i_eblock == info->si_lf_eblk) {
96 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070097 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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;
105 }
106
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;
115 }
116 } else
117 err = 0;
118
Andrew Stribblehillfac92be2005-09-09 13:02:04 -0700119 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
120 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 bi->i_sblock = phys;
122 phys += block;
123 info->si_lf_eblk = bi->i_eblock = phys;
124
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);
131out:
132 unlock_kernel();
133 return err;
134}
135
136static int bfs_writepage(struct page *page, struct writeback_control *wbc)
137{
138 return block_write_full_page(page, bfs_get_block, wbc);
139}
140
141static int bfs_readpage(struct file *file, struct page *page)
142{
143 return block_read_full_page(page, bfs_get_block);
144}
145
146static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
147{
148 return block_prepare_write(page, from, to, bfs_get_block);
149}
150
151static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
152{
153 return generic_block_bmap(mapping, block, bfs_get_block);
154}
155
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700156const struct address_space_operations bfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .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,
163};
164
165struct inode_operations bfs_file_inops;