disk-io.c 84 KB

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