[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / sysv / itree.c
blob60b40506748dab7bd266928a3a820273bbd9598f
1 /*
2 * linux/fs/sysv/itree.c
4 * Handling of indirect blocks' trees.
5 * AV, Sep--Dec 2000
6 */
8 #include <linux/buffer_head.h>
9 #include <linux/mount.h>
10 #include <linux/string.h>
11 #include "sysv.h"
13 enum {DIRECT = 10, DEPTH = 4}; /* Have triple indirect */
15 static inline void dirty_indirect(struct buffer_head *bh, struct inode *inode)
17 mark_buffer_dirty_inode(bh, inode);
18 if (IS_SYNC(inode))
19 sync_dirty_buffer(bh);
22 static int block_to_path(struct inode *inode, long block, int offsets[DEPTH])
24 struct super_block *sb = inode->i_sb;
25 struct sysv_sb_info *sbi = SYSV_SB(sb);
26 int ptrs_bits = sbi->s_ind_per_block_bits;
27 unsigned long indirect_blocks = sbi->s_ind_per_block,
28 double_blocks = sbi->s_ind_per_block_2;
29 int n = 0;
31 if (block < 0) {
32 printk("sysv_block_map: block < 0\n");
33 } else if (block < DIRECT) {
34 offsets[n++] = block;
35 } else if ( (block -= DIRECT) < indirect_blocks) {
36 offsets[n++] = DIRECT;
37 offsets[n++] = block;
38 } else if ((block -= indirect_blocks) < double_blocks) {
39 offsets[n++] = DIRECT+1;
40 offsets[n++] = block >> ptrs_bits;
41 offsets[n++] = block & (indirect_blocks - 1);
42 } else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
43 offsets[n++] = DIRECT+2;
44 offsets[n++] = block >> (ptrs_bits * 2);
45 offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
46 offsets[n++] = block & (indirect_blocks - 1);
47 } else {
48 /* nothing */;
50 return n;
53 static inline int block_to_cpu(struct sysv_sb_info *sbi, u32 nr)
55 return sbi->s_block_base + fs32_to_cpu(sbi, nr);
58 typedef struct {
59 u32 *p;
60 u32 key;
61 struct buffer_head *bh;
62 } Indirect;
64 static rwlock_t pointers_lock = RW_LOCK_UNLOCKED;
66 static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
68 p->key = *(p->p = v);
69 p->bh = bh;
72 static inline int verify_chain(Indirect *from, Indirect *to)
74 while (from <= to && from->key == *from->p)
75 from++;
76 return (from > to);
79 static inline u32 *block_end(struct buffer_head *bh)
81 return (u32*)((char*)bh->b_data + bh->b_size);
84 static Indirect *get_branch(struct inode *inode,
85 int depth,
86 int offsets[],
87 Indirect chain[],
88 int *err)
90 struct super_block *sb = inode->i_sb;
91 Indirect *p = chain;
92 struct buffer_head *bh;
94 *err = 0;
95 add_chain(chain, NULL, SYSV_I(inode)->i_data + *offsets);
96 if (!p->key)
97 goto no_block;
98 while (--depth) {
99 int block = block_to_cpu(SYSV_SB(sb), p->key);
100 bh = sb_bread(sb, block);
101 if (!bh)
102 goto failure;
103 read_lock(&pointers_lock);
104 if (!verify_chain(chain, p))
105 goto changed;
106 add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
107 read_unlock(&pointers_lock);
108 if (!p->key)
109 goto no_block;
111 return NULL;
113 changed:
114 read_unlock(&pointers_lock);
115 brelse(bh);
116 *err = -EAGAIN;
117 goto no_block;
118 failure:
119 *err = -EIO;
120 no_block:
121 return p;
124 static int alloc_branch(struct inode *inode,
125 int num,
126 int *offsets,
127 Indirect *branch)
129 int blocksize = inode->i_sb->s_blocksize;
130 int n = 0;
131 int i;
133 branch[0].key = sysv_new_block(inode->i_sb);
134 if (branch[0].key) for (n = 1; n < num; n++) {
135 struct buffer_head *bh;
136 int parent;
137 /* Allocate the next block */
138 branch[n].key = sysv_new_block(inode->i_sb);
139 if (!branch[n].key)
140 break;
142 * Get buffer_head for parent block, zero it out and set
143 * the pointer to new one, then send parent to disk.
145 parent = block_to_cpu(SYSV_SB(inode->i_sb), branch[n-1].key);
146 bh = sb_getblk(inode->i_sb, parent);
147 lock_buffer(bh);
148 memset(bh->b_data, 0, blocksize);
149 branch[n].bh = bh;
150 branch[n].p = (u32*) bh->b_data + offsets[n];
151 *branch[n].p = branch[n].key;
152 set_buffer_uptodate(bh);
153 unlock_buffer(bh);
154 dirty_indirect(bh, inode);
156 if (n == num)
157 return 0;
159 /* Allocation failed, free what we already allocated */
160 for (i = 1; i < n; i++)
161 bforget(branch[i].bh);
162 for (i = 0; i < n; i++)
163 sysv_free_block(inode->i_sb, branch[i].key);
164 return -ENOSPC;
167 static inline int splice_branch(struct inode *inode,
168 Indirect chain[],
169 Indirect *where,
170 int num)
172 int i;
174 /* Verify that place we are splicing to is still there and vacant */
175 write_lock(&pointers_lock);
176 if (!verify_chain(chain, where-1) || *where->p)
177 goto changed;
178 *where->p = where->key;
179 write_unlock(&pointers_lock);
181 inode->i_ctime = CURRENT_TIME;
183 /* had we spliced it onto indirect block? */
184 if (where->bh)
185 dirty_indirect(where->bh, inode);
187 if (IS_SYNC(inode))
188 sysv_sync_inode(inode);
189 else
190 mark_inode_dirty(inode);
191 return 0;
193 changed:
194 write_unlock(&pointers_lock);
195 for (i = 1; i < num; i++)
196 bforget(where[i].bh);
197 for (i = 0; i < num; i++)
198 sysv_free_block(inode->i_sb, where[i].key);
199 return -EAGAIN;
202 static int get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
204 int err = -EIO;
205 int offsets[DEPTH];
206 Indirect chain[DEPTH];
207 struct super_block *sb = inode->i_sb;
208 Indirect *partial;
209 int left;
210 int depth = block_to_path(inode, iblock, offsets);
212 if (depth == 0)
213 goto out;
215 reread:
216 partial = get_branch(inode, depth, offsets, chain, &err);
218 /* Simplest case - block found, no allocation needed */
219 if (!partial) {
220 got_it:
221 map_bh(bh_result, sb, block_to_cpu(SYSV_SB(sb),
222 chain[depth-1].key));
223 /* Clean up and exit */
224 partial = chain+depth-1; /* the whole chain */
225 goto cleanup;
228 /* Next simple case - plain lookup or failed read of indirect block */
229 if (!create || err == -EIO) {
230 cleanup:
231 while (partial > chain) {
232 brelse(partial->bh);
233 partial--;
235 out:
236 return err;
240 * Indirect block might be removed by truncate while we were
241 * reading it. Handling of that case (forget what we've got and
242 * reread) is taken out of the main path.
244 if (err == -EAGAIN)
245 goto changed;
247 left = (chain + depth) - partial;
248 err = alloc_branch(inode, left, offsets+(partial-chain), partial);
249 if (err)
250 goto cleanup;
252 if (splice_branch(inode, chain, partial, left) < 0)
253 goto changed;
255 set_buffer_new(bh_result);
256 goto got_it;
258 changed:
259 while (partial > chain) {
260 brelse(partial->bh);
261 partial--;
263 goto reread;
266 static inline int all_zeroes(u32 *p, u32 *q)
268 while (p < q)
269 if (*p++)
270 return 0;
271 return 1;
274 static Indirect *find_shared(struct inode *inode,
275 int depth,
276 int offsets[],
277 Indirect chain[],
278 u32 *top)
280 Indirect *partial, *p;
281 int k, err;
283 *top = 0;
284 for (k = depth; k > 1 && !offsets[k-1]; k--)
287 write_lock(&pointers_lock);
288 partial = get_branch(inode, k, offsets, chain, &err);
289 if (!partial)
290 partial = chain + k-1;
292 * If the branch acquired continuation since we've looked at it -
293 * fine, it should all survive and (new) top doesn't belong to us.
295 if (!partial->key && *partial->p) {
296 write_unlock(&pointers_lock);
297 goto no_top;
299 for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
302 * OK, we've found the last block that must survive. The rest of our
303 * branch should be detached before unlocking. However, if that rest
304 * of branch is all ours and does not grow immediately from the inode
305 * it's easier to cheat and just decrement partial->p.
307 if (p == chain + k - 1 && p > chain) {
308 p->p--;
309 } else {
310 *top = *p->p;
311 *p->p = 0;
313 write_unlock(&pointers_lock);
315 while (partial > p) {
316 brelse(partial->bh);
317 partial--;
319 no_top:
320 return partial;
323 static inline void free_data(struct inode *inode, u32 *p, u32 *q)
325 for ( ; p < q ; p++) {
326 u32 nr = *p;
327 if (nr) {
328 *p = 0;
329 sysv_free_block(inode->i_sb, nr);
330 mark_inode_dirty(inode);
335 static void free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
337 struct buffer_head * bh;
338 struct super_block *sb = inode->i_sb;
340 if (depth--) {
341 for ( ; p < q ; p++) {
342 int block;
343 u32 nr = *p;
344 if (!nr)
345 continue;
346 *p = 0;
347 block = block_to_cpu(SYSV_SB(sb), nr);
348 bh = sb_bread(sb, block);
349 if (!bh)
350 continue;
351 free_branches(inode, (u32*)bh->b_data,
352 block_end(bh), depth);
353 bforget(bh);
354 sysv_free_block(sb, nr);
355 mark_inode_dirty(inode);
357 } else
358 free_data(inode, p, q);
361 void sysv_truncate (struct inode * inode)
363 u32 *i_data = SYSV_I(inode)->i_data;
364 int offsets[DEPTH];
365 Indirect chain[DEPTH];
366 Indirect *partial;
367 int nr = 0;
368 int n;
369 long iblock;
370 unsigned blocksize;
372 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
373 S_ISLNK(inode->i_mode)))
374 return;
376 blocksize = inode->i_sb->s_blocksize;
377 iblock = (inode->i_size + blocksize-1)
378 >> inode->i_sb->s_blocksize_bits;
380 block_truncate_page(inode->i_mapping, inode->i_size, get_block);
382 n = block_to_path(inode, iblock, offsets);
383 if (n == 0)
384 return;
386 if (n == 1) {
387 free_data(inode, i_data+offsets[0], i_data + DIRECT);
388 goto do_indirects;
391 partial = find_shared(inode, n, offsets, chain, &nr);
392 /* Kill the top of shared branch (already detached) */
393 if (nr) {
394 if (partial == chain)
395 mark_inode_dirty(inode);
396 else
397 dirty_indirect(partial->bh, inode);
398 free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
400 /* Clear the ends of indirect blocks on the shared branch */
401 while (partial > chain) {
402 free_branches(inode, partial->p + 1, block_end(partial->bh),
403 (chain+n-1) - partial);
404 dirty_indirect(partial->bh, inode);
405 brelse (partial->bh);
406 partial--;
408 do_indirects:
409 /* Kill the remaining (whole) subtrees (== subtrees deeper than...) */
410 while (n < DEPTH) {
411 nr = i_data[DIRECT + n - 1];
412 if (nr) {
413 i_data[DIRECT + n - 1] = 0;
414 mark_inode_dirty(inode);
415 free_branches(inode, &nr, &nr+1, n);
417 n++;
419 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
420 if (IS_SYNC(inode))
421 sysv_sync_inode (inode);
422 else
423 mark_inode_dirty(inode);
426 static unsigned sysv_nblocks(struct super_block *s, loff_t size)
428 struct sysv_sb_info *sbi = SYSV_SB(s);
429 int ptrs_bits = sbi->s_ind_per_block_bits;
430 unsigned blocks, res, direct = DIRECT, i = DEPTH;
431 blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
432 res = blocks;
433 while (--i && blocks > direct) {
434 blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
435 res += blocks;
436 direct = 1;
438 return blocks;
441 int sysv_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
443 struct super_block *s = mnt->mnt_sb;
444 generic_fillattr(dentry->d_inode, stat);
445 stat->blocks = (s->s_blocksize / 512) * sysv_nblocks(s, stat->size);
446 stat->blksize = s->s_blocksize;
447 return 0;
450 static int sysv_writepage(struct page *page, struct writeback_control *wbc)
452 return block_write_full_page(page,get_block,wbc);
454 static int sysv_readpage(struct file *file, struct page *page)
456 return block_read_full_page(page,get_block);
458 static int sysv_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
460 return block_prepare_write(page,from,to,get_block);
462 static sector_t sysv_bmap(struct address_space *mapping, sector_t block)
464 return generic_block_bmap(mapping,block,get_block);
466 struct address_space_operations sysv_aops = {
467 .readpage = sysv_readpage,
468 .writepage = sysv_writepage,
469 .sync_page = block_sync_page,
470 .prepare_write = sysv_prepare_write,
471 .commit_write = generic_commit_write,
472 .bmap = sysv_bmap