disk-io.c 65 KB

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