xfs_sb.c 25 KB

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