disk-io.c 70 KB

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