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