super.c 59 KB

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