super.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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_wait_ordered_extents(root, 0, 0);
  753. trans = btrfs_start_transaction(root, 0);
  754. if (IS_ERR(trans))
  755. return PTR_ERR(trans);
  756. ret = btrfs_commit_transaction(trans, root);
  757. return ret;
  758. }
  759. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  760. {
  761. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  762. struct btrfs_root *root = info->tree_root;
  763. char *compress_type;
  764. if (btrfs_test_opt(root, DEGRADED))
  765. seq_puts(seq, ",degraded");
  766. if (btrfs_test_opt(root, NODATASUM))
  767. seq_puts(seq, ",nodatasum");
  768. if (btrfs_test_opt(root, NODATACOW))
  769. seq_puts(seq, ",nodatacow");
  770. if (btrfs_test_opt(root, NOBARRIER))
  771. seq_puts(seq, ",nobarrier");
  772. if (info->max_inline != 8192 * 1024)
  773. seq_printf(seq, ",max_inline=%llu",
  774. (unsigned long long)info->max_inline);
  775. if (info->alloc_start != 0)
  776. seq_printf(seq, ",alloc_start=%llu",
  777. (unsigned long long)info->alloc_start);
  778. if (info->thread_pool_size != min_t(unsigned long,
  779. num_online_cpus() + 2, 8))
  780. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  781. if (btrfs_test_opt(root, COMPRESS)) {
  782. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  783. compress_type = "zlib";
  784. else
  785. compress_type = "lzo";
  786. if (btrfs_test_opt(root, FORCE_COMPRESS))
  787. seq_printf(seq, ",compress-force=%s", compress_type);
  788. else
  789. seq_printf(seq, ",compress=%s", compress_type);
  790. }
  791. if (btrfs_test_opt(root, NOSSD))
  792. seq_puts(seq, ",nossd");
  793. if (btrfs_test_opt(root, SSD_SPREAD))
  794. seq_puts(seq, ",ssd_spread");
  795. else if (btrfs_test_opt(root, SSD))
  796. seq_puts(seq, ",ssd");
  797. if (btrfs_test_opt(root, NOTREELOG))
  798. seq_puts(seq, ",notreelog");
  799. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  800. seq_puts(seq, ",flushoncommit");
  801. if (btrfs_test_opt(root, DISCARD))
  802. seq_puts(seq, ",discard");
  803. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  804. seq_puts(seq, ",noacl");
  805. if (btrfs_test_opt(root, SPACE_CACHE))
  806. seq_puts(seq, ",space_cache");
  807. else
  808. seq_puts(seq, ",nospace_cache");
  809. if (btrfs_test_opt(root, CLEAR_CACHE))
  810. seq_puts(seq, ",clear_cache");
  811. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  812. seq_puts(seq, ",user_subvol_rm_allowed");
  813. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  814. seq_puts(seq, ",enospc_debug");
  815. if (btrfs_test_opt(root, AUTO_DEFRAG))
  816. seq_puts(seq, ",autodefrag");
  817. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  818. seq_puts(seq, ",inode_cache");
  819. if (btrfs_test_opt(root, SKIP_BALANCE))
  820. seq_puts(seq, ",skip_balance");
  821. if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
  822. seq_puts(seq, ",fatal_errors=panic");
  823. return 0;
  824. }
  825. static int btrfs_test_super(struct super_block *s, void *data)
  826. {
  827. struct btrfs_fs_info *p = data;
  828. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  829. return fs_info->fs_devices == p->fs_devices;
  830. }
  831. static int btrfs_set_super(struct super_block *s, void *data)
  832. {
  833. int err = set_anon_super(s, data);
  834. if (!err)
  835. s->s_fs_info = data;
  836. return err;
  837. }
  838. /*
  839. * subvolumes are identified by ino 256
  840. */
  841. static inline int is_subvolume_inode(struct inode *inode)
  842. {
  843. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  844. return 1;
  845. return 0;
  846. }
  847. /*
  848. * This will strip out the subvol=%s argument for an argument string and add
  849. * subvolid=0 to make sure we get the actual tree root for path walking to the
  850. * subvol we want.
  851. */
  852. static char *setup_root_args(char *args)
  853. {
  854. unsigned copied = 0;
  855. unsigned len = strlen(args) + 2;
  856. char *pos;
  857. char *ret;
  858. /*
  859. * We need the same args as before, but minus
  860. *
  861. * subvol=a
  862. *
  863. * and add
  864. *
  865. * subvolid=0
  866. *
  867. * which is a difference of 2 characters, so we allocate strlen(args) +
  868. * 2 characters.
  869. */
  870. ret = kzalloc(len * sizeof(char), GFP_NOFS);
  871. if (!ret)
  872. return NULL;
  873. pos = strstr(args, "subvol=");
  874. /* This shouldn't happen, but just in case.. */
  875. if (!pos) {
  876. kfree(ret);
  877. return NULL;
  878. }
  879. /*
  880. * The subvol=<> arg is not at the front of the string, copy everybody
  881. * up to that into ret.
  882. */
  883. if (pos != args) {
  884. *pos = '\0';
  885. strcpy(ret, args);
  886. copied += strlen(args);
  887. pos++;
  888. }
  889. strncpy(ret + copied, "subvolid=0", len - copied);
  890. /* Length of subvolid=0 */
  891. copied += 10;
  892. /*
  893. * If there is no , after the subvol= option then we know there's no
  894. * other options and we can just return.
  895. */
  896. pos = strchr(pos, ',');
  897. if (!pos)
  898. return ret;
  899. /* Copy the rest of the arguments into our buffer */
  900. strncpy(ret + copied, pos, len - copied);
  901. copied += strlen(pos);
  902. return ret;
  903. }
  904. static struct dentry *mount_subvol(const char *subvol_name, int flags,
  905. const char *device_name, char *data)
  906. {
  907. struct dentry *root;
  908. struct vfsmount *mnt;
  909. char *newargs;
  910. newargs = setup_root_args(data);
  911. if (!newargs)
  912. return ERR_PTR(-ENOMEM);
  913. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
  914. newargs);
  915. kfree(newargs);
  916. if (IS_ERR(mnt))
  917. return ERR_CAST(mnt);
  918. root = mount_subtree(mnt, subvol_name);
  919. if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
  920. struct super_block *s = root->d_sb;
  921. dput(root);
  922. root = ERR_PTR(-EINVAL);
  923. deactivate_locked_super(s);
  924. printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
  925. subvol_name);
  926. }
  927. return root;
  928. }
  929. /*
  930. * Find a superblock for the given device / mount point.
  931. *
  932. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  933. * for multiple device setup. Make sure to keep it in sync.
  934. */
  935. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  936. const char *device_name, void *data)
  937. {
  938. struct block_device *bdev = NULL;
  939. struct super_block *s;
  940. struct dentry *root;
  941. struct btrfs_fs_devices *fs_devices = NULL;
  942. struct btrfs_fs_info *fs_info = NULL;
  943. fmode_t mode = FMODE_READ;
  944. char *subvol_name = NULL;
  945. u64 subvol_objectid = 0;
  946. u64 subvol_rootid = 0;
  947. int error = 0;
  948. if (!(flags & MS_RDONLY))
  949. mode |= FMODE_WRITE;
  950. error = btrfs_parse_early_options(data, mode, fs_type,
  951. &subvol_name, &subvol_objectid,
  952. &subvol_rootid, &fs_devices);
  953. if (error) {
  954. kfree(subvol_name);
  955. return ERR_PTR(error);
  956. }
  957. if (subvol_name) {
  958. root = mount_subvol(subvol_name, flags, device_name, data);
  959. kfree(subvol_name);
  960. return root;
  961. }
  962. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  963. if (error)
  964. return ERR_PTR(error);
  965. /*
  966. * Setup a dummy root and fs_info for test/set super. This is because
  967. * we don't actually fill this stuff out until open_ctree, but we need
  968. * it for searching for existing supers, so this lets us do that and
  969. * then open_ctree will properly initialize everything later.
  970. */
  971. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  972. if (!fs_info)
  973. return ERR_PTR(-ENOMEM);
  974. fs_info->fs_devices = fs_devices;
  975. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  976. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  977. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  978. error = -ENOMEM;
  979. goto error_fs_info;
  980. }
  981. error = btrfs_open_devices(fs_devices, mode, fs_type);
  982. if (error)
  983. goto error_fs_info;
  984. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  985. error = -EACCES;
  986. goto error_close_devices;
  987. }
  988. bdev = fs_devices->latest_bdev;
  989. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  990. 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. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  1003. btrfs_sb(s)->bdev_holder = fs_type;
  1004. error = btrfs_fill_super(s, fs_devices, data,
  1005. flags & MS_SILENT ? 1 : 0);
  1006. }
  1007. root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
  1008. if (IS_ERR(root))
  1009. deactivate_locked_super(s);
  1010. return root;
  1011. error_close_devices:
  1012. btrfs_close_devices(fs_devices);
  1013. error_fs_info:
  1014. free_fs_info(fs_info);
  1015. return ERR_PTR(error);
  1016. }
  1017. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1018. {
  1019. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1020. struct btrfs_root *root = fs_info->tree_root;
  1021. unsigned old_flags = sb->s_flags;
  1022. unsigned long old_opts = fs_info->mount_opt;
  1023. unsigned long old_compress_type = fs_info->compress_type;
  1024. u64 old_max_inline = fs_info->max_inline;
  1025. u64 old_alloc_start = fs_info->alloc_start;
  1026. int old_thread_pool_size = fs_info->thread_pool_size;
  1027. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1028. int ret;
  1029. sync_filesystem(sb);
  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. }
  1047. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1048. ret = -EINVAL;
  1049. goto restore;
  1050. }
  1051. ret = btrfs_cleanup_fs_roots(fs_info);
  1052. if (ret)
  1053. goto restore;
  1054. /* recover relocation */
  1055. ret = btrfs_recover_relocation(root);
  1056. if (ret)
  1057. goto restore;
  1058. sb->s_flags &= ~MS_RDONLY;
  1059. }
  1060. return 0;
  1061. restore:
  1062. /* We've hit an error - don't reset MS_RDONLY */
  1063. if (sb->s_flags & MS_RDONLY)
  1064. old_flags |= MS_RDONLY;
  1065. sb->s_flags = old_flags;
  1066. fs_info->mount_opt = old_opts;
  1067. fs_info->compress_type = old_compress_type;
  1068. fs_info->max_inline = old_max_inline;
  1069. fs_info->alloc_start = old_alloc_start;
  1070. fs_info->thread_pool_size = old_thread_pool_size;
  1071. fs_info->metadata_ratio = old_metadata_ratio;
  1072. return ret;
  1073. }
  1074. /* Used to sort the devices by max_avail(descending sort) */
  1075. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1076. const void *dev_info2)
  1077. {
  1078. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1079. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1080. return -1;
  1081. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1082. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1083. return 1;
  1084. else
  1085. return 0;
  1086. }
  1087. /*
  1088. * sort the devices by max_avail, in which max free extent size of each device
  1089. * is stored.(Descending Sort)
  1090. */
  1091. static inline void btrfs_descending_sort_devices(
  1092. struct btrfs_device_info *devices,
  1093. size_t nr_devices)
  1094. {
  1095. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1096. btrfs_cmp_device_free_bytes, NULL);
  1097. }
  1098. /*
  1099. * The helper to calc the free space on the devices that can be used to store
  1100. * file data.
  1101. */
  1102. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  1103. {
  1104. struct btrfs_fs_info *fs_info = root->fs_info;
  1105. struct btrfs_device_info *devices_info;
  1106. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1107. struct btrfs_device *device;
  1108. u64 skip_space;
  1109. u64 type;
  1110. u64 avail_space;
  1111. u64 used_space;
  1112. u64 min_stripe_size;
  1113. int min_stripes = 1, num_stripes = 1;
  1114. int i = 0, nr_devices;
  1115. int ret;
  1116. nr_devices = fs_info->fs_devices->open_devices;
  1117. BUG_ON(!nr_devices);
  1118. devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
  1119. GFP_NOFS);
  1120. if (!devices_info)
  1121. return -ENOMEM;
  1122. /* calc min stripe number for data space alloction */
  1123. type = btrfs_get_alloc_profile(root, 1);
  1124. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1125. min_stripes = 2;
  1126. num_stripes = nr_devices;
  1127. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1128. min_stripes = 2;
  1129. num_stripes = 2;
  1130. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1131. min_stripes = 4;
  1132. num_stripes = 4;
  1133. }
  1134. if (type & BTRFS_BLOCK_GROUP_DUP)
  1135. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1136. else
  1137. min_stripe_size = BTRFS_STRIPE_LEN;
  1138. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  1139. if (!device->in_fs_metadata || !device->bdev)
  1140. continue;
  1141. avail_space = device->total_bytes - device->bytes_used;
  1142. /* align with stripe_len */
  1143. do_div(avail_space, BTRFS_STRIPE_LEN);
  1144. avail_space *= BTRFS_STRIPE_LEN;
  1145. /*
  1146. * In order to avoid overwritting the superblock on the drive,
  1147. * btrfs starts at an offset of at least 1MB when doing chunk
  1148. * allocation.
  1149. */
  1150. skip_space = 1024 * 1024;
  1151. /* user can set the offset in fs_info->alloc_start. */
  1152. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1153. device->total_bytes)
  1154. skip_space = max(fs_info->alloc_start, skip_space);
  1155. /*
  1156. * btrfs can not use the free space in [0, skip_space - 1],
  1157. * we must subtract it from the total. In order to implement
  1158. * it, we account the used space in this range first.
  1159. */
  1160. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  1161. &used_space);
  1162. if (ret) {
  1163. kfree(devices_info);
  1164. return ret;
  1165. }
  1166. /* calc the free space in [0, skip_space - 1] */
  1167. skip_space -= used_space;
  1168. /*
  1169. * we can use the free space in [0, skip_space - 1], subtract
  1170. * it from the total.
  1171. */
  1172. if (avail_space && avail_space >= skip_space)
  1173. avail_space -= skip_space;
  1174. else
  1175. avail_space = 0;
  1176. if (avail_space < min_stripe_size)
  1177. continue;
  1178. devices_info[i].dev = device;
  1179. devices_info[i].max_avail = avail_space;
  1180. i++;
  1181. }
  1182. nr_devices = i;
  1183. btrfs_descending_sort_devices(devices_info, nr_devices);
  1184. i = nr_devices - 1;
  1185. avail_space = 0;
  1186. while (nr_devices >= min_stripes) {
  1187. if (num_stripes > nr_devices)
  1188. num_stripes = nr_devices;
  1189. if (devices_info[i].max_avail >= min_stripe_size) {
  1190. int j;
  1191. u64 alloc_size;
  1192. avail_space += devices_info[i].max_avail * num_stripes;
  1193. alloc_size = devices_info[i].max_avail;
  1194. for (j = i + 1 - num_stripes; j <= i; j++)
  1195. devices_info[j].max_avail -= alloc_size;
  1196. }
  1197. i--;
  1198. nr_devices--;
  1199. }
  1200. kfree(devices_info);
  1201. *free_bytes = avail_space;
  1202. return 0;
  1203. }
  1204. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1205. {
  1206. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1207. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1208. struct list_head *head = &fs_info->space_info;
  1209. struct btrfs_space_info *found;
  1210. u64 total_used = 0;
  1211. u64 total_free_data = 0;
  1212. int bits = dentry->d_sb->s_blocksize_bits;
  1213. __be32 *fsid = (__be32 *)fs_info->fsid;
  1214. int ret;
  1215. /* holding chunk_muext to avoid allocating new chunks */
  1216. mutex_lock(&fs_info->chunk_mutex);
  1217. rcu_read_lock();
  1218. list_for_each_entry_rcu(found, head, list) {
  1219. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1220. total_free_data += found->disk_total - found->disk_used;
  1221. total_free_data -=
  1222. btrfs_account_ro_block_groups_free_space(found);
  1223. }
  1224. total_used += found->disk_used;
  1225. }
  1226. rcu_read_unlock();
  1227. buf->f_namelen = BTRFS_NAME_LEN;
  1228. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  1229. buf->f_bfree = buf->f_blocks - (total_used >> bits);
  1230. buf->f_bsize = dentry->d_sb->s_blocksize;
  1231. buf->f_type = BTRFS_SUPER_MAGIC;
  1232. buf->f_bavail = total_free_data;
  1233. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1234. if (ret) {
  1235. mutex_unlock(&fs_info->chunk_mutex);
  1236. return ret;
  1237. }
  1238. buf->f_bavail += total_free_data;
  1239. buf->f_bavail = buf->f_bavail >> bits;
  1240. mutex_unlock(&fs_info->chunk_mutex);
  1241. /* We treat it as constant endianness (it doesn't matter _which_)
  1242. because we want the fsid to come out the same whether mounted
  1243. on a big-endian or little-endian host */
  1244. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1245. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1246. /* Mask in the root object ID too, to disambiguate subvols */
  1247. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  1248. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  1249. return 0;
  1250. }
  1251. static void btrfs_kill_super(struct super_block *sb)
  1252. {
  1253. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1254. kill_anon_super(sb);
  1255. free_fs_info(fs_info);
  1256. }
  1257. static struct file_system_type btrfs_fs_type = {
  1258. .owner = THIS_MODULE,
  1259. .name = "btrfs",
  1260. .mount = btrfs_mount,
  1261. .kill_sb = btrfs_kill_super,
  1262. .fs_flags = FS_REQUIRES_DEV,
  1263. };
  1264. MODULE_ALIAS_FS("btrfs");
  1265. /*
  1266. * used by btrfsctl to scan devices when no FS is mounted
  1267. */
  1268. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1269. unsigned long arg)
  1270. {
  1271. struct btrfs_ioctl_vol_args *vol;
  1272. struct btrfs_fs_devices *fs_devices;
  1273. int ret = -ENOTTY;
  1274. if (!capable(CAP_SYS_ADMIN))
  1275. return -EPERM;
  1276. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1277. if (IS_ERR(vol))
  1278. return PTR_ERR(vol);
  1279. switch (cmd) {
  1280. case BTRFS_IOC_SCAN_DEV:
  1281. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1282. &btrfs_fs_type, &fs_devices);
  1283. break;
  1284. }
  1285. kfree(vol);
  1286. return ret;
  1287. }
  1288. static int btrfs_freeze(struct super_block *sb)
  1289. {
  1290. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1291. mutex_lock(&fs_info->transaction_kthread_mutex);
  1292. mutex_lock(&fs_info->cleaner_mutex);
  1293. return 0;
  1294. }
  1295. static int btrfs_unfreeze(struct super_block *sb)
  1296. {
  1297. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1298. mutex_unlock(&fs_info->cleaner_mutex);
  1299. mutex_unlock(&fs_info->transaction_kthread_mutex);
  1300. return 0;
  1301. }
  1302. static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
  1303. {
  1304. int ret;
  1305. ret = btrfs_dirty_inode(inode);
  1306. if (ret)
  1307. printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
  1308. "error %d\n", btrfs_ino(inode), ret);
  1309. }
  1310. static const struct super_operations btrfs_super_ops = {
  1311. .drop_inode = btrfs_drop_inode,
  1312. .evict_inode = btrfs_evict_inode,
  1313. .put_super = btrfs_put_super,
  1314. .sync_fs = btrfs_sync_fs,
  1315. .show_options = btrfs_show_options,
  1316. .write_inode = btrfs_write_inode,
  1317. .dirty_inode = btrfs_fs_dirty_inode,
  1318. .alloc_inode = btrfs_alloc_inode,
  1319. .destroy_inode = btrfs_destroy_inode,
  1320. .statfs = btrfs_statfs,
  1321. .remount_fs = btrfs_remount,
  1322. .freeze_fs = btrfs_freeze,
  1323. .unfreeze_fs = btrfs_unfreeze,
  1324. };
  1325. static const struct file_operations btrfs_ctl_fops = {
  1326. .unlocked_ioctl = btrfs_control_ioctl,
  1327. .compat_ioctl = btrfs_control_ioctl,
  1328. .owner = THIS_MODULE,
  1329. .llseek = noop_llseek,
  1330. };
  1331. static struct miscdevice btrfs_misc = {
  1332. .minor = BTRFS_MINOR,
  1333. .name = "btrfs-control",
  1334. .fops = &btrfs_ctl_fops
  1335. };
  1336. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  1337. MODULE_ALIAS("devname:btrfs-control");
  1338. static int btrfs_interface_init(void)
  1339. {
  1340. return misc_register(&btrfs_misc);
  1341. }
  1342. static void btrfs_interface_exit(void)
  1343. {
  1344. if (misc_deregister(&btrfs_misc) < 0)
  1345. printk(KERN_INFO "misc_deregister failed for control device");
  1346. }
  1347. static int __init init_btrfs_fs(void)
  1348. {
  1349. int err;
  1350. err = btrfs_init_sysfs();
  1351. if (err)
  1352. return err;
  1353. btrfs_init_compress();
  1354. err = btrfs_init_cachep();
  1355. if (err)
  1356. goto free_compress;
  1357. err = extent_io_init();
  1358. if (err)
  1359. goto free_cachep;
  1360. err = extent_map_init();
  1361. if (err)
  1362. goto free_extent_io;
  1363. err = btrfs_delayed_inode_init();
  1364. if (err)
  1365. goto free_extent_map;
  1366. err = btrfs_interface_init();
  1367. if (err)
  1368. goto free_delayed_inode;
  1369. err = register_filesystem(&btrfs_fs_type);
  1370. if (err)
  1371. goto unregister_ioctl;
  1372. btrfs_init_lockdep();
  1373. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  1374. return 0;
  1375. unregister_ioctl:
  1376. btrfs_interface_exit();
  1377. free_delayed_inode:
  1378. btrfs_delayed_inode_exit();
  1379. free_extent_map:
  1380. extent_map_exit();
  1381. free_extent_io:
  1382. extent_io_exit();
  1383. free_cachep:
  1384. btrfs_destroy_cachep();
  1385. free_compress:
  1386. btrfs_exit_compress();
  1387. btrfs_exit_sysfs();
  1388. return err;
  1389. }
  1390. static void __exit exit_btrfs_fs(void)
  1391. {
  1392. btrfs_destroy_cachep();
  1393. btrfs_delayed_inode_exit();
  1394. extent_map_exit();
  1395. extent_io_exit();
  1396. btrfs_interface_exit();
  1397. unregister_filesystem(&btrfs_fs_type);
  1398. btrfs_exit_sysfs();
  1399. btrfs_cleanup_fs_uuids();
  1400. btrfs_exit_compress();
  1401. }
  1402. module_init(init_btrfs_fs)
  1403. module_exit(exit_btrfs_fs)
  1404. MODULE_LICENSE("GPL");