disk-io.c 83 KB

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