super.c 66 KB

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