xfs_sb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_ialloc.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_cksum.h"
  34. #include "xfs_trans.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_alloc_btree.h"
  38. #include "xfs_ialloc_btree.h"
  39. #include "xfs_log.h"
  40. #include "xfs_rmap_btree.h"
  41. #include "xfs_bmap.h"
  42. #include "xfs_refcount_btree.h"
  43. /*
  44. * Physical superblock buffer manipulations. Shared with libxfs in userspace.
  45. */
  46. /*
  47. * Reference counting access wrappers to the perag structures.
  48. * Because we never free per-ag structures, the only thing we
  49. * have to protect against changes is the tree structure itself.
  50. */
  51. struct xfs_perag *
  52. xfs_perag_get(
  53. struct xfs_mount *mp,
  54. xfs_agnumber_t agno)
  55. {
  56. struct xfs_perag *pag;
  57. int ref = 0;
  58. rcu_read_lock();
  59. pag = radix_tree_lookup(&mp->m_perag_tree, agno);
  60. if (pag) {
  61. ASSERT(atomic_read(&pag->pag_ref) >= 0);
  62. ref = atomic_inc_return(&pag->pag_ref);
  63. }
  64. rcu_read_unlock();
  65. trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
  66. return pag;
  67. }
  68. /*
  69. * search from @first to find the next perag with the given tag set.
  70. */
  71. struct xfs_perag *
  72. xfs_perag_get_tag(
  73. struct xfs_mount *mp,
  74. xfs_agnumber_t first,
  75. int tag)
  76. {
  77. struct xfs_perag *pag;
  78. int found;
  79. int ref;
  80. rcu_read_lock();
  81. found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
  82. (void **)&pag, first, 1, tag);
  83. if (found <= 0) {
  84. rcu_read_unlock();
  85. return NULL;
  86. }
  87. ref = atomic_inc_return(&pag->pag_ref);
  88. rcu_read_unlock();
  89. trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
  90. return pag;
  91. }
  92. void
  93. xfs_perag_put(
  94. struct xfs_perag *pag)
  95. {
  96. int ref;
  97. ASSERT(atomic_read(&pag->pag_ref) > 0);
  98. ref = atomic_dec_return(&pag->pag_ref);
  99. trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
  100. }
  101. /*
  102. * Check the validity of the SB found.
  103. */
  104. STATIC int
  105. xfs_mount_validate_sb(
  106. xfs_mount_t *mp,
  107. xfs_sb_t *sbp,
  108. bool check_inprogress,
  109. bool check_version)
  110. {
  111. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  112. xfs_warn(mp, "bad magic number");
  113. return -EWRONGFS;
  114. }
  115. if (!xfs_sb_good_version(sbp)) {
  116. xfs_warn(mp, "bad version");
  117. return -EWRONGFS;
  118. }
  119. /*
  120. * Version 5 superblock feature mask validation. Reject combinations the
  121. * kernel cannot support up front before checking anything else. For
  122. * write validation, we don't need to check feature masks.
  123. */
  124. if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
  125. if (xfs_sb_has_compat_feature(sbp,
  126. XFS_SB_FEAT_COMPAT_UNKNOWN)) {
  127. xfs_warn(mp,
  128. "Superblock has unknown compatible features (0x%x) enabled.",
  129. (sbp->sb_features_compat &
  130. XFS_SB_FEAT_COMPAT_UNKNOWN));
  131. xfs_warn(mp,
  132. "Using a more recent kernel is recommended.");
  133. }
  134. if (xfs_sb_has_ro_compat_feature(sbp,
  135. XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
  136. xfs_alert(mp,
  137. "Superblock has unknown read-only compatible features (0x%x) enabled.",
  138. (sbp->sb_features_ro_compat &
  139. XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
  140. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  141. xfs_warn(mp,
  142. "Attempted to mount read-only compatible filesystem read-write.");
  143. xfs_warn(mp,
  144. "Filesystem can only be safely mounted read only.");
  145. return -EINVAL;
  146. }
  147. }
  148. if (xfs_sb_has_incompat_feature(sbp,
  149. XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
  150. xfs_warn(mp,
  151. "Superblock has unknown incompatible features (0x%x) enabled.",
  152. (sbp->sb_features_incompat &
  153. XFS_SB_FEAT_INCOMPAT_UNKNOWN));
  154. xfs_warn(mp,
  155. "Filesystem can not be safely mounted by this kernel.");
  156. return -EINVAL;
  157. }
  158. } else if (xfs_sb_version_hascrc(sbp)) {
  159. /*
  160. * We can't read verify the sb LSN because the read verifier is
  161. * called before the log is allocated and processed. We know the
  162. * log is set up before write verifier (!check_version) calls,
  163. * so just check it here.
  164. */
  165. if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
  166. return -EFSCORRUPTED;
  167. }
  168. if (xfs_sb_version_has_pquotino(sbp)) {
  169. if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
  170. xfs_notice(mp,
  171. "Version 5 of Super block has XFS_OQUOTA bits.");
  172. return -EFSCORRUPTED;
  173. }
  174. } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
  175. XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
  176. xfs_notice(mp,
  177. "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
  178. return -EFSCORRUPTED;
  179. }
  180. /*
  181. * Full inode chunks must be aligned to inode chunk size when
  182. * sparse inodes are enabled to support the sparse chunk
  183. * allocation algorithm and prevent overlapping inode records.
  184. */
  185. if (xfs_sb_version_hassparseinodes(sbp)) {
  186. uint32_t align;
  187. align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
  188. >> sbp->sb_blocklog;
  189. if (sbp->sb_inoalignmt != align) {
  190. xfs_warn(mp,
  191. "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
  192. sbp->sb_inoalignmt, align);
  193. return -EINVAL;
  194. }
  195. }
  196. if (unlikely(
  197. sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
  198. xfs_warn(mp,
  199. "filesystem is marked as having an external log; "
  200. "specify logdev on the mount command line.");
  201. return -EINVAL;
  202. }
  203. if (unlikely(
  204. sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
  205. xfs_warn(mp,
  206. "filesystem is marked as having an internal log; "
  207. "do not specify logdev on the mount command line.");
  208. return -EINVAL;
  209. }
  210. /*
  211. * More sanity checking. Most of these were stolen directly from
  212. * xfs_repair.
  213. */
  214. if (unlikely(
  215. sbp->sb_agcount <= 0 ||
  216. sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
  217. sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
  218. sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
  219. sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
  220. sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
  221. sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
  222. sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
  223. sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
  224. sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  225. sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
  226. sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  227. sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
  228. sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
  229. sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
  230. sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
  231. sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
  232. sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
  233. sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
  234. (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
  235. (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
  236. (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
  237. (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
  238. sbp->sb_dblocks == 0 ||
  239. sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
  240. sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
  241. sbp->sb_shared_vn != 0)) {
  242. xfs_notice(mp, "SB sanity check failed");
  243. return -EFSCORRUPTED;
  244. }
  245. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  246. sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
  247. xfs_notice(mp, "v5 SB sanity check failed");
  248. return -EFSCORRUPTED;
  249. }
  250. /*
  251. * Until this is fixed only page-sized or smaller data blocks work.
  252. */
  253. if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
  254. xfs_warn(mp,
  255. "File system with blocksize %d bytes. "
  256. "Only pagesize (%ld) or less will currently work.",
  257. sbp->sb_blocksize, PAGE_SIZE);
  258. return -ENOSYS;
  259. }
  260. /*
  261. * Currently only very few inode sizes are supported.
  262. */
  263. switch (sbp->sb_inodesize) {
  264. case 256:
  265. case 512:
  266. case 1024:
  267. case 2048:
  268. break;
  269. default:
  270. xfs_warn(mp, "inode size of %d bytes not supported",
  271. sbp->sb_inodesize);
  272. return -ENOSYS;
  273. }
  274. if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
  275. xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
  276. xfs_warn(mp,
  277. "file system too large to be mounted on this system.");
  278. return -EFBIG;
  279. }
  280. if (check_inprogress && sbp->sb_inprogress) {
  281. xfs_warn(mp, "Offline file system operation in progress!");
  282. return -EFSCORRUPTED;
  283. }
  284. return 0;
  285. }
  286. void
  287. xfs_sb_quota_from_disk(struct xfs_sb *sbp)
  288. {
  289. /*
  290. * older mkfs doesn't initialize quota inodes to NULLFSINO. This
  291. * leads to in-core values having two different values for a quota
  292. * inode to be invalid: 0 and NULLFSINO. Change it to a single value
  293. * NULLFSINO.
  294. *
  295. * Note that this change affect only the in-core values. These
  296. * values are not written back to disk unless any quota information
  297. * is written to the disk. Even in that case, sb_pquotino field is
  298. * not written to disk unless the superblock supports pquotino.
  299. */
  300. if (sbp->sb_uquotino == 0)
  301. sbp->sb_uquotino = NULLFSINO;
  302. if (sbp->sb_gquotino == 0)
  303. sbp->sb_gquotino = NULLFSINO;
  304. if (sbp->sb_pquotino == 0)
  305. sbp->sb_pquotino = NULLFSINO;
  306. /*
  307. * We need to do these manipilations only if we are working
  308. * with an older version of on-disk superblock.
  309. */
  310. if (xfs_sb_version_has_pquotino(sbp))
  311. return;
  312. if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
  313. sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
  314. XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
  315. if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
  316. sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
  317. XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
  318. sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
  319. if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
  320. sbp->sb_gquotino != NULLFSINO) {
  321. /*
  322. * In older version of superblock, on-disk superblock only
  323. * has sb_gquotino, and in-core superblock has both sb_gquotino
  324. * and sb_pquotino. But, only one of them is supported at any
  325. * point of time. So, if PQUOTA is set in disk superblock,
  326. * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
  327. * above is to make sure we don't do this twice and wipe them
  328. * both out!
  329. */
  330. sbp->sb_pquotino = sbp->sb_gquotino;
  331. sbp->sb_gquotino = NULLFSINO;
  332. }
  333. }
  334. static void
  335. __xfs_sb_from_disk(
  336. struct xfs_sb *to,
  337. xfs_dsb_t *from,
  338. bool convert_xquota)
  339. {
  340. to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
  341. to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
  342. to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
  343. to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
  344. to->sb_rextents = be64_to_cpu(from->sb_rextents);
  345. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  346. to->sb_logstart = be64_to_cpu(from->sb_logstart);
  347. to->sb_rootino = be64_to_cpu(from->sb_rootino);
  348. to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
  349. to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
  350. to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
  351. to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
  352. to->sb_agcount = be32_to_cpu(from->sb_agcount);
  353. to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
  354. to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
  355. to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
  356. to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
  357. to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
  358. to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
  359. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  360. to->sb_blocklog = from->sb_blocklog;
  361. to->sb_sectlog = from->sb_sectlog;
  362. to->sb_inodelog = from->sb_inodelog;
  363. to->sb_inopblog = from->sb_inopblog;
  364. to->sb_agblklog = from->sb_agblklog;
  365. to->sb_rextslog = from->sb_rextslog;
  366. to->sb_inprogress = from->sb_inprogress;
  367. to->sb_imax_pct = from->sb_imax_pct;
  368. to->sb_icount = be64_to_cpu(from->sb_icount);
  369. to->sb_ifree = be64_to_cpu(from->sb_ifree);
  370. to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
  371. to->sb_frextents = be64_to_cpu(from->sb_frextents);
  372. to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
  373. to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
  374. to->sb_qflags = be16_to_cpu(from->sb_qflags);
  375. to->sb_flags = from->sb_flags;
  376. to->sb_shared_vn = from->sb_shared_vn;
  377. to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
  378. to->sb_unit = be32_to_cpu(from->sb_unit);
  379. to->sb_width = be32_to_cpu(from->sb_width);
  380. to->sb_dirblklog = from->sb_dirblklog;
  381. to->sb_logsectlog = from->sb_logsectlog;
  382. to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
  383. to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
  384. to->sb_features2 = be32_to_cpu(from->sb_features2);
  385. to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
  386. to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
  387. to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
  388. to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
  389. to->sb_features_log_incompat =
  390. be32_to_cpu(from->sb_features_log_incompat);
  391. /* crc is only used on disk, not in memory; just init to 0 here. */
  392. to->sb_crc = 0;
  393. to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
  394. to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
  395. to->sb_lsn = be64_to_cpu(from->sb_lsn);
  396. /*
  397. * sb_meta_uuid is only on disk if it differs from sb_uuid and the
  398. * feature flag is set; if not set we keep it only in memory.
  399. */
  400. if (xfs_sb_version_hasmetauuid(to))
  401. uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
  402. else
  403. uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
  404. /* Convert on-disk flags to in-memory flags? */
  405. if (convert_xquota)
  406. xfs_sb_quota_from_disk(to);
  407. }
  408. void
  409. xfs_sb_from_disk(
  410. struct xfs_sb *to,
  411. xfs_dsb_t *from)
  412. {
  413. __xfs_sb_from_disk(to, from, true);
  414. }
  415. static void
  416. xfs_sb_quota_to_disk(
  417. struct xfs_dsb *to,
  418. struct xfs_sb *from)
  419. {
  420. __uint16_t qflags = from->sb_qflags;
  421. to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
  422. if (xfs_sb_version_has_pquotino(from)) {
  423. to->sb_qflags = cpu_to_be16(from->sb_qflags);
  424. to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
  425. to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
  426. return;
  427. }
  428. /*
  429. * The in-core version of sb_qflags do not have XFS_OQUOTA_*
  430. * flags, whereas the on-disk version does. So, convert incore
  431. * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
  432. */
  433. qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
  434. XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
  435. if (from->sb_qflags &
  436. (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
  437. qflags |= XFS_OQUOTA_ENFD;
  438. if (from->sb_qflags &
  439. (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
  440. qflags |= XFS_OQUOTA_CHKD;
  441. to->sb_qflags = cpu_to_be16(qflags);
  442. /*
  443. * GQUOTINO and PQUOTINO cannot be used together in versions
  444. * of superblock that do not have pquotino. from->sb_flags
  445. * tells us which quota is active and should be copied to
  446. * disk. If neither are active, we should NULL the inode.
  447. *
  448. * In all cases, the separate pquotino must remain 0 because it
  449. * it beyond the "end" of the valid non-pquotino superblock.
  450. */
  451. if (from->sb_qflags & XFS_GQUOTA_ACCT)
  452. to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
  453. else if (from->sb_qflags & XFS_PQUOTA_ACCT)
  454. to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
  455. else {
  456. /*
  457. * We can't rely on just the fields being logged to tell us
  458. * that it is safe to write NULLFSINO - we should only do that
  459. * if quotas are not actually enabled. Hence only write
  460. * NULLFSINO if both in-core quota inodes are NULL.
  461. */
  462. if (from->sb_gquotino == NULLFSINO &&
  463. from->sb_pquotino == NULLFSINO)
  464. to->sb_gquotino = cpu_to_be64(NULLFSINO);
  465. }
  466. to->sb_pquotino = 0;
  467. }
  468. void
  469. xfs_sb_to_disk(
  470. struct xfs_dsb *to,
  471. struct xfs_sb *from)
  472. {
  473. xfs_sb_quota_to_disk(to, from);
  474. to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
  475. to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
  476. to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
  477. to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
  478. to->sb_rextents = cpu_to_be64(from->sb_rextents);
  479. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  480. to->sb_logstart = cpu_to_be64(from->sb_logstart);
  481. to->sb_rootino = cpu_to_be64(from->sb_rootino);
  482. to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
  483. to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
  484. to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
  485. to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
  486. to->sb_agcount = cpu_to_be32(from->sb_agcount);
  487. to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
  488. to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
  489. to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
  490. to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
  491. to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
  492. to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
  493. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  494. to->sb_blocklog = from->sb_blocklog;
  495. to->sb_sectlog = from->sb_sectlog;
  496. to->sb_inodelog = from->sb_inodelog;
  497. to->sb_inopblog = from->sb_inopblog;
  498. to->sb_agblklog = from->sb_agblklog;
  499. to->sb_rextslog = from->sb_rextslog;
  500. to->sb_inprogress = from->sb_inprogress;
  501. to->sb_imax_pct = from->sb_imax_pct;
  502. to->sb_icount = cpu_to_be64(from->sb_icount);
  503. to->sb_ifree = cpu_to_be64(from->sb_ifree);
  504. to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
  505. to->sb_frextents = cpu_to_be64(from->sb_frextents);
  506. to->sb_flags = from->sb_flags;
  507. to->sb_shared_vn = from->sb_shared_vn;
  508. to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
  509. to->sb_unit = cpu_to_be32(from->sb_unit);
  510. to->sb_width = cpu_to_be32(from->sb_width);
  511. to->sb_dirblklog = from->sb_dirblklog;
  512. to->sb_logsectlog = from->sb_logsectlog;
  513. to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
  514. to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
  515. /*
  516. * We need to ensure that bad_features2 always matches features2.
  517. * Hence we enforce that here rather than having to remember to do it
  518. * everywhere else that updates features2.
  519. */
  520. from->sb_bad_features2 = from->sb_features2;
  521. to->sb_features2 = cpu_to_be32(from->sb_features2);
  522. to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
  523. if (xfs_sb_version_hascrc(from)) {
  524. to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
  525. to->sb_features_ro_compat =
  526. cpu_to_be32(from->sb_features_ro_compat);
  527. to->sb_features_incompat =
  528. cpu_to_be32(from->sb_features_incompat);
  529. to->sb_features_log_incompat =
  530. cpu_to_be32(from->sb_features_log_incompat);
  531. to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
  532. to->sb_lsn = cpu_to_be64(from->sb_lsn);
  533. if (xfs_sb_version_hasmetauuid(from))
  534. uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
  535. }
  536. }
  537. static int
  538. xfs_sb_verify(
  539. struct xfs_buf *bp,
  540. bool check_version)
  541. {
  542. struct xfs_mount *mp = bp->b_target->bt_mount;
  543. struct xfs_sb sb;
  544. /*
  545. * Use call variant which doesn't convert quota flags from disk
  546. * format, because xfs_mount_validate_sb checks the on-disk flags.
  547. */
  548. __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
  549. /*
  550. * Only check the in progress field for the primary superblock as
  551. * mkfs.xfs doesn't clear it from secondary superblocks.
  552. */
  553. return xfs_mount_validate_sb(mp, &sb,
  554. bp->b_maps[0].bm_bn == XFS_SB_DADDR,
  555. check_version);
  556. }
  557. /*
  558. * If the superblock has the CRC feature bit set or the CRC field is non-null,
  559. * check that the CRC is valid. We check the CRC field is non-null because a
  560. * single bit error could clear the feature bit and unused parts of the
  561. * superblock are supposed to be zero. Hence a non-null crc field indicates that
  562. * we've potentially lost a feature bit and we should check it anyway.
  563. *
  564. * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
  565. * last field in V4 secondary superblocks. So for secondary superblocks,
  566. * we are more forgiving, and ignore CRC failures if the primary doesn't
  567. * indicate that the fs version is V5.
  568. */
  569. static void
  570. xfs_sb_read_verify(
  571. struct xfs_buf *bp)
  572. {
  573. struct xfs_mount *mp = bp->b_target->bt_mount;
  574. struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
  575. int error;
  576. /*
  577. * open code the version check to avoid needing to convert the entire
  578. * superblock from disk order just to check the version number
  579. */
  580. if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
  581. (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
  582. XFS_SB_VERSION_5) ||
  583. dsb->sb_crc != 0)) {
  584. if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
  585. /* Only fail bad secondaries on a known V5 filesystem */
  586. if (bp->b_bn == XFS_SB_DADDR ||
  587. xfs_sb_version_hascrc(&mp->m_sb)) {
  588. error = -EFSBADCRC;
  589. goto out_error;
  590. }
  591. }
  592. }
  593. error = xfs_sb_verify(bp, true);
  594. out_error:
  595. if (error) {
  596. xfs_buf_ioerror(bp, error);
  597. if (error == -EFSCORRUPTED || error == -EFSBADCRC)
  598. xfs_verifier_error(bp);
  599. }
  600. }
  601. /*
  602. * We may be probed for a filesystem match, so we may not want to emit
  603. * messages when the superblock buffer is not actually an XFS superblock.
  604. * If we find an XFS superblock, then run a normal, noisy mount because we are
  605. * really going to mount it and want to know about errors.
  606. */
  607. static void
  608. xfs_sb_quiet_read_verify(
  609. struct xfs_buf *bp)
  610. {
  611. struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
  612. if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
  613. /* XFS filesystem, verify noisily! */
  614. xfs_sb_read_verify(bp);
  615. return;
  616. }
  617. /* quietly fail */
  618. xfs_buf_ioerror(bp, -EWRONGFS);
  619. }
  620. static void
  621. xfs_sb_write_verify(
  622. struct xfs_buf *bp)
  623. {
  624. struct xfs_mount *mp = bp->b_target->bt_mount;
  625. struct xfs_buf_log_item *bip = bp->b_fspriv;
  626. int error;
  627. error = xfs_sb_verify(bp, false);
  628. if (error) {
  629. xfs_buf_ioerror(bp, error);
  630. xfs_verifier_error(bp);
  631. return;
  632. }
  633. if (!xfs_sb_version_hascrc(&mp->m_sb))
  634. return;
  635. if (bip)
  636. XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  637. xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
  638. }
  639. const struct xfs_buf_ops xfs_sb_buf_ops = {
  640. .name = "xfs_sb",
  641. .verify_read = xfs_sb_read_verify,
  642. .verify_write = xfs_sb_write_verify,
  643. };
  644. const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
  645. .name = "xfs_sb_quiet",
  646. .verify_read = xfs_sb_quiet_read_verify,
  647. .verify_write = xfs_sb_write_verify,
  648. };
  649. /*
  650. * xfs_mount_common
  651. *
  652. * Mount initialization code establishing various mount
  653. * fields from the superblock associated with the given
  654. * mount structure
  655. */
  656. void
  657. xfs_sb_mount_common(
  658. struct xfs_mount *mp,
  659. struct xfs_sb *sbp)
  660. {
  661. mp->m_agfrotor = mp->m_agirotor = 0;
  662. spin_lock_init(&mp->m_agirotor_lock);
  663. mp->m_maxagi = mp->m_sb.sb_agcount;
  664. mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
  665. mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
  666. mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
  667. mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
  668. mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
  669. mp->m_blockmask = sbp->sb_blocksize - 1;
  670. mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
  671. mp->m_blockwmask = mp->m_blockwsize - 1;
  672. mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
  673. mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
  674. mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
  675. mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
  676. mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
  677. mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
  678. mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
  679. mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
  680. mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
  681. mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
  682. mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
  683. mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
  684. mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, 1);
  685. mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, 0);
  686. mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
  687. mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
  688. mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize,
  689. true);
  690. mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize,
  691. false);
  692. mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
  693. mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
  694. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  695. mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
  696. sbp->sb_inopblock);
  697. mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
  698. if (sbp->sb_spino_align)
  699. mp->m_ialloc_min_blks = sbp->sb_spino_align;
  700. else
  701. mp->m_ialloc_min_blks = mp->m_ialloc_blks;
  702. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  703. mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
  704. }
  705. /*
  706. * xfs_initialize_perag_data
  707. *
  708. * Read in each per-ag structure so we can count up the number of
  709. * allocated inodes, free inodes and used filesystem blocks as this
  710. * information is no longer persistent in the superblock. Once we have
  711. * this information, write it into the in-core superblock structure.
  712. */
  713. int
  714. xfs_initialize_perag_data(
  715. struct xfs_mount *mp,
  716. xfs_agnumber_t agcount)
  717. {
  718. xfs_agnumber_t index;
  719. xfs_perag_t *pag;
  720. xfs_sb_t *sbp = &mp->m_sb;
  721. uint64_t ifree = 0;
  722. uint64_t ialloc = 0;
  723. uint64_t bfree = 0;
  724. uint64_t bfreelst = 0;
  725. uint64_t btree = 0;
  726. int error;
  727. for (index = 0; index < agcount; index++) {
  728. /*
  729. * read the agf, then the agi. This gets us
  730. * all the information we need and populates the
  731. * per-ag structures for us.
  732. */
  733. error = xfs_alloc_pagf_init(mp, NULL, index, 0);
  734. if (error)
  735. return error;
  736. error = xfs_ialloc_pagi_init(mp, NULL, index);
  737. if (error)
  738. return error;
  739. pag = xfs_perag_get(mp, index);
  740. ifree += pag->pagi_freecount;
  741. ialloc += pag->pagi_count;
  742. bfree += pag->pagf_freeblks;
  743. bfreelst += pag->pagf_flcount;
  744. btree += pag->pagf_btreeblks;
  745. xfs_perag_put(pag);
  746. }
  747. /* Overwrite incore superblock counters with just-read data */
  748. spin_lock(&mp->m_sb_lock);
  749. sbp->sb_ifree = ifree;
  750. sbp->sb_icount = ialloc;
  751. sbp->sb_fdblocks = bfree + bfreelst + btree;
  752. spin_unlock(&mp->m_sb_lock);
  753. xfs_reinit_percpu_counters(mp);
  754. return 0;
  755. }
  756. /*
  757. * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
  758. * into the superblock buffer to be logged. It does not provide the higher
  759. * level of locking that is needed to protect the in-core superblock from
  760. * concurrent access.
  761. */
  762. void
  763. xfs_log_sb(
  764. struct xfs_trans *tp)
  765. {
  766. struct xfs_mount *mp = tp->t_mountp;
  767. struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
  768. mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
  769. mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
  770. mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
  771. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
  772. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
  773. xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
  774. }
  775. /*
  776. * xfs_sync_sb
  777. *
  778. * Sync the superblock to disk.
  779. *
  780. * Note that the caller is responsible for checking the frozen state of the
  781. * filesystem. This procedure uses the non-blocking transaction allocator and
  782. * thus will allow modifications to a frozen fs. This is required because this
  783. * code can be called during the process of freezing where use of the high-level
  784. * allocator would deadlock.
  785. */
  786. int
  787. xfs_sync_sb(
  788. struct xfs_mount *mp,
  789. bool wait)
  790. {
  791. struct xfs_trans *tp;
  792. int error;
  793. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
  794. XFS_TRANS_NO_WRITECOUNT, &tp);
  795. if (error)
  796. return error;
  797. xfs_log_sb(tp);
  798. if (wait)
  799. xfs_trans_set_sync(tp);
  800. return xfs_trans_commit(tp);
  801. }