super.c 52 KB

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