disk-io.c 100 KB

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