super.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. if (btrfs_test_opt(root, DEGRADED))
  586. seq_puts(seq, ",degraded");
  587. if (btrfs_test_opt(root, NODATASUM))
  588. seq_puts(seq, ",nodatasum");
  589. if (btrfs_test_opt(root, NODATACOW))
  590. seq_puts(seq, ",nodatacow");
  591. if (btrfs_test_opt(root, NOBARRIER))
  592. seq_puts(seq, ",nobarrier");
  593. if (info->max_inline != 8192 * 1024)
  594. seq_printf(seq, ",max_inline=%llu",
  595. (unsigned long long)info->max_inline);
  596. if (info->alloc_start != 0)
  597. seq_printf(seq, ",alloc_start=%llu",
  598. (unsigned long long)info->alloc_start);
  599. if (info->thread_pool_size != min_t(unsigned long,
  600. num_online_cpus() + 2, 8))
  601. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  602. if (btrfs_test_opt(root, COMPRESS))
  603. seq_puts(seq, ",compress");
  604. if (btrfs_test_opt(root, NOSSD))
  605. seq_puts(seq, ",nossd");
  606. if (btrfs_test_opt(root, SSD_SPREAD))
  607. seq_puts(seq, ",ssd_spread");
  608. else if (btrfs_test_opt(root, SSD))
  609. seq_puts(seq, ",ssd");
  610. if (btrfs_test_opt(root, NOTREELOG))
  611. seq_puts(seq, ",notreelog");
  612. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  613. seq_puts(seq, ",flushoncommit");
  614. if (btrfs_test_opt(root, DISCARD))
  615. seq_puts(seq, ",discard");
  616. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  617. seq_puts(seq, ",noacl");
  618. return 0;
  619. }
  620. static int btrfs_test_super(struct super_block *s, void *data)
  621. {
  622. struct btrfs_root *test_root = data;
  623. struct btrfs_root *root = btrfs_sb(s);
  624. /*
  625. * If this super block is going away, return false as it
  626. * can't match as an existing super block.
  627. */
  628. if (!atomic_read(&s->s_active))
  629. return 0;
  630. return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
  631. }
  632. static int btrfs_set_super(struct super_block *s, void *data)
  633. {
  634. s->s_fs_info = data;
  635. return set_anon_super(s, data);
  636. }
  637. /*
  638. * Find a superblock for the given device / mount point.
  639. *
  640. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  641. * for multiple device setup. Make sure to keep it in sync.
  642. */
  643. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  644. const char *dev_name, void *data)
  645. {
  646. struct block_device *bdev = NULL;
  647. struct super_block *s;
  648. struct dentry *root;
  649. struct btrfs_fs_devices *fs_devices = NULL;
  650. struct btrfs_root *tree_root = NULL;
  651. struct btrfs_fs_info *fs_info = NULL;
  652. fmode_t mode = FMODE_READ;
  653. char *subvol_name = NULL;
  654. u64 subvol_objectid = 0;
  655. int error = 0;
  656. if (!(flags & MS_RDONLY))
  657. mode |= FMODE_WRITE;
  658. error = btrfs_parse_early_options(data, mode, fs_type,
  659. &subvol_name, &subvol_objectid,
  660. &fs_devices);
  661. if (error)
  662. return ERR_PTR(error);
  663. error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
  664. if (error)
  665. goto error_free_subvol_name;
  666. error = btrfs_open_devices(fs_devices, mode, fs_type);
  667. if (error)
  668. goto error_free_subvol_name;
  669. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  670. error = -EACCES;
  671. goto error_close_devices;
  672. }
  673. /*
  674. * Setup a dummy root and fs_info for test/set super. This is because
  675. * we don't actually fill this stuff out until open_ctree, but we need
  676. * it for searching for existing supers, so this lets us do that and
  677. * then open_ctree will properly initialize everything later.
  678. */
  679. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  680. tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
  681. if (!fs_info || !tree_root) {
  682. error = -ENOMEM;
  683. goto error_close_devices;
  684. }
  685. fs_info->tree_root = tree_root;
  686. fs_info->fs_devices = fs_devices;
  687. tree_root->fs_info = fs_info;
  688. bdev = fs_devices->latest_bdev;
  689. s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
  690. if (IS_ERR(s))
  691. goto error_s;
  692. if (s->s_root) {
  693. if ((flags ^ s->s_flags) & MS_RDONLY) {
  694. deactivate_locked_super(s);
  695. error = -EBUSY;
  696. goto error_close_devices;
  697. }
  698. btrfs_close_devices(fs_devices);
  699. kfree(fs_info);
  700. kfree(tree_root);
  701. } else {
  702. char b[BDEVNAME_SIZE];
  703. s->s_flags = flags;
  704. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  705. error = btrfs_fill_super(s, fs_devices, data,
  706. flags & MS_SILENT ? 1 : 0);
  707. if (error) {
  708. deactivate_locked_super(s);
  709. goto error_free_subvol_name;
  710. }
  711. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  712. s->s_flags |= MS_ACTIVE;
  713. }
  714. root = get_default_root(s, subvol_objectid);
  715. if (IS_ERR(root)) {
  716. error = PTR_ERR(root);
  717. deactivate_locked_super(s);
  718. goto error_free_subvol_name;
  719. }
  720. /* if they gave us a subvolume name bind mount into that */
  721. if (strcmp(subvol_name, ".")) {
  722. struct dentry *new_root;
  723. mutex_lock(&root->d_inode->i_mutex);
  724. new_root = lookup_one_len(subvol_name, root,
  725. strlen(subvol_name));
  726. mutex_unlock(&root->d_inode->i_mutex);
  727. if (IS_ERR(new_root)) {
  728. dput(root);
  729. deactivate_locked_super(s);
  730. error = PTR_ERR(new_root);
  731. goto error_free_subvol_name;
  732. }
  733. if (!new_root->d_inode) {
  734. dput(root);
  735. dput(new_root);
  736. deactivate_locked_super(s);
  737. error = -ENXIO;
  738. goto error_free_subvol_name;
  739. }
  740. dput(root);
  741. root = new_root;
  742. }
  743. kfree(subvol_name);
  744. return root;
  745. error_s:
  746. error = PTR_ERR(s);
  747. error_close_devices:
  748. btrfs_close_devices(fs_devices);
  749. kfree(fs_info);
  750. kfree(tree_root);
  751. error_free_subvol_name:
  752. kfree(subvol_name);
  753. return ERR_PTR(error);
  754. }
  755. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  756. {
  757. struct btrfs_root *root = btrfs_sb(sb);
  758. int ret;
  759. ret = btrfs_parse_options(root, data);
  760. if (ret)
  761. return -EINVAL;
  762. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  763. return 0;
  764. if (*flags & MS_RDONLY) {
  765. sb->s_flags |= MS_RDONLY;
  766. ret = btrfs_commit_super(root);
  767. WARN_ON(ret);
  768. } else {
  769. if (root->fs_info->fs_devices->rw_devices == 0)
  770. return -EACCES;
  771. if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
  772. return -EINVAL;
  773. ret = btrfs_cleanup_fs_roots(root->fs_info);
  774. WARN_ON(ret);
  775. /* recover relocation */
  776. ret = btrfs_recover_relocation(root);
  777. WARN_ON(ret);
  778. sb->s_flags &= ~MS_RDONLY;
  779. }
  780. return 0;
  781. }
  782. /*
  783. * The helper to calc the free space on the devices that can be used to store
  784. * file data.
  785. */
  786. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  787. {
  788. struct btrfs_fs_info *fs_info = root->fs_info;
  789. struct btrfs_device_info *devices_info;
  790. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  791. struct btrfs_device *device;
  792. u64 skip_space;
  793. u64 type;
  794. u64 avail_space;
  795. u64 used_space;
  796. u64 min_stripe_size;
  797. int min_stripes = 1;
  798. int i = 0, nr_devices;
  799. int ret;
  800. nr_devices = fs_info->fs_devices->rw_devices;
  801. BUG_ON(!nr_devices);
  802. devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
  803. GFP_NOFS);
  804. if (!devices_info)
  805. return -ENOMEM;
  806. /* calc min stripe number for data space alloction */
  807. type = btrfs_get_alloc_profile(root, 1);
  808. if (type & BTRFS_BLOCK_GROUP_RAID0)
  809. min_stripes = 2;
  810. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  811. min_stripes = 2;
  812. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  813. min_stripes = 4;
  814. if (type & BTRFS_BLOCK_GROUP_DUP)
  815. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  816. else
  817. min_stripe_size = BTRFS_STRIPE_LEN;
  818. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  819. if (!device->in_fs_metadata)
  820. continue;
  821. avail_space = device->total_bytes - device->bytes_used;
  822. /* align with stripe_len */
  823. do_div(avail_space, BTRFS_STRIPE_LEN);
  824. avail_space *= BTRFS_STRIPE_LEN;
  825. /*
  826. * In order to avoid overwritting the superblock on the drive,
  827. * btrfs starts at an offset of at least 1MB when doing chunk
  828. * allocation.
  829. */
  830. skip_space = 1024 * 1024;
  831. /* user can set the offset in fs_info->alloc_start. */
  832. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  833. device->total_bytes)
  834. skip_space = max(fs_info->alloc_start, skip_space);
  835. /*
  836. * btrfs can not use the free space in [0, skip_space - 1],
  837. * we must subtract it from the total. In order to implement
  838. * it, we account the used space in this range first.
  839. */
  840. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  841. &used_space);
  842. if (ret) {
  843. kfree(devices_info);
  844. return ret;
  845. }
  846. /* calc the free space in [0, skip_space - 1] */
  847. skip_space -= used_space;
  848. /*
  849. * we can use the free space in [0, skip_space - 1], subtract
  850. * it from the total.
  851. */
  852. if (avail_space && avail_space >= skip_space)
  853. avail_space -= skip_space;
  854. else
  855. avail_space = 0;
  856. if (avail_space < min_stripe_size)
  857. continue;
  858. devices_info[i].dev = device;
  859. devices_info[i].max_avail = avail_space;
  860. i++;
  861. }
  862. nr_devices = i;
  863. btrfs_descending_sort_devices(devices_info, nr_devices);
  864. i = nr_devices - 1;
  865. avail_space = 0;
  866. while (nr_devices >= min_stripes) {
  867. if (devices_info[i].max_avail >= min_stripe_size) {
  868. int j;
  869. u64 alloc_size;
  870. avail_space += devices_info[i].max_avail * min_stripes;
  871. alloc_size = devices_info[i].max_avail;
  872. for (j = i + 1 - min_stripes; j <= i; j++)
  873. devices_info[j].max_avail -= alloc_size;
  874. }
  875. i--;
  876. nr_devices--;
  877. }
  878. kfree(devices_info);
  879. *free_bytes = avail_space;
  880. return 0;
  881. }
  882. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  883. {
  884. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  885. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  886. struct list_head *head = &root->fs_info->space_info;
  887. struct btrfs_space_info *found;
  888. u64 total_used = 0;
  889. u64 total_free_data = 0;
  890. int bits = dentry->d_sb->s_blocksize_bits;
  891. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  892. int ret;
  893. /* holding chunk_muext to avoid allocating new chunks */
  894. mutex_lock(&root->fs_info->chunk_mutex);
  895. rcu_read_lock();
  896. list_for_each_entry_rcu(found, head, list) {
  897. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  898. total_free_data += found->disk_total - found->disk_used;
  899. total_free_data -=
  900. btrfs_account_ro_block_groups_free_space(found);
  901. }
  902. total_used += found->disk_used;
  903. }
  904. rcu_read_unlock();
  905. buf->f_namelen = BTRFS_NAME_LEN;
  906. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  907. buf->f_bfree = buf->f_blocks - (total_used >> bits);
  908. buf->f_bsize = dentry->d_sb->s_blocksize;
  909. buf->f_type = BTRFS_SUPER_MAGIC;
  910. buf->f_bavail = total_free_data;
  911. ret = btrfs_calc_avail_data_space(root, &total_free_data);
  912. if (ret) {
  913. mutex_unlock(&root->fs_info->chunk_mutex);
  914. return ret;
  915. }
  916. buf->f_bavail += total_free_data;
  917. buf->f_bavail = buf->f_bavail >> bits;
  918. mutex_unlock(&root->fs_info->chunk_mutex);
  919. /* We treat it as constant endianness (it doesn't matter _which_)
  920. because we want the fsid to come out the same whether mounted
  921. on a big-endian or little-endian host */
  922. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  923. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  924. /* Mask in the root object ID too, to disambiguate subvols */
  925. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  926. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  927. return 0;
  928. }
  929. static struct file_system_type btrfs_fs_type = {
  930. .owner = THIS_MODULE,
  931. .name = "btrfs",
  932. .mount = btrfs_mount,
  933. .kill_sb = kill_anon_super,
  934. .fs_flags = FS_REQUIRES_DEV,
  935. };
  936. /*
  937. * used by btrfsctl to scan devices when no FS is mounted
  938. */
  939. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  940. unsigned long arg)
  941. {
  942. struct btrfs_ioctl_vol_args *vol;
  943. struct btrfs_fs_devices *fs_devices;
  944. int ret = -ENOTTY;
  945. if (!capable(CAP_SYS_ADMIN))
  946. return -EPERM;
  947. vol = memdup_user((void __user *)arg, sizeof(*vol));
  948. if (IS_ERR(vol))
  949. return PTR_ERR(vol);
  950. switch (cmd) {
  951. case BTRFS_IOC_SCAN_DEV:
  952. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  953. &btrfs_fs_type, &fs_devices);
  954. break;
  955. }
  956. kfree(vol);
  957. return ret;
  958. }
  959. static int btrfs_freeze(struct super_block *sb)
  960. {
  961. struct btrfs_root *root = btrfs_sb(sb);
  962. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  963. mutex_lock(&root->fs_info->cleaner_mutex);
  964. return 0;
  965. }
  966. static int btrfs_unfreeze(struct super_block *sb)
  967. {
  968. struct btrfs_root *root = btrfs_sb(sb);
  969. mutex_unlock(&root->fs_info->cleaner_mutex);
  970. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  971. return 0;
  972. }
  973. static const struct super_operations btrfs_super_ops = {
  974. .drop_inode = btrfs_drop_inode,
  975. .evict_inode = btrfs_evict_inode,
  976. .put_super = btrfs_put_super,
  977. .sync_fs = btrfs_sync_fs,
  978. .show_options = btrfs_show_options,
  979. .write_inode = btrfs_write_inode,
  980. .dirty_inode = btrfs_dirty_inode,
  981. .alloc_inode = btrfs_alloc_inode,
  982. .destroy_inode = btrfs_destroy_inode,
  983. .statfs = btrfs_statfs,
  984. .remount_fs = btrfs_remount,
  985. .freeze_fs = btrfs_freeze,
  986. .unfreeze_fs = btrfs_unfreeze,
  987. };
  988. static const struct file_operations btrfs_ctl_fops = {
  989. .unlocked_ioctl = btrfs_control_ioctl,
  990. .compat_ioctl = btrfs_control_ioctl,
  991. .owner = THIS_MODULE,
  992. .llseek = noop_llseek,
  993. };
  994. static struct miscdevice btrfs_misc = {
  995. .minor = BTRFS_MINOR,
  996. .name = "btrfs-control",
  997. .fops = &btrfs_ctl_fops
  998. };
  999. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  1000. MODULE_ALIAS("devname:btrfs-control");
  1001. static int btrfs_interface_init(void)
  1002. {
  1003. return misc_register(&btrfs_misc);
  1004. }
  1005. static void btrfs_interface_exit(void)
  1006. {
  1007. if (misc_deregister(&btrfs_misc) < 0)
  1008. printk(KERN_INFO "misc_deregister failed for control device");
  1009. }
  1010. static int __init init_btrfs_fs(void)
  1011. {
  1012. int err;
  1013. err = btrfs_init_sysfs();
  1014. if (err)
  1015. return err;
  1016. err = btrfs_init_compress();
  1017. if (err)
  1018. goto free_sysfs;
  1019. err = btrfs_init_cachep();
  1020. if (err)
  1021. goto free_compress;
  1022. err = extent_io_init();
  1023. if (err)
  1024. goto free_cachep;
  1025. err = extent_map_init();
  1026. if (err)
  1027. goto free_extent_io;
  1028. err = btrfs_interface_init();
  1029. if (err)
  1030. goto free_extent_map;
  1031. err = register_filesystem(&btrfs_fs_type);
  1032. if (err)
  1033. goto unregister_ioctl;
  1034. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  1035. return 0;
  1036. unregister_ioctl:
  1037. btrfs_interface_exit();
  1038. free_extent_map:
  1039. extent_map_exit();
  1040. free_extent_io:
  1041. extent_io_exit();
  1042. free_cachep:
  1043. btrfs_destroy_cachep();
  1044. free_compress:
  1045. btrfs_exit_compress();
  1046. free_sysfs:
  1047. btrfs_exit_sysfs();
  1048. return err;
  1049. }
  1050. static void __exit exit_btrfs_fs(void)
  1051. {
  1052. btrfs_destroy_cachep();
  1053. extent_map_exit();
  1054. extent_io_exit();
  1055. btrfs_interface_exit();
  1056. unregister_filesystem(&btrfs_fs_type);
  1057. btrfs_exit_sysfs();
  1058. btrfs_cleanup_fs_uuids();
  1059. btrfs_exit_compress();
  1060. }
  1061. module_init(init_btrfs_fs)
  1062. module_exit(exit_btrfs_fs)
  1063. MODULE_LICENSE("GPL");