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