super.c 65 KB

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