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