super.c 64 KB

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