super.c 35 KB

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