super.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331
  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. static 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. err = open_ctree(sb, fs_devices, (char *)data);
  965. if (err) {
  966. printk(KERN_ERR "BTRFS: open_ctree failed\n");
  967. return err;
  968. }
  969. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  970. key.type = BTRFS_INODE_ITEM_KEY;
  971. key.offset = 0;
  972. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  973. if (IS_ERR(inode)) {
  974. err = PTR_ERR(inode);
  975. goto fail_close;
  976. }
  977. sb->s_root = d_make_root(inode);
  978. if (!sb->s_root) {
  979. err = -ENOMEM;
  980. goto fail_close;
  981. }
  982. save_mount_options(sb, data);
  983. cleancache_init_fs(sb);
  984. sb->s_flags |= MS_ACTIVE;
  985. return 0;
  986. fail_close:
  987. close_ctree(fs_info->tree_root);
  988. return err;
  989. }
  990. int btrfs_sync_fs(struct super_block *sb, int wait)
  991. {
  992. struct btrfs_trans_handle *trans;
  993. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  994. struct btrfs_root *root = fs_info->tree_root;
  995. trace_btrfs_sync_fs(wait);
  996. if (!wait) {
  997. filemap_flush(fs_info->btree_inode->i_mapping);
  998. return 0;
  999. }
  1000. btrfs_wait_ordered_roots(fs_info, -1);
  1001. trans = btrfs_attach_transaction_barrier(root);
  1002. if (IS_ERR(trans)) {
  1003. /* no transaction, don't bother */
  1004. if (PTR_ERR(trans) == -ENOENT) {
  1005. /*
  1006. * Exit unless we have some pending changes
  1007. * that need to go through commit
  1008. */
  1009. if (fs_info->pending_changes == 0)
  1010. return 0;
  1011. /*
  1012. * A non-blocking test if the fs is frozen. We must not
  1013. * start a new transaction here otherwise a deadlock
  1014. * happens. The pending operations are delayed to the
  1015. * next commit after thawing.
  1016. */
  1017. if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
  1018. __sb_end_write(sb, SB_FREEZE_WRITE);
  1019. else
  1020. return 0;
  1021. trans = btrfs_start_transaction(root, 0);
  1022. }
  1023. if (IS_ERR(trans))
  1024. return PTR_ERR(trans);
  1025. }
  1026. return btrfs_commit_transaction(trans, root);
  1027. }
  1028. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  1029. {
  1030. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  1031. struct btrfs_root *root = info->tree_root;
  1032. char *compress_type;
  1033. if (btrfs_test_opt(root, DEGRADED))
  1034. seq_puts(seq, ",degraded");
  1035. if (btrfs_test_opt(root, NODATASUM))
  1036. seq_puts(seq, ",nodatasum");
  1037. if (btrfs_test_opt(root, NODATACOW))
  1038. seq_puts(seq, ",nodatacow");
  1039. if (btrfs_test_opt(root, NOBARRIER))
  1040. seq_puts(seq, ",nobarrier");
  1041. if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
  1042. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  1043. if (info->alloc_start != 0)
  1044. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  1045. if (info->thread_pool_size != min_t(unsigned long,
  1046. num_online_cpus() + 2, 8))
  1047. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  1048. if (btrfs_test_opt(root, COMPRESS)) {
  1049. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  1050. compress_type = "zlib";
  1051. else
  1052. compress_type = "lzo";
  1053. if (btrfs_test_opt(root, FORCE_COMPRESS))
  1054. seq_printf(seq, ",compress-force=%s", compress_type);
  1055. else
  1056. seq_printf(seq, ",compress=%s", compress_type);
  1057. }
  1058. if (btrfs_test_opt(root, NOSSD))
  1059. seq_puts(seq, ",nossd");
  1060. if (btrfs_test_opt(root, SSD_SPREAD))
  1061. seq_puts(seq, ",ssd_spread");
  1062. else if (btrfs_test_opt(root, SSD))
  1063. seq_puts(seq, ",ssd");
  1064. if (btrfs_test_opt(root, NOTREELOG))
  1065. seq_puts(seq, ",notreelog");
  1066. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  1067. seq_puts(seq, ",flushoncommit");
  1068. if (btrfs_test_opt(root, DISCARD))
  1069. seq_puts(seq, ",discard");
  1070. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  1071. seq_puts(seq, ",noacl");
  1072. if (btrfs_test_opt(root, SPACE_CACHE))
  1073. seq_puts(seq, ",space_cache");
  1074. else
  1075. seq_puts(seq, ",nospace_cache");
  1076. if (btrfs_test_opt(root, RESCAN_UUID_TREE))
  1077. seq_puts(seq, ",rescan_uuid_tree");
  1078. if (btrfs_test_opt(root, CLEAR_CACHE))
  1079. seq_puts(seq, ",clear_cache");
  1080. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1081. seq_puts(seq, ",user_subvol_rm_allowed");
  1082. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  1083. seq_puts(seq, ",enospc_debug");
  1084. if (btrfs_test_opt(root, AUTO_DEFRAG))
  1085. seq_puts(seq, ",autodefrag");
  1086. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  1087. seq_puts(seq, ",inode_cache");
  1088. if (btrfs_test_opt(root, SKIP_BALANCE))
  1089. seq_puts(seq, ",skip_balance");
  1090. if (btrfs_test_opt(root, RECOVERY))
  1091. seq_puts(seq, ",recovery");
  1092. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1093. if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
  1094. seq_puts(seq, ",check_int_data");
  1095. else if (btrfs_test_opt(root, CHECK_INTEGRITY))
  1096. seq_puts(seq, ",check_int");
  1097. if (info->check_integrity_print_mask)
  1098. seq_printf(seq, ",check_int_print_mask=%d",
  1099. info->check_integrity_print_mask);
  1100. #endif
  1101. if (info->metadata_ratio)
  1102. seq_printf(seq, ",metadata_ratio=%d",
  1103. info->metadata_ratio);
  1104. if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
  1105. seq_puts(seq, ",fatal_errors=panic");
  1106. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  1107. seq_printf(seq, ",commit=%d", info->commit_interval);
  1108. seq_printf(seq, ",subvolid=%llu",
  1109. BTRFS_I(d_inode(dentry))->root->root_key.objectid);
  1110. seq_puts(seq, ",subvol=");
  1111. seq_dentry(seq, dentry, " \t\n\\");
  1112. return 0;
  1113. }
  1114. static int btrfs_test_super(struct super_block *s, void *data)
  1115. {
  1116. struct btrfs_fs_info *p = data;
  1117. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1118. return fs_info->fs_devices == p->fs_devices;
  1119. }
  1120. static int btrfs_set_super(struct super_block *s, void *data)
  1121. {
  1122. int err = set_anon_super(s, data);
  1123. if (!err)
  1124. s->s_fs_info = data;
  1125. return err;
  1126. }
  1127. /*
  1128. * subvolumes are identified by ino 256
  1129. */
  1130. static inline int is_subvolume_inode(struct inode *inode)
  1131. {
  1132. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  1133. return 1;
  1134. return 0;
  1135. }
  1136. /*
  1137. * This will add subvolid=0 to the argument string while removing any subvol=
  1138. * and subvolid= arguments to make sure we get the top-level root for path
  1139. * walking to the subvol we want.
  1140. */
  1141. static char *setup_root_args(char *args)
  1142. {
  1143. char *buf, *dst, *sep;
  1144. if (!args)
  1145. return kstrdup("subvolid=0", GFP_NOFS);
  1146. /* The worst case is that we add ",subvolid=0" to the end. */
  1147. buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
  1148. if (!buf)
  1149. return NULL;
  1150. while (1) {
  1151. sep = strchrnul(args, ',');
  1152. if (!strstarts(args, "subvol=") &&
  1153. !strstarts(args, "subvolid=")) {
  1154. memcpy(dst, args, sep - args);
  1155. dst += sep - args;
  1156. *dst++ = ',';
  1157. }
  1158. if (*sep)
  1159. args = sep + 1;
  1160. else
  1161. break;
  1162. }
  1163. strcpy(dst, "subvolid=0");
  1164. return buf;
  1165. }
  1166. static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
  1167. int flags, const char *device_name,
  1168. char *data)
  1169. {
  1170. struct dentry *root;
  1171. struct vfsmount *mnt = NULL;
  1172. char *newargs;
  1173. int ret;
  1174. newargs = setup_root_args(data);
  1175. if (!newargs) {
  1176. root = ERR_PTR(-ENOMEM);
  1177. goto out;
  1178. }
  1179. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
  1180. if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
  1181. if (flags & MS_RDONLY) {
  1182. mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
  1183. device_name, newargs);
  1184. } else {
  1185. mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
  1186. device_name, newargs);
  1187. if (IS_ERR(mnt)) {
  1188. root = ERR_CAST(mnt);
  1189. mnt = NULL;
  1190. goto out;
  1191. }
  1192. down_write(&mnt->mnt_sb->s_umount);
  1193. ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
  1194. up_write(&mnt->mnt_sb->s_umount);
  1195. if (ret < 0) {
  1196. root = ERR_PTR(ret);
  1197. goto out;
  1198. }
  1199. }
  1200. }
  1201. if (IS_ERR(mnt)) {
  1202. root = ERR_CAST(mnt);
  1203. mnt = NULL;
  1204. goto out;
  1205. }
  1206. if (!subvol_name) {
  1207. if (!subvol_objectid) {
  1208. ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
  1209. &subvol_objectid);
  1210. if (ret) {
  1211. root = ERR_PTR(ret);
  1212. goto out;
  1213. }
  1214. }
  1215. subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
  1216. subvol_objectid);
  1217. if (IS_ERR(subvol_name)) {
  1218. root = ERR_CAST(subvol_name);
  1219. subvol_name = NULL;
  1220. goto out;
  1221. }
  1222. }
  1223. root = mount_subtree(mnt, subvol_name);
  1224. /* mount_subtree() drops our reference on the vfsmount. */
  1225. mnt = NULL;
  1226. if (!IS_ERR(root)) {
  1227. struct super_block *s = root->d_sb;
  1228. struct inode *root_inode = d_inode(root);
  1229. u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
  1230. ret = 0;
  1231. if (!is_subvolume_inode(root_inode)) {
  1232. pr_err("BTRFS: '%s' is not a valid subvolume\n",
  1233. subvol_name);
  1234. ret = -EINVAL;
  1235. }
  1236. if (subvol_objectid && root_objectid != subvol_objectid) {
  1237. /*
  1238. * This will also catch a race condition where a
  1239. * subvolume which was passed by ID is renamed and
  1240. * another subvolume is renamed over the old location.
  1241. */
  1242. pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n",
  1243. subvol_name, subvol_objectid);
  1244. ret = -EINVAL;
  1245. }
  1246. if (ret) {
  1247. dput(root);
  1248. root = ERR_PTR(ret);
  1249. deactivate_locked_super(s);
  1250. }
  1251. }
  1252. out:
  1253. mntput(mnt);
  1254. kfree(newargs);
  1255. kfree(subvol_name);
  1256. return root;
  1257. }
  1258. static int parse_security_options(char *orig_opts,
  1259. struct security_mnt_opts *sec_opts)
  1260. {
  1261. char *secdata = NULL;
  1262. int ret = 0;
  1263. secdata = alloc_secdata();
  1264. if (!secdata)
  1265. return -ENOMEM;
  1266. ret = security_sb_copy_data(orig_opts, secdata);
  1267. if (ret) {
  1268. free_secdata(secdata);
  1269. return ret;
  1270. }
  1271. ret = security_sb_parse_opts_str(secdata, sec_opts);
  1272. free_secdata(secdata);
  1273. return ret;
  1274. }
  1275. static int setup_security_options(struct btrfs_fs_info *fs_info,
  1276. struct super_block *sb,
  1277. struct security_mnt_opts *sec_opts)
  1278. {
  1279. int ret = 0;
  1280. /*
  1281. * Call security_sb_set_mnt_opts() to check whether new sec_opts
  1282. * is valid.
  1283. */
  1284. ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
  1285. if (ret)
  1286. return ret;
  1287. #ifdef CONFIG_SECURITY
  1288. if (!fs_info->security_opts.num_mnt_opts) {
  1289. /* first time security setup, copy sec_opts to fs_info */
  1290. memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
  1291. } else {
  1292. /*
  1293. * Since SELinux(the only one supports security_mnt_opts) does
  1294. * NOT support changing context during remount/mount same sb,
  1295. * This must be the same or part of the same security options,
  1296. * just free it.
  1297. */
  1298. security_free_mnt_opts(sec_opts);
  1299. }
  1300. #endif
  1301. return ret;
  1302. }
  1303. /*
  1304. * Find a superblock for the given device / mount point.
  1305. *
  1306. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  1307. * for multiple device setup. Make sure to keep it in sync.
  1308. */
  1309. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  1310. const char *device_name, void *data)
  1311. {
  1312. struct block_device *bdev = NULL;
  1313. struct super_block *s;
  1314. struct btrfs_fs_devices *fs_devices = NULL;
  1315. struct btrfs_fs_info *fs_info = NULL;
  1316. struct security_mnt_opts new_sec_opts;
  1317. fmode_t mode = FMODE_READ;
  1318. char *subvol_name = NULL;
  1319. u64 subvol_objectid = 0;
  1320. int error = 0;
  1321. if (!(flags & MS_RDONLY))
  1322. mode |= FMODE_WRITE;
  1323. error = btrfs_parse_early_options(data, mode, fs_type,
  1324. &subvol_name, &subvol_objectid,
  1325. &fs_devices);
  1326. if (error) {
  1327. kfree(subvol_name);
  1328. return ERR_PTR(error);
  1329. }
  1330. if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  1331. /* mount_subvol() will free subvol_name. */
  1332. return mount_subvol(subvol_name, subvol_objectid, flags,
  1333. device_name, data);
  1334. }
  1335. security_init_mnt_opts(&new_sec_opts);
  1336. if (data) {
  1337. error = parse_security_options(data, &new_sec_opts);
  1338. if (error)
  1339. return ERR_PTR(error);
  1340. }
  1341. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  1342. if (error)
  1343. goto error_sec_opts;
  1344. /*
  1345. * Setup a dummy root and fs_info for test/set super. This is because
  1346. * we don't actually fill this stuff out until open_ctree, but we need
  1347. * it for searching for existing supers, so this lets us do that and
  1348. * then open_ctree will properly initialize everything later.
  1349. */
  1350. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  1351. if (!fs_info) {
  1352. error = -ENOMEM;
  1353. goto error_sec_opts;
  1354. }
  1355. fs_info->fs_devices = fs_devices;
  1356. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1357. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1358. security_init_mnt_opts(&fs_info->security_opts);
  1359. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1360. error = -ENOMEM;
  1361. goto error_fs_info;
  1362. }
  1363. error = btrfs_open_devices(fs_devices, mode, fs_type);
  1364. if (error)
  1365. goto error_fs_info;
  1366. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  1367. error = -EACCES;
  1368. goto error_close_devices;
  1369. }
  1370. bdev = fs_devices->latest_bdev;
  1371. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  1372. fs_info);
  1373. if (IS_ERR(s)) {
  1374. error = PTR_ERR(s);
  1375. goto error_close_devices;
  1376. }
  1377. if (s->s_root) {
  1378. btrfs_close_devices(fs_devices);
  1379. free_fs_info(fs_info);
  1380. if ((flags ^ s->s_flags) & MS_RDONLY)
  1381. error = -EBUSY;
  1382. } else {
  1383. char b[BDEVNAME_SIZE];
  1384. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  1385. btrfs_sb(s)->bdev_holder = fs_type;
  1386. error = btrfs_fill_super(s, fs_devices, data,
  1387. flags & MS_SILENT ? 1 : 0);
  1388. }
  1389. if (error) {
  1390. deactivate_locked_super(s);
  1391. goto error_sec_opts;
  1392. }
  1393. fs_info = btrfs_sb(s);
  1394. error = setup_security_options(fs_info, s, &new_sec_opts);
  1395. if (error) {
  1396. deactivate_locked_super(s);
  1397. goto error_sec_opts;
  1398. }
  1399. return dget(s->s_root);
  1400. error_close_devices:
  1401. btrfs_close_devices(fs_devices);
  1402. error_fs_info:
  1403. free_fs_info(fs_info);
  1404. error_sec_opts:
  1405. security_free_mnt_opts(&new_sec_opts);
  1406. return ERR_PTR(error);
  1407. }
  1408. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1409. int new_pool_size, int old_pool_size)
  1410. {
  1411. if (new_pool_size == old_pool_size)
  1412. return;
  1413. fs_info->thread_pool_size = new_pool_size;
  1414. btrfs_info(fs_info, "resize thread pool %d -> %d",
  1415. old_pool_size, new_pool_size);
  1416. btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
  1417. btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
  1418. btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
  1419. btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
  1420. btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
  1421. btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
  1422. btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
  1423. new_pool_size);
  1424. btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  1425. btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
  1426. btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
  1427. btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
  1428. btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
  1429. new_pool_size);
  1430. }
  1431. static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
  1432. {
  1433. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1434. }
  1435. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1436. unsigned long old_opts, int flags)
  1437. {
  1438. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1439. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1440. (flags & MS_RDONLY))) {
  1441. /* wait for any defraggers to finish */
  1442. wait_event(fs_info->transaction_wait,
  1443. (atomic_read(&fs_info->defrag_running) == 0));
  1444. if (flags & MS_RDONLY)
  1445. sync_filesystem(fs_info->sb);
  1446. }
  1447. }
  1448. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1449. unsigned long old_opts)
  1450. {
  1451. /*
  1452. * We need cleanup all defragable inodes if the autodefragment is
  1453. * close or the fs is R/O.
  1454. */
  1455. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1456. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1457. (fs_info->sb->s_flags & MS_RDONLY))) {
  1458. btrfs_cleanup_defrag_inodes(fs_info);
  1459. }
  1460. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1461. }
  1462. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1463. {
  1464. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1465. struct btrfs_root *root = fs_info->tree_root;
  1466. unsigned old_flags = sb->s_flags;
  1467. unsigned long old_opts = fs_info->mount_opt;
  1468. unsigned long old_compress_type = fs_info->compress_type;
  1469. u64 old_max_inline = fs_info->max_inline;
  1470. u64 old_alloc_start = fs_info->alloc_start;
  1471. int old_thread_pool_size = fs_info->thread_pool_size;
  1472. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1473. int ret;
  1474. sync_filesystem(sb);
  1475. btrfs_remount_prepare(fs_info);
  1476. if (data) {
  1477. struct security_mnt_opts new_sec_opts;
  1478. security_init_mnt_opts(&new_sec_opts);
  1479. ret = parse_security_options(data, &new_sec_opts);
  1480. if (ret)
  1481. goto restore;
  1482. ret = setup_security_options(fs_info, sb,
  1483. &new_sec_opts);
  1484. if (ret) {
  1485. security_free_mnt_opts(&new_sec_opts);
  1486. goto restore;
  1487. }
  1488. }
  1489. ret = btrfs_parse_options(root, data);
  1490. if (ret) {
  1491. ret = -EINVAL;
  1492. goto restore;
  1493. }
  1494. btrfs_remount_begin(fs_info, old_opts, *flags);
  1495. btrfs_resize_thread_pool(fs_info,
  1496. fs_info->thread_pool_size, old_thread_pool_size);
  1497. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1498. goto out;
  1499. if (*flags & MS_RDONLY) {
  1500. /*
  1501. * this also happens on 'umount -rf' or on shutdown, when
  1502. * the filesystem is busy.
  1503. */
  1504. cancel_work_sync(&fs_info->async_reclaim_work);
  1505. /* wait for the uuid_scan task to finish */
  1506. down(&fs_info->uuid_tree_rescan_sem);
  1507. /* avoid complains from lockdep et al. */
  1508. up(&fs_info->uuid_tree_rescan_sem);
  1509. sb->s_flags |= MS_RDONLY;
  1510. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1511. btrfs_scrub_cancel(fs_info);
  1512. btrfs_pause_balance(fs_info);
  1513. ret = btrfs_commit_super(root);
  1514. if (ret)
  1515. goto restore;
  1516. } else {
  1517. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  1518. btrfs_err(fs_info,
  1519. "Remounting read-write after error is not allowed");
  1520. ret = -EINVAL;
  1521. goto restore;
  1522. }
  1523. if (fs_info->fs_devices->rw_devices == 0) {
  1524. ret = -EACCES;
  1525. goto restore;
  1526. }
  1527. if (fs_info->fs_devices->missing_devices >
  1528. fs_info->num_tolerated_disk_barrier_failures &&
  1529. !(*flags & MS_RDONLY)) {
  1530. btrfs_warn(fs_info,
  1531. "too many missing devices, writeable remount is not allowed");
  1532. ret = -EACCES;
  1533. goto restore;
  1534. }
  1535. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1536. ret = -EINVAL;
  1537. goto restore;
  1538. }
  1539. ret = btrfs_cleanup_fs_roots(fs_info);
  1540. if (ret)
  1541. goto restore;
  1542. /* recover relocation */
  1543. mutex_lock(&fs_info->cleaner_mutex);
  1544. ret = btrfs_recover_relocation(root);
  1545. mutex_unlock(&fs_info->cleaner_mutex);
  1546. if (ret)
  1547. goto restore;
  1548. ret = btrfs_resume_balance_async(fs_info);
  1549. if (ret)
  1550. goto restore;
  1551. ret = btrfs_resume_dev_replace_async(fs_info);
  1552. if (ret) {
  1553. btrfs_warn(fs_info, "failed to resume dev_replace");
  1554. goto restore;
  1555. }
  1556. if (!fs_info->uuid_root) {
  1557. btrfs_info(fs_info, "creating UUID tree");
  1558. ret = btrfs_create_uuid_tree(fs_info);
  1559. if (ret) {
  1560. btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
  1561. goto restore;
  1562. }
  1563. }
  1564. sb->s_flags &= ~MS_RDONLY;
  1565. }
  1566. out:
  1567. wake_up_process(fs_info->transaction_kthread);
  1568. btrfs_remount_cleanup(fs_info, old_opts);
  1569. return 0;
  1570. restore:
  1571. /* We've hit an error - don't reset MS_RDONLY */
  1572. if (sb->s_flags & MS_RDONLY)
  1573. old_flags |= MS_RDONLY;
  1574. sb->s_flags = old_flags;
  1575. fs_info->mount_opt = old_opts;
  1576. fs_info->compress_type = old_compress_type;
  1577. fs_info->max_inline = old_max_inline;
  1578. mutex_lock(&fs_info->chunk_mutex);
  1579. fs_info->alloc_start = old_alloc_start;
  1580. mutex_unlock(&fs_info->chunk_mutex);
  1581. btrfs_resize_thread_pool(fs_info,
  1582. old_thread_pool_size, fs_info->thread_pool_size);
  1583. fs_info->metadata_ratio = old_metadata_ratio;
  1584. btrfs_remount_cleanup(fs_info, old_opts);
  1585. return ret;
  1586. }
  1587. /* Used to sort the devices by max_avail(descending sort) */
  1588. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1589. const void *dev_info2)
  1590. {
  1591. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1592. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1593. return -1;
  1594. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1595. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1596. return 1;
  1597. else
  1598. return 0;
  1599. }
  1600. /*
  1601. * sort the devices by max_avail, in which max free extent size of each device
  1602. * is stored.(Descending Sort)
  1603. */
  1604. static inline void btrfs_descending_sort_devices(
  1605. struct btrfs_device_info *devices,
  1606. size_t nr_devices)
  1607. {
  1608. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1609. btrfs_cmp_device_free_bytes, NULL);
  1610. }
  1611. /*
  1612. * The helper to calc the free space on the devices that can be used to store
  1613. * file data.
  1614. */
  1615. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  1616. {
  1617. struct btrfs_fs_info *fs_info = root->fs_info;
  1618. struct btrfs_device_info *devices_info;
  1619. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1620. struct btrfs_device *device;
  1621. u64 skip_space;
  1622. u64 type;
  1623. u64 avail_space;
  1624. u64 used_space;
  1625. u64 min_stripe_size;
  1626. int min_stripes = 1, num_stripes = 1;
  1627. int i = 0, nr_devices;
  1628. int ret;
  1629. /*
  1630. * We aren't under the device list lock, so this is racey-ish, but good
  1631. * enough for our purposes.
  1632. */
  1633. nr_devices = fs_info->fs_devices->open_devices;
  1634. if (!nr_devices) {
  1635. smp_mb();
  1636. nr_devices = fs_info->fs_devices->open_devices;
  1637. ASSERT(nr_devices);
  1638. if (!nr_devices) {
  1639. *free_bytes = 0;
  1640. return 0;
  1641. }
  1642. }
  1643. devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
  1644. GFP_NOFS);
  1645. if (!devices_info)
  1646. return -ENOMEM;
  1647. /* calc min stripe number for data space alloction */
  1648. type = btrfs_get_alloc_profile(root, 1);
  1649. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1650. min_stripes = 2;
  1651. num_stripes = nr_devices;
  1652. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1653. min_stripes = 2;
  1654. num_stripes = 2;
  1655. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1656. min_stripes = 4;
  1657. num_stripes = 4;
  1658. }
  1659. if (type & BTRFS_BLOCK_GROUP_DUP)
  1660. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1661. else
  1662. min_stripe_size = BTRFS_STRIPE_LEN;
  1663. if (fs_info->alloc_start)
  1664. mutex_lock(&fs_devices->device_list_mutex);
  1665. rcu_read_lock();
  1666. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  1667. if (!device->in_fs_metadata || !device->bdev ||
  1668. device->is_tgtdev_for_dev_replace)
  1669. continue;
  1670. if (i >= nr_devices)
  1671. break;
  1672. avail_space = device->total_bytes - device->bytes_used;
  1673. /* align with stripe_len */
  1674. avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
  1675. avail_space *= BTRFS_STRIPE_LEN;
  1676. /*
  1677. * In order to avoid overwritting the superblock on the drive,
  1678. * btrfs starts at an offset of at least 1MB when doing chunk
  1679. * allocation.
  1680. */
  1681. skip_space = 1024 * 1024;
  1682. /* user can set the offset in fs_info->alloc_start. */
  1683. if (fs_info->alloc_start &&
  1684. fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1685. device->total_bytes) {
  1686. rcu_read_unlock();
  1687. skip_space = max(fs_info->alloc_start, skip_space);
  1688. /*
  1689. * btrfs can not use the free space in
  1690. * [0, skip_space - 1], we must subtract it from the
  1691. * total. In order to implement it, we account the used
  1692. * space in this range first.
  1693. */
  1694. ret = btrfs_account_dev_extents_size(device, 0,
  1695. skip_space - 1,
  1696. &used_space);
  1697. if (ret) {
  1698. kfree(devices_info);
  1699. mutex_unlock(&fs_devices->device_list_mutex);
  1700. return ret;
  1701. }
  1702. rcu_read_lock();
  1703. /* calc the free space in [0, skip_space - 1] */
  1704. skip_space -= used_space;
  1705. }
  1706. /*
  1707. * we can use the free space in [0, skip_space - 1], subtract
  1708. * it from the total.
  1709. */
  1710. if (avail_space && avail_space >= skip_space)
  1711. avail_space -= skip_space;
  1712. else
  1713. avail_space = 0;
  1714. if (avail_space < min_stripe_size)
  1715. continue;
  1716. devices_info[i].dev = device;
  1717. devices_info[i].max_avail = avail_space;
  1718. i++;
  1719. }
  1720. rcu_read_unlock();
  1721. if (fs_info->alloc_start)
  1722. mutex_unlock(&fs_devices->device_list_mutex);
  1723. nr_devices = i;
  1724. btrfs_descending_sort_devices(devices_info, nr_devices);
  1725. i = nr_devices - 1;
  1726. avail_space = 0;
  1727. while (nr_devices >= min_stripes) {
  1728. if (num_stripes > nr_devices)
  1729. num_stripes = nr_devices;
  1730. if (devices_info[i].max_avail >= min_stripe_size) {
  1731. int j;
  1732. u64 alloc_size;
  1733. avail_space += devices_info[i].max_avail * num_stripes;
  1734. alloc_size = devices_info[i].max_avail;
  1735. for (j = i + 1 - num_stripes; j <= i; j++)
  1736. devices_info[j].max_avail -= alloc_size;
  1737. }
  1738. i--;
  1739. nr_devices--;
  1740. }
  1741. kfree(devices_info);
  1742. *free_bytes = avail_space;
  1743. return 0;
  1744. }
  1745. /*
  1746. * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
  1747. *
  1748. * If there's a redundant raid level at DATA block groups, use the respective
  1749. * multiplier to scale the sizes.
  1750. *
  1751. * Unused device space usage is based on simulating the chunk allocator
  1752. * algorithm that respects the device sizes, order of allocations and the
  1753. * 'alloc_start' value, this is a close approximation of the actual use but
  1754. * there are other factors that may change the result (like a new metadata
  1755. * chunk).
  1756. *
  1757. * FIXME: not accurate for mixed block groups, total and free/used are ok,
  1758. * available appears slightly larger.
  1759. */
  1760. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1761. {
  1762. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1763. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1764. struct list_head *head = &fs_info->space_info;
  1765. struct btrfs_space_info *found;
  1766. u64 total_used = 0;
  1767. u64 total_free_data = 0;
  1768. int bits = dentry->d_sb->s_blocksize_bits;
  1769. __be32 *fsid = (__be32 *)fs_info->fsid;
  1770. unsigned factor = 1;
  1771. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  1772. int ret;
  1773. /*
  1774. * holding chunk_muext to avoid allocating new chunks, holding
  1775. * device_list_mutex to avoid the device being removed
  1776. */
  1777. rcu_read_lock();
  1778. list_for_each_entry_rcu(found, head, list) {
  1779. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1780. int i;
  1781. total_free_data += found->disk_total - found->disk_used;
  1782. total_free_data -=
  1783. btrfs_account_ro_block_groups_free_space(found);
  1784. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1785. if (!list_empty(&found->block_groups[i])) {
  1786. switch (i) {
  1787. case BTRFS_RAID_DUP:
  1788. case BTRFS_RAID_RAID1:
  1789. case BTRFS_RAID_RAID10:
  1790. factor = 2;
  1791. }
  1792. }
  1793. }
  1794. }
  1795. total_used += found->disk_used;
  1796. }
  1797. rcu_read_unlock();
  1798. buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  1799. buf->f_blocks >>= bits;
  1800. buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  1801. /* Account global block reserve as used, it's in logical size already */
  1802. spin_lock(&block_rsv->lock);
  1803. buf->f_bfree -= block_rsv->size >> bits;
  1804. spin_unlock(&block_rsv->lock);
  1805. buf->f_bavail = div_u64(total_free_data, factor);
  1806. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1807. if (ret)
  1808. return ret;
  1809. buf->f_bavail += div_u64(total_free_data, factor);
  1810. buf->f_bavail = buf->f_bavail >> bits;
  1811. buf->f_type = BTRFS_SUPER_MAGIC;
  1812. buf->f_bsize = dentry->d_sb->s_blocksize;
  1813. buf->f_namelen = BTRFS_NAME_LEN;
  1814. /* We treat it as constant endianness (it doesn't matter _which_)
  1815. because we want the fsid to come out the same whether mounted
  1816. on a big-endian or little-endian host */
  1817. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1818. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1819. /* Mask in the root object ID too, to disambiguate subvols */
  1820. buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
  1821. buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
  1822. return 0;
  1823. }
  1824. static void btrfs_kill_super(struct super_block *sb)
  1825. {
  1826. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1827. kill_anon_super(sb);
  1828. free_fs_info(fs_info);
  1829. }
  1830. static struct file_system_type btrfs_fs_type = {
  1831. .owner = THIS_MODULE,
  1832. .name = "btrfs",
  1833. .mount = btrfs_mount,
  1834. .kill_sb = btrfs_kill_super,
  1835. .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
  1836. };
  1837. MODULE_ALIAS_FS("btrfs");
  1838. static int btrfs_control_open(struct inode *inode, struct file *file)
  1839. {
  1840. /*
  1841. * The control file's private_data is used to hold the
  1842. * transaction when it is started and is used to keep
  1843. * track of whether a transaction is already in progress.
  1844. */
  1845. file->private_data = NULL;
  1846. return 0;
  1847. }
  1848. /*
  1849. * used by btrfsctl to scan devices when no FS is mounted
  1850. */
  1851. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1852. unsigned long arg)
  1853. {
  1854. struct btrfs_ioctl_vol_args *vol;
  1855. struct btrfs_fs_devices *fs_devices;
  1856. int ret = -ENOTTY;
  1857. if (!capable(CAP_SYS_ADMIN))
  1858. return -EPERM;
  1859. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1860. if (IS_ERR(vol))
  1861. return PTR_ERR(vol);
  1862. switch (cmd) {
  1863. case BTRFS_IOC_SCAN_DEV:
  1864. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1865. &btrfs_fs_type, &fs_devices);
  1866. break;
  1867. case BTRFS_IOC_DEVICES_READY:
  1868. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1869. &btrfs_fs_type, &fs_devices);
  1870. if (ret)
  1871. break;
  1872. ret = !(fs_devices->num_devices == fs_devices->total_devices);
  1873. break;
  1874. }
  1875. kfree(vol);
  1876. return ret;
  1877. }
  1878. static int btrfs_freeze(struct super_block *sb)
  1879. {
  1880. struct btrfs_trans_handle *trans;
  1881. struct btrfs_root *root = btrfs_sb(sb)->tree_root;
  1882. trans = btrfs_attach_transaction_barrier(root);
  1883. if (IS_ERR(trans)) {
  1884. /* no transaction, don't bother */
  1885. if (PTR_ERR(trans) == -ENOENT)
  1886. return 0;
  1887. return PTR_ERR(trans);
  1888. }
  1889. return btrfs_commit_transaction(trans, root);
  1890. }
  1891. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  1892. {
  1893. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  1894. struct btrfs_fs_devices *cur_devices;
  1895. struct btrfs_device *dev, *first_dev = NULL;
  1896. struct list_head *head;
  1897. struct rcu_string *name;
  1898. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1899. cur_devices = fs_info->fs_devices;
  1900. while (cur_devices) {
  1901. head = &cur_devices->devices;
  1902. list_for_each_entry(dev, head, dev_list) {
  1903. if (dev->missing)
  1904. continue;
  1905. if (!dev->name)
  1906. continue;
  1907. if (!first_dev || dev->devid < first_dev->devid)
  1908. first_dev = dev;
  1909. }
  1910. cur_devices = cur_devices->seed;
  1911. }
  1912. if (first_dev) {
  1913. rcu_read_lock();
  1914. name = rcu_dereference(first_dev->name);
  1915. seq_escape(m, name->str, " \t\n\\");
  1916. rcu_read_unlock();
  1917. } else {
  1918. WARN_ON(1);
  1919. }
  1920. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1921. return 0;
  1922. }
  1923. static const struct super_operations btrfs_super_ops = {
  1924. .drop_inode = btrfs_drop_inode,
  1925. .evict_inode = btrfs_evict_inode,
  1926. .put_super = btrfs_put_super,
  1927. .sync_fs = btrfs_sync_fs,
  1928. .show_options = btrfs_show_options,
  1929. .show_devname = btrfs_show_devname,
  1930. .write_inode = btrfs_write_inode,
  1931. .alloc_inode = btrfs_alloc_inode,
  1932. .destroy_inode = btrfs_destroy_inode,
  1933. .statfs = btrfs_statfs,
  1934. .remount_fs = btrfs_remount,
  1935. .freeze_fs = btrfs_freeze,
  1936. };
  1937. static const struct file_operations btrfs_ctl_fops = {
  1938. .open = btrfs_control_open,
  1939. .unlocked_ioctl = btrfs_control_ioctl,
  1940. .compat_ioctl = btrfs_control_ioctl,
  1941. .owner = THIS_MODULE,
  1942. .llseek = noop_llseek,
  1943. };
  1944. static struct miscdevice btrfs_misc = {
  1945. .minor = BTRFS_MINOR,
  1946. .name = "btrfs-control",
  1947. .fops = &btrfs_ctl_fops
  1948. };
  1949. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  1950. MODULE_ALIAS("devname:btrfs-control");
  1951. static int btrfs_interface_init(void)
  1952. {
  1953. return misc_register(&btrfs_misc);
  1954. }
  1955. static void btrfs_interface_exit(void)
  1956. {
  1957. if (misc_deregister(&btrfs_misc) < 0)
  1958. printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n");
  1959. }
  1960. static void btrfs_print_info(void)
  1961. {
  1962. printk(KERN_INFO "Btrfs loaded"
  1963. #ifdef CONFIG_BTRFS_DEBUG
  1964. ", debug=on"
  1965. #endif
  1966. #ifdef CONFIG_BTRFS_ASSERT
  1967. ", assert=on"
  1968. #endif
  1969. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1970. ", integrity-checker=on"
  1971. #endif
  1972. "\n");
  1973. }
  1974. static int btrfs_run_sanity_tests(void)
  1975. {
  1976. int ret;
  1977. ret = btrfs_init_test_fs();
  1978. if (ret)
  1979. return ret;
  1980. ret = btrfs_test_free_space_cache();
  1981. if (ret)
  1982. goto out;
  1983. ret = btrfs_test_extent_buffer_operations();
  1984. if (ret)
  1985. goto out;
  1986. ret = btrfs_test_extent_io();
  1987. if (ret)
  1988. goto out;
  1989. ret = btrfs_test_inodes();
  1990. if (ret)
  1991. goto out;
  1992. ret = btrfs_test_qgroups();
  1993. out:
  1994. btrfs_destroy_test_fs();
  1995. return ret;
  1996. }
  1997. static int __init init_btrfs_fs(void)
  1998. {
  1999. int err;
  2000. err = btrfs_hash_init();
  2001. if (err)
  2002. return err;
  2003. btrfs_props_init();
  2004. err = btrfs_init_sysfs();
  2005. if (err)
  2006. goto free_hash;
  2007. btrfs_init_compress();
  2008. err = btrfs_init_cachep();
  2009. if (err)
  2010. goto free_compress;
  2011. err = extent_io_init();
  2012. if (err)
  2013. goto free_cachep;
  2014. err = extent_map_init();
  2015. if (err)
  2016. goto free_extent_io;
  2017. err = ordered_data_init();
  2018. if (err)
  2019. goto free_extent_map;
  2020. err = btrfs_delayed_inode_init();
  2021. if (err)
  2022. goto free_ordered_data;
  2023. err = btrfs_auto_defrag_init();
  2024. if (err)
  2025. goto free_delayed_inode;
  2026. err = btrfs_delayed_ref_init();
  2027. if (err)
  2028. goto free_auto_defrag;
  2029. err = btrfs_prelim_ref_init();
  2030. if (err)
  2031. goto free_delayed_ref;
  2032. err = btrfs_end_io_wq_init();
  2033. if (err)
  2034. goto free_prelim_ref;
  2035. err = btrfs_interface_init();
  2036. if (err)
  2037. goto free_end_io_wq;
  2038. btrfs_init_lockdep();
  2039. btrfs_print_info();
  2040. err = btrfs_run_sanity_tests();
  2041. if (err)
  2042. goto unregister_ioctl;
  2043. err = register_filesystem(&btrfs_fs_type);
  2044. if (err)
  2045. goto unregister_ioctl;
  2046. return 0;
  2047. unregister_ioctl:
  2048. btrfs_interface_exit();
  2049. free_end_io_wq:
  2050. btrfs_end_io_wq_exit();
  2051. free_prelim_ref:
  2052. btrfs_prelim_ref_exit();
  2053. free_delayed_ref:
  2054. btrfs_delayed_ref_exit();
  2055. free_auto_defrag:
  2056. btrfs_auto_defrag_exit();
  2057. free_delayed_inode:
  2058. btrfs_delayed_inode_exit();
  2059. free_ordered_data:
  2060. ordered_data_exit();
  2061. free_extent_map:
  2062. extent_map_exit();
  2063. free_extent_io:
  2064. extent_io_exit();
  2065. free_cachep:
  2066. btrfs_destroy_cachep();
  2067. free_compress:
  2068. btrfs_exit_compress();
  2069. btrfs_exit_sysfs();
  2070. free_hash:
  2071. btrfs_hash_exit();
  2072. return err;
  2073. }
  2074. static void __exit exit_btrfs_fs(void)
  2075. {
  2076. btrfs_destroy_cachep();
  2077. btrfs_delayed_ref_exit();
  2078. btrfs_auto_defrag_exit();
  2079. btrfs_delayed_inode_exit();
  2080. btrfs_prelim_ref_exit();
  2081. ordered_data_exit();
  2082. extent_map_exit();
  2083. extent_io_exit();
  2084. btrfs_interface_exit();
  2085. btrfs_end_io_wq_exit();
  2086. unregister_filesystem(&btrfs_fs_type);
  2087. btrfs_exit_sysfs();
  2088. btrfs_cleanup_fs_uuids();
  2089. btrfs_exit_compress();
  2090. btrfs_hash_exit();
  2091. }
  2092. late_initcall(init_btrfs_fs);
  2093. module_exit(exit_btrfs_fs)
  2094. MODULE_LICENSE("GPL");