super.c 66 KB

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