disk-io.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. /*
  2. * Copyright (C) 2007 Oracle. 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/version.h>
  19. #include <linux/fs.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/swap.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/writeback.h>
  25. #include <linux/buffer_head.h> // for block_sync_page
  26. #include <linux/workqueue.h>
  27. #include <linux/kthread.h>
  28. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  29. # include <linux/freezer.h>
  30. #else
  31. # include <linux/sched.h>
  32. #endif
  33. #include "crc32c.h"
  34. #include "ctree.h"
  35. #include "disk-io.h"
  36. #include "transaction.h"
  37. #include "btrfs_inode.h"
  38. #include "volumes.h"
  39. #include "print-tree.h"
  40. #include "async-thread.h"
  41. #include "locking.h"
  42. #include "ref-cache.h"
  43. #include "tree-log.h"
  44. #if 0
  45. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  46. {
  47. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  48. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  49. (unsigned long long)extent_buffer_blocknr(buf),
  50. (unsigned long long)btrfs_header_blocknr(buf));
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. #endif
  56. static struct extent_io_ops btree_extent_io_ops;
  57. static void end_workqueue_fn(struct btrfs_work *work);
  58. struct end_io_wq {
  59. struct bio *bio;
  60. bio_end_io_t *end_io;
  61. void *private;
  62. struct btrfs_fs_info *info;
  63. int error;
  64. int metadata;
  65. struct list_head list;
  66. struct btrfs_work work;
  67. };
  68. struct async_submit_bio {
  69. struct inode *inode;
  70. struct bio *bio;
  71. struct list_head list;
  72. extent_submit_bio_hook_t *submit_bio_hook;
  73. int rw;
  74. int mirror_num;
  75. struct btrfs_work work;
  76. };
  77. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  78. size_t page_offset, u64 start, u64 len,
  79. int create)
  80. {
  81. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  82. struct extent_map *em;
  83. int ret;
  84. spin_lock(&em_tree->lock);
  85. em = lookup_extent_mapping(em_tree, start, len);
  86. if (em) {
  87. em->bdev =
  88. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  89. spin_unlock(&em_tree->lock);
  90. goto out;
  91. }
  92. spin_unlock(&em_tree->lock);
  93. em = alloc_extent_map(GFP_NOFS);
  94. if (!em) {
  95. em = ERR_PTR(-ENOMEM);
  96. goto out;
  97. }
  98. em->start = 0;
  99. em->len = (u64)-1;
  100. em->block_start = 0;
  101. em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  102. spin_lock(&em_tree->lock);
  103. ret = add_extent_mapping(em_tree, em);
  104. if (ret == -EEXIST) {
  105. u64 failed_start = em->start;
  106. u64 failed_len = em->len;
  107. printk("failed to insert %Lu %Lu -> %Lu into tree\n",
  108. em->start, em->len, em->block_start);
  109. free_extent_map(em);
  110. em = lookup_extent_mapping(em_tree, start, len);
  111. if (em) {
  112. printk("after failing, found %Lu %Lu %Lu\n",
  113. em->start, em->len, em->block_start);
  114. ret = 0;
  115. } else {
  116. em = lookup_extent_mapping(em_tree, failed_start,
  117. failed_len);
  118. if (em) {
  119. printk("double failure lookup gives us "
  120. "%Lu %Lu -> %Lu\n", em->start,
  121. em->len, em->block_start);
  122. free_extent_map(em);
  123. }
  124. ret = -EIO;
  125. }
  126. } else if (ret) {
  127. free_extent_map(em);
  128. em = NULL;
  129. }
  130. spin_unlock(&em_tree->lock);
  131. if (ret)
  132. em = ERR_PTR(ret);
  133. out:
  134. return em;
  135. }
  136. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  137. {
  138. return btrfs_crc32c(seed, data, len);
  139. }
  140. void btrfs_csum_final(u32 crc, char *result)
  141. {
  142. *(__le32 *)result = ~cpu_to_le32(crc);
  143. }
  144. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  145. int verify)
  146. {
  147. char result[BTRFS_CRC32_SIZE];
  148. unsigned long len;
  149. unsigned long cur_len;
  150. unsigned long offset = BTRFS_CSUM_SIZE;
  151. char *map_token = NULL;
  152. char *kaddr;
  153. unsigned long map_start;
  154. unsigned long map_len;
  155. int err;
  156. u32 crc = ~(u32)0;
  157. len = buf->len - offset;
  158. while(len > 0) {
  159. err = map_private_extent_buffer(buf, offset, 32,
  160. &map_token, &kaddr,
  161. &map_start, &map_len, KM_USER0);
  162. if (err) {
  163. printk("failed to map extent buffer! %lu\n",
  164. offset);
  165. return 1;
  166. }
  167. cur_len = min(len, map_len - (offset - map_start));
  168. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  169. crc, cur_len);
  170. len -= cur_len;
  171. offset += cur_len;
  172. unmap_extent_buffer(buf, map_token, KM_USER0);
  173. }
  174. btrfs_csum_final(crc, result);
  175. if (verify) {
  176. /* FIXME, this is not good */
  177. if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
  178. u32 val;
  179. u32 found = 0;
  180. memcpy(&found, result, BTRFS_CRC32_SIZE);
  181. read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
  182. printk("btrfs: %s checksum verify failed on %llu "
  183. "wanted %X found %X level %d\n",
  184. root->fs_info->sb->s_id,
  185. buf->start, val, found, btrfs_header_level(buf));
  186. return 1;
  187. }
  188. } else {
  189. write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
  190. }
  191. return 0;
  192. }
  193. static int verify_parent_transid(struct extent_io_tree *io_tree,
  194. struct extent_buffer *eb, u64 parent_transid)
  195. {
  196. int ret;
  197. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  198. return 0;
  199. lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
  200. if (extent_buffer_uptodate(io_tree, eb) &&
  201. btrfs_header_generation(eb) == parent_transid) {
  202. ret = 0;
  203. goto out;
  204. }
  205. printk("parent transid verify failed on %llu wanted %llu found %llu\n",
  206. (unsigned long long)eb->start,
  207. (unsigned long long)parent_transid,
  208. (unsigned long long)btrfs_header_generation(eb));
  209. ret = 1;
  210. clear_extent_buffer_uptodate(io_tree, eb);
  211. out:
  212. unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
  213. GFP_NOFS);
  214. return ret;
  215. }
  216. static int btree_read_extent_buffer_pages(struct btrfs_root *root,
  217. struct extent_buffer *eb,
  218. u64 start, u64 parent_transid)
  219. {
  220. struct extent_io_tree *io_tree;
  221. int ret;
  222. int num_copies = 0;
  223. int mirror_num = 0;
  224. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  225. while (1) {
  226. ret = read_extent_buffer_pages(io_tree, eb, start, 1,
  227. btree_get_extent, mirror_num);
  228. if (!ret &&
  229. !verify_parent_transid(io_tree, eb, parent_transid))
  230. return ret;
  231. printk("read extent buffer pages failed with ret %d mirror no %d\n", ret, mirror_num);
  232. num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
  233. eb->start, eb->len);
  234. if (num_copies == 1)
  235. return ret;
  236. mirror_num++;
  237. if (mirror_num > num_copies)
  238. return ret;
  239. }
  240. return -EIO;
  241. }
  242. int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  243. {
  244. struct extent_io_tree *tree;
  245. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  246. u64 found_start;
  247. int found_level;
  248. unsigned long len;
  249. struct extent_buffer *eb;
  250. int ret;
  251. tree = &BTRFS_I(page->mapping->host)->io_tree;
  252. if (page->private == EXTENT_PAGE_PRIVATE)
  253. goto out;
  254. if (!page->private)
  255. goto out;
  256. len = page->private >> 2;
  257. if (len == 0) {
  258. WARN_ON(1);
  259. }
  260. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  261. ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
  262. btrfs_header_generation(eb));
  263. BUG_ON(ret);
  264. found_start = btrfs_header_bytenr(eb);
  265. if (found_start != start) {
  266. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  267. start, found_start, len);
  268. WARN_ON(1);
  269. goto err;
  270. }
  271. if (eb->first_page != page) {
  272. printk("bad first page %lu %lu\n", eb->first_page->index,
  273. page->index);
  274. WARN_ON(1);
  275. goto err;
  276. }
  277. if (!PageUptodate(page)) {
  278. printk("csum not up to date page %lu\n", page->index);
  279. WARN_ON(1);
  280. goto err;
  281. }
  282. found_level = btrfs_header_level(eb);
  283. csum_tree_block(root, eb, 0);
  284. err:
  285. free_extent_buffer(eb);
  286. out:
  287. return 0;
  288. }
  289. static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
  290. {
  291. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  292. csum_dirty_buffer(root, page);
  293. return 0;
  294. }
  295. int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  296. struct extent_state *state)
  297. {
  298. struct extent_io_tree *tree;
  299. u64 found_start;
  300. int found_level;
  301. unsigned long len;
  302. struct extent_buffer *eb;
  303. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  304. int ret = 0;
  305. tree = &BTRFS_I(page->mapping->host)->io_tree;
  306. if (page->private == EXTENT_PAGE_PRIVATE)
  307. goto out;
  308. if (!page->private)
  309. goto out;
  310. len = page->private >> 2;
  311. if (len == 0) {
  312. WARN_ON(1);
  313. }
  314. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  315. found_start = btrfs_header_bytenr(eb);
  316. if (found_start != start) {
  317. printk("bad tree block start %llu %llu\n",
  318. (unsigned long long)found_start,
  319. (unsigned long long)eb->start);
  320. ret = -EIO;
  321. goto err;
  322. }
  323. if (eb->first_page != page) {
  324. printk("bad first page %lu %lu\n", eb->first_page->index,
  325. page->index);
  326. WARN_ON(1);
  327. ret = -EIO;
  328. goto err;
  329. }
  330. if (memcmp_extent_buffer(eb, root->fs_info->fsid,
  331. (unsigned long)btrfs_header_fsid(eb),
  332. BTRFS_FSID_SIZE)) {
  333. printk("bad fsid on block %Lu\n", eb->start);
  334. ret = -EIO;
  335. goto err;
  336. }
  337. found_level = btrfs_header_level(eb);
  338. ret = csum_tree_block(root, eb, 1);
  339. if (ret)
  340. ret = -EIO;
  341. end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
  342. end = eb->start + end - 1;
  343. err:
  344. free_extent_buffer(eb);
  345. out:
  346. return ret;
  347. }
  348. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  349. static void end_workqueue_bio(struct bio *bio, int err)
  350. #else
  351. static int end_workqueue_bio(struct bio *bio,
  352. unsigned int bytes_done, int err)
  353. #endif
  354. {
  355. struct end_io_wq *end_io_wq = bio->bi_private;
  356. struct btrfs_fs_info *fs_info;
  357. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  358. if (bio->bi_size)
  359. return 1;
  360. #endif
  361. fs_info = end_io_wq->info;
  362. end_io_wq->error = err;
  363. end_io_wq->work.func = end_workqueue_fn;
  364. end_io_wq->work.flags = 0;
  365. if (bio->bi_rw & (1 << BIO_RW))
  366. btrfs_queue_worker(&fs_info->endio_write_workers,
  367. &end_io_wq->work);
  368. else
  369. btrfs_queue_worker(&fs_info->endio_workers, &end_io_wq->work);
  370. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  371. return 0;
  372. #endif
  373. }
  374. int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  375. int metadata)
  376. {
  377. struct end_io_wq *end_io_wq;
  378. end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
  379. if (!end_io_wq)
  380. return -ENOMEM;
  381. end_io_wq->private = bio->bi_private;
  382. end_io_wq->end_io = bio->bi_end_io;
  383. end_io_wq->info = info;
  384. end_io_wq->error = 0;
  385. end_io_wq->bio = bio;
  386. end_io_wq->metadata = metadata;
  387. bio->bi_private = end_io_wq;
  388. bio->bi_end_io = end_workqueue_bio;
  389. return 0;
  390. }
  391. unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
  392. {
  393. unsigned long limit = min_t(unsigned long,
  394. info->workers.max_workers,
  395. info->fs_devices->open_devices);
  396. return 256 * limit;
  397. }
  398. int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
  399. {
  400. return atomic_read(&info->nr_async_bios) >
  401. btrfs_async_submit_limit(info);
  402. }
  403. static void run_one_async_submit(struct btrfs_work *work)
  404. {
  405. struct btrfs_fs_info *fs_info;
  406. struct async_submit_bio *async;
  407. int limit;
  408. async = container_of(work, struct async_submit_bio, work);
  409. fs_info = BTRFS_I(async->inode)->root->fs_info;
  410. limit = btrfs_async_submit_limit(fs_info);
  411. limit = limit * 2 / 3;
  412. atomic_dec(&fs_info->nr_async_submits);
  413. if (atomic_read(&fs_info->nr_async_submits) < limit &&
  414. waitqueue_active(&fs_info->async_submit_wait))
  415. wake_up(&fs_info->async_submit_wait);
  416. async->submit_bio_hook(async->inode, async->rw, async->bio,
  417. async->mirror_num);
  418. kfree(async);
  419. }
  420. int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
  421. int rw, struct bio *bio, int mirror_num,
  422. extent_submit_bio_hook_t *submit_bio_hook)
  423. {
  424. struct async_submit_bio *async;
  425. int limit = btrfs_async_submit_limit(fs_info);
  426. async = kmalloc(sizeof(*async), GFP_NOFS);
  427. if (!async)
  428. return -ENOMEM;
  429. async->inode = inode;
  430. async->rw = rw;
  431. async->bio = bio;
  432. async->mirror_num = mirror_num;
  433. async->submit_bio_hook = submit_bio_hook;
  434. async->work.func = run_one_async_submit;
  435. async->work.flags = 0;
  436. atomic_inc(&fs_info->nr_async_submits);
  437. btrfs_queue_worker(&fs_info->workers, &async->work);
  438. if (atomic_read(&fs_info->nr_async_submits) > limit) {
  439. wait_event_timeout(fs_info->async_submit_wait,
  440. (atomic_read(&fs_info->nr_async_submits) < limit),
  441. HZ/10);
  442. wait_event_timeout(fs_info->async_submit_wait,
  443. (atomic_read(&fs_info->nr_async_bios) < limit),
  444. HZ/10);
  445. }
  446. return 0;
  447. }
  448. static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  449. int mirror_num)
  450. {
  451. struct btrfs_root *root = BTRFS_I(inode)->root;
  452. u64 offset;
  453. int ret;
  454. offset = bio->bi_sector << 9;
  455. /*
  456. * when we're called for a write, we're already in the async
  457. * submission context. Just jump into btrfs_map_bio
  458. */
  459. if (rw & (1 << BIO_RW)) {
  460. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
  461. mirror_num, 1);
  462. }
  463. /*
  464. * called for a read, do the setup so that checksum validation
  465. * can happen in the async kernel threads
  466. */
  467. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
  468. BUG_ON(ret);
  469. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
  470. }
  471. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  472. int mirror_num)
  473. {
  474. /*
  475. * kthread helpers are used to submit writes so that checksumming
  476. * can happen in parallel across all CPUs
  477. */
  478. if (!(rw & (1 << BIO_RW))) {
  479. return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
  480. }
  481. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  482. inode, rw, bio, mirror_num,
  483. __btree_submit_bio_hook);
  484. }
  485. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  486. {
  487. struct extent_io_tree *tree;
  488. tree = &BTRFS_I(page->mapping->host)->io_tree;
  489. if (current->flags & PF_MEMALLOC) {
  490. redirty_page_for_writepage(wbc, page);
  491. unlock_page(page);
  492. return 0;
  493. }
  494. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  495. }
  496. static int btree_writepages(struct address_space *mapping,
  497. struct writeback_control *wbc)
  498. {
  499. struct extent_io_tree *tree;
  500. tree = &BTRFS_I(mapping->host)->io_tree;
  501. if (wbc->sync_mode == WB_SYNC_NONE) {
  502. u64 num_dirty;
  503. u64 start = 0;
  504. unsigned long thresh = 8 * 1024 * 1024;
  505. if (wbc->for_kupdate)
  506. return 0;
  507. num_dirty = count_range_bits(tree, &start, (u64)-1,
  508. thresh, EXTENT_DIRTY);
  509. if (num_dirty < thresh) {
  510. return 0;
  511. }
  512. }
  513. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  514. }
  515. int btree_readpage(struct file *file, struct page *page)
  516. {
  517. struct extent_io_tree *tree;
  518. tree = &BTRFS_I(page->mapping->host)->io_tree;
  519. return extent_read_full_page(tree, page, btree_get_extent);
  520. }
  521. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  522. {
  523. struct extent_io_tree *tree;
  524. struct extent_map_tree *map;
  525. int ret;
  526. if (PageWriteback(page) || PageDirty(page))
  527. return 0;
  528. tree = &BTRFS_I(page->mapping->host)->io_tree;
  529. map = &BTRFS_I(page->mapping->host)->extent_tree;
  530. ret = try_release_extent_state(map, tree, page, gfp_flags);
  531. if (!ret) {
  532. return 0;
  533. }
  534. ret = try_release_extent_buffer(tree, page);
  535. if (ret == 1) {
  536. ClearPagePrivate(page);
  537. set_page_private(page, 0);
  538. page_cache_release(page);
  539. }
  540. return ret;
  541. }
  542. static void btree_invalidatepage(struct page *page, unsigned long offset)
  543. {
  544. struct extent_io_tree *tree;
  545. tree = &BTRFS_I(page->mapping->host)->io_tree;
  546. extent_invalidatepage(tree, page, offset);
  547. btree_releasepage(page, GFP_NOFS);
  548. if (PagePrivate(page)) {
  549. printk("warning page private not zero on page %Lu\n",
  550. page_offset(page));
  551. ClearPagePrivate(page);
  552. set_page_private(page, 0);
  553. page_cache_release(page);
  554. }
  555. }
  556. #if 0
  557. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  558. {
  559. struct buffer_head *bh;
  560. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  561. struct buffer_head *head;
  562. if (!page_has_buffers(page)) {
  563. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  564. (1 << BH_Dirty)|(1 << BH_Uptodate));
  565. }
  566. head = page_buffers(page);
  567. bh = head;
  568. do {
  569. if (buffer_dirty(bh))
  570. csum_tree_block(root, bh, 0);
  571. bh = bh->b_this_page;
  572. } while (bh != head);
  573. return block_write_full_page(page, btree_get_block, wbc);
  574. }
  575. #endif
  576. static struct address_space_operations btree_aops = {
  577. .readpage = btree_readpage,
  578. .writepage = btree_writepage,
  579. .writepages = btree_writepages,
  580. .releasepage = btree_releasepage,
  581. .invalidatepage = btree_invalidatepage,
  582. .sync_page = block_sync_page,
  583. };
  584. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  585. u64 parent_transid)
  586. {
  587. struct extent_buffer *buf = NULL;
  588. struct inode *btree_inode = root->fs_info->btree_inode;
  589. int ret = 0;
  590. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  591. if (!buf)
  592. return 0;
  593. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  594. buf, 0, 0, btree_get_extent, 0);
  595. free_extent_buffer(buf);
  596. return ret;
  597. }
  598. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  599. u64 bytenr, u32 blocksize)
  600. {
  601. struct inode *btree_inode = root->fs_info->btree_inode;
  602. struct extent_buffer *eb;
  603. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  604. bytenr, blocksize, GFP_NOFS);
  605. return eb;
  606. }
  607. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  608. u64 bytenr, u32 blocksize)
  609. {
  610. struct inode *btree_inode = root->fs_info->btree_inode;
  611. struct extent_buffer *eb;
  612. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  613. bytenr, blocksize, NULL, GFP_NOFS);
  614. return eb;
  615. }
  616. int btrfs_write_tree_block(struct extent_buffer *buf)
  617. {
  618. return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
  619. buf->start + buf->len - 1, WB_SYNC_NONE);
  620. }
  621. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  622. {
  623. return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
  624. buf->start, buf->start + buf->len -1);
  625. }
  626. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  627. u32 blocksize, u64 parent_transid)
  628. {
  629. struct extent_buffer *buf = NULL;
  630. struct inode *btree_inode = root->fs_info->btree_inode;
  631. struct extent_io_tree *io_tree;
  632. int ret;
  633. io_tree = &BTRFS_I(btree_inode)->io_tree;
  634. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  635. if (!buf)
  636. return NULL;
  637. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  638. if (ret == 0) {
  639. buf->flags |= EXTENT_UPTODATE;
  640. } else {
  641. WARN_ON(1);
  642. }
  643. return buf;
  644. }
  645. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  646. struct extent_buffer *buf)
  647. {
  648. struct inode *btree_inode = root->fs_info->btree_inode;
  649. if (btrfs_header_generation(buf) ==
  650. root->fs_info->running_transaction->transid) {
  651. WARN_ON(!btrfs_tree_locked(buf));
  652. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  653. buf);
  654. }
  655. return 0;
  656. }
  657. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  658. u32 stripesize, struct btrfs_root *root,
  659. struct btrfs_fs_info *fs_info,
  660. u64 objectid)
  661. {
  662. root->node = NULL;
  663. root->inode = NULL;
  664. root->commit_root = NULL;
  665. root->ref_tree = NULL;
  666. root->sectorsize = sectorsize;
  667. root->nodesize = nodesize;
  668. root->leafsize = leafsize;
  669. root->stripesize = stripesize;
  670. root->ref_cows = 0;
  671. root->track_dirty = 0;
  672. root->fs_info = fs_info;
  673. root->objectid = objectid;
  674. root->last_trans = 0;
  675. root->highest_inode = 0;
  676. root->last_inode_alloc = 0;
  677. root->name = NULL;
  678. root->in_sysfs = 0;
  679. INIT_LIST_HEAD(&root->dirty_list);
  680. INIT_LIST_HEAD(&root->orphan_list);
  681. INIT_LIST_HEAD(&root->dead_list);
  682. spin_lock_init(&root->node_lock);
  683. spin_lock_init(&root->list_lock);
  684. mutex_init(&root->objectid_mutex);
  685. mutex_init(&root->log_mutex);
  686. extent_io_tree_init(&root->dirty_log_pages,
  687. fs_info->btree_inode->i_mapping, GFP_NOFS);
  688. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  689. root->ref_tree = &root->ref_tree_struct;
  690. memset(&root->root_key, 0, sizeof(root->root_key));
  691. memset(&root->root_item, 0, sizeof(root->root_item));
  692. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  693. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  694. root->defrag_trans_start = fs_info->generation;
  695. init_completion(&root->kobj_unregister);
  696. root->defrag_running = 0;
  697. root->defrag_level = 0;
  698. root->root_key.objectid = objectid;
  699. return 0;
  700. }
  701. static int find_and_setup_root(struct btrfs_root *tree_root,
  702. struct btrfs_fs_info *fs_info,
  703. u64 objectid,
  704. struct btrfs_root *root)
  705. {
  706. int ret;
  707. u32 blocksize;
  708. __setup_root(tree_root->nodesize, tree_root->leafsize,
  709. tree_root->sectorsize, tree_root->stripesize,
  710. root, fs_info, objectid);
  711. ret = btrfs_find_last_root(tree_root, objectid,
  712. &root->root_item, &root->root_key);
  713. BUG_ON(ret);
  714. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  715. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  716. blocksize, 0);
  717. BUG_ON(!root->node);
  718. return 0;
  719. }
  720. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  721. struct btrfs_fs_info *fs_info)
  722. {
  723. struct extent_buffer *eb;
  724. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  725. u64 start = 0;
  726. u64 end = 0;
  727. int ret;
  728. if (!log_root_tree)
  729. return 0;
  730. while(1) {
  731. ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
  732. 0, &start, &end, EXTENT_DIRTY);
  733. if (ret)
  734. break;
  735. clear_extent_dirty(&log_root_tree->dirty_log_pages,
  736. start, end, GFP_NOFS);
  737. }
  738. eb = fs_info->log_root_tree->node;
  739. WARN_ON(btrfs_header_level(eb) != 0);
  740. WARN_ON(btrfs_header_nritems(eb) != 0);
  741. ret = btrfs_free_reserved_extent(fs_info->tree_root,
  742. eb->start, eb->len);
  743. BUG_ON(ret);
  744. free_extent_buffer(eb);
  745. kfree(fs_info->log_root_tree);
  746. fs_info->log_root_tree = NULL;
  747. return 0;
  748. }
  749. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  750. struct btrfs_fs_info *fs_info)
  751. {
  752. struct btrfs_root *root;
  753. struct btrfs_root *tree_root = fs_info->tree_root;
  754. root = kzalloc(sizeof(*root), GFP_NOFS);
  755. if (!root)
  756. return -ENOMEM;
  757. __setup_root(tree_root->nodesize, tree_root->leafsize,
  758. tree_root->sectorsize, tree_root->stripesize,
  759. root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  760. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  761. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  762. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  763. root->ref_cows = 0;
  764. root->node = btrfs_alloc_free_block(trans, root, root->leafsize,
  765. BTRFS_TREE_LOG_OBJECTID,
  766. 0, 0, 0, 0, 0);
  767. btrfs_set_header_nritems(root->node, 0);
  768. btrfs_set_header_level(root->node, 0);
  769. btrfs_set_header_bytenr(root->node, root->node->start);
  770. btrfs_set_header_generation(root->node, trans->transid);
  771. btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID);
  772. write_extent_buffer(root->node, root->fs_info->fsid,
  773. (unsigned long)btrfs_header_fsid(root->node),
  774. BTRFS_FSID_SIZE);
  775. btrfs_mark_buffer_dirty(root->node);
  776. btrfs_tree_unlock(root->node);
  777. fs_info->log_root_tree = root;
  778. return 0;
  779. }
  780. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
  781. struct btrfs_key *location)
  782. {
  783. struct btrfs_root *root;
  784. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  785. struct btrfs_path *path;
  786. struct extent_buffer *l;
  787. u64 highest_inode;
  788. u32 blocksize;
  789. int ret = 0;
  790. root = kzalloc(sizeof(*root), GFP_NOFS);
  791. if (!root)
  792. return ERR_PTR(-ENOMEM);
  793. if (location->offset == (u64)-1) {
  794. ret = find_and_setup_root(tree_root, fs_info,
  795. location->objectid, root);
  796. if (ret) {
  797. kfree(root);
  798. return ERR_PTR(ret);
  799. }
  800. goto insert;
  801. }
  802. __setup_root(tree_root->nodesize, tree_root->leafsize,
  803. tree_root->sectorsize, tree_root->stripesize,
  804. root, fs_info, location->objectid);
  805. path = btrfs_alloc_path();
  806. BUG_ON(!path);
  807. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  808. if (ret != 0) {
  809. if (ret > 0)
  810. ret = -ENOENT;
  811. goto out;
  812. }
  813. l = path->nodes[0];
  814. read_extent_buffer(l, &root->root_item,
  815. btrfs_item_ptr_offset(l, path->slots[0]),
  816. sizeof(root->root_item));
  817. memcpy(&root->root_key, location, sizeof(*location));
  818. ret = 0;
  819. out:
  820. btrfs_release_path(root, path);
  821. btrfs_free_path(path);
  822. if (ret) {
  823. kfree(root);
  824. return ERR_PTR(ret);
  825. }
  826. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  827. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  828. blocksize, 0);
  829. BUG_ON(!root->node);
  830. insert:
  831. if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
  832. root->ref_cows = 1;
  833. ret = btrfs_find_highest_inode(root, &highest_inode);
  834. if (ret == 0) {
  835. root->highest_inode = highest_inode;
  836. root->last_inode_alloc = highest_inode;
  837. }
  838. }
  839. return root;
  840. }
  841. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  842. u64 root_objectid)
  843. {
  844. struct btrfs_root *root;
  845. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  846. return fs_info->tree_root;
  847. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  848. return fs_info->extent_root;
  849. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  850. (unsigned long)root_objectid);
  851. return root;
  852. }
  853. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  854. struct btrfs_key *location)
  855. {
  856. struct btrfs_root *root;
  857. int ret;
  858. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  859. return fs_info->tree_root;
  860. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  861. return fs_info->extent_root;
  862. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  863. return fs_info->chunk_root;
  864. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  865. return fs_info->dev_root;
  866. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  867. (unsigned long)location->objectid);
  868. if (root)
  869. return root;
  870. root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
  871. if (IS_ERR(root))
  872. return root;
  873. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  874. (unsigned long)root->root_key.objectid,
  875. root);
  876. if (ret) {
  877. free_extent_buffer(root->node);
  878. kfree(root);
  879. return ERR_PTR(ret);
  880. }
  881. ret = btrfs_find_dead_roots(fs_info->tree_root,
  882. root->root_key.objectid, root);
  883. BUG_ON(ret);
  884. return root;
  885. }
  886. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  887. struct btrfs_key *location,
  888. const char *name, int namelen)
  889. {
  890. struct btrfs_root *root;
  891. int ret;
  892. root = btrfs_read_fs_root_no_name(fs_info, location);
  893. if (!root)
  894. return NULL;
  895. if (root->in_sysfs)
  896. return root;
  897. ret = btrfs_set_root_name(root, name, namelen);
  898. if (ret) {
  899. free_extent_buffer(root->node);
  900. kfree(root);
  901. return ERR_PTR(ret);
  902. }
  903. ret = btrfs_sysfs_add_root(root);
  904. if (ret) {
  905. free_extent_buffer(root->node);
  906. kfree(root->name);
  907. kfree(root);
  908. return ERR_PTR(ret);
  909. }
  910. root->in_sysfs = 1;
  911. return root;
  912. }
  913. #if 0
  914. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  915. struct btrfs_hasher *hasher;
  916. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  917. if (!hasher)
  918. return -ENOMEM;
  919. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  920. if (!hasher->hash_tfm) {
  921. kfree(hasher);
  922. return -EINVAL;
  923. }
  924. spin_lock(&info->hash_lock);
  925. list_add(&hasher->list, &info->hashers);
  926. spin_unlock(&info->hash_lock);
  927. return 0;
  928. }
  929. #endif
  930. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  931. {
  932. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  933. int ret = 0;
  934. struct list_head *cur;
  935. struct btrfs_device *device;
  936. struct backing_dev_info *bdi;
  937. if ((bdi_bits & (1 << BDI_write_congested)) &&
  938. btrfs_congested_async(info, 0))
  939. return 1;
  940. list_for_each(cur, &info->fs_devices->devices) {
  941. device = list_entry(cur, struct btrfs_device, dev_list);
  942. if (!device->bdev)
  943. continue;
  944. bdi = blk_get_backing_dev_info(device->bdev);
  945. if (bdi && bdi_congested(bdi, bdi_bits)) {
  946. ret = 1;
  947. break;
  948. }
  949. }
  950. return ret;
  951. }
  952. /*
  953. * this unplugs every device on the box, and it is only used when page
  954. * is null
  955. */
  956. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  957. {
  958. struct list_head *cur;
  959. struct btrfs_device *device;
  960. struct btrfs_fs_info *info;
  961. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  962. list_for_each(cur, &info->fs_devices->devices) {
  963. device = list_entry(cur, struct btrfs_device, dev_list);
  964. bdi = blk_get_backing_dev_info(device->bdev);
  965. if (bdi->unplug_io_fn) {
  966. bdi->unplug_io_fn(bdi, page);
  967. }
  968. }
  969. }
  970. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  971. {
  972. struct inode *inode;
  973. struct extent_map_tree *em_tree;
  974. struct extent_map *em;
  975. struct address_space *mapping;
  976. u64 offset;
  977. /* the generic O_DIRECT read code does this */
  978. if (!page) {
  979. __unplug_io_fn(bdi, page);
  980. return;
  981. }
  982. /*
  983. * page->mapping may change at any time. Get a consistent copy
  984. * and use that for everything below
  985. */
  986. smp_mb();
  987. mapping = page->mapping;
  988. if (!mapping)
  989. return;
  990. inode = mapping->host;
  991. offset = page_offset(page);
  992. em_tree = &BTRFS_I(inode)->extent_tree;
  993. spin_lock(&em_tree->lock);
  994. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  995. spin_unlock(&em_tree->lock);
  996. if (!em) {
  997. __unplug_io_fn(bdi, page);
  998. return;
  999. }
  1000. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1001. free_extent_map(em);
  1002. __unplug_io_fn(bdi, page);
  1003. return;
  1004. }
  1005. offset = offset - em->start;
  1006. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  1007. em->block_start + offset, page);
  1008. free_extent_map(em);
  1009. }
  1010. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  1011. {
  1012. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1013. bdi_init(bdi);
  1014. #endif
  1015. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1016. bdi->state = 0;
  1017. bdi->capabilities = default_backing_dev_info.capabilities;
  1018. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  1019. bdi->unplug_io_data = info;
  1020. bdi->congested_fn = btrfs_congested_fn;
  1021. bdi->congested_data = info;
  1022. return 0;
  1023. }
  1024. static int bio_ready_for_csum(struct bio *bio)
  1025. {
  1026. u64 length = 0;
  1027. u64 buf_len = 0;
  1028. u64 start = 0;
  1029. struct page *page;
  1030. struct extent_io_tree *io_tree = NULL;
  1031. struct btrfs_fs_info *info = NULL;
  1032. struct bio_vec *bvec;
  1033. int i;
  1034. int ret;
  1035. bio_for_each_segment(bvec, bio, i) {
  1036. page = bvec->bv_page;
  1037. if (page->private == EXTENT_PAGE_PRIVATE) {
  1038. length += bvec->bv_len;
  1039. continue;
  1040. }
  1041. if (!page->private) {
  1042. length += bvec->bv_len;
  1043. continue;
  1044. }
  1045. length = bvec->bv_len;
  1046. buf_len = page->private >> 2;
  1047. start = page_offset(page) + bvec->bv_offset;
  1048. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  1049. info = BTRFS_I(page->mapping->host)->root->fs_info;
  1050. }
  1051. /* are we fully contained in this bio? */
  1052. if (buf_len <= length)
  1053. return 1;
  1054. ret = extent_range_uptodate(io_tree, start + length,
  1055. start + buf_len - 1);
  1056. if (ret == 1)
  1057. return ret;
  1058. return ret;
  1059. }
  1060. /*
  1061. * called by the kthread helper functions to finally call the bio end_io
  1062. * functions. This is where read checksum verification actually happens
  1063. */
  1064. static void end_workqueue_fn(struct btrfs_work *work)
  1065. {
  1066. struct bio *bio;
  1067. struct end_io_wq *end_io_wq;
  1068. struct btrfs_fs_info *fs_info;
  1069. int error;
  1070. end_io_wq = container_of(work, struct end_io_wq, work);
  1071. bio = end_io_wq->bio;
  1072. fs_info = end_io_wq->info;
  1073. /* metadata bios are special because the whole tree block must
  1074. * be checksummed at once. This makes sure the entire block is in
  1075. * ram and up to date before trying to verify things. For
  1076. * blocksize <= pagesize, it is basically a noop
  1077. */
  1078. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  1079. btrfs_queue_worker(&fs_info->endio_workers,
  1080. &end_io_wq->work);
  1081. return;
  1082. }
  1083. error = end_io_wq->error;
  1084. bio->bi_private = end_io_wq->private;
  1085. bio->bi_end_io = end_io_wq->end_io;
  1086. kfree(end_io_wq);
  1087. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1088. bio_endio(bio, bio->bi_size, error);
  1089. #else
  1090. bio_endio(bio, error);
  1091. #endif
  1092. }
  1093. static int cleaner_kthread(void *arg)
  1094. {
  1095. struct btrfs_root *root = arg;
  1096. do {
  1097. smp_mb();
  1098. if (root->fs_info->closing)
  1099. break;
  1100. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1101. mutex_lock(&root->fs_info->cleaner_mutex);
  1102. btrfs_clean_old_snapshots(root);
  1103. mutex_unlock(&root->fs_info->cleaner_mutex);
  1104. if (freezing(current)) {
  1105. refrigerator();
  1106. } else {
  1107. smp_mb();
  1108. if (root->fs_info->closing)
  1109. break;
  1110. set_current_state(TASK_INTERRUPTIBLE);
  1111. schedule();
  1112. __set_current_state(TASK_RUNNING);
  1113. }
  1114. } while (!kthread_should_stop());
  1115. return 0;
  1116. }
  1117. static int transaction_kthread(void *arg)
  1118. {
  1119. struct btrfs_root *root = arg;
  1120. struct btrfs_trans_handle *trans;
  1121. struct btrfs_transaction *cur;
  1122. unsigned long now;
  1123. unsigned long delay;
  1124. int ret;
  1125. do {
  1126. smp_mb();
  1127. if (root->fs_info->closing)
  1128. break;
  1129. delay = HZ * 30;
  1130. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1131. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1132. if (root->fs_info->total_ref_cache_size > 20 * 1024 * 1024) {
  1133. printk("btrfs: total reference cache size %Lu\n",
  1134. root->fs_info->total_ref_cache_size);
  1135. }
  1136. mutex_lock(&root->fs_info->trans_mutex);
  1137. cur = root->fs_info->running_transaction;
  1138. if (!cur) {
  1139. mutex_unlock(&root->fs_info->trans_mutex);
  1140. goto sleep;
  1141. }
  1142. now = get_seconds();
  1143. if (now < cur->start_time || now - cur->start_time < 30) {
  1144. mutex_unlock(&root->fs_info->trans_mutex);
  1145. delay = HZ * 5;
  1146. goto sleep;
  1147. }
  1148. mutex_unlock(&root->fs_info->trans_mutex);
  1149. trans = btrfs_start_transaction(root, 1);
  1150. ret = btrfs_commit_transaction(trans, root);
  1151. sleep:
  1152. wake_up_process(root->fs_info->cleaner_kthread);
  1153. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1154. if (freezing(current)) {
  1155. refrigerator();
  1156. } else {
  1157. if (root->fs_info->closing)
  1158. break;
  1159. set_current_state(TASK_INTERRUPTIBLE);
  1160. schedule_timeout(delay);
  1161. __set_current_state(TASK_RUNNING);
  1162. }
  1163. } while (!kthread_should_stop());
  1164. return 0;
  1165. }
  1166. struct btrfs_root *open_ctree(struct super_block *sb,
  1167. struct btrfs_fs_devices *fs_devices,
  1168. char *options)
  1169. {
  1170. u32 sectorsize;
  1171. u32 nodesize;
  1172. u32 leafsize;
  1173. u32 blocksize;
  1174. u32 stripesize;
  1175. struct buffer_head *bh;
  1176. struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
  1177. GFP_NOFS);
  1178. struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root),
  1179. GFP_NOFS);
  1180. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1181. GFP_NOFS);
  1182. struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
  1183. GFP_NOFS);
  1184. struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
  1185. GFP_NOFS);
  1186. struct btrfs_root *log_tree_root;
  1187. int ret;
  1188. int err = -EINVAL;
  1189. struct btrfs_super_block *disk_super;
  1190. if (!extent_root || !tree_root || !fs_info) {
  1191. err = -ENOMEM;
  1192. goto fail;
  1193. }
  1194. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1195. INIT_LIST_HEAD(&fs_info->trans_list);
  1196. INIT_LIST_HEAD(&fs_info->dead_roots);
  1197. INIT_LIST_HEAD(&fs_info->hashers);
  1198. INIT_LIST_HEAD(&fs_info->delalloc_inodes);
  1199. spin_lock_init(&fs_info->hash_lock);
  1200. spin_lock_init(&fs_info->delalloc_lock);
  1201. spin_lock_init(&fs_info->new_trans_lock);
  1202. spin_lock_init(&fs_info->ref_cache_lock);
  1203. init_completion(&fs_info->kobj_unregister);
  1204. fs_info->tree_root = tree_root;
  1205. fs_info->extent_root = extent_root;
  1206. fs_info->chunk_root = chunk_root;
  1207. fs_info->dev_root = dev_root;
  1208. fs_info->fs_devices = fs_devices;
  1209. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1210. INIT_LIST_HEAD(&fs_info->space_info);
  1211. btrfs_mapping_init(&fs_info->mapping_tree);
  1212. atomic_set(&fs_info->nr_async_submits, 0);
  1213. atomic_set(&fs_info->nr_async_bios, 0);
  1214. atomic_set(&fs_info->throttles, 0);
  1215. atomic_set(&fs_info->throttle_gen, 0);
  1216. fs_info->sb = sb;
  1217. fs_info->max_extent = (u64)-1;
  1218. fs_info->max_inline = 8192 * 1024;
  1219. setup_bdi(fs_info, &fs_info->bdi);
  1220. fs_info->btree_inode = new_inode(sb);
  1221. fs_info->btree_inode->i_ino = 1;
  1222. fs_info->btree_inode->i_nlink = 1;
  1223. fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
  1224. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1225. spin_lock_init(&fs_info->ordered_extent_lock);
  1226. sb->s_blocksize = 4096;
  1227. sb->s_blocksize_bits = blksize_bits(4096);
  1228. /*
  1229. * we set the i_size on the btree inode to the max possible int.
  1230. * the real end of the address space is determined by all of
  1231. * the devices in the system
  1232. */
  1233. fs_info->btree_inode->i_size = OFFSET_MAX;
  1234. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1235. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1236. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1237. fs_info->btree_inode->i_mapping,
  1238. GFP_NOFS);
  1239. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1240. GFP_NOFS);
  1241. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1242. spin_lock_init(&fs_info->block_group_cache_lock);
  1243. fs_info->block_group_cache_tree.rb_node = NULL;
  1244. extent_io_tree_init(&fs_info->pinned_extents,
  1245. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1246. extent_io_tree_init(&fs_info->pending_del,
  1247. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1248. extent_io_tree_init(&fs_info->extent_ins,
  1249. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1250. fs_info->do_barriers = 1;
  1251. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1252. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1253. sizeof(struct btrfs_key));
  1254. insert_inode_hash(fs_info->btree_inode);
  1255. mutex_init(&fs_info->trans_mutex);
  1256. mutex_init(&fs_info->tree_log_mutex);
  1257. mutex_init(&fs_info->drop_mutex);
  1258. mutex_init(&fs_info->alloc_mutex);
  1259. mutex_init(&fs_info->chunk_mutex);
  1260. mutex_init(&fs_info->transaction_kthread_mutex);
  1261. mutex_init(&fs_info->cleaner_mutex);
  1262. mutex_init(&fs_info->volume_mutex);
  1263. init_waitqueue_head(&fs_info->transaction_throttle);
  1264. init_waitqueue_head(&fs_info->transaction_wait);
  1265. init_waitqueue_head(&fs_info->async_submit_wait);
  1266. init_waitqueue_head(&fs_info->tree_log_wait);
  1267. atomic_set(&fs_info->tree_log_commit, 0);
  1268. atomic_set(&fs_info->tree_log_writers, 0);
  1269. fs_info->tree_log_transid = 0;
  1270. #if 0
  1271. ret = add_hasher(fs_info, "crc32c");
  1272. if (ret) {
  1273. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1274. err = -ENOMEM;
  1275. goto fail_iput;
  1276. }
  1277. #endif
  1278. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1279. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1280. bh = __bread(fs_devices->latest_bdev,
  1281. BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  1282. if (!bh)
  1283. goto fail_iput;
  1284. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1285. brelse(bh);
  1286. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1287. disk_super = &fs_info->super_copy;
  1288. if (!btrfs_super_root(disk_super))
  1289. goto fail_sb_buffer;
  1290. err = btrfs_parse_options(tree_root, options);
  1291. if (err)
  1292. goto fail_sb_buffer;
  1293. /*
  1294. * we need to start all the end_io workers up front because the
  1295. * queue work function gets called at interrupt time, and so it
  1296. * cannot dynamically grow.
  1297. */
  1298. btrfs_init_workers(&fs_info->workers, "worker",
  1299. fs_info->thread_pool_size);
  1300. btrfs_init_workers(&fs_info->submit_workers, "submit",
  1301. min_t(u64, fs_devices->num_devices,
  1302. fs_info->thread_pool_size));
  1303. /* a higher idle thresh on the submit workers makes it much more
  1304. * likely that bios will be send down in a sane order to the
  1305. * devices
  1306. */
  1307. fs_info->submit_workers.idle_thresh = 64;
  1308. /* fs_info->workers is responsible for checksumming file data
  1309. * blocks and metadata. Using a larger idle thresh allows each
  1310. * worker thread to operate on things in roughly the order they
  1311. * were sent by the writeback daemons, improving overall locality
  1312. * of the IO going down the pipe.
  1313. */
  1314. fs_info->workers.idle_thresh = 128;
  1315. btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
  1316. btrfs_init_workers(&fs_info->endio_workers, "endio",
  1317. fs_info->thread_pool_size);
  1318. btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
  1319. fs_info->thread_pool_size);
  1320. /*
  1321. * endios are largely parallel and should have a very
  1322. * low idle thresh
  1323. */
  1324. fs_info->endio_workers.idle_thresh = 4;
  1325. fs_info->endio_write_workers.idle_thresh = 64;
  1326. btrfs_start_workers(&fs_info->workers, 1);
  1327. btrfs_start_workers(&fs_info->submit_workers, 1);
  1328. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1329. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1330. btrfs_start_workers(&fs_info->endio_write_workers,
  1331. fs_info->thread_pool_size);
  1332. err = -EINVAL;
  1333. if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
  1334. printk("Btrfs: wanted %llu devices, but found %llu\n",
  1335. (unsigned long long)btrfs_super_num_devices(disk_super),
  1336. (unsigned long long)fs_devices->open_devices);
  1337. if (btrfs_test_opt(tree_root, DEGRADED))
  1338. printk("continuing in degraded mode\n");
  1339. else {
  1340. goto fail_sb_buffer;
  1341. }
  1342. }
  1343. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1344. nodesize = btrfs_super_nodesize(disk_super);
  1345. leafsize = btrfs_super_leafsize(disk_super);
  1346. sectorsize = btrfs_super_sectorsize(disk_super);
  1347. stripesize = btrfs_super_stripesize(disk_super);
  1348. tree_root->nodesize = nodesize;
  1349. tree_root->leafsize = leafsize;
  1350. tree_root->sectorsize = sectorsize;
  1351. tree_root->stripesize = stripesize;
  1352. sb->s_blocksize = sectorsize;
  1353. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1354. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1355. sizeof(disk_super->magic))) {
  1356. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1357. goto fail_sb_buffer;
  1358. }
  1359. mutex_lock(&fs_info->chunk_mutex);
  1360. ret = btrfs_read_sys_array(tree_root);
  1361. mutex_unlock(&fs_info->chunk_mutex);
  1362. if (ret) {
  1363. printk("btrfs: failed to read the system array on %s\n",
  1364. sb->s_id);
  1365. goto fail_sys_array;
  1366. }
  1367. blocksize = btrfs_level_size(tree_root,
  1368. btrfs_super_chunk_root_level(disk_super));
  1369. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1370. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1371. chunk_root->node = read_tree_block(chunk_root,
  1372. btrfs_super_chunk_root(disk_super),
  1373. blocksize, 0);
  1374. BUG_ON(!chunk_root->node);
  1375. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1376. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1377. BTRFS_UUID_SIZE);
  1378. mutex_lock(&fs_info->chunk_mutex);
  1379. ret = btrfs_read_chunk_tree(chunk_root);
  1380. mutex_unlock(&fs_info->chunk_mutex);
  1381. BUG_ON(ret);
  1382. btrfs_close_extra_devices(fs_devices);
  1383. blocksize = btrfs_level_size(tree_root,
  1384. btrfs_super_root_level(disk_super));
  1385. tree_root->node = read_tree_block(tree_root,
  1386. btrfs_super_root(disk_super),
  1387. blocksize, 0);
  1388. if (!tree_root->node)
  1389. goto fail_sb_buffer;
  1390. ret = find_and_setup_root(tree_root, fs_info,
  1391. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1392. if (ret)
  1393. goto fail_tree_root;
  1394. extent_root->track_dirty = 1;
  1395. ret = find_and_setup_root(tree_root, fs_info,
  1396. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1397. dev_root->track_dirty = 1;
  1398. if (ret)
  1399. goto fail_extent_root;
  1400. btrfs_read_block_groups(extent_root);
  1401. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  1402. fs_info->data_alloc_profile = (u64)-1;
  1403. fs_info->metadata_alloc_profile = (u64)-1;
  1404. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1405. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1406. "btrfs-cleaner");
  1407. if (!fs_info->cleaner_kthread)
  1408. goto fail_extent_root;
  1409. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1410. tree_root,
  1411. "btrfs-transaction");
  1412. if (!fs_info->transaction_kthread)
  1413. goto fail_cleaner;
  1414. if (btrfs_super_log_root(disk_super) != 0) {
  1415. u32 blocksize;
  1416. u64 bytenr = btrfs_super_log_root(disk_super);
  1417. blocksize =
  1418. btrfs_level_size(tree_root,
  1419. btrfs_super_log_root_level(disk_super));
  1420. log_tree_root = kzalloc(sizeof(struct btrfs_root),
  1421. GFP_NOFS);
  1422. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1423. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1424. log_tree_root->node = read_tree_block(tree_root, bytenr,
  1425. blocksize, 0);
  1426. ret = btrfs_recover_log_trees(log_tree_root);
  1427. BUG_ON(ret);
  1428. }
  1429. fs_info->last_trans_committed = btrfs_super_generation(disk_super);
  1430. return tree_root;
  1431. fail_cleaner:
  1432. kthread_stop(fs_info->cleaner_kthread);
  1433. fail_extent_root:
  1434. free_extent_buffer(extent_root->node);
  1435. fail_tree_root:
  1436. free_extent_buffer(tree_root->node);
  1437. fail_sys_array:
  1438. fail_sb_buffer:
  1439. btrfs_stop_workers(&fs_info->fixup_workers);
  1440. btrfs_stop_workers(&fs_info->workers);
  1441. btrfs_stop_workers(&fs_info->endio_workers);
  1442. btrfs_stop_workers(&fs_info->endio_write_workers);
  1443. btrfs_stop_workers(&fs_info->submit_workers);
  1444. fail_iput:
  1445. iput(fs_info->btree_inode);
  1446. fail:
  1447. btrfs_close_devices(fs_info->fs_devices);
  1448. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1449. kfree(extent_root);
  1450. kfree(tree_root);
  1451. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1452. bdi_destroy(&fs_info->bdi);
  1453. #endif
  1454. kfree(fs_info);
  1455. return ERR_PTR(err);
  1456. }
  1457. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1458. {
  1459. char b[BDEVNAME_SIZE];
  1460. if (uptodate) {
  1461. set_buffer_uptodate(bh);
  1462. } else {
  1463. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1464. printk(KERN_WARNING "lost page write due to "
  1465. "I/O error on %s\n",
  1466. bdevname(bh->b_bdev, b));
  1467. }
  1468. /* note, we dont' set_buffer_write_io_error because we have
  1469. * our own ways of dealing with the IO errors
  1470. */
  1471. clear_buffer_uptodate(bh);
  1472. }
  1473. unlock_buffer(bh);
  1474. put_bh(bh);
  1475. }
  1476. int write_all_supers(struct btrfs_root *root)
  1477. {
  1478. struct list_head *cur;
  1479. struct list_head *head = &root->fs_info->fs_devices->devices;
  1480. struct btrfs_device *dev;
  1481. struct btrfs_super_block *sb;
  1482. struct btrfs_dev_item *dev_item;
  1483. struct buffer_head *bh;
  1484. int ret;
  1485. int do_barriers;
  1486. int max_errors;
  1487. int total_errors = 0;
  1488. u32 crc;
  1489. u64 flags;
  1490. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1491. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1492. sb = &root->fs_info->super_for_commit;
  1493. dev_item = &sb->dev_item;
  1494. list_for_each(cur, head) {
  1495. dev = list_entry(cur, struct btrfs_device, dev_list);
  1496. if (!dev->bdev) {
  1497. total_errors++;
  1498. continue;
  1499. }
  1500. if (!dev->in_fs_metadata)
  1501. continue;
  1502. btrfs_set_stack_device_type(dev_item, dev->type);
  1503. btrfs_set_stack_device_id(dev_item, dev->devid);
  1504. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1505. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1506. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1507. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1508. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1509. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1510. flags = btrfs_super_flags(sb);
  1511. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1512. crc = ~(u32)0;
  1513. crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
  1514. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  1515. btrfs_csum_final(crc, sb->csum);
  1516. bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
  1517. BTRFS_SUPER_INFO_SIZE);
  1518. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1519. dev->pending_io = bh;
  1520. get_bh(bh);
  1521. set_buffer_uptodate(bh);
  1522. lock_buffer(bh);
  1523. bh->b_end_io = btrfs_end_buffer_write_sync;
  1524. if (do_barriers && dev->barriers) {
  1525. ret = submit_bh(WRITE_BARRIER, bh);
  1526. if (ret == -EOPNOTSUPP) {
  1527. printk("btrfs: disabling barriers on dev %s\n",
  1528. dev->name);
  1529. set_buffer_uptodate(bh);
  1530. dev->barriers = 0;
  1531. get_bh(bh);
  1532. lock_buffer(bh);
  1533. ret = submit_bh(WRITE, bh);
  1534. }
  1535. } else {
  1536. ret = submit_bh(WRITE, bh);
  1537. }
  1538. if (ret)
  1539. total_errors++;
  1540. }
  1541. if (total_errors > max_errors) {
  1542. printk("btrfs: %d errors while writing supers\n", total_errors);
  1543. BUG();
  1544. }
  1545. total_errors = 0;
  1546. list_for_each(cur, head) {
  1547. dev = list_entry(cur, struct btrfs_device, dev_list);
  1548. if (!dev->bdev)
  1549. continue;
  1550. if (!dev->in_fs_metadata)
  1551. continue;
  1552. BUG_ON(!dev->pending_io);
  1553. bh = dev->pending_io;
  1554. wait_on_buffer(bh);
  1555. if (!buffer_uptodate(dev->pending_io)) {
  1556. if (do_barriers && dev->barriers) {
  1557. printk("btrfs: disabling barriers on dev %s\n",
  1558. dev->name);
  1559. set_buffer_uptodate(bh);
  1560. get_bh(bh);
  1561. lock_buffer(bh);
  1562. dev->barriers = 0;
  1563. ret = submit_bh(WRITE, bh);
  1564. BUG_ON(ret);
  1565. wait_on_buffer(bh);
  1566. if (!buffer_uptodate(bh))
  1567. total_errors++;
  1568. } else {
  1569. total_errors++;
  1570. }
  1571. }
  1572. dev->pending_io = NULL;
  1573. brelse(bh);
  1574. }
  1575. if (total_errors > max_errors) {
  1576. printk("btrfs: %d errors while writing supers\n", total_errors);
  1577. BUG();
  1578. }
  1579. return 0;
  1580. }
  1581. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  1582. *root)
  1583. {
  1584. int ret;
  1585. ret = write_all_supers(root);
  1586. return ret;
  1587. }
  1588. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1589. {
  1590. radix_tree_delete(&fs_info->fs_roots_radix,
  1591. (unsigned long)root->root_key.objectid);
  1592. if (root->in_sysfs)
  1593. btrfs_sysfs_del_root(root);
  1594. if (root->inode)
  1595. iput(root->inode);
  1596. if (root->node)
  1597. free_extent_buffer(root->node);
  1598. if (root->commit_root)
  1599. free_extent_buffer(root->commit_root);
  1600. if (root->name)
  1601. kfree(root->name);
  1602. kfree(root);
  1603. return 0;
  1604. }
  1605. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1606. {
  1607. int ret;
  1608. struct btrfs_root *gang[8];
  1609. int i;
  1610. while(1) {
  1611. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1612. (void **)gang, 0,
  1613. ARRAY_SIZE(gang));
  1614. if (!ret)
  1615. break;
  1616. for (i = 0; i < ret; i++)
  1617. btrfs_free_fs_root(fs_info, gang[i]);
  1618. }
  1619. return 0;
  1620. }
  1621. int close_ctree(struct btrfs_root *root)
  1622. {
  1623. int ret;
  1624. struct btrfs_trans_handle *trans;
  1625. struct btrfs_fs_info *fs_info = root->fs_info;
  1626. fs_info->closing = 1;
  1627. smp_mb();
  1628. kthread_stop(root->fs_info->transaction_kthread);
  1629. kthread_stop(root->fs_info->cleaner_kthread);
  1630. btrfs_clean_old_snapshots(root);
  1631. trans = btrfs_start_transaction(root, 1);
  1632. ret = btrfs_commit_transaction(trans, root);
  1633. /* run commit again to drop the original snapshot */
  1634. trans = btrfs_start_transaction(root, 1);
  1635. btrfs_commit_transaction(trans, root);
  1636. ret = btrfs_write_and_wait_transaction(NULL, root);
  1637. BUG_ON(ret);
  1638. write_ctree_super(NULL, root);
  1639. if (fs_info->delalloc_bytes) {
  1640. printk("btrfs: at unmount delalloc count %Lu\n",
  1641. fs_info->delalloc_bytes);
  1642. }
  1643. if (fs_info->total_ref_cache_size) {
  1644. printk("btrfs: at umount reference cache size %Lu\n",
  1645. fs_info->total_ref_cache_size);
  1646. }
  1647. if (fs_info->extent_root->node)
  1648. free_extent_buffer(fs_info->extent_root->node);
  1649. if (fs_info->tree_root->node)
  1650. free_extent_buffer(fs_info->tree_root->node);
  1651. if (root->fs_info->chunk_root->node);
  1652. free_extent_buffer(root->fs_info->chunk_root->node);
  1653. if (root->fs_info->dev_root->node);
  1654. free_extent_buffer(root->fs_info->dev_root->node);
  1655. btrfs_free_block_groups(root->fs_info);
  1656. fs_info->closing = 2;
  1657. del_fs_roots(fs_info);
  1658. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1659. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  1660. btrfs_stop_workers(&fs_info->fixup_workers);
  1661. btrfs_stop_workers(&fs_info->workers);
  1662. btrfs_stop_workers(&fs_info->endio_workers);
  1663. btrfs_stop_workers(&fs_info->endio_write_workers);
  1664. btrfs_stop_workers(&fs_info->submit_workers);
  1665. iput(fs_info->btree_inode);
  1666. #if 0
  1667. while(!list_empty(&fs_info->hashers)) {
  1668. struct btrfs_hasher *hasher;
  1669. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1670. hashers);
  1671. list_del(&hasher->hashers);
  1672. crypto_free_hash(&fs_info->hash_tfm);
  1673. kfree(hasher);
  1674. }
  1675. #endif
  1676. btrfs_close_devices(fs_info->fs_devices);
  1677. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1678. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1679. bdi_destroy(&fs_info->bdi);
  1680. #endif
  1681. kfree(fs_info->extent_root);
  1682. kfree(fs_info->tree_root);
  1683. kfree(fs_info->chunk_root);
  1684. kfree(fs_info->dev_root);
  1685. return 0;
  1686. }
  1687. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  1688. {
  1689. int ret;
  1690. struct inode *btree_inode = buf->first_page->mapping->host;
  1691. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1692. if (!ret)
  1693. return ret;
  1694. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  1695. parent_transid);
  1696. return !ret;
  1697. }
  1698. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  1699. {
  1700. struct inode *btree_inode = buf->first_page->mapping->host;
  1701. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  1702. buf);
  1703. }
  1704. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  1705. {
  1706. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1707. u64 transid = btrfs_header_generation(buf);
  1708. struct inode *btree_inode = root->fs_info->btree_inode;
  1709. WARN_ON(!btrfs_tree_locked(buf));
  1710. if (transid != root->fs_info->generation) {
  1711. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  1712. (unsigned long long)buf->start,
  1713. transid, root->fs_info->generation);
  1714. WARN_ON(1);
  1715. }
  1716. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  1717. }
  1718. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  1719. {
  1720. /*
  1721. * looks as though older kernels can get into trouble with
  1722. * this code, they end up stuck in balance_dirty_pages forever
  1723. */
  1724. struct extent_io_tree *tree;
  1725. u64 num_dirty;
  1726. u64 start = 0;
  1727. unsigned long thresh = 96 * 1024 * 1024;
  1728. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  1729. if (current_is_pdflush() || current->flags & PF_MEMALLOC)
  1730. return;
  1731. num_dirty = count_range_bits(tree, &start, (u64)-1,
  1732. thresh, EXTENT_DIRTY);
  1733. if (num_dirty > thresh) {
  1734. balance_dirty_pages_ratelimited_nr(
  1735. root->fs_info->btree_inode->i_mapping, 1);
  1736. }
  1737. return;
  1738. }
  1739. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  1740. {
  1741. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1742. int ret;
  1743. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  1744. if (ret == 0) {
  1745. buf->flags |= EXTENT_UPTODATE;
  1746. }
  1747. return ret;
  1748. }
  1749. int btree_lock_page_hook(struct page *page)
  1750. {
  1751. struct inode *inode = page->mapping->host;
  1752. struct btrfs_root *root = BTRFS_I(inode)->root;
  1753. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1754. struct extent_buffer *eb;
  1755. unsigned long len;
  1756. u64 bytenr = page_offset(page);
  1757. if (page->private == EXTENT_PAGE_PRIVATE)
  1758. goto out;
  1759. len = page->private >> 2;
  1760. eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
  1761. if (!eb)
  1762. goto out;
  1763. btrfs_tree_lock(eb);
  1764. spin_lock(&root->fs_info->hash_lock);
  1765. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  1766. spin_unlock(&root->fs_info->hash_lock);
  1767. btrfs_tree_unlock(eb);
  1768. free_extent_buffer(eb);
  1769. out:
  1770. lock_page(page);
  1771. return 0;
  1772. }
  1773. static struct extent_io_ops btree_extent_io_ops = {
  1774. .write_cache_pages_lock_hook = btree_lock_page_hook,
  1775. .writepage_io_hook = btree_writepage_io_hook,
  1776. .readpage_end_io_hook = btree_readpage_end_io_hook,
  1777. .submit_bio_hook = btree_submit_bio_hook,
  1778. /* note we're sharing with inode.c for the merge bio hook */
  1779. .merge_bio_hook = btrfs_merge_bio_hook,
  1780. };