disk-io.c 64 KB

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