super.c 57 KB

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