disk-io.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  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 (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. tree = &BTRFS_I(page->mapping->host)->io_tree;
  527. map = &BTRFS_I(page->mapping->host)->extent_tree;
  528. ret = try_release_extent_state(map, tree, page, gfp_flags);
  529. if (!ret) {
  530. return 0;
  531. }
  532. ret = try_release_extent_buffer(tree, page);
  533. if (ret == 1) {
  534. ClearPagePrivate(page);
  535. set_page_private(page, 0);
  536. page_cache_release(page);
  537. }
  538. return ret;
  539. }
  540. static void btree_invalidatepage(struct page *page, unsigned long offset)
  541. {
  542. struct extent_io_tree *tree;
  543. tree = &BTRFS_I(page->mapping->host)->io_tree;
  544. extent_invalidatepage(tree, page, offset);
  545. btree_releasepage(page, GFP_NOFS);
  546. if (PagePrivate(page)) {
  547. printk("warning page private not zero on page %Lu\n",
  548. page_offset(page));
  549. ClearPagePrivate(page);
  550. set_page_private(page, 0);
  551. page_cache_release(page);
  552. }
  553. }
  554. #if 0
  555. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  556. {
  557. struct buffer_head *bh;
  558. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  559. struct buffer_head *head;
  560. if (!page_has_buffers(page)) {
  561. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  562. (1 << BH_Dirty)|(1 << BH_Uptodate));
  563. }
  564. head = page_buffers(page);
  565. bh = head;
  566. do {
  567. if (buffer_dirty(bh))
  568. csum_tree_block(root, bh, 0);
  569. bh = bh->b_this_page;
  570. } while (bh != head);
  571. return block_write_full_page(page, btree_get_block, wbc);
  572. }
  573. #endif
  574. static struct address_space_operations btree_aops = {
  575. .readpage = btree_readpage,
  576. .writepage = btree_writepage,
  577. .writepages = btree_writepages,
  578. .releasepage = btree_releasepage,
  579. .invalidatepage = btree_invalidatepage,
  580. .sync_page = block_sync_page,
  581. };
  582. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  583. u64 parent_transid)
  584. {
  585. struct extent_buffer *buf = NULL;
  586. struct inode *btree_inode = root->fs_info->btree_inode;
  587. int ret = 0;
  588. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  589. if (!buf)
  590. return 0;
  591. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  592. buf, 0, 0, btree_get_extent, 0);
  593. free_extent_buffer(buf);
  594. return ret;
  595. }
  596. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  597. u64 bytenr, u32 blocksize)
  598. {
  599. struct inode *btree_inode = root->fs_info->btree_inode;
  600. struct extent_buffer *eb;
  601. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  602. bytenr, blocksize, GFP_NOFS);
  603. return eb;
  604. }
  605. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  606. u64 bytenr, u32 blocksize)
  607. {
  608. struct inode *btree_inode = root->fs_info->btree_inode;
  609. struct extent_buffer *eb;
  610. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  611. bytenr, blocksize, NULL, GFP_NOFS);
  612. return eb;
  613. }
  614. int btrfs_write_tree_block(struct extent_buffer *buf)
  615. {
  616. return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
  617. buf->start + buf->len - 1, WB_SYNC_NONE);
  618. }
  619. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  620. {
  621. return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
  622. buf->start, buf->start + buf->len -1);
  623. }
  624. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  625. u32 blocksize, u64 parent_transid)
  626. {
  627. struct extent_buffer *buf = NULL;
  628. struct inode *btree_inode = root->fs_info->btree_inode;
  629. struct extent_io_tree *io_tree;
  630. int ret;
  631. io_tree = &BTRFS_I(btree_inode)->io_tree;
  632. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  633. if (!buf)
  634. return NULL;
  635. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  636. if (ret == 0) {
  637. buf->flags |= EXTENT_UPTODATE;
  638. } else {
  639. WARN_ON(1);
  640. }
  641. return buf;
  642. }
  643. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  644. struct extent_buffer *buf)
  645. {
  646. struct inode *btree_inode = root->fs_info->btree_inode;
  647. if (btrfs_header_generation(buf) ==
  648. root->fs_info->running_transaction->transid) {
  649. WARN_ON(!btrfs_tree_locked(buf));
  650. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  651. buf);
  652. }
  653. return 0;
  654. }
  655. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  656. u32 stripesize, struct btrfs_root *root,
  657. struct btrfs_fs_info *fs_info,
  658. u64 objectid)
  659. {
  660. root->node = NULL;
  661. root->inode = NULL;
  662. root->commit_root = NULL;
  663. root->ref_tree = NULL;
  664. root->sectorsize = sectorsize;
  665. root->nodesize = nodesize;
  666. root->leafsize = leafsize;
  667. root->stripesize = stripesize;
  668. root->ref_cows = 0;
  669. root->track_dirty = 0;
  670. root->fs_info = fs_info;
  671. root->objectid = objectid;
  672. root->last_trans = 0;
  673. root->highest_inode = 0;
  674. root->last_inode_alloc = 0;
  675. root->name = NULL;
  676. root->in_sysfs = 0;
  677. INIT_LIST_HEAD(&root->dirty_list);
  678. INIT_LIST_HEAD(&root->orphan_list);
  679. INIT_LIST_HEAD(&root->dead_list);
  680. spin_lock_init(&root->node_lock);
  681. spin_lock_init(&root->list_lock);
  682. mutex_init(&root->objectid_mutex);
  683. mutex_init(&root->log_mutex);
  684. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  685. root->ref_tree = &root->ref_tree_struct;
  686. memset(&root->root_key, 0, sizeof(root->root_key));
  687. memset(&root->root_item, 0, sizeof(root->root_item));
  688. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  689. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  690. root->defrag_trans_start = fs_info->generation;
  691. init_completion(&root->kobj_unregister);
  692. root->defrag_running = 0;
  693. root->defrag_level = 0;
  694. root->root_key.objectid = objectid;
  695. return 0;
  696. }
  697. static int find_and_setup_root(struct btrfs_root *tree_root,
  698. struct btrfs_fs_info *fs_info,
  699. u64 objectid,
  700. struct btrfs_root *root)
  701. {
  702. int ret;
  703. u32 blocksize;
  704. __setup_root(tree_root->nodesize, tree_root->leafsize,
  705. tree_root->sectorsize, tree_root->stripesize,
  706. root, fs_info, objectid);
  707. ret = btrfs_find_last_root(tree_root, objectid,
  708. &root->root_item, &root->root_key);
  709. BUG_ON(ret);
  710. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  711. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  712. blocksize, 0);
  713. BUG_ON(!root->node);
  714. return 0;
  715. }
  716. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  717. struct btrfs_fs_info *fs_info)
  718. {
  719. struct extent_buffer *eb;
  720. int ret;
  721. if (!fs_info->log_root_tree)
  722. return 0;
  723. eb = fs_info->log_root_tree->node;
  724. WARN_ON(btrfs_header_level(eb) != 0);
  725. WARN_ON(btrfs_header_nritems(eb) != 0);
  726. ret = btrfs_free_extent(trans, fs_info->tree_root,
  727. eb->start, eb->len,
  728. BTRFS_TREE_LOG_OBJECTID, 0, 0, 0, 1);
  729. BUG_ON(ret);
  730. free_extent_buffer(eb);
  731. kfree(fs_info->log_root_tree);
  732. fs_info->log_root_tree = NULL;
  733. return 0;
  734. }
  735. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  736. struct btrfs_fs_info *fs_info)
  737. {
  738. struct btrfs_root *root;
  739. struct btrfs_root *tree_root = fs_info->tree_root;
  740. root = kzalloc(sizeof(*root), GFP_NOFS);
  741. if (!root)
  742. return -ENOMEM;
  743. __setup_root(tree_root->nodesize, tree_root->leafsize,
  744. tree_root->sectorsize, tree_root->stripesize,
  745. root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  746. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  747. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  748. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  749. root->ref_cows = 0;
  750. root->node = btrfs_alloc_free_block(trans, root, root->leafsize,
  751. BTRFS_TREE_LOG_OBJECTID,
  752. 0, 0, 0, 0, 0);
  753. btrfs_set_header_nritems(root->node, 0);
  754. btrfs_set_header_level(root->node, 0);
  755. btrfs_set_header_bytenr(root->node, root->node->start);
  756. btrfs_set_header_generation(root->node, trans->transid);
  757. btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID);
  758. write_extent_buffer(root->node, root->fs_info->fsid,
  759. (unsigned long)btrfs_header_fsid(root->node),
  760. BTRFS_FSID_SIZE);
  761. btrfs_mark_buffer_dirty(root->node);
  762. btrfs_tree_unlock(root->node);
  763. fs_info->log_root_tree = root;
  764. return 0;
  765. }
  766. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
  767. struct btrfs_key *location)
  768. {
  769. struct btrfs_root *root;
  770. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  771. struct btrfs_path *path;
  772. struct extent_buffer *l;
  773. u64 highest_inode;
  774. u32 blocksize;
  775. int ret = 0;
  776. root = kzalloc(sizeof(*root), GFP_NOFS);
  777. if (!root)
  778. return ERR_PTR(-ENOMEM);
  779. if (location->offset == (u64)-1) {
  780. ret = find_and_setup_root(tree_root, fs_info,
  781. location->objectid, root);
  782. if (ret) {
  783. kfree(root);
  784. return ERR_PTR(ret);
  785. }
  786. goto insert;
  787. }
  788. __setup_root(tree_root->nodesize, tree_root->leafsize,
  789. tree_root->sectorsize, tree_root->stripesize,
  790. root, fs_info, location->objectid);
  791. path = btrfs_alloc_path();
  792. BUG_ON(!path);
  793. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  794. if (ret != 0) {
  795. if (ret > 0)
  796. ret = -ENOENT;
  797. goto out;
  798. }
  799. l = path->nodes[0];
  800. read_extent_buffer(l, &root->root_item,
  801. btrfs_item_ptr_offset(l, path->slots[0]),
  802. sizeof(root->root_item));
  803. memcpy(&root->root_key, location, sizeof(*location));
  804. ret = 0;
  805. out:
  806. btrfs_release_path(root, path);
  807. btrfs_free_path(path);
  808. if (ret) {
  809. kfree(root);
  810. return ERR_PTR(ret);
  811. }
  812. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  813. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  814. blocksize, 0);
  815. BUG_ON(!root->node);
  816. insert:
  817. if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
  818. root->ref_cows = 1;
  819. ret = btrfs_find_highest_inode(root, &highest_inode);
  820. if (ret == 0) {
  821. root->highest_inode = highest_inode;
  822. root->last_inode_alloc = highest_inode;
  823. }
  824. }
  825. return root;
  826. }
  827. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  828. u64 root_objectid)
  829. {
  830. struct btrfs_root *root;
  831. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  832. return fs_info->tree_root;
  833. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  834. return fs_info->extent_root;
  835. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  836. (unsigned long)root_objectid);
  837. return root;
  838. }
  839. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  840. struct btrfs_key *location)
  841. {
  842. struct btrfs_root *root;
  843. int ret;
  844. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  845. return fs_info->tree_root;
  846. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  847. return fs_info->extent_root;
  848. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  849. return fs_info->chunk_root;
  850. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  851. return fs_info->dev_root;
  852. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  853. (unsigned long)location->objectid);
  854. if (root)
  855. return root;
  856. root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
  857. if (IS_ERR(root))
  858. return root;
  859. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  860. (unsigned long)root->root_key.objectid,
  861. root);
  862. if (ret) {
  863. free_extent_buffer(root->node);
  864. kfree(root);
  865. return ERR_PTR(ret);
  866. }
  867. ret = btrfs_find_dead_roots(fs_info->tree_root,
  868. root->root_key.objectid, root);
  869. BUG_ON(ret);
  870. return root;
  871. }
  872. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  873. struct btrfs_key *location,
  874. const char *name, int namelen)
  875. {
  876. struct btrfs_root *root;
  877. int ret;
  878. root = btrfs_read_fs_root_no_name(fs_info, location);
  879. if (!root)
  880. return NULL;
  881. if (root->in_sysfs)
  882. return root;
  883. ret = btrfs_set_root_name(root, name, namelen);
  884. if (ret) {
  885. free_extent_buffer(root->node);
  886. kfree(root);
  887. return ERR_PTR(ret);
  888. }
  889. ret = btrfs_sysfs_add_root(root);
  890. if (ret) {
  891. free_extent_buffer(root->node);
  892. kfree(root->name);
  893. kfree(root);
  894. return ERR_PTR(ret);
  895. }
  896. root->in_sysfs = 1;
  897. return root;
  898. }
  899. #if 0
  900. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  901. struct btrfs_hasher *hasher;
  902. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  903. if (!hasher)
  904. return -ENOMEM;
  905. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  906. if (!hasher->hash_tfm) {
  907. kfree(hasher);
  908. return -EINVAL;
  909. }
  910. spin_lock(&info->hash_lock);
  911. list_add(&hasher->list, &info->hashers);
  912. spin_unlock(&info->hash_lock);
  913. return 0;
  914. }
  915. #endif
  916. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  917. {
  918. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  919. int ret = 0;
  920. struct list_head *cur;
  921. struct btrfs_device *device;
  922. struct backing_dev_info *bdi;
  923. if ((bdi_bits & (1 << BDI_write_congested)) &&
  924. btrfs_congested_async(info, 0))
  925. return 1;
  926. list_for_each(cur, &info->fs_devices->devices) {
  927. device = list_entry(cur, struct btrfs_device, dev_list);
  928. if (!device->bdev)
  929. continue;
  930. bdi = blk_get_backing_dev_info(device->bdev);
  931. if (bdi && bdi_congested(bdi, bdi_bits)) {
  932. ret = 1;
  933. break;
  934. }
  935. }
  936. return ret;
  937. }
  938. /*
  939. * this unplugs every device on the box, and it is only used when page
  940. * is null
  941. */
  942. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  943. {
  944. struct list_head *cur;
  945. struct btrfs_device *device;
  946. struct btrfs_fs_info *info;
  947. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  948. list_for_each(cur, &info->fs_devices->devices) {
  949. device = list_entry(cur, struct btrfs_device, dev_list);
  950. bdi = blk_get_backing_dev_info(device->bdev);
  951. if (bdi->unplug_io_fn) {
  952. bdi->unplug_io_fn(bdi, page);
  953. }
  954. }
  955. }
  956. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  957. {
  958. struct inode *inode;
  959. struct extent_map_tree *em_tree;
  960. struct extent_map *em;
  961. struct address_space *mapping;
  962. u64 offset;
  963. /* the generic O_DIRECT read code does this */
  964. if (!page) {
  965. __unplug_io_fn(bdi, page);
  966. return;
  967. }
  968. /*
  969. * page->mapping may change at any time. Get a consistent copy
  970. * and use that for everything below
  971. */
  972. smp_mb();
  973. mapping = page->mapping;
  974. if (!mapping)
  975. return;
  976. inode = mapping->host;
  977. offset = page_offset(page);
  978. em_tree = &BTRFS_I(inode)->extent_tree;
  979. spin_lock(&em_tree->lock);
  980. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  981. spin_unlock(&em_tree->lock);
  982. if (!em) {
  983. __unplug_io_fn(bdi, page);
  984. return;
  985. }
  986. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  987. free_extent_map(em);
  988. __unplug_io_fn(bdi, page);
  989. return;
  990. }
  991. offset = offset - em->start;
  992. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  993. em->block_start + offset, page);
  994. free_extent_map(em);
  995. }
  996. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  997. {
  998. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  999. bdi_init(bdi);
  1000. #endif
  1001. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1002. bdi->state = 0;
  1003. bdi->capabilities = default_backing_dev_info.capabilities;
  1004. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  1005. bdi->unplug_io_data = info;
  1006. bdi->congested_fn = btrfs_congested_fn;
  1007. bdi->congested_data = info;
  1008. return 0;
  1009. }
  1010. static int bio_ready_for_csum(struct bio *bio)
  1011. {
  1012. u64 length = 0;
  1013. u64 buf_len = 0;
  1014. u64 start = 0;
  1015. struct page *page;
  1016. struct extent_io_tree *io_tree = NULL;
  1017. struct btrfs_fs_info *info = NULL;
  1018. struct bio_vec *bvec;
  1019. int i;
  1020. int ret;
  1021. bio_for_each_segment(bvec, bio, i) {
  1022. page = bvec->bv_page;
  1023. if (page->private == EXTENT_PAGE_PRIVATE) {
  1024. length += bvec->bv_len;
  1025. continue;
  1026. }
  1027. if (!page->private) {
  1028. length += bvec->bv_len;
  1029. continue;
  1030. }
  1031. length = bvec->bv_len;
  1032. buf_len = page->private >> 2;
  1033. start = page_offset(page) + bvec->bv_offset;
  1034. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  1035. info = BTRFS_I(page->mapping->host)->root->fs_info;
  1036. }
  1037. /* are we fully contained in this bio? */
  1038. if (buf_len <= length)
  1039. return 1;
  1040. ret = extent_range_uptodate(io_tree, start + length,
  1041. start + buf_len - 1);
  1042. if (ret == 1)
  1043. return ret;
  1044. return ret;
  1045. }
  1046. /*
  1047. * called by the kthread helper functions to finally call the bio end_io
  1048. * functions. This is where read checksum verification actually happens
  1049. */
  1050. static void end_workqueue_fn(struct btrfs_work *work)
  1051. {
  1052. struct bio *bio;
  1053. struct end_io_wq *end_io_wq;
  1054. struct btrfs_fs_info *fs_info;
  1055. int error;
  1056. end_io_wq = container_of(work, struct end_io_wq, work);
  1057. bio = end_io_wq->bio;
  1058. fs_info = end_io_wq->info;
  1059. /* metadata bios are special because the whole tree block must
  1060. * be checksummed at once. This makes sure the entire block is in
  1061. * ram and up to date before trying to verify things. For
  1062. * blocksize <= pagesize, it is basically a noop
  1063. */
  1064. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  1065. btrfs_queue_worker(&fs_info->endio_workers,
  1066. &end_io_wq->work);
  1067. return;
  1068. }
  1069. error = end_io_wq->error;
  1070. bio->bi_private = end_io_wq->private;
  1071. bio->bi_end_io = end_io_wq->end_io;
  1072. kfree(end_io_wq);
  1073. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1074. bio_endio(bio, bio->bi_size, error);
  1075. #else
  1076. bio_endio(bio, error);
  1077. #endif
  1078. }
  1079. static int cleaner_kthread(void *arg)
  1080. {
  1081. struct btrfs_root *root = arg;
  1082. do {
  1083. smp_mb();
  1084. if (root->fs_info->closing)
  1085. break;
  1086. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1087. mutex_lock(&root->fs_info->cleaner_mutex);
  1088. btrfs_clean_old_snapshots(root);
  1089. mutex_unlock(&root->fs_info->cleaner_mutex);
  1090. if (freezing(current)) {
  1091. refrigerator();
  1092. } else {
  1093. smp_mb();
  1094. if (root->fs_info->closing)
  1095. break;
  1096. set_current_state(TASK_INTERRUPTIBLE);
  1097. schedule();
  1098. __set_current_state(TASK_RUNNING);
  1099. }
  1100. } while (!kthread_should_stop());
  1101. return 0;
  1102. }
  1103. static int transaction_kthread(void *arg)
  1104. {
  1105. struct btrfs_root *root = arg;
  1106. struct btrfs_trans_handle *trans;
  1107. struct btrfs_transaction *cur;
  1108. unsigned long now;
  1109. unsigned long delay;
  1110. int ret;
  1111. do {
  1112. smp_mb();
  1113. if (root->fs_info->closing)
  1114. break;
  1115. delay = HZ * 30;
  1116. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1117. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1118. if (root->fs_info->total_ref_cache_size > 20 * 1024 * 1024) {
  1119. printk("btrfs: total reference cache size %Lu\n",
  1120. root->fs_info->total_ref_cache_size);
  1121. }
  1122. mutex_lock(&root->fs_info->trans_mutex);
  1123. cur = root->fs_info->running_transaction;
  1124. if (!cur) {
  1125. mutex_unlock(&root->fs_info->trans_mutex);
  1126. goto sleep;
  1127. }
  1128. now = get_seconds();
  1129. if (now < cur->start_time || now - cur->start_time < 30) {
  1130. mutex_unlock(&root->fs_info->trans_mutex);
  1131. delay = HZ * 5;
  1132. goto sleep;
  1133. }
  1134. mutex_unlock(&root->fs_info->trans_mutex);
  1135. trans = btrfs_start_transaction(root, 1);
  1136. ret = btrfs_commit_transaction(trans, root);
  1137. sleep:
  1138. wake_up_process(root->fs_info->cleaner_kthread);
  1139. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1140. if (freezing(current)) {
  1141. refrigerator();
  1142. } else {
  1143. if (root->fs_info->closing)
  1144. break;
  1145. set_current_state(TASK_INTERRUPTIBLE);
  1146. schedule_timeout(delay);
  1147. __set_current_state(TASK_RUNNING);
  1148. }
  1149. } while (!kthread_should_stop());
  1150. return 0;
  1151. }
  1152. struct btrfs_root *open_ctree(struct super_block *sb,
  1153. struct btrfs_fs_devices *fs_devices,
  1154. char *options)
  1155. {
  1156. u32 sectorsize;
  1157. u32 nodesize;
  1158. u32 leafsize;
  1159. u32 blocksize;
  1160. u32 stripesize;
  1161. struct buffer_head *bh;
  1162. struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
  1163. GFP_NOFS);
  1164. struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root),
  1165. GFP_NOFS);
  1166. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1167. GFP_NOFS);
  1168. struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
  1169. GFP_NOFS);
  1170. struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
  1171. GFP_NOFS);
  1172. struct btrfs_root *log_tree_root;
  1173. int ret;
  1174. int err = -EINVAL;
  1175. struct btrfs_super_block *disk_super;
  1176. if (!extent_root || !tree_root || !fs_info) {
  1177. err = -ENOMEM;
  1178. goto fail;
  1179. }
  1180. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1181. INIT_LIST_HEAD(&fs_info->trans_list);
  1182. INIT_LIST_HEAD(&fs_info->dead_roots);
  1183. INIT_LIST_HEAD(&fs_info->hashers);
  1184. INIT_LIST_HEAD(&fs_info->delalloc_inodes);
  1185. spin_lock_init(&fs_info->hash_lock);
  1186. spin_lock_init(&fs_info->delalloc_lock);
  1187. spin_lock_init(&fs_info->new_trans_lock);
  1188. spin_lock_init(&fs_info->ref_cache_lock);
  1189. init_completion(&fs_info->kobj_unregister);
  1190. fs_info->tree_root = tree_root;
  1191. fs_info->extent_root = extent_root;
  1192. fs_info->chunk_root = chunk_root;
  1193. fs_info->dev_root = dev_root;
  1194. fs_info->fs_devices = fs_devices;
  1195. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1196. INIT_LIST_HEAD(&fs_info->space_info);
  1197. btrfs_mapping_init(&fs_info->mapping_tree);
  1198. atomic_set(&fs_info->nr_async_submits, 0);
  1199. atomic_set(&fs_info->nr_async_bios, 0);
  1200. atomic_set(&fs_info->throttles, 0);
  1201. atomic_set(&fs_info->throttle_gen, 0);
  1202. fs_info->sb = sb;
  1203. fs_info->max_extent = (u64)-1;
  1204. fs_info->max_inline = 8192 * 1024;
  1205. setup_bdi(fs_info, &fs_info->bdi);
  1206. fs_info->btree_inode = new_inode(sb);
  1207. fs_info->btree_inode->i_ino = 1;
  1208. fs_info->btree_inode->i_nlink = 1;
  1209. fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
  1210. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1211. spin_lock_init(&fs_info->ordered_extent_lock);
  1212. sb->s_blocksize = 4096;
  1213. sb->s_blocksize_bits = blksize_bits(4096);
  1214. /*
  1215. * we set the i_size on the btree inode to the max possible int.
  1216. * the real end of the address space is determined by all of
  1217. * the devices in the system
  1218. */
  1219. fs_info->btree_inode->i_size = OFFSET_MAX;
  1220. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1221. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1222. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1223. fs_info->btree_inode->i_mapping,
  1224. GFP_NOFS);
  1225. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1226. GFP_NOFS);
  1227. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1228. extent_io_tree_init(&fs_info->free_space_cache,
  1229. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1230. extent_io_tree_init(&fs_info->block_group_cache,
  1231. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1232. extent_io_tree_init(&fs_info->pinned_extents,
  1233. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1234. extent_io_tree_init(&fs_info->pending_del,
  1235. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1236. extent_io_tree_init(&fs_info->extent_ins,
  1237. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1238. fs_info->do_barriers = 1;
  1239. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1240. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1241. sizeof(struct btrfs_key));
  1242. insert_inode_hash(fs_info->btree_inode);
  1243. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  1244. mutex_init(&fs_info->trans_mutex);
  1245. mutex_init(&fs_info->tree_log_mutex);
  1246. mutex_init(&fs_info->drop_mutex);
  1247. mutex_init(&fs_info->alloc_mutex);
  1248. mutex_init(&fs_info->chunk_mutex);
  1249. mutex_init(&fs_info->transaction_kthread_mutex);
  1250. mutex_init(&fs_info->cleaner_mutex);
  1251. mutex_init(&fs_info->volume_mutex);
  1252. init_waitqueue_head(&fs_info->transaction_throttle);
  1253. init_waitqueue_head(&fs_info->transaction_wait);
  1254. init_waitqueue_head(&fs_info->async_submit_wait);
  1255. init_waitqueue_head(&fs_info->tree_log_wait);
  1256. atomic_set(&fs_info->tree_log_commit, 0);
  1257. atomic_set(&fs_info->tree_log_writers, 0);
  1258. fs_info->tree_log_transid = 0;
  1259. #if 0
  1260. ret = add_hasher(fs_info, "crc32c");
  1261. if (ret) {
  1262. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1263. err = -ENOMEM;
  1264. goto fail_iput;
  1265. }
  1266. #endif
  1267. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1268. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1269. bh = __bread(fs_devices->latest_bdev,
  1270. BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  1271. if (!bh)
  1272. goto fail_iput;
  1273. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1274. brelse(bh);
  1275. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1276. disk_super = &fs_info->super_copy;
  1277. if (!btrfs_super_root(disk_super))
  1278. goto fail_sb_buffer;
  1279. err = btrfs_parse_options(tree_root, options);
  1280. if (err)
  1281. goto fail_sb_buffer;
  1282. /*
  1283. * we need to start all the end_io workers up front because the
  1284. * queue work function gets called at interrupt time, and so it
  1285. * cannot dynamically grow.
  1286. */
  1287. btrfs_init_workers(&fs_info->workers, "worker",
  1288. fs_info->thread_pool_size);
  1289. btrfs_init_workers(&fs_info->submit_workers, "submit",
  1290. min_t(u64, fs_devices->num_devices,
  1291. fs_info->thread_pool_size));
  1292. /* a higher idle thresh on the submit workers makes it much more
  1293. * likely that bios will be send down in a sane order to the
  1294. * devices
  1295. */
  1296. fs_info->submit_workers.idle_thresh = 64;
  1297. /* fs_info->workers is responsible for checksumming file data
  1298. * blocks and metadata. Using a larger idle thresh allows each
  1299. * worker thread to operate on things in roughly the order they
  1300. * were sent by the writeback daemons, improving overall locality
  1301. * of the IO going down the pipe.
  1302. */
  1303. fs_info->workers.idle_thresh = 128;
  1304. btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
  1305. btrfs_init_workers(&fs_info->endio_workers, "endio",
  1306. fs_info->thread_pool_size);
  1307. btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
  1308. fs_info->thread_pool_size);
  1309. /*
  1310. * endios are largely parallel and should have a very
  1311. * low idle thresh
  1312. */
  1313. fs_info->endio_workers.idle_thresh = 4;
  1314. fs_info->endio_write_workers.idle_thresh = 4;
  1315. btrfs_start_workers(&fs_info->workers, 1);
  1316. btrfs_start_workers(&fs_info->submit_workers, 1);
  1317. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1318. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1319. btrfs_start_workers(&fs_info->endio_write_workers,
  1320. fs_info->thread_pool_size);
  1321. err = -EINVAL;
  1322. if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
  1323. printk("Btrfs: wanted %llu devices, but found %llu\n",
  1324. (unsigned long long)btrfs_super_num_devices(disk_super),
  1325. (unsigned long long)fs_devices->open_devices);
  1326. if (btrfs_test_opt(tree_root, DEGRADED))
  1327. printk("continuing in degraded mode\n");
  1328. else {
  1329. goto fail_sb_buffer;
  1330. }
  1331. }
  1332. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1333. nodesize = btrfs_super_nodesize(disk_super);
  1334. leafsize = btrfs_super_leafsize(disk_super);
  1335. sectorsize = btrfs_super_sectorsize(disk_super);
  1336. stripesize = btrfs_super_stripesize(disk_super);
  1337. tree_root->nodesize = nodesize;
  1338. tree_root->leafsize = leafsize;
  1339. tree_root->sectorsize = sectorsize;
  1340. tree_root->stripesize = stripesize;
  1341. sb->s_blocksize = sectorsize;
  1342. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1343. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1344. sizeof(disk_super->magic))) {
  1345. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1346. goto fail_sb_buffer;
  1347. }
  1348. mutex_lock(&fs_info->chunk_mutex);
  1349. ret = btrfs_read_sys_array(tree_root);
  1350. mutex_unlock(&fs_info->chunk_mutex);
  1351. if (ret) {
  1352. printk("btrfs: failed to read the system array on %s\n",
  1353. sb->s_id);
  1354. goto fail_sys_array;
  1355. }
  1356. blocksize = btrfs_level_size(tree_root,
  1357. btrfs_super_chunk_root_level(disk_super));
  1358. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1359. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1360. chunk_root->node = read_tree_block(chunk_root,
  1361. btrfs_super_chunk_root(disk_super),
  1362. blocksize, 0);
  1363. BUG_ON(!chunk_root->node);
  1364. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1365. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1366. BTRFS_UUID_SIZE);
  1367. mutex_lock(&fs_info->chunk_mutex);
  1368. ret = btrfs_read_chunk_tree(chunk_root);
  1369. mutex_unlock(&fs_info->chunk_mutex);
  1370. BUG_ON(ret);
  1371. btrfs_close_extra_devices(fs_devices);
  1372. blocksize = btrfs_level_size(tree_root,
  1373. btrfs_super_root_level(disk_super));
  1374. tree_root->node = read_tree_block(tree_root,
  1375. btrfs_super_root(disk_super),
  1376. blocksize, 0);
  1377. if (!tree_root->node)
  1378. goto fail_sb_buffer;
  1379. ret = find_and_setup_root(tree_root, fs_info,
  1380. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1381. if (ret)
  1382. goto fail_tree_root;
  1383. extent_root->track_dirty = 1;
  1384. ret = find_and_setup_root(tree_root, fs_info,
  1385. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1386. dev_root->track_dirty = 1;
  1387. if (ret)
  1388. goto fail_extent_root;
  1389. btrfs_read_block_groups(extent_root);
  1390. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  1391. fs_info->data_alloc_profile = (u64)-1;
  1392. fs_info->metadata_alloc_profile = (u64)-1;
  1393. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1394. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1395. "btrfs-cleaner");
  1396. if (!fs_info->cleaner_kthread)
  1397. goto fail_extent_root;
  1398. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1399. tree_root,
  1400. "btrfs-transaction");
  1401. if (!fs_info->transaction_kthread)
  1402. goto fail_cleaner;
  1403. if (btrfs_super_log_root(disk_super) != 0) {
  1404. u32 blocksize;
  1405. u64 bytenr = btrfs_super_log_root(disk_super);
  1406. blocksize =
  1407. btrfs_level_size(tree_root,
  1408. btrfs_super_log_root_level(disk_super));
  1409. log_tree_root = kzalloc(sizeof(struct btrfs_root),
  1410. GFP_NOFS);
  1411. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1412. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1413. log_tree_root->node = read_tree_block(tree_root, bytenr,
  1414. blocksize, 0);
  1415. ret = btrfs_recover_log_trees(log_tree_root);
  1416. BUG_ON(ret);
  1417. }
  1418. fs_info->last_trans_committed = btrfs_super_generation(disk_super);
  1419. return tree_root;
  1420. fail_cleaner:
  1421. kthread_stop(fs_info->cleaner_kthread);
  1422. fail_extent_root:
  1423. free_extent_buffer(extent_root->node);
  1424. fail_tree_root:
  1425. free_extent_buffer(tree_root->node);
  1426. fail_sys_array:
  1427. fail_sb_buffer:
  1428. btrfs_stop_workers(&fs_info->fixup_workers);
  1429. btrfs_stop_workers(&fs_info->workers);
  1430. btrfs_stop_workers(&fs_info->endio_workers);
  1431. btrfs_stop_workers(&fs_info->endio_write_workers);
  1432. btrfs_stop_workers(&fs_info->submit_workers);
  1433. fail_iput:
  1434. iput(fs_info->btree_inode);
  1435. fail:
  1436. btrfs_close_devices(fs_info->fs_devices);
  1437. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1438. kfree(extent_root);
  1439. kfree(tree_root);
  1440. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1441. bdi_destroy(&fs_info->bdi);
  1442. #endif
  1443. kfree(fs_info);
  1444. return ERR_PTR(err);
  1445. }
  1446. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1447. {
  1448. char b[BDEVNAME_SIZE];
  1449. if (uptodate) {
  1450. set_buffer_uptodate(bh);
  1451. } else {
  1452. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1453. printk(KERN_WARNING "lost page write due to "
  1454. "I/O error on %s\n",
  1455. bdevname(bh->b_bdev, b));
  1456. }
  1457. /* note, we dont' set_buffer_write_io_error because we have
  1458. * our own ways of dealing with the IO errors
  1459. */
  1460. clear_buffer_uptodate(bh);
  1461. }
  1462. unlock_buffer(bh);
  1463. put_bh(bh);
  1464. }
  1465. int write_all_supers(struct btrfs_root *root)
  1466. {
  1467. struct list_head *cur;
  1468. struct list_head *head = &root->fs_info->fs_devices->devices;
  1469. struct btrfs_device *dev;
  1470. struct btrfs_super_block *sb;
  1471. struct btrfs_dev_item *dev_item;
  1472. struct buffer_head *bh;
  1473. int ret;
  1474. int do_barriers;
  1475. int max_errors;
  1476. int total_errors = 0;
  1477. u32 crc;
  1478. u64 flags;
  1479. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1480. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1481. sb = &root->fs_info->super_for_commit;
  1482. dev_item = &sb->dev_item;
  1483. list_for_each(cur, head) {
  1484. dev = list_entry(cur, struct btrfs_device, dev_list);
  1485. if (!dev->bdev) {
  1486. total_errors++;
  1487. continue;
  1488. }
  1489. if (!dev->in_fs_metadata)
  1490. continue;
  1491. btrfs_set_stack_device_type(dev_item, dev->type);
  1492. btrfs_set_stack_device_id(dev_item, dev->devid);
  1493. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1494. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1495. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1496. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1497. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1498. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1499. flags = btrfs_super_flags(sb);
  1500. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1501. crc = ~(u32)0;
  1502. crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
  1503. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  1504. btrfs_csum_final(crc, sb->csum);
  1505. bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
  1506. BTRFS_SUPER_INFO_SIZE);
  1507. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1508. dev->pending_io = bh;
  1509. get_bh(bh);
  1510. set_buffer_uptodate(bh);
  1511. lock_buffer(bh);
  1512. bh->b_end_io = btrfs_end_buffer_write_sync;
  1513. if (do_barriers && dev->barriers) {
  1514. ret = submit_bh(WRITE_BARRIER, bh);
  1515. if (ret == -EOPNOTSUPP) {
  1516. printk("btrfs: disabling barriers on dev %s\n",
  1517. dev->name);
  1518. set_buffer_uptodate(bh);
  1519. dev->barriers = 0;
  1520. get_bh(bh);
  1521. lock_buffer(bh);
  1522. ret = submit_bh(WRITE, bh);
  1523. }
  1524. } else {
  1525. ret = submit_bh(WRITE, bh);
  1526. }
  1527. if (ret)
  1528. total_errors++;
  1529. }
  1530. if (total_errors > max_errors) {
  1531. printk("btrfs: %d errors while writing supers\n", total_errors);
  1532. BUG();
  1533. }
  1534. total_errors = 0;
  1535. list_for_each(cur, head) {
  1536. dev = list_entry(cur, struct btrfs_device, dev_list);
  1537. if (!dev->bdev)
  1538. continue;
  1539. if (!dev->in_fs_metadata)
  1540. continue;
  1541. BUG_ON(!dev->pending_io);
  1542. bh = dev->pending_io;
  1543. wait_on_buffer(bh);
  1544. if (!buffer_uptodate(dev->pending_io)) {
  1545. if (do_barriers && dev->barriers) {
  1546. printk("btrfs: disabling barriers on dev %s\n",
  1547. dev->name);
  1548. set_buffer_uptodate(bh);
  1549. get_bh(bh);
  1550. lock_buffer(bh);
  1551. dev->barriers = 0;
  1552. ret = submit_bh(WRITE, bh);
  1553. BUG_ON(ret);
  1554. wait_on_buffer(bh);
  1555. if (!buffer_uptodate(bh))
  1556. total_errors++;
  1557. } else {
  1558. total_errors++;
  1559. }
  1560. }
  1561. dev->pending_io = NULL;
  1562. brelse(bh);
  1563. }
  1564. if (total_errors > max_errors) {
  1565. printk("btrfs: %d errors while writing supers\n", total_errors);
  1566. BUG();
  1567. }
  1568. return 0;
  1569. }
  1570. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  1571. *root)
  1572. {
  1573. int ret;
  1574. ret = write_all_supers(root);
  1575. return ret;
  1576. }
  1577. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1578. {
  1579. radix_tree_delete(&fs_info->fs_roots_radix,
  1580. (unsigned long)root->root_key.objectid);
  1581. if (root->in_sysfs)
  1582. btrfs_sysfs_del_root(root);
  1583. if (root->inode)
  1584. iput(root->inode);
  1585. if (root->node)
  1586. free_extent_buffer(root->node);
  1587. if (root->commit_root)
  1588. free_extent_buffer(root->commit_root);
  1589. if (root->name)
  1590. kfree(root->name);
  1591. kfree(root);
  1592. return 0;
  1593. }
  1594. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1595. {
  1596. int ret;
  1597. struct btrfs_root *gang[8];
  1598. int i;
  1599. while(1) {
  1600. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1601. (void **)gang, 0,
  1602. ARRAY_SIZE(gang));
  1603. if (!ret)
  1604. break;
  1605. for (i = 0; i < ret; i++)
  1606. btrfs_free_fs_root(fs_info, gang[i]);
  1607. }
  1608. return 0;
  1609. }
  1610. int close_ctree(struct btrfs_root *root)
  1611. {
  1612. int ret;
  1613. struct btrfs_trans_handle *trans;
  1614. struct btrfs_fs_info *fs_info = root->fs_info;
  1615. fs_info->closing = 1;
  1616. smp_mb();
  1617. kthread_stop(root->fs_info->transaction_kthread);
  1618. kthread_stop(root->fs_info->cleaner_kthread);
  1619. btrfs_clean_old_snapshots(root);
  1620. trans = btrfs_start_transaction(root, 1);
  1621. ret = btrfs_commit_transaction(trans, root);
  1622. /* run commit again to drop the original snapshot */
  1623. trans = btrfs_start_transaction(root, 1);
  1624. btrfs_commit_transaction(trans, root);
  1625. ret = btrfs_write_and_wait_transaction(NULL, root);
  1626. BUG_ON(ret);
  1627. write_ctree_super(NULL, root);
  1628. if (fs_info->delalloc_bytes) {
  1629. printk("btrfs: at unmount delalloc count %Lu\n",
  1630. fs_info->delalloc_bytes);
  1631. }
  1632. if (fs_info->total_ref_cache_size) {
  1633. printk("btrfs: at umount reference cache size %Lu\n",
  1634. fs_info->total_ref_cache_size);
  1635. }
  1636. if (fs_info->extent_root->node)
  1637. free_extent_buffer(fs_info->extent_root->node);
  1638. if (fs_info->tree_root->node)
  1639. free_extent_buffer(fs_info->tree_root->node);
  1640. if (root->fs_info->chunk_root->node);
  1641. free_extent_buffer(root->fs_info->chunk_root->node);
  1642. if (root->fs_info->dev_root->node);
  1643. free_extent_buffer(root->fs_info->dev_root->node);
  1644. btrfs_free_block_groups(root->fs_info);
  1645. fs_info->closing = 2;
  1646. del_fs_roots(fs_info);
  1647. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1648. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  1649. btrfs_stop_workers(&fs_info->fixup_workers);
  1650. btrfs_stop_workers(&fs_info->workers);
  1651. btrfs_stop_workers(&fs_info->endio_workers);
  1652. btrfs_stop_workers(&fs_info->endio_write_workers);
  1653. btrfs_stop_workers(&fs_info->submit_workers);
  1654. iput(fs_info->btree_inode);
  1655. #if 0
  1656. while(!list_empty(&fs_info->hashers)) {
  1657. struct btrfs_hasher *hasher;
  1658. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1659. hashers);
  1660. list_del(&hasher->hashers);
  1661. crypto_free_hash(&fs_info->hash_tfm);
  1662. kfree(hasher);
  1663. }
  1664. #endif
  1665. btrfs_close_devices(fs_info->fs_devices);
  1666. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1667. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1668. bdi_destroy(&fs_info->bdi);
  1669. #endif
  1670. kfree(fs_info->extent_root);
  1671. kfree(fs_info->tree_root);
  1672. kfree(fs_info->chunk_root);
  1673. kfree(fs_info->dev_root);
  1674. return 0;
  1675. }
  1676. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  1677. {
  1678. int ret;
  1679. struct inode *btree_inode = buf->first_page->mapping->host;
  1680. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1681. if (!ret)
  1682. return ret;
  1683. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  1684. parent_transid);
  1685. return !ret;
  1686. }
  1687. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  1688. {
  1689. struct inode *btree_inode = buf->first_page->mapping->host;
  1690. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  1691. buf);
  1692. }
  1693. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  1694. {
  1695. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1696. u64 transid = btrfs_header_generation(buf);
  1697. struct inode *btree_inode = root->fs_info->btree_inode;
  1698. WARN_ON(!btrfs_tree_locked(buf));
  1699. if (transid != root->fs_info->generation) {
  1700. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  1701. (unsigned long long)buf->start,
  1702. transid, root->fs_info->generation);
  1703. WARN_ON(1);
  1704. }
  1705. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  1706. }
  1707. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  1708. {
  1709. /*
  1710. * looks as though older kernels can get into trouble with
  1711. * this code, they end up stuck in balance_dirty_pages forever
  1712. */
  1713. struct extent_io_tree *tree;
  1714. u64 num_dirty;
  1715. u64 start = 0;
  1716. unsigned long thresh = 96 * 1024 * 1024;
  1717. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  1718. if (current_is_pdflush() || current->flags & PF_MEMALLOC)
  1719. return;
  1720. num_dirty = count_range_bits(tree, &start, (u64)-1,
  1721. thresh, EXTENT_DIRTY);
  1722. if (num_dirty > thresh) {
  1723. balance_dirty_pages_ratelimited_nr(
  1724. root->fs_info->btree_inode->i_mapping, 1);
  1725. }
  1726. return;
  1727. }
  1728. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  1729. {
  1730. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1731. int ret;
  1732. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  1733. if (ret == 0) {
  1734. buf->flags |= EXTENT_UPTODATE;
  1735. }
  1736. return ret;
  1737. }
  1738. int btree_lock_page_hook(struct page *page)
  1739. {
  1740. struct inode *inode = page->mapping->host;
  1741. struct btrfs_root *root = BTRFS_I(inode)->root;
  1742. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1743. struct extent_buffer *eb;
  1744. unsigned long len;
  1745. u64 bytenr = page_offset(page);
  1746. if (page->private == EXTENT_PAGE_PRIVATE)
  1747. goto out;
  1748. len = page->private >> 2;
  1749. eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
  1750. if (!eb)
  1751. goto out;
  1752. btrfs_tree_lock(eb);
  1753. spin_lock(&root->fs_info->hash_lock);
  1754. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  1755. spin_unlock(&root->fs_info->hash_lock);
  1756. btrfs_tree_unlock(eb);
  1757. free_extent_buffer(eb);
  1758. out:
  1759. lock_page(page);
  1760. return 0;
  1761. }
  1762. static struct extent_io_ops btree_extent_io_ops = {
  1763. .write_cache_pages_lock_hook = btree_lock_page_hook,
  1764. .writepage_io_hook = btree_writepage_io_hook,
  1765. .readpage_end_io_hook = btree_readpage_end_io_hook,
  1766. .submit_bio_hook = btree_submit_bio_hook,
  1767. /* note we're sharing with inode.c for the merge bio hook */
  1768. .merge_bio_hook = btrfs_merge_bio_hook,
  1769. };