super.c 36 KB

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