]> err.no Git - linux-2.6/blob - fs/hfs/btree.c
Pull bugzilla-9627 into release branch
[linux-2.6] / fs / hfs / btree.c
1 /*
2  *  linux/fs/hfs/btree.c
3  *
4  * Copyright (C) 2001
5  * Brad Boyer (flar@allandria.com)
6  * (C) 2003 Ardis Technologies <roman@ardistech.com>
7  *
8  * Handle opening/closing btree
9  */
10
11 #include <linux/pagemap.h>
12 #include <linux/log2.h>
13
14 #include "btree.h"
15
16 /* Get a reference to a B*Tree and do some initial checks */
17 struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp keycmp)
18 {
19         struct hfs_btree *tree;
20         struct hfs_btree_header_rec *head;
21         struct address_space *mapping;
22         struct page *page;
23         unsigned int size;
24
25         tree = kzalloc(sizeof(*tree), GFP_KERNEL);
26         if (!tree)
27                 return NULL;
28
29         init_MUTEX(&tree->tree_lock);
30         spin_lock_init(&tree->hash_lock);
31         /* Set the correct compare function */
32         tree->sb = sb;
33         tree->cnid = id;
34         tree->keycmp = keycmp;
35
36         tree->inode = iget_locked(sb, id);
37         if (!tree->inode)
38                 goto free_tree;
39         BUG_ON(!(tree->inode->i_state & I_NEW));
40         {
41         struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
42         HFS_I(tree->inode)->flags = 0;
43         init_MUTEX(&HFS_I(tree->inode)->extents_lock);
44         switch (id) {
45         case HFS_EXT_CNID:
46                 hfs_inode_read_fork(tree->inode, mdb->drXTExtRec, mdb->drXTFlSize,
47                                     mdb->drXTFlSize, be32_to_cpu(mdb->drXTClpSiz));
48                 tree->inode->i_mapping->a_ops = &hfs_btree_aops;
49                 break;
50         case HFS_CAT_CNID:
51                 hfs_inode_read_fork(tree->inode, mdb->drCTExtRec, mdb->drCTFlSize,
52                                     mdb->drCTFlSize, be32_to_cpu(mdb->drCTClpSiz));
53                 tree->inode->i_mapping->a_ops = &hfs_btree_aops;
54                 break;
55         default:
56                 BUG();
57         }
58         }
59         unlock_new_inode(tree->inode);
60
61         mapping = tree->inode->i_mapping;
62         page = read_mapping_page(mapping, 0, NULL);
63         if (IS_ERR(page))
64                 goto free_tree;
65
66         /* Load the header */
67         head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
68         tree->root = be32_to_cpu(head->root);
69         tree->leaf_count = be32_to_cpu(head->leaf_count);
70         tree->leaf_head = be32_to_cpu(head->leaf_head);
71         tree->leaf_tail = be32_to_cpu(head->leaf_tail);
72         tree->node_count = be32_to_cpu(head->node_count);
73         tree->free_nodes = be32_to_cpu(head->free_nodes);
74         tree->attributes = be32_to_cpu(head->attributes);
75         tree->node_size = be16_to_cpu(head->node_size);
76         tree->max_key_len = be16_to_cpu(head->max_key_len);
77         tree->depth = be16_to_cpu(head->depth);
78
79         size = tree->node_size;
80         if (!is_power_of_2(size))
81                 goto fail_page;
82         if (!tree->node_count)
83                 goto fail_page;
84         if ((id == HFS_EXT_CNID) && (tree->max_key_len != HFS_MAX_EXT_KEYLEN)) {
85                 printk(KERN_ERR "hfs: invalid extent max_key_len %d\n",
86                         tree->max_key_len);
87                 goto fail_page;
88         }
89         if ((id == HFS_CAT_CNID) && (tree->max_key_len != HFS_MAX_CAT_KEYLEN)) {
90                 printk(KERN_ERR "hfs: invalid catalog max_key_len %d\n",
91                         tree->max_key_len);
92                 goto fail_page;
93         }
94
95         tree->node_size_shift = ffs(size) - 1;
96         tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
97
98         kunmap(page);
99         page_cache_release(page);
100         return tree;
101
102  fail_page:
103         page_cache_release(page);
104  free_tree:
105         tree->inode->i_mapping->a_ops = &hfs_aops;
106         iput(tree->inode);
107         kfree(tree);
108         return NULL;
109 }
110
111 /* Release resources used by a btree */
112 void hfs_btree_close(struct hfs_btree *tree)
113 {
114         struct hfs_bnode *node;
115         int i;
116
117         if (!tree)
118                 return;
119
120         for (i = 0; i < NODE_HASH_SIZE; i++) {
121                 while ((node = tree->node_hash[i])) {
122                         tree->node_hash[i] = node->next_hash;
123                         if (atomic_read(&node->refcnt))
124                                 printk(KERN_ERR "hfs: node %d:%d still has %d user(s)!\n",
125                                         node->tree->cnid, node->this, atomic_read(&node->refcnt));
126                         hfs_bnode_free(node);
127                         tree->node_hash_cnt--;
128                 }
129         }
130         iput(tree->inode);
131         kfree(tree);
132 }
133
134 void hfs_btree_write(struct hfs_btree *tree)
135 {
136         struct hfs_btree_header_rec *head;
137         struct hfs_bnode *node;
138         struct page *page;
139
140         node = hfs_bnode_find(tree, 0);
141         if (IS_ERR(node))
142                 /* panic? */
143                 return;
144         /* Load the header */
145         page = node->page[0];
146         head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
147
148         head->root = cpu_to_be32(tree->root);
149         head->leaf_count = cpu_to_be32(tree->leaf_count);
150         head->leaf_head = cpu_to_be32(tree->leaf_head);
151         head->leaf_tail = cpu_to_be32(tree->leaf_tail);
152         head->node_count = cpu_to_be32(tree->node_count);
153         head->free_nodes = cpu_to_be32(tree->free_nodes);
154         head->attributes = cpu_to_be32(tree->attributes);
155         head->depth = cpu_to_be16(tree->depth);
156
157         kunmap(page);
158         set_page_dirty(page);
159         hfs_bnode_put(node);
160 }
161
162 static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
163 {
164         struct hfs_btree *tree = prev->tree;
165         struct hfs_bnode *node;
166         struct hfs_bnode_desc desc;
167         __be32 cnid;
168
169         node = hfs_bnode_create(tree, idx);
170         if (IS_ERR(node))
171                 return node;
172
173         if (!tree->free_nodes)
174                 panic("FIXME!!!");
175         tree->free_nodes--;
176         prev->next = idx;
177         cnid = cpu_to_be32(idx);
178         hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
179
180         node->type = HFS_NODE_MAP;
181         node->num_recs = 1;
182         hfs_bnode_clear(node, 0, tree->node_size);
183         desc.next = 0;
184         desc.prev = 0;
185         desc.type = HFS_NODE_MAP;
186         desc.height = 0;
187         desc.num_recs = cpu_to_be16(1);
188         desc.reserved = 0;
189         hfs_bnode_write(node, &desc, 0, sizeof(desc));
190         hfs_bnode_write_u16(node, 14, 0x8000);
191         hfs_bnode_write_u16(node, tree->node_size - 2, 14);
192         hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
193
194         return node;
195 }
196
197 struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
198 {
199         struct hfs_bnode *node, *next_node;
200         struct page **pagep;
201         u32 nidx, idx;
202         u16 off, len;
203         u8 *data, byte, m;
204         int i;
205
206         while (!tree->free_nodes) {
207                 struct inode *inode = tree->inode;
208                 u32 count;
209                 int res;
210
211                 res = hfs_extend_file(inode);
212                 if (res)
213                         return ERR_PTR(res);
214                 HFS_I(inode)->phys_size = inode->i_size =
215                                 (loff_t)HFS_I(inode)->alloc_blocks *
216                                 HFS_SB(tree->sb)->alloc_blksz;
217                 HFS_I(inode)->fs_blocks = inode->i_size >>
218                                           tree->sb->s_blocksize_bits;
219                 inode_set_bytes(inode, inode->i_size);
220                 count = inode->i_size >> tree->node_size_shift;
221                 tree->free_nodes = count - tree->node_count;
222                 tree->node_count = count;
223         }
224
225         nidx = 0;
226         node = hfs_bnode_find(tree, nidx);
227         if (IS_ERR(node))
228                 return node;
229         len = hfs_brec_lenoff(node, 2, &off);
230
231         off += node->page_offset;
232         pagep = node->page + (off >> PAGE_CACHE_SHIFT);
233         data = kmap(*pagep);
234         off &= ~PAGE_CACHE_MASK;
235         idx = 0;
236
237         for (;;) {
238                 while (len) {
239                         byte = data[off];
240                         if (byte != 0xff) {
241                                 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
242                                         if (!(byte & m)) {
243                                                 idx += i;
244                                                 data[off] |= m;
245                                                 set_page_dirty(*pagep);
246                                                 kunmap(*pagep);
247                                                 tree->free_nodes--;
248                                                 mark_inode_dirty(tree->inode);
249                                                 hfs_bnode_put(node);
250                                                 return hfs_bnode_create(tree, idx);
251                                         }
252                                 }
253                         }
254                         if (++off >= PAGE_CACHE_SIZE) {
255                                 kunmap(*pagep);
256                                 data = kmap(*++pagep);
257                                 off = 0;
258                         }
259                         idx += 8;
260                         len--;
261                 }
262                 kunmap(*pagep);
263                 nidx = node->next;
264                 if (!nidx) {
265                         printk(KERN_DEBUG "hfs: create new bmap node...\n");
266                         next_node = hfs_bmap_new_bmap(node, idx);
267                 } else
268                         next_node = hfs_bnode_find(tree, nidx);
269                 hfs_bnode_put(node);
270                 if (IS_ERR(next_node))
271                         return next_node;
272                 node = next_node;
273
274                 len = hfs_brec_lenoff(node, 0, &off);
275                 off += node->page_offset;
276                 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
277                 data = kmap(*pagep);
278                 off &= ~PAGE_CACHE_MASK;
279         }
280 }
281
282 void hfs_bmap_free(struct hfs_bnode *node)
283 {
284         struct hfs_btree *tree;
285         struct page *page;
286         u16 off, len;
287         u32 nidx;
288         u8 *data, byte, m;
289
290         dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
291         tree = node->tree;
292         nidx = node->this;
293         node = hfs_bnode_find(tree, 0);
294         if (IS_ERR(node))
295                 return;
296         len = hfs_brec_lenoff(node, 2, &off);
297         while (nidx >= len * 8) {
298                 u32 i;
299
300                 nidx -= len * 8;
301                 i = node->next;
302                 hfs_bnode_put(node);
303                 if (!i) {
304                         /* panic */;
305                         printk(KERN_CRIT "hfs: unable to free bnode %u. bmap not found!\n", node->this);
306                         return;
307                 }
308                 node = hfs_bnode_find(tree, i);
309                 if (IS_ERR(node))
310                         return;
311                 if (node->type != HFS_NODE_MAP) {
312                         /* panic */;
313                         printk(KERN_CRIT "hfs: invalid bmap found! (%u,%d)\n", node->this, node->type);
314                         hfs_bnode_put(node);
315                         return;
316                 }
317                 len = hfs_brec_lenoff(node, 0, &off);
318         }
319         off += node->page_offset + nidx / 8;
320         page = node->page[off >> PAGE_CACHE_SHIFT];
321         data = kmap(page);
322         off &= ~PAGE_CACHE_MASK;
323         m = 1 << (~nidx & 7);
324         byte = data[off];
325         if (!(byte & m)) {
326                 printk(KERN_CRIT "hfs: trying to free free bnode %u(%d)\n", node->this, node->type);
327                 kunmap(page);
328                 hfs_bnode_put(node);
329                 return;
330         }
331         data[off] = byte & ~m;
332         set_page_dirty(page);
333         kunmap(page);
334         hfs_bnode_put(node);
335         tree->free_nodes++;
336         mark_inode_dirty(tree->inode);
337 }