disk-io.c 53 KB

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