bnode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfsplus/bnode.c
  4. *
  5. * Copyright (C) 2001
  6. * Brad Boyer (flar@allandria.com)
  7. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  8. *
  9. * Handle basic btree node operations
  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>
  16. #include "hfsplus_fs.h"
  17. #include "hfsplus_raw.h"
  18. /* Copy a specified range of bytes from the raw data of a node */
  19. void hfs_bnode_read(struct hfs_bnode *node, void *buf, int off, int len)
  20. {
  21. struct page **pagep;
  22. int l;
  23. off += node->page_offset;
  24. pagep = node->page + (off >> PAGE_SHIFT);
  25. off &= ~PAGE_MASK;
  26. l = min_t(int, len, PAGE_SIZE - off);
  27. memcpy(buf, kmap(*pagep) + off, l);
  28. kunmap(*pagep);
  29. while ((len -= l) != 0) {
  30. buf += l;
  31. l = min_t(int, len, PAGE_SIZE);
  32. memcpy(buf, kmap(*++pagep), l);
  33. kunmap(*pagep);
  34. }
  35. }
  36. u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
  37. {
  38. __be16 data;
  39. /* TODO: optimize later... */
  40. hfs_bnode_read(node, &data, off, 2);
  41. return be16_to_cpu(data);
  42. }
  43. u8 hfs_bnode_read_u8(struct hfs_bnode *node, int off)
  44. {
  45. u8 data;
  46. /* TODO: optimize later... */
  47. hfs_bnode_read(node, &data, off, 1);
  48. return data;
  49. }
  50. void hfs_bnode_read_key(struct hfs_bnode *node, void *key, int off)
  51. {
  52. struct hfs_btree *tree;
  53. int key_len;
  54. tree = node->tree;
  55. if (node->type == HFS_NODE_LEAF ||
  56. tree->attributes & HFS_TREE_VARIDXKEYS ||
  57. node->tree->cnid == HFSPLUS_ATTR_CNID)
  58. key_len = hfs_bnode_read_u16(node, off) + 2;
  59. else
  60. key_len = tree->max_key_len + 2;
  61. hfs_bnode_read(node, key, off, key_len);
  62. }
  63. void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
  64. {
  65. struct page **pagep;
  66. int l;
  67. off += node->page_offset;
  68. pagep = node->page + (off >> PAGE_SHIFT);
  69. off &= ~PAGE_MASK;
  70. l = min_t(int, len, PAGE_SIZE - off);
  71. memcpy(kmap(*pagep) + off, buf, l);
  72. set_page_dirty(*pagep);
  73. kunmap(*pagep);
  74. while ((len -= l) != 0) {
  75. buf += l;
  76. l = min_t(int, len, PAGE_SIZE);
  77. memcpy(kmap(*++pagep), buf, l);
  78. set_page_dirty(*pagep);
  79. kunmap(*pagep);
  80. }
  81. }
  82. void hfs_bnode_write_u16(struct hfs_bnode *node, int off, u16 data)
  83. {
  84. __be16 v = cpu_to_be16(data);
  85. /* TODO: optimize later... */
  86. hfs_bnode_write(node, &v, off, 2);
  87. }
  88. void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
  89. {
  90. struct page **pagep;
  91. int l;
  92. off += node->page_offset;
  93. pagep = node->page + (off >> PAGE_SHIFT);
  94. off &= ~PAGE_MASK;
  95. l = min_t(int, len, PAGE_SIZE - off);
  96. memset(kmap(*pagep) + off, 0, l);
  97. set_page_dirty(*pagep);
  98. kunmap(*pagep);
  99. while ((len -= l) != 0) {
  100. l = min_t(int, len, PAGE_SIZE);
  101. memset(kmap(*++pagep), 0, l);
  102. set_page_dirty(*pagep);
  103. kunmap(*pagep);
  104. }
  105. }
  106. void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
  107. struct hfs_bnode *src_node, int src, int len)
  108. {
  109. struct page **src_page, **dst_page;
  110. int l;
  111. hfs_dbg(BNODE_MOD, "copybytes: %u,%u,%u\n", dst, src, len);
  112. if (!len)
  113. return;
  114. src += src_node->page_offset;
  115. dst += dst_node->page_offset;
  116. src_page = src_node->page + (src >> PAGE_SHIFT);
  117. src &= ~PAGE_MASK;
  118. dst_page = dst_node->page + (dst >> PAGE_SHIFT);
  119. dst &= ~PAGE_MASK;
  120. if (src == dst) {
  121. l = min_t(int, len, PAGE_SIZE - src);
  122. memcpy(kmap(*dst_page) + src, kmap(*src_page) + src, l);
  123. kunmap(*src_page);
  124. set_page_dirty(*dst_page);
  125. kunmap(*dst_page);
  126. while ((len -= l) != 0) {
  127. l = min_t(int, len, PAGE_SIZE);
  128. memcpy(kmap(*++dst_page), kmap(*++src_page), l);
  129. kunmap(*src_page);
  130. set_page_dirty(*dst_page);
  131. kunmap(*dst_page);
  132. }
  133. } else {
  134. void *src_ptr, *dst_ptr;
  135. do {
  136. src_ptr = kmap(*src_page) + src;
  137. dst_ptr = kmap(*dst_page) + dst;
  138. if (PAGE_SIZE - src < PAGE_SIZE - dst) {
  139. l = PAGE_SIZE - src;
  140. src = 0;
  141. dst += l;
  142. } else {
  143. l = PAGE_SIZE - dst;
  144. src += l;
  145. dst = 0;
  146. }
  147. l = min(len, l);
  148. memcpy(dst_ptr, src_ptr, l);
  149. kunmap(*src_page);
  150. set_page_dirty(*dst_page);
  151. kunmap(*dst_page);
  152. if (!dst)
  153. dst_page++;
  154. else
  155. src_page++;
  156. } while ((len -= l));
  157. }
  158. }
  159. void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
  160. {
  161. struct page **src_page, **dst_page;
  162. int l;
  163. hfs_dbg(BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len);
  164. if (!len)
  165. return;
  166. src += node->page_offset;
  167. dst += node->page_offset;
  168. if (dst > src) {
  169. src += len - 1;
  170. src_page = node->page + (src >> PAGE_SHIFT);
  171. src = (src & ~PAGE_MASK) + 1;
  172. dst += len - 1;
  173. dst_page = node->page + (dst >> PAGE_SHIFT);
  174. dst = (dst & ~PAGE_MASK) + 1;
  175. if (src == dst) {
  176. while (src < len) {
  177. memmove(kmap(*dst_page), kmap(*src_page), src);
  178. kunmap(*src_page);
  179. set_page_dirty(*dst_page);
  180. kunmap(*dst_page);
  181. len -= src;
  182. src = PAGE_SIZE;
  183. src_page--;
  184. dst_page--;
  185. }
  186. src -= len;
  187. memmove(kmap(*dst_page) + src,
  188. kmap(*src_page) + src, len);
  189. kunmap(*src_page);
  190. set_page_dirty(*dst_page);
  191. kunmap(*dst_page);
  192. } else {
  193. void *src_ptr, *dst_ptr;
  194. do {
  195. src_ptr = kmap(*src_page) + src;
  196. dst_ptr = kmap(*dst_page) + dst;
  197. if (src < dst) {
  198. l = src;
  199. src = PAGE_SIZE;
  200. dst -= l;
  201. } else {
  202. l = dst;
  203. src -= l;
  204. dst = PAGE_SIZE;
  205. }
  206. l = min(len, l);
  207. memmove(dst_ptr - l, src_ptr - l, l);
  208. kunmap(*src_page);
  209. set_page_dirty(*dst_page);
  210. kunmap(*dst_page);
  211. if (dst == PAGE_SIZE)
  212. dst_page--;
  213. else
  214. src_page--;
  215. } while ((len -= l));
  216. }
  217. } else {
  218. src_page = node->page + (src >> PAGE_SHIFT);
  219. src &= ~PAGE_MASK;
  220. dst_page = node->page + (dst >> PAGE_SHIFT);
  221. dst &= ~PAGE_MASK;
  222. if (src == dst) {
  223. l = min_t(int, len, PAGE_SIZE - src);
  224. memmove(kmap(*dst_page) + src,
  225. kmap(*src_page) + src, l);
  226. kunmap(*src_page);
  227. set_page_dirty(*dst_page);
  228. kunmap(*dst_page);
  229. while ((len -= l) != 0) {
  230. l = min_t(int, len, PAGE_SIZE);
  231. memmove(kmap(*++dst_page),
  232. kmap(*++src_page), l);
  233. kunmap(*src_page);
  234. set_page_dirty(*dst_page);
  235. kunmap(*dst_page);
  236. }
  237. } else {
  238. void *src_ptr, *dst_ptr;
  239. do {
  240. src_ptr = kmap(*src_page) + src;
  241. dst_ptr = kmap(*dst_page) + dst;
  242. if (PAGE_SIZE - src <
  243. PAGE_SIZE - dst) {
  244. l = PAGE_SIZE - src;
  245. src = 0;
  246. dst += l;
  247. } else {
  248. l = PAGE_SIZE - dst;
  249. src += l;
  250. dst = 0;
  251. }
  252. l = min(len, l);
  253. memmove(dst_ptr, src_ptr, l);
  254. kunmap(*src_page);
  255. set_page_dirty(*dst_page);
  256. kunmap(*dst_page);
  257. if (!dst)
  258. dst_page++;
  259. else
  260. src_page++;
  261. } while ((len -= l));
  262. }
  263. }
  264. }
  265. void hfs_bnode_dump(struct hfs_bnode *node)
  266. {
  267. struct hfs_bnode_desc desc;
  268. __be32 cnid;
  269. int i, off, key_off;
  270. hfs_dbg(BNODE_MOD, "bnode: %d\n", node->this);
  271. hfs_bnode_read(node, &desc, 0, sizeof(desc));
  272. hfs_dbg(BNODE_MOD, "%d, %d, %d, %d, %d\n",
  273. be32_to_cpu(desc.next), be32_to_cpu(desc.prev),
  274. desc.type, desc.height, be16_to_cpu(desc.num_recs));
  275. off = node->tree->node_size - 2;
  276. for (i = be16_to_cpu(desc.num_recs); i >= 0; off -= 2, i--) {
  277. key_off = hfs_bnode_read_u16(node, off);
  278. hfs_dbg(BNODE_MOD, " %d", key_off);
  279. if (i && node->type == HFS_NODE_INDEX) {
  280. int tmp;
  281. if (node->tree->attributes & HFS_TREE_VARIDXKEYS ||
  282. node->tree->cnid == HFSPLUS_ATTR_CNID)
  283. tmp = hfs_bnode_read_u16(node, key_off) + 2;
  284. else
  285. tmp = node->tree->max_key_len + 2;
  286. hfs_dbg_cont(BNODE_MOD, " (%d", tmp);
  287. hfs_bnode_read(node, &cnid, key_off + tmp, 4);
  288. hfs_dbg_cont(BNODE_MOD, ",%d)", be32_to_cpu(cnid));
  289. } else if (i && node->type == HFS_NODE_LEAF) {
  290. int tmp;
  291. tmp = hfs_bnode_read_u16(node, key_off);
  292. hfs_dbg_cont(BNODE_MOD, " (%d)", tmp);
  293. }
  294. }
  295. hfs_dbg_cont(BNODE_MOD, "\n");
  296. }
  297. void hfs_bnode_unlink(struct hfs_bnode *node)
  298. {
  299. struct hfs_btree *tree;
  300. struct hfs_bnode *tmp;
  301. __be32 cnid;
  302. tree = node->tree;
  303. if (node->prev) {
  304. tmp = hfs_bnode_find(tree, node->prev);
  305. if (IS_ERR(tmp))
  306. return;
  307. tmp->next = node->next;
  308. cnid = cpu_to_be32(tmp->next);
  309. hfs_bnode_write(tmp, &cnid,
  310. offsetof(struct hfs_bnode_desc, next), 4);
  311. hfs_bnode_put(tmp);
  312. } else if (node->type == HFS_NODE_LEAF)
  313. tree->leaf_head = node->next;
  314. if (node->next) {
  315. tmp = hfs_bnode_find(tree, node->next);
  316. if (IS_ERR(tmp))
  317. return;
  318. tmp->prev = node->prev;
  319. cnid = cpu_to_be32(tmp->prev);
  320. hfs_bnode_write(tmp, &cnid,
  321. offsetof(struct hfs_bnode_desc, prev), 4);
  322. hfs_bnode_put(tmp);
  323. } else if (node->type == HFS_NODE_LEAF)
  324. tree->leaf_tail = node->prev;
  325. /* move down? */
  326. if (!node->prev && !node->next)
  327. hfs_dbg(BNODE_MOD, "hfs_btree_del_level\n");
  328. if (!node->parent) {
  329. tree->root = 0;
  330. tree->depth = 0;
  331. }
  332. set_bit(HFS_BNODE_DELETED, &node->flags);
  333. }
  334. static inline int hfs_bnode_hash(u32 num)
  335. {
  336. num = (num >> 16) + num;
  337. num += num >> 8;
  338. return num & (NODE_HASH_SIZE - 1);
  339. }
  340. struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
  341. {
  342. struct hfs_bnode *node;
  343. if (cnid >= tree->node_count) {
  344. pr_err("request for non-existent node %d in B*Tree\n",
  345. cnid);
  346. return NULL;
  347. }
  348. for (node = tree->node_hash[hfs_bnode_hash(cnid)];
  349. node; node = node->next_hash)
  350. if (node->this == cnid)
  351. return node;
  352. return NULL;
  353. }
  354. static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
  355. {
  356. struct hfs_bnode *node, *node2;
  357. struct address_space *mapping;
  358. struct page *page;
  359. int size, block, i, hash;
  360. loff_t off;
  361. if (cnid >= tree->node_count) {
  362. pr_err("request for non-existent node %d in B*Tree\n",
  363. cnid);
  364. return NULL;
  365. }
  366. size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
  367. sizeof(struct page *);
  368. node = kzalloc(size, GFP_KERNEL);
  369. if (!node)
  370. return NULL;
  371. node->tree = tree;
  372. node->this = cnid;
  373. set_bit(HFS_BNODE_NEW, &node->flags);
  374. atomic_set(&node->refcnt, 1);
  375. hfs_dbg(BNODE_REFS, "new_node(%d:%d): 1\n",
  376. node->tree->cnid, node->this);
  377. init_waitqueue_head(&node->lock_wq);
  378. spin_lock(&tree->hash_lock);
  379. node2 = hfs_bnode_findhash(tree, cnid);
  380. if (!node2) {
  381. hash = hfs_bnode_hash(cnid);
  382. node->next_hash = tree->node_hash[hash];
  383. tree->node_hash[hash] = node;
  384. tree->node_hash_cnt++;
  385. } else {
  386. spin_unlock(&tree->hash_lock);
  387. kfree(node);
  388. wait_event(node2->lock_wq,
  389. !test_bit(HFS_BNODE_NEW, &node2->flags));
  390. return node2;
  391. }
  392. spin_unlock(&tree->hash_lock);
  393. mapping = tree->inode->i_mapping;
  394. off = (loff_t)cnid << tree->node_size_shift;
  395. block = off >> PAGE_SHIFT;
  396. node->page_offset = off & ~PAGE_MASK;
  397. for (i = 0; i < tree->pages_per_bnode; block++, i++) {
  398. page = read_mapping_page(mapping, block, NULL);
  399. if (IS_ERR(page))
  400. goto fail;
  401. if (PageError(page)) {
  402. put_page(page);
  403. goto fail;
  404. }
  405. node->page[i] = page;
  406. }
  407. return node;
  408. fail:
  409. set_bit(HFS_BNODE_ERROR, &node->flags);
  410. return node;
  411. }
  412. void hfs_bnode_unhash(struct hfs_bnode *node)
  413. {
  414. struct hfs_bnode **p;
  415. hfs_dbg(BNODE_REFS, "remove_node(%d:%d): %d\n",
  416. node->tree->cnid, node->this, atomic_read(&node->refcnt));
  417. for (p = &node->tree->node_hash[hfs_bnode_hash(node->this)];
  418. *p && *p != node; p = &(*p)->next_hash)
  419. ;
  420. BUG_ON(!*p);
  421. *p = node->next_hash;
  422. node->tree->node_hash_cnt--;
  423. }
  424. /* Load a particular node out of a tree */
  425. struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
  426. {
  427. struct hfs_bnode *node;
  428. struct hfs_bnode_desc *desc;
  429. int i, rec_off, off, next_off;
  430. int entry_size, key_size;
  431. spin_lock(&tree->hash_lock);
  432. node = hfs_bnode_findhash(tree, num);
  433. if (node) {
  434. hfs_bnode_get(node);
  435. spin_unlock(&tree->hash_lock);
  436. wait_event(node->lock_wq,
  437. !test_bit(HFS_BNODE_NEW, &node->flags));
  438. if (test_bit(HFS_BNODE_ERROR, &node->flags))
  439. goto node_error;
  440. return node;
  441. }
  442. spin_unlock(&tree->hash_lock);
  443. node = __hfs_bnode_create(tree, num);
  444. if (!node)
  445. return ERR_PTR(-ENOMEM);
  446. if (test_bit(HFS_BNODE_ERROR, &node->flags))
  447. goto node_error;
  448. if (!test_bit(HFS_BNODE_NEW, &node->flags))
  449. return node;
  450. desc = (struct hfs_bnode_desc *)(kmap(node->page[0]) +
  451. node->page_offset);
  452. node->prev = be32_to_cpu(desc->prev);
  453. node->next = be32_to_cpu(desc->next);
  454. node->num_recs = be16_to_cpu(desc->num_recs);
  455. node->type = desc->type;
  456. node->height = desc->height;
  457. kunmap(node->page[0]);
  458. switch (node->type) {
  459. case HFS_NODE_HEADER:
  460. case HFS_NODE_MAP:
  461. if (node->height != 0)
  462. goto node_error;
  463. break;
  464. case HFS_NODE_LEAF:
  465. if (node->height != 1)
  466. goto node_error;
  467. break;
  468. case HFS_NODE_INDEX:
  469. if (node->height <= 1 || node->height > tree->depth)
  470. goto node_error;
  471. break;
  472. default:
  473. goto node_error;
  474. }
  475. rec_off = tree->node_size - 2;
  476. off = hfs_bnode_read_u16(node, rec_off);
  477. if (off != sizeof(struct hfs_bnode_desc))
  478. goto node_error;
  479. for (i = 1; i <= node->num_recs; off = next_off, i++) {
  480. rec_off -= 2;
  481. next_off = hfs_bnode_read_u16(node, rec_off);
  482. if (next_off <= off ||
  483. next_off > tree->node_size ||
  484. next_off & 1)
  485. goto node_error;
  486. entry_size = next_off - off;
  487. if (node->type != HFS_NODE_INDEX &&
  488. node->type != HFS_NODE_LEAF)
  489. continue;
  490. key_size = hfs_bnode_read_u16(node, off) + 2;
  491. if (key_size >= entry_size || key_size & 1)
  492. goto node_error;
  493. }
  494. clear_bit(HFS_BNODE_NEW, &node->flags);
  495. wake_up(&node->lock_wq);
  496. return node;
  497. node_error:
  498. set_bit(HFS_BNODE_ERROR, &node->flags);
  499. clear_bit(HFS_BNODE_NEW, &node->flags);
  500. wake_up(&node->lock_wq);
  501. hfs_bnode_put(node);
  502. return ERR_PTR(-EIO);
  503. }
  504. void hfs_bnode_free(struct hfs_bnode *node)
  505. {
  506. int i;
  507. for (i = 0; i < node->tree->pages_per_bnode; i++)
  508. if (node->page[i])
  509. put_page(node->page[i]);
  510. kfree(node);
  511. }
  512. struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
  513. {
  514. struct hfs_bnode *node;
  515. struct page **pagep;
  516. int i;
  517. spin_lock(&tree->hash_lock);
  518. node = hfs_bnode_findhash(tree, num);
  519. spin_unlock(&tree->hash_lock);
  520. if (node) {
  521. pr_crit("new node %u already hashed?\n", num);
  522. WARN_ON(1);
  523. return node;
  524. }
  525. node = __hfs_bnode_create(tree, num);
  526. if (!node)
  527. return ERR_PTR(-ENOMEM);
  528. if (test_bit(HFS_BNODE_ERROR, &node->flags)) {
  529. hfs_bnode_put(node);
  530. return ERR_PTR(-EIO);
  531. }
  532. pagep = node->page;
  533. memset(kmap(*pagep) + node->page_offset, 0,
  534. min_t(int, PAGE_SIZE, tree->node_size));
  535. set_page_dirty(*pagep);
  536. kunmap(*pagep);
  537. for (i = 1; i < tree->pages_per_bnode; i++) {
  538. memset(kmap(*++pagep), 0, PAGE_SIZE);
  539. set_page_dirty(*pagep);
  540. kunmap(*pagep);
  541. }
  542. clear_bit(HFS_BNODE_NEW, &node->flags);
  543. wake_up(&node->lock_wq);
  544. return node;
  545. }
  546. void hfs_bnode_get(struct hfs_bnode *node)
  547. {
  548. if (node) {
  549. atomic_inc(&node->refcnt);
  550. hfs_dbg(BNODE_REFS, "get_node(%d:%d): %d\n",
  551. node->tree->cnid, node->this,
  552. atomic_read(&node->refcnt));
  553. }
  554. }
  555. /* Dispose of resources used by a node */
  556. void hfs_bnode_put(struct hfs_bnode *node)
  557. {
  558. if (node) {
  559. struct hfs_btree *tree = node->tree;
  560. int i;
  561. hfs_dbg(BNODE_REFS, "put_node(%d:%d): %d\n",
  562. node->tree->cnid, node->this,
  563. atomic_read(&node->refcnt));
  564. BUG_ON(!atomic_read(&node->refcnt));
  565. if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
  566. return;
  567. for (i = 0; i < tree->pages_per_bnode; i++) {
  568. if (!node->page[i])
  569. continue;
  570. mark_page_accessed(node->page[i]);
  571. }
  572. if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
  573. hfs_bnode_unhash(node);
  574. spin_unlock(&tree->hash_lock);
  575. if (hfs_bnode_need_zeroout(tree))
  576. hfs_bnode_clear(node, 0, tree->node_size);
  577. hfs_bmap_free(node);
  578. hfs_bnode_free(node);
  579. return;
  580. }
  581. spin_unlock(&tree->hash_lock);
  582. }
  583. }
  584. /*
  585. * Unused nodes have to be zeroed if this is the catalog tree and
  586. * a corresponding flag in the volume header is set.
  587. */
  588. bool hfs_bnode_need_zeroout(struct hfs_btree *tree)
  589. {
  590. struct super_block *sb = tree->inode->i_sb;
  591. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  592. const u32 volume_attr = be32_to_cpu(sbi->s_vhdr->attributes);
  593. return tree->cnid == HFSPLUS_CAT_CNID &&
  594. volume_attr & HFSPLUS_VOL_UNUSED_NODE_FIX;
  595. }