super.c 65 KB

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