super.c 65 KB

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