super.c 31 KB

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