super.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  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 <linux/btrfs.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 "print-tree.h"
  50. #include "hash.h"
  51. #include "props.h"
  52. #include "xattr.h"
  53. #include "volumes.h"
  54. #include "export.h"
  55. #include "compression.h"
  56. #include "rcu-string.h"
  57. #include "dev-replace.h"
  58. #include "free-space-cache.h"
  59. #include "backref.h"
  60. #include "tests/btrfs-tests.h"
  61. #define CREATE_TRACE_POINTS
  62. #include <trace/events/btrfs.h>
  63. static const struct super_operations btrfs_super_ops;
  64. static struct file_system_type btrfs_fs_type;
  65. static int btrfs_remount(struct super_block *sb, int *flags, char *data);
  66. static const char *btrfs_decode_error(int errno)
  67. {
  68. char *errstr = "unknown";
  69. switch (errno) {
  70. case -EIO:
  71. errstr = "IO failure";
  72. break;
  73. case -ENOMEM:
  74. errstr = "Out of memory";
  75. break;
  76. case -EROFS:
  77. errstr = "Readonly filesystem";
  78. break;
  79. case -EEXIST:
  80. errstr = "Object already exists";
  81. break;
  82. case -ENOSPC:
  83. errstr = "No space left";
  84. break;
  85. case -ENOENT:
  86. errstr = "No such entry";
  87. break;
  88. }
  89. return errstr;
  90. }
  91. static void save_error_info(struct btrfs_fs_info *fs_info)
  92. {
  93. /*
  94. * today we only save the error info into ram. Long term we'll
  95. * also send it down to the disk
  96. */
  97. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  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 (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  106. sb->s_flags |= MS_RDONLY;
  107. btrfs_info(fs_info, "forced readonly");
  108. /*
  109. * Note that a running device replace operation is not
  110. * canceled here although there is no way to update
  111. * the progress. It would add the risk of a deadlock,
  112. * therefore the canceling is ommited. The only penalty
  113. * is that some I/O remains active until the procedure
  114. * completes. The next time when the filesystem is
  115. * mounted writeable again, the device replace
  116. * operation continues.
  117. */
  118. }
  119. }
  120. #ifdef CONFIG_PRINTK
  121. /*
  122. * __btrfs_std_error decodes expected errors from the caller and
  123. * invokes the approciate error response.
  124. */
  125. void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
  126. unsigned int line, int errno, const char *fmt, ...)
  127. {
  128. struct super_block *sb = fs_info->sb;
  129. const char *errstr;
  130. /*
  131. * Special case: if the error is EROFS, and we're already
  132. * under MS_RDONLY, then it is safe here.
  133. */
  134. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  135. return;
  136. errstr = btrfs_decode_error(errno);
  137. if (fmt) {
  138. struct va_format vaf;
  139. va_list args;
  140. va_start(args, fmt);
  141. vaf.fmt = fmt;
  142. vaf.va = &args;
  143. printk(KERN_CRIT
  144. "BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
  145. sb->s_id, function, line, errno, errstr, &vaf);
  146. va_end(args);
  147. } else {
  148. printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
  149. sb->s_id, function, line, errno, errstr);
  150. }
  151. /* Don't go through full error handling during mount */
  152. save_error_info(fs_info);
  153. if (sb->s_flags & MS_BORN)
  154. btrfs_handle_error(fs_info);
  155. }
  156. static const char * const logtypes[] = {
  157. "emergency",
  158. "alert",
  159. "critical",
  160. "error",
  161. "warning",
  162. "notice",
  163. "info",
  164. "debug",
  165. };
  166. void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
  167. {
  168. struct super_block *sb = fs_info->sb;
  169. char lvl[4];
  170. struct va_format vaf;
  171. va_list args;
  172. const char *type = logtypes[4];
  173. int kern_level;
  174. va_start(args, fmt);
  175. kern_level = printk_get_level(fmt);
  176. if (kern_level) {
  177. size_t size = printk_skip_level(fmt) - fmt;
  178. memcpy(lvl, fmt, size);
  179. lvl[size] = '\0';
  180. fmt += size;
  181. type = logtypes[kern_level - '0'];
  182. } else
  183. *lvl = '\0';
  184. vaf.fmt = fmt;
  185. vaf.va = &args;
  186. printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
  187. va_end(args);
  188. }
  189. #else
  190. void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
  191. unsigned int line, int errno, const char *fmt, ...)
  192. {
  193. struct super_block *sb = fs_info->sb;
  194. /*
  195. * Special case: if the error is EROFS, and we're already
  196. * under MS_RDONLY, then it is safe here.
  197. */
  198. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  199. return;
  200. /* Don't go through full error handling during mount */
  201. if (sb->s_flags & MS_BORN) {
  202. save_error_info(fs_info);
  203. btrfs_handle_error(fs_info);
  204. }
  205. }
  206. #endif
  207. /*
  208. * We only mark the transaction aborted and then set the file system read-only.
  209. * This will prevent new transactions from starting or trying to join this
  210. * one.
  211. *
  212. * This means that error recovery at the call site is limited to freeing
  213. * any local memory allocations and passing the error code up without
  214. * further cleanup. The transaction should complete as it normally would
  215. * in the call path but will return -EIO.
  216. *
  217. * We'll complete the cleanup in btrfs_end_transaction and
  218. * btrfs_commit_transaction.
  219. */
  220. void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  221. struct btrfs_root *root, const char *function,
  222. unsigned int line, int errno)
  223. {
  224. /*
  225. * Report first abort since mount
  226. */
  227. if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
  228. &root->fs_info->fs_state)) {
  229. WARN(1, KERN_DEBUG "BTRFS: Transaction aborted (error %d)\n",
  230. errno);
  231. }
  232. trans->aborted = errno;
  233. /* Nothing used. The other threads that have joined this
  234. * transaction may be able to continue. */
  235. if (!trans->blocks_used) {
  236. const char *errstr;
  237. errstr = btrfs_decode_error(errno);
  238. btrfs_warn(root->fs_info,
  239. "%s:%d: Aborting unused transaction(%s).",
  240. function, line, errstr);
  241. return;
  242. }
  243. ACCESS_ONCE(trans->transaction->aborted) = errno;
  244. /* Wake up anybody who may be waiting on this transaction */
  245. wake_up(&root->fs_info->transaction_wait);
  246. wake_up(&root->fs_info->transaction_blocked_wait);
  247. __btrfs_std_error(root->fs_info, function, line, errno, NULL);
  248. }
  249. /*
  250. * __btrfs_panic decodes unexpected, fatal errors from the caller,
  251. * issues an alert, and either panics or BUGs, depending on mount options.
  252. */
  253. void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  254. unsigned int line, int errno, const char *fmt, ...)
  255. {
  256. char *s_id = "<unknown>";
  257. const char *errstr;
  258. struct va_format vaf = { .fmt = fmt };
  259. va_list args;
  260. if (fs_info)
  261. s_id = fs_info->sb->s_id;
  262. va_start(args, fmt);
  263. vaf.va = &args;
  264. errstr = btrfs_decode_error(errno);
  265. if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
  266. panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  267. s_id, function, line, &vaf, errno, errstr);
  268. btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
  269. function, line, &vaf, errno, errstr);
  270. va_end(args);
  271. /* Caller calls BUG() */
  272. }
  273. static void btrfs_put_super(struct super_block *sb)
  274. {
  275. (void)close_ctree(btrfs_sb(sb)->tree_root);
  276. /* FIXME: need to fix VFS to return error? */
  277. /* AV: return it _where_? ->put_super() can be triggered by any number
  278. * of async events, up to and including delivery of SIGKILL to the
  279. * last process that kept it busy. Or segfault in the aforementioned
  280. * process... Whom would you report that to?
  281. */
  282. }
  283. enum {
  284. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  285. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  286. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  287. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  288. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  289. Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
  290. Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
  291. Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
  292. Opt_check_integrity, Opt_check_integrity_including_extent_data,
  293. Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
  294. Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
  295. Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
  296. Opt_datasum, Opt_treelog, Opt_noinode_cache,
  297. Opt_err,
  298. };
  299. static match_table_t tokens = {
  300. {Opt_degraded, "degraded"},
  301. {Opt_subvol, "subvol=%s"},
  302. {Opt_subvolid, "subvolid=%s"},
  303. {Opt_device, "device=%s"},
  304. {Opt_nodatasum, "nodatasum"},
  305. {Opt_datasum, "datasum"},
  306. {Opt_nodatacow, "nodatacow"},
  307. {Opt_datacow, "datacow"},
  308. {Opt_nobarrier, "nobarrier"},
  309. {Opt_barrier, "barrier"},
  310. {Opt_max_inline, "max_inline=%s"},
  311. {Opt_alloc_start, "alloc_start=%s"},
  312. {Opt_thread_pool, "thread_pool=%d"},
  313. {Opt_compress, "compress"},
  314. {Opt_compress_type, "compress=%s"},
  315. {Opt_compress_force, "compress-force"},
  316. {Opt_compress_force_type, "compress-force=%s"},
  317. {Opt_ssd, "ssd"},
  318. {Opt_ssd_spread, "ssd_spread"},
  319. {Opt_nossd, "nossd"},
  320. {Opt_acl, "acl"},
  321. {Opt_noacl, "noacl"},
  322. {Opt_notreelog, "notreelog"},
  323. {Opt_treelog, "treelog"},
  324. {Opt_flushoncommit, "flushoncommit"},
  325. {Opt_noflushoncommit, "noflushoncommit"},
  326. {Opt_ratio, "metadata_ratio=%d"},
  327. {Opt_discard, "discard"},
  328. {Opt_nodiscard, "nodiscard"},
  329. {Opt_space_cache, "space_cache"},
  330. {Opt_clear_cache, "clear_cache"},
  331. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  332. {Opt_enospc_debug, "enospc_debug"},
  333. {Opt_noenospc_debug, "noenospc_debug"},
  334. {Opt_subvolrootid, "subvolrootid=%d"},
  335. {Opt_defrag, "autodefrag"},
  336. {Opt_nodefrag, "noautodefrag"},
  337. {Opt_inode_cache, "inode_cache"},
  338. {Opt_noinode_cache, "noinode_cache"},
  339. {Opt_no_space_cache, "nospace_cache"},
  340. {Opt_recovery, "recovery"},
  341. {Opt_skip_balance, "skip_balance"},
  342. {Opt_check_integrity, "check_int"},
  343. {Opt_check_integrity_including_extent_data, "check_int_data"},
  344. {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
  345. {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
  346. {Opt_fatal_errors, "fatal_errors=%s"},
  347. {Opt_commit_interval, "commit=%d"},
  348. {Opt_err, NULL},
  349. };
  350. /*
  351. * Regular mount options parser. Everything that is needed only when
  352. * reading in a new superblock is parsed here.
  353. * XXX JDM: This needs to be cleaned up for remount.
  354. */
  355. int btrfs_parse_options(struct btrfs_root *root, char *options)
  356. {
  357. struct btrfs_fs_info *info = root->fs_info;
  358. substring_t args[MAX_OPT_ARGS];
  359. char *p, *num, *orig = NULL;
  360. u64 cache_gen;
  361. int intarg;
  362. int ret = 0;
  363. char *compress_type;
  364. bool compress_force = false;
  365. bool compress = false;
  366. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  367. if (cache_gen)
  368. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  369. if (!options)
  370. goto out;
  371. /*
  372. * strsep changes the string, duplicate it because parse_options
  373. * gets called twice
  374. */
  375. options = kstrdup(options, GFP_NOFS);
  376. if (!options)
  377. return -ENOMEM;
  378. orig = options;
  379. while ((p = strsep(&options, ",")) != NULL) {
  380. int token;
  381. if (!*p)
  382. continue;
  383. token = match_token(p, tokens, args);
  384. switch (token) {
  385. case Opt_degraded:
  386. btrfs_info(root->fs_info, "allowing degraded mounts");
  387. btrfs_set_opt(info->mount_opt, DEGRADED);
  388. break;
  389. case Opt_subvol:
  390. case Opt_subvolid:
  391. case Opt_subvolrootid:
  392. case Opt_device:
  393. /*
  394. * These are parsed by btrfs_parse_early_options
  395. * and can be happily ignored here.
  396. */
  397. break;
  398. case Opt_nodatasum:
  399. btrfs_set_and_info(root, NODATASUM,
  400. "setting nodatasum");
  401. break;
  402. case Opt_datasum:
  403. if (btrfs_test_opt(root, NODATASUM)) {
  404. if (btrfs_test_opt(root, NODATACOW))
  405. btrfs_info(root->fs_info, "setting datasum, datacow enabled");
  406. else
  407. btrfs_info(root->fs_info, "setting datasum");
  408. }
  409. btrfs_clear_opt(info->mount_opt, NODATACOW);
  410. btrfs_clear_opt(info->mount_opt, NODATASUM);
  411. break;
  412. case Opt_nodatacow:
  413. if (!btrfs_test_opt(root, NODATACOW)) {
  414. if (!btrfs_test_opt(root, COMPRESS) ||
  415. !btrfs_test_opt(root, FORCE_COMPRESS)) {
  416. btrfs_info(root->fs_info,
  417. "setting nodatacow, compression disabled");
  418. } else {
  419. btrfs_info(root->fs_info, "setting nodatacow");
  420. }
  421. }
  422. btrfs_clear_opt(info->mount_opt, COMPRESS);
  423. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  424. btrfs_set_opt(info->mount_opt, NODATACOW);
  425. btrfs_set_opt(info->mount_opt, NODATASUM);
  426. break;
  427. case Opt_datacow:
  428. btrfs_clear_and_info(root, NODATACOW,
  429. "setting datacow");
  430. break;
  431. case Opt_compress_force:
  432. case Opt_compress_force_type:
  433. compress_force = true;
  434. /* Fallthrough */
  435. case Opt_compress:
  436. case Opt_compress_type:
  437. compress = true;
  438. if (token == Opt_compress ||
  439. token == Opt_compress_force ||
  440. strcmp(args[0].from, "zlib") == 0) {
  441. compress_type = "zlib";
  442. info->compress_type = BTRFS_COMPRESS_ZLIB;
  443. btrfs_set_opt(info->mount_opt, COMPRESS);
  444. btrfs_clear_opt(info->mount_opt, NODATACOW);
  445. btrfs_clear_opt(info->mount_opt, NODATASUM);
  446. } else if (strcmp(args[0].from, "lzo") == 0) {
  447. compress_type = "lzo";
  448. info->compress_type = BTRFS_COMPRESS_LZO;
  449. btrfs_set_opt(info->mount_opt, COMPRESS);
  450. btrfs_clear_opt(info->mount_opt, NODATACOW);
  451. btrfs_clear_opt(info->mount_opt, NODATASUM);
  452. btrfs_set_fs_incompat(info, COMPRESS_LZO);
  453. } else if (strncmp(args[0].from, "no", 2) == 0) {
  454. compress_type = "no";
  455. btrfs_clear_opt(info->mount_opt, COMPRESS);
  456. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  457. compress_force = false;
  458. } else {
  459. ret = -EINVAL;
  460. goto out;
  461. }
  462. if (compress_force) {
  463. btrfs_set_and_info(root, FORCE_COMPRESS,
  464. "force %s compression",
  465. compress_type);
  466. } else if (compress) {
  467. if (!btrfs_test_opt(root, COMPRESS))
  468. btrfs_info(root->fs_info,
  469. "btrfs: use %s compression",
  470. compress_type);
  471. }
  472. break;
  473. case Opt_ssd:
  474. btrfs_set_and_info(root, SSD,
  475. "use ssd allocation scheme");
  476. break;
  477. case Opt_ssd_spread:
  478. btrfs_set_and_info(root, SSD_SPREAD,
  479. "use spread ssd allocation scheme");
  480. btrfs_set_opt(info->mount_opt, SSD);
  481. break;
  482. case Opt_nossd:
  483. btrfs_set_and_info(root, NOSSD,
  484. "not using ssd allocation scheme");
  485. btrfs_clear_opt(info->mount_opt, SSD);
  486. break;
  487. case Opt_barrier:
  488. btrfs_clear_and_info(root, NOBARRIER,
  489. "turning on barriers");
  490. break;
  491. case Opt_nobarrier:
  492. btrfs_set_and_info(root, NOBARRIER,
  493. "turning off barriers");
  494. break;
  495. case Opt_thread_pool:
  496. ret = match_int(&args[0], &intarg);
  497. if (ret) {
  498. goto out;
  499. } else if (intarg > 0) {
  500. info->thread_pool_size = intarg;
  501. } else {
  502. ret = -EINVAL;
  503. goto out;
  504. }
  505. break;
  506. case Opt_max_inline:
  507. num = match_strdup(&args[0]);
  508. if (num) {
  509. info->max_inline = memparse(num, NULL);
  510. kfree(num);
  511. if (info->max_inline) {
  512. info->max_inline = min_t(u64,
  513. info->max_inline,
  514. root->sectorsize);
  515. }
  516. btrfs_info(root->fs_info, "max_inline at %llu",
  517. info->max_inline);
  518. } else {
  519. ret = -ENOMEM;
  520. goto out;
  521. }
  522. break;
  523. case Opt_alloc_start:
  524. num = match_strdup(&args[0]);
  525. if (num) {
  526. mutex_lock(&info->chunk_mutex);
  527. info->alloc_start = memparse(num, NULL);
  528. mutex_unlock(&info->chunk_mutex);
  529. kfree(num);
  530. btrfs_info(root->fs_info, "allocations start at %llu",
  531. info->alloc_start);
  532. } else {
  533. ret = -ENOMEM;
  534. goto out;
  535. }
  536. break;
  537. case Opt_acl:
  538. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  539. root->fs_info->sb->s_flags |= MS_POSIXACL;
  540. break;
  541. #else
  542. btrfs_err(root->fs_info,
  543. "support for ACL not compiled in!");
  544. ret = -EINVAL;
  545. goto out;
  546. #endif
  547. case Opt_noacl:
  548. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  549. break;
  550. case Opt_notreelog:
  551. btrfs_set_and_info(root, NOTREELOG,
  552. "disabling tree log");
  553. break;
  554. case Opt_treelog:
  555. btrfs_clear_and_info(root, NOTREELOG,
  556. "enabling tree log");
  557. break;
  558. case Opt_flushoncommit:
  559. btrfs_set_and_info(root, FLUSHONCOMMIT,
  560. "turning on flush-on-commit");
  561. break;
  562. case Opt_noflushoncommit:
  563. btrfs_clear_and_info(root, FLUSHONCOMMIT,
  564. "turning off flush-on-commit");
  565. break;
  566. case Opt_ratio:
  567. ret = match_int(&args[0], &intarg);
  568. if (ret) {
  569. goto out;
  570. } else if (intarg >= 0) {
  571. info->metadata_ratio = intarg;
  572. btrfs_info(root->fs_info, "metadata ratio %d",
  573. info->metadata_ratio);
  574. } else {
  575. ret = -EINVAL;
  576. goto out;
  577. }
  578. break;
  579. case Opt_discard:
  580. btrfs_set_and_info(root, DISCARD,
  581. "turning on discard");
  582. break;
  583. case Opt_nodiscard:
  584. btrfs_clear_and_info(root, DISCARD,
  585. "turning off discard");
  586. break;
  587. case Opt_space_cache:
  588. btrfs_set_and_info(root, SPACE_CACHE,
  589. "enabling disk space caching");
  590. break;
  591. case Opt_rescan_uuid_tree:
  592. btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
  593. break;
  594. case Opt_no_space_cache:
  595. btrfs_clear_and_info(root, SPACE_CACHE,
  596. "disabling disk space caching");
  597. break;
  598. case Opt_inode_cache:
  599. btrfs_set_and_info(root, CHANGE_INODE_CACHE,
  600. "enabling inode map caching");
  601. break;
  602. case Opt_noinode_cache:
  603. btrfs_clear_and_info(root, CHANGE_INODE_CACHE,
  604. "disabling inode map caching");
  605. break;
  606. case Opt_clear_cache:
  607. btrfs_set_and_info(root, CLEAR_CACHE,
  608. "force clearing of disk cache");
  609. break;
  610. case Opt_user_subvol_rm_allowed:
  611. btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  612. break;
  613. case Opt_enospc_debug:
  614. btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
  615. break;
  616. case Opt_noenospc_debug:
  617. btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
  618. break;
  619. case Opt_defrag:
  620. btrfs_set_and_info(root, AUTO_DEFRAG,
  621. "enabling auto defrag");
  622. break;
  623. case Opt_nodefrag:
  624. btrfs_clear_and_info(root, AUTO_DEFRAG,
  625. "disabling auto defrag");
  626. break;
  627. case Opt_recovery:
  628. btrfs_info(root->fs_info, "enabling auto recovery");
  629. btrfs_set_opt(info->mount_opt, RECOVERY);
  630. break;
  631. case Opt_skip_balance:
  632. btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
  633. break;
  634. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  635. case Opt_check_integrity_including_extent_data:
  636. btrfs_info(root->fs_info,
  637. "enabling check integrity including extent data");
  638. btrfs_set_opt(info->mount_opt,
  639. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
  640. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  641. break;
  642. case Opt_check_integrity:
  643. btrfs_info(root->fs_info, "enabling check integrity");
  644. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  645. break;
  646. case Opt_check_integrity_print_mask:
  647. ret = match_int(&args[0], &intarg);
  648. if (ret) {
  649. goto out;
  650. } else if (intarg >= 0) {
  651. info->check_integrity_print_mask = intarg;
  652. btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x",
  653. info->check_integrity_print_mask);
  654. } else {
  655. ret = -EINVAL;
  656. goto out;
  657. }
  658. break;
  659. #else
  660. case Opt_check_integrity_including_extent_data:
  661. case Opt_check_integrity:
  662. case Opt_check_integrity_print_mask:
  663. btrfs_err(root->fs_info,
  664. "support for check_integrity* not compiled in!");
  665. ret = -EINVAL;
  666. goto out;
  667. #endif
  668. case Opt_fatal_errors:
  669. if (strcmp(args[0].from, "panic") == 0)
  670. btrfs_set_opt(info->mount_opt,
  671. PANIC_ON_FATAL_ERROR);
  672. else if (strcmp(args[0].from, "bug") == 0)
  673. btrfs_clear_opt(info->mount_opt,
  674. PANIC_ON_FATAL_ERROR);
  675. else {
  676. ret = -EINVAL;
  677. goto out;
  678. }
  679. break;
  680. case Opt_commit_interval:
  681. intarg = 0;
  682. ret = match_int(&args[0], &intarg);
  683. if (ret < 0) {
  684. btrfs_err(root->fs_info, "invalid commit interval");
  685. ret = -EINVAL;
  686. goto out;
  687. }
  688. if (intarg > 0) {
  689. if (intarg > 300) {
  690. btrfs_warn(root->fs_info, "excessive commit interval %d",
  691. intarg);
  692. }
  693. info->commit_interval = intarg;
  694. } else {
  695. btrfs_info(root->fs_info, "using default commit interval %ds",
  696. BTRFS_DEFAULT_COMMIT_INTERVAL);
  697. info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  698. }
  699. break;
  700. case Opt_err:
  701. btrfs_info(root->fs_info, "unrecognized mount option '%s'", p);
  702. ret = -EINVAL;
  703. goto out;
  704. default:
  705. break;
  706. }
  707. }
  708. out:
  709. if (!ret && btrfs_test_opt(root, SPACE_CACHE))
  710. btrfs_info(root->fs_info, "disk space caching is enabled");
  711. kfree(orig);
  712. return ret;
  713. }
  714. /*
  715. * Parse mount options that are required early in the mount process.
  716. *
  717. * All other options will be parsed on much later in the mount process and
  718. * only when we need to allocate a new super block.
  719. */
  720. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  721. void *holder, char **subvol_name, u64 *subvol_objectid,
  722. struct btrfs_fs_devices **fs_devices)
  723. {
  724. substring_t args[MAX_OPT_ARGS];
  725. char *device_name, *opts, *orig, *p;
  726. char *num = NULL;
  727. int error = 0;
  728. if (!options)
  729. return 0;
  730. /*
  731. * strsep changes the string, duplicate it because parse_options
  732. * gets called twice
  733. */
  734. opts = kstrdup(options, GFP_KERNEL);
  735. if (!opts)
  736. return -ENOMEM;
  737. orig = opts;
  738. while ((p = strsep(&opts, ",")) != NULL) {
  739. int token;
  740. if (!*p)
  741. continue;
  742. token = match_token(p, tokens, args);
  743. switch (token) {
  744. case Opt_subvol:
  745. kfree(*subvol_name);
  746. *subvol_name = match_strdup(&args[0]);
  747. if (!*subvol_name) {
  748. error = -ENOMEM;
  749. goto out;
  750. }
  751. break;
  752. case Opt_subvolid:
  753. num = match_strdup(&args[0]);
  754. if (num) {
  755. *subvol_objectid = memparse(num, NULL);
  756. kfree(num);
  757. /* we want the original fs_tree */
  758. if (!*subvol_objectid)
  759. *subvol_objectid =
  760. BTRFS_FS_TREE_OBJECTID;
  761. } else {
  762. error = -EINVAL;
  763. goto out;
  764. }
  765. break;
  766. case Opt_subvolrootid:
  767. printk(KERN_WARNING
  768. "BTRFS: 'subvolrootid' mount option is deprecated and has "
  769. "no effect\n");
  770. break;
  771. case Opt_device:
  772. device_name = match_strdup(&args[0]);
  773. if (!device_name) {
  774. error = -ENOMEM;
  775. goto out;
  776. }
  777. error = btrfs_scan_one_device(device_name,
  778. flags, holder, fs_devices);
  779. kfree(device_name);
  780. if (error)
  781. goto out;
  782. break;
  783. default:
  784. break;
  785. }
  786. }
  787. out:
  788. kfree(orig);
  789. return error;
  790. }
  791. static struct dentry *get_default_root(struct super_block *sb,
  792. u64 subvol_objectid)
  793. {
  794. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  795. struct btrfs_root *root = fs_info->tree_root;
  796. struct btrfs_root *new_root;
  797. struct btrfs_dir_item *di;
  798. struct btrfs_path *path;
  799. struct btrfs_key location;
  800. struct inode *inode;
  801. u64 dir_id;
  802. int new = 0;
  803. /*
  804. * We have a specific subvol we want to mount, just setup location and
  805. * go look up the root.
  806. */
  807. if (subvol_objectid) {
  808. location.objectid = subvol_objectid;
  809. location.type = BTRFS_ROOT_ITEM_KEY;
  810. location.offset = (u64)-1;
  811. goto find_root;
  812. }
  813. path = btrfs_alloc_path();
  814. if (!path)
  815. return ERR_PTR(-ENOMEM);
  816. path->leave_spinning = 1;
  817. /*
  818. * Find the "default" dir item which points to the root item that we
  819. * will mount by default if we haven't been given a specific subvolume
  820. * to mount.
  821. */
  822. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  823. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
  824. if (IS_ERR(di)) {
  825. btrfs_free_path(path);
  826. return ERR_CAST(di);
  827. }
  828. if (!di) {
  829. /*
  830. * Ok the default dir item isn't there. This is weird since
  831. * it's always been there, but don't freak out, just try and
  832. * mount to root most subvolume.
  833. */
  834. btrfs_free_path(path);
  835. dir_id = BTRFS_FIRST_FREE_OBJECTID;
  836. new_root = fs_info->fs_root;
  837. goto setup_root;
  838. }
  839. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  840. btrfs_free_path(path);
  841. find_root:
  842. new_root = btrfs_read_fs_root_no_name(fs_info, &location);
  843. if (IS_ERR(new_root))
  844. return ERR_CAST(new_root);
  845. dir_id = btrfs_root_dirid(&new_root->root_item);
  846. setup_root:
  847. location.objectid = dir_id;
  848. location.type = BTRFS_INODE_ITEM_KEY;
  849. location.offset = 0;
  850. inode = btrfs_iget(sb, &location, new_root, &new);
  851. if (IS_ERR(inode))
  852. return ERR_CAST(inode);
  853. /*
  854. * If we're just mounting the root most subvol put the inode and return
  855. * a reference to the dentry. We will have already gotten a reference
  856. * to the inode in btrfs_fill_super so we're good to go.
  857. */
  858. if (!new && sb->s_root->d_inode == inode) {
  859. iput(inode);
  860. return dget(sb->s_root);
  861. }
  862. return d_obtain_root(inode);
  863. }
  864. static int btrfs_fill_super(struct super_block *sb,
  865. struct btrfs_fs_devices *fs_devices,
  866. void *data, int silent)
  867. {
  868. struct inode *inode;
  869. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  870. struct btrfs_key key;
  871. int err;
  872. sb->s_maxbytes = MAX_LFS_FILESIZE;
  873. sb->s_magic = BTRFS_SUPER_MAGIC;
  874. sb->s_op = &btrfs_super_ops;
  875. sb->s_d_op = &btrfs_dentry_operations;
  876. sb->s_export_op = &btrfs_export_ops;
  877. sb->s_xattr = btrfs_xattr_handlers;
  878. sb->s_time_gran = 1;
  879. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  880. sb->s_flags |= MS_POSIXACL;
  881. #endif
  882. sb->s_flags |= MS_I_VERSION;
  883. err = open_ctree(sb, fs_devices, (char *)data);
  884. if (err) {
  885. printk(KERN_ERR "BTRFS: open_ctree failed\n");
  886. return err;
  887. }
  888. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  889. key.type = BTRFS_INODE_ITEM_KEY;
  890. key.offset = 0;
  891. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  892. if (IS_ERR(inode)) {
  893. err = PTR_ERR(inode);
  894. goto fail_close;
  895. }
  896. sb->s_root = d_make_root(inode);
  897. if (!sb->s_root) {
  898. err = -ENOMEM;
  899. goto fail_close;
  900. }
  901. save_mount_options(sb, data);
  902. cleancache_init_fs(sb);
  903. sb->s_flags |= MS_ACTIVE;
  904. return 0;
  905. fail_close:
  906. close_ctree(fs_info->tree_root);
  907. return err;
  908. }
  909. int btrfs_sync_fs(struct super_block *sb, int wait)
  910. {
  911. struct btrfs_trans_handle *trans;
  912. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  913. struct btrfs_root *root = fs_info->tree_root;
  914. trace_btrfs_sync_fs(wait);
  915. if (!wait) {
  916. filemap_flush(fs_info->btree_inode->i_mapping);
  917. return 0;
  918. }
  919. btrfs_wait_ordered_roots(fs_info, -1);
  920. trans = btrfs_attach_transaction_barrier(root);
  921. if (IS_ERR(trans)) {
  922. /* no transaction, don't bother */
  923. if (PTR_ERR(trans) == -ENOENT)
  924. return 0;
  925. return PTR_ERR(trans);
  926. }
  927. return btrfs_commit_transaction(trans, root);
  928. }
  929. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  930. {
  931. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  932. struct btrfs_root *root = info->tree_root;
  933. char *compress_type;
  934. if (btrfs_test_opt(root, DEGRADED))
  935. seq_puts(seq, ",degraded");
  936. if (btrfs_test_opt(root, NODATASUM))
  937. seq_puts(seq, ",nodatasum");
  938. if (btrfs_test_opt(root, NODATACOW))
  939. seq_puts(seq, ",nodatacow");
  940. if (btrfs_test_opt(root, NOBARRIER))
  941. seq_puts(seq, ",nobarrier");
  942. if (info->max_inline != 8192 * 1024)
  943. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  944. if (info->alloc_start != 0)
  945. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  946. if (info->thread_pool_size != min_t(unsigned long,
  947. num_online_cpus() + 2, 8))
  948. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  949. if (btrfs_test_opt(root, COMPRESS)) {
  950. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  951. compress_type = "zlib";
  952. else
  953. compress_type = "lzo";
  954. if (btrfs_test_opt(root, FORCE_COMPRESS))
  955. seq_printf(seq, ",compress-force=%s", compress_type);
  956. else
  957. seq_printf(seq, ",compress=%s", compress_type);
  958. }
  959. if (btrfs_test_opt(root, NOSSD))
  960. seq_puts(seq, ",nossd");
  961. if (btrfs_test_opt(root, SSD_SPREAD))
  962. seq_puts(seq, ",ssd_spread");
  963. else if (btrfs_test_opt(root, SSD))
  964. seq_puts(seq, ",ssd");
  965. if (btrfs_test_opt(root, NOTREELOG))
  966. seq_puts(seq, ",notreelog");
  967. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  968. seq_puts(seq, ",flushoncommit");
  969. if (btrfs_test_opt(root, DISCARD))
  970. seq_puts(seq, ",discard");
  971. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  972. seq_puts(seq, ",noacl");
  973. if (btrfs_test_opt(root, SPACE_CACHE))
  974. seq_puts(seq, ",space_cache");
  975. else
  976. seq_puts(seq, ",nospace_cache");
  977. if (btrfs_test_opt(root, RESCAN_UUID_TREE))
  978. seq_puts(seq, ",rescan_uuid_tree");
  979. if (btrfs_test_opt(root, CLEAR_CACHE))
  980. seq_puts(seq, ",clear_cache");
  981. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  982. seq_puts(seq, ",user_subvol_rm_allowed");
  983. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  984. seq_puts(seq, ",enospc_debug");
  985. if (btrfs_test_opt(root, AUTO_DEFRAG))
  986. seq_puts(seq, ",autodefrag");
  987. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  988. seq_puts(seq, ",inode_cache");
  989. if (btrfs_test_opt(root, SKIP_BALANCE))
  990. seq_puts(seq, ",skip_balance");
  991. if (btrfs_test_opt(root, RECOVERY))
  992. seq_puts(seq, ",recovery");
  993. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  994. if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
  995. seq_puts(seq, ",check_int_data");
  996. else if (btrfs_test_opt(root, CHECK_INTEGRITY))
  997. seq_puts(seq, ",check_int");
  998. if (info->check_integrity_print_mask)
  999. seq_printf(seq, ",check_int_print_mask=%d",
  1000. info->check_integrity_print_mask);
  1001. #endif
  1002. if (info->metadata_ratio)
  1003. seq_printf(seq, ",metadata_ratio=%d",
  1004. info->metadata_ratio);
  1005. if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
  1006. seq_puts(seq, ",fatal_errors=panic");
  1007. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  1008. seq_printf(seq, ",commit=%d", info->commit_interval);
  1009. return 0;
  1010. }
  1011. static int btrfs_test_super(struct super_block *s, void *data)
  1012. {
  1013. struct btrfs_fs_info *p = data;
  1014. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1015. return fs_info->fs_devices == p->fs_devices;
  1016. }
  1017. static int btrfs_set_super(struct super_block *s, void *data)
  1018. {
  1019. int err = set_anon_super(s, data);
  1020. if (!err)
  1021. s->s_fs_info = data;
  1022. return err;
  1023. }
  1024. /*
  1025. * subvolumes are identified by ino 256
  1026. */
  1027. static inline int is_subvolume_inode(struct inode *inode)
  1028. {
  1029. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  1030. return 1;
  1031. return 0;
  1032. }
  1033. /*
  1034. * This will strip out the subvol=%s argument for an argument string and add
  1035. * subvolid=0 to make sure we get the actual tree root for path walking to the
  1036. * subvol we want.
  1037. */
  1038. static char *setup_root_args(char *args)
  1039. {
  1040. unsigned len = strlen(args) + 2 + 1;
  1041. char *src, *dst, *buf;
  1042. /*
  1043. * We need the same args as before, but with this substitution:
  1044. * s!subvol=[^,]+!subvolid=0!
  1045. *
  1046. * Since the replacement string is up to 2 bytes longer than the
  1047. * original, allocate strlen(args) + 2 + 1 bytes.
  1048. */
  1049. src = strstr(args, "subvol=");
  1050. /* This shouldn't happen, but just in case.. */
  1051. if (!src)
  1052. return NULL;
  1053. buf = dst = kmalloc(len, GFP_NOFS);
  1054. if (!buf)
  1055. return NULL;
  1056. /*
  1057. * If the subvol= arg is not at the start of the string,
  1058. * copy whatever precedes it into buf.
  1059. */
  1060. if (src != args) {
  1061. *src++ = '\0';
  1062. strcpy(buf, args);
  1063. dst += strlen(args);
  1064. }
  1065. strcpy(dst, "subvolid=0");
  1066. dst += strlen("subvolid=0");
  1067. /*
  1068. * If there is a "," after the original subvol=... string,
  1069. * copy that suffix into our buffer. Otherwise, we're done.
  1070. */
  1071. src = strchr(src, ',');
  1072. if (src)
  1073. strcpy(dst, src);
  1074. return buf;
  1075. }
  1076. static struct dentry *mount_subvol(const char *subvol_name, int flags,
  1077. const char *device_name, char *data)
  1078. {
  1079. struct dentry *root;
  1080. struct vfsmount *mnt;
  1081. char *newargs;
  1082. newargs = setup_root_args(data);
  1083. if (!newargs)
  1084. return ERR_PTR(-ENOMEM);
  1085. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
  1086. newargs);
  1087. if (PTR_RET(mnt) == -EBUSY) {
  1088. if (flags & MS_RDONLY) {
  1089. mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY, device_name,
  1090. newargs);
  1091. } else {
  1092. int r;
  1093. mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY, device_name,
  1094. newargs);
  1095. if (IS_ERR(mnt)) {
  1096. kfree(newargs);
  1097. return ERR_CAST(mnt);
  1098. }
  1099. r = btrfs_remount(mnt->mnt_sb, &flags, NULL);
  1100. if (r < 0) {
  1101. /* FIXME: release vfsmount mnt ??*/
  1102. kfree(newargs);
  1103. return ERR_PTR(r);
  1104. }
  1105. }
  1106. }
  1107. kfree(newargs);
  1108. if (IS_ERR(mnt))
  1109. return ERR_CAST(mnt);
  1110. root = mount_subtree(mnt, subvol_name);
  1111. if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
  1112. struct super_block *s = root->d_sb;
  1113. dput(root);
  1114. root = ERR_PTR(-EINVAL);
  1115. deactivate_locked_super(s);
  1116. printk(KERN_ERR "BTRFS: '%s' is not a valid subvolume\n",
  1117. subvol_name);
  1118. }
  1119. return root;
  1120. }
  1121. /*
  1122. * Find a superblock for the given device / mount point.
  1123. *
  1124. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  1125. * for multiple device setup. Make sure to keep it in sync.
  1126. */
  1127. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  1128. const char *device_name, void *data)
  1129. {
  1130. struct block_device *bdev = NULL;
  1131. struct super_block *s;
  1132. struct dentry *root;
  1133. struct btrfs_fs_devices *fs_devices = NULL;
  1134. struct btrfs_fs_info *fs_info = NULL;
  1135. fmode_t mode = FMODE_READ;
  1136. char *subvol_name = NULL;
  1137. u64 subvol_objectid = 0;
  1138. int error = 0;
  1139. if (!(flags & MS_RDONLY))
  1140. mode |= FMODE_WRITE;
  1141. error = btrfs_parse_early_options(data, mode, fs_type,
  1142. &subvol_name, &subvol_objectid,
  1143. &fs_devices);
  1144. if (error) {
  1145. kfree(subvol_name);
  1146. return ERR_PTR(error);
  1147. }
  1148. if (subvol_name) {
  1149. root = mount_subvol(subvol_name, flags, device_name, data);
  1150. kfree(subvol_name);
  1151. return root;
  1152. }
  1153. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  1154. if (error)
  1155. return ERR_PTR(error);
  1156. /*
  1157. * Setup a dummy root and fs_info for test/set super. This is because
  1158. * we don't actually fill this stuff out until open_ctree, but we need
  1159. * it for searching for existing supers, so this lets us do that and
  1160. * then open_ctree will properly initialize everything later.
  1161. */
  1162. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  1163. if (!fs_info)
  1164. return ERR_PTR(-ENOMEM);
  1165. fs_info->fs_devices = fs_devices;
  1166. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1167. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1168. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1169. error = -ENOMEM;
  1170. goto error_fs_info;
  1171. }
  1172. error = btrfs_open_devices(fs_devices, mode, fs_type);
  1173. if (error)
  1174. goto error_fs_info;
  1175. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  1176. error = -EACCES;
  1177. goto error_close_devices;
  1178. }
  1179. bdev = fs_devices->latest_bdev;
  1180. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  1181. fs_info);
  1182. if (IS_ERR(s)) {
  1183. error = PTR_ERR(s);
  1184. goto error_close_devices;
  1185. }
  1186. if (s->s_root) {
  1187. btrfs_close_devices(fs_devices);
  1188. free_fs_info(fs_info);
  1189. if ((flags ^ s->s_flags) & MS_RDONLY)
  1190. error = -EBUSY;
  1191. } else {
  1192. char b[BDEVNAME_SIZE];
  1193. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  1194. btrfs_sb(s)->bdev_holder = fs_type;
  1195. error = btrfs_fill_super(s, fs_devices, data,
  1196. flags & MS_SILENT ? 1 : 0);
  1197. }
  1198. root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
  1199. if (IS_ERR(root))
  1200. deactivate_locked_super(s);
  1201. return root;
  1202. error_close_devices:
  1203. btrfs_close_devices(fs_devices);
  1204. error_fs_info:
  1205. free_fs_info(fs_info);
  1206. return ERR_PTR(error);
  1207. }
  1208. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1209. int new_pool_size, int old_pool_size)
  1210. {
  1211. if (new_pool_size == old_pool_size)
  1212. return;
  1213. fs_info->thread_pool_size = new_pool_size;
  1214. btrfs_info(fs_info, "resize thread pool %d -> %d",
  1215. old_pool_size, new_pool_size);
  1216. btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
  1217. btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
  1218. btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
  1219. btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
  1220. btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
  1221. btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
  1222. btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
  1223. new_pool_size);
  1224. btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  1225. btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
  1226. btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
  1227. btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
  1228. btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
  1229. new_pool_size);
  1230. }
  1231. static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
  1232. {
  1233. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1234. }
  1235. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1236. unsigned long old_opts, int flags)
  1237. {
  1238. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1239. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1240. (flags & MS_RDONLY))) {
  1241. /* wait for any defraggers to finish */
  1242. wait_event(fs_info->transaction_wait,
  1243. (atomic_read(&fs_info->defrag_running) == 0));
  1244. if (flags & MS_RDONLY)
  1245. sync_filesystem(fs_info->sb);
  1246. }
  1247. }
  1248. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1249. unsigned long old_opts)
  1250. {
  1251. /*
  1252. * We need cleanup all defragable inodes if the autodefragment is
  1253. * close or the fs is R/O.
  1254. */
  1255. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1256. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1257. (fs_info->sb->s_flags & MS_RDONLY))) {
  1258. btrfs_cleanup_defrag_inodes(fs_info);
  1259. }
  1260. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1261. }
  1262. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1263. {
  1264. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1265. struct btrfs_root *root = fs_info->tree_root;
  1266. unsigned old_flags = sb->s_flags;
  1267. unsigned long old_opts = fs_info->mount_opt;
  1268. unsigned long old_compress_type = fs_info->compress_type;
  1269. u64 old_max_inline = fs_info->max_inline;
  1270. u64 old_alloc_start = fs_info->alloc_start;
  1271. int old_thread_pool_size = fs_info->thread_pool_size;
  1272. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1273. int ret;
  1274. sync_filesystem(sb);
  1275. btrfs_remount_prepare(fs_info);
  1276. ret = btrfs_parse_options(root, data);
  1277. if (ret) {
  1278. ret = -EINVAL;
  1279. goto restore;
  1280. }
  1281. btrfs_remount_begin(fs_info, old_opts, *flags);
  1282. btrfs_resize_thread_pool(fs_info,
  1283. fs_info->thread_pool_size, old_thread_pool_size);
  1284. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1285. goto out;
  1286. if (*flags & MS_RDONLY) {
  1287. /*
  1288. * this also happens on 'umount -rf' or on shutdown, when
  1289. * the filesystem is busy.
  1290. */
  1291. cancel_work_sync(&fs_info->async_reclaim_work);
  1292. /* wait for the uuid_scan task to finish */
  1293. down(&fs_info->uuid_tree_rescan_sem);
  1294. /* avoid complains from lockdep et al. */
  1295. up(&fs_info->uuid_tree_rescan_sem);
  1296. sb->s_flags |= MS_RDONLY;
  1297. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1298. btrfs_scrub_cancel(fs_info);
  1299. btrfs_pause_balance(fs_info);
  1300. ret = btrfs_commit_super(root);
  1301. if (ret)
  1302. goto restore;
  1303. } else {
  1304. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  1305. btrfs_err(fs_info,
  1306. "Remounting read-write after error is not allowed");
  1307. ret = -EINVAL;
  1308. goto restore;
  1309. }
  1310. if (fs_info->fs_devices->rw_devices == 0) {
  1311. ret = -EACCES;
  1312. goto restore;
  1313. }
  1314. if (fs_info->fs_devices->missing_devices >
  1315. fs_info->num_tolerated_disk_barrier_failures &&
  1316. !(*flags & MS_RDONLY)) {
  1317. btrfs_warn(fs_info,
  1318. "too many missing devices, writeable remount is not allowed");
  1319. ret = -EACCES;
  1320. goto restore;
  1321. }
  1322. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1323. ret = -EINVAL;
  1324. goto restore;
  1325. }
  1326. ret = btrfs_cleanup_fs_roots(fs_info);
  1327. if (ret)
  1328. goto restore;
  1329. /* recover relocation */
  1330. mutex_lock(&fs_info->cleaner_mutex);
  1331. ret = btrfs_recover_relocation(root);
  1332. mutex_unlock(&fs_info->cleaner_mutex);
  1333. if (ret)
  1334. goto restore;
  1335. ret = btrfs_resume_balance_async(fs_info);
  1336. if (ret)
  1337. goto restore;
  1338. ret = btrfs_resume_dev_replace_async(fs_info);
  1339. if (ret) {
  1340. btrfs_warn(fs_info, "failed to resume dev_replace");
  1341. goto restore;
  1342. }
  1343. if (!fs_info->uuid_root) {
  1344. btrfs_info(fs_info, "creating UUID tree");
  1345. ret = btrfs_create_uuid_tree(fs_info);
  1346. if (ret) {
  1347. btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
  1348. goto restore;
  1349. }
  1350. }
  1351. sb->s_flags &= ~MS_RDONLY;
  1352. }
  1353. out:
  1354. wake_up_process(fs_info->transaction_kthread);
  1355. btrfs_remount_cleanup(fs_info, old_opts);
  1356. return 0;
  1357. restore:
  1358. /* We've hit an error - don't reset MS_RDONLY */
  1359. if (sb->s_flags & MS_RDONLY)
  1360. old_flags |= MS_RDONLY;
  1361. sb->s_flags = old_flags;
  1362. fs_info->mount_opt = old_opts;
  1363. fs_info->compress_type = old_compress_type;
  1364. fs_info->max_inline = old_max_inline;
  1365. mutex_lock(&fs_info->chunk_mutex);
  1366. fs_info->alloc_start = old_alloc_start;
  1367. mutex_unlock(&fs_info->chunk_mutex);
  1368. btrfs_resize_thread_pool(fs_info,
  1369. old_thread_pool_size, fs_info->thread_pool_size);
  1370. fs_info->metadata_ratio = old_metadata_ratio;
  1371. btrfs_remount_cleanup(fs_info, old_opts);
  1372. return ret;
  1373. }
  1374. /* Used to sort the devices by max_avail(descending sort) */
  1375. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1376. const void *dev_info2)
  1377. {
  1378. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1379. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1380. return -1;
  1381. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1382. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1383. return 1;
  1384. else
  1385. return 0;
  1386. }
  1387. /*
  1388. * sort the devices by max_avail, in which max free extent size of each device
  1389. * is stored.(Descending Sort)
  1390. */
  1391. static inline void btrfs_descending_sort_devices(
  1392. struct btrfs_device_info *devices,
  1393. size_t nr_devices)
  1394. {
  1395. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1396. btrfs_cmp_device_free_bytes, NULL);
  1397. }
  1398. /*
  1399. * The helper to calc the free space on the devices that can be used to store
  1400. * file data.
  1401. */
  1402. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  1403. {
  1404. struct btrfs_fs_info *fs_info = root->fs_info;
  1405. struct btrfs_device_info *devices_info;
  1406. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1407. struct btrfs_device *device;
  1408. u64 skip_space;
  1409. u64 type;
  1410. u64 avail_space;
  1411. u64 used_space;
  1412. u64 min_stripe_size;
  1413. int min_stripes = 1, num_stripes = 1;
  1414. int i = 0, nr_devices;
  1415. int ret;
  1416. nr_devices = fs_info->fs_devices->open_devices;
  1417. BUG_ON(!nr_devices);
  1418. devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
  1419. GFP_NOFS);
  1420. if (!devices_info)
  1421. return -ENOMEM;
  1422. /* calc min stripe number for data space alloction */
  1423. type = btrfs_get_alloc_profile(root, 1);
  1424. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1425. min_stripes = 2;
  1426. num_stripes = nr_devices;
  1427. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1428. min_stripes = 2;
  1429. num_stripes = 2;
  1430. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1431. min_stripes = 4;
  1432. num_stripes = 4;
  1433. }
  1434. if (type & BTRFS_BLOCK_GROUP_DUP)
  1435. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1436. else
  1437. min_stripe_size = BTRFS_STRIPE_LEN;
  1438. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  1439. if (!device->in_fs_metadata || !device->bdev ||
  1440. device->is_tgtdev_for_dev_replace)
  1441. continue;
  1442. avail_space = device->total_bytes - device->bytes_used;
  1443. /* align with stripe_len */
  1444. do_div(avail_space, BTRFS_STRIPE_LEN);
  1445. avail_space *= BTRFS_STRIPE_LEN;
  1446. /*
  1447. * In order to avoid overwritting the superblock on the drive,
  1448. * btrfs starts at an offset of at least 1MB when doing chunk
  1449. * allocation.
  1450. */
  1451. skip_space = 1024 * 1024;
  1452. /* user can set the offset in fs_info->alloc_start. */
  1453. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1454. device->total_bytes)
  1455. skip_space = max(fs_info->alloc_start, skip_space);
  1456. /*
  1457. * btrfs can not use the free space in [0, skip_space - 1],
  1458. * we must subtract it from the total. In order to implement
  1459. * it, we account the used space in this range first.
  1460. */
  1461. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  1462. &used_space);
  1463. if (ret) {
  1464. kfree(devices_info);
  1465. return ret;
  1466. }
  1467. /* calc the free space in [0, skip_space - 1] */
  1468. skip_space -= used_space;
  1469. /*
  1470. * we can use the free space in [0, skip_space - 1], subtract
  1471. * it from the total.
  1472. */
  1473. if (avail_space && avail_space >= skip_space)
  1474. avail_space -= skip_space;
  1475. else
  1476. avail_space = 0;
  1477. if (avail_space < min_stripe_size)
  1478. continue;
  1479. devices_info[i].dev = device;
  1480. devices_info[i].max_avail = avail_space;
  1481. i++;
  1482. }
  1483. nr_devices = i;
  1484. btrfs_descending_sort_devices(devices_info, nr_devices);
  1485. i = nr_devices - 1;
  1486. avail_space = 0;
  1487. while (nr_devices >= min_stripes) {
  1488. if (num_stripes > nr_devices)
  1489. num_stripes = nr_devices;
  1490. if (devices_info[i].max_avail >= min_stripe_size) {
  1491. int j;
  1492. u64 alloc_size;
  1493. avail_space += devices_info[i].max_avail * num_stripes;
  1494. alloc_size = devices_info[i].max_avail;
  1495. for (j = i + 1 - num_stripes; j <= i; j++)
  1496. devices_info[j].max_avail -= alloc_size;
  1497. }
  1498. i--;
  1499. nr_devices--;
  1500. }
  1501. kfree(devices_info);
  1502. *free_bytes = avail_space;
  1503. return 0;
  1504. }
  1505. /*
  1506. * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
  1507. *
  1508. * If there's a redundant raid level at DATA block groups, use the respective
  1509. * multiplier to scale the sizes.
  1510. *
  1511. * Unused device space usage is based on simulating the chunk allocator
  1512. * algorithm that respects the device sizes, order of allocations and the
  1513. * 'alloc_start' value, this is a close approximation of the actual use but
  1514. * there are other factors that may change the result (like a new metadata
  1515. * chunk).
  1516. *
  1517. * FIXME: not accurate for mixed block groups, total and free/used are ok,
  1518. * available appears slightly larger.
  1519. */
  1520. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1521. {
  1522. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1523. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1524. struct list_head *head = &fs_info->space_info;
  1525. struct btrfs_space_info *found;
  1526. u64 total_used = 0;
  1527. u64 total_free_data = 0;
  1528. int bits = dentry->d_sb->s_blocksize_bits;
  1529. __be32 *fsid = (__be32 *)fs_info->fsid;
  1530. unsigned factor = 1;
  1531. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  1532. int ret;
  1533. /* holding chunk_muext to avoid allocating new chunks */
  1534. mutex_lock(&fs_info->chunk_mutex);
  1535. rcu_read_lock();
  1536. list_for_each_entry_rcu(found, head, list) {
  1537. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1538. int i;
  1539. total_free_data += found->disk_total - found->disk_used;
  1540. total_free_data -=
  1541. btrfs_account_ro_block_groups_free_space(found);
  1542. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1543. if (!list_empty(&found->block_groups[i])) {
  1544. switch (i) {
  1545. case BTRFS_RAID_DUP:
  1546. case BTRFS_RAID_RAID1:
  1547. case BTRFS_RAID_RAID10:
  1548. factor = 2;
  1549. }
  1550. }
  1551. }
  1552. }
  1553. total_used += found->disk_used;
  1554. }
  1555. rcu_read_unlock();
  1556. buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  1557. buf->f_blocks >>= bits;
  1558. buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  1559. /* Account global block reserve as used, it's in logical size already */
  1560. spin_lock(&block_rsv->lock);
  1561. buf->f_bfree -= block_rsv->size >> bits;
  1562. spin_unlock(&block_rsv->lock);
  1563. buf->f_bavail = total_free_data;
  1564. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1565. if (ret) {
  1566. mutex_unlock(&fs_info->chunk_mutex);
  1567. return ret;
  1568. }
  1569. buf->f_bavail += div_u64(total_free_data, factor);
  1570. buf->f_bavail = buf->f_bavail >> bits;
  1571. mutex_unlock(&fs_info->chunk_mutex);
  1572. buf->f_type = BTRFS_SUPER_MAGIC;
  1573. buf->f_bsize = dentry->d_sb->s_blocksize;
  1574. buf->f_namelen = BTRFS_NAME_LEN;
  1575. /* We treat it as constant endianness (it doesn't matter _which_)
  1576. because we want the fsid to come out the same whether mounted
  1577. on a big-endian or little-endian host */
  1578. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1579. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1580. /* Mask in the root object ID too, to disambiguate subvols */
  1581. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  1582. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  1583. return 0;
  1584. }
  1585. static void btrfs_kill_super(struct super_block *sb)
  1586. {
  1587. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1588. kill_anon_super(sb);
  1589. free_fs_info(fs_info);
  1590. }
  1591. static struct file_system_type btrfs_fs_type = {
  1592. .owner = THIS_MODULE,
  1593. .name = "btrfs",
  1594. .mount = btrfs_mount,
  1595. .kill_sb = btrfs_kill_super,
  1596. .fs_flags = FS_REQUIRES_DEV,
  1597. };
  1598. MODULE_ALIAS_FS("btrfs");
  1599. /*
  1600. * used by btrfsctl to scan devices when no FS is mounted
  1601. */
  1602. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1603. unsigned long arg)
  1604. {
  1605. struct btrfs_ioctl_vol_args *vol;
  1606. struct btrfs_fs_devices *fs_devices;
  1607. int ret = -ENOTTY;
  1608. if (!capable(CAP_SYS_ADMIN))
  1609. return -EPERM;
  1610. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1611. if (IS_ERR(vol))
  1612. return PTR_ERR(vol);
  1613. switch (cmd) {
  1614. case BTRFS_IOC_SCAN_DEV:
  1615. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1616. &btrfs_fs_type, &fs_devices);
  1617. break;
  1618. case BTRFS_IOC_DEVICES_READY:
  1619. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1620. &btrfs_fs_type, &fs_devices);
  1621. if (ret)
  1622. break;
  1623. ret = !(fs_devices->num_devices == fs_devices->total_devices);
  1624. break;
  1625. }
  1626. kfree(vol);
  1627. return ret;
  1628. }
  1629. static int btrfs_freeze(struct super_block *sb)
  1630. {
  1631. struct btrfs_trans_handle *trans;
  1632. struct btrfs_root *root = btrfs_sb(sb)->tree_root;
  1633. trans = btrfs_attach_transaction_barrier(root);
  1634. if (IS_ERR(trans)) {
  1635. /* no transaction, don't bother */
  1636. if (PTR_ERR(trans) == -ENOENT)
  1637. return 0;
  1638. return PTR_ERR(trans);
  1639. }
  1640. return btrfs_commit_transaction(trans, root);
  1641. }
  1642. static int btrfs_unfreeze(struct super_block *sb)
  1643. {
  1644. return 0;
  1645. }
  1646. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  1647. {
  1648. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  1649. struct btrfs_fs_devices *cur_devices;
  1650. struct btrfs_device *dev, *first_dev = NULL;
  1651. struct list_head *head;
  1652. struct rcu_string *name;
  1653. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1654. cur_devices = fs_info->fs_devices;
  1655. while (cur_devices) {
  1656. head = &cur_devices->devices;
  1657. list_for_each_entry(dev, head, dev_list) {
  1658. if (dev->missing)
  1659. continue;
  1660. if (!dev->name)
  1661. continue;
  1662. if (!first_dev || dev->devid < first_dev->devid)
  1663. first_dev = dev;
  1664. }
  1665. cur_devices = cur_devices->seed;
  1666. }
  1667. if (first_dev) {
  1668. rcu_read_lock();
  1669. name = rcu_dereference(first_dev->name);
  1670. seq_escape(m, name->str, " \t\n\\");
  1671. rcu_read_unlock();
  1672. } else {
  1673. WARN_ON(1);
  1674. }
  1675. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1676. return 0;
  1677. }
  1678. static const struct super_operations btrfs_super_ops = {
  1679. .drop_inode = btrfs_drop_inode,
  1680. .evict_inode = btrfs_evict_inode,
  1681. .put_super = btrfs_put_super,
  1682. .sync_fs = btrfs_sync_fs,
  1683. .show_options = btrfs_show_options,
  1684. .show_devname = btrfs_show_devname,
  1685. .write_inode = btrfs_write_inode,
  1686. .alloc_inode = btrfs_alloc_inode,
  1687. .destroy_inode = btrfs_destroy_inode,
  1688. .statfs = btrfs_statfs,
  1689. .remount_fs = btrfs_remount,
  1690. .freeze_fs = btrfs_freeze,
  1691. .unfreeze_fs = btrfs_unfreeze,
  1692. };
  1693. static const struct file_operations btrfs_ctl_fops = {
  1694. .unlocked_ioctl = btrfs_control_ioctl,
  1695. .compat_ioctl = btrfs_control_ioctl,
  1696. .owner = THIS_MODULE,
  1697. .llseek = noop_llseek,
  1698. };
  1699. static struct miscdevice btrfs_misc = {
  1700. .minor = BTRFS_MINOR,
  1701. .name = "btrfs-control",
  1702. .fops = &btrfs_ctl_fops
  1703. };
  1704. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  1705. MODULE_ALIAS("devname:btrfs-control");
  1706. static int btrfs_interface_init(void)
  1707. {
  1708. return misc_register(&btrfs_misc);
  1709. }
  1710. static void btrfs_interface_exit(void)
  1711. {
  1712. if (misc_deregister(&btrfs_misc) < 0)
  1713. printk(KERN_INFO "BTRFS: misc_deregister failed for control device\n");
  1714. }
  1715. static void btrfs_print_info(void)
  1716. {
  1717. printk(KERN_INFO "Btrfs loaded"
  1718. #ifdef CONFIG_BTRFS_DEBUG
  1719. ", debug=on"
  1720. #endif
  1721. #ifdef CONFIG_BTRFS_ASSERT
  1722. ", assert=on"
  1723. #endif
  1724. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1725. ", integrity-checker=on"
  1726. #endif
  1727. "\n");
  1728. }
  1729. static int btrfs_run_sanity_tests(void)
  1730. {
  1731. int ret;
  1732. ret = btrfs_init_test_fs();
  1733. if (ret)
  1734. return ret;
  1735. ret = btrfs_test_free_space_cache();
  1736. if (ret)
  1737. goto out;
  1738. ret = btrfs_test_extent_buffer_operations();
  1739. if (ret)
  1740. goto out;
  1741. ret = btrfs_test_extent_io();
  1742. if (ret)
  1743. goto out;
  1744. ret = btrfs_test_inodes();
  1745. if (ret)
  1746. goto out;
  1747. ret = btrfs_test_qgroups();
  1748. out:
  1749. btrfs_destroy_test_fs();
  1750. return ret;
  1751. }
  1752. static int __init init_btrfs_fs(void)
  1753. {
  1754. int err;
  1755. err = btrfs_hash_init();
  1756. if (err)
  1757. return err;
  1758. btrfs_props_init();
  1759. err = btrfs_init_sysfs();
  1760. if (err)
  1761. goto free_hash;
  1762. btrfs_init_compress();
  1763. err = btrfs_init_cachep();
  1764. if (err)
  1765. goto free_compress;
  1766. err = extent_io_init();
  1767. if (err)
  1768. goto free_cachep;
  1769. err = extent_map_init();
  1770. if (err)
  1771. goto free_extent_io;
  1772. err = ordered_data_init();
  1773. if (err)
  1774. goto free_extent_map;
  1775. err = btrfs_delayed_inode_init();
  1776. if (err)
  1777. goto free_ordered_data;
  1778. err = btrfs_auto_defrag_init();
  1779. if (err)
  1780. goto free_delayed_inode;
  1781. err = btrfs_delayed_ref_init();
  1782. if (err)
  1783. goto free_auto_defrag;
  1784. err = btrfs_prelim_ref_init();
  1785. if (err)
  1786. goto free_prelim_ref;
  1787. err = btrfs_interface_init();
  1788. if (err)
  1789. goto free_delayed_ref;
  1790. btrfs_init_lockdep();
  1791. btrfs_print_info();
  1792. err = btrfs_run_sanity_tests();
  1793. if (err)
  1794. goto unregister_ioctl;
  1795. err = register_filesystem(&btrfs_fs_type);
  1796. if (err)
  1797. goto unregister_ioctl;
  1798. return 0;
  1799. unregister_ioctl:
  1800. btrfs_interface_exit();
  1801. free_prelim_ref:
  1802. btrfs_prelim_ref_exit();
  1803. free_delayed_ref:
  1804. btrfs_delayed_ref_exit();
  1805. free_auto_defrag:
  1806. btrfs_auto_defrag_exit();
  1807. free_delayed_inode:
  1808. btrfs_delayed_inode_exit();
  1809. free_ordered_data:
  1810. ordered_data_exit();
  1811. free_extent_map:
  1812. extent_map_exit();
  1813. free_extent_io:
  1814. extent_io_exit();
  1815. free_cachep:
  1816. btrfs_destroy_cachep();
  1817. free_compress:
  1818. btrfs_exit_compress();
  1819. btrfs_exit_sysfs();
  1820. free_hash:
  1821. btrfs_hash_exit();
  1822. return err;
  1823. }
  1824. static void __exit exit_btrfs_fs(void)
  1825. {
  1826. btrfs_destroy_cachep();
  1827. btrfs_delayed_ref_exit();
  1828. btrfs_auto_defrag_exit();
  1829. btrfs_delayed_inode_exit();
  1830. btrfs_prelim_ref_exit();
  1831. ordered_data_exit();
  1832. extent_map_exit();
  1833. extent_io_exit();
  1834. btrfs_interface_exit();
  1835. unregister_filesystem(&btrfs_fs_type);
  1836. btrfs_exit_sysfs();
  1837. btrfs_cleanup_fs_uuids();
  1838. btrfs_exit_compress();
  1839. btrfs_hash_exit();
  1840. }
  1841. late_initcall(init_btrfs_fs);
  1842. module_exit(exit_btrfs_fs)
  1843. MODULE_LICENSE("GPL");