super.c 30 KB

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