xfs_sb.c 24 KB

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