super.c 35 KB

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