disk-io.c 59 KB

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