disk-io.c 69 KB

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