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