super.c 34 KB

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