disk-io.c 55 KB

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