PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/btrfs/free-space-cache.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 2693 lines | 2027 code | 385 blank | 281 comment | 362 complexity | af944864f119a8810e99a784b98d0beb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (C) 2008 Red Hat. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/pagemap.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/math64.h>
  22. #include "ctree.h"
  23. #include "free-space-cache.h"
  24. #include "transaction.h"
  25. #include "disk-io.h"
  26. #include "extent_io.h"
  27. #include "inode-map.h"
  28. #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  29. #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
  30. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  31. struct btrfs_free_space *info);
  32. static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
  33. struct btrfs_path *path,
  34. u64 offset)
  35. {
  36. struct btrfs_key key;
  37. struct btrfs_key location;
  38. struct btrfs_disk_key disk_key;
  39. struct btrfs_free_space_header *header;
  40. struct extent_buffer *leaf;
  41. struct inode *inode = NULL;
  42. int ret;
  43. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  44. key.offset = offset;
  45. key.type = 0;
  46. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  47. if (ret < 0)
  48. return ERR_PTR(ret);
  49. if (ret > 0) {
  50. btrfs_release_path(path);
  51. return ERR_PTR(-ENOENT);
  52. }
  53. leaf = path->nodes[0];
  54. header = btrfs_item_ptr(leaf, path->slots[0],
  55. struct btrfs_free_space_header);
  56. btrfs_free_space_key(leaf, header, &disk_key);
  57. btrfs_disk_key_to_cpu(&location, &disk_key);
  58. btrfs_release_path(path);
  59. inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
  60. if (!inode)
  61. return ERR_PTR(-ENOENT);
  62. if (IS_ERR(inode))
  63. return inode;
  64. if (is_bad_inode(inode)) {
  65. iput(inode);
  66. return ERR_PTR(-ENOENT);
  67. }
  68. inode->i_mapping->flags &= ~__GFP_FS;
  69. return inode;
  70. }
  71. struct inode *lookup_free_space_inode(struct btrfs_root *root,
  72. struct btrfs_block_group_cache
  73. *block_group, struct btrfs_path *path)
  74. {
  75. struct inode *inode = NULL;
  76. spin_lock(&block_group->lock);
  77. if (block_group->inode)
  78. inode = igrab(block_group->inode);
  79. spin_unlock(&block_group->lock);
  80. if (inode)
  81. return inode;
  82. inode = __lookup_free_space_inode(root, path,
  83. block_group->key.objectid);
  84. if (IS_ERR(inode))
  85. return inode;
  86. spin_lock(&block_group->lock);
  87. if (!btrfs_fs_closing(root->fs_info)) {
  88. block_group->inode = igrab(inode);
  89. block_group->iref = 1;
  90. }
  91. spin_unlock(&block_group->lock);
  92. return inode;
  93. }
  94. int __create_free_space_inode(struct btrfs_root *root,
  95. struct btrfs_trans_handle *trans,
  96. struct btrfs_path *path, u64 ino, u64 offset)
  97. {
  98. struct btrfs_key key;
  99. struct btrfs_disk_key disk_key;
  100. struct btrfs_free_space_header *header;
  101. struct btrfs_inode_item *inode_item;
  102. struct extent_buffer *leaf;
  103. int ret;
  104. ret = btrfs_insert_empty_inode(trans, root, path, ino);
  105. if (ret)
  106. return ret;
  107. leaf = path->nodes[0];
  108. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  109. struct btrfs_inode_item);
  110. btrfs_item_key(leaf, &disk_key, path->slots[0]);
  111. memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
  112. sizeof(*inode_item));
  113. btrfs_set_inode_generation(leaf, inode_item, trans->transid);
  114. btrfs_set_inode_size(leaf, inode_item, 0);
  115. btrfs_set_inode_nbytes(leaf, inode_item, 0);
  116. btrfs_set_inode_uid(leaf, inode_item, 0);
  117. btrfs_set_inode_gid(leaf, inode_item, 0);
  118. btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
  119. btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
  120. BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
  121. btrfs_set_inode_nlink(leaf, inode_item, 1);
  122. btrfs_set_inode_transid(leaf, inode_item, trans->transid);
  123. btrfs_set_inode_block_group(leaf, inode_item, offset);
  124. btrfs_mark_buffer_dirty(leaf);
  125. btrfs_release_path(path);
  126. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  127. key.offset = offset;
  128. key.type = 0;
  129. ret = btrfs_insert_empty_item(trans, root, path, &key,
  130. sizeof(struct btrfs_free_space_header));
  131. if (ret < 0) {
  132. btrfs_release_path(path);
  133. return ret;
  134. }
  135. leaf = path->nodes[0];
  136. header = btrfs_item_ptr(leaf, path->slots[0],
  137. struct btrfs_free_space_header);
  138. memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
  139. btrfs_set_free_space_key(leaf, header, &disk_key);
  140. btrfs_mark_buffer_dirty(leaf);
  141. btrfs_release_path(path);
  142. return 0;
  143. }
  144. int create_free_space_inode(struct btrfs_root *root,
  145. struct btrfs_trans_handle *trans,
  146. struct btrfs_block_group_cache *block_group,
  147. struct btrfs_path *path)
  148. {
  149. int ret;
  150. u64 ino;
  151. ret = btrfs_find_free_objectid(root, &ino);
  152. if (ret < 0)
  153. return ret;
  154. return __create_free_space_inode(root, trans, path, ino,
  155. block_group->key.objectid);
  156. }
  157. int btrfs_truncate_free_space_cache(struct btrfs_root *root,
  158. struct btrfs_trans_handle *trans,
  159. struct btrfs_path *path,
  160. struct inode *inode)
  161. {
  162. loff_t oldsize;
  163. int ret = 0;
  164. trans->block_rsv = root->orphan_block_rsv;
  165. ret = btrfs_block_rsv_check(trans, root,
  166. root->orphan_block_rsv,
  167. 0, 5);
  168. if (ret)
  169. return ret;
  170. oldsize = i_size_read(inode);
  171. btrfs_i_size_write(inode, 0);
  172. truncate_pagecache(inode, oldsize, 0);
  173. /*
  174. * We don't need an orphan item because truncating the free space cache
  175. * will never be split across transactions.
  176. */
  177. ret = btrfs_truncate_inode_items(trans, root, inode,
  178. 0, BTRFS_EXTENT_DATA_KEY);
  179. if (ret) {
  180. WARN_ON(1);
  181. return ret;
  182. }
  183. ret = btrfs_update_inode(trans, root, inode);
  184. return ret;
  185. }
  186. static int readahead_cache(struct inode *inode)
  187. {
  188. struct file_ra_state *ra;
  189. unsigned long last_index;
  190. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  191. if (!ra)
  192. return -ENOMEM;
  193. file_ra_state_init(ra, inode->i_mapping);
  194. last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  195. page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
  196. kfree(ra);
  197. return 0;
  198. }
  199. int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
  200. struct btrfs_free_space_ctl *ctl,
  201. struct btrfs_path *path, u64 offset)
  202. {
  203. struct btrfs_free_space_header *header;
  204. struct extent_buffer *leaf;
  205. struct page *page;
  206. u32 *checksums = NULL, *crc;
  207. char *disk_crcs = NULL;
  208. struct btrfs_key key;
  209. struct list_head bitmaps;
  210. u64 num_entries;
  211. u64 num_bitmaps;
  212. u64 generation;
  213. u32 cur_crc = ~(u32)0;
  214. pgoff_t index = 0;
  215. unsigned long first_page_offset;
  216. int num_checksums;
  217. int ret = 0;
  218. INIT_LIST_HEAD(&bitmaps);
  219. /* Nothing in the space cache, goodbye */
  220. if (!i_size_read(inode))
  221. goto out;
  222. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  223. key.offset = offset;
  224. key.type = 0;
  225. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  226. if (ret < 0)
  227. goto out;
  228. else if (ret > 0) {
  229. btrfs_release_path(path);
  230. ret = 0;
  231. goto out;
  232. }
  233. ret = -1;
  234. leaf = path->nodes[0];
  235. header = btrfs_item_ptr(leaf, path->slots[0],
  236. struct btrfs_free_space_header);
  237. num_entries = btrfs_free_space_entries(leaf, header);
  238. num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
  239. generation = btrfs_free_space_generation(leaf, header);
  240. btrfs_release_path(path);
  241. if (BTRFS_I(inode)->generation != generation) {
  242. printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
  243. " not match free space cache generation (%llu)\n",
  244. (unsigned long long)BTRFS_I(inode)->generation,
  245. (unsigned long long)generation);
  246. goto out;
  247. }
  248. if (!num_entries)
  249. goto out;
  250. /* Setup everything for doing checksumming */
  251. num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
  252. checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
  253. if (!checksums)
  254. goto out;
  255. first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
  256. disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
  257. if (!disk_crcs)
  258. goto out;
  259. ret = readahead_cache(inode);
  260. if (ret)
  261. goto out;
  262. while (1) {
  263. struct btrfs_free_space_entry *entry;
  264. struct btrfs_free_space *e;
  265. void *addr;
  266. unsigned long offset = 0;
  267. unsigned long start_offset = 0;
  268. int need_loop = 0;
  269. if (!num_entries && !num_bitmaps)
  270. break;
  271. if (index == 0) {
  272. start_offset = first_page_offset;
  273. offset = start_offset;
  274. }
  275. page = grab_cache_page(inode->i_mapping, index);
  276. if (!page)
  277. goto free_cache;
  278. if (!PageUptodate(page)) {
  279. btrfs_readpage(NULL, page);
  280. lock_page(page);
  281. if (!PageUptodate(page)) {
  282. unlock_page(page);
  283. page_cache_release(page);
  284. printk(KERN_ERR "btrfs: error reading free "
  285. "space cache\n");
  286. goto free_cache;
  287. }
  288. }
  289. addr = kmap(page);
  290. if (index == 0) {
  291. u64 *gen;
  292. memcpy(disk_crcs, addr, first_page_offset);
  293. gen = addr + (sizeof(u32) * num_checksums);
  294. if (*gen != BTRFS_I(inode)->generation) {
  295. printk(KERN_ERR "btrfs: space cache generation"
  296. " (%llu) does not match inode (%llu)\n",
  297. (unsigned long long)*gen,
  298. (unsigned long long)
  299. BTRFS_I(inode)->generation);
  300. kunmap(page);
  301. unlock_page(page);
  302. page_cache_release(page);
  303. goto free_cache;
  304. }
  305. crc = (u32 *)disk_crcs;
  306. }
  307. entry = addr + start_offset;
  308. /* First lets check our crc before we do anything fun */
  309. cur_crc = ~(u32)0;
  310. cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
  311. PAGE_CACHE_SIZE - start_offset);
  312. btrfs_csum_final(cur_crc, (char *)&cur_crc);
  313. if (cur_crc != *crc) {
  314. printk(KERN_ERR "btrfs: crc mismatch for page %lu\n",
  315. index);
  316. kunmap(page);
  317. unlock_page(page);
  318. page_cache_release(page);
  319. goto free_cache;
  320. }
  321. crc++;
  322. while (1) {
  323. if (!num_entries)
  324. break;
  325. need_loop = 1;
  326. e = kmem_cache_zalloc(btrfs_free_space_cachep,
  327. GFP_NOFS);
  328. if (!e) {
  329. kunmap(page);
  330. unlock_page(page);
  331. page_cache_release(page);
  332. goto free_cache;
  333. }
  334. e->offset = le64_to_cpu(entry->offset);
  335. e->bytes = le64_to_cpu(entry->bytes);
  336. if (!e->bytes) {
  337. kunmap(page);
  338. kmem_cache_free(btrfs_free_space_cachep, e);
  339. unlock_page(page);
  340. page_cache_release(page);
  341. goto free_cache;
  342. }
  343. if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
  344. spin_lock(&ctl->tree_lock);
  345. ret = link_free_space(ctl, e);
  346. spin_unlock(&ctl->tree_lock);
  347. if (ret) {
  348. printk(KERN_ERR "Duplicate entries in "
  349. "free space cache, dumping\n");
  350. kunmap(page);
  351. unlock_page(page);
  352. page_cache_release(page);
  353. goto free_cache;
  354. }
  355. } else {
  356. e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  357. if (!e->bitmap) {
  358. kunmap(page);
  359. kmem_cache_free(
  360. btrfs_free_space_cachep, e);
  361. unlock_page(page);
  362. page_cache_release(page);
  363. goto free_cache;
  364. }
  365. spin_lock(&ctl->tree_lock);
  366. ret = link_free_space(ctl, e);
  367. ctl->total_bitmaps++;
  368. ctl->op->recalc_thresholds(ctl);
  369. spin_unlock(&ctl->tree_lock);
  370. if (ret) {
  371. printk(KERN_ERR "Duplicate entries in "
  372. "free space cache, dumping\n");
  373. kunmap(page);
  374. unlock_page(page);
  375. page_cache_release(page);
  376. goto free_cache;
  377. }
  378. list_add_tail(&e->list, &bitmaps);
  379. }
  380. num_entries--;
  381. offset += sizeof(struct btrfs_free_space_entry);
  382. if (offset + sizeof(struct btrfs_free_space_entry) >=
  383. PAGE_CACHE_SIZE)
  384. break;
  385. entry++;
  386. }
  387. /*
  388. * We read an entry out of this page, we need to move on to the
  389. * next page.
  390. */
  391. if (need_loop) {
  392. kunmap(page);
  393. goto next;
  394. }
  395. /*
  396. * We add the bitmaps at the end of the entries in order that
  397. * the bitmap entries are added to the cache.
  398. */
  399. e = list_entry(bitmaps.next, struct btrfs_free_space, list);
  400. list_del_init(&e->list);
  401. memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
  402. kunmap(page);
  403. num_bitmaps--;
  404. next:
  405. unlock_page(page);
  406. page_cache_release(page);
  407. index++;
  408. }
  409. ret = 1;
  410. out:
  411. kfree(checksums);
  412. kfree(disk_crcs);
  413. return ret;
  414. free_cache:
  415. __btrfs_remove_free_space_cache(ctl);
  416. goto out;
  417. }
  418. int load_free_space_cache(struct btrfs_fs_info *fs_info,
  419. struct btrfs_block_group_cache *block_group)
  420. {
  421. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  422. struct btrfs_root *root = fs_info->tree_root;
  423. struct inode *inode;
  424. struct btrfs_path *path;
  425. int ret;
  426. bool matched;
  427. u64 used = btrfs_block_group_used(&block_group->item);
  428. /*
  429. * If we're unmounting then just return, since this does a search on the
  430. * normal root and not the commit root and we could deadlock.
  431. */
  432. if (btrfs_fs_closing(fs_info))
  433. return 0;
  434. /*
  435. * If this block group has been marked to be cleared for one reason or
  436. * another then we can't trust the on disk cache, so just return.
  437. */
  438. spin_lock(&block_group->lock);
  439. if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
  440. spin_unlock(&block_group->lock);
  441. return 0;
  442. }
  443. spin_unlock(&block_group->lock);
  444. path = btrfs_alloc_path();
  445. if (!path)
  446. return 0;
  447. inode = lookup_free_space_inode(root, block_group, path);
  448. if (IS_ERR(inode)) {
  449. btrfs_free_path(path);
  450. return 0;
  451. }
  452. ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
  453. path, block_group->key.objectid);
  454. btrfs_free_path(path);
  455. if (ret <= 0)
  456. goto out;
  457. spin_lock(&ctl->tree_lock);
  458. matched = (ctl->free_space == (block_group->key.offset - used -
  459. block_group->bytes_super));
  460. spin_unlock(&ctl->tree_lock);
  461. if (!matched) {
  462. __btrfs_remove_free_space_cache(ctl);
  463. printk(KERN_ERR "block group %llu has an wrong amount of free "
  464. "space\n", block_group->key.objectid);
  465. ret = -1;
  466. }
  467. out:
  468. if (ret < 0) {
  469. /* This cache is bogus, make sure it gets cleared */
  470. spin_lock(&block_group->lock);
  471. block_group->disk_cache_state = BTRFS_DC_CLEAR;
  472. spin_unlock(&block_group->lock);
  473. ret = 0;
  474. printk(KERN_ERR "btrfs: failed to load free space cache "
  475. "for block group %llu\n", block_group->key.objectid);
  476. }
  477. iput(inode);
  478. return ret;
  479. }
  480. int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
  481. struct btrfs_free_space_ctl *ctl,
  482. struct btrfs_block_group_cache *block_group,
  483. struct btrfs_trans_handle *trans,
  484. struct btrfs_path *path, u64 offset)
  485. {
  486. struct btrfs_free_space_header *header;
  487. struct extent_buffer *leaf;
  488. struct rb_node *node;
  489. struct list_head *pos, *n;
  490. struct page **pages;
  491. struct page *page;
  492. struct extent_state *cached_state = NULL;
  493. struct btrfs_free_cluster *cluster = NULL;
  494. struct extent_io_tree *unpin = NULL;
  495. struct list_head bitmap_list;
  496. struct btrfs_key key;
  497. u64 start, end, len;
  498. u64 bytes = 0;
  499. u32 *crc, *checksums;
  500. unsigned long first_page_offset;
  501. int index = 0, num_pages = 0;
  502. int entries = 0;
  503. int bitmaps = 0;
  504. int ret = -1;
  505. bool next_page = false;
  506. bool out_of_space = false;
  507. INIT_LIST_HEAD(&bitmap_list);
  508. node = rb_first(&ctl->free_space_offset);
  509. if (!node)
  510. return 0;
  511. if (!i_size_read(inode))
  512. return -1;
  513. num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  514. PAGE_CACHE_SHIFT;
  515. /* Since the first page has all of our checksums and our generation we
  516. * need to calculate the offset into the page that we can start writing
  517. * our entries.
  518. */
  519. first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
  520. filemap_write_and_wait(inode->i_mapping);
  521. btrfs_wait_ordered_range(inode, inode->i_size &
  522. ~(root->sectorsize - 1), (u64)-1);
  523. /* make sure we don't overflow that first page */
  524. if (first_page_offset + sizeof(struct btrfs_free_space_entry) >= PAGE_CACHE_SIZE) {
  525. /* this is really the same as running out of space, where we also return 0 */
  526. printk(KERN_CRIT "Btrfs: free space cache was too big for the crc page\n");
  527. ret = 0;
  528. goto out_update;
  529. }
  530. /* We need a checksum per page. */
  531. crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
  532. if (!crc)
  533. return -1;
  534. pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
  535. if (!pages) {
  536. kfree(crc);
  537. return -1;
  538. }
  539. /* Get the cluster for this block_group if it exists */
  540. if (block_group && !list_empty(&block_group->cluster_list))
  541. cluster = list_entry(block_group->cluster_list.next,
  542. struct btrfs_free_cluster,
  543. block_group_list);
  544. /*
  545. * We shouldn't have switched the pinned extents yet so this is the
  546. * right one
  547. */
  548. unpin = root->fs_info->pinned_extents;
  549. /*
  550. * Lock all pages first so we can lock the extent safely.
  551. *
  552. * NOTE: Because we hold the ref the entire time we're going to write to
  553. * the page find_get_page should never fail, so we don't do a check
  554. * after find_get_page at this point. Just putting this here so people
  555. * know and don't freak out.
  556. */
  557. while (index < num_pages) {
  558. page = grab_cache_page(inode->i_mapping, index);
  559. if (!page) {
  560. int i;
  561. for (i = 0; i < num_pages; i++) {
  562. unlock_page(pages[i]);
  563. page_cache_release(pages[i]);
  564. }
  565. goto out_free;
  566. }
  567. pages[index] = page;
  568. index++;
  569. }
  570. index = 0;
  571. lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
  572. 0, &cached_state, GFP_NOFS);
  573. /*
  574. * When searching for pinned extents, we need to start at our start
  575. * offset.
  576. */
  577. if (block_group)
  578. start = block_group->key.objectid;
  579. /* Write out the extent entries */
  580. do {
  581. struct btrfs_free_space_entry *entry;
  582. void *addr;
  583. unsigned long offset = 0;
  584. unsigned long start_offset = 0;
  585. next_page = false;
  586. if (index == 0) {
  587. start_offset = first_page_offset;
  588. offset = start_offset;
  589. }
  590. if (index >= num_pages) {
  591. out_of_space = true;
  592. break;
  593. }
  594. page = pages[index];
  595. addr = kmap(page);
  596. entry = addr + start_offset;
  597. memset(addr, 0, PAGE_CACHE_SIZE);
  598. while (node && !next_page) {
  599. struct btrfs_free_space *e;
  600. e = rb_entry(node, struct btrfs_free_space, offset_index);
  601. entries++;
  602. entry->offset = cpu_to_le64(e->offset);
  603. entry->bytes = cpu_to_le64(e->bytes);
  604. if (e->bitmap) {
  605. entry->type = BTRFS_FREE_SPACE_BITMAP;
  606. list_add_tail(&e->list, &bitmap_list);
  607. bitmaps++;
  608. } else {
  609. entry->type = BTRFS_FREE_SPACE_EXTENT;
  610. }
  611. node = rb_next(node);
  612. if (!node && cluster) {
  613. node = rb_first(&cluster->root);
  614. cluster = NULL;
  615. }
  616. offset += sizeof(struct btrfs_free_space_entry);
  617. if (offset + sizeof(struct btrfs_free_space_entry) >=
  618. PAGE_CACHE_SIZE)
  619. next_page = true;
  620. entry++;
  621. }
  622. /*
  623. * We want to add any pinned extents to our free space cache
  624. * so we don't leak the space
  625. */
  626. while (block_group && !next_page &&
  627. (start < block_group->key.objectid +
  628. block_group->key.offset)) {
  629. ret = find_first_extent_bit(unpin, start, &start, &end,
  630. EXTENT_DIRTY);
  631. if (ret) {
  632. ret = 0;
  633. break;
  634. }
  635. /* This pinned extent is out of our range */
  636. if (start >= block_group->key.objectid +
  637. block_group->key.offset)
  638. break;
  639. len = block_group->key.objectid +
  640. block_group->key.offset - start;
  641. len = min(len, end + 1 - start);
  642. entries++;
  643. entry->offset = cpu_to_le64(start);
  644. entry->bytes = cpu_to_le64(len);
  645. entry->type = BTRFS_FREE_SPACE_EXTENT;
  646. start = end + 1;
  647. offset += sizeof(struct btrfs_free_space_entry);
  648. if (offset + sizeof(struct btrfs_free_space_entry) >=
  649. PAGE_CACHE_SIZE)
  650. next_page = true;
  651. entry++;
  652. }
  653. *crc = ~(u32)0;
  654. *crc = btrfs_csum_data(root, addr + start_offset, *crc,
  655. PAGE_CACHE_SIZE - start_offset);
  656. kunmap(page);
  657. btrfs_csum_final(*crc, (char *)crc);
  658. crc++;
  659. bytes += PAGE_CACHE_SIZE;
  660. index++;
  661. } while (node || next_page);
  662. /* Write out the bitmaps */
  663. list_for_each_safe(pos, n, &bitmap_list) {
  664. void *addr;
  665. struct btrfs_free_space *entry =
  666. list_entry(pos, struct btrfs_free_space, list);
  667. if (index >= num_pages) {
  668. out_of_space = true;
  669. break;
  670. }
  671. page = pages[index];
  672. addr = kmap(page);
  673. memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
  674. *crc = ~(u32)0;
  675. *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
  676. kunmap(page);
  677. btrfs_csum_final(*crc, (char *)crc);
  678. crc++;
  679. bytes += PAGE_CACHE_SIZE;
  680. list_del_init(&entry->list);
  681. index++;
  682. }
  683. if (out_of_space) {
  684. btrfs_drop_pages(pages, num_pages);
  685. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  686. i_size_read(inode) - 1, &cached_state,
  687. GFP_NOFS);
  688. ret = 0;
  689. goto out_free;
  690. }
  691. /* Zero out the rest of the pages just to make sure */
  692. while (index < num_pages) {
  693. void *addr;
  694. page = pages[index];
  695. addr = kmap(page);
  696. memset(addr, 0, PAGE_CACHE_SIZE);
  697. kunmap(page);
  698. bytes += PAGE_CACHE_SIZE;
  699. index++;
  700. }
  701. /* Write the checksums and trans id to the first page */
  702. {
  703. void *addr;
  704. u64 *gen;
  705. page = pages[0];
  706. addr = kmap(page);
  707. memcpy(addr, checksums, sizeof(u32) * num_pages);
  708. gen = addr + (sizeof(u32) * num_pages);
  709. *gen = trans->transid;
  710. kunmap(page);
  711. }
  712. ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
  713. bytes, &cached_state);
  714. btrfs_drop_pages(pages, num_pages);
  715. unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
  716. i_size_read(inode) - 1, &cached_state, GFP_NOFS);
  717. if (ret) {
  718. ret = 0;
  719. goto out_free;
  720. }
  721. BTRFS_I(inode)->generation = trans->transid;
  722. filemap_write_and_wait(inode->i_mapping);
  723. key.objectid = BTRFS_FREE_SPACE_OBJECTID;
  724. key.offset = offset;
  725. key.type = 0;
  726. ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
  727. if (ret < 0) {
  728. ret = -1;
  729. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  730. EXTENT_DIRTY | EXTENT_DELALLOC |
  731. EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
  732. goto out_free;
  733. }
  734. leaf = path->nodes[0];
  735. if (ret > 0) {
  736. struct btrfs_key found_key;
  737. BUG_ON(!path->slots[0]);
  738. path->slots[0]--;
  739. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  740. if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
  741. found_key.offset != offset) {
  742. ret = -1;
  743. clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
  744. EXTENT_DIRTY | EXTENT_DELALLOC |
  745. EXTENT_DO_ACCOUNTING, 0, 0, NULL,
  746. GFP_NOFS);
  747. btrfs_release_path(path);
  748. goto out_free;
  749. }
  750. }
  751. header = btrfs_item_ptr(leaf, path->slots[0],
  752. struct btrfs_free_space_header);
  753. btrfs_set_free_space_entries(leaf, header, entries);
  754. btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
  755. btrfs_set_free_space_generation(leaf, header, trans->transid);
  756. btrfs_mark_buffer_dirty(leaf);
  757. btrfs_release_path(path);
  758. ret = 1;
  759. out_free:
  760. kfree(checksums);
  761. kfree(pages);
  762. out_update:
  763. if (ret != 1) {
  764. invalidate_inode_pages2_range(inode->i_mapping, 0, index);
  765. BTRFS_I(inode)->generation = 0;
  766. }
  767. btrfs_update_inode(trans, root, inode);
  768. return ret;
  769. }
  770. int btrfs_write_out_cache(struct btrfs_root *root,
  771. struct btrfs_trans_handle *trans,
  772. struct btrfs_block_group_cache *block_group,
  773. struct btrfs_path *path)
  774. {
  775. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  776. struct inode *inode;
  777. int ret = 0;
  778. root = root->fs_info->tree_root;
  779. spin_lock(&block_group->lock);
  780. if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
  781. spin_unlock(&block_group->lock);
  782. return 0;
  783. }
  784. spin_unlock(&block_group->lock);
  785. inode = lookup_free_space_inode(root, block_group, path);
  786. if (IS_ERR(inode))
  787. return 0;
  788. ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
  789. path, block_group->key.objectid);
  790. if (ret < 0) {
  791. spin_lock(&block_group->lock);
  792. block_group->disk_cache_state = BTRFS_DC_ERROR;
  793. spin_unlock(&block_group->lock);
  794. ret = 0;
  795. printk(KERN_ERR "btrfs: failed to write free space cace "
  796. "for block group %llu\n", block_group->key.objectid);
  797. }
  798. iput(inode);
  799. return ret;
  800. }
  801. static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
  802. u64 offset)
  803. {
  804. BUG_ON(offset < bitmap_start);
  805. offset -= bitmap_start;
  806. return (unsigned long)(div_u64(offset, unit));
  807. }
  808. static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
  809. {
  810. return (unsigned long)(div_u64(bytes, unit));
  811. }
  812. static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
  813. u64 offset)
  814. {
  815. u64 bitmap_start;
  816. u64 bytes_per_bitmap;
  817. bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
  818. bitmap_start = offset - ctl->start;
  819. bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
  820. bitmap_start *= bytes_per_bitmap;
  821. bitmap_start += ctl->start;
  822. return bitmap_start;
  823. }
  824. static int tree_insert_offset(struct rb_root *root, u64 offset,
  825. struct rb_node *node, int bitmap)
  826. {
  827. struct rb_node **p = &root->rb_node;
  828. struct rb_node *parent = NULL;
  829. struct btrfs_free_space *info;
  830. while (*p) {
  831. parent = *p;
  832. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  833. if (offset < info->offset) {
  834. p = &(*p)->rb_left;
  835. } else if (offset > info->offset) {
  836. p = &(*p)->rb_right;
  837. } else {
  838. /*
  839. * we could have a bitmap entry and an extent entry
  840. * share the same offset. If this is the case, we want
  841. * the extent entry to always be found first if we do a
  842. * linear search through the tree, since we want to have
  843. * the quickest allocation time, and allocating from an
  844. * extent is faster than allocating from a bitmap. So
  845. * if we're inserting a bitmap and we find an entry at
  846. * this offset, we want to go right, or after this entry
  847. * logically. If we are inserting an extent and we've
  848. * found a bitmap, we want to go left, or before
  849. * logically.
  850. */
  851. if (bitmap) {
  852. if (info->bitmap) {
  853. WARN_ON_ONCE(1);
  854. return -EEXIST;
  855. }
  856. p = &(*p)->rb_right;
  857. } else {
  858. if (!info->bitmap) {
  859. WARN_ON_ONCE(1);
  860. return -EEXIST;
  861. }
  862. p = &(*p)->rb_left;
  863. }
  864. }
  865. }
  866. rb_link_node(node, parent, p);
  867. rb_insert_color(node, root);
  868. return 0;
  869. }
  870. /*
  871. * searches the tree for the given offset.
  872. *
  873. * fuzzy - If this is set, then we are trying to make an allocation, and we just
  874. * want a section that has at least bytes size and comes at or after the given
  875. * offset.
  876. */
  877. static struct btrfs_free_space *
  878. tree_search_offset(struct btrfs_free_space_ctl *ctl,
  879. u64 offset, int bitmap_only, int fuzzy)
  880. {
  881. struct rb_node *n = ctl->free_space_offset.rb_node;
  882. struct btrfs_free_space *entry, *prev = NULL;
  883. /* find entry that is closest to the 'offset' */
  884. while (1) {
  885. if (!n) {
  886. entry = NULL;
  887. break;
  888. }
  889. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  890. prev = entry;
  891. if (offset < entry->offset)
  892. n = n->rb_left;
  893. else if (offset > entry->offset)
  894. n = n->rb_right;
  895. else
  896. break;
  897. }
  898. if (bitmap_only) {
  899. if (!entry)
  900. return NULL;
  901. if (entry->bitmap)
  902. return entry;
  903. /*
  904. * bitmap entry and extent entry may share same offset,
  905. * in that case, bitmap entry comes after extent entry.
  906. */
  907. n = rb_next(n);
  908. if (!n)
  909. return NULL;
  910. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  911. if (entry->offset != offset)
  912. return NULL;
  913. WARN_ON(!entry->bitmap);
  914. return entry;
  915. } else if (entry) {
  916. if (entry->bitmap) {
  917. /*
  918. * if previous extent entry covers the offset,
  919. * we should return it instead of the bitmap entry
  920. */
  921. n = &entry->offset_index;
  922. while (1) {
  923. n = rb_prev(n);
  924. if (!n)
  925. break;
  926. prev = rb_entry(n, struct btrfs_free_space,
  927. offset_index);
  928. if (!prev->bitmap) {
  929. if (prev->offset + prev->bytes > offset)
  930. entry = prev;
  931. break;
  932. }
  933. }
  934. }
  935. return entry;
  936. }
  937. if (!prev)
  938. return NULL;
  939. /* find last entry before the 'offset' */
  940. entry = prev;
  941. if (entry->offset > offset) {
  942. n = rb_prev(&entry->offset_index);
  943. if (n) {
  944. entry = rb_entry(n, struct btrfs_free_space,
  945. offset_index);
  946. BUG_ON(entry->offset > offset);
  947. } else {
  948. if (fuzzy)
  949. return entry;
  950. else
  951. return NULL;
  952. }
  953. }
  954. if (entry->bitmap) {
  955. n = &entry->offset_index;
  956. while (1) {
  957. n = rb_prev(n);
  958. if (!n)
  959. break;
  960. prev = rb_entry(n, struct btrfs_free_space,
  961. offset_index);
  962. if (!prev->bitmap) {
  963. if (prev->offset + prev->bytes > offset)
  964. return prev;
  965. break;
  966. }
  967. }
  968. if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
  969. return entry;
  970. } else if (entry->offset + entry->bytes > offset)
  971. return entry;
  972. if (!fuzzy)
  973. return NULL;
  974. while (1) {
  975. if (entry->bitmap) {
  976. if (entry->offset + BITS_PER_BITMAP *
  977. ctl->unit > offset)
  978. break;
  979. } else {
  980. if (entry->offset + entry->bytes > offset)
  981. break;
  982. }
  983. n = rb_next(&entry->offset_index);
  984. if (!n)
  985. return NULL;
  986. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  987. }
  988. return entry;
  989. }
  990. static inline void
  991. __unlink_free_space(struct btrfs_free_space_ctl *ctl,
  992. struct btrfs_free_space *info)
  993. {
  994. rb_erase(&info->offset_index, &ctl->free_space_offset);
  995. ctl->free_extents--;
  996. }
  997. static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
  998. struct btrfs_free_space *info)
  999. {
  1000. __unlink_free_space(ctl, info);
  1001. ctl->free_space -= info->bytes;
  1002. }
  1003. static int link_free_space(struct btrfs_free_space_ctl *ctl,
  1004. struct btrfs_free_space *info)
  1005. {
  1006. int ret = 0;
  1007. BUG_ON(!info->bitmap && !info->bytes);
  1008. ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
  1009. &info->offset_index, (info->bitmap != NULL));
  1010. if (ret)
  1011. return ret;
  1012. ctl->free_space += info->bytes;
  1013. ctl->free_extents++;
  1014. return ret;
  1015. }
  1016. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  1017. {
  1018. struct btrfs_block_group_cache *block_group = ctl->private;
  1019. u64 max_bytes;
  1020. u64 bitmap_bytes;
  1021. u64 extent_bytes;
  1022. u64 size = block_group->key.offset;
  1023. u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
  1024. int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
  1025. BUG_ON(ctl->total_bitmaps > max_bitmaps);
  1026. /*
  1027. * The goal is to keep the total amount of memory used per 1gb of space
  1028. * at or below 32k, so we need to adjust how much memory we allow to be
  1029. * used by extent based free space tracking
  1030. */
  1031. if (size < 1024 * 1024 * 1024)
  1032. max_bytes = MAX_CACHE_BYTES_PER_GIG;
  1033. else
  1034. max_bytes = MAX_CACHE_BYTES_PER_GIG *
  1035. div64_u64(size, 1024 * 1024 * 1024);
  1036. /*
  1037. * we want to account for 1 more bitmap than what we have so we can make
  1038. * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
  1039. * we add more bitmaps.
  1040. */
  1041. bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
  1042. if (bitmap_bytes >= max_bytes) {
  1043. ctl->extents_thresh = 0;
  1044. return;
  1045. }
  1046. /*
  1047. * we want the extent entry threshold to always be at most 1/2 the maxw
  1048. * bytes we can have, or whatever is less than that.
  1049. */
  1050. extent_bytes = max_bytes - bitmap_bytes;
  1051. extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
  1052. ctl->extents_thresh =
  1053. div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
  1054. }
  1055. static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
  1056. struct btrfs_free_space *info, u64 offset,
  1057. u64 bytes)
  1058. {
  1059. unsigned long start, count;
  1060. start = offset_to_bit(info->offset, ctl->unit, offset);
  1061. count = bytes_to_bits(bytes, ctl->unit);
  1062. BUG_ON(start + count > BITS_PER_BITMAP);
  1063. bitmap_clear(info->bitmap, start, count);
  1064. info->bytes -= bytes;
  1065. ctl->free_space -= bytes;
  1066. }
  1067. static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
  1068. struct btrfs_free_space *info, u64 offset,
  1069. u64 bytes)
  1070. {
  1071. unsigned long start, count;
  1072. start = offset_to_bit(info->offset, ctl->unit, offset);
  1073. count = bytes_to_bits(bytes, ctl->unit);
  1074. BUG_ON(start + count > BITS_PER_BITMAP);
  1075. bitmap_set(info->bitmap, start, count);
  1076. info->bytes += bytes;
  1077. ctl->free_space += bytes;
  1078. }
  1079. static int search_bitmap(struct btrfs_free_space_ctl *ctl,
  1080. struct btrfs_free_space *bitmap_info, u64 *offset,
  1081. u64 *bytes)
  1082. {
  1083. unsigned long found_bits = 0;
  1084. unsigned long bits, i;
  1085. unsigned long next_zero;
  1086. i = offset_to_bit(bitmap_info->offset, ctl->unit,
  1087. max_t(u64, *offset, bitmap_info->offset));
  1088. bits = bytes_to_bits(*bytes, ctl->unit);
  1089. for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
  1090. i < BITS_PER_BITMAP;
  1091. i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
  1092. next_zero = find_next_zero_bit(bitmap_info->bitmap,
  1093. BITS_PER_BITMAP, i);
  1094. if ((next_zero - i) >= bits) {
  1095. found_bits = next_zero - i;
  1096. break;
  1097. }
  1098. i = next_zero;
  1099. }
  1100. if (found_bits) {
  1101. *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
  1102. *bytes = (u64)(found_bits) * ctl->unit;
  1103. return 0;
  1104. }
  1105. return -1;
  1106. }
  1107. static struct btrfs_free_space *
  1108. find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
  1109. {
  1110. struct btrfs_free_space *entry;
  1111. struct rb_node *node;
  1112. int ret;
  1113. if (!ctl->free_space_offset.rb_node)
  1114. return NULL;
  1115. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
  1116. if (!entry)
  1117. return NULL;
  1118. for (node = &entry->offset_index; node; node = rb_next(node)) {
  1119. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1120. if (entry->bytes < *bytes)
  1121. continue;
  1122. if (entry->bitmap) {
  1123. ret = search_bitmap(ctl, entry, offset, bytes);
  1124. if (!ret)
  1125. return entry;
  1126. continue;
  1127. }
  1128. *offset = entry->offset;
  1129. *bytes = entry->bytes;
  1130. return entry;
  1131. }
  1132. return NULL;
  1133. }
  1134. static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
  1135. struct btrfs_free_space *info, u64 offset)
  1136. {
  1137. info->offset = offset_to_bitmap(ctl, offset);
  1138. info->bytes = 0;
  1139. link_free_space(ctl, info);
  1140. ctl->total_bitmaps++;
  1141. ctl->op->recalc_thresholds(ctl);
  1142. }
  1143. static void free_bitmap(struct btrfs_free_space_ctl *ctl,
  1144. struct btrfs_free_space *bitmap_info)
  1145. {
  1146. unlink_free_space(ctl, bitmap_info);
  1147. kfree(bitmap_info->bitmap);
  1148. kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
  1149. ctl->total_bitmaps--;
  1150. ctl->op->recalc_thresholds(ctl);
  1151. }
  1152. static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
  1153. struct btrfs_free_space *bitmap_info,
  1154. u64 *offset, u64 *bytes)
  1155. {
  1156. u64 end;
  1157. u64 search_start, search_bytes;
  1158. int ret;
  1159. again:
  1160. end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
  1161. /*
  1162. * XXX - this can go away after a few releases.
  1163. *
  1164. * since the only user of btrfs_remove_free_space is the tree logging
  1165. * stuff, and the only way to test that is under crash conditions, we
  1166. * want to have this debug stuff here just in case somethings not
  1167. * working. Search the bitmap for the space we are trying to use to
  1168. * make sure its actually there. If its not there then we need to stop
  1169. * because something has gone wrong.
  1170. */
  1171. search_start = *offset;
  1172. search_bytes = *bytes;
  1173. search_bytes = min(search_bytes, end - search_start + 1);
  1174. ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
  1175. BUG_ON(ret < 0 || search_start != *offset);
  1176. if (*offset > bitmap_info->offset && *offset + *bytes > end) {
  1177. bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
  1178. *bytes -= end - *offset + 1;
  1179. *offset = end + 1;
  1180. } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
  1181. bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
  1182. *bytes = 0;
  1183. }
  1184. if (*bytes) {
  1185. struct rb_node *next = rb_next(&bitmap_info->offset_index);
  1186. if (!bitmap_info->bytes)
  1187. free_bitmap(ctl, bitmap_info);
  1188. /*
  1189. * no entry after this bitmap, but we still have bytes to
  1190. * remove, so something has gone wrong.
  1191. */
  1192. if (!next)
  1193. return -EINVAL;
  1194. bitmap_info = rb_entry(next, struct btrfs_free_space,
  1195. offset_index);
  1196. /*
  1197. * if the next entry isn't a bitmap we need to return to let the
  1198. * extent stuff do its work.
  1199. */
  1200. if (!bitmap_info->bitmap)
  1201. return -EAGAIN;
  1202. /*
  1203. * Ok the next item is a bitmap, but it may not actually hold
  1204. * the information for the rest of this free space stuff, so
  1205. * look for it, and if we don't find it return so we can try
  1206. * everything over again.
  1207. */
  1208. search_start = *offset;
  1209. search_bytes = *bytes;
  1210. ret = search_bitmap(ctl, bitmap_info, &search_start,
  1211. &search_bytes);
  1212. if (ret < 0 || search_start != *offset)
  1213. return -EAGAIN;
  1214. goto again;
  1215. } else if (!bitmap_info->bytes)
  1216. free_bitmap(ctl, bitmap_info);
  1217. return 0;
  1218. }
  1219. static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
  1220. struct btrfs_free_space *info, u64 offset,
  1221. u64 bytes)
  1222. {
  1223. u64 bytes_to_set = 0;
  1224. u64 end;
  1225. end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
  1226. bytes_to_set = min(end - offset, bytes);
  1227. bitmap_set_bits(ctl, info, offset, bytes_to_set);
  1228. return bytes_to_set;
  1229. }
  1230. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  1231. struct btrfs_free_space *info)
  1232. {
  1233. struct btrfs_block_group_cache *block_group = ctl->private;
  1234. /*
  1235. * If we are below the extents threshold then we can add this as an
  1236. * extent, and don't have to deal with the bitmap
  1237. */
  1238. if (ctl->free_extents < ctl->extents_thresh) {
  1239. /*
  1240. * If this block group has some small extents we don't want to
  1241. * use up all of our free slots in the cache with them, we want
  1242. * to reserve them to larger extents, however if we have plent
  1243. * of cache left then go ahead an dadd them, no sense in adding
  1244. * the overhead of a bitmap if we don't have to.
  1245. */
  1246. if (info->bytes <= block_group->sectorsize * 4) {
  1247. if (ctl->free_extents * 2 <= ctl->extents_thresh)
  1248. return false;
  1249. } else {
  1250. return false;
  1251. }
  1252. }
  1253. /*
  1254. * some block groups are so tiny they can't be enveloped by a bitmap, so
  1255. * don't even bother to create a bitmap for this
  1256. */
  1257. if (BITS_PER_BITMAP * block_group->sectorsize >
  1258. block_group->key.offset)
  1259. return false;
  1260. return true;
  1261. }
  1262. static struct btrfs_free_space_op free_space_op = {
  1263. .recalc_thresholds = recalculate_thresholds,
  1264. .use_bitmap = use_bitmap,
  1265. };
  1266. static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
  1267. struct btrfs_free_space *info)
  1268. {
  1269. struct btrfs_free_space *bitmap_info;
  1270. struct btrfs_block_group_cache *block_group = NULL;
  1271. int added = 0;
  1272. u64 bytes, offset, bytes_added;
  1273. int ret;
  1274. bytes = info->bytes;
  1275. offset = info->offset;
  1276. if (!ctl->op->use_bitmap(ctl, info))
  1277. return 0;
  1278. if (ctl->op == &free_space_op)
  1279. block_group = ctl->private;
  1280. again:
  1281. /*
  1282. * Since we link bitmaps right into the cluster we need to see if we
  1283. * have a cluster here, and if so and it has our bitmap we need to add
  1284. * the free space to that bitmap.
  1285. */
  1286. if (block_group && !list_empty(&block_group->cluster_list)) {
  1287. struct btrfs_free_cluster *cluster;
  1288. struct rb_node *node;
  1289. struct btrfs_free_space *entry;
  1290. cluster = list_entry(block_group->cluster_list.next,
  1291. struct btrfs_free_cluster,
  1292. block_group_list);
  1293. spin_lock(&cluster->lock);
  1294. node = rb_first(&cluster->root);
  1295. if (!node) {
  1296. spin_unlock(&cluster->lock);
  1297. goto no_cluster_bitmap;
  1298. }
  1299. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1300. if (!entry->bitmap) {
  1301. spin_unlock(&cluster->lock);
  1302. goto no_cluster_bitmap;
  1303. }
  1304. if (entry->offset == offset_to_bitmap(ctl, offset)) {
  1305. bytes_added = add_bytes_to_bitmap(ctl, entry,
  1306. offset, bytes);
  1307. bytes -= bytes_added;
  1308. offset += bytes_added;
  1309. }
  1310. spin_unlock(&cluster->lock);
  1311. if (!bytes) {
  1312. ret = 1;
  1313. goto out;
  1314. }
  1315. }
  1316. no_cluster_bitmap:
  1317. bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1318. 1, 0);
  1319. if (!bitmap_info) {
  1320. BUG_ON(added);
  1321. goto new_bitmap;
  1322. }
  1323. bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
  1324. bytes -= bytes_added;
  1325. offset += bytes_added;
  1326. added = 0;
  1327. if (!bytes) {
  1328. ret = 1;
  1329. goto out;
  1330. } else
  1331. goto again;
  1332. new_bitmap:
  1333. if (info && info->bitmap) {
  1334. add_new_bitmap(ctl, info, offset);
  1335. added = 1;
  1336. info = NULL;
  1337. goto again;
  1338. } else {
  1339. spin_unlock(&ctl->tree_lock);
  1340. /* no pre-allocated info, allocate a new one */
  1341. if (!info) {
  1342. info = kmem_cache_zalloc(btrfs_free_space_cachep,
  1343. GFP_NOFS);
  1344. if (!info) {
  1345. spin_lock(&ctl->tree_lock);
  1346. ret = -ENOMEM;
  1347. goto out;
  1348. }
  1349. }
  1350. /* allocate the bitmap */
  1351. info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
  1352. spin_lock(&ctl->tree_lock);
  1353. if (!info->bitmap) {
  1354. ret = -ENOMEM;
  1355. goto out;
  1356. }
  1357. goto again;
  1358. }
  1359. out:
  1360. if (info) {
  1361. if (info->bitmap)
  1362. kfree(info->bitmap);
  1363. kmem_cache_free(btrfs_free_space_cachep, info);
  1364. }
  1365. return ret;
  1366. }
  1367. static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
  1368. struct btrfs_free_space *info, bool update_stat)
  1369. {
  1370. struct btrfs_free_space *left_info;
  1371. struct btrfs_free_space *right_info;
  1372. bool merged = false;
  1373. u64 offset = info->offset;
  1374. u64 bytes = info->bytes;
  1375. /*
  1376. * first we want to see if there is free space adjacent to the range we
  1377. * are adding, if there is remove that struct and add a new one to
  1378. * cover the entire range
  1379. */
  1380. right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
  1381. if (right_info && rb_prev(&right_info->offset_index))
  1382. left_info = rb_entry(rb_prev(&right_info->offset_index),
  1383. struct btrfs_free_space, offset_index);
  1384. else
  1385. left_info = tree_search_offset(ctl, offset - 1, 0, 0);
  1386. if (right_info && !right_info->bitmap) {
  1387. if (update_stat)
  1388. unlink_free_space(ctl, right_info);
  1389. else
  1390. __unlink_free_space(ctl, right_info);
  1391. info->bytes += right_info->bytes;
  1392. kmem_cache_free(btrfs_free_space_cachep, right_info);
  1393. merged = true;
  1394. }
  1395. if (left_info && !left_info->bitmap &&
  1396. left_info->offset + left_info->bytes == offset) {
  1397. if (update_stat)
  1398. unlink_free_space(ctl, left_info);
  1399. else
  1400. __unlink_free_space(ctl, left_info);
  1401. info->offset = left_info->offset;
  1402. info->bytes += left_info->bytes;
  1403. kmem_cache_free(btrfs_free_space_cachep, left_info);
  1404. merged = true;
  1405. }
  1406. return merged;
  1407. }
  1408. int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
  1409. u64 offset, u64 bytes)
  1410. {
  1411. struct btrfs_free_space *info;
  1412. int ret = 0;
  1413. info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
  1414. if (!info)
  1415. return -ENOMEM;
  1416. info->offset = offset;
  1417. info->bytes = bytes;
  1418. spin_lock(&ctl->tree_lock);
  1419. if (try_merge_free_space(ctl, info, true))
  1420. goto link;
  1421. /*
  1422. * There was no extent directly to the left or right of this new
  1423. * extent then we know we're going to have to allocate a new extent, so
  1424. * before we do that see if we need to drop this into a bitmap
  1425. */
  1426. ret = insert_into_bitmap(ctl, info);
  1427. if (ret < 0) {
  1428. goto out;
  1429. } else if (ret) {
  1430. ret = 0;
  1431. goto out;
  1432. }
  1433. link:
  1434. ret = link_free_space(ctl, info);
  1435. if (ret)
  1436. kmem_cache_free(btrfs_free_space_cachep, info);
  1437. out:
  1438. spin_unlock(&ctl->tree_lock);
  1439. if (ret) {
  1440. printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
  1441. BUG_ON(ret == -EEXIST);
  1442. }
  1443. return ret;
  1444. }
  1445. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  1446. u64 offset, u64 bytes)
  1447. {
  1448. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1449. struct btrfs_free_space *info;
  1450. struct btrfs_free_space *next_info = NULL;
  1451. int ret = 0;
  1452. spin_lock(&ctl->tree_lock);
  1453. again:
  1454. info = tree_search_offset(ctl, offset, 0, 0);
  1455. if (!info) {
  1456. /*
  1457. * oops didn't find an extent that matched the space we wanted
  1458. * to remove, look for a bitmap instead
  1459. */
  1460. info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
  1461. 1, 0);
  1462. if (!info) {
  1463. WARN_ON(1);
  1464. goto out_lock;
  1465. }
  1466. }
  1467. if (info->bytes < bytes && rb_next(&info->offset_index)) {
  1468. u64 end;
  1469. next_info = rb_entry(rb_next(&info->offset_index),
  1470. struct btrfs_free_space,
  1471. offset_index);
  1472. if (next_info->bitmap)
  1473. end = next_info->offset +
  1474. BITS_PER_BITMAP * ctl->unit - 1;
  1475. else
  1476. end = next_info->offset + next_info->bytes;
  1477. if (next_info->bytes < bytes ||
  1478. next_info->offset > offset || offset > end) {
  1479. printk(KERN_CRIT "Found free space at %llu, size %llu,"
  1480. " trying to use %llu\n",
  1481. (unsigned long long)info->offset,
  1482. (unsigned long long)info->bytes,
  1483. (unsigned long long)bytes);
  1484. WARN_ON(1);
  1485. ret = -EINVAL;
  1486. goto out_lock;
  1487. }
  1488. info = next_info;
  1489. }
  1490. if (info->bytes == bytes) {
  1491. unlink_free_space(ctl, info);
  1492. if (info->bitmap) {
  1493. kfree(info->bitmap);
  1494. ctl->total_bitmaps--;
  1495. }
  1496. kmem_cache_free(btrfs_free_space_cachep, info);
  1497. goto out_lock;
  1498. }
  1499. if (!info->bitmap && info->offset == offset) {
  1500. unlink_free_space(ctl, info);
  1501. info->offset += bytes;
  1502. info->bytes -= bytes;
  1503. link_free_space(ctl, info);
  1504. goto out_lock;
  1505. }
  1506. if (!info->bitmap && info->offset <= offset &&
  1507. info->offset + info->bytes >= offset + bytes) {
  1508. u64 old_start = info->offset;
  1509. /*
  1510. * we're freeing space in the middle of the info,
  1511. * this can happen during tree log replay
  1512. *
  1513. * first unlink the old info and then
  1514. * insert it again after the hole we're creating
  1515. */
  1516. unlink_free_space(ctl, info);
  1517. if (offset + bytes < info->offset + info->bytes) {
  1518. u64 old_end = info->offset + info->bytes;
  1519. info->offset = offset + bytes;
  1520. info->bytes = old_end - info->offset;
  1521. ret = link_free_space(ctl, info);
  1522. WARN_ON(ret);
  1523. if (ret)
  1524. goto out_lock;
  1525. } else {
  1526. /* the hole we're creating ends at the end
  1527. * of the info struct, just free the info
  1528. */
  1529. kmem_cache_free(btrfs_free_space_cachep, info);
  1530. }
  1531. spin_unlock(&ctl->tree_lock);
  1532. /* step two, insert a new info struct to cover
  1533. * anything before the hole
  1534. */
  1535. ret = btrfs_add_free_space(block_group, old_start,
  1536. offset - old_start);
  1537. WARN_ON(ret);
  1538. goto out;
  1539. }
  1540. ret = remove_from_bitmap(ctl, info, &offset, &bytes);
  1541. if (ret == -EAGAIN)
  1542. goto again;
  1543. BUG_ON(ret);
  1544. out_lock:
  1545. spin_unlock(&ctl->tree_lock);
  1546. out:
  1547. return ret;
  1548. }
  1549. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  1550. u64 bytes)
  1551. {
  1552. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1553. struct btrfs_free_space *info;
  1554. struct rb_node *n;
  1555. int count = 0;
  1556. for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
  1557. info = rb_entry(n, struct btrfs_free_space, offset_index);
  1558. if (info->bytes >= bytes)
  1559. count++;
  1560. printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
  1561. (unsigned long long)info->offset,
  1562. (unsigned long long)info->bytes,
  1563. (info->bitmap) ? "yes" : "no");
  1564. }
  1565. printk(KERN_INFO "block group has cluster?: %s\n",
  1566. list_empty(&block_group->cluster_list) ? "no" : "yes");
  1567. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  1568. "\n", count);
  1569. }
  1570. void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
  1571. {
  1572. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1573. spin_lock_init(&ctl->tree_lock);
  1574. ctl->unit = block_group->sectorsize;
  1575. ctl->start = block_group->key.objectid;
  1576. ctl->private = block_group;
  1577. ctl->op = &free_space_op;
  1578. /*
  1579. * we only want to have 32k of ram per block group for keeping
  1580. * track of free space, and if we pass 1/2 of that we want to
  1581. * start converting things over to using bitmaps
  1582. */
  1583. ctl->extents_thresh = ((1024 * 32) / 2) /
  1584. sizeof(struct btrfs_free_space);
  1585. }
  1586. /*
  1587. * for a given cluster, put all of its extents back into the free
  1588. * space cache. If the block group passed doesn't match the block group
  1589. * pointed to by the cluster, someone else raced in and freed the
  1590. * cluster already. In that case, we just return without changing anything
  1591. */
  1592. static int
  1593. __btrfs_return_cluster_to_free_space(
  1594. struct btrfs_block_group_cache *block_group,
  1595. struct btrfs_free_cluster *cluster)
  1596. {
  1597. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1598. struct btrfs_free_space *entry;
  1599. struct rb_node *node;
  1600. spin_lock(&cluster->lock);
  1601. if (cluster->block_group != block_group)
  1602. goto out;
  1603. cluster->block_group = NULL;
  1604. cluster->window_start = 0;
  1605. list_del_init(&cluster->block_group_list);
  1606. node = rb_first(&cluster->root);
  1607. while (node) {
  1608. bool bitmap;
  1609. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1610. node = rb_next(&entry->offset_index);
  1611. rb_erase(&entry->offset_index, &cluster->root);
  1612. bitmap = (entry->bitmap != NULL);
  1613. if (!bitmap)
  1614. try_merge_free_space(ctl, entry, false);
  1615. tree_insert_offset(&ctl->free_space_offset,
  1616. entry->offset, &entry->offset_index, bitmap);
  1617. }
  1618. cluster->root = RB_ROOT;
  1619. out:
  1620. spin_unlock(&cluster->lock);
  1621. btrfs_put_block_group(block_group);
  1622. return 0;
  1623. }
  1624. void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
  1625. {
  1626. struct btrfs_free_space *info;
  1627. struct rb_node *node;
  1628. while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
  1629. info = rb_entry(node, struct btrfs_free_space, offset_index);
  1630. if (!info->bitmap) {
  1631. unlink_free_space(ctl, info);
  1632. kmem_cache_free(btrfs_free_space_cachep, info);
  1633. } else {
  1634. free_bitmap(ctl, info);
  1635. }
  1636. if (need_resched()) {
  1637. spin_unlock(&ctl->tree_lock);
  1638. cond_resched();
  1639. spin_lock(&ctl->tree_lock);
  1640. }
  1641. }
  1642. }
  1643. void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
  1644. {
  1645. spin_lock(&ctl->tree_lock);
  1646. __btrfs_remove_free_space_cache_locked(ctl);
  1647. spin_unlock(&ctl->tree_lock);
  1648. }
  1649. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  1650. {
  1651. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1652. struct btrfs_free_cluster *cluster;
  1653. struct list_head *head;
  1654. spin_lock(&ctl->tree_lock);
  1655. while ((head = block_group->cluster_list.next) !=
  1656. &block_group->cluster_list) {
  1657. cluster = list_entry(head, struct btrfs_free_cluster,
  1658. block_group_list);
  1659. WARN_ON(cluster->block_group != block_group);
  1660. __btrfs_return_cluster_to_free_space(block_group, cluster);
  1661. if (need_resched()) {
  1662. spin_unlock(&ctl->tree_lock);
  1663. cond_resched();
  1664. spin_lock(&ctl->tree_lock);
  1665. }
  1666. }
  1667. __btrfs_remove_free_space_cache_locked(ctl);
  1668. spin_unlock(&ctl->tree_lock);
  1669. }
  1670. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  1671. u64 offset, u64 bytes, u64 empty_size)
  1672. {
  1673. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1674. struct btrfs_free_space *entry = NULL;
  1675. u64 bytes_search = bytes + empty_size;
  1676. u64 ret = 0;
  1677. spin_lock(&ctl->tree_lock);
  1678. entry = find_free_space(ctl, &offset, &bytes_search);
  1679. if (!entry)
  1680. goto out;
  1681. ret = offset;
  1682. if (entry->bitmap) {
  1683. bitmap_clear_bits(ctl, entry, offset, bytes);
  1684. if (!entry->bytes)
  1685. free_bitmap(ctl, entry);
  1686. } else {
  1687. unlink_free_space(ctl, entry);
  1688. entry->offset += bytes;
  1689. entry->bytes -= bytes;
  1690. if (!entry->bytes)
  1691. kmem_cache_free(btrfs_free_space_cachep, entry);
  1692. else
  1693. link_free_space(ctl, entry);
  1694. }
  1695. out:
  1696. spin_unlock(&ctl->tree_lock);
  1697. return ret;
  1698. }
  1699. /*
  1700. * given a cluster, put all of its extents back into the free space
  1701. * cache. If a block group is passed, this function will only free
  1702. * a cluster that belongs to the passed block group.
  1703. *
  1704. * Otherwise, it'll get a reference on the block group pointed to by the
  1705. * cluster and remove the cluster from it.
  1706. */
  1707. int btrfs_return_cluster_to_free_space(
  1708. struct btrfs_block_group_cache *block_group,
  1709. struct btrfs_free_cluster *cluster)
  1710. {
  1711. struct btrfs_free_space_ctl *ctl;
  1712. int ret;
  1713. /* first, get a safe pointer to the block group */
  1714. spin_lock(&cluster->lock);
  1715. if (!block_group) {
  1716. block_group = cluster->block_group;
  1717. if (!block_group) {
  1718. spin_unlock(&cluster->lock);
  1719. return 0;
  1720. }
  1721. } else if (cluster->block_group != block_group) {
  1722. /* someone else has already freed it don't redo their work */
  1723. spin_unlock(&cluster->lock);
  1724. return 0;
  1725. }
  1726. atomic_inc(&block_group->count);
  1727. spin_unlock(&cluster->lock);
  1728. ctl = block_group->free_space_ctl;
  1729. /* now return any extents the cluster had on it */
  1730. spin_lock(&ctl->tree_lock);
  1731. ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
  1732. spin_unlock(&ctl->tree_lock);
  1733. /* finally drop our ref */
  1734. btrfs_put_block_group(block_group);
  1735. return ret;
  1736. }
  1737. static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
  1738. struct btrfs_free_cluster *cluster,
  1739. struct btrfs_free_space *entry,
  1740. u64 bytes, u64 min_start)
  1741. {
  1742. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1743. int err;
  1744. u64 search_start = cluster->window_start;
  1745. u64 search_bytes = bytes;
  1746. u64 ret = 0;
  1747. search_start = min_start;
  1748. search_bytes = bytes;
  1749. err = search_bitmap(ctl, entry, &search_start, &search_bytes);
  1750. if (err)
  1751. return 0;
  1752. ret = search_start;
  1753. bitmap_clear_bits(ctl, entry, ret, bytes);
  1754. return ret;
  1755. }
  1756. /*
  1757. * given a cluster, try to allocate 'bytes' from it, returns 0
  1758. * if it couldn't find anything suitably large, or a logical disk offset
  1759. * if things worked out
  1760. */
  1761. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  1762. struct btrfs_free_cluster *cluster, u64 bytes,
  1763. u64 min_start)
  1764. {
  1765. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1766. struct btrfs_free_space *entry = NULL;
  1767. struct rb_node *node;
  1768. u64 ret = 0;
  1769. spin_lock(&cluster->lock);
  1770. if (bytes > cluster->max_size)
  1771. goto out;
  1772. if (cluster->block_group != block_group)
  1773. goto out;
  1774. node = rb_first(&cluster->root);
  1775. if (!node)
  1776. goto out;
  1777. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1778. while(1) {
  1779. if (entry->bytes < bytes ||
  1780. (!entry->bitmap && entry->offset < min_start)) {
  1781. node = rb_next(&entry->offset_index);
  1782. if (!node)
  1783. break;
  1784. entry = rb_entry(node, struct btrfs_free_space,
  1785. offset_index);
  1786. continue;
  1787. }
  1788. if (entry->bitmap) {
  1789. ret = btrfs_alloc_from_bitmap(block_group,
  1790. cluster, entry, bytes,
  1791. min_start);
  1792. if (ret == 0) {
  1793. node = rb_next(&entry->offset_index);
  1794. if (!node)
  1795. break;
  1796. entry = rb_entry(node, struct btrfs_free_space,
  1797. offset_index);
  1798. continue;
  1799. }
  1800. } else {
  1801. ret = entry->offset;
  1802. entry->offset += bytes;
  1803. entry->bytes -= bytes;
  1804. }
  1805. if (entry->bytes == 0)
  1806. rb_erase(&entry->offset_index, &cluster->root);
  1807. break;
  1808. }
  1809. out:
  1810. spin_unlock(&cluster->lock);
  1811. if (!ret)
  1812. return 0;
  1813. spin_lock(&ctl->tree_lock);
  1814. ctl->free_space -= bytes;
  1815. if (entry->bytes == 0) {
  1816. ctl->free_extents--;
  1817. if (entry->bitmap) {
  1818. kfree(entry->bitmap);
  1819. ctl->total_bitmaps--;
  1820. ctl->op->recalc_thresholds(ctl);
  1821. }
  1822. kmem_cache_free(btrfs_free_space_cachep, entry);
  1823. }
  1824. spin_unlock(&ctl->tree_lock);
  1825. return ret;
  1826. }
  1827. static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
  1828. struct btrfs_free_space *entry,
  1829. struct btrfs_free_cluster *cluster,
  1830. u64 offset, u64 bytes, u64 min_bytes)
  1831. {
  1832. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1833. unsigned long next_zero;
  1834. unsigned long i;
  1835. unsigned long search_bits;
  1836. unsigned long total_bits;
  1837. unsigned long found_bits;
  1838. unsigned long start = 0;
  1839. unsigned long total_found = 0;
  1840. int ret;
  1841. bool found = false;
  1842. i = offset_to_bit(entry->offset, block_group->sectorsize,
  1843. max_t(u64, offset, entry->offset));
  1844. search_bits = bytes_to_bits(bytes, block_group->sectorsize);
  1845. total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
  1846. again:
  1847. found_bits = 0;
  1848. for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
  1849. i < BITS_PER_BITMAP;
  1850. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
  1851. next_zero = find_next_zero_bit(entry->bitmap,
  1852. BITS_PER_BITMAP, i);
  1853. if (next_zero - i >= search_bits) {
  1854. found_bits = next_zero - i;
  1855. break;
  1856. }
  1857. i = next_zero;
  1858. }
  1859. if (!found_bits)
  1860. return -ENOSPC;
  1861. if (!found) {
  1862. start = i;
  1863. found = true;
  1864. }
  1865. total_found += found_bits;
  1866. if (cluster->max_size < found_bits * block_group->sectorsize)
  1867. cluster->max_size = found_bits * block_group->sectorsize;
  1868. if (total_found < total_bits) {
  1869. i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
  1870. if (i - start > total_bits * 2) {
  1871. total_found = 0;
  1872. cluster->max_size = 0;
  1873. found = false;
  1874. }
  1875. goto again;
  1876. }
  1877. cluster->window_start = start * block_group->sectorsize +
  1878. entry->offset;
  1879. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  1880. ret = tree_insert_offset(&cluster->root, entry->offset,
  1881. &entry->offset_index, 1);
  1882. BUG_ON(ret);
  1883. return 0;
  1884. }
  1885. /*
  1886. * This searches the block group for just extents to fill the cluster with.
  1887. */
  1888. static noinline int
  1889. setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
  1890. struct btrfs_free_cluster *cluster,
  1891. struct list_head *bitmaps, u64 offset, u64 bytes,
  1892. u64 min_bytes)
  1893. {
  1894. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1895. struct btrfs_free_space *first = NULL;
  1896. struct btrfs_free_space *entry = NULL;
  1897. struct btrfs_free_space *prev = NULL;
  1898. struct btrfs_free_space *last;
  1899. struct rb_node *node;
  1900. u64 window_start;
  1901. u64 window_free;
  1902. u64 max_extent;
  1903. u64 max_gap = 128 * 1024;
  1904. entry = tree_search_offset(ctl, offset, 0, 1);
  1905. if (!entry)
  1906. return -ENOSPC;
  1907. /*
  1908. * We don't want bitmaps, so just move along until we find a normal
  1909. * extent entry.
  1910. */
  1911. while (entry->bitmap) {
  1912. if (list_empty(&entry->list))
  1913. list_add_tail(&entry->list, bitmaps);
  1914. node = rb_next(&entry->offset_index);
  1915. if (!node)
  1916. return -ENOSPC;
  1917. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1918. }
  1919. window_start = entry->offset;
  1920. window_free = entry->bytes;
  1921. max_extent = entry->bytes;
  1922. first = entry;
  1923. last = entry;
  1924. prev = entry;
  1925. while (window_free <= min_bytes) {
  1926. node = rb_next(&entry->offset_index);
  1927. if (!node)
  1928. return -ENOSPC;
  1929. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1930. if (entry->bitmap) {
  1931. if (list_empty(&entry->list))
  1932. list_add_tail(&entry->list, bitmaps);
  1933. continue;
  1934. }
  1935. /*
  1936. * we haven't filled the empty size and the window is
  1937. * very large. reset and try again
  1938. */
  1939. if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
  1940. entry->offset - window_start > (min_bytes * 2)) {
  1941. first = entry;
  1942. window_start = entry->offset;
  1943. window_free = entry->bytes;
  1944. last = entry;
  1945. max_extent = entry->bytes;
  1946. } else {
  1947. last = entry;
  1948. window_free += entry->bytes;
  1949. if (entry->bytes > max_extent)
  1950. max_extent = entry->bytes;
  1951. }
  1952. prev = entry;
  1953. }
  1954. cluster->window_start = first->offset;
  1955. node = &first->offset_index;
  1956. /*
  1957. * now we've found our entries, pull them out of the free space
  1958. * cache and put them into the cluster rbtree
  1959. */
  1960. do {
  1961. int ret;
  1962. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  1963. node = rb_next(&entry->offset_index);
  1964. if (entry->bitmap)
  1965. continue;
  1966. rb_erase(&entry->offset_index, &ctl->free_space_offset);
  1967. ret = tree_insert_offset(&cluster->root, entry->offset,
  1968. &entry->offset_index, 0);
  1969. BUG_ON(ret);
  1970. } while (node && entry != last);
  1971. cluster->max_size = max_extent;
  1972. return 0;
  1973. }
  1974. /*
  1975. * This specifically looks for bitmaps that may work in the cluster, we assume
  1976. * that we have already failed to find extents that will work.
  1977. */
  1978. static noinline int
  1979. setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
  1980. struct btrfs_free_cluster *cluster,
  1981. struct list_head *bitmaps, u64 offset, u64 bytes,
  1982. u64 min_bytes)
  1983. {
  1984. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  1985. struct btrfs_free_space *entry;
  1986. struct rb_node *node;
  1987. int ret = -ENOSPC;
  1988. if (ctl->total_bitmaps == 0)
  1989. return -ENOSPC;
  1990. /*
  1991. * First check our cached list of bitmaps and see if there is an entry
  1992. * here that will work.
  1993. */
  1994. list_for_each_entry(entry, bitmaps, list) {
  1995. if (entry->bytes < min_bytes)
  1996. continue;
  1997. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  1998. bytes, min_bytes);
  1999. if (!ret)
  2000. return 0;
  2001. }
  2002. /*
  2003. * If we do have entries on our list and we are here then we didn't find
  2004. * anything, so go ahead and get the next entry after the last entry in
  2005. * this list and start the search from there.
  2006. */
  2007. if (!list_empty(bitmaps)) {
  2008. entry = list_entry(bitmaps->prev, struct btrfs_free_space,
  2009. list);
  2010. node = rb_next(&entry->offset_index);
  2011. if (!node)
  2012. return -ENOSPC;
  2013. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2014. goto search;
  2015. }
  2016. entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
  2017. if (!entry)
  2018. return -ENOSPC;
  2019. search:
  2020. node = &entry->offset_index;
  2021. do {
  2022. entry = rb_entry(node, struct btrfs_free_space, offset_index);
  2023. node = rb_next(&entry->offset_index);
  2024. if (!entry->bitmap)
  2025. continue;
  2026. if (entry->bytes < min_bytes)
  2027. continue;
  2028. ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
  2029. bytes, min_bytes);
  2030. } while (ret && node);
  2031. return ret;
  2032. }
  2033. /*
  2034. * here we try to find a cluster of blocks in a block group. The goal
  2035. * is to find at least bytes free and up to empty_size + bytes free.
  2036. * We might not find them all in one contiguous area.
  2037. *
  2038. * returns zero and sets up cluster if things worked out, otherwise
  2039. * it returns -enospc
  2040. */
  2041. int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
  2042. struct btrfs_root *root,
  2043. struct btrfs_block_group_cache *block_group,
  2044. struct btrfs_free_cluster *cluster,
  2045. u64 offset, u64 bytes, u64 empty_size)
  2046. {
  2047. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2048. struct list_head bitmaps;
  2049. struct btrfs_free_space *entry, *tmp;
  2050. u64 min_bytes;
  2051. int ret;
  2052. /* for metadata, allow allocates with more holes */
  2053. if (btrfs_test_opt(root, SSD_SPREAD)) {
  2054. min_bytes = bytes + empty_size;
  2055. } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
  2056. /*
  2057. * we want to do larger allocations when we are
  2058. * flushing out the delayed refs, it helps prevent
  2059. * making more work as we go along.
  2060. */
  2061. if (trans->transaction->delayed_refs.flushing)
  2062. min_bytes = max(bytes, (bytes + empty_size) >> 1);
  2063. else
  2064. min_bytes = max(bytes, (bytes + empty_size) >> 4);
  2065. } else
  2066. min_bytes = max(bytes, (bytes + empty_size) >> 2);
  2067. spin_lock(&ctl->tree_lock);
  2068. /*
  2069. * If we know we don't have enough space to make a cluster don't even
  2070. * bother doing all the work to try and find one.
  2071. */
  2072. if (ctl->free_space < min_bytes) {
  2073. spin_unlock(&ctl->tree_lock);
  2074. return -ENOSPC;
  2075. }
  2076. spin_lock(&cluster->lock);
  2077. /* someone already found a cluster, hooray */
  2078. if (cluster->block_group) {
  2079. ret = 0;
  2080. goto out;
  2081. }
  2082. INIT_LIST_HEAD(&bitmaps);
  2083. ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
  2084. bytes, min_bytes);
  2085. if (ret)
  2086. ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
  2087. offset, bytes, min_bytes);
  2088. /* Clear our temporary list */
  2089. list_for_each_entry_safe(entry, tmp, &bitmaps, list)
  2090. list_del_init(&entry->list);
  2091. if (!ret) {
  2092. atomic_inc(&block_group->count);
  2093. list_add_tail(&cluster->block_group_list,
  2094. &block_group->cluster_list);
  2095. cluster->block_group = block_group;
  2096. }
  2097. out:
  2098. spin_unlock(&cluster->lock);
  2099. spin_unlock(&ctl->tree_lock);
  2100. return ret;
  2101. }
  2102. /*
  2103. * simple code to zero out a cluster
  2104. */
  2105. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
  2106. {
  2107. spin_lock_init(&cluster->lock);
  2108. spin_lock_init(&cluster->refill_lock);
  2109. cluster->root = RB_ROOT;
  2110. cluster->max_size = 0;
  2111. INIT_LIST_HEAD(&cluster->block_group_list);
  2112. cluster->block_group = NULL;
  2113. }
  2114. int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
  2115. u64 *trimmed, u64 start, u64 end, u64 minlen)
  2116. {
  2117. struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
  2118. struct btrfs_free_space *entry = NULL;
  2119. struct btrfs_fs_info *fs_info = block_group->fs_info;
  2120. u64 bytes = 0;
  2121. u64 actually_trimmed;
  2122. int ret = 0;
  2123. *trimmed = 0;
  2124. while (start < end) {
  2125. spin_lock(&ctl->tree_lock);
  2126. if (ctl->free_space < minlen) {
  2127. spin_unlock(&ctl->tree_lock);
  2128. break;
  2129. }
  2130. entry = tree_search_offset(ctl, start, 0, 1);
  2131. if (!entry)
  2132. entry = tree_search_offset(ctl,
  2133. offset_to_bitmap(ctl, start),
  2134. 1, 1);
  2135. if (!entry || entry->offset >= end) {
  2136. spin_unlock(&ctl->tree_lock);
  2137. break;
  2138. }
  2139. if (entry->bitmap) {
  2140. ret = search_bitmap(ctl, entry, &start, &bytes);
  2141. if (!ret) {
  2142. if (start >= end) {
  2143. spin_unlock(&ctl->tree_lock);
  2144. break;
  2145. }
  2146. bytes = min(bytes, end - start);
  2147. bitmap_clear_bits(ctl, entry, start, bytes);
  2148. if (entry->bytes == 0)
  2149. free_bitmap(ctl, entry);
  2150. } else {
  2151. start = entry->offset + BITS_PER_BITMAP *
  2152. block_group->sectorsize;
  2153. spin_unlock(&ctl->tree_lock);
  2154. ret = 0;
  2155. continue;
  2156. }
  2157. } else {
  2158. start = entry->offset;
  2159. bytes = min(entry->bytes, end - start);
  2160. unlink_free_space(ctl, entry);
  2161. kmem_cache_free(btrfs_free_space_cachep, entry);
  2162. }
  2163. spin_unlock(&ctl->tree_lock);
  2164. if (bytes >= minlen) {
  2165. int update_ret;
  2166. update_ret = btrfs_update_reserved_bytes(block_group,
  2167. bytes, 1, 1);
  2168. ret = btrfs_error_discard_extent(fs_info->extent_root,
  2169. start,
  2170. bytes,
  2171. &actually_trimmed);
  2172. btrfs_add_free_space(block_group, start, bytes);
  2173. if (!update_ret)
  2174. btrfs_update_reserved_bytes(block_group,
  2175. bytes, 0, 1);
  2176. if (ret)
  2177. break;
  2178. *trimmed += actually_trimmed;
  2179. }
  2180. start += bytes;
  2181. bytes = 0;
  2182. if (fatal_signal_pending(current)) {
  2183. ret = -ERESTARTSYS;
  2184. break;
  2185. }
  2186. cond_resched();
  2187. }
  2188. return ret;
  2189. }
  2190. /*
  2191. * Find the left-most item in the cache tree, and then return the
  2192. * smallest inode number in the item.
  2193. *
  2194. * Note: the returned inode number may not be the smallest one in
  2195. * the tree, if the left-most item is a bitmap.
  2196. */
  2197. u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
  2198. {
  2199. struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
  2200. struct btrfs_free_space *entry = NULL;
  2201. u64 ino = 0;
  2202. spin_lock(&ctl->tree_lock);
  2203. if (RB_EMPTY_ROOT(&ctl->free_space_offset))
  2204. goto out;
  2205. entry = rb_entry(rb_first(&ctl->free_space_offset),
  2206. struct btrfs_free_space, offset_index);
  2207. if (!entry->bitmap) {
  2208. ino = entry->offset;
  2209. unlink_free_space(ctl, entry);
  2210. entry->offset++;
  2211. entry->bytes--;
  2212. if (!entry->bytes)
  2213. kmem_cache_free(btrfs_free_space_cachep, entry);
  2214. else
  2215. link_free_space(ctl, entry);
  2216. } else {
  2217. u64 offset = 0;
  2218. u64 count = 1;
  2219. int ret;
  2220. ret = search_bitmap(ctl, entry, &offset, &count);
  2221. BUG_ON(ret);
  2222. ino = offset;
  2223. bitmap_clear_bits(ctl, entry, offset, 1);
  2224. if (entry->bytes == 0)
  2225. free_bitmap(ctl, entry);
  2226. }
  2227. out:
  2228. spin_unlock(&ctl->tree_lock);
  2229. return ino;
  2230. }
  2231. struct inode *lookup_free_ino_inode(struct btrfs_root *root,
  2232. struct btrfs_path *path)
  2233. {
  2234. struct inode *inode = NULL;
  2235. spin_lock(&root->cache_lock);
  2236. if (root->cache_inode)
  2237. inode = igrab(root->cache_inode);
  2238. spin_unlock(&root->cache_lock);
  2239. if (inode)
  2240. return inode;
  2241. inode = __lookup_free_space_inode(root, path, 0);
  2242. if (IS_ERR(inode))
  2243. return inode;
  2244. spin_lock(&root->cache_lock);
  2245. if (!btrfs_fs_closing(root->fs_info))
  2246. root->cache_inode = igrab(inode);
  2247. spin_unlock(&root->cache_lock);
  2248. return inode;
  2249. }
  2250. int create_free_ino_inode(struct btrfs_root *root,
  2251. struct btrfs_trans_handle *trans,
  2252. struct btrfs_path *path)
  2253. {
  2254. return __create_free_space_inode(root, trans, path,
  2255. BTRFS_FREE_INO_OBJECTID, 0);
  2256. }
  2257. int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  2258. {
  2259. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2260. struct btrfs_path *path;
  2261. struct inode *inode;
  2262. int ret = 0;
  2263. u64 root_gen = btrfs_root_generation(&root->root_item);
  2264. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2265. return 0;
  2266. /*
  2267. * If we're unmounting then just return, since this does a search on the
  2268. * normal root and not the commit root and we could deadlock.
  2269. */
  2270. if (btrfs_fs_closing(fs_info))
  2271. return 0;
  2272. path = btrfs_alloc_path();
  2273. if (!path)
  2274. return 0;
  2275. inode = lookup_free_ino_inode(root, path);
  2276. if (IS_ERR(inode))
  2277. goto out;
  2278. if (root_gen != BTRFS_I(inode)->generation)
  2279. goto out_put;
  2280. ret = __load_free_space_cache(root, inode, ctl, path, 0);
  2281. if (ret < 0)
  2282. printk(KERN_ERR "btrfs: failed to load free ino cache for "
  2283. "root %llu\n", root->root_key.objectid);
  2284. out_put:
  2285. iput(inode);
  2286. out:
  2287. btrfs_free_path(path);
  2288. return ret;
  2289. }
  2290. int btrfs_write_out_ino_cache(struct btrfs_root *root,
  2291. struct btrfs_trans_handle *trans,
  2292. struct btrfs_path *path)
  2293. {
  2294. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  2295. struct inode *inode;
  2296. int ret;
  2297. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  2298. return 0;
  2299. inode = lookup_free_ino_inode(root, path);
  2300. if (IS_ERR(inode))
  2301. return 0;
  2302. ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
  2303. if (ret < 0)
  2304. printk(KERN_ERR "btrfs: failed to write free ino cache "
  2305. "for root %llu\n", root->root_key.objectid);
  2306. iput(inode);
  2307. return ret;
  2308. }