super.c 63 KB

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