super.c 65 KB

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