xfs_sb.c 25 KB

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