disk-io.c 83 KB

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