super.c 65 KB

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