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