disk-io.c 96 KB

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