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