xfs_sb.c 24 KB

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