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