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