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