blob: ce014ceb89efcc0b810f99a8cac4bbf815f21bfc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hfsplus/bnode.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handle basic btree node operations
9 */
10
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/pagemap.h>
14#include <linux/fs.h>
15#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include "hfsplus_fs.h"
18#include "hfsplus_raw.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* Copy a specified range of bytes from the raw data of a node */
21void hfs_bnode_read(struct hfs_bnode *node, void *buf, int off, int len)
22{
23 struct page **pagep;
24 int l;
25
26 off += node->page_offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030027 pagep = node->page + (off >> PAGE_SHIFT);
28 off &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030030 l = min_t(int, len, PAGE_SIZE - off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 memcpy(buf, kmap(*pagep) + off, l);
32 kunmap(*pagep);
33
34 while ((len -= l) != 0) {
35 buf += l;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030036 l = min_t(int, len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 memcpy(buf, kmap(*++pagep), l);
38 kunmap(*pagep);
39 }
40}
41
42u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
43{
44 __be16 data;
Anton Salikhmetov21f22962010-12-16 18:08:39 +020045 /* TODO: optimize later... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 hfs_bnode_read(node, &data, off, 2);
47 return be16_to_cpu(data);
48}
49
50u8 hfs_bnode_read_u8(struct hfs_bnode *node, int off)
51{
52 u8 data;
Anton Salikhmetov21f22962010-12-16 18:08:39 +020053 /* TODO: optimize later... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 hfs_bnode_read(node, &data, off, 1);
55 return data;
56}
57
58void hfs_bnode_read_key(struct hfs_bnode *node, void *key, int off)
59{
60 struct hfs_btree *tree;
61 int key_len;
62
63 tree = node->tree;
64 if (node->type == HFS_NODE_LEAF ||
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -080065 tree->attributes & HFS_TREE_VARIDXKEYS ||
66 node->tree->cnid == HFSPLUS_ATTR_CNID)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 key_len = hfs_bnode_read_u16(node, off) + 2;
68 else
69 key_len = tree->max_key_len + 2;
70
71 hfs_bnode_read(node, key, off, key_len);
72}
73
74void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
75{
76 struct page **pagep;
77 int l;
78
79 off += node->page_offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030080 pagep = node->page + (off >> PAGE_SHIFT);
81 off &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030083 l = min_t(int, len, PAGE_SIZE - off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 memcpy(kmap(*pagep) + off, buf, l);
85 set_page_dirty(*pagep);
86 kunmap(*pagep);
87
88 while ((len -= l) != 0) {
89 buf += l;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030090 l = min_t(int, len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 memcpy(kmap(*++pagep), buf, l);
92 set_page_dirty(*pagep);
93 kunmap(*pagep);
94 }
95}
96
97void hfs_bnode_write_u16(struct hfs_bnode *node, int off, u16 data)
98{
99 __be16 v = cpu_to_be16(data);
Anton Salikhmetov21f22962010-12-16 18:08:39 +0200100 /* TODO: optimize later... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 hfs_bnode_write(node, &v, off, 2);
102}
103
104void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
105{
106 struct page **pagep;
107 int l;
108
109 off += node->page_offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300110 pagep = node->page + (off >> PAGE_SHIFT);
111 off &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300113 l = min_t(int, len, PAGE_SIZE - off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 memset(kmap(*pagep) + off, 0, l);
115 set_page_dirty(*pagep);
116 kunmap(*pagep);
117
118 while ((len -= l) != 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300119 l = min_t(int, len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 memset(kmap(*++pagep), 0, l);
121 set_page_dirty(*pagep);
122 kunmap(*pagep);
123 }
124}
125
126void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
127 struct hfs_bnode *src_node, int src, int len)
128{
129 struct hfs_btree *tree;
130 struct page **src_page, **dst_page;
131 int l;
132
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700133 hfs_dbg(BNODE_MOD, "copybytes: %u,%u,%u\n", dst, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!len)
135 return;
136 tree = src_node->tree;
137 src += src_node->page_offset;
138 dst += dst_node->page_offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300139 src_page = src_node->page + (src >> PAGE_SHIFT);
140 src &= ~PAGE_MASK;
141 dst_page = dst_node->page + (dst >> PAGE_SHIFT);
142 dst &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (src == dst) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300145 l = min_t(int, len, PAGE_SIZE - src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 memcpy(kmap(*dst_page) + src, kmap(*src_page) + src, l);
147 kunmap(*src_page);
148 set_page_dirty(*dst_page);
149 kunmap(*dst_page);
150
151 while ((len -= l) != 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300152 l = min_t(int, len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 memcpy(kmap(*++dst_page), kmap(*++src_page), l);
154 kunmap(*src_page);
155 set_page_dirty(*dst_page);
156 kunmap(*dst_page);
157 }
158 } else {
159 void *src_ptr, *dst_ptr;
160
161 do {
162 src_ptr = kmap(*src_page) + src;
163 dst_ptr = kmap(*dst_page) + dst;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300164 if (PAGE_SIZE - src < PAGE_SIZE - dst) {
165 l = PAGE_SIZE - src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 src = 0;
167 dst += l;
168 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300169 l = PAGE_SIZE - dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 src += l;
171 dst = 0;
172 }
173 l = min(len, l);
174 memcpy(dst_ptr, src_ptr, l);
175 kunmap(*src_page);
176 set_page_dirty(*dst_page);
177 kunmap(*dst_page);
178 if (!dst)
179 dst_page++;
180 else
181 src_page++;
182 } while ((len -= l));
183 }
184}
185
186void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
187{
188 struct page **src_page, **dst_page;
189 int l;
190
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700191 hfs_dbg(BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!len)
193 return;
194 src += node->page_offset;
195 dst += node->page_offset;
196 if (dst > src) {
197 src += len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300198 src_page = node->page + (src >> PAGE_SHIFT);
199 src = (src & ~PAGE_MASK) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dst += len - 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300201 dst_page = node->page + (dst >> PAGE_SHIFT);
202 dst = (dst & ~PAGE_MASK) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (src == dst) {
205 while (src < len) {
206 memmove(kmap(*dst_page), kmap(*src_page), src);
207 kunmap(*src_page);
208 set_page_dirty(*dst_page);
209 kunmap(*dst_page);
210 len -= src;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300211 src = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 src_page--;
213 dst_page--;
214 }
215 src -= len;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200216 memmove(kmap(*dst_page) + src,
217 kmap(*src_page) + src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kunmap(*src_page);
219 set_page_dirty(*dst_page);
220 kunmap(*dst_page);
221 } else {
222 void *src_ptr, *dst_ptr;
223
224 do {
225 src_ptr = kmap(*src_page) + src;
226 dst_ptr = kmap(*dst_page) + dst;
227 if (src < dst) {
228 l = src;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300229 src = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 dst -= l;
231 } else {
232 l = dst;
233 src -= l;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 dst = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 l = min(len, l);
237 memmove(dst_ptr - l, src_ptr - l, l);
238 kunmap(*src_page);
239 set_page_dirty(*dst_page);
240 kunmap(*dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 if (dst == PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 dst_page--;
243 else
244 src_page--;
245 } while ((len -= l));
246 }
247 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300248 src_page = node->page + (src >> PAGE_SHIFT);
249 src &= ~PAGE_MASK;
250 dst_page = node->page + (dst >> PAGE_SHIFT);
251 dst &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (src == dst) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300254 l = min_t(int, len, PAGE_SIZE - src);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200255 memmove(kmap(*dst_page) + src,
256 kmap(*src_page) + src, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 kunmap(*src_page);
258 set_page_dirty(*dst_page);
259 kunmap(*dst_page);
260
261 while ((len -= l) != 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300262 l = min_t(int, len, PAGE_SIZE);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200263 memmove(kmap(*++dst_page),
264 kmap(*++src_page), l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 kunmap(*src_page);
266 set_page_dirty(*dst_page);
267 kunmap(*dst_page);
268 }
269 } else {
270 void *src_ptr, *dst_ptr;
271
272 do {
273 src_ptr = kmap(*src_page) + src;
274 dst_ptr = kmap(*dst_page) + dst;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300275 if (PAGE_SIZE - src <
276 PAGE_SIZE - dst) {
277 l = PAGE_SIZE - src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 src = 0;
279 dst += l;
280 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300281 l = PAGE_SIZE - dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 src += l;
283 dst = 0;
284 }
285 l = min(len, l);
286 memmove(dst_ptr, src_ptr, l);
287 kunmap(*src_page);
288 set_page_dirty(*dst_page);
289 kunmap(*dst_page);
290 if (!dst)
291 dst_page++;
292 else
293 src_page++;
294 } while ((len -= l));
295 }
296 }
297}
298
299void hfs_bnode_dump(struct hfs_bnode *node)
300{
301 struct hfs_bnode_desc desc;
302 __be32 cnid;
303 int i, off, key_off;
304
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700305 hfs_dbg(BNODE_MOD, "bnode: %d\n", node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 hfs_bnode_read(node, &desc, 0, sizeof(desc));
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700307 hfs_dbg(BNODE_MOD, "%d, %d, %d, %d, %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 be32_to_cpu(desc.next), be32_to_cpu(desc.prev),
309 desc.type, desc.height, be16_to_cpu(desc.num_recs));
310
311 off = node->tree->node_size - 2;
312 for (i = be16_to_cpu(desc.num_recs); i >= 0; off -= 2, i--) {
313 key_off = hfs_bnode_read_u16(node, off);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700314 hfs_dbg(BNODE_MOD, " %d", key_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (i && node->type == HFS_NODE_INDEX) {
316 int tmp;
317
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800318 if (node->tree->attributes & HFS_TREE_VARIDXKEYS ||
319 node->tree->cnid == HFSPLUS_ATTR_CNID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 tmp = hfs_bnode_read_u16(node, key_off) + 2;
321 else
322 tmp = node->tree->max_key_len + 2;
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700323 hfs_dbg_cont(BNODE_MOD, " (%d", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 hfs_bnode_read(node, &cnid, key_off + tmp, 4);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700325 hfs_dbg_cont(BNODE_MOD, ",%d)", be32_to_cpu(cnid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 } else if (i && node->type == HFS_NODE_LEAF) {
327 int tmp;
328
329 tmp = hfs_bnode_read_u16(node, key_off);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700330 hfs_dbg_cont(BNODE_MOD, " (%d)", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332 }
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700333 hfs_dbg_cont(BNODE_MOD, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336void hfs_bnode_unlink(struct hfs_bnode *node)
337{
338 struct hfs_btree *tree;
339 struct hfs_bnode *tmp;
340 __be32 cnid;
341
342 tree = node->tree;
343 if (node->prev) {
344 tmp = hfs_bnode_find(tree, node->prev);
345 if (IS_ERR(tmp))
346 return;
347 tmp->next = node->next;
348 cnid = cpu_to_be32(tmp->next);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200349 hfs_bnode_write(tmp, &cnid,
350 offsetof(struct hfs_bnode_desc, next), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 hfs_bnode_put(tmp);
352 } else if (node->type == HFS_NODE_LEAF)
353 tree->leaf_head = node->next;
354
355 if (node->next) {
356 tmp = hfs_bnode_find(tree, node->next);
357 if (IS_ERR(tmp))
358 return;
359 tmp->prev = node->prev;
360 cnid = cpu_to_be32(tmp->prev);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200361 hfs_bnode_write(tmp, &cnid,
362 offsetof(struct hfs_bnode_desc, prev), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 hfs_bnode_put(tmp);
364 } else if (node->type == HFS_NODE_LEAF)
365 tree->leaf_tail = node->prev;
366
Anton Salikhmetov21f22962010-12-16 18:08:39 +0200367 /* move down? */
Anton Salikhmetovb2837fc2010-12-16 18:08:41 +0200368 if (!node->prev && !node->next)
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700369 hfs_dbg(BNODE_MOD, "hfs_btree_del_level\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!node->parent) {
371 tree->root = 0;
372 tree->depth = 0;
373 }
374 set_bit(HFS_BNODE_DELETED, &node->flags);
375}
376
377static inline int hfs_bnode_hash(u32 num)
378{
379 num = (num >> 16) + num;
380 num += num >> 8;
381 return num & (NODE_HASH_SIZE - 1);
382}
383
384struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
385{
386 struct hfs_bnode *node;
387
388 if (cnid >= tree->node_count) {
Fabian Fredericke46707d2014-06-06 14:36:25 -0700389 pr_err("request for non-existent node %d in B*Tree\n",
390 cnid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return NULL;
392 }
393
394 for (node = tree->node_hash[hfs_bnode_hash(cnid)];
Anton Salikhmetovb2837fc2010-12-16 18:08:41 +0200395 node; node = node->next_hash)
396 if (node->this == cnid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return NULL;
399}
400
401static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
402{
403 struct super_block *sb;
404 struct hfs_bnode *node, *node2;
405 struct address_space *mapping;
406 struct page *page;
407 int size, block, i, hash;
408 loff_t off;
409
410 if (cnid >= tree->node_count) {
Fabian Fredericke46707d2014-06-06 14:36:25 -0700411 pr_err("request for non-existent node %d in B*Tree\n",
412 cnid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return NULL;
414 }
415
416 sb = tree->inode->i_sb;
417 size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
418 sizeof(struct page *);
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700419 node = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!node)
421 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 node->tree = tree;
423 node->this = cnid;
424 set_bit(HFS_BNODE_NEW, &node->flags);
425 atomic_set(&node->refcnt, 1);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700426 hfs_dbg(BNODE_REFS, "new_node(%d:%d): 1\n",
427 node->tree->cnid, node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 init_waitqueue_head(&node->lock_wq);
429 spin_lock(&tree->hash_lock);
430 node2 = hfs_bnode_findhash(tree, cnid);
431 if (!node2) {
432 hash = hfs_bnode_hash(cnid);
433 node->next_hash = tree->node_hash[hash];
434 tree->node_hash[hash] = node;
435 tree->node_hash_cnt++;
436 } else {
437 spin_unlock(&tree->hash_lock);
438 kfree(node);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200439 wait_event(node2->lock_wq,
440 !test_bit(HFS_BNODE_NEW, &node2->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return node2;
442 }
443 spin_unlock(&tree->hash_lock);
444
445 mapping = tree->inode->i_mapping;
446 off = (loff_t)cnid << tree->node_size_shift;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300447 block = off >> PAGE_SHIFT;
448 node->page_offset = off & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 for (i = 0; i < tree->pages_per_bnode; block++, i++) {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700450 page = read_mapping_page(mapping, block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (IS_ERR(page))
452 goto fail;
453 if (PageError(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300454 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto fail;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 node->page[i] = page;
458 }
459
460 return node;
461fail:
462 set_bit(HFS_BNODE_ERROR, &node->flags);
463 return node;
464}
465
466void hfs_bnode_unhash(struct hfs_bnode *node)
467{
468 struct hfs_bnode **p;
469
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700470 hfs_dbg(BNODE_REFS, "remove_node(%d:%d): %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 node->tree->cnid, node->this, atomic_read(&node->refcnt));
472 for (p = &node->tree->node_hash[hfs_bnode_hash(node->this)];
473 *p && *p != node; p = &(*p)->next_hash)
474 ;
Eric Sesterhenn0bf3ba52006-04-01 01:14:43 +0200475 BUG_ON(!*p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *p = node->next_hash;
477 node->tree->node_hash_cnt--;
478}
479
480/* Load a particular node out of a tree */
481struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
482{
483 struct hfs_bnode *node;
484 struct hfs_bnode_desc *desc;
485 int i, rec_off, off, next_off;
486 int entry_size, key_size;
487
488 spin_lock(&tree->hash_lock);
489 node = hfs_bnode_findhash(tree, num);
490 if (node) {
491 hfs_bnode_get(node);
492 spin_unlock(&tree->hash_lock);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200493 wait_event(node->lock_wq,
494 !test_bit(HFS_BNODE_NEW, &node->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (test_bit(HFS_BNODE_ERROR, &node->flags))
496 goto node_error;
497 return node;
498 }
499 spin_unlock(&tree->hash_lock);
500 node = __hfs_bnode_create(tree, num);
501 if (!node)
502 return ERR_PTR(-ENOMEM);
503 if (test_bit(HFS_BNODE_ERROR, &node->flags))
504 goto node_error;
505 if (!test_bit(HFS_BNODE_NEW, &node->flags))
506 return node;
507
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200508 desc = (struct hfs_bnode_desc *)(kmap(node->page[0]) +
509 node->page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 node->prev = be32_to_cpu(desc->prev);
511 node->next = be32_to_cpu(desc->next);
512 node->num_recs = be16_to_cpu(desc->num_recs);
513 node->type = desc->type;
514 node->height = desc->height;
515 kunmap(node->page[0]);
516
517 switch (node->type) {
518 case HFS_NODE_HEADER:
519 case HFS_NODE_MAP:
520 if (node->height != 0)
521 goto node_error;
522 break;
523 case HFS_NODE_LEAF:
524 if (node->height != 1)
525 goto node_error;
526 break;
527 case HFS_NODE_INDEX:
528 if (node->height <= 1 || node->height > tree->depth)
529 goto node_error;
530 break;
531 default:
532 goto node_error;
533 }
534
535 rec_off = tree->node_size - 2;
536 off = hfs_bnode_read_u16(node, rec_off);
537 if (off != sizeof(struct hfs_bnode_desc))
538 goto node_error;
539 for (i = 1; i <= node->num_recs; off = next_off, i++) {
540 rec_off -= 2;
541 next_off = hfs_bnode_read_u16(node, rec_off);
542 if (next_off <= off ||
543 next_off > tree->node_size ||
544 next_off & 1)
545 goto node_error;
546 entry_size = next_off - off;
547 if (node->type != HFS_NODE_INDEX &&
548 node->type != HFS_NODE_LEAF)
549 continue;
550 key_size = hfs_bnode_read_u16(node, off) + 2;
551 if (key_size >= entry_size || key_size & 1)
552 goto node_error;
553 }
554 clear_bit(HFS_BNODE_NEW, &node->flags);
555 wake_up(&node->lock_wq);
556 return node;
557
558node_error:
559 set_bit(HFS_BNODE_ERROR, &node->flags);
560 clear_bit(HFS_BNODE_NEW, &node->flags);
561 wake_up(&node->lock_wq);
562 hfs_bnode_put(node);
563 return ERR_PTR(-EIO);
564}
565
566void hfs_bnode_free(struct hfs_bnode *node)
567{
Anton Salikhmetov21f22962010-12-16 18:08:39 +0200568 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Anton Salikhmetov21f22962010-12-16 18:08:39 +0200570 for (i = 0; i < node->tree->pages_per_bnode; i++)
571 if (node->page[i])
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300572 put_page(node->page[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 kfree(node);
574}
575
576struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
577{
578 struct hfs_bnode *node;
579 struct page **pagep;
580 int i;
581
582 spin_lock(&tree->hash_lock);
583 node = hfs_bnode_findhash(tree, num);
584 spin_unlock(&tree->hash_lock);
585 if (node) {
Joe Perchesd6142672013-04-30 15:27:55 -0700586 pr_crit("new node %u already hashed?\n", num);
Roman Zippel634725a2006-01-18 17:43:05 -0800587 WARN_ON(1);
588 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 node = __hfs_bnode_create(tree, num);
591 if (!node)
592 return ERR_PTR(-ENOMEM);
593 if (test_bit(HFS_BNODE_ERROR, &node->flags)) {
594 hfs_bnode_put(node);
595 return ERR_PTR(-EIO);
596 }
597
598 pagep = node->page;
599 memset(kmap(*pagep) + node->page_offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300600 min_t(int, PAGE_SIZE, tree->node_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 set_page_dirty(*pagep);
602 kunmap(*pagep);
603 for (i = 1; i < tree->pages_per_bnode; i++) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300604 memset(kmap(*++pagep), 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 set_page_dirty(*pagep);
606 kunmap(*pagep);
607 }
608 clear_bit(HFS_BNODE_NEW, &node->flags);
609 wake_up(&node->lock_wq);
610
611 return node;
612}
613
614void hfs_bnode_get(struct hfs_bnode *node)
615{
616 if (node) {
617 atomic_inc(&node->refcnt);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700618 hfs_dbg(BNODE_REFS, "get_node(%d:%d): %d\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200619 node->tree->cnid, node->this,
620 atomic_read(&node->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622}
623
624/* Dispose of resources used by a node */
625void hfs_bnode_put(struct hfs_bnode *node)
626{
627 if (node) {
628 struct hfs_btree *tree = node->tree;
629 int i;
630
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700631 hfs_dbg(BNODE_REFS, "put_node(%d:%d): %d\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200632 node->tree->cnid, node->this,
633 atomic_read(&node->refcnt));
Eric Sesterhenn0bf3ba52006-04-01 01:14:43 +0200634 BUG_ON(!atomic_read(&node->refcnt));
Roman Zippela5e39852005-09-06 15:18:47 -0700635 if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 for (i = 0; i < tree->pages_per_bnode; i++) {
Roman Zippel74f9c9c2005-08-01 21:11:41 -0700638 if (!node->page[i])
639 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 mark_page_accessed(node->page[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
644 hfs_bnode_unhash(node);
645 spin_unlock(&tree->hash_lock);
Sergei Antonov2cd282a2014-06-06 14:36:28 -0700646 if (hfs_bnode_need_zeroout(tree))
647 hfs_bnode_clear(node, 0, tree->node_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 hfs_bmap_free(node);
649 hfs_bnode_free(node);
650 return;
651 }
652 spin_unlock(&tree->hash_lock);
653 }
654}
655
Sergei Antonov2cd282a2014-06-06 14:36:28 -0700656/*
657 * Unused nodes have to be zeroed if this is the catalog tree and
658 * a corresponding flag in the volume header is set.
659 */
660bool hfs_bnode_need_zeroout(struct hfs_btree *tree)
661{
662 struct super_block *sb = tree->inode->i_sb;
663 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
664 const u32 volume_attr = be32_to_cpu(sbi->s_vhdr->attributes);
665
666 return tree->cnid == HFSPLUS_CAT_CNID &&
667 volume_attr & HFSPLUS_VOL_UNUSED_NODE_FIX;
668}