disk-io.c 53 KB

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