super.c 60 KB

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