blob: 2194e5d4eea88f0f11b99d555717484810069aa2 [file] [log] [blame]
Dave Chinnerff550682013-08-12 20:49:41 +10001/*
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#include "xfs_fs.h"
Dave Chinner632b89e2013-10-29 22:11:58 +110020#include "xfs_shared.h"
Dave Chinnerff550682013-08-12 20:49:41 +100021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Dave Chinnerff550682013-08-12 20:49:41 +100024#include "xfs_bit.h"
Dave Chinnerff550682013-08-12 20:49:41 +100025#include "xfs_sb.h"
Dave Chinnerff550682013-08-12 20:49:41 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinnerff550682013-08-12 20:49:41 +100028#include "xfs_inode.h"
Dave Chinnerff550682013-08-12 20:49:41 +100029#include "xfs_ialloc.h"
30#include "xfs_alloc.h"
Dave Chinnerff550682013-08-12 20:49:41 +100031#include "xfs_error.h"
Dave Chinnerff550682013-08-12 20:49:41 +100032#include "xfs_trace.h"
33#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.h"
Dave Chinnerff550682013-08-12 20:49:41 +100035#include "xfs_buf_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_bmap_btree.h"
37#include "xfs_alloc_btree.h"
38#include "xfs_ialloc_btree.h"
Brian Fostera45086e2015-10-12 15:59:25 +110039#include "xfs_log.h"
Dave Chinnerff550682013-08-12 20:49:41 +100040
41/*
42 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
43 */
44
Dave Chinnerff550682013-08-12 20:49:41 +100045/*
46 * Reference counting access wrappers to the perag structures.
47 * Because we never free per-ag structures, the only thing we
48 * have to protect against changes is the tree structure itself.
49 */
50struct xfs_perag *
51xfs_perag_get(
52 struct xfs_mount *mp,
53 xfs_agnumber_t agno)
54{
55 struct xfs_perag *pag;
56 int ref = 0;
57
58 rcu_read_lock();
59 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
60 if (pag) {
61 ASSERT(atomic_read(&pag->pag_ref) >= 0);
62 ref = atomic_inc_return(&pag->pag_ref);
63 }
64 rcu_read_unlock();
65 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
66 return pag;
67}
68
69/*
70 * search from @first to find the next perag with the given tag set.
71 */
72struct xfs_perag *
73xfs_perag_get_tag(
74 struct xfs_mount *mp,
75 xfs_agnumber_t first,
76 int tag)
77{
78 struct xfs_perag *pag;
79 int found;
80 int ref;
81
82 rcu_read_lock();
83 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
84 (void **)&pag, first, 1, tag);
85 if (found <= 0) {
86 rcu_read_unlock();
87 return NULL;
88 }
89 ref = atomic_inc_return(&pag->pag_ref);
90 rcu_read_unlock();
91 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
92 return pag;
93}
94
95void
96xfs_perag_put(
97 struct xfs_perag *pag)
98{
99 int ref;
100
101 ASSERT(atomic_read(&pag->pag_ref) > 0);
102 ref = atomic_dec_return(&pag->pag_ref);
103 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
104}
105
106/*
107 * Check the validity of the SB found.
108 */
109STATIC int
110xfs_mount_validate_sb(
111 xfs_mount_t *mp,
112 xfs_sb_t *sbp,
113 bool check_inprogress,
114 bool check_version)
115{
Dave Chinnerff550682013-08-12 20:49:41 +1000116 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
117 xfs_warn(mp, "bad magic number");
Dave Chinner24513372014-06-25 14:58:08 +1000118 return -EWRONGFS;
Dave Chinnerff550682013-08-12 20:49:41 +1000119 }
120
121
122 if (!xfs_sb_good_version(sbp)) {
123 xfs_warn(mp, "bad version");
Dave Chinner24513372014-06-25 14:58:08 +1000124 return -EWRONGFS;
Dave Chinnerff550682013-08-12 20:49:41 +1000125 }
126
127 /*
128 * Version 5 superblock feature mask validation. Reject combinations the
129 * kernel cannot support up front before checking anything else. For
130 * write validation, we don't need to check feature masks.
131 */
132 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
Dave Chinnerff550682013-08-12 20:49:41 +1000133 if (xfs_sb_has_compat_feature(sbp,
134 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
135 xfs_warn(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000136"Superblock has unknown compatible features (0x%x) enabled.",
Dave Chinnerff550682013-08-12 20:49:41 +1000137 (sbp->sb_features_compat &
138 XFS_SB_FEAT_COMPAT_UNKNOWN));
Joe Perchesf41febd2015-07-29 11:52:04 +1000139 xfs_warn(mp,
140"Using a more recent kernel is recommended.");
Dave Chinnerff550682013-08-12 20:49:41 +1000141 }
142
143 if (xfs_sb_has_ro_compat_feature(sbp,
144 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
145 xfs_alert(mp,
146"Superblock has unknown read-only compatible features (0x%x) enabled.",
147 (sbp->sb_features_ro_compat &
148 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
149 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
150 xfs_warn(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000151"Attempted to mount read-only compatible filesystem read-write.");
152 xfs_warn(mp,
Dave Chinnerff550682013-08-12 20:49:41 +1000153"Filesystem can only be safely mounted read only.");
Joe Perchesf41febd2015-07-29 11:52:04 +1000154
Dave Chinner24513372014-06-25 14:58:08 +1000155 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000156 }
157 }
158 if (xfs_sb_has_incompat_feature(sbp,
159 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
160 xfs_warn(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +1000161"Superblock has unknown incompatible features (0x%x) enabled.",
Dave Chinnerff550682013-08-12 20:49:41 +1000162 (sbp->sb_features_incompat &
163 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
Joe Perchesf41febd2015-07-29 11:52:04 +1000164 xfs_warn(mp,
165"Filesystem can not be safely mounted by this kernel.");
Dave Chinner24513372014-06-25 14:58:08 +1000166 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000167 }
Brian Fostera45086e2015-10-12 15:59:25 +1100168 } else if (xfs_sb_version_hascrc(sbp)) {
169 /*
170 * We can't read verify the sb LSN because the read verifier is
171 * called before the log is allocated and processed. We know the
172 * log is set up before write verifier (!check_version) calls,
173 * so just check it here.
174 */
175 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
176 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000177 }
178
179 if (xfs_sb_version_has_pquotino(sbp)) {
180 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
181 xfs_notice(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500182 "Version 5 of Super block has XFS_OQUOTA bits.");
Dave Chinner24513372014-06-25 14:58:08 +1000183 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000184 }
185 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
186 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
187 xfs_notice(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500188"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
Dave Chinner24513372014-06-25 14:58:08 +1000189 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000190 }
191
Brian Fostere5376fc2015-05-29 08:57:27 +1000192 /*
193 * Full inode chunks must be aligned to inode chunk size when
194 * sparse inodes are enabled to support the sparse chunk
195 * allocation algorithm and prevent overlapping inode records.
196 */
197 if (xfs_sb_version_hassparseinodes(sbp)) {
198 uint32_t align;
199
Brian Fostere5376fc2015-05-29 08:57:27 +1000200 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
201 >> sbp->sb_blocklog;
202 if (sbp->sb_inoalignmt != align) {
203 xfs_warn(mp,
204"Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
205 sbp->sb_inoalignmt, align);
206 return -EINVAL;
207 }
208 }
209
Dave Chinnerff550682013-08-12 20:49:41 +1000210 if (unlikely(
211 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
212 xfs_warn(mp,
213 "filesystem is marked as having an external log; "
214 "specify logdev on the mount command line.");
Dave Chinner24513372014-06-25 14:58:08 +1000215 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000216 }
217
218 if (unlikely(
219 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
220 xfs_warn(mp,
221 "filesystem is marked as having an internal log; "
222 "do not specify logdev on the mount command line.");
Dave Chinner24513372014-06-25 14:58:08 +1000223 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000224 }
225
226 /*
227 * More sanity checking. Most of these were stolen directly from
228 * xfs_repair.
229 */
230 if (unlikely(
231 sbp->sb_agcount <= 0 ||
232 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
233 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
234 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
235 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
236 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
237 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
238 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
239 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
240 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
241 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
Eric Sandeene1b05722014-09-09 11:47:24 +1000242 sbp->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG ||
Dave Chinnerff550682013-08-12 20:49:41 +1000243 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
244 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
245 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
246 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
247 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
Eric Sandeene1b05722014-09-09 11:47:24 +1000248 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
Eric Sandeen392c6de2014-02-07 15:26:11 +1100249 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
Dave Chinnerff550682013-08-12 20:49:41 +1000250 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
251 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
252 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
253 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
254 sbp->sb_dblocks == 0 ||
255 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
Dave Chinnerab3e57b2014-05-20 07:47:05 +1000256 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
257 sbp->sb_shared_vn != 0)) {
Eric Sandeen5ef11eb2014-02-19 15:39:35 +1100258 xfs_notice(mp, "SB sanity check failed");
Dave Chinner24513372014-06-25 14:58:08 +1000259 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000260 }
261
262 /*
263 * Until this is fixed only page-sized or smaller data blocks work.
264 */
265 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
266 xfs_warn(mp,
267 "File system with blocksize %d bytes. "
268 "Only pagesize (%ld) or less will currently work.",
269 sbp->sb_blocksize, PAGE_SIZE);
Dave Chinner24513372014-06-25 14:58:08 +1000270 return -ENOSYS;
Dave Chinnerff550682013-08-12 20:49:41 +1000271 }
272
273 /*
274 * Currently only very few inode sizes are supported.
275 */
276 switch (sbp->sb_inodesize) {
277 case 256:
278 case 512:
279 case 1024:
280 case 2048:
281 break;
282 default:
283 xfs_warn(mp, "inode size of %d bytes not supported",
284 sbp->sb_inodesize);
Dave Chinner24513372014-06-25 14:58:08 +1000285 return -ENOSYS;
Dave Chinnerff550682013-08-12 20:49:41 +1000286 }
287
288 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
289 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
290 xfs_warn(mp,
291 "file system too large to be mounted on this system.");
Dave Chinner24513372014-06-25 14:58:08 +1000292 return -EFBIG;
Dave Chinnerff550682013-08-12 20:49:41 +1000293 }
294
295 if (check_inprogress && sbp->sb_inprogress) {
296 xfs_warn(mp, "Offline file system operation in progress!");
Dave Chinner24513372014-06-25 14:58:08 +1000297 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000298 }
Dave Chinnerff550682013-08-12 20:49:41 +1000299 return 0;
300}
301
302void
303xfs_sb_quota_from_disk(struct xfs_sb *sbp)
304{
305 /*
306 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
307 * leads to in-core values having two different values for a quota
308 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
309 * NULLFSINO.
310 *
311 * Note that this change affect only the in-core values. These
312 * values are not written back to disk unless any quota information
313 * is written to the disk. Even in that case, sb_pquotino field is
314 * not written to disk unless the superblock supports pquotino.
315 */
316 if (sbp->sb_uquotino == 0)
317 sbp->sb_uquotino = NULLFSINO;
318 if (sbp->sb_gquotino == 0)
319 sbp->sb_gquotino = NULLFSINO;
320 if (sbp->sb_pquotino == 0)
321 sbp->sb_pquotino = NULLFSINO;
322
323 /*
324 * We need to do these manipilations only if we are working
325 * with an older version of on-disk superblock.
326 */
327 if (xfs_sb_version_has_pquotino(sbp))
328 return;
329
330 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
331 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
332 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
333 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
334 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
335 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
336 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
337
338 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
339 /*
340 * In older version of superblock, on-disk superblock only
341 * has sb_gquotino, and in-core superblock has both sb_gquotino
342 * and sb_pquotino. But, only one of them is supported at any
343 * point of time. So, if PQUOTA is set in disk superblock,
344 * copy over sb_gquotino to sb_pquotino.
345 */
346 sbp->sb_pquotino = sbp->sb_gquotino;
347 sbp->sb_gquotino = NULLFSINO;
348 }
349}
350
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000351static void
352__xfs_sb_from_disk(
Dave Chinnerff550682013-08-12 20:49:41 +1000353 struct xfs_sb *to,
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000354 xfs_dsb_t *from,
355 bool convert_xquota)
Dave Chinnerff550682013-08-12 20:49:41 +1000356{
357 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
358 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
359 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
360 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
361 to->sb_rextents = be64_to_cpu(from->sb_rextents);
362 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
363 to->sb_logstart = be64_to_cpu(from->sb_logstart);
364 to->sb_rootino = be64_to_cpu(from->sb_rootino);
365 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
366 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
367 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
368 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
369 to->sb_agcount = be32_to_cpu(from->sb_agcount);
370 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
371 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
372 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
373 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
374 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
375 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
376 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
377 to->sb_blocklog = from->sb_blocklog;
378 to->sb_sectlog = from->sb_sectlog;
379 to->sb_inodelog = from->sb_inodelog;
380 to->sb_inopblog = from->sb_inopblog;
381 to->sb_agblklog = from->sb_agblklog;
382 to->sb_rextslog = from->sb_rextslog;
383 to->sb_inprogress = from->sb_inprogress;
384 to->sb_imax_pct = from->sb_imax_pct;
385 to->sb_icount = be64_to_cpu(from->sb_icount);
386 to->sb_ifree = be64_to_cpu(from->sb_ifree);
387 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
388 to->sb_frextents = be64_to_cpu(from->sb_frextents);
389 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
390 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
391 to->sb_qflags = be16_to_cpu(from->sb_qflags);
392 to->sb_flags = from->sb_flags;
393 to->sb_shared_vn = from->sb_shared_vn;
394 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
395 to->sb_unit = be32_to_cpu(from->sb_unit);
396 to->sb_width = be32_to_cpu(from->sb_width);
397 to->sb_dirblklog = from->sb_dirblklog;
398 to->sb_logsectlog = from->sb_logsectlog;
399 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
400 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
401 to->sb_features2 = be32_to_cpu(from->sb_features2);
402 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
403 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
404 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
405 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
406 to->sb_features_log_incompat =
407 be32_to_cpu(from->sb_features_log_incompat);
Eric Sandeen04dd1a02014-10-02 09:24:11 +1000408 /* crc is only used on disk, not in memory; just init to 0 here. */
409 to->sb_crc = 0;
Brian Fosterfb4f2b42015-05-29 08:54:03 +1000410 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
Dave Chinnerff550682013-08-12 20:49:41 +1000411 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
412 to->sb_lsn = be64_to_cpu(from->sb_lsn);
Eric Sandeence748ea2015-07-29 11:53:31 +1000413 /*
414 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
415 * feature flag is set; if not set we keep it only in memory.
416 */
417 if (xfs_sb_version_hasmetauuid(to))
418 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
419 else
420 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000421 /* Convert on-disk flags to in-memory flags? */
422 if (convert_xquota)
423 xfs_sb_quota_from_disk(to);
424}
425
426void
427xfs_sb_from_disk(
428 struct xfs_sb *to,
429 xfs_dsb_t *from)
430{
431 __xfs_sb_from_disk(to, from, true);
Dave Chinnerff550682013-08-12 20:49:41 +1000432}
433
Dave Chinner4d11a402015-01-22 09:10:26 +1100434static void
Dave Chinnerff550682013-08-12 20:49:41 +1000435xfs_sb_quota_to_disk(
Dave Chinner4d11a402015-01-22 09:10:26 +1100436 struct xfs_dsb *to,
437 struct xfs_sb *from)
Dave Chinnerff550682013-08-12 20:49:41 +1000438{
439 __uint16_t qflags = from->sb_qflags;
440
Dave Chinner4d11a402015-01-22 09:10:26 +1100441 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
442 if (xfs_sb_version_has_pquotino(from)) {
443 to->sb_qflags = cpu_to_be16(from->sb_qflags);
444 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
445 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
Dave Chinnerff550682013-08-12 20:49:41 +1000446 return;
Dave Chinnerff550682013-08-12 20:49:41 +1000447 }
448
449 /*
Dave Chinner4d11a402015-01-22 09:10:26 +1100450 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
451 * flags, whereas the on-disk version does. So, convert incore
452 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
Dave Chinnerff550682013-08-12 20:49:41 +1000453 */
Dave Chinner4d11a402015-01-22 09:10:26 +1100454 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
455 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
456
457 if (from->sb_qflags &
458 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
459 qflags |= XFS_OQUOTA_ENFD;
460 if (from->sb_qflags &
461 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
462 qflags |= XFS_OQUOTA_CHKD;
463 to->sb_qflags = cpu_to_be16(qflags);
464
465 /*
466 * GQUOTINO and PQUOTINO cannot be used together in versions
467 * of superblock that do not have pquotino. from->sb_flags
468 * tells us which quota is active and should be copied to
469 * disk. If neither are active, we should NULL the inode.
470 *
471 * In all cases, the separate pquotino must remain 0 because it
472 * it beyond the "end" of the valid non-pquotino superblock.
473 */
474 if (from->sb_qflags & XFS_GQUOTA_ACCT)
Dave Chinnerff550682013-08-12 20:49:41 +1000475 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
Dave Chinner4d11a402015-01-22 09:10:26 +1100476 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
Dave Chinnerff550682013-08-12 20:49:41 +1000477 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
Dave Chinner03e01342014-07-15 07:28:41 +1000478 else {
479 /*
480 * We can't rely on just the fields being logged to tell us
481 * that it is safe to write NULLFSINO - we should only do that
482 * if quotas are not actually enabled. Hence only write
483 * NULLFSINO if both in-core quota inodes are NULL.
484 */
485 if (from->sb_gquotino == NULLFSINO &&
486 from->sb_pquotino == NULLFSINO)
487 to->sb_gquotino = cpu_to_be64(NULLFSINO);
488 }
Dave Chinnerff550682013-08-12 20:49:41 +1000489
Dave Chinner4d11a402015-01-22 09:10:26 +1100490 to->sb_pquotino = 0;
Dave Chinnerff550682013-08-12 20:49:41 +1000491}
492
Dave Chinnerff550682013-08-12 20:49:41 +1000493void
494xfs_sb_to_disk(
Dave Chinner4d11a402015-01-22 09:10:26 +1100495 struct xfs_dsb *to,
496 struct xfs_sb *from)
Dave Chinnerff550682013-08-12 20:49:41 +1000497{
Dave Chinner4d11a402015-01-22 09:10:26 +1100498 xfs_sb_quota_to_disk(to, from);
Dave Chinnerff550682013-08-12 20:49:41 +1000499
Dave Chinner4d11a402015-01-22 09:10:26 +1100500 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
501 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
502 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
503 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
504 to->sb_rextents = cpu_to_be64(from->sb_rextents);
505 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
506 to->sb_logstart = cpu_to_be64(from->sb_logstart);
507 to->sb_rootino = cpu_to_be64(from->sb_rootino);
508 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
509 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
510 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
511 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
512 to->sb_agcount = cpu_to_be32(from->sb_agcount);
513 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
514 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
515 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
516 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
517 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
518 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
519 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
520 to->sb_blocklog = from->sb_blocklog;
521 to->sb_sectlog = from->sb_sectlog;
522 to->sb_inodelog = from->sb_inodelog;
523 to->sb_inopblog = from->sb_inopblog;
524 to->sb_agblklog = from->sb_agblklog;
525 to->sb_rextslog = from->sb_rextslog;
526 to->sb_inprogress = from->sb_inprogress;
527 to->sb_imax_pct = from->sb_imax_pct;
528 to->sb_icount = cpu_to_be64(from->sb_icount);
529 to->sb_ifree = cpu_to_be64(from->sb_ifree);
530 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
531 to->sb_frextents = cpu_to_be64(from->sb_frextents);
Dave Chinnerff550682013-08-12 20:49:41 +1000532
Dave Chinner4d11a402015-01-22 09:10:26 +1100533 to->sb_flags = from->sb_flags;
534 to->sb_shared_vn = from->sb_shared_vn;
535 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
536 to->sb_unit = cpu_to_be32(from->sb_unit);
537 to->sb_width = cpu_to_be32(from->sb_width);
538 to->sb_dirblklog = from->sb_dirblklog;
539 to->sb_logsectlog = from->sb_logsectlog;
540 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
541 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
Dave Chinner074e4272015-01-22 09:10:33 +1100542
543 /*
544 * We need to ensure that bad_features2 always matches features2.
545 * Hence we enforce that here rather than having to remember to do it
546 * everywhere else that updates features2.
547 */
548 from->sb_bad_features2 = from->sb_features2;
Dave Chinner4d11a402015-01-22 09:10:26 +1100549 to->sb_features2 = cpu_to_be32(from->sb_features2);
550 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
Dave Chinnerff550682013-08-12 20:49:41 +1000551
Dave Chinner4d11a402015-01-22 09:10:26 +1100552 if (xfs_sb_version_hascrc(from)) {
553 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
554 to->sb_features_ro_compat =
555 cpu_to_be32(from->sb_features_ro_compat);
556 to->sb_features_incompat =
557 cpu_to_be32(from->sb_features_incompat);
558 to->sb_features_log_incompat =
559 cpu_to_be32(from->sb_features_log_incompat);
Brian Fosterfb4f2b42015-05-29 08:54:03 +1000560 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
Dave Chinner4d11a402015-01-22 09:10:26 +1100561 to->sb_lsn = cpu_to_be64(from->sb_lsn);
Eric Sandeence748ea2015-07-29 11:53:31 +1000562 if (xfs_sb_version_hasmetauuid(from))
563 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
Dave Chinnerff550682013-08-12 20:49:41 +1000564 }
565}
566
567static int
568xfs_sb_verify(
569 struct xfs_buf *bp,
570 bool check_version)
571{
572 struct xfs_mount *mp = bp->b_target->bt_mount;
573 struct xfs_sb sb;
574
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000575 /*
576 * Use call variant which doesn't convert quota flags from disk
577 * format, because xfs_mount_validate_sb checks the on-disk flags.
578 */
579 __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
Dave Chinnerff550682013-08-12 20:49:41 +1000580
581 /*
582 * Only check the in progress field for the primary superblock as
583 * mkfs.xfs doesn't clear it from secondary superblocks.
584 */
585 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
586 check_version);
587}
588
589/*
590 * If the superblock has the CRC feature bit set or the CRC field is non-null,
591 * check that the CRC is valid. We check the CRC field is non-null because a
592 * single bit error could clear the feature bit and unused parts of the
593 * superblock are supposed to be zero. Hence a non-null crc field indicates that
594 * we've potentially lost a feature bit and we should check it anyway.
Eric Sandeen10e6e652013-09-09 15:33:29 -0500595 *
596 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
597 * last field in V4 secondary superblocks. So for secondary superblocks,
598 * we are more forgiving, and ignore CRC failures if the primary doesn't
599 * indicate that the fs version is V5.
Dave Chinnerff550682013-08-12 20:49:41 +1000600 */
601static void
602xfs_sb_read_verify(
603 struct xfs_buf *bp)
604{
605 struct xfs_mount *mp = bp->b_target->bt_mount;
606 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
607 int error;
608
609 /*
610 * open code the version check to avoid needing to convert the entire
611 * superblock from disk order just to check the version number
612 */
613 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
614 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
615 XFS_SB_VERSION_5) ||
616 dsb->sb_crc != 0)) {
617
Eric Sandeen51582172014-02-27 15:17:27 +1100618 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
Eric Sandeen10e6e652013-09-09 15:33:29 -0500619 /* Only fail bad secondaries on a known V5 filesystem */
Eric Sandeen7a01e7072014-02-19 15:33:05 +1100620 if (bp->b_bn == XFS_SB_DADDR ||
Eric Sandeen10e6e652013-09-09 15:33:29 -0500621 xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner24513372014-06-25 14:58:08 +1000622 error = -EFSBADCRC;
Eric Sandeen10e6e652013-09-09 15:33:29 -0500623 goto out_error;
624 }
Dave Chinnerff550682013-08-12 20:49:41 +1000625 }
626 }
627 error = xfs_sb_verify(bp, true);
628
629out_error:
630 if (error) {
Dave Chinnerff550682013-08-12 20:49:41 +1000631 xfs_buf_ioerror(bp, error);
Dave Chinner24513372014-06-25 14:58:08 +1000632 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
Eric Sandeence5028c2014-02-27 15:23:10 +1100633 xfs_verifier_error(bp);
Dave Chinnerff550682013-08-12 20:49:41 +1000634 }
635}
636
637/*
638 * We may be probed for a filesystem match, so we may not want to emit
639 * messages when the superblock buffer is not actually an XFS superblock.
Zhi Yong Wu25337872013-08-12 03:15:02 +0000640 * If we find an XFS superblock, then run a normal, noisy mount because we are
Dave Chinnerff550682013-08-12 20:49:41 +1000641 * really going to mount it and want to know about errors.
642 */
643static void
644xfs_sb_quiet_read_verify(
645 struct xfs_buf *bp)
646{
647 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
648
Dave Chinnerff550682013-08-12 20:49:41 +1000649 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
650 /* XFS filesystem, verify noisily! */
651 xfs_sb_read_verify(bp);
652 return;
653 }
654 /* quietly fail */
Dave Chinner24513372014-06-25 14:58:08 +1000655 xfs_buf_ioerror(bp, -EWRONGFS);
Dave Chinnerff550682013-08-12 20:49:41 +1000656}
657
658static void
659xfs_sb_write_verify(
660 struct xfs_buf *bp)
661{
662 struct xfs_mount *mp = bp->b_target->bt_mount;
663 struct xfs_buf_log_item *bip = bp->b_fspriv;
664 int error;
665
666 error = xfs_sb_verify(bp, false);
667 if (error) {
Dave Chinnerff550682013-08-12 20:49:41 +1000668 xfs_buf_ioerror(bp, error);
Eric Sandeence5028c2014-02-27 15:23:10 +1100669 xfs_verifier_error(bp);
Dave Chinnerff550682013-08-12 20:49:41 +1000670 return;
671 }
672
673 if (!xfs_sb_version_hascrc(&mp->m_sb))
674 return;
675
676 if (bip)
677 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
678
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100679 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
Dave Chinnerff550682013-08-12 20:49:41 +1000680}
681
682const struct xfs_buf_ops xfs_sb_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100683 .name = "xfs_sb",
Dave Chinnerff550682013-08-12 20:49:41 +1000684 .verify_read = xfs_sb_read_verify,
685 .verify_write = xfs_sb_write_verify,
686};
687
688const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100689 .name = "xfs_sb_quiet",
Dave Chinnerff550682013-08-12 20:49:41 +1000690 .verify_read = xfs_sb_quiet_read_verify,
691 .verify_write = xfs_sb_write_verify,
692};
693
694/*
695 * xfs_mount_common
696 *
697 * Mount initialization code establishing various mount
698 * fields from the superblock associated with the given
699 * mount structure
700 */
701void
702xfs_sb_mount_common(
703 struct xfs_mount *mp,
704 struct xfs_sb *sbp)
705{
706 mp->m_agfrotor = mp->m_agirotor = 0;
707 spin_lock_init(&mp->m_agirotor_lock);
708 mp->m_maxagi = mp->m_sb.sb_agcount;
709 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
710 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
711 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
712 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
713 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
714 mp->m_blockmask = sbp->sb_blocksize - 1;
715 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
716 mp->m_blockwmask = mp->m_blockwsize - 1;
717
718 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
719 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
720 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
721 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
722
723 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
724 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
725 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
726 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
727
728 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
729 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
730 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
731 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
732
733 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
734 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
735 sbp->sb_inopblock);
736 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
Brian Foster066a1882015-05-29 08:55:20 +1000737
738 if (sbp->sb_spino_align)
739 mp->m_ialloc_min_blks = sbp->sb_spino_align;
740 else
741 mp->m_ialloc_min_blks = mp->m_ialloc_blks;
Dave Chinnerff550682013-08-12 20:49:41 +1000742}
743
744/*
745 * xfs_initialize_perag_data
746 *
747 * Read in each per-ag structure so we can count up the number of
748 * allocated inodes, free inodes and used filesystem blocks as this
749 * information is no longer persistent in the superblock. Once we have
750 * this information, write it into the in-core superblock structure.
751 */
752int
753xfs_initialize_perag_data(
754 struct xfs_mount *mp,
755 xfs_agnumber_t agcount)
756{
757 xfs_agnumber_t index;
758 xfs_perag_t *pag;
759 xfs_sb_t *sbp = &mp->m_sb;
760 uint64_t ifree = 0;
761 uint64_t ialloc = 0;
762 uint64_t bfree = 0;
763 uint64_t bfreelst = 0;
764 uint64_t btree = 0;
765 int error;
766
767 for (index = 0; index < agcount; index++) {
768 /*
769 * read the agf, then the agi. This gets us
770 * all the information we need and populates the
771 * per-ag structures for us.
772 */
773 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
774 if (error)
775 return error;
776
777 error = xfs_ialloc_pagi_init(mp, NULL, index);
778 if (error)
779 return error;
780 pag = xfs_perag_get(mp, index);
781 ifree += pag->pagi_freecount;
782 ialloc += pag->pagi_count;
783 bfree += pag->pagf_freeblks;
784 bfreelst += pag->pagf_flcount;
785 btree += pag->pagf_btreeblks;
786 xfs_perag_put(pag);
787 }
Dave Chinner5681ca42015-02-23 21:22:31 +1100788
789 /* Overwrite incore superblock counters with just-read data */
Dave Chinnerff550682013-08-12 20:49:41 +1000790 spin_lock(&mp->m_sb_lock);
791 sbp->sb_ifree = ifree;
792 sbp->sb_icount = ialloc;
793 sbp->sb_fdblocks = bfree + bfreelst + btree;
794 spin_unlock(&mp->m_sb_lock);
795
Dave Chinner5681ca42015-02-23 21:22:31 +1100796 xfs_reinit_percpu_counters(mp);
Dave Chinnerff550682013-08-12 20:49:41 +1000797
798 return 0;
799}
800
801/*
Dave Chinner61e63ec2015-01-22 09:10:31 +1100802 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
803 * into the superblock buffer to be logged. It does not provide the higher
804 * level of locking that is needed to protect the in-core superblock from
805 * concurrent access.
Dave Chinnerff550682013-08-12 20:49:41 +1000806 */
807void
Dave Chinner61e63ec2015-01-22 09:10:31 +1100808xfs_log_sb(
Dave Chinner4d11a402015-01-22 09:10:26 +1100809 struct xfs_trans *tp)
Dave Chinnerff550682013-08-12 20:49:41 +1000810{
Dave Chinner4d11a402015-01-22 09:10:26 +1100811 struct xfs_mount *mp = tp->t_mountp;
812 struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
Dave Chinnerff550682013-08-12 20:49:41 +1000813
Dave Chinner501ab322015-02-23 21:19:28 +1100814 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100815 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100816 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
Dave Chinner501ab322015-02-23 21:19:28 +1100817
Dave Chinner4d11a402015-01-22 09:10:26 +1100818 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
Dave Chinnerff550682013-08-12 20:49:41 +1000819 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
Dave Chinner4d11a402015-01-22 09:10:26 +1100820 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
Dave Chinnerff550682013-08-12 20:49:41 +1000821}
Dave Chinner61e63ec2015-01-22 09:10:31 +1100822
823/*
824 * xfs_sync_sb
825 *
826 * Sync the superblock to disk.
827 *
828 * Note that the caller is responsible for checking the frozen state of the
829 * filesystem. This procedure uses the non-blocking transaction allocator and
830 * thus will allow modifications to a frozen fs. This is required because this
831 * code can be called during the process of freezing where use of the high-level
832 * allocator would deadlock.
833 */
834int
835xfs_sync_sb(
836 struct xfs_mount *mp,
837 bool wait)
838{
839 struct xfs_trans *tp;
840 int error;
841
Christoph Hellwig253f4912016-04-06 09:19:55 +1000842 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
843 XFS_TRANS_NO_WRITECOUNT, &tp);
844 if (error)
Dave Chinner61e63ec2015-01-22 09:10:31 +1100845 return error;
Dave Chinner61e63ec2015-01-22 09:10:31 +1100846
847 xfs_log_sb(tp);
848 if (wait)
849 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000850 return xfs_trans_commit(tp);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100851}