super.c 65 KB

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