super.c 37 KB

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