super.c 52 KB

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