disk-io.c 82 KB

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