super.c 67 KB

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