super.c 69 KB

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