10 #include <linux/string.h>
15 static inline void dirty_indirect(
struct buffer_head *bh,
struct inode *
inode)
22 static int block_to_path(
struct inode *inode,
long block,
int offsets[
DEPTH])
32 printk(
"sysv_block_map: block < 0\n");
33 }
else if (block <
DIRECT) {
35 }
else if ( (block -=
DIRECT) < indirect_blocks) {
38 }
else if ((block -= indirect_blocks) < double_blocks) {
40 offsets[n++] = block >> ptrs_bits;
41 offsets[n++] = block & (indirect_blocks - 1);
42 }
else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
44 offsets[n++] = block >> (ptrs_bits * 2);
45 offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
46 offsets[n++] = block & (indirect_blocks - 1);
61 struct buffer_head *bh;
74 while (from <= to && from->
key == *from->
p)
79 static inline sysv_zone_t *block_end(
struct buffer_head *bh)
81 return (
sysv_zone_t*)((
char*)bh->b_data + bh->b_size);
87 static Indirect *get_branch(
struct inode *inode,
95 struct buffer_head *bh;
98 add_chain(chain,
NULL, SYSV_I(inode)->i_data + *offsets);
102 int block = block_to_cpu(SYSV_SB(sb), p->
key);
103 bh = sb_bread(sb, block);
106 if (!verify_chain(chain, p))
108 add_chain(++p, bh, (
sysv_zone_t*)bh->b_data + *++offsets);
124 static int alloc_branch(
struct inode *inode,
129 int blocksize = inode->
i_sb->s_blocksize;
134 if (branch[0].
key)
for (n = 1; n < num; n++) {
135 struct buffer_head *bh;
145 parent = block_to_cpu(SYSV_SB(inode->
i_sb), branch[n-1].
key);
146 bh = sb_getblk(inode->
i_sb, parent);
148 memset(bh->b_data, 0, blocksize);
151 *branch[n].
p = branch[n].
key;
152 set_buffer_uptodate(bh);
154 dirty_indirect(bh, inode);
160 for (i = 1; i <
n; i++)
161 bforget(branch[i].bh);
162 for (i = 0; i <
n; i++)
167 static inline int splice_branch(
struct inode *inode,
176 if (!verify_chain(chain, where-1) || *where->
p)
178 *where->
p = where->
key;
185 dirty_indirect(where->
bh, inode);
190 mark_inode_dirty(inode);
195 for (i = 1; i < num; i++)
196 bforget(where[i].bh);
197 for (i = 0; i < num; i++)
202 static int get_block(
struct inode *inode,
sector_t iblock,
struct buffer_head *bh_result,
int create)
210 int depth = block_to_path(inode, iblock, offsets);
217 partial = get_branch(inode, depth, offsets, chain, &err);
223 map_bh(bh_result, sb, block_to_cpu(SYSV_SB(sb),
224 chain[depth-1].key));
226 partial = chain+depth-1;
231 if (!create || err == -
EIO) {
233 while (partial > chain) {
249 left = (chain +
depth) - partial;
250 err = alloc_branch(inode, left, offsets+(partial-chain), partial);
254 if (splice_branch(inode, chain, partial, left) < 0)
257 set_buffer_new(bh_result);
261 while (partial > chain) {
276 static Indirect *find_shared(
struct inode *inode,
286 for (k = depth; k > 1 && !offsets[k-1]; k--)
290 partial = get_branch(inode, k, offsets, chain, &err);
292 partial = chain + k-1;
297 if (!partial->
key && *partial->
p) {
301 for (p=partial; p>chain && all_zeroes((
sysv_zone_t*)p->
bh->b_data,p->
p); p--)
309 if (p == chain + k - 1 && p > chain) {
317 while (partial > p) {
327 for ( ; p <
q ; p++) {
332 mark_inode_dirty(inode);
339 struct buffer_head * bh;
343 for ( ; p <
q ; p++) {
349 block = block_to_cpu(SYSV_SB(sb), nr);
350 bh = sb_bread(sb, block);
354 block_end(bh), depth);
357 mark_inode_dirty(inode);
360 free_data(inode, p, q);
378 blocksize = inode->
i_sb->s_blocksize;
379 iblock = (inode->
i_size + blocksize-1)
380 >> inode->
i_sb->s_blocksize_bits;
384 n = block_to_path(inode, iblock, offsets);
389 free_data(inode, i_data+offsets[0], i_data +
DIRECT);
393 partial = find_shared(inode, n, offsets, chain, &nr);
396 if (partial == chain)
397 mark_inode_dirty(inode);
399 dirty_indirect(partial->
bh, inode);
400 free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
403 while (partial > chain) {
404 free_branches(inode, partial->
p + 1, block_end(partial->
bh),
405 (chain+n-1) - partial);
406 dirty_indirect(partial->
bh, inode);
407 brelse (partial->
bh);
413 nr = i_data[
DIRECT + n - 1];
415 i_data[
DIRECT + n - 1] = 0;
416 mark_inode_dirty(inode);
417 free_branches(inode, &nr, &nr+1, n);
425 mark_inode_dirty(inode);
435 while (--i && blocks > direct) {
436 blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
468 loff_t
pos,
unsigned len,
unsigned flags,
469 struct page **pagep,
void **fsdata)
475 loff_t isize = mapping->
host->i_size;
476 if (pos + len > isize)
489 .readpage = sysv_readpage,
490 .writepage = sysv_writepage,
491 .write_begin = sysv_write_begin,