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