disk-io.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  596. struct extent_buffer *eb;
  597. int was_dirty;
  598. tree = &BTRFS_I(page->mapping->host)->io_tree;
  599. if (!(current->flags & PF_MEMALLOC)) {
  600. return extent_write_full_page(tree, page,
  601. btree_get_extent, wbc);
  602. }
  603. redirty_page_for_writepage(wbc, page);
  604. eb = btrfs_find_tree_block(root, page_offset(page),
  605. PAGE_CACHE_SIZE);
  606. WARN_ON(!eb);
  607. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  608. if (!was_dirty) {
  609. spin_lock(&root->fs_info->delalloc_lock);
  610. root->fs_info->dirty_metadata_bytes += PAGE_CACHE_SIZE;
  611. spin_unlock(&root->fs_info->delalloc_lock);
  612. }
  613. free_extent_buffer(eb);
  614. unlock_page(page);
  615. return 0;
  616. }
  617. static int btree_writepages(struct address_space *mapping,
  618. struct writeback_control *wbc)
  619. {
  620. struct extent_io_tree *tree;
  621. tree = &BTRFS_I(mapping->host)->io_tree;
  622. if (wbc->sync_mode == WB_SYNC_NONE) {
  623. struct btrfs_root *root = BTRFS_I(mapping->host)->root;
  624. u64 num_dirty;
  625. unsigned long thresh = 32 * 1024 * 1024;
  626. if (wbc->for_kupdate)
  627. return 0;
  628. /* this is a bit racy, but that's ok */
  629. num_dirty = root->fs_info->dirty_metadata_bytes;
  630. if (num_dirty < thresh)
  631. return 0;
  632. }
  633. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  634. }
  635. static int btree_readpage(struct file *file, struct page *page)
  636. {
  637. struct extent_io_tree *tree;
  638. tree = &BTRFS_I(page->mapping->host)->io_tree;
  639. return extent_read_full_page(tree, page, btree_get_extent);
  640. }
  641. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  642. {
  643. struct extent_io_tree *tree;
  644. struct extent_map_tree *map;
  645. int ret;
  646. if (PageWriteback(page) || PageDirty(page))
  647. return 0;
  648. tree = &BTRFS_I(page->mapping->host)->io_tree;
  649. map = &BTRFS_I(page->mapping->host)->extent_tree;
  650. ret = try_release_extent_state(map, tree, page, gfp_flags);
  651. if (!ret)
  652. return 0;
  653. ret = try_release_extent_buffer(tree, page);
  654. if (ret == 1) {
  655. ClearPagePrivate(page);
  656. set_page_private(page, 0);
  657. page_cache_release(page);
  658. }
  659. return ret;
  660. }
  661. static void btree_invalidatepage(struct page *page, unsigned long offset)
  662. {
  663. struct extent_io_tree *tree;
  664. tree = &BTRFS_I(page->mapping->host)->io_tree;
  665. extent_invalidatepage(tree, page, offset);
  666. btree_releasepage(page, GFP_NOFS);
  667. if (PagePrivate(page)) {
  668. printk(KERN_WARNING "btrfs warning page private not zero "
  669. "on page %llu\n", (unsigned long long)page_offset(page));
  670. ClearPagePrivate(page);
  671. set_page_private(page, 0);
  672. page_cache_release(page);
  673. }
  674. }
  675. #if 0
  676. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  677. {
  678. struct buffer_head *bh;
  679. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  680. struct buffer_head *head;
  681. if (!page_has_buffers(page)) {
  682. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  683. (1 << BH_Dirty)|(1 << BH_Uptodate));
  684. }
  685. head = page_buffers(page);
  686. bh = head;
  687. do {
  688. if (buffer_dirty(bh))
  689. csum_tree_block(root, bh, 0);
  690. bh = bh->b_this_page;
  691. } while (bh != head);
  692. return block_write_full_page(page, btree_get_block, wbc);
  693. }
  694. #endif
  695. static struct address_space_operations btree_aops = {
  696. .readpage = btree_readpage,
  697. .writepage = btree_writepage,
  698. .writepages = btree_writepages,
  699. .releasepage = btree_releasepage,
  700. .invalidatepage = btree_invalidatepage,
  701. .sync_page = block_sync_page,
  702. };
  703. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  704. u64 parent_transid)
  705. {
  706. struct extent_buffer *buf = NULL;
  707. struct inode *btree_inode = root->fs_info->btree_inode;
  708. int ret = 0;
  709. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  710. if (!buf)
  711. return 0;
  712. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  713. buf, 0, 0, btree_get_extent, 0);
  714. free_extent_buffer(buf);
  715. return ret;
  716. }
  717. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  718. u64 bytenr, u32 blocksize)
  719. {
  720. struct inode *btree_inode = root->fs_info->btree_inode;
  721. struct extent_buffer *eb;
  722. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  723. bytenr, blocksize, GFP_NOFS);
  724. return eb;
  725. }
  726. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  727. u64 bytenr, u32 blocksize)
  728. {
  729. struct inode *btree_inode = root->fs_info->btree_inode;
  730. struct extent_buffer *eb;
  731. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  732. bytenr, blocksize, NULL, GFP_NOFS);
  733. return eb;
  734. }
  735. int btrfs_write_tree_block(struct extent_buffer *buf)
  736. {
  737. return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
  738. buf->start + buf->len - 1, WB_SYNC_ALL);
  739. }
  740. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  741. {
  742. return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
  743. buf->start, buf->start + buf->len - 1);
  744. }
  745. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  746. u32 blocksize, u64 parent_transid)
  747. {
  748. struct extent_buffer *buf = NULL;
  749. struct inode *btree_inode = root->fs_info->btree_inode;
  750. struct extent_io_tree *io_tree;
  751. int ret;
  752. io_tree = &BTRFS_I(btree_inode)->io_tree;
  753. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  754. if (!buf)
  755. return NULL;
  756. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  757. if (ret == 0)
  758. set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
  759. else
  760. WARN_ON(1);
  761. return buf;
  762. }
  763. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  764. struct extent_buffer *buf)
  765. {
  766. struct inode *btree_inode = root->fs_info->btree_inode;
  767. if (btrfs_header_generation(buf) ==
  768. root->fs_info->running_transaction->transid) {
  769. btrfs_assert_tree_locked(buf);
  770. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
  771. spin_lock(&root->fs_info->delalloc_lock);
  772. if (root->fs_info->dirty_metadata_bytes >= buf->len)
  773. root->fs_info->dirty_metadata_bytes -= buf->len;
  774. else
  775. WARN_ON(1);
  776. spin_unlock(&root->fs_info->delalloc_lock);
  777. }
  778. /* ugh, clear_extent_buffer_dirty needs to lock the page */
  779. btrfs_set_lock_blocking(buf);
  780. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  781. buf);
  782. }
  783. return 0;
  784. }
  785. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  786. u32 stripesize, struct btrfs_root *root,
  787. struct btrfs_fs_info *fs_info,
  788. u64 objectid)
  789. {
  790. root->node = NULL;
  791. root->commit_root = NULL;
  792. root->ref_tree = NULL;
  793. root->sectorsize = sectorsize;
  794. root->nodesize = nodesize;
  795. root->leafsize = leafsize;
  796. root->stripesize = stripesize;
  797. root->ref_cows = 0;
  798. root->track_dirty = 0;
  799. root->fs_info = fs_info;
  800. root->objectid = objectid;
  801. root->last_trans = 0;
  802. root->highest_inode = 0;
  803. root->last_inode_alloc = 0;
  804. root->name = NULL;
  805. root->in_sysfs = 0;
  806. INIT_LIST_HEAD(&root->dirty_list);
  807. INIT_LIST_HEAD(&root->orphan_list);
  808. INIT_LIST_HEAD(&root->dead_list);
  809. spin_lock_init(&root->node_lock);
  810. spin_lock_init(&root->list_lock);
  811. mutex_init(&root->objectid_mutex);
  812. mutex_init(&root->log_mutex);
  813. init_waitqueue_head(&root->log_writer_wait);
  814. init_waitqueue_head(&root->log_commit_wait[0]);
  815. init_waitqueue_head(&root->log_commit_wait[1]);
  816. atomic_set(&root->log_commit[0], 0);
  817. atomic_set(&root->log_commit[1], 0);
  818. atomic_set(&root->log_writers, 0);
  819. root->log_batch = 0;
  820. root->log_transid = 0;
  821. extent_io_tree_init(&root->dirty_log_pages,
  822. fs_info->btree_inode->i_mapping, GFP_NOFS);
  823. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  824. root->ref_tree = &root->ref_tree_struct;
  825. memset(&root->root_key, 0, sizeof(root->root_key));
  826. memset(&root->root_item, 0, sizeof(root->root_item));
  827. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  828. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  829. root->defrag_trans_start = fs_info->generation;
  830. init_completion(&root->kobj_unregister);
  831. root->defrag_running = 0;
  832. root->defrag_level = 0;
  833. root->root_key.objectid = objectid;
  834. root->anon_super.s_root = NULL;
  835. root->anon_super.s_dev = 0;
  836. INIT_LIST_HEAD(&root->anon_super.s_list);
  837. INIT_LIST_HEAD(&root->anon_super.s_instances);
  838. init_rwsem(&root->anon_super.s_umount);
  839. return 0;
  840. }
  841. static int find_and_setup_root(struct btrfs_root *tree_root,
  842. struct btrfs_fs_info *fs_info,
  843. u64 objectid,
  844. struct btrfs_root *root)
  845. {
  846. int ret;
  847. u32 blocksize;
  848. u64 generation;
  849. __setup_root(tree_root->nodesize, tree_root->leafsize,
  850. tree_root->sectorsize, tree_root->stripesize,
  851. root, fs_info, objectid);
  852. ret = btrfs_find_last_root(tree_root, objectid,
  853. &root->root_item, &root->root_key);
  854. BUG_ON(ret);
  855. generation = btrfs_root_generation(&root->root_item);
  856. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  857. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  858. blocksize, generation);
  859. BUG_ON(!root->node);
  860. return 0;
  861. }
  862. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  863. struct btrfs_fs_info *fs_info)
  864. {
  865. struct extent_buffer *eb;
  866. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  867. u64 start = 0;
  868. u64 end = 0;
  869. int ret;
  870. if (!log_root_tree)
  871. return 0;
  872. while (1) {
  873. ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
  874. 0, &start, &end, EXTENT_DIRTY);
  875. if (ret)
  876. break;
  877. clear_extent_dirty(&log_root_tree->dirty_log_pages,
  878. start, end, GFP_NOFS);
  879. }
  880. eb = fs_info->log_root_tree->node;
  881. WARN_ON(btrfs_header_level(eb) != 0);
  882. WARN_ON(btrfs_header_nritems(eb) != 0);
  883. ret = btrfs_free_reserved_extent(fs_info->tree_root,
  884. eb->start, eb->len);
  885. BUG_ON(ret);
  886. free_extent_buffer(eb);
  887. kfree(fs_info->log_root_tree);
  888. fs_info->log_root_tree = NULL;
  889. return 0;
  890. }
  891. static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
  892. struct btrfs_fs_info *fs_info)
  893. {
  894. struct btrfs_root *root;
  895. struct btrfs_root *tree_root = fs_info->tree_root;
  896. struct extent_buffer *leaf;
  897. root = kzalloc(sizeof(*root), GFP_NOFS);
  898. if (!root)
  899. return ERR_PTR(-ENOMEM);
  900. __setup_root(tree_root->nodesize, tree_root->leafsize,
  901. tree_root->sectorsize, tree_root->stripesize,
  902. root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  903. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  904. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  905. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  906. /*
  907. * log trees do not get reference counted because they go away
  908. * before a real commit is actually done. They do store pointers
  909. * to file data extents, and those reference counts still get
  910. * updated (along with back refs to the log tree).
  911. */
  912. root->ref_cows = 0;
  913. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  914. 0, BTRFS_TREE_LOG_OBJECTID,
  915. trans->transid, 0, 0, 0);
  916. if (IS_ERR(leaf)) {
  917. kfree(root);
  918. return ERR_CAST(leaf);
  919. }
  920. root->node = leaf;
  921. btrfs_set_header_nritems(root->node, 0);
  922. btrfs_set_header_level(root->node, 0);
  923. btrfs_set_header_bytenr(root->node, root->node->start);
  924. btrfs_set_header_generation(root->node, trans->transid);
  925. btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID);
  926. write_extent_buffer(root->node, root->fs_info->fsid,
  927. (unsigned long)btrfs_header_fsid(root->node),
  928. BTRFS_FSID_SIZE);
  929. btrfs_mark_buffer_dirty(root->node);
  930. btrfs_tree_unlock(root->node);
  931. return root;
  932. }
  933. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  934. struct btrfs_fs_info *fs_info)
  935. {
  936. struct btrfs_root *log_root;
  937. log_root = alloc_log_tree(trans, fs_info);
  938. if (IS_ERR(log_root))
  939. return PTR_ERR(log_root);
  940. WARN_ON(fs_info->log_root_tree);
  941. fs_info->log_root_tree = log_root;
  942. return 0;
  943. }
  944. int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
  945. struct btrfs_root *root)
  946. {
  947. struct btrfs_root *log_root;
  948. struct btrfs_inode_item *inode_item;
  949. log_root = alloc_log_tree(trans, root->fs_info);
  950. if (IS_ERR(log_root))
  951. return PTR_ERR(log_root);
  952. log_root->last_trans = trans->transid;
  953. log_root->root_key.offset = root->root_key.objectid;
  954. inode_item = &log_root->root_item.inode;
  955. inode_item->generation = cpu_to_le64(1);
  956. inode_item->size = cpu_to_le64(3);
  957. inode_item->nlink = cpu_to_le32(1);
  958. inode_item->nbytes = cpu_to_le64(root->leafsize);
  959. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  960. btrfs_set_root_bytenr(&log_root->root_item, log_root->node->start);
  961. btrfs_set_root_generation(&log_root->root_item, trans->transid);
  962. WARN_ON(root->log_root);
  963. root->log_root = log_root;
  964. root->log_transid = 0;
  965. return 0;
  966. }
  967. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
  968. struct btrfs_key *location)
  969. {
  970. struct btrfs_root *root;
  971. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  972. struct btrfs_path *path;
  973. struct extent_buffer *l;
  974. u64 highest_inode;
  975. u64 generation;
  976. u32 blocksize;
  977. int ret = 0;
  978. root = kzalloc(sizeof(*root), GFP_NOFS);
  979. if (!root)
  980. return ERR_PTR(-ENOMEM);
  981. if (location->offset == (u64)-1) {
  982. ret = find_and_setup_root(tree_root, fs_info,
  983. location->objectid, root);
  984. if (ret) {
  985. kfree(root);
  986. return ERR_PTR(ret);
  987. }
  988. goto insert;
  989. }
  990. __setup_root(tree_root->nodesize, tree_root->leafsize,
  991. tree_root->sectorsize, tree_root->stripesize,
  992. root, fs_info, location->objectid);
  993. path = btrfs_alloc_path();
  994. BUG_ON(!path);
  995. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  996. if (ret != 0) {
  997. if (ret > 0)
  998. ret = -ENOENT;
  999. goto out;
  1000. }
  1001. l = path->nodes[0];
  1002. read_extent_buffer(l, &root->root_item,
  1003. btrfs_item_ptr_offset(l, path->slots[0]),
  1004. sizeof(root->root_item));
  1005. memcpy(&root->root_key, location, sizeof(*location));
  1006. ret = 0;
  1007. out:
  1008. btrfs_release_path(root, path);
  1009. btrfs_free_path(path);
  1010. if (ret) {
  1011. kfree(root);
  1012. return ERR_PTR(ret);
  1013. }
  1014. generation = btrfs_root_generation(&root->root_item);
  1015. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  1016. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  1017. blocksize, generation);
  1018. BUG_ON(!root->node);
  1019. insert:
  1020. if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
  1021. root->ref_cows = 1;
  1022. ret = btrfs_find_highest_inode(root, &highest_inode);
  1023. if (ret == 0) {
  1024. root->highest_inode = highest_inode;
  1025. root->last_inode_alloc = highest_inode;
  1026. }
  1027. }
  1028. return root;
  1029. }
  1030. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  1031. u64 root_objectid)
  1032. {
  1033. struct btrfs_root *root;
  1034. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  1035. return fs_info->tree_root;
  1036. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1037. return fs_info->extent_root;
  1038. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1039. (unsigned long)root_objectid);
  1040. return root;
  1041. }
  1042. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  1043. struct btrfs_key *location)
  1044. {
  1045. struct btrfs_root *root;
  1046. int ret;
  1047. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1048. return fs_info->tree_root;
  1049. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1050. return fs_info->extent_root;
  1051. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  1052. return fs_info->chunk_root;
  1053. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  1054. return fs_info->dev_root;
  1055. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  1056. return fs_info->csum_root;
  1057. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1058. (unsigned long)location->objectid);
  1059. if (root)
  1060. return root;
  1061. root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
  1062. if (IS_ERR(root))
  1063. return root;
  1064. set_anon_super(&root->anon_super, NULL);
  1065. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  1066. (unsigned long)root->root_key.objectid,
  1067. root);
  1068. if (ret) {
  1069. free_extent_buffer(root->node);
  1070. kfree(root);
  1071. return ERR_PTR(ret);
  1072. }
  1073. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  1074. ret = btrfs_find_dead_roots(fs_info->tree_root,
  1075. root->root_key.objectid, root);
  1076. BUG_ON(ret);
  1077. btrfs_orphan_cleanup(root);
  1078. }
  1079. return root;
  1080. }
  1081. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  1082. struct btrfs_key *location,
  1083. const char *name, int namelen)
  1084. {
  1085. struct btrfs_root *root;
  1086. int ret;
  1087. root = btrfs_read_fs_root_no_name(fs_info, location);
  1088. if (!root)
  1089. return NULL;
  1090. if (root->in_sysfs)
  1091. return root;
  1092. ret = btrfs_set_root_name(root, name, namelen);
  1093. if (ret) {
  1094. free_extent_buffer(root->node);
  1095. kfree(root);
  1096. return ERR_PTR(ret);
  1097. }
  1098. #if 0
  1099. ret = btrfs_sysfs_add_root(root);
  1100. if (ret) {
  1101. free_extent_buffer(root->node);
  1102. kfree(root->name);
  1103. kfree(root);
  1104. return ERR_PTR(ret);
  1105. }
  1106. #endif
  1107. root->in_sysfs = 1;
  1108. return root;
  1109. }
  1110. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1111. {
  1112. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1113. int ret = 0;
  1114. struct btrfs_device *device;
  1115. struct backing_dev_info *bdi;
  1116. #if 0
  1117. if ((bdi_bits & (1 << BDI_write_congested)) &&
  1118. btrfs_congested_async(info, 0))
  1119. return 1;
  1120. #endif
  1121. list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
  1122. if (!device->bdev)
  1123. continue;
  1124. bdi = blk_get_backing_dev_info(device->bdev);
  1125. if (bdi && bdi_congested(bdi, bdi_bits)) {
  1126. ret = 1;
  1127. break;
  1128. }
  1129. }
  1130. return ret;
  1131. }
  1132. /*
  1133. * this unplugs every device on the box, and it is only used when page
  1134. * is null
  1135. */
  1136. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1137. {
  1138. struct btrfs_device *device;
  1139. struct btrfs_fs_info *info;
  1140. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  1141. list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
  1142. if (!device->bdev)
  1143. continue;
  1144. bdi = blk_get_backing_dev_info(device->bdev);
  1145. if (bdi->unplug_io_fn)
  1146. bdi->unplug_io_fn(bdi, page);
  1147. }
  1148. }
  1149. static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1150. {
  1151. struct inode *inode;
  1152. struct extent_map_tree *em_tree;
  1153. struct extent_map *em;
  1154. struct address_space *mapping;
  1155. u64 offset;
  1156. /* the generic O_DIRECT read code does this */
  1157. if (1 || !page) {
  1158. __unplug_io_fn(bdi, page);
  1159. return;
  1160. }
  1161. /*
  1162. * page->mapping may change at any time. Get a consistent copy
  1163. * and use that for everything below
  1164. */
  1165. smp_mb();
  1166. mapping = page->mapping;
  1167. if (!mapping)
  1168. return;
  1169. inode = mapping->host;
  1170. /*
  1171. * don't do the expensive searching for a small number of
  1172. * devices
  1173. */
  1174. if (BTRFS_I(inode)->root->fs_info->fs_devices->open_devices <= 2) {
  1175. __unplug_io_fn(bdi, page);
  1176. return;
  1177. }
  1178. offset = page_offset(page);
  1179. em_tree = &BTRFS_I(inode)->extent_tree;
  1180. spin_lock(&em_tree->lock);
  1181. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  1182. spin_unlock(&em_tree->lock);
  1183. if (!em) {
  1184. __unplug_io_fn(bdi, page);
  1185. return;
  1186. }
  1187. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1188. free_extent_map(em);
  1189. __unplug_io_fn(bdi, page);
  1190. return;
  1191. }
  1192. offset = offset - em->start;
  1193. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  1194. em->block_start + offset, page);
  1195. free_extent_map(em);
  1196. }
  1197. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  1198. {
  1199. bdi_init(bdi);
  1200. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1201. bdi->state = 0;
  1202. bdi->capabilities = default_backing_dev_info.capabilities;
  1203. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  1204. bdi->unplug_io_data = info;
  1205. bdi->congested_fn = btrfs_congested_fn;
  1206. bdi->congested_data = info;
  1207. return 0;
  1208. }
  1209. static int bio_ready_for_csum(struct bio *bio)
  1210. {
  1211. u64 length = 0;
  1212. u64 buf_len = 0;
  1213. u64 start = 0;
  1214. struct page *page;
  1215. struct extent_io_tree *io_tree = NULL;
  1216. struct btrfs_fs_info *info = NULL;
  1217. struct bio_vec *bvec;
  1218. int i;
  1219. int ret;
  1220. bio_for_each_segment(bvec, bio, i) {
  1221. page = bvec->bv_page;
  1222. if (page->private == EXTENT_PAGE_PRIVATE) {
  1223. length += bvec->bv_len;
  1224. continue;
  1225. }
  1226. if (!page->private) {
  1227. length += bvec->bv_len;
  1228. continue;
  1229. }
  1230. length = bvec->bv_len;
  1231. buf_len = page->private >> 2;
  1232. start = page_offset(page) + bvec->bv_offset;
  1233. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  1234. info = BTRFS_I(page->mapping->host)->root->fs_info;
  1235. }
  1236. /* are we fully contained in this bio? */
  1237. if (buf_len <= length)
  1238. return 1;
  1239. ret = extent_range_uptodate(io_tree, start + length,
  1240. start + buf_len - 1);
  1241. if (ret == 1)
  1242. return ret;
  1243. return ret;
  1244. }
  1245. /*
  1246. * called by the kthread helper functions to finally call the bio end_io
  1247. * functions. This is where read checksum verification actually happens
  1248. */
  1249. static void end_workqueue_fn(struct btrfs_work *work)
  1250. {
  1251. struct bio *bio;
  1252. struct end_io_wq *end_io_wq;
  1253. struct btrfs_fs_info *fs_info;
  1254. int error;
  1255. end_io_wq = container_of(work, struct end_io_wq, work);
  1256. bio = end_io_wq->bio;
  1257. fs_info = end_io_wq->info;
  1258. /* metadata bio reads are special because the whole tree block must
  1259. * be checksummed at once. This makes sure the entire block is in
  1260. * ram and up to date before trying to verify things. For
  1261. * blocksize <= pagesize, it is basically a noop
  1262. */
  1263. if (!(bio->bi_rw & (1 << BIO_RW)) && end_io_wq->metadata &&
  1264. !bio_ready_for_csum(bio)) {
  1265. btrfs_queue_worker(&fs_info->endio_meta_workers,
  1266. &end_io_wq->work);
  1267. return;
  1268. }
  1269. error = end_io_wq->error;
  1270. bio->bi_private = end_io_wq->private;
  1271. bio->bi_end_io = end_io_wq->end_io;
  1272. kfree(end_io_wq);
  1273. bio_endio(bio, error);
  1274. }
  1275. static int cleaner_kthread(void *arg)
  1276. {
  1277. struct btrfs_root *root = arg;
  1278. do {
  1279. smp_mb();
  1280. if (root->fs_info->closing)
  1281. break;
  1282. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1283. mutex_lock(&root->fs_info->cleaner_mutex);
  1284. btrfs_clean_old_snapshots(root);
  1285. mutex_unlock(&root->fs_info->cleaner_mutex);
  1286. if (freezing(current)) {
  1287. refrigerator();
  1288. } else {
  1289. smp_mb();
  1290. if (root->fs_info->closing)
  1291. break;
  1292. set_current_state(TASK_INTERRUPTIBLE);
  1293. schedule();
  1294. __set_current_state(TASK_RUNNING);
  1295. }
  1296. } while (!kthread_should_stop());
  1297. return 0;
  1298. }
  1299. static int transaction_kthread(void *arg)
  1300. {
  1301. struct btrfs_root *root = arg;
  1302. struct btrfs_trans_handle *trans;
  1303. struct btrfs_transaction *cur;
  1304. unsigned long now;
  1305. unsigned long delay;
  1306. int ret;
  1307. do {
  1308. smp_mb();
  1309. if (root->fs_info->closing)
  1310. break;
  1311. delay = HZ * 30;
  1312. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1313. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1314. mutex_lock(&root->fs_info->trans_mutex);
  1315. cur = root->fs_info->running_transaction;
  1316. if (!cur) {
  1317. mutex_unlock(&root->fs_info->trans_mutex);
  1318. goto sleep;
  1319. }
  1320. now = get_seconds();
  1321. if (now < cur->start_time || now - cur->start_time < 30) {
  1322. mutex_unlock(&root->fs_info->trans_mutex);
  1323. delay = HZ * 5;
  1324. goto sleep;
  1325. }
  1326. mutex_unlock(&root->fs_info->trans_mutex);
  1327. trans = btrfs_start_transaction(root, 1);
  1328. ret = btrfs_commit_transaction(trans, root);
  1329. sleep:
  1330. wake_up_process(root->fs_info->cleaner_kthread);
  1331. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1332. if (freezing(current)) {
  1333. refrigerator();
  1334. } else {
  1335. if (root->fs_info->closing)
  1336. break;
  1337. set_current_state(TASK_INTERRUPTIBLE);
  1338. schedule_timeout(delay);
  1339. __set_current_state(TASK_RUNNING);
  1340. }
  1341. } while (!kthread_should_stop());
  1342. return 0;
  1343. }
  1344. struct btrfs_root *open_ctree(struct super_block *sb,
  1345. struct btrfs_fs_devices *fs_devices,
  1346. char *options)
  1347. {
  1348. u32 sectorsize;
  1349. u32 nodesize;
  1350. u32 leafsize;
  1351. u32 blocksize;
  1352. u32 stripesize;
  1353. u64 generation;
  1354. u64 features;
  1355. struct btrfs_key location;
  1356. struct buffer_head *bh;
  1357. struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
  1358. GFP_NOFS);
  1359. struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
  1360. GFP_NOFS);
  1361. struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root),
  1362. GFP_NOFS);
  1363. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1364. GFP_NOFS);
  1365. struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
  1366. GFP_NOFS);
  1367. struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
  1368. GFP_NOFS);
  1369. struct btrfs_root *log_tree_root;
  1370. int ret;
  1371. int err = -EINVAL;
  1372. struct btrfs_super_block *disk_super;
  1373. if (!extent_root || !tree_root || !fs_info ||
  1374. !chunk_root || !dev_root || !csum_root) {
  1375. err = -ENOMEM;
  1376. goto fail;
  1377. }
  1378. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1379. INIT_LIST_HEAD(&fs_info->trans_list);
  1380. INIT_LIST_HEAD(&fs_info->dead_roots);
  1381. INIT_LIST_HEAD(&fs_info->hashers);
  1382. INIT_LIST_HEAD(&fs_info->delalloc_inodes);
  1383. INIT_LIST_HEAD(&fs_info->ordered_operations);
  1384. spin_lock_init(&fs_info->delalloc_lock);
  1385. spin_lock_init(&fs_info->new_trans_lock);
  1386. spin_lock_init(&fs_info->ref_cache_lock);
  1387. init_completion(&fs_info->kobj_unregister);
  1388. fs_info->tree_root = tree_root;
  1389. fs_info->extent_root = extent_root;
  1390. fs_info->csum_root = csum_root;
  1391. fs_info->chunk_root = chunk_root;
  1392. fs_info->dev_root = dev_root;
  1393. fs_info->fs_devices = fs_devices;
  1394. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1395. INIT_LIST_HEAD(&fs_info->space_info);
  1396. btrfs_mapping_init(&fs_info->mapping_tree);
  1397. atomic_set(&fs_info->nr_async_submits, 0);
  1398. atomic_set(&fs_info->async_delalloc_pages, 0);
  1399. atomic_set(&fs_info->async_submit_draining, 0);
  1400. atomic_set(&fs_info->nr_async_bios, 0);
  1401. atomic_set(&fs_info->throttles, 0);
  1402. atomic_set(&fs_info->throttle_gen, 0);
  1403. fs_info->sb = sb;
  1404. fs_info->max_extent = (u64)-1;
  1405. fs_info->max_inline = 8192 * 1024;
  1406. setup_bdi(fs_info, &fs_info->bdi);
  1407. fs_info->btree_inode = new_inode(sb);
  1408. fs_info->btree_inode->i_ino = 1;
  1409. fs_info->btree_inode->i_nlink = 1;
  1410. fs_info->thread_pool_size = min_t(unsigned long,
  1411. num_online_cpus() + 2, 8);
  1412. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1413. spin_lock_init(&fs_info->ordered_extent_lock);
  1414. sb->s_blocksize = 4096;
  1415. sb->s_blocksize_bits = blksize_bits(4096);
  1416. /*
  1417. * we set the i_size on the btree inode to the max possible int.
  1418. * the real end of the address space is determined by all of
  1419. * the devices in the system
  1420. */
  1421. fs_info->btree_inode->i_size = OFFSET_MAX;
  1422. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1423. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1424. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1425. fs_info->btree_inode->i_mapping,
  1426. GFP_NOFS);
  1427. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1428. GFP_NOFS);
  1429. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1430. spin_lock_init(&fs_info->block_group_cache_lock);
  1431. fs_info->block_group_cache_tree.rb_node = NULL;
  1432. extent_io_tree_init(&fs_info->pinned_extents,
  1433. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1434. fs_info->do_barriers = 1;
  1435. INIT_LIST_HEAD(&fs_info->dead_reloc_roots);
  1436. btrfs_leaf_ref_tree_init(&fs_info->reloc_ref_tree);
  1437. btrfs_leaf_ref_tree_init(&fs_info->shared_ref_tree);
  1438. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1439. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1440. sizeof(struct btrfs_key));
  1441. insert_inode_hash(fs_info->btree_inode);
  1442. mutex_init(&fs_info->trans_mutex);
  1443. mutex_init(&fs_info->ordered_operations_mutex);
  1444. mutex_init(&fs_info->tree_log_mutex);
  1445. mutex_init(&fs_info->drop_mutex);
  1446. mutex_init(&fs_info->pinned_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. init_waitqueue_head(&fs_info->transaction_throttle);
  1453. init_waitqueue_head(&fs_info->transaction_wait);
  1454. init_waitqueue_head(&fs_info->async_submit_wait);
  1455. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1456. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1457. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  1458. if (!bh)
  1459. goto fail_iput;
  1460. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1461. memcpy(&fs_info->super_for_commit, &fs_info->super_copy,
  1462. sizeof(fs_info->super_for_commit));
  1463. brelse(bh);
  1464. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1465. disk_super = &fs_info->super_copy;
  1466. if (!btrfs_super_root(disk_super))
  1467. goto fail_iput;
  1468. ret = btrfs_parse_options(tree_root, options);
  1469. if (ret) {
  1470. err = ret;
  1471. goto fail_iput;
  1472. }
  1473. features = btrfs_super_incompat_flags(disk_super) &
  1474. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  1475. if (features) {
  1476. printk(KERN_ERR "BTRFS: couldn't mount because of "
  1477. "unsupported optional features (%Lx).\n",
  1478. features);
  1479. err = -EINVAL;
  1480. goto fail_iput;
  1481. }
  1482. features = btrfs_super_compat_ro_flags(disk_super) &
  1483. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  1484. if (!(sb->s_flags & MS_RDONLY) && features) {
  1485. printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
  1486. "unsupported option features (%Lx).\n",
  1487. features);
  1488. err = -EINVAL;
  1489. goto fail_iput;
  1490. }
  1491. /*
  1492. * we need to start all the end_io workers up front because the
  1493. * queue work function gets called at interrupt time, and so it
  1494. * cannot dynamically grow.
  1495. */
  1496. btrfs_init_workers(&fs_info->workers, "worker",
  1497. fs_info->thread_pool_size);
  1498. btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
  1499. fs_info->thread_pool_size);
  1500. btrfs_init_workers(&fs_info->submit_workers, "submit",
  1501. min_t(u64, fs_devices->num_devices,
  1502. fs_info->thread_pool_size));
  1503. /* a higher idle thresh on the submit workers makes it much more
  1504. * likely that bios will be send down in a sane order to the
  1505. * devices
  1506. */
  1507. fs_info->submit_workers.idle_thresh = 64;
  1508. fs_info->workers.idle_thresh = 16;
  1509. fs_info->workers.ordered = 1;
  1510. fs_info->delalloc_workers.idle_thresh = 2;
  1511. fs_info->delalloc_workers.ordered = 1;
  1512. btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
  1513. btrfs_init_workers(&fs_info->endio_workers, "endio",
  1514. fs_info->thread_pool_size);
  1515. btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
  1516. fs_info->thread_pool_size);
  1517. btrfs_init_workers(&fs_info->endio_meta_write_workers,
  1518. "endio-meta-write", fs_info->thread_pool_size);
  1519. btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
  1520. fs_info->thread_pool_size);
  1521. /*
  1522. * endios are largely parallel and should have a very
  1523. * low idle thresh
  1524. */
  1525. fs_info->endio_workers.idle_thresh = 4;
  1526. fs_info->endio_meta_workers.idle_thresh = 4;
  1527. fs_info->endio_write_workers.idle_thresh = 64;
  1528. fs_info->endio_meta_write_workers.idle_thresh = 64;
  1529. btrfs_start_workers(&fs_info->workers, 1);
  1530. btrfs_start_workers(&fs_info->submit_workers, 1);
  1531. btrfs_start_workers(&fs_info->delalloc_workers, 1);
  1532. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1533. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1534. btrfs_start_workers(&fs_info->endio_meta_workers,
  1535. fs_info->thread_pool_size);
  1536. btrfs_start_workers(&fs_info->endio_meta_write_workers,
  1537. fs_info->thread_pool_size);
  1538. btrfs_start_workers(&fs_info->endio_write_workers,
  1539. fs_info->thread_pool_size);
  1540. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1541. fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
  1542. 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
  1543. nodesize = btrfs_super_nodesize(disk_super);
  1544. leafsize = btrfs_super_leafsize(disk_super);
  1545. sectorsize = btrfs_super_sectorsize(disk_super);
  1546. stripesize = btrfs_super_stripesize(disk_super);
  1547. tree_root->nodesize = nodesize;
  1548. tree_root->leafsize = leafsize;
  1549. tree_root->sectorsize = sectorsize;
  1550. tree_root->stripesize = stripesize;
  1551. sb->s_blocksize = sectorsize;
  1552. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1553. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1554. sizeof(disk_super->magic))) {
  1555. printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id);
  1556. goto fail_sb_buffer;
  1557. }
  1558. mutex_lock(&fs_info->chunk_mutex);
  1559. ret = btrfs_read_sys_array(tree_root);
  1560. mutex_unlock(&fs_info->chunk_mutex);
  1561. if (ret) {
  1562. printk(KERN_WARNING "btrfs: failed to read the system "
  1563. "array on %s\n", sb->s_id);
  1564. goto fail_sys_array;
  1565. }
  1566. blocksize = btrfs_level_size(tree_root,
  1567. btrfs_super_chunk_root_level(disk_super));
  1568. generation = btrfs_super_chunk_root_generation(disk_super);
  1569. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1570. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1571. chunk_root->node = read_tree_block(chunk_root,
  1572. btrfs_super_chunk_root(disk_super),
  1573. blocksize, generation);
  1574. BUG_ON(!chunk_root->node);
  1575. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1576. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1577. BTRFS_UUID_SIZE);
  1578. mutex_lock(&fs_info->chunk_mutex);
  1579. ret = btrfs_read_chunk_tree(chunk_root);
  1580. mutex_unlock(&fs_info->chunk_mutex);
  1581. if (ret) {
  1582. printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n",
  1583. sb->s_id);
  1584. goto fail_chunk_root;
  1585. }
  1586. btrfs_close_extra_devices(fs_devices);
  1587. blocksize = btrfs_level_size(tree_root,
  1588. btrfs_super_root_level(disk_super));
  1589. generation = btrfs_super_generation(disk_super);
  1590. tree_root->node = read_tree_block(tree_root,
  1591. btrfs_super_root(disk_super),
  1592. blocksize, generation);
  1593. if (!tree_root->node)
  1594. goto fail_chunk_root;
  1595. ret = find_and_setup_root(tree_root, fs_info,
  1596. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1597. if (ret)
  1598. goto fail_tree_root;
  1599. extent_root->track_dirty = 1;
  1600. ret = find_and_setup_root(tree_root, fs_info,
  1601. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1602. dev_root->track_dirty = 1;
  1603. if (ret)
  1604. goto fail_extent_root;
  1605. ret = find_and_setup_root(tree_root, fs_info,
  1606. BTRFS_CSUM_TREE_OBJECTID, csum_root);
  1607. if (ret)
  1608. goto fail_extent_root;
  1609. csum_root->track_dirty = 1;
  1610. btrfs_read_block_groups(extent_root);
  1611. fs_info->generation = generation;
  1612. fs_info->last_trans_committed = generation;
  1613. fs_info->data_alloc_profile = (u64)-1;
  1614. fs_info->metadata_alloc_profile = (u64)-1;
  1615. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1616. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1617. "btrfs-cleaner");
  1618. if (IS_ERR(fs_info->cleaner_kthread))
  1619. goto fail_csum_root;
  1620. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1621. tree_root,
  1622. "btrfs-transaction");
  1623. if (IS_ERR(fs_info->transaction_kthread))
  1624. goto fail_cleaner;
  1625. if (btrfs_super_log_root(disk_super) != 0) {
  1626. u64 bytenr = btrfs_super_log_root(disk_super);
  1627. if (fs_devices->rw_devices == 0) {
  1628. printk(KERN_WARNING "Btrfs log replay required "
  1629. "on RO media\n");
  1630. err = -EIO;
  1631. goto fail_trans_kthread;
  1632. }
  1633. blocksize =
  1634. btrfs_level_size(tree_root,
  1635. btrfs_super_log_root_level(disk_super));
  1636. log_tree_root = kzalloc(sizeof(struct btrfs_root),
  1637. GFP_NOFS);
  1638. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1639. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1640. log_tree_root->node = read_tree_block(tree_root, bytenr,
  1641. blocksize,
  1642. generation + 1);
  1643. ret = btrfs_recover_log_trees(log_tree_root);
  1644. BUG_ON(ret);
  1645. if (sb->s_flags & MS_RDONLY) {
  1646. ret = btrfs_commit_super(tree_root);
  1647. BUG_ON(ret);
  1648. }
  1649. }
  1650. if (!(sb->s_flags & MS_RDONLY)) {
  1651. ret = btrfs_cleanup_reloc_trees(tree_root);
  1652. BUG_ON(ret);
  1653. }
  1654. location.objectid = BTRFS_FS_TREE_OBJECTID;
  1655. location.type = BTRFS_ROOT_ITEM_KEY;
  1656. location.offset = (u64)-1;
  1657. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  1658. if (!fs_info->fs_root)
  1659. goto fail_trans_kthread;
  1660. return tree_root;
  1661. fail_trans_kthread:
  1662. kthread_stop(fs_info->transaction_kthread);
  1663. fail_cleaner:
  1664. kthread_stop(fs_info->cleaner_kthread);
  1665. /*
  1666. * make sure we're done with the btree inode before we stop our
  1667. * kthreads
  1668. */
  1669. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1670. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1671. fail_csum_root:
  1672. free_extent_buffer(csum_root->node);
  1673. fail_extent_root:
  1674. free_extent_buffer(extent_root->node);
  1675. fail_tree_root:
  1676. free_extent_buffer(tree_root->node);
  1677. fail_chunk_root:
  1678. free_extent_buffer(chunk_root->node);
  1679. fail_sys_array:
  1680. free_extent_buffer(dev_root->node);
  1681. fail_sb_buffer:
  1682. btrfs_stop_workers(&fs_info->fixup_workers);
  1683. btrfs_stop_workers(&fs_info->delalloc_workers);
  1684. btrfs_stop_workers(&fs_info->workers);
  1685. btrfs_stop_workers(&fs_info->endio_workers);
  1686. btrfs_stop_workers(&fs_info->endio_meta_workers);
  1687. btrfs_stop_workers(&fs_info->endio_meta_write_workers);
  1688. btrfs_stop_workers(&fs_info->endio_write_workers);
  1689. btrfs_stop_workers(&fs_info->submit_workers);
  1690. fail_iput:
  1691. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1692. iput(fs_info->btree_inode);
  1693. btrfs_close_devices(fs_info->fs_devices);
  1694. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1695. bdi_destroy(&fs_info->bdi);
  1696. fail:
  1697. kfree(extent_root);
  1698. kfree(tree_root);
  1699. kfree(fs_info);
  1700. kfree(chunk_root);
  1701. kfree(dev_root);
  1702. kfree(csum_root);
  1703. return ERR_PTR(err);
  1704. }
  1705. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1706. {
  1707. char b[BDEVNAME_SIZE];
  1708. if (uptodate) {
  1709. set_buffer_uptodate(bh);
  1710. } else {
  1711. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1712. printk(KERN_WARNING "lost page write due to "
  1713. "I/O error on %s\n",
  1714. bdevname(bh->b_bdev, b));
  1715. }
  1716. /* note, we dont' set_buffer_write_io_error because we have
  1717. * our own ways of dealing with the IO errors
  1718. */
  1719. clear_buffer_uptodate(bh);
  1720. }
  1721. unlock_buffer(bh);
  1722. put_bh(bh);
  1723. }
  1724. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  1725. {
  1726. struct buffer_head *bh;
  1727. struct buffer_head *latest = NULL;
  1728. struct btrfs_super_block *super;
  1729. int i;
  1730. u64 transid = 0;
  1731. u64 bytenr;
  1732. /* we would like to check all the supers, but that would make
  1733. * a btrfs mount succeed after a mkfs from a different FS.
  1734. * So, we need to add a special mount option to scan for
  1735. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  1736. */
  1737. for (i = 0; i < 1; i++) {
  1738. bytenr = btrfs_sb_offset(i);
  1739. if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
  1740. break;
  1741. bh = __bread(bdev, bytenr / 4096, 4096);
  1742. if (!bh)
  1743. continue;
  1744. super = (struct btrfs_super_block *)bh->b_data;
  1745. if (btrfs_super_bytenr(super) != bytenr ||
  1746. strncmp((char *)(&super->magic), BTRFS_MAGIC,
  1747. sizeof(super->magic))) {
  1748. brelse(bh);
  1749. continue;
  1750. }
  1751. if (!latest || btrfs_super_generation(super) > transid) {
  1752. brelse(latest);
  1753. latest = bh;
  1754. transid = btrfs_super_generation(super);
  1755. } else {
  1756. brelse(bh);
  1757. }
  1758. }
  1759. return latest;
  1760. }
  1761. static int write_dev_supers(struct btrfs_device *device,
  1762. struct btrfs_super_block *sb,
  1763. int do_barriers, int wait, int max_mirrors)
  1764. {
  1765. struct buffer_head *bh;
  1766. int i;
  1767. int ret;
  1768. int errors = 0;
  1769. u32 crc;
  1770. u64 bytenr;
  1771. int last_barrier = 0;
  1772. if (max_mirrors == 0)
  1773. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  1774. /* make sure only the last submit_bh does a barrier */
  1775. if (do_barriers) {
  1776. for (i = 0; i < max_mirrors; i++) {
  1777. bytenr = btrfs_sb_offset(i);
  1778. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  1779. device->total_bytes)
  1780. break;
  1781. last_barrier = i;
  1782. }
  1783. }
  1784. for (i = 0; i < max_mirrors; i++) {
  1785. bytenr = btrfs_sb_offset(i);
  1786. if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
  1787. break;
  1788. if (wait) {
  1789. bh = __find_get_block(device->bdev, bytenr / 4096,
  1790. BTRFS_SUPER_INFO_SIZE);
  1791. BUG_ON(!bh);
  1792. brelse(bh);
  1793. wait_on_buffer(bh);
  1794. if (buffer_uptodate(bh)) {
  1795. brelse(bh);
  1796. continue;
  1797. }
  1798. } else {
  1799. btrfs_set_super_bytenr(sb, bytenr);
  1800. crc = ~(u32)0;
  1801. crc = btrfs_csum_data(NULL, (char *)sb +
  1802. BTRFS_CSUM_SIZE, crc,
  1803. BTRFS_SUPER_INFO_SIZE -
  1804. BTRFS_CSUM_SIZE);
  1805. btrfs_csum_final(crc, sb->csum);
  1806. bh = __getblk(device->bdev, bytenr / 4096,
  1807. BTRFS_SUPER_INFO_SIZE);
  1808. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1809. set_buffer_uptodate(bh);
  1810. get_bh(bh);
  1811. lock_buffer(bh);
  1812. bh->b_end_io = btrfs_end_buffer_write_sync;
  1813. }
  1814. if (i == last_barrier && do_barriers && device->barriers) {
  1815. ret = submit_bh(WRITE_BARRIER, bh);
  1816. if (ret == -EOPNOTSUPP) {
  1817. printk("btrfs: disabling barriers on dev %s\n",
  1818. device->name);
  1819. set_buffer_uptodate(bh);
  1820. device->barriers = 0;
  1821. get_bh(bh);
  1822. lock_buffer(bh);
  1823. ret = submit_bh(WRITE, bh);
  1824. }
  1825. } else {
  1826. ret = submit_bh(WRITE, bh);
  1827. }
  1828. if (!ret && wait) {
  1829. wait_on_buffer(bh);
  1830. if (!buffer_uptodate(bh))
  1831. errors++;
  1832. } else if (ret) {
  1833. errors++;
  1834. }
  1835. if (wait)
  1836. brelse(bh);
  1837. }
  1838. return errors < i ? 0 : -1;
  1839. }
  1840. int write_all_supers(struct btrfs_root *root, int max_mirrors)
  1841. {
  1842. struct list_head *head = &root->fs_info->fs_devices->devices;
  1843. struct btrfs_device *dev;
  1844. struct btrfs_super_block *sb;
  1845. struct btrfs_dev_item *dev_item;
  1846. int ret;
  1847. int do_barriers;
  1848. int max_errors;
  1849. int total_errors = 0;
  1850. u64 flags;
  1851. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1852. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1853. sb = &root->fs_info->super_for_commit;
  1854. dev_item = &sb->dev_item;
  1855. list_for_each_entry(dev, head, dev_list) {
  1856. if (!dev->bdev) {
  1857. total_errors++;
  1858. continue;
  1859. }
  1860. if (!dev->in_fs_metadata || !dev->writeable)
  1861. continue;
  1862. btrfs_set_stack_device_generation(dev_item, 0);
  1863. btrfs_set_stack_device_type(dev_item, dev->type);
  1864. btrfs_set_stack_device_id(dev_item, dev->devid);
  1865. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1866. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1867. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1868. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1869. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1870. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1871. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
  1872. flags = btrfs_super_flags(sb);
  1873. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1874. ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
  1875. if (ret)
  1876. total_errors++;
  1877. }
  1878. if (total_errors > max_errors) {
  1879. printk(KERN_ERR "btrfs: %d errors while writing supers\n",
  1880. total_errors);
  1881. BUG();
  1882. }
  1883. total_errors = 0;
  1884. list_for_each_entry(dev, head, dev_list) {
  1885. if (!dev->bdev)
  1886. continue;
  1887. if (!dev->in_fs_metadata || !dev->writeable)
  1888. continue;
  1889. ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
  1890. if (ret)
  1891. total_errors++;
  1892. }
  1893. if (total_errors > max_errors) {
  1894. printk(KERN_ERR "btrfs: %d errors while writing supers\n",
  1895. total_errors);
  1896. BUG();
  1897. }
  1898. return 0;
  1899. }
  1900. int write_ctree_super(struct btrfs_trans_handle *trans,
  1901. struct btrfs_root *root, int max_mirrors)
  1902. {
  1903. int ret;
  1904. ret = write_all_supers(root, max_mirrors);
  1905. return ret;
  1906. }
  1907. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1908. {
  1909. radix_tree_delete(&fs_info->fs_roots_radix,
  1910. (unsigned long)root->root_key.objectid);
  1911. if (root->anon_super.s_dev) {
  1912. down_write(&root->anon_super.s_umount);
  1913. kill_anon_super(&root->anon_super);
  1914. }
  1915. if (root->node)
  1916. free_extent_buffer(root->node);
  1917. if (root->commit_root)
  1918. free_extent_buffer(root->commit_root);
  1919. kfree(root->name);
  1920. kfree(root);
  1921. return 0;
  1922. }
  1923. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1924. {
  1925. int ret;
  1926. struct btrfs_root *gang[8];
  1927. int i;
  1928. while (1) {
  1929. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1930. (void **)gang, 0,
  1931. ARRAY_SIZE(gang));
  1932. if (!ret)
  1933. break;
  1934. for (i = 0; i < ret; i++)
  1935. btrfs_free_fs_root(fs_info, gang[i]);
  1936. }
  1937. return 0;
  1938. }
  1939. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  1940. {
  1941. u64 root_objectid = 0;
  1942. struct btrfs_root *gang[8];
  1943. int i;
  1944. int ret;
  1945. while (1) {
  1946. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1947. (void **)gang, root_objectid,
  1948. ARRAY_SIZE(gang));
  1949. if (!ret)
  1950. break;
  1951. for (i = 0; i < ret; i++) {
  1952. root_objectid = gang[i]->root_key.objectid;
  1953. ret = btrfs_find_dead_roots(fs_info->tree_root,
  1954. root_objectid, gang[i]);
  1955. BUG_ON(ret);
  1956. btrfs_orphan_cleanup(gang[i]);
  1957. }
  1958. root_objectid++;
  1959. }
  1960. return 0;
  1961. }
  1962. int btrfs_commit_super(struct btrfs_root *root)
  1963. {
  1964. struct btrfs_trans_handle *trans;
  1965. int ret;
  1966. mutex_lock(&root->fs_info->cleaner_mutex);
  1967. btrfs_clean_old_snapshots(root);
  1968. mutex_unlock(&root->fs_info->cleaner_mutex);
  1969. trans = btrfs_start_transaction(root, 1);
  1970. ret = btrfs_commit_transaction(trans, root);
  1971. BUG_ON(ret);
  1972. /* run commit again to drop the original snapshot */
  1973. trans = btrfs_start_transaction(root, 1);
  1974. btrfs_commit_transaction(trans, root);
  1975. ret = btrfs_write_and_wait_transaction(NULL, root);
  1976. BUG_ON(ret);
  1977. ret = write_ctree_super(NULL, root, 0);
  1978. return ret;
  1979. }
  1980. int close_ctree(struct btrfs_root *root)
  1981. {
  1982. struct btrfs_fs_info *fs_info = root->fs_info;
  1983. int ret;
  1984. fs_info->closing = 1;
  1985. smp_mb();
  1986. kthread_stop(root->fs_info->transaction_kthread);
  1987. kthread_stop(root->fs_info->cleaner_kthread);
  1988. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  1989. ret = btrfs_commit_super(root);
  1990. if (ret)
  1991. printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
  1992. }
  1993. if (fs_info->delalloc_bytes) {
  1994. printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
  1995. fs_info->delalloc_bytes);
  1996. }
  1997. if (fs_info->total_ref_cache_size) {
  1998. printk(KERN_INFO "btrfs: at umount reference cache size %llu\n",
  1999. (unsigned long long)fs_info->total_ref_cache_size);
  2000. }
  2001. if (fs_info->extent_root->node)
  2002. free_extent_buffer(fs_info->extent_root->node);
  2003. if (fs_info->tree_root->node)
  2004. free_extent_buffer(fs_info->tree_root->node);
  2005. if (root->fs_info->chunk_root->node)
  2006. free_extent_buffer(root->fs_info->chunk_root->node);
  2007. if (root->fs_info->dev_root->node)
  2008. free_extent_buffer(root->fs_info->dev_root->node);
  2009. if (root->fs_info->csum_root->node)
  2010. free_extent_buffer(root->fs_info->csum_root->node);
  2011. btrfs_free_block_groups(root->fs_info);
  2012. del_fs_roots(fs_info);
  2013. iput(fs_info->btree_inode);
  2014. btrfs_stop_workers(&fs_info->fixup_workers);
  2015. btrfs_stop_workers(&fs_info->delalloc_workers);
  2016. btrfs_stop_workers(&fs_info->workers);
  2017. btrfs_stop_workers(&fs_info->endio_workers);
  2018. btrfs_stop_workers(&fs_info->endio_meta_workers);
  2019. btrfs_stop_workers(&fs_info->endio_meta_write_workers);
  2020. btrfs_stop_workers(&fs_info->endio_write_workers);
  2021. btrfs_stop_workers(&fs_info->submit_workers);
  2022. #if 0
  2023. while (!list_empty(&fs_info->hashers)) {
  2024. struct btrfs_hasher *hasher;
  2025. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  2026. hashers);
  2027. list_del(&hasher->hashers);
  2028. crypto_free_hash(&fs_info->hash_tfm);
  2029. kfree(hasher);
  2030. }
  2031. #endif
  2032. btrfs_close_devices(fs_info->fs_devices);
  2033. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  2034. bdi_destroy(&fs_info->bdi);
  2035. kfree(fs_info->extent_root);
  2036. kfree(fs_info->tree_root);
  2037. kfree(fs_info->chunk_root);
  2038. kfree(fs_info->dev_root);
  2039. kfree(fs_info->csum_root);
  2040. return 0;
  2041. }
  2042. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  2043. {
  2044. int ret;
  2045. struct inode *btree_inode = buf->first_page->mapping->host;
  2046. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  2047. if (!ret)
  2048. return ret;
  2049. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  2050. parent_transid);
  2051. return !ret;
  2052. }
  2053. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  2054. {
  2055. struct inode *btree_inode = buf->first_page->mapping->host;
  2056. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  2057. buf);
  2058. }
  2059. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  2060. {
  2061. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2062. u64 transid = btrfs_header_generation(buf);
  2063. struct inode *btree_inode = root->fs_info->btree_inode;
  2064. int was_dirty;
  2065. btrfs_assert_tree_locked(buf);
  2066. if (transid != root->fs_info->generation) {
  2067. printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
  2068. "found %llu running %llu\n",
  2069. (unsigned long long)buf->start,
  2070. (unsigned long long)transid,
  2071. (unsigned long long)root->fs_info->generation);
  2072. WARN_ON(1);
  2073. }
  2074. was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  2075. buf);
  2076. if (!was_dirty) {
  2077. spin_lock(&root->fs_info->delalloc_lock);
  2078. root->fs_info->dirty_metadata_bytes += buf->len;
  2079. spin_unlock(&root->fs_info->delalloc_lock);
  2080. }
  2081. }
  2082. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  2083. {
  2084. /*
  2085. * looks as though older kernels can get into trouble with
  2086. * this code, they end up stuck in balance_dirty_pages forever
  2087. */
  2088. struct extent_io_tree *tree;
  2089. u64 num_dirty;
  2090. u64 start = 0;
  2091. unsigned long thresh = 32 * 1024 * 1024;
  2092. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  2093. if (current_is_pdflush() || current->flags & PF_MEMALLOC)
  2094. return;
  2095. num_dirty = count_range_bits(tree, &start, (u64)-1,
  2096. thresh, EXTENT_DIRTY);
  2097. if (num_dirty > thresh) {
  2098. balance_dirty_pages_ratelimited_nr(
  2099. root->fs_info->btree_inode->i_mapping, 1);
  2100. }
  2101. return;
  2102. }
  2103. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  2104. {
  2105. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2106. int ret;
  2107. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  2108. if (ret == 0)
  2109. set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
  2110. return ret;
  2111. }
  2112. int btree_lock_page_hook(struct page *page)
  2113. {
  2114. struct inode *inode = page->mapping->host;
  2115. struct btrfs_root *root = BTRFS_I(inode)->root;
  2116. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2117. struct extent_buffer *eb;
  2118. unsigned long len;
  2119. u64 bytenr = page_offset(page);
  2120. if (page->private == EXTENT_PAGE_PRIVATE)
  2121. goto out;
  2122. len = page->private >> 2;
  2123. eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
  2124. if (!eb)
  2125. goto out;
  2126. btrfs_tree_lock(eb);
  2127. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  2128. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  2129. spin_lock(&root->fs_info->delalloc_lock);
  2130. if (root->fs_info->dirty_metadata_bytes >= eb->len)
  2131. root->fs_info->dirty_metadata_bytes -= eb->len;
  2132. else
  2133. WARN_ON(1);
  2134. spin_unlock(&root->fs_info->delalloc_lock);
  2135. }
  2136. btrfs_tree_unlock(eb);
  2137. free_extent_buffer(eb);
  2138. out:
  2139. lock_page(page);
  2140. return 0;
  2141. }
  2142. static struct extent_io_ops btree_extent_io_ops = {
  2143. .write_cache_pages_lock_hook = btree_lock_page_hook,
  2144. .readpage_end_io_hook = btree_readpage_end_io_hook,
  2145. .submit_bio_hook = btree_submit_bio_hook,
  2146. /* note we're sharing with inode.c for the merge bio hook */
  2147. .merge_bio_hook = btrfs_merge_bio_hook,
  2148. };