super.c 56 KB

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