super.c 66 KB

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