xfs_sb.c 24 KB

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