super.c 64 KB

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