super.c 35 KB

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