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