super.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  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/blkdev.h>
  19. #include <linux/module.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/fs.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/highmem.h>
  24. #include <linux/time.h>
  25. #include <linux/init.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mount.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/parser.h>
  36. #include <linux/ctype.h>
  37. #include <linux/namei.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/magic.h>
  40. #include <linux/slab.h>
  41. #include <linux/cleancache.h>
  42. #include <linux/ratelimit.h>
  43. #include <linux/btrfs.h>
  44. #include "delayed-inode.h"
  45. #include "ctree.h"
  46. #include "disk-io.h"
  47. #include "transaction.h"
  48. #include "btrfs_inode.h"
  49. #include "print-tree.h"
  50. #include "hash.h"
  51. #include "props.h"
  52. #include "xattr.h"
  53. #include "volumes.h"
  54. #include "export.h"
  55. #include "compression.h"
  56. #include "rcu-string.h"
  57. #include "dev-replace.h"
  58. #include "free-space-cache.h"
  59. #include "backref.h"
  60. #include "tests/btrfs-tests.h"
  61. #include "qgroup.h"
  62. #define CREATE_TRACE_POINTS
  63. #include <trace/events/btrfs.h>
  64. static const struct super_operations btrfs_super_ops;
  65. static struct file_system_type btrfs_fs_type;
  66. static int btrfs_remount(struct super_block *sb, int *flags, char *data);
  67. const char *btrfs_decode_error(int errno)
  68. {
  69. char *errstr = "unknown";
  70. switch (errno) {
  71. case -EIO:
  72. errstr = "IO failure";
  73. break;
  74. case -ENOMEM:
  75. errstr = "Out of memory";
  76. break;
  77. case -EROFS:
  78. errstr = "Readonly filesystem";
  79. break;
  80. case -EEXIST:
  81. errstr = "Object already exists";
  82. break;
  83. case -ENOSPC:
  84. errstr = "No space left";
  85. break;
  86. case -ENOENT:
  87. errstr = "No such entry";
  88. break;
  89. }
  90. return errstr;
  91. }
  92. static void save_error_info(struct btrfs_fs_info *fs_info)
  93. {
  94. /*
  95. * today we only save the error info into ram. Long term we'll
  96. * also send it down to the disk
  97. */
  98. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  99. }
  100. /* btrfs handle error by forcing the filesystem readonly */
  101. static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
  102. {
  103. struct super_block *sb = fs_info->sb;
  104. if (sb->s_flags & MS_RDONLY)
  105. return;
  106. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  107. sb->s_flags |= MS_RDONLY;
  108. btrfs_info(fs_info, "forced readonly");
  109. /*
  110. * Note that a running device replace operation is not
  111. * canceled here although there is no way to update
  112. * the progress. It would add the risk of a deadlock,
  113. * therefore the canceling is ommited. The only penalty
  114. * is that some I/O remains active until the procedure
  115. * completes. The next time when the filesystem is
  116. * mounted writeable again, the device replace
  117. * operation continues.
  118. */
  119. }
  120. }
  121. /*
  122. * __btrfs_std_error decodes expected errors from the caller and
  123. * invokes the approciate error response.
  124. */
  125. __cold
  126. void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
  127. unsigned int line, int errno, const char *fmt, ...)
  128. {
  129. struct super_block *sb = fs_info->sb;
  130. #ifdef CONFIG_PRINTK
  131. const char *errstr;
  132. #endif
  133. /*
  134. * Special case: if the error is EROFS, and we're already
  135. * under MS_RDONLY, then it is safe here.
  136. */
  137. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  138. return;
  139. #ifdef CONFIG_PRINTK
  140. errstr = btrfs_decode_error(errno);
  141. if (fmt) {
  142. struct va_format vaf;
  143. va_list args;
  144. va_start(args, fmt);
  145. vaf.fmt = fmt;
  146. vaf.va = &args;
  147. printk(KERN_CRIT
  148. "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
  149. sb->s_id, function, line, errno, errstr, &vaf);
  150. va_end(args);
  151. } else {
  152. printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
  153. sb->s_id, function, line, errno, errstr);
  154. }
  155. #endif
  156. /* Don't go through full error handling during mount */
  157. save_error_info(fs_info);
  158. if (sb->s_flags & MS_BORN)
  159. btrfs_handle_error(fs_info);
  160. }
  161. #ifdef CONFIG_PRINTK
  162. static const char * const logtypes[] = {
  163. "emergency",
  164. "alert",
  165. "critical",
  166. "error",
  167. "warning",
  168. "notice",
  169. "info",
  170. "debug",
  171. };
  172. void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
  173. {
  174. struct super_block *sb = fs_info->sb;
  175. char lvl[4];
  176. struct va_format vaf;
  177. va_list args;
  178. const char *type = logtypes[4];
  179. int kern_level;
  180. va_start(args, fmt);
  181. kern_level = printk_get_level(fmt);
  182. if (kern_level) {
  183. size_t size = printk_skip_level(fmt) - fmt;
  184. memcpy(lvl, fmt, size);
  185. lvl[size] = '\0';
  186. fmt += size;
  187. type = logtypes[kern_level - '0'];
  188. } else
  189. *lvl = '\0';
  190. vaf.fmt = fmt;
  191. vaf.va = &args;
  192. printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
  193. va_end(args);
  194. }
  195. #endif
  196. /*
  197. * We only mark the transaction aborted and then set the file system read-only.
  198. * This will prevent new transactions from starting or trying to join this
  199. * one.
  200. *
  201. * This means that error recovery at the call site is limited to freeing
  202. * any local memory allocations and passing the error code up without
  203. * further cleanup. The transaction should complete as it normally would
  204. * in the call path but will return -EIO.
  205. *
  206. * We'll complete the cleanup in btrfs_end_transaction and
  207. * btrfs_commit_transaction.
  208. */
  209. __cold
  210. void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  211. struct btrfs_root *root, const char *function,
  212. unsigned int line, int errno)
  213. {
  214. trans->aborted = errno;
  215. /* Nothing used. The other threads that have joined this
  216. * transaction may be able to continue. */
  217. if (!trans->blocks_used && list_empty(&trans->new_bgs)) {
  218. const char *errstr;
  219. errstr = btrfs_decode_error(errno);
  220. btrfs_warn(root->fs_info,
  221. "%s:%d: Aborting unused transaction(%s).",
  222. function, line, errstr);
  223. return;
  224. }
  225. ACCESS_ONCE(trans->transaction->aborted) = errno;
  226. /* Wake up anybody who may be waiting on this transaction */
  227. wake_up(&root->fs_info->transaction_wait);
  228. wake_up(&root->fs_info->transaction_blocked_wait);
  229. __btrfs_std_error(root->fs_info, function, line, errno, NULL);
  230. }
  231. /*
  232. * __btrfs_panic decodes unexpected, fatal errors from the caller,
  233. * issues an alert, and either panics or BUGs, depending on mount options.
  234. */
  235. __cold
  236. void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  237. unsigned int line, int errno, const char *fmt, ...)
  238. {
  239. char *s_id = "<unknown>";
  240. const char *errstr;
  241. struct va_format vaf = { .fmt = fmt };
  242. va_list args;
  243. if (fs_info)
  244. s_id = fs_info->sb->s_id;
  245. va_start(args, fmt);
  246. vaf.va = &args;
  247. errstr = btrfs_decode_error(errno);
  248. if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
  249. panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  250. s_id, function, line, &vaf, errno, errstr);
  251. btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
  252. function, line, &vaf, errno, errstr);
  253. va_end(args);
  254. /* Caller calls BUG() */
  255. }
  256. static void btrfs_put_super(struct super_block *sb)
  257. {
  258. close_ctree(btrfs_sb(sb)->tree_root);
  259. }
  260. enum {
  261. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  262. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  263. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  264. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  265. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  266. Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
  267. Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
  268. Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
  269. Opt_skip_balance, Opt_check_integrity,
  270. Opt_check_integrity_including_extent_data,
  271. Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
  272. Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
  273. Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
  274. Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
  275. Opt_nologreplay, Opt_norecovery,
  276. #ifdef CONFIG_BTRFS_DEBUG
  277. Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
  278. #endif
  279. Opt_err,
  280. };
  281. static const match_table_t tokens = {
  282. {Opt_degraded, "degraded"},
  283. {Opt_subvol, "subvol=%s"},
  284. {Opt_subvolid, "subvolid=%s"},
  285. {Opt_device, "device=%s"},
  286. {Opt_nodatasum, "nodatasum"},
  287. {Opt_datasum, "datasum"},
  288. {Opt_nodatacow, "nodatacow"},
  289. {Opt_datacow, "datacow"},
  290. {Opt_nobarrier, "nobarrier"},
  291. {Opt_barrier, "barrier"},
  292. {Opt_max_inline, "max_inline=%s"},
  293. {Opt_alloc_start, "alloc_start=%s"},
  294. {Opt_thread_pool, "thread_pool=%d"},
  295. {Opt_compress, "compress"},
  296. {Opt_compress_type, "compress=%s"},
  297. {Opt_compress_force, "compress-force"},
  298. {Opt_compress_force_type, "compress-force=%s"},
  299. {Opt_ssd, "ssd"},
  300. {Opt_ssd_spread, "ssd_spread"},
  301. {Opt_nossd, "nossd"},
  302. {Opt_acl, "acl"},
  303. {Opt_noacl, "noacl"},
  304. {Opt_notreelog, "notreelog"},
  305. {Opt_treelog, "treelog"},
  306. {Opt_nologreplay, "nologreplay"},
  307. {Opt_norecovery, "norecovery"},
  308. {Opt_flushoncommit, "flushoncommit"},
  309. {Opt_noflushoncommit, "noflushoncommit"},
  310. {Opt_ratio, "metadata_ratio=%d"},
  311. {Opt_discard, "discard"},
  312. {Opt_nodiscard, "nodiscard"},
  313. {Opt_space_cache, "space_cache"},
  314. {Opt_space_cache_version, "space_cache=%s"},
  315. {Opt_clear_cache, "clear_cache"},
  316. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  317. {Opt_enospc_debug, "enospc_debug"},
  318. {Opt_noenospc_debug, "noenospc_debug"},
  319. {Opt_subvolrootid, "subvolrootid=%d"},
  320. {Opt_defrag, "autodefrag"},
  321. {Opt_nodefrag, "noautodefrag"},
  322. {Opt_inode_cache, "inode_cache"},
  323. {Opt_noinode_cache, "noinode_cache"},
  324. {Opt_no_space_cache, "nospace_cache"},
  325. {Opt_recovery, "recovery"}, /* deprecated */
  326. {Opt_usebackuproot, "usebackuproot"},
  327. {Opt_skip_balance, "skip_balance"},
  328. {Opt_check_integrity, "check_int"},
  329. {Opt_check_integrity_including_extent_data, "check_int_data"},
  330. {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
  331. {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
  332. {Opt_fatal_errors, "fatal_errors=%s"},
  333. {Opt_commit_interval, "commit=%d"},
  334. #ifdef CONFIG_BTRFS_DEBUG
  335. {Opt_fragment_data, "fragment=data"},
  336. {Opt_fragment_metadata, "fragment=metadata"},
  337. {Opt_fragment_all, "fragment=all"},
  338. #endif
  339. {Opt_err, NULL},
  340. };
  341. /*
  342. * Regular mount options parser. Everything that is needed only when
  343. * reading in a new superblock is parsed here.
  344. * XXX JDM: This needs to be cleaned up for remount.
  345. */
  346. int btrfs_parse_options(struct btrfs_root *root, char *options,
  347. unsigned long new_flags)
  348. {
  349. struct btrfs_fs_info *info = root->fs_info;
  350. substring_t args[MAX_OPT_ARGS];
  351. char *p, *num, *orig = NULL;
  352. u64 cache_gen;
  353. int intarg;
  354. int ret = 0;
  355. char *compress_type;
  356. bool compress_force = false;
  357. enum btrfs_compression_type saved_compress_type;
  358. bool saved_compress_force;
  359. int no_compress = 0;
  360. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  361. if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE))
  362. btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
  363. else if (cache_gen)
  364. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  365. /*
  366. * Even the options are empty, we still need to do extra check
  367. * against new flags
  368. */
  369. if (!options)
  370. goto check;
  371. /*
  372. * strsep changes the string, duplicate it because parse_options
  373. * gets called twice
  374. */
  375. options = kstrdup(options, GFP_NOFS);
  376. if (!options)
  377. return -ENOMEM;
  378. orig = options;
  379. while ((p = strsep(&options, ",")) != NULL) {
  380. int token;
  381. if (!*p)
  382. continue;
  383. token = match_token(p, tokens, args);
  384. switch (token) {
  385. case Opt_degraded:
  386. btrfs_info(root->fs_info, "allowing degraded mounts");
  387. btrfs_set_opt(info->mount_opt, DEGRADED);
  388. break;
  389. case Opt_subvol:
  390. case Opt_subvolid:
  391. case Opt_subvolrootid:
  392. case Opt_device:
  393. /*
  394. * These are parsed by btrfs_parse_early_options
  395. * and can be happily ignored here.
  396. */
  397. break;
  398. case Opt_nodatasum:
  399. btrfs_set_and_info(root, NODATASUM,
  400. "setting nodatasum");
  401. break;
  402. case Opt_datasum:
  403. if (btrfs_test_opt(root, NODATASUM)) {
  404. if (btrfs_test_opt(root, NODATACOW))
  405. btrfs_info(root->fs_info, "setting datasum, datacow enabled");
  406. else
  407. btrfs_info(root->fs_info, "setting datasum");
  408. }
  409. btrfs_clear_opt(info->mount_opt, NODATACOW);
  410. btrfs_clear_opt(info->mount_opt, NODATASUM);
  411. break;
  412. case Opt_nodatacow:
  413. if (!btrfs_test_opt(root, NODATACOW)) {
  414. if (!btrfs_test_opt(root, COMPRESS) ||
  415. !btrfs_test_opt(root, FORCE_COMPRESS)) {
  416. btrfs_info(root->fs_info,
  417. "setting nodatacow, compression disabled");
  418. } else {
  419. btrfs_info(root->fs_info, "setting nodatacow");
  420. }
  421. }
  422. btrfs_clear_opt(info->mount_opt, COMPRESS);
  423. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  424. btrfs_set_opt(info->mount_opt, NODATACOW);
  425. btrfs_set_opt(info->mount_opt, NODATASUM);
  426. break;
  427. case Opt_datacow:
  428. btrfs_clear_and_info(root, NODATACOW,
  429. "setting datacow");
  430. break;
  431. case Opt_compress_force:
  432. case Opt_compress_force_type:
  433. compress_force = true;
  434. /* Fallthrough */
  435. case Opt_compress:
  436. case Opt_compress_type:
  437. saved_compress_type = btrfs_test_opt(root, COMPRESS) ?
  438. info->compress_type : BTRFS_COMPRESS_NONE;
  439. saved_compress_force =
  440. btrfs_test_opt(root, FORCE_COMPRESS);
  441. if (token == Opt_compress ||
  442. token == Opt_compress_force ||
  443. strcmp(args[0].from, "zlib") == 0) {
  444. compress_type = "zlib";
  445. info->compress_type = BTRFS_COMPRESS_ZLIB;
  446. btrfs_set_opt(info->mount_opt, COMPRESS);
  447. btrfs_clear_opt(info->mount_opt, NODATACOW);
  448. btrfs_clear_opt(info->mount_opt, NODATASUM);
  449. no_compress = 0;
  450. } else if (strcmp(args[0].from, "lzo") == 0) {
  451. compress_type = "lzo";
  452. info->compress_type = BTRFS_COMPRESS_LZO;
  453. btrfs_set_opt(info->mount_opt, COMPRESS);
  454. btrfs_clear_opt(info->mount_opt, NODATACOW);
  455. btrfs_clear_opt(info->mount_opt, NODATASUM);
  456. btrfs_set_fs_incompat(info, COMPRESS_LZO);
  457. no_compress = 0;
  458. } else if (strncmp(args[0].from, "no", 2) == 0) {
  459. compress_type = "no";
  460. btrfs_clear_opt(info->mount_opt, COMPRESS);
  461. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  462. compress_force = false;
  463. no_compress++;
  464. } else {
  465. ret = -EINVAL;
  466. goto out;
  467. }
  468. if (compress_force) {
  469. btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
  470. } else {
  471. /*
  472. * If we remount from compress-force=xxx to
  473. * compress=xxx, we need clear FORCE_COMPRESS
  474. * flag, otherwise, there is no way for users
  475. * to disable forcible compression separately.
  476. */
  477. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  478. }
  479. if ((btrfs_test_opt(root, COMPRESS) &&
  480. (info->compress_type != saved_compress_type ||
  481. compress_force != saved_compress_force)) ||
  482. (!btrfs_test_opt(root, COMPRESS) &&
  483. no_compress == 1)) {
  484. btrfs_info(root->fs_info,
  485. "%s %s compression",
  486. (compress_force) ? "force" : "use",
  487. compress_type);
  488. }
  489. compress_force = false;
  490. break;
  491. case Opt_ssd:
  492. btrfs_set_and_info(root, SSD,
  493. "use ssd allocation scheme");
  494. break;
  495. case Opt_ssd_spread:
  496. btrfs_set_and_info(root, SSD_SPREAD,
  497. "use spread ssd allocation scheme");
  498. btrfs_set_opt(info->mount_opt, SSD);
  499. break;
  500. case Opt_nossd:
  501. btrfs_set_and_info(root, NOSSD,
  502. "not using ssd allocation scheme");
  503. btrfs_clear_opt(info->mount_opt, SSD);
  504. break;
  505. case Opt_barrier:
  506. btrfs_clear_and_info(root, NOBARRIER,
  507. "turning on barriers");
  508. break;
  509. case Opt_nobarrier:
  510. btrfs_set_and_info(root, NOBARRIER,
  511. "turning off barriers");
  512. break;
  513. case Opt_thread_pool:
  514. ret = match_int(&args[0], &intarg);
  515. if (ret) {
  516. goto out;
  517. } else if (intarg > 0) {
  518. info->thread_pool_size = intarg;
  519. } else {
  520. ret = -EINVAL;
  521. goto out;
  522. }
  523. break;
  524. case Opt_max_inline:
  525. num = match_strdup(&args[0]);
  526. if (num) {
  527. info->max_inline = memparse(num, NULL);
  528. kfree(num);
  529. if (info->max_inline) {
  530. info->max_inline = min_t(u64,
  531. info->max_inline,
  532. root->sectorsize);
  533. }
  534. btrfs_info(root->fs_info, "max_inline at %llu",
  535. info->max_inline);
  536. } else {
  537. ret = -ENOMEM;
  538. goto out;
  539. }
  540. break;
  541. case Opt_alloc_start:
  542. num = match_strdup(&args[0]);
  543. if (num) {
  544. mutex_lock(&info->chunk_mutex);
  545. info->alloc_start = memparse(num, NULL);
  546. mutex_unlock(&info->chunk_mutex);
  547. kfree(num);
  548. btrfs_info(root->fs_info, "allocations start at %llu",
  549. info->alloc_start);
  550. } else {
  551. ret = -ENOMEM;
  552. goto out;
  553. }
  554. break;
  555. case Opt_acl:
  556. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  557. root->fs_info->sb->s_flags |= MS_POSIXACL;
  558. break;
  559. #else
  560. btrfs_err(root->fs_info,
  561. "support for ACL not compiled in!");
  562. ret = -EINVAL;
  563. goto out;
  564. #endif
  565. case Opt_noacl:
  566. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  567. break;
  568. case Opt_notreelog:
  569. btrfs_set_and_info(root, NOTREELOG,
  570. "disabling tree log");
  571. break;
  572. case Opt_treelog:
  573. btrfs_clear_and_info(root, NOTREELOG,
  574. "enabling tree log");
  575. break;
  576. case Opt_norecovery:
  577. case Opt_nologreplay:
  578. btrfs_set_and_info(root, NOLOGREPLAY,
  579. "disabling log replay at mount time");
  580. break;
  581. case Opt_flushoncommit:
  582. btrfs_set_and_info(root, FLUSHONCOMMIT,
  583. "turning on flush-on-commit");
  584. break;
  585. case Opt_noflushoncommit:
  586. btrfs_clear_and_info(root, FLUSHONCOMMIT,
  587. "turning off flush-on-commit");
  588. break;
  589. case Opt_ratio:
  590. ret = match_int(&args[0], &intarg);
  591. if (ret) {
  592. goto out;
  593. } else if (intarg >= 0) {
  594. info->metadata_ratio = intarg;
  595. btrfs_info(root->fs_info, "metadata ratio %d",
  596. info->metadata_ratio);
  597. } else {
  598. ret = -EINVAL;
  599. goto out;
  600. }
  601. break;
  602. case Opt_discard:
  603. btrfs_set_and_info(root, DISCARD,
  604. "turning on discard");
  605. break;
  606. case Opt_nodiscard:
  607. btrfs_clear_and_info(root, DISCARD,
  608. "turning off discard");
  609. break;
  610. case Opt_space_cache:
  611. case Opt_space_cache_version:
  612. if (token == Opt_space_cache ||
  613. strcmp(args[0].from, "v1") == 0) {
  614. btrfs_clear_opt(root->fs_info->mount_opt,
  615. FREE_SPACE_TREE);
  616. btrfs_set_and_info(root, SPACE_CACHE,
  617. "enabling disk space caching");
  618. } else if (strcmp(args[0].from, "v2") == 0) {
  619. btrfs_clear_opt(root->fs_info->mount_opt,
  620. SPACE_CACHE);
  621. btrfs_set_and_info(root, FREE_SPACE_TREE,
  622. "enabling free space tree");
  623. } else {
  624. ret = -EINVAL;
  625. goto out;
  626. }
  627. break;
  628. case Opt_rescan_uuid_tree:
  629. btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
  630. break;
  631. case Opt_no_space_cache:
  632. if (btrfs_test_opt(root, SPACE_CACHE)) {
  633. btrfs_clear_and_info(root, SPACE_CACHE,
  634. "disabling disk space caching");
  635. }
  636. if (btrfs_test_opt(root, FREE_SPACE_TREE)) {
  637. btrfs_clear_and_info(root, FREE_SPACE_TREE,
  638. "disabling free space tree");
  639. }
  640. break;
  641. case Opt_inode_cache:
  642. btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
  643. "enabling inode map caching");
  644. break;
  645. case Opt_noinode_cache:
  646. btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
  647. "disabling inode map caching");
  648. break;
  649. case Opt_clear_cache:
  650. btrfs_set_and_info(root, CLEAR_CACHE,
  651. "force clearing of disk cache");
  652. break;
  653. case Opt_user_subvol_rm_allowed:
  654. btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  655. break;
  656. case Opt_enospc_debug:
  657. btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
  658. break;
  659. case Opt_noenospc_debug:
  660. btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
  661. break;
  662. case Opt_defrag:
  663. btrfs_set_and_info(root, AUTO_DEFRAG,
  664. "enabling auto defrag");
  665. break;
  666. case Opt_nodefrag:
  667. btrfs_clear_and_info(root, AUTO_DEFRAG,
  668. "disabling auto defrag");
  669. break;
  670. case Opt_recovery:
  671. btrfs_warn(root->fs_info,
  672. "'recovery' is deprecated, use 'usebackuproot' instead");
  673. case Opt_usebackuproot:
  674. btrfs_info(root->fs_info,
  675. "trying to use backup root at mount time");
  676. btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
  677. break;
  678. case Opt_skip_balance:
  679. btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
  680. break;
  681. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  682. case Opt_check_integrity_including_extent_data:
  683. btrfs_info(root->fs_info,
  684. "enabling check integrity including extent data");
  685. btrfs_set_opt(info->mount_opt,
  686. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
  687. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  688. break;
  689. case Opt_check_integrity:
  690. btrfs_info(root->fs_info, "enabling check integrity");
  691. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  692. break;
  693. case Opt_check_integrity_print_mask:
  694. ret = match_int(&args[0], &intarg);
  695. if (ret) {
  696. goto out;
  697. } else if (intarg >= 0) {
  698. info->check_integrity_print_mask = intarg;
  699. btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x",
  700. info->check_integrity_print_mask);
  701. } else {
  702. ret = -EINVAL;
  703. goto out;
  704. }
  705. break;
  706. #else
  707. case Opt_check_integrity_including_extent_data:
  708. case Opt_check_integrity:
  709. case Opt_check_integrity_print_mask:
  710. btrfs_err(root->fs_info,
  711. "support for check_integrity* not compiled in!");
  712. ret = -EINVAL;
  713. goto out;
  714. #endif
  715. case Opt_fatal_errors:
  716. if (strcmp(args[0].from, "panic") == 0)
  717. btrfs_set_opt(info->mount_opt,
  718. PANIC_ON_FATAL_ERROR);
  719. else if (strcmp(args[0].from, "bug") == 0)
  720. btrfs_clear_opt(info->mount_opt,
  721. PANIC_ON_FATAL_ERROR);
  722. else {
  723. ret = -EINVAL;
  724. goto out;
  725. }
  726. break;
  727. case Opt_commit_interval:
  728. intarg = 0;
  729. ret = match_int(&args[0], &intarg);
  730. if (ret < 0) {
  731. btrfs_err(root->fs_info, "invalid commit interval");
  732. ret = -EINVAL;
  733. goto out;
  734. }
  735. if (intarg > 0) {
  736. if (intarg > 300) {
  737. btrfs_warn(root->fs_info, "excessive commit interval %d",
  738. intarg);
  739. }
  740. info->commit_interval = intarg;
  741. } else {
  742. btrfs_info(root->fs_info, "using default commit interval %ds",
  743. BTRFS_DEFAULT_COMMIT_INTERVAL);
  744. info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  745. }
  746. break;
  747. #ifdef CONFIG_BTRFS_DEBUG
  748. case Opt_fragment_all:
  749. btrfs_info(root->fs_info, "fragmenting all space");
  750. btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
  751. btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
  752. break;
  753. case Opt_fragment_metadata:
  754. btrfs_info(root->fs_info, "fragmenting metadata");
  755. btrfs_set_opt(info->mount_opt,
  756. FRAGMENT_METADATA);
  757. break;
  758. case Opt_fragment_data:
  759. btrfs_info(root->fs_info, "fragmenting data");
  760. btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
  761. break;
  762. #endif
  763. case Opt_err:
  764. btrfs_info(root->fs_info, "unrecognized mount option '%s'", p);
  765. ret = -EINVAL;
  766. goto out;
  767. default:
  768. break;
  769. }
  770. }
  771. check:
  772. /*
  773. * Extra check for current option against current flag
  774. */
  775. if (btrfs_test_opt(root, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) {
  776. btrfs_err(root->fs_info,
  777. "nologreplay must be used with ro mount option");
  778. ret = -EINVAL;
  779. }
  780. out:
  781. if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE) &&
  782. !btrfs_test_opt(root, FREE_SPACE_TREE) &&
  783. !btrfs_test_opt(root, CLEAR_CACHE)) {
  784. btrfs_err(root->fs_info, "cannot disable free space tree");
  785. ret = -EINVAL;
  786. }
  787. if (!ret && btrfs_test_opt(root, SPACE_CACHE))
  788. btrfs_info(root->fs_info, "disk space caching is enabled");
  789. if (!ret && btrfs_test_opt(root, FREE_SPACE_TREE))
  790. btrfs_info(root->fs_info, "using free space tree");
  791. kfree(orig);
  792. return ret;
  793. }
  794. /*
  795. * Parse mount options that are required early in the mount process.
  796. *
  797. * All other options will be parsed on much later in the mount process and
  798. * only when we need to allocate a new super block.
  799. */
  800. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  801. void *holder, char **subvol_name, u64 *subvol_objectid,
  802. struct btrfs_fs_devices **fs_devices)
  803. {
  804. substring_t args[MAX_OPT_ARGS];
  805. char *device_name, *opts, *orig, *p;
  806. char *num = NULL;
  807. int error = 0;
  808. if (!options)
  809. return 0;
  810. /*
  811. * strsep changes the string, duplicate it because parse_options
  812. * gets called twice
  813. */
  814. opts = kstrdup(options, GFP_KERNEL);
  815. if (!opts)
  816. return -ENOMEM;
  817. orig = opts;
  818. while ((p = strsep(&opts, ",")) != NULL) {
  819. int token;
  820. if (!*p)
  821. continue;
  822. token = match_token(p, tokens, args);
  823. switch (token) {
  824. case Opt_subvol:
  825. kfree(*subvol_name);
  826. *subvol_name = match_strdup(&args[0]);
  827. if (!*subvol_name) {
  828. error = -ENOMEM;
  829. goto out;
  830. }
  831. break;
  832. case Opt_subvolid:
  833. num = match_strdup(&args[0]);
  834. if (num) {
  835. *subvol_objectid = memparse(num, NULL);
  836. kfree(num);
  837. /* we want the original fs_tree */
  838. if (!*subvol_objectid)
  839. *subvol_objectid =
  840. BTRFS_FS_TREE_OBJECTID;
  841. } else {
  842. error = -EINVAL;
  843. goto out;
  844. }
  845. break;
  846. case Opt_subvolrootid:
  847. printk(KERN_WARNING
  848. "BTRFS: 'subvolrootid' mount option is deprecated and has "
  849. "no effect\n");
  850. break;
  851. case Opt_device:
  852. device_name = match_strdup(&args[0]);
  853. if (!device_name) {
  854. error = -ENOMEM;
  855. goto out;
  856. }
  857. error = btrfs_scan_one_device(device_name,
  858. flags, holder, fs_devices);
  859. kfree(device_name);
  860. if (error)
  861. goto out;
  862. break;
  863. default:
  864. break;
  865. }
  866. }
  867. out:
  868. kfree(orig);
  869. return error;
  870. }
  871. static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
  872. u64 subvol_objectid)
  873. {
  874. struct btrfs_root *root = fs_info->tree_root;
  875. struct btrfs_root *fs_root;
  876. struct btrfs_root_ref *root_ref;
  877. struct btrfs_inode_ref *inode_ref;
  878. struct btrfs_key key;
  879. struct btrfs_path *path = NULL;
  880. char *name = NULL, *ptr;
  881. u64 dirid;
  882. int len;
  883. int ret;
  884. path = btrfs_alloc_path();
  885. if (!path) {
  886. ret = -ENOMEM;
  887. goto err;
  888. }
  889. path->leave_spinning = 1;
  890. name = kmalloc(PATH_MAX, GFP_NOFS);
  891. if (!name) {
  892. ret = -ENOMEM;
  893. goto err;
  894. }
  895. ptr = name + PATH_MAX - 1;
  896. ptr[0] = '\0';
  897. /*
  898. * Walk up the subvolume trees in the tree of tree roots by root
  899. * backrefs until we hit the top-level subvolume.
  900. */
  901. while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  902. key.objectid = subvol_objectid;
  903. key.type = BTRFS_ROOT_BACKREF_KEY;
  904. key.offset = (u64)-1;
  905. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  906. if (ret < 0) {
  907. goto err;
  908. } else if (ret > 0) {
  909. ret = btrfs_previous_item(root, path, subvol_objectid,
  910. BTRFS_ROOT_BACKREF_KEY);
  911. if (ret < 0) {
  912. goto err;
  913. } else if (ret > 0) {
  914. ret = -ENOENT;
  915. goto err;
  916. }
  917. }
  918. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  919. subvol_objectid = key.offset;
  920. root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  921. struct btrfs_root_ref);
  922. len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
  923. ptr -= len + 1;
  924. if (ptr < name) {
  925. ret = -ENAMETOOLONG;
  926. goto err;
  927. }
  928. read_extent_buffer(path->nodes[0], ptr + 1,
  929. (unsigned long)(root_ref + 1), len);
  930. ptr[0] = '/';
  931. dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
  932. btrfs_release_path(path);
  933. key.objectid = subvol_objectid;
  934. key.type = BTRFS_ROOT_ITEM_KEY;
  935. key.offset = (u64)-1;
  936. fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
  937. if (IS_ERR(fs_root)) {
  938. ret = PTR_ERR(fs_root);
  939. goto err;
  940. }
  941. /*
  942. * Walk up the filesystem tree by inode refs until we hit the
  943. * root directory.
  944. */
  945. while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
  946. key.objectid = dirid;
  947. key.type = BTRFS_INODE_REF_KEY;
  948. key.offset = (u64)-1;
  949. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  950. if (ret < 0) {
  951. goto err;
  952. } else if (ret > 0) {
  953. ret = btrfs_previous_item(fs_root, path, dirid,
  954. BTRFS_INODE_REF_KEY);
  955. if (ret < 0) {
  956. goto err;
  957. } else if (ret > 0) {
  958. ret = -ENOENT;
  959. goto err;
  960. }
  961. }
  962. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  963. dirid = key.offset;
  964. inode_ref = btrfs_item_ptr(path->nodes[0],
  965. path->slots[0],
  966. struct btrfs_inode_ref);
  967. len = btrfs_inode_ref_name_len(path->nodes[0],
  968. inode_ref);
  969. ptr -= len + 1;
  970. if (ptr < name) {
  971. ret = -ENAMETOOLONG;
  972. goto err;
  973. }
  974. read_extent_buffer(path->nodes[0], ptr + 1,
  975. (unsigned long)(inode_ref + 1), len);
  976. ptr[0] = '/';
  977. btrfs_release_path(path);
  978. }
  979. }
  980. btrfs_free_path(path);
  981. if (ptr == name + PATH_MAX - 1) {
  982. name[0] = '/';
  983. name[1] = '\0';
  984. } else {
  985. memmove(name, ptr, name + PATH_MAX - ptr);
  986. }
  987. return name;
  988. err:
  989. btrfs_free_path(path);
  990. kfree(name);
  991. return ERR_PTR(ret);
  992. }
  993. static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
  994. {
  995. struct btrfs_root *root = fs_info->tree_root;
  996. struct btrfs_dir_item *di;
  997. struct btrfs_path *path;
  998. struct btrfs_key location;
  999. u64 dir_id;
  1000. path = btrfs_alloc_path();
  1001. if (!path)
  1002. return -ENOMEM;
  1003. path->leave_spinning = 1;
  1004. /*
  1005. * Find the "default" dir item which points to the root item that we
  1006. * will mount by default if we haven't been given a specific subvolume
  1007. * to mount.
  1008. */
  1009. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  1010. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
  1011. if (IS_ERR(di)) {
  1012. btrfs_free_path(path);
  1013. return PTR_ERR(di);
  1014. }
  1015. if (!di) {
  1016. /*
  1017. * Ok the default dir item isn't there. This is weird since
  1018. * it's always been there, but don't freak out, just try and
  1019. * mount the top-level subvolume.
  1020. */
  1021. btrfs_free_path(path);
  1022. *objectid = BTRFS_FS_TREE_OBJECTID;
  1023. return 0;
  1024. }
  1025. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  1026. btrfs_free_path(path);
  1027. *objectid = location.objectid;
  1028. return 0;
  1029. }
  1030. static int btrfs_fill_super(struct super_block *sb,
  1031. struct btrfs_fs_devices *fs_devices,
  1032. void *data, int silent)
  1033. {
  1034. struct inode *inode;
  1035. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1036. struct btrfs_key key;
  1037. int err;
  1038. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1039. sb->s_magic = BTRFS_SUPER_MAGIC;
  1040. sb->s_op = &btrfs_super_ops;
  1041. sb->s_d_op = &btrfs_dentry_operations;
  1042. sb->s_export_op = &btrfs_export_ops;
  1043. sb->s_xattr = btrfs_xattr_handlers;
  1044. sb->s_time_gran = 1;
  1045. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  1046. sb->s_flags |= MS_POSIXACL;
  1047. #endif
  1048. sb->s_flags |= MS_I_VERSION;
  1049. sb->s_iflags |= SB_I_CGROUPWB;
  1050. err = open_ctree(sb, fs_devices, (char *)data);
  1051. if (err) {
  1052. printk(KERN_ERR "BTRFS: open_ctree failed\n");
  1053. return err;
  1054. }
  1055. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  1056. key.type = BTRFS_INODE_ITEM_KEY;
  1057. key.offset = 0;
  1058. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  1059. if (IS_ERR(inode)) {
  1060. err = PTR_ERR(inode);
  1061. goto fail_close;
  1062. }
  1063. sb->s_root = d_make_root(inode);
  1064. if (!sb->s_root) {
  1065. err = -ENOMEM;
  1066. goto fail_close;
  1067. }
  1068. save_mount_options(sb, data);
  1069. cleancache_init_fs(sb);
  1070. sb->s_flags |= MS_ACTIVE;
  1071. return 0;
  1072. fail_close:
  1073. close_ctree(fs_info->tree_root);
  1074. return err;
  1075. }
  1076. int btrfs_sync_fs(struct super_block *sb, int wait)
  1077. {
  1078. struct btrfs_trans_handle *trans;
  1079. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1080. struct btrfs_root *root = fs_info->tree_root;
  1081. trace_btrfs_sync_fs(wait);
  1082. if (!wait) {
  1083. filemap_flush(fs_info->btree_inode->i_mapping);
  1084. return 0;
  1085. }
  1086. btrfs_wait_ordered_roots(fs_info, -1);
  1087. trans = btrfs_attach_transaction_barrier(root);
  1088. if (IS_ERR(trans)) {
  1089. /* no transaction, don't bother */
  1090. if (PTR_ERR(trans) == -ENOENT) {
  1091. /*
  1092. * Exit unless we have some pending changes
  1093. * that need to go through commit
  1094. */
  1095. if (fs_info->pending_changes == 0)
  1096. return 0;
  1097. /*
  1098. * A non-blocking test if the fs is frozen. We must not
  1099. * start a new transaction here otherwise a deadlock
  1100. * happens. The pending operations are delayed to the
  1101. * next commit after thawing.
  1102. */
  1103. if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
  1104. __sb_end_write(sb, SB_FREEZE_WRITE);
  1105. else
  1106. return 0;
  1107. trans = btrfs_start_transaction(root, 0);
  1108. }
  1109. if (IS_ERR(trans))
  1110. return PTR_ERR(trans);
  1111. }
  1112. return btrfs_commit_transaction(trans, root);
  1113. }
  1114. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  1115. {
  1116. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  1117. struct btrfs_root *root = info->tree_root;
  1118. char *compress_type;
  1119. if (btrfs_test_opt(root, DEGRADED))
  1120. seq_puts(seq, ",degraded");
  1121. if (btrfs_test_opt(root, NODATASUM))
  1122. seq_puts(seq, ",nodatasum");
  1123. if (btrfs_test_opt(root, NODATACOW))
  1124. seq_puts(seq, ",nodatacow");
  1125. if (btrfs_test_opt(root, NOBARRIER))
  1126. seq_puts(seq, ",nobarrier");
  1127. if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
  1128. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  1129. if (info->alloc_start != 0)
  1130. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  1131. if (info->thread_pool_size != min_t(unsigned long,
  1132. num_online_cpus() + 2, 8))
  1133. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  1134. if (btrfs_test_opt(root, COMPRESS)) {
  1135. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  1136. compress_type = "zlib";
  1137. else
  1138. compress_type = "lzo";
  1139. if (btrfs_test_opt(root, FORCE_COMPRESS))
  1140. seq_printf(seq, ",compress-force=%s", compress_type);
  1141. else
  1142. seq_printf(seq, ",compress=%s", compress_type);
  1143. }
  1144. if (btrfs_test_opt(root, NOSSD))
  1145. seq_puts(seq, ",nossd");
  1146. if (btrfs_test_opt(root, SSD_SPREAD))
  1147. seq_puts(seq, ",ssd_spread");
  1148. else if (btrfs_test_opt(root, SSD))
  1149. seq_puts(seq, ",ssd");
  1150. if (btrfs_test_opt(root, NOTREELOG))
  1151. seq_puts(seq, ",notreelog");
  1152. if (btrfs_test_opt(root, NOLOGREPLAY))
  1153. seq_puts(seq, ",nologreplay");
  1154. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  1155. seq_puts(seq, ",flushoncommit");
  1156. if (btrfs_test_opt(root, DISCARD))
  1157. seq_puts(seq, ",discard");
  1158. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  1159. seq_puts(seq, ",noacl");
  1160. if (btrfs_test_opt(root, SPACE_CACHE))
  1161. seq_puts(seq, ",space_cache");
  1162. else if (btrfs_test_opt(root, FREE_SPACE_TREE))
  1163. seq_puts(seq, ",space_cache=v2");
  1164. else
  1165. seq_puts(seq, ",nospace_cache");
  1166. if (btrfs_test_opt(root, RESCAN_UUID_TREE))
  1167. seq_puts(seq, ",rescan_uuid_tree");
  1168. if (btrfs_test_opt(root, CLEAR_CACHE))
  1169. seq_puts(seq, ",clear_cache");
  1170. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1171. seq_puts(seq, ",user_subvol_rm_allowed");
  1172. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  1173. seq_puts(seq, ",enospc_debug");
  1174. if (btrfs_test_opt(root, AUTO_DEFRAG))
  1175. seq_puts(seq, ",autodefrag");
  1176. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  1177. seq_puts(seq, ",inode_cache");
  1178. if (btrfs_test_opt(root, SKIP_BALANCE))
  1179. seq_puts(seq, ",skip_balance");
  1180. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1181. if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
  1182. seq_puts(seq, ",check_int_data");
  1183. else if (btrfs_test_opt(root, CHECK_INTEGRITY))
  1184. seq_puts(seq, ",check_int");
  1185. if (info->check_integrity_print_mask)
  1186. seq_printf(seq, ",check_int_print_mask=%d",
  1187. info->check_integrity_print_mask);
  1188. #endif
  1189. if (info->metadata_ratio)
  1190. seq_printf(seq, ",metadata_ratio=%d",
  1191. info->metadata_ratio);
  1192. if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
  1193. seq_puts(seq, ",fatal_errors=panic");
  1194. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  1195. seq_printf(seq, ",commit=%d", info->commit_interval);
  1196. #ifdef CONFIG_BTRFS_DEBUG
  1197. if (btrfs_test_opt(root, FRAGMENT_DATA))
  1198. seq_puts(seq, ",fragment=data");
  1199. if (btrfs_test_opt(root, FRAGMENT_METADATA))
  1200. seq_puts(seq, ",fragment=metadata");
  1201. #endif
  1202. seq_printf(seq, ",subvolid=%llu",
  1203. BTRFS_I(d_inode(dentry))->root->root_key.objectid);
  1204. seq_puts(seq, ",subvol=");
  1205. seq_dentry(seq, dentry, " \t\n\\");
  1206. return 0;
  1207. }
  1208. static int btrfs_test_super(struct super_block *s, void *data)
  1209. {
  1210. struct btrfs_fs_info *p = data;
  1211. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1212. return fs_info->fs_devices == p->fs_devices;
  1213. }
  1214. static int btrfs_set_super(struct super_block *s, void *data)
  1215. {
  1216. int err = set_anon_super(s, data);
  1217. if (!err)
  1218. s->s_fs_info = data;
  1219. return err;
  1220. }
  1221. /*
  1222. * subvolumes are identified by ino 256
  1223. */
  1224. static inline int is_subvolume_inode(struct inode *inode)
  1225. {
  1226. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  1227. return 1;
  1228. return 0;
  1229. }
  1230. /*
  1231. * This will add subvolid=0 to the argument string while removing any subvol=
  1232. * and subvolid= arguments to make sure we get the top-level root for path
  1233. * walking to the subvol we want.
  1234. */
  1235. static char *setup_root_args(char *args)
  1236. {
  1237. char *buf, *dst, *sep;
  1238. if (!args)
  1239. return kstrdup("subvolid=0", GFP_NOFS);
  1240. /* The worst case is that we add ",subvolid=0" to the end. */
  1241. buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
  1242. if (!buf)
  1243. return NULL;
  1244. while (1) {
  1245. sep = strchrnul(args, ',');
  1246. if (!strstarts(args, "subvol=") &&
  1247. !strstarts(args, "subvolid=")) {
  1248. memcpy(dst, args, sep - args);
  1249. dst += sep - args;
  1250. *dst++ = ',';
  1251. }
  1252. if (*sep)
  1253. args = sep + 1;
  1254. else
  1255. break;
  1256. }
  1257. strcpy(dst, "subvolid=0");
  1258. return buf;
  1259. }
  1260. static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
  1261. int flags, const char *device_name,
  1262. char *data)
  1263. {
  1264. struct dentry *root;
  1265. struct vfsmount *mnt = NULL;
  1266. char *newargs;
  1267. int ret;
  1268. newargs = setup_root_args(data);
  1269. if (!newargs) {
  1270. root = ERR_PTR(-ENOMEM);
  1271. goto out;
  1272. }
  1273. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
  1274. if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
  1275. if (flags & MS_RDONLY) {
  1276. mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
  1277. device_name, newargs);
  1278. } else {
  1279. mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
  1280. device_name, newargs);
  1281. if (IS_ERR(mnt)) {
  1282. root = ERR_CAST(mnt);
  1283. mnt = NULL;
  1284. goto out;
  1285. }
  1286. down_write(&mnt->mnt_sb->s_umount);
  1287. ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
  1288. up_write(&mnt->mnt_sb->s_umount);
  1289. if (ret < 0) {
  1290. root = ERR_PTR(ret);
  1291. goto out;
  1292. }
  1293. }
  1294. }
  1295. if (IS_ERR(mnt)) {
  1296. root = ERR_CAST(mnt);
  1297. mnt = NULL;
  1298. goto out;
  1299. }
  1300. if (!subvol_name) {
  1301. if (!subvol_objectid) {
  1302. ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
  1303. &subvol_objectid);
  1304. if (ret) {
  1305. root = ERR_PTR(ret);
  1306. goto out;
  1307. }
  1308. }
  1309. subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
  1310. subvol_objectid);
  1311. if (IS_ERR(subvol_name)) {
  1312. root = ERR_CAST(subvol_name);
  1313. subvol_name = NULL;
  1314. goto out;
  1315. }
  1316. }
  1317. root = mount_subtree(mnt, subvol_name);
  1318. /* mount_subtree() drops our reference on the vfsmount. */
  1319. mnt = NULL;
  1320. if (!IS_ERR(root)) {
  1321. struct super_block *s = root->d_sb;
  1322. struct inode *root_inode = d_inode(root);
  1323. u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
  1324. ret = 0;
  1325. if (!is_subvolume_inode(root_inode)) {
  1326. pr_err("BTRFS: '%s' is not a valid subvolume\n",
  1327. subvol_name);
  1328. ret = -EINVAL;
  1329. }
  1330. if (subvol_objectid && root_objectid != subvol_objectid) {
  1331. /*
  1332. * This will also catch a race condition where a
  1333. * subvolume which was passed by ID is renamed and
  1334. * another subvolume is renamed over the old location.
  1335. */
  1336. pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n",
  1337. subvol_name, subvol_objectid);
  1338. ret = -EINVAL;
  1339. }
  1340. if (ret) {
  1341. dput(root);
  1342. root = ERR_PTR(ret);
  1343. deactivate_locked_super(s);
  1344. }
  1345. }
  1346. out:
  1347. mntput(mnt);
  1348. kfree(newargs);
  1349. kfree(subvol_name);
  1350. return root;
  1351. }
  1352. static int parse_security_options(char *orig_opts,
  1353. struct security_mnt_opts *sec_opts)
  1354. {
  1355. char *secdata = NULL;
  1356. int ret = 0;
  1357. secdata = alloc_secdata();
  1358. if (!secdata)
  1359. return -ENOMEM;
  1360. ret = security_sb_copy_data(orig_opts, secdata);
  1361. if (ret) {
  1362. free_secdata(secdata);
  1363. return ret;
  1364. }
  1365. ret = security_sb_parse_opts_str(secdata, sec_opts);
  1366. free_secdata(secdata);
  1367. return ret;
  1368. }
  1369. static int setup_security_options(struct btrfs_fs_info *fs_info,
  1370. struct super_block *sb,
  1371. struct security_mnt_opts *sec_opts)
  1372. {
  1373. int ret = 0;
  1374. /*
  1375. * Call security_sb_set_mnt_opts() to check whether new sec_opts
  1376. * is valid.
  1377. */
  1378. ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
  1379. if (ret)
  1380. return ret;
  1381. #ifdef CONFIG_SECURITY
  1382. if (!fs_info->security_opts.num_mnt_opts) {
  1383. /* first time security setup, copy sec_opts to fs_info */
  1384. memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
  1385. } else {
  1386. /*
  1387. * Since SELinux(the only one supports security_mnt_opts) does
  1388. * NOT support changing context during remount/mount same sb,
  1389. * This must be the same or part of the same security options,
  1390. * just free it.
  1391. */
  1392. security_free_mnt_opts(sec_opts);
  1393. }
  1394. #endif
  1395. return ret;
  1396. }
  1397. /*
  1398. * Find a superblock for the given device / mount point.
  1399. *
  1400. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  1401. * for multiple device setup. Make sure to keep it in sync.
  1402. */
  1403. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  1404. const char *device_name, void *data)
  1405. {
  1406. struct block_device *bdev = NULL;
  1407. struct super_block *s;
  1408. struct btrfs_fs_devices *fs_devices = NULL;
  1409. struct btrfs_fs_info *fs_info = NULL;
  1410. struct security_mnt_opts new_sec_opts;
  1411. fmode_t mode = FMODE_READ;
  1412. char *subvol_name = NULL;
  1413. u64 subvol_objectid = 0;
  1414. int error = 0;
  1415. if (!(flags & MS_RDONLY))
  1416. mode |= FMODE_WRITE;
  1417. error = btrfs_parse_early_options(data, mode, fs_type,
  1418. &subvol_name, &subvol_objectid,
  1419. &fs_devices);
  1420. if (error) {
  1421. kfree(subvol_name);
  1422. return ERR_PTR(error);
  1423. }
  1424. if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  1425. /* mount_subvol() will free subvol_name. */
  1426. return mount_subvol(subvol_name, subvol_objectid, flags,
  1427. device_name, data);
  1428. }
  1429. security_init_mnt_opts(&new_sec_opts);
  1430. if (data) {
  1431. error = parse_security_options(data, &new_sec_opts);
  1432. if (error)
  1433. return ERR_PTR(error);
  1434. }
  1435. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  1436. if (error)
  1437. goto error_sec_opts;
  1438. /*
  1439. * Setup a dummy root and fs_info for test/set super. This is because
  1440. * we don't actually fill this stuff out until open_ctree, but we need
  1441. * it for searching for existing supers, so this lets us do that and
  1442. * then open_ctree will properly initialize everything later.
  1443. */
  1444. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  1445. if (!fs_info) {
  1446. error = -ENOMEM;
  1447. goto error_sec_opts;
  1448. }
  1449. fs_info->fs_devices = fs_devices;
  1450. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1451. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1452. security_init_mnt_opts(&fs_info->security_opts);
  1453. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1454. error = -ENOMEM;
  1455. goto error_fs_info;
  1456. }
  1457. error = btrfs_open_devices(fs_devices, mode, fs_type);
  1458. if (error)
  1459. goto error_fs_info;
  1460. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  1461. error = -EACCES;
  1462. goto error_close_devices;
  1463. }
  1464. bdev = fs_devices->latest_bdev;
  1465. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  1466. fs_info);
  1467. if (IS_ERR(s)) {
  1468. error = PTR_ERR(s);
  1469. goto error_close_devices;
  1470. }
  1471. if (s->s_root) {
  1472. btrfs_close_devices(fs_devices);
  1473. free_fs_info(fs_info);
  1474. if ((flags ^ s->s_flags) & MS_RDONLY)
  1475. error = -EBUSY;
  1476. } else {
  1477. snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
  1478. btrfs_sb(s)->bdev_holder = fs_type;
  1479. error = btrfs_fill_super(s, fs_devices, data,
  1480. flags & MS_SILENT ? 1 : 0);
  1481. }
  1482. if (error) {
  1483. deactivate_locked_super(s);
  1484. goto error_sec_opts;
  1485. }
  1486. fs_info = btrfs_sb(s);
  1487. error = setup_security_options(fs_info, s, &new_sec_opts);
  1488. if (error) {
  1489. deactivate_locked_super(s);
  1490. goto error_sec_opts;
  1491. }
  1492. return dget(s->s_root);
  1493. error_close_devices:
  1494. btrfs_close_devices(fs_devices);
  1495. error_fs_info:
  1496. free_fs_info(fs_info);
  1497. error_sec_opts:
  1498. security_free_mnt_opts(&new_sec_opts);
  1499. return ERR_PTR(error);
  1500. }
  1501. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1502. int new_pool_size, int old_pool_size)
  1503. {
  1504. if (new_pool_size == old_pool_size)
  1505. return;
  1506. fs_info->thread_pool_size = new_pool_size;
  1507. btrfs_info(fs_info, "resize thread pool %d -> %d",
  1508. old_pool_size, new_pool_size);
  1509. btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
  1510. btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
  1511. btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
  1512. btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
  1513. btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
  1514. btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
  1515. btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
  1516. new_pool_size);
  1517. btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  1518. btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
  1519. btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
  1520. btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
  1521. btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
  1522. new_pool_size);
  1523. }
  1524. static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
  1525. {
  1526. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1527. }
  1528. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1529. unsigned long old_opts, int flags)
  1530. {
  1531. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1532. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1533. (flags & MS_RDONLY))) {
  1534. /* wait for any defraggers to finish */
  1535. wait_event(fs_info->transaction_wait,
  1536. (atomic_read(&fs_info->defrag_running) == 0));
  1537. if (flags & MS_RDONLY)
  1538. sync_filesystem(fs_info->sb);
  1539. }
  1540. }
  1541. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1542. unsigned long old_opts)
  1543. {
  1544. /*
  1545. * We need cleanup all defragable inodes if the autodefragment is
  1546. * close or the fs is R/O.
  1547. */
  1548. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1549. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1550. (fs_info->sb->s_flags & MS_RDONLY))) {
  1551. btrfs_cleanup_defrag_inodes(fs_info);
  1552. }
  1553. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1554. }
  1555. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1556. {
  1557. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1558. struct btrfs_root *root = fs_info->tree_root;
  1559. unsigned old_flags = sb->s_flags;
  1560. unsigned long old_opts = fs_info->mount_opt;
  1561. unsigned long old_compress_type = fs_info->compress_type;
  1562. u64 old_max_inline = fs_info->max_inline;
  1563. u64 old_alloc_start = fs_info->alloc_start;
  1564. int old_thread_pool_size = fs_info->thread_pool_size;
  1565. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1566. int ret;
  1567. sync_filesystem(sb);
  1568. btrfs_remount_prepare(fs_info);
  1569. if (data) {
  1570. struct security_mnt_opts new_sec_opts;
  1571. security_init_mnt_opts(&new_sec_opts);
  1572. ret = parse_security_options(data, &new_sec_opts);
  1573. if (ret)
  1574. goto restore;
  1575. ret = setup_security_options(fs_info, sb,
  1576. &new_sec_opts);
  1577. if (ret) {
  1578. security_free_mnt_opts(&new_sec_opts);
  1579. goto restore;
  1580. }
  1581. }
  1582. ret = btrfs_parse_options(root, data, *flags);
  1583. if (ret) {
  1584. ret = -EINVAL;
  1585. goto restore;
  1586. }
  1587. btrfs_remount_begin(fs_info, old_opts, *flags);
  1588. btrfs_resize_thread_pool(fs_info,
  1589. fs_info->thread_pool_size, old_thread_pool_size);
  1590. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1591. goto out;
  1592. if (*flags & MS_RDONLY) {
  1593. /*
  1594. * this also happens on 'umount -rf' or on shutdown, when
  1595. * the filesystem is busy.
  1596. */
  1597. cancel_work_sync(&fs_info->async_reclaim_work);
  1598. /* wait for the uuid_scan task to finish */
  1599. down(&fs_info->uuid_tree_rescan_sem);
  1600. /* avoid complains from lockdep et al. */
  1601. up(&fs_info->uuid_tree_rescan_sem);
  1602. sb->s_flags |= MS_RDONLY;
  1603. /*
  1604. * Setting MS_RDONLY will put the cleaner thread to
  1605. * sleep at the next loop if it's already active.
  1606. * If it's already asleep, we'll leave unused block
  1607. * groups on disk until we're mounted read-write again
  1608. * unless we clean them up here.
  1609. */
  1610. btrfs_delete_unused_bgs(fs_info);
  1611. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1612. btrfs_scrub_cancel(fs_info);
  1613. btrfs_pause_balance(fs_info);
  1614. ret = btrfs_commit_super(root);
  1615. if (ret)
  1616. goto restore;
  1617. } else {
  1618. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  1619. btrfs_err(fs_info,
  1620. "Remounting read-write after error is not allowed");
  1621. ret = -EINVAL;
  1622. goto restore;
  1623. }
  1624. if (fs_info->fs_devices->rw_devices == 0) {
  1625. ret = -EACCES;
  1626. goto restore;
  1627. }
  1628. if (fs_info->fs_devices->missing_devices >
  1629. fs_info->num_tolerated_disk_barrier_failures &&
  1630. !(*flags & MS_RDONLY)) {
  1631. btrfs_warn(fs_info,
  1632. "too many missing devices, writeable remount is not allowed");
  1633. ret = -EACCES;
  1634. goto restore;
  1635. }
  1636. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1637. ret = -EINVAL;
  1638. goto restore;
  1639. }
  1640. ret = btrfs_cleanup_fs_roots(fs_info);
  1641. if (ret)
  1642. goto restore;
  1643. /* recover relocation */
  1644. mutex_lock(&fs_info->cleaner_mutex);
  1645. ret = btrfs_recover_relocation(root);
  1646. mutex_unlock(&fs_info->cleaner_mutex);
  1647. if (ret)
  1648. goto restore;
  1649. ret = btrfs_resume_balance_async(fs_info);
  1650. if (ret)
  1651. goto restore;
  1652. ret = btrfs_resume_dev_replace_async(fs_info);
  1653. if (ret) {
  1654. btrfs_warn(fs_info, "failed to resume dev_replace");
  1655. goto restore;
  1656. }
  1657. if (!fs_info->uuid_root) {
  1658. btrfs_info(fs_info, "creating UUID tree");
  1659. ret = btrfs_create_uuid_tree(fs_info);
  1660. if (ret) {
  1661. btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
  1662. goto restore;
  1663. }
  1664. }
  1665. sb->s_flags &= ~MS_RDONLY;
  1666. }
  1667. out:
  1668. wake_up_process(fs_info->transaction_kthread);
  1669. btrfs_remount_cleanup(fs_info, old_opts);
  1670. return 0;
  1671. restore:
  1672. /* We've hit an error - don't reset MS_RDONLY */
  1673. if (sb->s_flags & MS_RDONLY)
  1674. old_flags |= MS_RDONLY;
  1675. sb->s_flags = old_flags;
  1676. fs_info->mount_opt = old_opts;
  1677. fs_info->compress_type = old_compress_type;
  1678. fs_info->max_inline = old_max_inline;
  1679. mutex_lock(&fs_info->chunk_mutex);
  1680. fs_info->alloc_start = old_alloc_start;
  1681. mutex_unlock(&fs_info->chunk_mutex);
  1682. btrfs_resize_thread_pool(fs_info,
  1683. old_thread_pool_size, fs_info->thread_pool_size);
  1684. fs_info->metadata_ratio = old_metadata_ratio;
  1685. btrfs_remount_cleanup(fs_info, old_opts);
  1686. return ret;
  1687. }
  1688. /* Used to sort the devices by max_avail(descending sort) */
  1689. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1690. const void *dev_info2)
  1691. {
  1692. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1693. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1694. return -1;
  1695. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1696. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1697. return 1;
  1698. else
  1699. return 0;
  1700. }
  1701. /*
  1702. * sort the devices by max_avail, in which max free extent size of each device
  1703. * is stored.(Descending Sort)
  1704. */
  1705. static inline void btrfs_descending_sort_devices(
  1706. struct btrfs_device_info *devices,
  1707. size_t nr_devices)
  1708. {
  1709. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1710. btrfs_cmp_device_free_bytes, NULL);
  1711. }
  1712. /*
  1713. * The helper to calc the free space on the devices that can be used to store
  1714. * file data.
  1715. */
  1716. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  1717. {
  1718. struct btrfs_fs_info *fs_info = root->fs_info;
  1719. struct btrfs_device_info *devices_info;
  1720. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1721. struct btrfs_device *device;
  1722. u64 skip_space;
  1723. u64 type;
  1724. u64 avail_space;
  1725. u64 used_space;
  1726. u64 min_stripe_size;
  1727. int min_stripes = 1, num_stripes = 1;
  1728. int i = 0, nr_devices;
  1729. int ret;
  1730. /*
  1731. * We aren't under the device list lock, so this is racey-ish, but good
  1732. * enough for our purposes.
  1733. */
  1734. nr_devices = fs_info->fs_devices->open_devices;
  1735. if (!nr_devices) {
  1736. smp_mb();
  1737. nr_devices = fs_info->fs_devices->open_devices;
  1738. ASSERT(nr_devices);
  1739. if (!nr_devices) {
  1740. *free_bytes = 0;
  1741. return 0;
  1742. }
  1743. }
  1744. devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
  1745. GFP_NOFS);
  1746. if (!devices_info)
  1747. return -ENOMEM;
  1748. /* calc min stripe number for data space alloction */
  1749. type = btrfs_get_alloc_profile(root, 1);
  1750. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1751. min_stripes = 2;
  1752. num_stripes = nr_devices;
  1753. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1754. min_stripes = 2;
  1755. num_stripes = 2;
  1756. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1757. min_stripes = 4;
  1758. num_stripes = 4;
  1759. }
  1760. if (type & BTRFS_BLOCK_GROUP_DUP)
  1761. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1762. else
  1763. min_stripe_size = BTRFS_STRIPE_LEN;
  1764. if (fs_info->alloc_start)
  1765. mutex_lock(&fs_devices->device_list_mutex);
  1766. rcu_read_lock();
  1767. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  1768. if (!device->in_fs_metadata || !device->bdev ||
  1769. device->is_tgtdev_for_dev_replace)
  1770. continue;
  1771. if (i >= nr_devices)
  1772. break;
  1773. avail_space = device->total_bytes - device->bytes_used;
  1774. /* align with stripe_len */
  1775. avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
  1776. avail_space *= BTRFS_STRIPE_LEN;
  1777. /*
  1778. * In order to avoid overwritting the superblock on the drive,
  1779. * btrfs starts at an offset of at least 1MB when doing chunk
  1780. * allocation.
  1781. */
  1782. skip_space = SZ_1M;
  1783. /* user can set the offset in fs_info->alloc_start. */
  1784. if (fs_info->alloc_start &&
  1785. fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1786. device->total_bytes) {
  1787. rcu_read_unlock();
  1788. skip_space = max(fs_info->alloc_start, skip_space);
  1789. /*
  1790. * btrfs can not use the free space in
  1791. * [0, skip_space - 1], we must subtract it from the
  1792. * total. In order to implement it, we account the used
  1793. * space in this range first.
  1794. */
  1795. ret = btrfs_account_dev_extents_size(device, 0,
  1796. skip_space - 1,
  1797. &used_space);
  1798. if (ret) {
  1799. kfree(devices_info);
  1800. mutex_unlock(&fs_devices->device_list_mutex);
  1801. return ret;
  1802. }
  1803. rcu_read_lock();
  1804. /* calc the free space in [0, skip_space - 1] */
  1805. skip_space -= used_space;
  1806. }
  1807. /*
  1808. * we can use the free space in [0, skip_space - 1], subtract
  1809. * it from the total.
  1810. */
  1811. if (avail_space && avail_space >= skip_space)
  1812. avail_space -= skip_space;
  1813. else
  1814. avail_space = 0;
  1815. if (avail_space < min_stripe_size)
  1816. continue;
  1817. devices_info[i].dev = device;
  1818. devices_info[i].max_avail = avail_space;
  1819. i++;
  1820. }
  1821. rcu_read_unlock();
  1822. if (fs_info->alloc_start)
  1823. mutex_unlock(&fs_devices->device_list_mutex);
  1824. nr_devices = i;
  1825. btrfs_descending_sort_devices(devices_info, nr_devices);
  1826. i = nr_devices - 1;
  1827. avail_space = 0;
  1828. while (nr_devices >= min_stripes) {
  1829. if (num_stripes > nr_devices)
  1830. num_stripes = nr_devices;
  1831. if (devices_info[i].max_avail >= min_stripe_size) {
  1832. int j;
  1833. u64 alloc_size;
  1834. avail_space += devices_info[i].max_avail * num_stripes;
  1835. alloc_size = devices_info[i].max_avail;
  1836. for (j = i + 1 - num_stripes; j <= i; j++)
  1837. devices_info[j].max_avail -= alloc_size;
  1838. }
  1839. i--;
  1840. nr_devices--;
  1841. }
  1842. kfree(devices_info);
  1843. *free_bytes = avail_space;
  1844. return 0;
  1845. }
  1846. /*
  1847. * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
  1848. *
  1849. * If there's a redundant raid level at DATA block groups, use the respective
  1850. * multiplier to scale the sizes.
  1851. *
  1852. * Unused device space usage is based on simulating the chunk allocator
  1853. * algorithm that respects the device sizes, order of allocations and the
  1854. * 'alloc_start' value, this is a close approximation of the actual use but
  1855. * there are other factors that may change the result (like a new metadata
  1856. * chunk).
  1857. *
  1858. * If metadata is exhausted, f_bavail will be 0.
  1859. *
  1860. * FIXME: not accurate for mixed block groups, total and free/used are ok,
  1861. * available appears slightly larger.
  1862. */
  1863. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1864. {
  1865. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1866. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1867. struct list_head *head = &fs_info->space_info;
  1868. struct btrfs_space_info *found;
  1869. u64 total_used = 0;
  1870. u64 total_free_data = 0;
  1871. u64 total_free_meta = 0;
  1872. int bits = dentry->d_sb->s_blocksize_bits;
  1873. __be32 *fsid = (__be32 *)fs_info->fsid;
  1874. unsigned factor = 1;
  1875. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  1876. int ret;
  1877. u64 thresh = 0;
  1878. /*
  1879. * holding chunk_muext to avoid allocating new chunks, holding
  1880. * device_list_mutex to avoid the device being removed
  1881. */
  1882. rcu_read_lock();
  1883. list_for_each_entry_rcu(found, head, list) {
  1884. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1885. int i;
  1886. total_free_data += found->disk_total - found->disk_used;
  1887. total_free_data -=
  1888. btrfs_account_ro_block_groups_free_space(found);
  1889. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1890. if (!list_empty(&found->block_groups[i])) {
  1891. switch (i) {
  1892. case BTRFS_RAID_DUP:
  1893. case BTRFS_RAID_RAID1:
  1894. case BTRFS_RAID_RAID10:
  1895. factor = 2;
  1896. }
  1897. }
  1898. }
  1899. }
  1900. if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
  1901. total_free_meta += found->disk_total - found->disk_used;
  1902. total_used += found->disk_used;
  1903. }
  1904. rcu_read_unlock();
  1905. buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  1906. buf->f_blocks >>= bits;
  1907. buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  1908. /* Account global block reserve as used, it's in logical size already */
  1909. spin_lock(&block_rsv->lock);
  1910. buf->f_bfree -= block_rsv->size >> bits;
  1911. spin_unlock(&block_rsv->lock);
  1912. buf->f_bavail = div_u64(total_free_data, factor);
  1913. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1914. if (ret)
  1915. return ret;
  1916. buf->f_bavail += div_u64(total_free_data, factor);
  1917. buf->f_bavail = buf->f_bavail >> bits;
  1918. /*
  1919. * We calculate the remaining metadata space minus global reserve. If
  1920. * this is (supposedly) smaller than zero, there's no space. But this
  1921. * does not hold in practice, the exhausted state happens where's still
  1922. * some positive delta. So we apply some guesswork and compare the
  1923. * delta to a 4M threshold. (Practically observed delta was ~2M.)
  1924. *
  1925. * We probably cannot calculate the exact threshold value because this
  1926. * depends on the internal reservations requested by various
  1927. * operations, so some operations that consume a few metadata will
  1928. * succeed even if the Avail is zero. But this is better than the other
  1929. * way around.
  1930. */
  1931. thresh = 4 * 1024 * 1024;
  1932. if (total_free_meta - thresh < block_rsv->size)
  1933. buf->f_bavail = 0;
  1934. buf->f_type = BTRFS_SUPER_MAGIC;
  1935. buf->f_bsize = dentry->d_sb->s_blocksize;
  1936. buf->f_namelen = BTRFS_NAME_LEN;
  1937. /* We treat it as constant endianness (it doesn't matter _which_)
  1938. because we want the fsid to come out the same whether mounted
  1939. on a big-endian or little-endian host */
  1940. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1941. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1942. /* Mask in the root object ID too, to disambiguate subvols */
  1943. buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
  1944. buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
  1945. return 0;
  1946. }
  1947. static void btrfs_kill_super(struct super_block *sb)
  1948. {
  1949. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1950. kill_anon_super(sb);
  1951. free_fs_info(fs_info);
  1952. }
  1953. static struct file_system_type btrfs_fs_type = {
  1954. .owner = THIS_MODULE,
  1955. .name = "btrfs",
  1956. .mount = btrfs_mount,
  1957. .kill_sb = btrfs_kill_super,
  1958. .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
  1959. };
  1960. MODULE_ALIAS_FS("btrfs");
  1961. static int btrfs_control_open(struct inode *inode, struct file *file)
  1962. {
  1963. /*
  1964. * The control file's private_data is used to hold the
  1965. * transaction when it is started and is used to keep
  1966. * track of whether a transaction is already in progress.
  1967. */
  1968. file->private_data = NULL;
  1969. return 0;
  1970. }
  1971. /*
  1972. * used by btrfsctl to scan devices when no FS is mounted
  1973. */
  1974. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1975. unsigned long arg)
  1976. {
  1977. struct btrfs_ioctl_vol_args *vol;
  1978. struct btrfs_fs_devices *fs_devices;
  1979. int ret = -ENOTTY;
  1980. if (!capable(CAP_SYS_ADMIN))
  1981. return -EPERM;
  1982. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1983. if (IS_ERR(vol))
  1984. return PTR_ERR(vol);
  1985. switch (cmd) {
  1986. case BTRFS_IOC_SCAN_DEV:
  1987. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1988. &btrfs_fs_type, &fs_devices);
  1989. break;
  1990. case BTRFS_IOC_DEVICES_READY:
  1991. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1992. &btrfs_fs_type, &fs_devices);
  1993. if (ret)
  1994. break;
  1995. ret = !(fs_devices->num_devices == fs_devices->total_devices);
  1996. break;
  1997. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  1998. ret = btrfs_ioctl_get_supported_features((void __user*)arg);
  1999. break;
  2000. }
  2001. kfree(vol);
  2002. return ret;
  2003. }
  2004. static int btrfs_freeze(struct super_block *sb)
  2005. {
  2006. struct btrfs_trans_handle *trans;
  2007. struct btrfs_root *root = btrfs_sb(sb)->tree_root;
  2008. trans = btrfs_attach_transaction_barrier(root);
  2009. if (IS_ERR(trans)) {
  2010. /* no transaction, don't bother */
  2011. if (PTR_ERR(trans) == -ENOENT)
  2012. return 0;
  2013. return PTR_ERR(trans);
  2014. }
  2015. return btrfs_commit_transaction(trans, root);
  2016. }
  2017. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  2018. {
  2019. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  2020. struct btrfs_fs_devices *cur_devices;
  2021. struct btrfs_device *dev, *first_dev = NULL;
  2022. struct list_head *head;
  2023. struct rcu_string *name;
  2024. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2025. cur_devices = fs_info->fs_devices;
  2026. while (cur_devices) {
  2027. head = &cur_devices->devices;
  2028. list_for_each_entry(dev, head, dev_list) {
  2029. if (dev->missing)
  2030. continue;
  2031. if (!dev->name)
  2032. continue;
  2033. if (!first_dev || dev->devid < first_dev->devid)
  2034. first_dev = dev;
  2035. }
  2036. cur_devices = cur_devices->seed;
  2037. }
  2038. if (first_dev) {
  2039. rcu_read_lock();
  2040. name = rcu_dereference(first_dev->name);
  2041. seq_escape(m, name->str, " \t\n\\");
  2042. rcu_read_unlock();
  2043. } else {
  2044. WARN_ON(1);
  2045. }
  2046. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2047. return 0;
  2048. }
  2049. static const struct super_operations btrfs_super_ops = {
  2050. .drop_inode = btrfs_drop_inode,
  2051. .evict_inode = btrfs_evict_inode,
  2052. .put_super = btrfs_put_super,
  2053. .sync_fs = btrfs_sync_fs,
  2054. .show_options = btrfs_show_options,
  2055. .show_devname = btrfs_show_devname,
  2056. .write_inode = btrfs_write_inode,
  2057. .alloc_inode = btrfs_alloc_inode,
  2058. .destroy_inode = btrfs_destroy_inode,
  2059. .statfs = btrfs_statfs,
  2060. .remount_fs = btrfs_remount,
  2061. .freeze_fs = btrfs_freeze,
  2062. };
  2063. static const struct file_operations btrfs_ctl_fops = {
  2064. .open = btrfs_control_open,
  2065. .unlocked_ioctl = btrfs_control_ioctl,
  2066. .compat_ioctl = btrfs_control_ioctl,
  2067. .owner = THIS_MODULE,
  2068. .llseek = noop_llseek,
  2069. };
  2070. static struct miscdevice btrfs_misc = {
  2071. .minor = BTRFS_MINOR,
  2072. .name = "btrfs-control",
  2073. .fops = &btrfs_ctl_fops
  2074. };
  2075. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  2076. MODULE_ALIAS("devname:btrfs-control");
  2077. static int btrfs_interface_init(void)
  2078. {
  2079. return misc_register(&btrfs_misc);
  2080. }
  2081. static void btrfs_interface_exit(void)
  2082. {
  2083. misc_deregister(&btrfs_misc);
  2084. }
  2085. static void btrfs_print_info(void)
  2086. {
  2087. printk(KERN_INFO "Btrfs loaded"
  2088. #ifdef CONFIG_BTRFS_DEBUG
  2089. ", debug=on"
  2090. #endif
  2091. #ifdef CONFIG_BTRFS_ASSERT
  2092. ", assert=on"
  2093. #endif
  2094. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2095. ", integrity-checker=on"
  2096. #endif
  2097. "\n");
  2098. }
  2099. static int btrfs_run_sanity_tests(void)
  2100. {
  2101. int ret;
  2102. ret = btrfs_init_test_fs();
  2103. if (ret)
  2104. return ret;
  2105. ret = btrfs_test_free_space_cache();
  2106. if (ret)
  2107. goto out;
  2108. ret = btrfs_test_extent_buffer_operations();
  2109. if (ret)
  2110. goto out;
  2111. ret = btrfs_test_extent_io();
  2112. if (ret)
  2113. goto out;
  2114. ret = btrfs_test_inodes();
  2115. if (ret)
  2116. goto out;
  2117. ret = btrfs_test_qgroups();
  2118. if (ret)
  2119. goto out;
  2120. ret = btrfs_test_free_space_tree();
  2121. out:
  2122. btrfs_destroy_test_fs();
  2123. return ret;
  2124. }
  2125. static int __init init_btrfs_fs(void)
  2126. {
  2127. int err;
  2128. err = btrfs_hash_init();
  2129. if (err)
  2130. return err;
  2131. btrfs_props_init();
  2132. err = btrfs_init_sysfs();
  2133. if (err)
  2134. goto free_hash;
  2135. btrfs_init_compress();
  2136. err = btrfs_init_cachep();
  2137. if (err)
  2138. goto free_compress;
  2139. err = extent_io_init();
  2140. if (err)
  2141. goto free_cachep;
  2142. err = extent_map_init();
  2143. if (err)
  2144. goto free_extent_io;
  2145. err = ordered_data_init();
  2146. if (err)
  2147. goto free_extent_map;
  2148. err = btrfs_delayed_inode_init();
  2149. if (err)
  2150. goto free_ordered_data;
  2151. err = btrfs_auto_defrag_init();
  2152. if (err)
  2153. goto free_delayed_inode;
  2154. err = btrfs_delayed_ref_init();
  2155. if (err)
  2156. goto free_auto_defrag;
  2157. err = btrfs_prelim_ref_init();
  2158. if (err)
  2159. goto free_delayed_ref;
  2160. err = btrfs_end_io_wq_init();
  2161. if (err)
  2162. goto free_prelim_ref;
  2163. err = btrfs_interface_init();
  2164. if (err)
  2165. goto free_end_io_wq;
  2166. btrfs_init_lockdep();
  2167. btrfs_print_info();
  2168. err = btrfs_run_sanity_tests();
  2169. if (err)
  2170. goto unregister_ioctl;
  2171. err = register_filesystem(&btrfs_fs_type);
  2172. if (err)
  2173. goto unregister_ioctl;
  2174. return 0;
  2175. unregister_ioctl:
  2176. btrfs_interface_exit();
  2177. free_end_io_wq:
  2178. btrfs_end_io_wq_exit();
  2179. free_prelim_ref:
  2180. btrfs_prelim_ref_exit();
  2181. free_delayed_ref:
  2182. btrfs_delayed_ref_exit();
  2183. free_auto_defrag:
  2184. btrfs_auto_defrag_exit();
  2185. free_delayed_inode:
  2186. btrfs_delayed_inode_exit();
  2187. free_ordered_data:
  2188. ordered_data_exit();
  2189. free_extent_map:
  2190. extent_map_exit();
  2191. free_extent_io:
  2192. extent_io_exit();
  2193. free_cachep:
  2194. btrfs_destroy_cachep();
  2195. free_compress:
  2196. btrfs_exit_compress();
  2197. btrfs_exit_sysfs();
  2198. free_hash:
  2199. btrfs_hash_exit();
  2200. return err;
  2201. }
  2202. static void __exit exit_btrfs_fs(void)
  2203. {
  2204. btrfs_destroy_cachep();
  2205. btrfs_delayed_ref_exit();
  2206. btrfs_auto_defrag_exit();
  2207. btrfs_delayed_inode_exit();
  2208. btrfs_prelim_ref_exit();
  2209. ordered_data_exit();
  2210. extent_map_exit();
  2211. extent_io_exit();
  2212. btrfs_interface_exit();
  2213. btrfs_end_io_wq_exit();
  2214. unregister_filesystem(&btrfs_fs_type);
  2215. btrfs_exit_sysfs();
  2216. btrfs_cleanup_fs_uuids();
  2217. btrfs_exit_compress();
  2218. btrfs_hash_exit();
  2219. }
  2220. late_initcall(init_btrfs_fs);
  2221. module_exit(exit_btrfs_fs)
  2222. MODULE_LICENSE("GPL");