disk-io.c 53 KB

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