super.c 60 KB

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