super.c 35 KB

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