blob: 584ec896a53374f81da05906d517fba717686504 [file] [log] [blame]
Dave Chinnerff550682013-08-12 20:49:41 +10001/*
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"
Dave Chinner632b89e2013-10-29 22:11:58 +110020#include "xfs_shared.h"
Dave Chinnerff550682013-08-12 20:49:41 +100021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Dave Chinnerff550682013-08-12 20:49:41 +100024#include "xfs_bit.h"
Dave Chinnerff550682013-08-12 20:49:41 +100025#include "xfs_sb.h"
Dave Chinnerff550682013-08-12 20:49:41 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinnerff550682013-08-12 20:49:41 +100028#include "xfs_inode.h"
Dave Chinnerff550682013-08-12 20:49:41 +100029#include "xfs_ialloc.h"
30#include "xfs_alloc.h"
Dave Chinnerff550682013-08-12 20:49:41 +100031#include "xfs_error.h"
Dave Chinnerff550682013-08-12 20:49:41 +100032#include "xfs_trace.h"
33#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.h"
Dave Chinnerff550682013-08-12 20:49:41 +100035#include "xfs_buf_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_bmap_btree.h"
37#include "xfs_alloc_btree.h"
38#include "xfs_ialloc_btree.h"
Brian Fostera45086e2015-10-12 15:59:25 +110039#include "xfs_log.h"
Darrick J. Wong035e00a2016-08-03 11:36:07 +100040#include "xfs_rmap_btree.h"
Darrick J. Wong1946b912016-10-03 09:11:18 -070041#include "xfs_bmap.h"
42#include "xfs_refcount_btree.h"
Dave Chinnerff550682013-08-12 20:49:41 +100043
44/*
45 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
46 */
47
Dave Chinnerff550682013-08-12 20:49:41 +100048/*
49 * Reference counting access wrappers to the perag structures.
50 * Because we never free per-ag structures, the only thing we
51 * have to protect against changes is the tree structure itself.
52 */
53struct xfs_perag *
54xfs_perag_get(
55 struct xfs_mount *mp,
56 xfs_agnumber_t agno)
57{
58 struct xfs_perag *pag;
59 int ref = 0;
60
61 rcu_read_lock();
62 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
63 if (pag) {
64 ASSERT(atomic_read(&pag->pag_ref) >= 0);
65 ref = atomic_inc_return(&pag->pag_ref);
66 }
67 rcu_read_unlock();
68 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
69 return pag;
70}
71
72/*
73 * search from @first to find the next perag with the given tag set.
74 */
75struct xfs_perag *
76xfs_perag_get_tag(
77 struct xfs_mount *mp,
78 xfs_agnumber_t first,
79 int tag)
80{
81 struct xfs_perag *pag;
82 int found;
83 int ref;
84
85 rcu_read_lock();
86 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
87 (void **)&pag, first, 1, tag);
88 if (found <= 0) {
89 rcu_read_unlock();
90 return NULL;
91 }
92 ref = atomic_inc_return(&pag->pag_ref);
93 rcu_read_unlock();
94 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
95 return pag;
96}
97
98void
99xfs_perag_put(
100 struct xfs_perag *pag)
101{
102 int ref;
103
104 ASSERT(atomic_read(&pag->pag_ref) > 0);
105 ref = atomic_dec_return(&pag->pag_ref);
106 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
107}
108
109/*
110 * Check the validity of the SB found.
111 */
112STATIC int
113xfs_mount_validate_sb(
114 xfs_mount_t *mp,
115 xfs_sb_t *sbp,
116 bool check_inprogress,
117 bool check_version)
118{
Dave Chinnerff550682013-08-12 20:49:41 +1000119 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
120 xfs_warn(mp, "bad magic number");
Dave Chinner24513372014-06-25 14:58:08 +1000121 return -EWRONGFS;
Dave Chinnerff550682013-08-12 20:49:41 +1000122 }
123
124
125 if (!xfs_sb_good_version(sbp)) {
126 xfs_warn(mp, "bad version");
Dave Chinner24513372014-06-25 14:58:08 +1000127 return -EWRONGFS;
Dave Chinnerff550682013-08-12 20:49:41 +1000128 }
129
130 /*
131 * Version 5 superblock feature mask validation. Reject combinations the
132 * kernel cannot support up front before checking anything else. For
133 * write validation, we don't need to check feature masks.
134 */
135 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
Dave Chinnerff550682013-08-12 20:49:41 +1000136 if (xfs_sb_has_compat_feature(sbp,
137 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
138 xfs_warn(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000139"Superblock has unknown compatible features (0x%x) enabled.",
Dave Chinnerff550682013-08-12 20:49:41 +1000140 (sbp->sb_features_compat &
141 XFS_SB_FEAT_COMPAT_UNKNOWN));
Joe Perchesf41febd2015-07-29 11:52:04 +1000142 xfs_warn(mp,
143"Using a more recent kernel is recommended.");
Dave Chinnerff550682013-08-12 20:49:41 +1000144 }
145
146 if (xfs_sb_has_ro_compat_feature(sbp,
147 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
148 xfs_alert(mp,
149"Superblock has unknown read-only compatible features (0x%x) enabled.",
150 (sbp->sb_features_ro_compat &
151 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
152 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
153 xfs_warn(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000154"Attempted to mount read-only compatible filesystem read-write.");
155 xfs_warn(mp,
Dave Chinnerff550682013-08-12 20:49:41 +1000156"Filesystem can only be safely mounted read only.");
Joe Perchesf41febd2015-07-29 11:52:04 +1000157
Dave Chinner24513372014-06-25 14:58:08 +1000158 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000159 }
160 }
161 if (xfs_sb_has_incompat_feature(sbp,
162 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
163 xfs_warn(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000164"Superblock has unknown incompatible features (0x%x) enabled.",
Dave Chinnerff550682013-08-12 20:49:41 +1000165 (sbp->sb_features_incompat &
166 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
Joe Perchesf41febd2015-07-29 11:52:04 +1000167 xfs_warn(mp,
168"Filesystem can not be safely mounted by this kernel.");
Dave Chinner24513372014-06-25 14:58:08 +1000169 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000170 }
Brian Fostera45086e2015-10-12 15:59:25 +1100171 } else if (xfs_sb_version_hascrc(sbp)) {
172 /*
173 * We can't read verify the sb LSN because the read verifier is
174 * called before the log is allocated and processed. We know the
175 * log is set up before write verifier (!check_version) calls,
176 * so just check it here.
177 */
178 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
179 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000180 }
181
182 if (xfs_sb_version_has_pquotino(sbp)) {
183 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
184 xfs_notice(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500185 "Version 5 of Super block has XFS_OQUOTA bits.");
Dave Chinner24513372014-06-25 14:58:08 +1000186 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000187 }
188 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
189 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
190 xfs_notice(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500191"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
Dave Chinner24513372014-06-25 14:58:08 +1000192 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000193 }
194
Brian Fostere5376fc2015-05-29 08:57:27 +1000195 /*
196 * Full inode chunks must be aligned to inode chunk size when
197 * sparse inodes are enabled to support the sparse chunk
198 * allocation algorithm and prevent overlapping inode records.
199 */
200 if (xfs_sb_version_hassparseinodes(sbp)) {
201 uint32_t align;
202
Brian Fostere5376fc2015-05-29 08:57:27 +1000203 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
204 >> sbp->sb_blocklog;
205 if (sbp->sb_inoalignmt != align) {
206 xfs_warn(mp,
207"Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
208 sbp->sb_inoalignmt, align);
209 return -EINVAL;
210 }
211 }
212
Dave Chinnerff550682013-08-12 20:49:41 +1000213 if (unlikely(
214 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
215 xfs_warn(mp,
216 "filesystem is marked as having an external log; "
217 "specify logdev on the mount command line.");
Dave Chinner24513372014-06-25 14:58:08 +1000218 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000219 }
220
221 if (unlikely(
222 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
223 xfs_warn(mp,
224 "filesystem is marked as having an internal log; "
225 "do not specify logdev on the mount command line.");
Dave Chinner24513372014-06-25 14:58:08 +1000226 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000227 }
228
229 /*
230 * More sanity checking. Most of these were stolen directly from
231 * xfs_repair.
232 */
233 if (unlikely(
234 sbp->sb_agcount <= 0 ||
235 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
236 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
237 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
238 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
239 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
240 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
241 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
242 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
243 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
244 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
Darrick J. Wong29094162017-02-02 08:56:07 +0100245 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
Dave Chinnerff550682013-08-12 20:49:41 +1000246 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
247 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
248 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
249 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
250 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
Eric Sandeene1b05722014-09-09 11:47:24 +1000251 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
Eric Sandeen392c6de2014-02-07 15:26:11 +1100252 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
Dave Chinnerff550682013-08-12 20:49:41 +1000253 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
254 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
255 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
256 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
257 sbp->sb_dblocks == 0 ||
258 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
Dave Chinnerab3e57b2014-05-20 07:47:05 +1000259 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
260 sbp->sb_shared_vn != 0)) {
Eric Sandeen5ef11eb2014-02-19 15:39:35 +1100261 xfs_notice(mp, "SB sanity check failed");
Dave Chinner24513372014-06-25 14:58:08 +1000262 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000263 }
264
Darrick J. Wong63fa7932017-01-09 16:38:37 +0100265 if (xfs_sb_version_hascrc(&mp->m_sb) &&
266 sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
267 xfs_notice(mp, "v5 SB sanity check failed");
268 return -EFSCORRUPTED;
269 }
270
Dave Chinnerff550682013-08-12 20:49:41 +1000271 /*
272 * Until this is fixed only page-sized or smaller data blocks work.
273 */
274 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
275 xfs_warn(mp,
276 "File system with blocksize %d bytes. "
277 "Only pagesize (%ld) or less will currently work.",
278 sbp->sb_blocksize, PAGE_SIZE);
Dave Chinner24513372014-06-25 14:58:08 +1000279 return -ENOSYS;
Dave Chinnerff550682013-08-12 20:49:41 +1000280 }
281
282 /*
283 * Currently only very few inode sizes are supported.
284 */
285 switch (sbp->sb_inodesize) {
286 case 256:
287 case 512:
288 case 1024:
289 case 2048:
290 break;
291 default:
292 xfs_warn(mp, "inode size of %d bytes not supported",
293 sbp->sb_inodesize);
Dave Chinner24513372014-06-25 14:58:08 +1000294 return -ENOSYS;
Dave Chinnerff550682013-08-12 20:49:41 +1000295 }
296
297 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
298 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
299 xfs_warn(mp,
300 "file system too large to be mounted on this system.");
Dave Chinner24513372014-06-25 14:58:08 +1000301 return -EFBIG;
Dave Chinnerff550682013-08-12 20:49:41 +1000302 }
303
304 if (check_inprogress && sbp->sb_inprogress) {
305 xfs_warn(mp, "Offline file system operation in progress!");
Dave Chinner24513372014-06-25 14:58:08 +1000306 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000307 }
Dave Chinnerff550682013-08-12 20:49:41 +1000308 return 0;
309}
310
311void
312xfs_sb_quota_from_disk(struct xfs_sb *sbp)
313{
314 /*
315 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
316 * leads to in-core values having two different values for a quota
317 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
318 * NULLFSINO.
319 *
320 * Note that this change affect only the in-core values. These
321 * values are not written back to disk unless any quota information
322 * is written to the disk. Even in that case, sb_pquotino field is
323 * not written to disk unless the superblock supports pquotino.
324 */
325 if (sbp->sb_uquotino == 0)
326 sbp->sb_uquotino = NULLFSINO;
327 if (sbp->sb_gquotino == 0)
328 sbp->sb_gquotino = NULLFSINO;
329 if (sbp->sb_pquotino == 0)
330 sbp->sb_pquotino = NULLFSINO;
331
332 /*
333 * We need to do these manipilations only if we are working
334 * with an older version of on-disk superblock.
335 */
336 if (xfs_sb_version_has_pquotino(sbp))
337 return;
338
339 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
340 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
341 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
342 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
343 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
344 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
345 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
346
Eric Sandeenae8b6cb2017-01-09 16:38:32 +0100347 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
348 sbp->sb_gquotino != NULLFSINO) {
Dave Chinnerff550682013-08-12 20:49:41 +1000349 /*
350 * In older version of superblock, on-disk superblock only
351 * has sb_gquotino, and in-core superblock has both sb_gquotino
352 * and sb_pquotino. But, only one of them is supported at any
353 * point of time. So, if PQUOTA is set in disk superblock,
Eric Sandeenae8b6cb2017-01-09 16:38:32 +0100354 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
355 * above is to make sure we don't do this twice and wipe them
356 * both out!
Dave Chinnerff550682013-08-12 20:49:41 +1000357 */
358 sbp->sb_pquotino = sbp->sb_gquotino;
359 sbp->sb_gquotino = NULLFSINO;
360 }
361}
362
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000363static void
364__xfs_sb_from_disk(
Dave Chinnerff550682013-08-12 20:49:41 +1000365 struct xfs_sb *to,
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000366 xfs_dsb_t *from,
367 bool convert_xquota)
Dave Chinnerff550682013-08-12 20:49:41 +1000368{
369 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
370 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
371 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
372 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
373 to->sb_rextents = be64_to_cpu(from->sb_rextents);
374 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
375 to->sb_logstart = be64_to_cpu(from->sb_logstart);
376 to->sb_rootino = be64_to_cpu(from->sb_rootino);
377 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
378 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
379 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
380 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
381 to->sb_agcount = be32_to_cpu(from->sb_agcount);
382 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
383 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
384 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
385 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
386 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
387 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
388 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
389 to->sb_blocklog = from->sb_blocklog;
390 to->sb_sectlog = from->sb_sectlog;
391 to->sb_inodelog = from->sb_inodelog;
392 to->sb_inopblog = from->sb_inopblog;
393 to->sb_agblklog = from->sb_agblklog;
394 to->sb_rextslog = from->sb_rextslog;
395 to->sb_inprogress = from->sb_inprogress;
396 to->sb_imax_pct = from->sb_imax_pct;
397 to->sb_icount = be64_to_cpu(from->sb_icount);
398 to->sb_ifree = be64_to_cpu(from->sb_ifree);
399 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
400 to->sb_frextents = be64_to_cpu(from->sb_frextents);
401 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
402 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
403 to->sb_qflags = be16_to_cpu(from->sb_qflags);
404 to->sb_flags = from->sb_flags;
405 to->sb_shared_vn = from->sb_shared_vn;
406 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
407 to->sb_unit = be32_to_cpu(from->sb_unit);
408 to->sb_width = be32_to_cpu(from->sb_width);
409 to->sb_dirblklog = from->sb_dirblklog;
410 to->sb_logsectlog = from->sb_logsectlog;
411 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
412 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
413 to->sb_features2 = be32_to_cpu(from->sb_features2);
414 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
415 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
416 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
417 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
418 to->sb_features_log_incompat =
419 be32_to_cpu(from->sb_features_log_incompat);
Eric Sandeen04dd1a02014-10-02 09:24:11 +1000420 /* crc is only used on disk, not in memory; just init to 0 here. */
421 to->sb_crc = 0;
Brian Fosterfb4f2b42015-05-29 08:54:03 +1000422 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
Dave Chinnerff550682013-08-12 20:49:41 +1000423 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
424 to->sb_lsn = be64_to_cpu(from->sb_lsn);
Eric Sandeence748ea2015-07-29 11:53:31 +1000425 /*
426 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
427 * feature flag is set; if not set we keep it only in memory.
428 */
429 if (xfs_sb_version_hasmetauuid(to))
430 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
431 else
432 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000433 /* Convert on-disk flags to in-memory flags? */
434 if (convert_xquota)
435 xfs_sb_quota_from_disk(to);
436}
437
438void
439xfs_sb_from_disk(
440 struct xfs_sb *to,
441 xfs_dsb_t *from)
442{
443 __xfs_sb_from_disk(to, from, true);
Dave Chinnerff550682013-08-12 20:49:41 +1000444}
445
Dave Chinner4d11a402015-01-22 09:10:26 +1100446static void
Dave Chinnerff550682013-08-12 20:49:41 +1000447xfs_sb_quota_to_disk(
Dave Chinner4d11a402015-01-22 09:10:26 +1100448 struct xfs_dsb *to,
449 struct xfs_sb *from)
Dave Chinnerff550682013-08-12 20:49:41 +1000450{
451 __uint16_t qflags = from->sb_qflags;
452
Dave Chinner4d11a402015-01-22 09:10:26 +1100453 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
454 if (xfs_sb_version_has_pquotino(from)) {
455 to->sb_qflags = cpu_to_be16(from->sb_qflags);
456 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
457 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
Dave Chinnerff550682013-08-12 20:49:41 +1000458 return;
Dave Chinnerff550682013-08-12 20:49:41 +1000459 }
460
461 /*
Dave Chinner4d11a402015-01-22 09:10:26 +1100462 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
463 * flags, whereas the on-disk version does. So, convert incore
464 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
Dave Chinnerff550682013-08-12 20:49:41 +1000465 */
Dave Chinner4d11a402015-01-22 09:10:26 +1100466 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
467 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
468
469 if (from->sb_qflags &
470 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
471 qflags |= XFS_OQUOTA_ENFD;
472 if (from->sb_qflags &
473 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
474 qflags |= XFS_OQUOTA_CHKD;
475 to->sb_qflags = cpu_to_be16(qflags);
476
477 /*
478 * GQUOTINO and PQUOTINO cannot be used together in versions
479 * of superblock that do not have pquotino. from->sb_flags
480 * tells us which quota is active and should be copied to
481 * disk. If neither are active, we should NULL the inode.
482 *
483 * In all cases, the separate pquotino must remain 0 because it
484 * it beyond the "end" of the valid non-pquotino superblock.
485 */
486 if (from->sb_qflags & XFS_GQUOTA_ACCT)
Dave Chinnerff550682013-08-12 20:49:41 +1000487 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
Dave Chinner4d11a402015-01-22 09:10:26 +1100488 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
Dave Chinnerff550682013-08-12 20:49:41 +1000489 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
Dave Chinner03e01342014-07-15 07:28:41 +1000490 else {
491 /*
492 * We can't rely on just the fields being logged to tell us
493 * that it is safe to write NULLFSINO - we should only do that
494 * if quotas are not actually enabled. Hence only write
495 * NULLFSINO if both in-core quota inodes are NULL.
496 */
497 if (from->sb_gquotino == NULLFSINO &&
498 from->sb_pquotino == NULLFSINO)
499 to->sb_gquotino = cpu_to_be64(NULLFSINO);
500 }
Dave Chinnerff550682013-08-12 20:49:41 +1000501
Dave Chinner4d11a402015-01-22 09:10:26 +1100502 to->sb_pquotino = 0;
Dave Chinnerff550682013-08-12 20:49:41 +1000503}
504
Dave Chinnerff550682013-08-12 20:49:41 +1000505void
506xfs_sb_to_disk(
Dave Chinner4d11a402015-01-22 09:10:26 +1100507 struct xfs_dsb *to,
508 struct xfs_sb *from)
Dave Chinnerff550682013-08-12 20:49:41 +1000509{
Dave Chinner4d11a402015-01-22 09:10:26 +1100510 xfs_sb_quota_to_disk(to, from);
Dave Chinnerff550682013-08-12 20:49:41 +1000511
Dave Chinner4d11a402015-01-22 09:10:26 +1100512 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
513 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
514 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
515 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
516 to->sb_rextents = cpu_to_be64(from->sb_rextents);
517 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
518 to->sb_logstart = cpu_to_be64(from->sb_logstart);
519 to->sb_rootino = cpu_to_be64(from->sb_rootino);
520 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
521 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
522 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
523 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
524 to->sb_agcount = cpu_to_be32(from->sb_agcount);
525 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
526 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
527 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
528 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
529 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
530 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
531 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
532 to->sb_blocklog = from->sb_blocklog;
533 to->sb_sectlog = from->sb_sectlog;
534 to->sb_inodelog = from->sb_inodelog;
535 to->sb_inopblog = from->sb_inopblog;
536 to->sb_agblklog = from->sb_agblklog;
537 to->sb_rextslog = from->sb_rextslog;
538 to->sb_inprogress = from->sb_inprogress;
539 to->sb_imax_pct = from->sb_imax_pct;
540 to->sb_icount = cpu_to_be64(from->sb_icount);
541 to->sb_ifree = cpu_to_be64(from->sb_ifree);
542 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
543 to->sb_frextents = cpu_to_be64(from->sb_frextents);
Dave Chinnerff550682013-08-12 20:49:41 +1000544
Dave Chinner4d11a402015-01-22 09:10:26 +1100545 to->sb_flags = from->sb_flags;
546 to->sb_shared_vn = from->sb_shared_vn;
547 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
548 to->sb_unit = cpu_to_be32(from->sb_unit);
549 to->sb_width = cpu_to_be32(from->sb_width);
550 to->sb_dirblklog = from->sb_dirblklog;
551 to->sb_logsectlog = from->sb_logsectlog;
552 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
553 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
Dave Chinner074e4272015-01-22 09:10:33 +1100554
555 /*
556 * We need to ensure that bad_features2 always matches features2.
557 * Hence we enforce that here rather than having to remember to do it
558 * everywhere else that updates features2.
559 */
560 from->sb_bad_features2 = from->sb_features2;
Dave Chinner4d11a402015-01-22 09:10:26 +1100561 to->sb_features2 = cpu_to_be32(from->sb_features2);
562 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
Dave Chinnerff550682013-08-12 20:49:41 +1000563
Dave Chinner4d11a402015-01-22 09:10:26 +1100564 if (xfs_sb_version_hascrc(from)) {
565 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
566 to->sb_features_ro_compat =
567 cpu_to_be32(from->sb_features_ro_compat);
568 to->sb_features_incompat =
569 cpu_to_be32(from->sb_features_incompat);
570 to->sb_features_log_incompat =
571 cpu_to_be32(from->sb_features_log_incompat);
Brian Fosterfb4f2b42015-05-29 08:54:03 +1000572 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
Dave Chinner4d11a402015-01-22 09:10:26 +1100573 to->sb_lsn = cpu_to_be64(from->sb_lsn);
Eric Sandeence748ea2015-07-29 11:53:31 +1000574 if (xfs_sb_version_hasmetauuid(from))
575 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
Dave Chinnerff550682013-08-12 20:49:41 +1000576 }
577}
578
579static int
580xfs_sb_verify(
581 struct xfs_buf *bp,
582 bool check_version)
583{
584 struct xfs_mount *mp = bp->b_target->bt_mount;
585 struct xfs_sb sb;
586
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000587 /*
588 * Use call variant which doesn't convert quota flags from disk
589 * format, because xfs_mount_validate_sb checks the on-disk flags.
590 */
591 __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
Dave Chinnerff550682013-08-12 20:49:41 +1000592
593 /*
594 * Only check the in progress field for the primary superblock as
595 * mkfs.xfs doesn't clear it from secondary superblocks.
596 */
Dave Chinnerf3d7ebd2016-08-26 16:01:30 +1000597 return xfs_mount_validate_sb(mp, &sb,
598 bp->b_maps[0].bm_bn == XFS_SB_DADDR,
Dave Chinnerff550682013-08-12 20:49:41 +1000599 check_version);
600}
601
602/*
603 * If the superblock has the CRC feature bit set or the CRC field is non-null,
604 * check that the CRC is valid. We check the CRC field is non-null because a
605 * single bit error could clear the feature bit and unused parts of the
606 * superblock are supposed to be zero. Hence a non-null crc field indicates that
607 * we've potentially lost a feature bit and we should check it anyway.
Eric Sandeen10e6e652013-09-09 15:33:29 -0500608 *
609 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
610 * last field in V4 secondary superblocks. So for secondary superblocks,
611 * we are more forgiving, and ignore CRC failures if the primary doesn't
612 * indicate that the fs version is V5.
Dave Chinnerff550682013-08-12 20:49:41 +1000613 */
614static void
615xfs_sb_read_verify(
616 struct xfs_buf *bp)
617{
618 struct xfs_mount *mp = bp->b_target->bt_mount;
619 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
620 int error;
621
622 /*
623 * open code the version check to avoid needing to convert the entire
624 * superblock from disk order just to check the version number
625 */
626 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
627 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
628 XFS_SB_VERSION_5) ||
629 dsb->sb_crc != 0)) {
630
Eric Sandeen51582172014-02-27 15:17:27 +1100631 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
Eric Sandeen10e6e652013-09-09 15:33:29 -0500632 /* Only fail bad secondaries on a known V5 filesystem */
Eric Sandeen7a01e7072014-02-19 15:33:05 +1100633 if (bp->b_bn == XFS_SB_DADDR ||
Eric Sandeen10e6e652013-09-09 15:33:29 -0500634 xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner24513372014-06-25 14:58:08 +1000635 error = -EFSBADCRC;
Eric Sandeen10e6e652013-09-09 15:33:29 -0500636 goto out_error;
637 }
Dave Chinnerff550682013-08-12 20:49:41 +1000638 }
639 }
640 error = xfs_sb_verify(bp, true);
641
642out_error:
643 if (error) {
Dave Chinnerff550682013-08-12 20:49:41 +1000644 xfs_buf_ioerror(bp, error);
Dave Chinner24513372014-06-25 14:58:08 +1000645 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
Eric Sandeence5028c2014-02-27 15:23:10 +1100646 xfs_verifier_error(bp);
Dave Chinnerff550682013-08-12 20:49:41 +1000647 }
648}
649
650/*
651 * We may be probed for a filesystem match, so we may not want to emit
652 * messages when the superblock buffer is not actually an XFS superblock.
Zhi Yong Wu25337872013-08-12 03:15:02 +0000653 * If we find an XFS superblock, then run a normal, noisy mount because we are
Dave Chinnerff550682013-08-12 20:49:41 +1000654 * really going to mount it and want to know about errors.
655 */
656static void
657xfs_sb_quiet_read_verify(
658 struct xfs_buf *bp)
659{
660 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
661
Dave Chinnerff550682013-08-12 20:49:41 +1000662 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
663 /* XFS filesystem, verify noisily! */
664 xfs_sb_read_verify(bp);
665 return;
666 }
667 /* quietly fail */
Dave Chinner24513372014-06-25 14:58:08 +1000668 xfs_buf_ioerror(bp, -EWRONGFS);
Dave Chinnerff550682013-08-12 20:49:41 +1000669}
670
671static void
672xfs_sb_write_verify(
673 struct xfs_buf *bp)
674{
675 struct xfs_mount *mp = bp->b_target->bt_mount;
676 struct xfs_buf_log_item *bip = bp->b_fspriv;
677 int error;
678
679 error = xfs_sb_verify(bp, false);
680 if (error) {
Dave Chinnerff550682013-08-12 20:49:41 +1000681 xfs_buf_ioerror(bp, error);
Eric Sandeence5028c2014-02-27 15:23:10 +1100682 xfs_verifier_error(bp);
Dave Chinnerff550682013-08-12 20:49:41 +1000683 return;
684 }
685
686 if (!xfs_sb_version_hascrc(&mp->m_sb))
687 return;
688
689 if (bip)
690 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
691
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100692 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
Dave Chinnerff550682013-08-12 20:49:41 +1000693}
694
695const struct xfs_buf_ops xfs_sb_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100696 .name = "xfs_sb",
Dave Chinnerff550682013-08-12 20:49:41 +1000697 .verify_read = xfs_sb_read_verify,
698 .verify_write = xfs_sb_write_verify,
699};
700
701const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100702 .name = "xfs_sb_quiet",
Dave Chinnerff550682013-08-12 20:49:41 +1000703 .verify_read = xfs_sb_quiet_read_verify,
704 .verify_write = xfs_sb_write_verify,
705};
706
707/*
708 * xfs_mount_common
709 *
710 * Mount initialization code establishing various mount
711 * fields from the superblock associated with the given
712 * mount structure
713 */
714void
715xfs_sb_mount_common(
716 struct xfs_mount *mp,
717 struct xfs_sb *sbp)
718{
719 mp->m_agfrotor = mp->m_agirotor = 0;
720 spin_lock_init(&mp->m_agirotor_lock);
721 mp->m_maxagi = mp->m_sb.sb_agcount;
722 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
723 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
724 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
725 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
726 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
727 mp->m_blockmask = sbp->sb_blocksize - 1;
728 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
729 mp->m_blockwmask = mp->m_blockwsize - 1;
730
731 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
732 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
733 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
734 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
735
736 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
737 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
738 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
739 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
740
741 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
742 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
743 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
744 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
745
Darrick J. Wong035e00a2016-08-03 11:36:07 +1000746 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, 1);
747 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, 0);
748 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
749 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
750
Darrick J. Wong1946b912016-10-03 09:11:18 -0700751 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize,
752 true);
753 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize,
754 false);
755 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
756 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
757
Dave Chinnerff550682013-08-12 20:49:41 +1000758 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
759 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
760 sbp->sb_inopblock);
761 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
Brian Foster066a1882015-05-29 08:55:20 +1000762
763 if (sbp->sb_spino_align)
764 mp->m_ialloc_min_blks = sbp->sb_spino_align;
765 else
766 mp->m_ialloc_min_blks = mp->m_ialloc_blks;
Darrick J. Wong52548852016-08-03 11:38:24 +1000767 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
768 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
Dave Chinnerff550682013-08-12 20:49:41 +1000769}
770
771/*
772 * xfs_initialize_perag_data
773 *
774 * Read in each per-ag structure so we can count up the number of
775 * allocated inodes, free inodes and used filesystem blocks as this
776 * information is no longer persistent in the superblock. Once we have
777 * this information, write it into the in-core superblock structure.
778 */
779int
780xfs_initialize_perag_data(
781 struct xfs_mount *mp,
782 xfs_agnumber_t agcount)
783{
784 xfs_agnumber_t index;
785 xfs_perag_t *pag;
786 xfs_sb_t *sbp = &mp->m_sb;
787 uint64_t ifree = 0;
788 uint64_t ialloc = 0;
789 uint64_t bfree = 0;
790 uint64_t bfreelst = 0;
791 uint64_t btree = 0;
792 int error;
793
794 for (index = 0; index < agcount; index++) {
795 /*
796 * read the agf, then the agi. This gets us
797 * all the information we need and populates the
798 * per-ag structures for us.
799 */
800 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
801 if (error)
802 return error;
803
804 error = xfs_ialloc_pagi_init(mp, NULL, index);
805 if (error)
806 return error;
807 pag = xfs_perag_get(mp, index);
808 ifree += pag->pagi_freecount;
809 ialloc += pag->pagi_count;
810 bfree += pag->pagf_freeblks;
811 bfreelst += pag->pagf_flcount;
812 btree += pag->pagf_btreeblks;
813 xfs_perag_put(pag);
814 }
Dave Chinner5681ca42015-02-23 21:22:31 +1100815
816 /* Overwrite incore superblock counters with just-read data */
Dave Chinnerff550682013-08-12 20:49:41 +1000817 spin_lock(&mp->m_sb_lock);
818 sbp->sb_ifree = ifree;
819 sbp->sb_icount = ialloc;
820 sbp->sb_fdblocks = bfree + bfreelst + btree;
821 spin_unlock(&mp->m_sb_lock);
822
Dave Chinner5681ca42015-02-23 21:22:31 +1100823 xfs_reinit_percpu_counters(mp);
Dave Chinnerff550682013-08-12 20:49:41 +1000824
825 return 0;
826}
827
828/*
Dave Chinner61e63ec2015-01-22 09:10:31 +1100829 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
830 * into the superblock buffer to be logged. It does not provide the higher
831 * level of locking that is needed to protect the in-core superblock from
832 * concurrent access.
Dave Chinnerff550682013-08-12 20:49:41 +1000833 */
834void
Dave Chinner61e63ec2015-01-22 09:10:31 +1100835xfs_log_sb(
Dave Chinner4d11a402015-01-22 09:10:26 +1100836 struct xfs_trans *tp)
Dave Chinnerff550682013-08-12 20:49:41 +1000837{
Dave Chinner4d11a402015-01-22 09:10:26 +1100838 struct xfs_mount *mp = tp->t_mountp;
839 struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
Dave Chinnerff550682013-08-12 20:49:41 +1000840
Dave Chinner501ab322015-02-23 21:19:28 +1100841 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100842 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100843 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
Dave Chinner501ab322015-02-23 21:19:28 +1100844
Dave Chinner4d11a402015-01-22 09:10:26 +1100845 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
Dave Chinnerff550682013-08-12 20:49:41 +1000846 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
Dave Chinner4d11a402015-01-22 09:10:26 +1100847 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
Dave Chinnerff550682013-08-12 20:49:41 +1000848}
Dave Chinner61e63ec2015-01-22 09:10:31 +1100849
850/*
851 * xfs_sync_sb
852 *
853 * Sync the superblock to disk.
854 *
855 * Note that the caller is responsible for checking the frozen state of the
856 * filesystem. This procedure uses the non-blocking transaction allocator and
857 * thus will allow modifications to a frozen fs. This is required because this
858 * code can be called during the process of freezing where use of the high-level
859 * allocator would deadlock.
860 */
861int
862xfs_sync_sb(
863 struct xfs_mount *mp,
864 bool wait)
865{
866 struct xfs_trans *tp;
867 int error;
868
Christoph Hellwig253f4912016-04-06 09:19:55 +1000869 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
870 XFS_TRANS_NO_WRITECOUNT, &tp);
871 if (error)
Dave Chinner61e63ec2015-01-22 09:10:31 +1100872 return error;
Dave Chinner61e63ec2015-01-22 09:10:31 +1100873
874 xfs_log_sb(tp);
875 if (wait)
876 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000877 return xfs_trans_commit(tp);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100878}