super.c 35 KB

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