xfs_sb.c 25 KB

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