super.c 57 KB

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