super.c 40 KB

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