disk-io.c 70 KB

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