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