super.c 36 KB

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