super.c 61 KB

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