super.c 65 KB

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