super.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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 "compat.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 "ioctl.h"
  50. #include "print-tree.h"
  51. #include "xattr.h"
  52. #include "volumes.h"
  53. #include "version.h"
  54. #include "export.h"
  55. #include "compression.h"
  56. #define CREATE_TRACE_POINTS
  57. #include <trace/events/btrfs.h>
  58. static const struct super_operations btrfs_super_ops;
  59. static struct file_system_type btrfs_fs_type;
  60. static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
  61. char nbuf[16])
  62. {
  63. char *errstr = NULL;
  64. switch (errno) {
  65. case -EIO:
  66. errstr = "IO failure";
  67. break;
  68. case -ENOMEM:
  69. errstr = "Out of memory";
  70. break;
  71. case -EROFS:
  72. errstr = "Readonly filesystem";
  73. break;
  74. default:
  75. if (nbuf) {
  76. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  77. errstr = nbuf;
  78. }
  79. break;
  80. }
  81. return errstr;
  82. }
  83. static void __save_error_info(struct btrfs_fs_info *fs_info)
  84. {
  85. /*
  86. * today we only save the error info into ram. Long term we'll
  87. * also send it down to the disk
  88. */
  89. fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
  90. }
  91. /* NOTE:
  92. * We move write_super stuff at umount in order to avoid deadlock
  93. * for umount hold all lock.
  94. */
  95. static void save_error_info(struct btrfs_fs_info *fs_info)
  96. {
  97. __save_error_info(fs_info);
  98. }
  99. /* btrfs handle error by forcing the filesystem readonly */
  100. static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
  101. {
  102. struct super_block *sb = fs_info->sb;
  103. if (sb->s_flags & MS_RDONLY)
  104. return;
  105. if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  106. sb->s_flags |= MS_RDONLY;
  107. printk(KERN_INFO "btrfs is forced readonly\n");
  108. }
  109. }
  110. /*
  111. * __btrfs_std_error decodes expected errors from the caller and
  112. * invokes the approciate error response.
  113. */
  114. void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
  115. unsigned int line, int errno)
  116. {
  117. struct super_block *sb = fs_info->sb;
  118. char nbuf[16];
  119. const char *errstr;
  120. /*
  121. * Special case: if the error is EROFS, and we're already
  122. * under MS_RDONLY, then it is safe here.
  123. */
  124. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  125. return;
  126. errstr = btrfs_decode_error(fs_info, errno, nbuf);
  127. printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
  128. sb->s_id, function, line, errstr);
  129. save_error_info(fs_info);
  130. btrfs_handle_error(fs_info);
  131. }
  132. static void btrfs_put_super(struct super_block *sb)
  133. {
  134. (void)close_ctree(btrfs_sb(sb)->tree_root);
  135. /* FIXME: need to fix VFS to return error? */
  136. /* AV: return it _where_? ->put_super() can be triggered by any number
  137. * of async events, up to and including delivery of SIGKILL to the
  138. * last process that kept it busy. Or segfault in the aforementioned
  139. * process... Whom would you report that to?
  140. */
  141. }
  142. enum {
  143. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  144. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  145. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  146. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  147. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  148. Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
  149. Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
  150. Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
  151. Opt_check_integrity, Opt_check_integrity_including_extent_data,
  152. Opt_check_integrity_print_mask,
  153. Opt_err,
  154. };
  155. static match_table_t tokens = {
  156. {Opt_degraded, "degraded"},
  157. {Opt_subvol, "subvol=%s"},
  158. {Opt_subvolid, "subvolid=%d"},
  159. {Opt_device, "device=%s"},
  160. {Opt_nodatasum, "nodatasum"},
  161. {Opt_nodatacow, "nodatacow"},
  162. {Opt_nobarrier, "nobarrier"},
  163. {Opt_max_inline, "max_inline=%s"},
  164. {Opt_alloc_start, "alloc_start=%s"},
  165. {Opt_thread_pool, "thread_pool=%d"},
  166. {Opt_compress, "compress"},
  167. {Opt_compress_type, "compress=%s"},
  168. {Opt_compress_force, "compress-force"},
  169. {Opt_compress_force_type, "compress-force=%s"},
  170. {Opt_ssd, "ssd"},
  171. {Opt_ssd_spread, "ssd_spread"},
  172. {Opt_nossd, "nossd"},
  173. {Opt_noacl, "noacl"},
  174. {Opt_notreelog, "notreelog"},
  175. {Opt_flushoncommit, "flushoncommit"},
  176. {Opt_ratio, "metadata_ratio=%d"},
  177. {Opt_discard, "discard"},
  178. {Opt_space_cache, "space_cache"},
  179. {Opt_clear_cache, "clear_cache"},
  180. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  181. {Opt_enospc_debug, "enospc_debug"},
  182. {Opt_subvolrootid, "subvolrootid=%d"},
  183. {Opt_defrag, "autodefrag"},
  184. {Opt_inode_cache, "inode_cache"},
  185. {Opt_no_space_cache, "nospace_cache"},
  186. {Opt_recovery, "recovery"},
  187. {Opt_skip_balance, "skip_balance"},
  188. {Opt_check_integrity, "check_int"},
  189. {Opt_check_integrity_including_extent_data, "check_int_data"},
  190. {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
  191. {Opt_err, NULL},
  192. };
  193. /*
  194. * Regular mount options parser. Everything that is needed only when
  195. * reading in a new superblock is parsed here.
  196. */
  197. int btrfs_parse_options(struct btrfs_root *root, char *options)
  198. {
  199. struct btrfs_fs_info *info = root->fs_info;
  200. substring_t args[MAX_OPT_ARGS];
  201. char *p, *num, *orig = NULL;
  202. u64 cache_gen;
  203. int intarg;
  204. int ret = 0;
  205. char *compress_type;
  206. bool compress_force = false;
  207. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  208. if (cache_gen)
  209. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  210. if (!options)
  211. goto out;
  212. /*
  213. * strsep changes the string, duplicate it because parse_options
  214. * gets called twice
  215. */
  216. options = kstrdup(options, GFP_NOFS);
  217. if (!options)
  218. return -ENOMEM;
  219. orig = options;
  220. while ((p = strsep(&options, ",")) != NULL) {
  221. int token;
  222. if (!*p)
  223. continue;
  224. token = match_token(p, tokens, args);
  225. switch (token) {
  226. case Opt_degraded:
  227. printk(KERN_INFO "btrfs: allowing degraded mounts\n");
  228. btrfs_set_opt(info->mount_opt, DEGRADED);
  229. break;
  230. case Opt_subvol:
  231. case Opt_subvolid:
  232. case Opt_subvolrootid:
  233. case Opt_device:
  234. /*
  235. * These are parsed by btrfs_parse_early_options
  236. * and can be happily ignored here.
  237. */
  238. break;
  239. case Opt_nodatasum:
  240. printk(KERN_INFO "btrfs: setting nodatasum\n");
  241. btrfs_set_opt(info->mount_opt, NODATASUM);
  242. break;
  243. case Opt_nodatacow:
  244. printk(KERN_INFO "btrfs: setting nodatacow\n");
  245. btrfs_set_opt(info->mount_opt, NODATACOW);
  246. btrfs_set_opt(info->mount_opt, NODATASUM);
  247. break;
  248. case Opt_compress_force:
  249. case Opt_compress_force_type:
  250. compress_force = true;
  251. case Opt_compress:
  252. case Opt_compress_type:
  253. if (token == Opt_compress ||
  254. token == Opt_compress_force ||
  255. strcmp(args[0].from, "zlib") == 0) {
  256. compress_type = "zlib";
  257. info->compress_type = BTRFS_COMPRESS_ZLIB;
  258. } else if (strcmp(args[0].from, "lzo") == 0) {
  259. compress_type = "lzo";
  260. info->compress_type = BTRFS_COMPRESS_LZO;
  261. } else {
  262. ret = -EINVAL;
  263. goto out;
  264. }
  265. btrfs_set_opt(info->mount_opt, COMPRESS);
  266. if (compress_force) {
  267. btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
  268. pr_info("btrfs: force %s compression\n",
  269. compress_type);
  270. } else
  271. pr_info("btrfs: use %s compression\n",
  272. compress_type);
  273. break;
  274. case Opt_ssd:
  275. printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
  276. btrfs_set_opt(info->mount_opt, SSD);
  277. break;
  278. case Opt_ssd_spread:
  279. printk(KERN_INFO "btrfs: use spread ssd "
  280. "allocation scheme\n");
  281. btrfs_set_opt(info->mount_opt, SSD);
  282. btrfs_set_opt(info->mount_opt, SSD_SPREAD);
  283. break;
  284. case Opt_nossd:
  285. printk(KERN_INFO "btrfs: not using ssd allocation "
  286. "scheme\n");
  287. btrfs_set_opt(info->mount_opt, NOSSD);
  288. btrfs_clear_opt(info->mount_opt, SSD);
  289. btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
  290. break;
  291. case Opt_nobarrier:
  292. printk(KERN_INFO "btrfs: turning off barriers\n");
  293. btrfs_set_opt(info->mount_opt, NOBARRIER);
  294. break;
  295. case Opt_thread_pool:
  296. intarg = 0;
  297. match_int(&args[0], &intarg);
  298. if (intarg) {
  299. info->thread_pool_size = intarg;
  300. printk(KERN_INFO "btrfs: thread pool %d\n",
  301. info->thread_pool_size);
  302. }
  303. break;
  304. case Opt_max_inline:
  305. num = match_strdup(&args[0]);
  306. if (num) {
  307. info->max_inline = memparse(num, NULL);
  308. kfree(num);
  309. if (info->max_inline) {
  310. info->max_inline = max_t(u64,
  311. info->max_inline,
  312. root->sectorsize);
  313. }
  314. printk(KERN_INFO "btrfs: max_inline at %llu\n",
  315. (unsigned long long)info->max_inline);
  316. }
  317. break;
  318. case Opt_alloc_start:
  319. num = match_strdup(&args[0]);
  320. if (num) {
  321. info->alloc_start = memparse(num, NULL);
  322. kfree(num);
  323. printk(KERN_INFO
  324. "btrfs: allocations start at %llu\n",
  325. (unsigned long long)info->alloc_start);
  326. }
  327. break;
  328. case Opt_noacl:
  329. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  330. break;
  331. case Opt_notreelog:
  332. printk(KERN_INFO "btrfs: disabling tree log\n");
  333. btrfs_set_opt(info->mount_opt, NOTREELOG);
  334. break;
  335. case Opt_flushoncommit:
  336. printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
  337. btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
  338. break;
  339. case Opt_ratio:
  340. intarg = 0;
  341. match_int(&args[0], &intarg);
  342. if (intarg) {
  343. info->metadata_ratio = intarg;
  344. printk(KERN_INFO "btrfs: metadata ratio %d\n",
  345. info->metadata_ratio);
  346. }
  347. break;
  348. case Opt_discard:
  349. btrfs_set_opt(info->mount_opt, DISCARD);
  350. break;
  351. case Opt_space_cache:
  352. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  353. break;
  354. case Opt_no_space_cache:
  355. printk(KERN_INFO "btrfs: disabling disk space caching\n");
  356. btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
  357. break;
  358. case Opt_inode_cache:
  359. printk(KERN_INFO "btrfs: enabling inode map caching\n");
  360. btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
  361. break;
  362. case Opt_clear_cache:
  363. printk(KERN_INFO "btrfs: force clearing of disk cache\n");
  364. btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
  365. break;
  366. case Opt_user_subvol_rm_allowed:
  367. btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  368. break;
  369. case Opt_enospc_debug:
  370. btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
  371. break;
  372. case Opt_defrag:
  373. printk(KERN_INFO "btrfs: enabling auto defrag");
  374. btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
  375. break;
  376. case Opt_recovery:
  377. printk(KERN_INFO "btrfs: enabling auto recovery");
  378. btrfs_set_opt(info->mount_opt, RECOVERY);
  379. break;
  380. case Opt_skip_balance:
  381. btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
  382. break;
  383. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  384. case Opt_check_integrity_including_extent_data:
  385. printk(KERN_INFO "btrfs: enabling check integrity"
  386. " including extent data\n");
  387. btrfs_set_opt(info->mount_opt,
  388. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
  389. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  390. break;
  391. case Opt_check_integrity:
  392. printk(KERN_INFO "btrfs: enabling check integrity\n");
  393. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  394. break;
  395. case Opt_check_integrity_print_mask:
  396. intarg = 0;
  397. match_int(&args[0], &intarg);
  398. if (intarg) {
  399. info->check_integrity_print_mask = intarg;
  400. printk(KERN_INFO "btrfs:"
  401. " check_integrity_print_mask 0x%x\n",
  402. info->check_integrity_print_mask);
  403. }
  404. break;
  405. #else
  406. case Opt_check_integrity_including_extent_data:
  407. case Opt_check_integrity:
  408. case Opt_check_integrity_print_mask:
  409. printk(KERN_ERR "btrfs: support for check_integrity*"
  410. " not compiled in!\n");
  411. ret = -EINVAL;
  412. goto out;
  413. #endif
  414. case Opt_err:
  415. printk(KERN_INFO "btrfs: unrecognized mount option "
  416. "'%s'\n", p);
  417. ret = -EINVAL;
  418. goto out;
  419. default:
  420. break;
  421. }
  422. }
  423. out:
  424. if (!ret && btrfs_test_opt(root, SPACE_CACHE))
  425. printk(KERN_INFO "btrfs: disk space caching is enabled\n");
  426. kfree(orig);
  427. return ret;
  428. }
  429. /*
  430. * Parse mount options that are required early in the mount process.
  431. *
  432. * All other options will be parsed on much later in the mount process and
  433. * only when we need to allocate a new super block.
  434. */
  435. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  436. void *holder, char **subvol_name, u64 *subvol_objectid,
  437. u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
  438. {
  439. substring_t args[MAX_OPT_ARGS];
  440. char *device_name, *opts, *orig, *p;
  441. int error = 0;
  442. int intarg;
  443. if (!options)
  444. return 0;
  445. /*
  446. * strsep changes the string, duplicate it because parse_options
  447. * gets called twice
  448. */
  449. opts = kstrdup(options, GFP_KERNEL);
  450. if (!opts)
  451. return -ENOMEM;
  452. orig = opts;
  453. while ((p = strsep(&opts, ",")) != NULL) {
  454. int token;
  455. if (!*p)
  456. continue;
  457. token = match_token(p, tokens, args);
  458. switch (token) {
  459. case Opt_subvol:
  460. kfree(*subvol_name);
  461. *subvol_name = match_strdup(&args[0]);
  462. break;
  463. case Opt_subvolid:
  464. intarg = 0;
  465. error = match_int(&args[0], &intarg);
  466. if (!error) {
  467. /* we want the original fs_tree */
  468. if (!intarg)
  469. *subvol_objectid =
  470. BTRFS_FS_TREE_OBJECTID;
  471. else
  472. *subvol_objectid = intarg;
  473. }
  474. break;
  475. case Opt_subvolrootid:
  476. intarg = 0;
  477. error = match_int(&args[0], &intarg);
  478. if (!error) {
  479. /* we want the original fs_tree */
  480. if (!intarg)
  481. *subvol_rootid =
  482. BTRFS_FS_TREE_OBJECTID;
  483. else
  484. *subvol_rootid = intarg;
  485. }
  486. break;
  487. case Opt_device:
  488. device_name = match_strdup(&args[0]);
  489. if (!device_name) {
  490. error = -ENOMEM;
  491. goto out;
  492. }
  493. error = btrfs_scan_one_device(device_name,
  494. flags, holder, fs_devices);
  495. kfree(device_name);
  496. if (error)
  497. goto out;
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. out:
  504. kfree(orig);
  505. return error;
  506. }
  507. static struct dentry *get_default_root(struct super_block *sb,
  508. u64 subvol_objectid)
  509. {
  510. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  511. struct btrfs_root *root = fs_info->tree_root;
  512. struct btrfs_root *new_root;
  513. struct btrfs_dir_item *di;
  514. struct btrfs_path *path;
  515. struct btrfs_key location;
  516. struct inode *inode;
  517. u64 dir_id;
  518. int new = 0;
  519. /*
  520. * We have a specific subvol we want to mount, just setup location and
  521. * go look up the root.
  522. */
  523. if (subvol_objectid) {
  524. location.objectid = subvol_objectid;
  525. location.type = BTRFS_ROOT_ITEM_KEY;
  526. location.offset = (u64)-1;
  527. goto find_root;
  528. }
  529. path = btrfs_alloc_path();
  530. if (!path)
  531. return ERR_PTR(-ENOMEM);
  532. path->leave_spinning = 1;
  533. /*
  534. * Find the "default" dir item which points to the root item that we
  535. * will mount by default if we haven't been given a specific subvolume
  536. * to mount.
  537. */
  538. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  539. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
  540. if (IS_ERR(di)) {
  541. btrfs_free_path(path);
  542. return ERR_CAST(di);
  543. }
  544. if (!di) {
  545. /*
  546. * Ok the default dir item isn't there. This is weird since
  547. * it's always been there, but don't freak out, just try and
  548. * mount to root most subvolume.
  549. */
  550. btrfs_free_path(path);
  551. dir_id = BTRFS_FIRST_FREE_OBJECTID;
  552. new_root = fs_info->fs_root;
  553. goto setup_root;
  554. }
  555. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  556. btrfs_free_path(path);
  557. find_root:
  558. new_root = btrfs_read_fs_root_no_name(fs_info, &location);
  559. if (IS_ERR(new_root))
  560. return ERR_CAST(new_root);
  561. if (btrfs_root_refs(&new_root->root_item) == 0)
  562. return ERR_PTR(-ENOENT);
  563. dir_id = btrfs_root_dirid(&new_root->root_item);
  564. setup_root:
  565. location.objectid = dir_id;
  566. location.type = BTRFS_INODE_ITEM_KEY;
  567. location.offset = 0;
  568. inode = btrfs_iget(sb, &location, new_root, &new);
  569. if (IS_ERR(inode))
  570. return ERR_CAST(inode);
  571. /*
  572. * If we're just mounting the root most subvol put the inode and return
  573. * a reference to the dentry. We will have already gotten a reference
  574. * to the inode in btrfs_fill_super so we're good to go.
  575. */
  576. if (!new && sb->s_root->d_inode == inode) {
  577. iput(inode);
  578. return dget(sb->s_root);
  579. }
  580. return d_obtain_alias(inode);
  581. }
  582. static int btrfs_fill_super(struct super_block *sb,
  583. struct btrfs_fs_devices *fs_devices,
  584. void *data, int silent)
  585. {
  586. struct inode *inode;
  587. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  588. struct btrfs_key key;
  589. int err;
  590. sb->s_maxbytes = MAX_LFS_FILESIZE;
  591. sb->s_magic = BTRFS_SUPER_MAGIC;
  592. sb->s_op = &btrfs_super_ops;
  593. sb->s_d_op = &btrfs_dentry_operations;
  594. sb->s_export_op = &btrfs_export_ops;
  595. sb->s_xattr = btrfs_xattr_handlers;
  596. sb->s_time_gran = 1;
  597. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  598. sb->s_flags |= MS_POSIXACL;
  599. #endif
  600. err = open_ctree(sb, fs_devices, (char *)data);
  601. if (err) {
  602. printk("btrfs: open_ctree failed\n");
  603. return err;
  604. }
  605. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  606. key.type = BTRFS_INODE_ITEM_KEY;
  607. key.offset = 0;
  608. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  609. if (IS_ERR(inode)) {
  610. err = PTR_ERR(inode);
  611. goto fail_close;
  612. }
  613. sb->s_root = d_make_root(inode);
  614. if (!sb->s_root) {
  615. err = -ENOMEM;
  616. goto fail_close;
  617. }
  618. save_mount_options(sb, data);
  619. cleancache_init_fs(sb);
  620. sb->s_flags |= MS_ACTIVE;
  621. return 0;
  622. fail_close:
  623. close_ctree(fs_info->tree_root);
  624. return err;
  625. }
  626. int btrfs_sync_fs(struct super_block *sb, int wait)
  627. {
  628. struct btrfs_trans_handle *trans;
  629. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  630. struct btrfs_root *root = fs_info->tree_root;
  631. int ret;
  632. trace_btrfs_sync_fs(wait);
  633. if (!wait) {
  634. filemap_flush(fs_info->btree_inode->i_mapping);
  635. return 0;
  636. }
  637. btrfs_start_delalloc_inodes(root, 0);
  638. btrfs_wait_ordered_extents(root, 0, 0);
  639. trans = btrfs_start_transaction(root, 0);
  640. if (IS_ERR(trans))
  641. return PTR_ERR(trans);
  642. ret = btrfs_commit_transaction(trans, root);
  643. return ret;
  644. }
  645. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  646. {
  647. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  648. struct btrfs_root *root = info->tree_root;
  649. char *compress_type;
  650. if (btrfs_test_opt(root, DEGRADED))
  651. seq_puts(seq, ",degraded");
  652. if (btrfs_test_opt(root, NODATASUM))
  653. seq_puts(seq, ",nodatasum");
  654. if (btrfs_test_opt(root, NODATACOW))
  655. seq_puts(seq, ",nodatacow");
  656. if (btrfs_test_opt(root, NOBARRIER))
  657. seq_puts(seq, ",nobarrier");
  658. if (info->max_inline != 8192 * 1024)
  659. seq_printf(seq, ",max_inline=%llu",
  660. (unsigned long long)info->max_inline);
  661. if (info->alloc_start != 0)
  662. seq_printf(seq, ",alloc_start=%llu",
  663. (unsigned long long)info->alloc_start);
  664. if (info->thread_pool_size != min_t(unsigned long,
  665. num_online_cpus() + 2, 8))
  666. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  667. if (btrfs_test_opt(root, COMPRESS)) {
  668. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  669. compress_type = "zlib";
  670. else
  671. compress_type = "lzo";
  672. if (btrfs_test_opt(root, FORCE_COMPRESS))
  673. seq_printf(seq, ",compress-force=%s", compress_type);
  674. else
  675. seq_printf(seq, ",compress=%s", compress_type);
  676. }
  677. if (btrfs_test_opt(root, NOSSD))
  678. seq_puts(seq, ",nossd");
  679. if (btrfs_test_opt(root, SSD_SPREAD))
  680. seq_puts(seq, ",ssd_spread");
  681. else if (btrfs_test_opt(root, SSD))
  682. seq_puts(seq, ",ssd");
  683. if (btrfs_test_opt(root, NOTREELOG))
  684. seq_puts(seq, ",notreelog");
  685. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  686. seq_puts(seq, ",flushoncommit");
  687. if (btrfs_test_opt(root, DISCARD))
  688. seq_puts(seq, ",discard");
  689. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  690. seq_puts(seq, ",noacl");
  691. if (btrfs_test_opt(root, SPACE_CACHE))
  692. seq_puts(seq, ",space_cache");
  693. else
  694. seq_puts(seq, ",nospace_cache");
  695. if (btrfs_test_opt(root, CLEAR_CACHE))
  696. seq_puts(seq, ",clear_cache");
  697. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  698. seq_puts(seq, ",user_subvol_rm_allowed");
  699. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  700. seq_puts(seq, ",enospc_debug");
  701. if (btrfs_test_opt(root, AUTO_DEFRAG))
  702. seq_puts(seq, ",autodefrag");
  703. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  704. seq_puts(seq, ",inode_cache");
  705. if (btrfs_test_opt(root, SKIP_BALANCE))
  706. seq_puts(seq, ",skip_balance");
  707. return 0;
  708. }
  709. static int btrfs_test_super(struct super_block *s, void *data)
  710. {
  711. struct btrfs_fs_info *p = data;
  712. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  713. return fs_info->fs_devices == p->fs_devices;
  714. }
  715. static int btrfs_set_super(struct super_block *s, void *data)
  716. {
  717. int err = set_anon_super(s, data);
  718. if (!err)
  719. s->s_fs_info = data;
  720. return err;
  721. }
  722. /*
  723. * subvolumes are identified by ino 256
  724. */
  725. static inline int is_subvolume_inode(struct inode *inode)
  726. {
  727. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  728. return 1;
  729. return 0;
  730. }
  731. /*
  732. * This will strip out the subvol=%s argument for an argument string and add
  733. * subvolid=0 to make sure we get the actual tree root for path walking to the
  734. * subvol we want.
  735. */
  736. static char *setup_root_args(char *args)
  737. {
  738. unsigned copied = 0;
  739. unsigned len = strlen(args) + 2;
  740. char *pos;
  741. char *ret;
  742. /*
  743. * We need the same args as before, but minus
  744. *
  745. * subvol=a
  746. *
  747. * and add
  748. *
  749. * subvolid=0
  750. *
  751. * which is a difference of 2 characters, so we allocate strlen(args) +
  752. * 2 characters.
  753. */
  754. ret = kzalloc(len * sizeof(char), GFP_NOFS);
  755. if (!ret)
  756. return NULL;
  757. pos = strstr(args, "subvol=");
  758. /* This shouldn't happen, but just in case.. */
  759. if (!pos) {
  760. kfree(ret);
  761. return NULL;
  762. }
  763. /*
  764. * The subvol=<> arg is not at the front of the string, copy everybody
  765. * up to that into ret.
  766. */
  767. if (pos != args) {
  768. *pos = '\0';
  769. strcpy(ret, args);
  770. copied += strlen(args);
  771. pos++;
  772. }
  773. strncpy(ret + copied, "subvolid=0", len - copied);
  774. /* Length of subvolid=0 */
  775. copied += 10;
  776. /*
  777. * If there is no , after the subvol= option then we know there's no
  778. * other options and we can just return.
  779. */
  780. pos = strchr(pos, ',');
  781. if (!pos)
  782. return ret;
  783. /* Copy the rest of the arguments into our buffer */
  784. strncpy(ret + copied, pos, len - copied);
  785. copied += strlen(pos);
  786. return ret;
  787. }
  788. static struct dentry *mount_subvol(const char *subvol_name, int flags,
  789. const char *device_name, char *data)
  790. {
  791. struct dentry *root;
  792. struct vfsmount *mnt;
  793. char *newargs;
  794. newargs = setup_root_args(data);
  795. if (!newargs)
  796. return ERR_PTR(-ENOMEM);
  797. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
  798. newargs);
  799. kfree(newargs);
  800. if (IS_ERR(mnt))
  801. return ERR_CAST(mnt);
  802. root = mount_subtree(mnt, subvol_name);
  803. if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
  804. struct super_block *s = root->d_sb;
  805. dput(root);
  806. root = ERR_PTR(-EINVAL);
  807. deactivate_locked_super(s);
  808. printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
  809. subvol_name);
  810. }
  811. return root;
  812. }
  813. /*
  814. * Find a superblock for the given device / mount point.
  815. *
  816. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  817. * for multiple device setup. Make sure to keep it in sync.
  818. */
  819. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  820. const char *device_name, void *data)
  821. {
  822. struct block_device *bdev = NULL;
  823. struct super_block *s;
  824. struct dentry *root;
  825. struct btrfs_fs_devices *fs_devices = NULL;
  826. struct btrfs_fs_info *fs_info = NULL;
  827. fmode_t mode = FMODE_READ;
  828. char *subvol_name = NULL;
  829. u64 subvol_objectid = 0;
  830. u64 subvol_rootid = 0;
  831. int error = 0;
  832. if (!(flags & MS_RDONLY))
  833. mode |= FMODE_WRITE;
  834. error = btrfs_parse_early_options(data, mode, fs_type,
  835. &subvol_name, &subvol_objectid,
  836. &subvol_rootid, &fs_devices);
  837. if (error) {
  838. kfree(subvol_name);
  839. return ERR_PTR(error);
  840. }
  841. if (subvol_name) {
  842. root = mount_subvol(subvol_name, flags, device_name, data);
  843. kfree(subvol_name);
  844. return root;
  845. }
  846. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  847. if (error)
  848. return ERR_PTR(error);
  849. /*
  850. * Setup a dummy root and fs_info for test/set super. This is because
  851. * we don't actually fill this stuff out until open_ctree, but we need
  852. * it for searching for existing supers, so this lets us do that and
  853. * then open_ctree will properly initialize everything later.
  854. */
  855. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  856. if (!fs_info)
  857. return ERR_PTR(-ENOMEM);
  858. fs_info->fs_devices = fs_devices;
  859. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  860. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  861. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  862. error = -ENOMEM;
  863. goto error_fs_info;
  864. }
  865. error = btrfs_open_devices(fs_devices, mode, fs_type);
  866. if (error)
  867. goto error_fs_info;
  868. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  869. error = -EACCES;
  870. goto error_close_devices;
  871. }
  872. bdev = fs_devices->latest_bdev;
  873. s = sget(fs_type, btrfs_test_super, btrfs_set_super, fs_info);
  874. if (IS_ERR(s)) {
  875. error = PTR_ERR(s);
  876. goto error_close_devices;
  877. }
  878. if (s->s_root) {
  879. btrfs_close_devices(fs_devices);
  880. free_fs_info(fs_info);
  881. if ((flags ^ s->s_flags) & MS_RDONLY)
  882. error = -EBUSY;
  883. } else {
  884. char b[BDEVNAME_SIZE];
  885. s->s_flags = flags | MS_NOSEC;
  886. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  887. btrfs_sb(s)->bdev_holder = fs_type;
  888. error = btrfs_fill_super(s, fs_devices, data,
  889. flags & MS_SILENT ? 1 : 0);
  890. }
  891. root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
  892. if (IS_ERR(root))
  893. deactivate_locked_super(s);
  894. return root;
  895. error_close_devices:
  896. btrfs_close_devices(fs_devices);
  897. error_fs_info:
  898. free_fs_info(fs_info);
  899. return ERR_PTR(error);
  900. }
  901. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  902. {
  903. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  904. struct btrfs_root *root = fs_info->tree_root;
  905. int ret;
  906. ret = btrfs_parse_options(root, data);
  907. if (ret)
  908. return -EINVAL;
  909. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  910. return 0;
  911. if (*flags & MS_RDONLY) {
  912. sb->s_flags |= MS_RDONLY;
  913. ret = btrfs_commit_super(root);
  914. WARN_ON(ret);
  915. } else {
  916. if (fs_info->fs_devices->rw_devices == 0)
  917. return -EACCES;
  918. if (btrfs_super_log_root(fs_info->super_copy) != 0)
  919. return -EINVAL;
  920. ret = btrfs_cleanup_fs_roots(fs_info);
  921. WARN_ON(ret);
  922. /* recover relocation */
  923. ret = btrfs_recover_relocation(root);
  924. WARN_ON(ret);
  925. sb->s_flags &= ~MS_RDONLY;
  926. }
  927. return 0;
  928. }
  929. /* Used to sort the devices by max_avail(descending sort) */
  930. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  931. const void *dev_info2)
  932. {
  933. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  934. ((struct btrfs_device_info *)dev_info2)->max_avail)
  935. return -1;
  936. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  937. ((struct btrfs_device_info *)dev_info2)->max_avail)
  938. return 1;
  939. else
  940. return 0;
  941. }
  942. /*
  943. * sort the devices by max_avail, in which max free extent size of each device
  944. * is stored.(Descending Sort)
  945. */
  946. static inline void btrfs_descending_sort_devices(
  947. struct btrfs_device_info *devices,
  948. size_t nr_devices)
  949. {
  950. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  951. btrfs_cmp_device_free_bytes, NULL);
  952. }
  953. /*
  954. * The helper to calc the free space on the devices that can be used to store
  955. * file data.
  956. */
  957. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  958. {
  959. struct btrfs_fs_info *fs_info = root->fs_info;
  960. struct btrfs_device_info *devices_info;
  961. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  962. struct btrfs_device *device;
  963. u64 skip_space;
  964. u64 type;
  965. u64 avail_space;
  966. u64 used_space;
  967. u64 min_stripe_size;
  968. int min_stripes = 1, num_stripes = 1;
  969. int i = 0, nr_devices;
  970. int ret;
  971. nr_devices = fs_info->fs_devices->open_devices;
  972. BUG_ON(!nr_devices);
  973. devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
  974. GFP_NOFS);
  975. if (!devices_info)
  976. return -ENOMEM;
  977. /* calc min stripe number for data space alloction */
  978. type = btrfs_get_alloc_profile(root, 1);
  979. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  980. min_stripes = 2;
  981. num_stripes = nr_devices;
  982. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  983. min_stripes = 2;
  984. num_stripes = 2;
  985. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  986. min_stripes = 4;
  987. num_stripes = 4;
  988. }
  989. if (type & BTRFS_BLOCK_GROUP_DUP)
  990. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  991. else
  992. min_stripe_size = BTRFS_STRIPE_LEN;
  993. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  994. if (!device->in_fs_metadata || !device->bdev)
  995. continue;
  996. avail_space = device->total_bytes - device->bytes_used;
  997. /* align with stripe_len */
  998. do_div(avail_space, BTRFS_STRIPE_LEN);
  999. avail_space *= BTRFS_STRIPE_LEN;
  1000. /*
  1001. * In order to avoid overwritting the superblock on the drive,
  1002. * btrfs starts at an offset of at least 1MB when doing chunk
  1003. * allocation.
  1004. */
  1005. skip_space = 1024 * 1024;
  1006. /* user can set the offset in fs_info->alloc_start. */
  1007. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1008. device->total_bytes)
  1009. skip_space = max(fs_info->alloc_start, skip_space);
  1010. /*
  1011. * btrfs can not use the free space in [0, skip_space - 1],
  1012. * we must subtract it from the total. In order to implement
  1013. * it, we account the used space in this range first.
  1014. */
  1015. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  1016. &used_space);
  1017. if (ret) {
  1018. kfree(devices_info);
  1019. return ret;
  1020. }
  1021. /* calc the free space in [0, skip_space - 1] */
  1022. skip_space -= used_space;
  1023. /*
  1024. * we can use the free space in [0, skip_space - 1], subtract
  1025. * it from the total.
  1026. */
  1027. if (avail_space && avail_space >= skip_space)
  1028. avail_space -= skip_space;
  1029. else
  1030. avail_space = 0;
  1031. if (avail_space < min_stripe_size)
  1032. continue;
  1033. devices_info[i].dev = device;
  1034. devices_info[i].max_avail = avail_space;
  1035. i++;
  1036. }
  1037. nr_devices = i;
  1038. btrfs_descending_sort_devices(devices_info, nr_devices);
  1039. i = nr_devices - 1;
  1040. avail_space = 0;
  1041. while (nr_devices >= min_stripes) {
  1042. if (num_stripes > nr_devices)
  1043. num_stripes = nr_devices;
  1044. if (devices_info[i].max_avail >= min_stripe_size) {
  1045. int j;
  1046. u64 alloc_size;
  1047. avail_space += devices_info[i].max_avail * num_stripes;
  1048. alloc_size = devices_info[i].max_avail;
  1049. for (j = i + 1 - num_stripes; j <= i; j++)
  1050. devices_info[j].max_avail -= alloc_size;
  1051. }
  1052. i--;
  1053. nr_devices--;
  1054. }
  1055. kfree(devices_info);
  1056. *free_bytes = avail_space;
  1057. return 0;
  1058. }
  1059. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1060. {
  1061. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1062. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1063. struct list_head *head = &fs_info->space_info;
  1064. struct btrfs_space_info *found;
  1065. u64 total_used = 0;
  1066. u64 total_free_data = 0;
  1067. int bits = dentry->d_sb->s_blocksize_bits;
  1068. __be32 *fsid = (__be32 *)fs_info->fsid;
  1069. int ret;
  1070. /* holding chunk_muext to avoid allocating new chunks */
  1071. mutex_lock(&fs_info->chunk_mutex);
  1072. rcu_read_lock();
  1073. list_for_each_entry_rcu(found, head, list) {
  1074. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1075. total_free_data += found->disk_total - found->disk_used;
  1076. total_free_data -=
  1077. btrfs_account_ro_block_groups_free_space(found);
  1078. }
  1079. total_used += found->disk_used;
  1080. }
  1081. rcu_read_unlock();
  1082. buf->f_namelen = BTRFS_NAME_LEN;
  1083. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  1084. buf->f_bfree = buf->f_blocks - (total_used >> bits);
  1085. buf->f_bsize = dentry->d_sb->s_blocksize;
  1086. buf->f_type = BTRFS_SUPER_MAGIC;
  1087. buf->f_bavail = total_free_data;
  1088. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1089. if (ret) {
  1090. mutex_unlock(&fs_info->chunk_mutex);
  1091. return ret;
  1092. }
  1093. buf->f_bavail += total_free_data;
  1094. buf->f_bavail = buf->f_bavail >> bits;
  1095. mutex_unlock(&fs_info->chunk_mutex);
  1096. /* We treat it as constant endianness (it doesn't matter _which_)
  1097. because we want the fsid to come out the same whether mounted
  1098. on a big-endian or little-endian host */
  1099. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1100. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1101. /* Mask in the root object ID too, to disambiguate subvols */
  1102. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  1103. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  1104. return 0;
  1105. }
  1106. static void btrfs_kill_super(struct super_block *sb)
  1107. {
  1108. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1109. kill_anon_super(sb);
  1110. free_fs_info(fs_info);
  1111. }
  1112. static struct file_system_type btrfs_fs_type = {
  1113. .owner = THIS_MODULE,
  1114. .name = "btrfs",
  1115. .mount = btrfs_mount,
  1116. .kill_sb = btrfs_kill_super,
  1117. .fs_flags = FS_REQUIRES_DEV,
  1118. };
  1119. /*
  1120. * used by btrfsctl to scan devices when no FS is mounted
  1121. */
  1122. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1123. unsigned long arg)
  1124. {
  1125. struct btrfs_ioctl_vol_args *vol;
  1126. struct btrfs_fs_devices *fs_devices;
  1127. int ret = -ENOTTY;
  1128. if (!capable(CAP_SYS_ADMIN))
  1129. return -EPERM;
  1130. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1131. if (IS_ERR(vol))
  1132. return PTR_ERR(vol);
  1133. switch (cmd) {
  1134. case BTRFS_IOC_SCAN_DEV:
  1135. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1136. &btrfs_fs_type, &fs_devices);
  1137. break;
  1138. }
  1139. kfree(vol);
  1140. return ret;
  1141. }
  1142. static int btrfs_freeze(struct super_block *sb)
  1143. {
  1144. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1145. mutex_lock(&fs_info->transaction_kthread_mutex);
  1146. mutex_lock(&fs_info->cleaner_mutex);
  1147. return 0;
  1148. }
  1149. static int btrfs_unfreeze(struct super_block *sb)
  1150. {
  1151. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1152. mutex_unlock(&fs_info->cleaner_mutex);
  1153. mutex_unlock(&fs_info->transaction_kthread_mutex);
  1154. return 0;
  1155. }
  1156. static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
  1157. {
  1158. int ret;
  1159. ret = btrfs_dirty_inode(inode);
  1160. if (ret)
  1161. printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
  1162. "error %d\n", btrfs_ino(inode), ret);
  1163. }
  1164. static const struct super_operations btrfs_super_ops = {
  1165. .drop_inode = btrfs_drop_inode,
  1166. .evict_inode = btrfs_evict_inode,
  1167. .put_super = btrfs_put_super,
  1168. .sync_fs = btrfs_sync_fs,
  1169. .show_options = btrfs_show_options,
  1170. .write_inode = btrfs_write_inode,
  1171. .dirty_inode = btrfs_fs_dirty_inode,
  1172. .alloc_inode = btrfs_alloc_inode,
  1173. .destroy_inode = btrfs_destroy_inode,
  1174. .statfs = btrfs_statfs,
  1175. .remount_fs = btrfs_remount,
  1176. .freeze_fs = btrfs_freeze,
  1177. .unfreeze_fs = btrfs_unfreeze,
  1178. };
  1179. static const struct file_operations btrfs_ctl_fops = {
  1180. .unlocked_ioctl = btrfs_control_ioctl,
  1181. .compat_ioctl = btrfs_control_ioctl,
  1182. .owner = THIS_MODULE,
  1183. .llseek = noop_llseek,
  1184. };
  1185. static struct miscdevice btrfs_misc = {
  1186. .minor = BTRFS_MINOR,
  1187. .name = "btrfs-control",
  1188. .fops = &btrfs_ctl_fops
  1189. };
  1190. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  1191. MODULE_ALIAS("devname:btrfs-control");
  1192. static int btrfs_interface_init(void)
  1193. {
  1194. return misc_register(&btrfs_misc);
  1195. }
  1196. static void btrfs_interface_exit(void)
  1197. {
  1198. if (misc_deregister(&btrfs_misc) < 0)
  1199. printk(KERN_INFO "misc_deregister failed for control device");
  1200. }
  1201. static int __init init_btrfs_fs(void)
  1202. {
  1203. int err;
  1204. err = btrfs_init_sysfs();
  1205. if (err)
  1206. return err;
  1207. err = btrfs_init_compress();
  1208. if (err)
  1209. goto free_sysfs;
  1210. err = btrfs_init_cachep();
  1211. if (err)
  1212. goto free_compress;
  1213. err = extent_io_init();
  1214. if (err)
  1215. goto free_cachep;
  1216. err = extent_map_init();
  1217. if (err)
  1218. goto free_extent_io;
  1219. err = btrfs_delayed_inode_init();
  1220. if (err)
  1221. goto free_extent_map;
  1222. err = btrfs_interface_init();
  1223. if (err)
  1224. goto free_delayed_inode;
  1225. err = register_filesystem(&btrfs_fs_type);
  1226. if (err)
  1227. goto unregister_ioctl;
  1228. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  1229. return 0;
  1230. unregister_ioctl:
  1231. btrfs_interface_exit();
  1232. free_delayed_inode:
  1233. btrfs_delayed_inode_exit();
  1234. free_extent_map:
  1235. extent_map_exit();
  1236. free_extent_io:
  1237. extent_io_exit();
  1238. free_cachep:
  1239. btrfs_destroy_cachep();
  1240. free_compress:
  1241. btrfs_exit_compress();
  1242. free_sysfs:
  1243. btrfs_exit_sysfs();
  1244. return err;
  1245. }
  1246. static void __exit exit_btrfs_fs(void)
  1247. {
  1248. btrfs_destroy_cachep();
  1249. btrfs_delayed_inode_exit();
  1250. extent_map_exit();
  1251. extent_io_exit();
  1252. btrfs_interface_exit();
  1253. unregister_filesystem(&btrfs_fs_type);
  1254. btrfs_exit_sysfs();
  1255. btrfs_cleanup_fs_uuids();
  1256. btrfs_exit_compress();
  1257. }
  1258. module_init(init_btrfs_fs)
  1259. module_exit(exit_btrfs_fs)
  1260. MODULE_LICENSE("GPL");