super.c 50 KB

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