blob: a270095ec3c07ac5c3dd7b4c06fbc5830ee9cd08 [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"
Dave Chinnerff550682013-08-12 20:49:41 +100027#include "xfs_inode.h"
Dave Chinnerff550682013-08-12 20:49:41 +100028#include "xfs_ialloc.h"
29#include "xfs_alloc.h"
Dave Chinnerff550682013-08-12 20:49:41 +100030#include "xfs_error.h"
Dave Chinnerff550682013-08-12 20:49:41 +100031#include "xfs_trace.h"
32#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinnerff550682013-08-12 20:49:41 +100034#include "xfs_buf_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_bmap_btree.h"
36#include "xfs_alloc_btree.h"
37#include "xfs_ialloc_btree.h"
Dave Chinnerff550682013-08-12 20:49:41 +100038
39/*
40 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
41 */
42
Dave Chinnerff550682013-08-12 20:49:41 +100043/*
44 * Reference counting access wrappers to the perag structures.
45 * Because we never free per-ag structures, the only thing we
46 * have to protect against changes is the tree structure itself.
47 */
48struct xfs_perag *
49xfs_perag_get(
50 struct xfs_mount *mp,
51 xfs_agnumber_t agno)
52{
53 struct xfs_perag *pag;
54 int ref = 0;
55
56 rcu_read_lock();
57 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
58 if (pag) {
59 ASSERT(atomic_read(&pag->pag_ref) >= 0);
60 ref = atomic_inc_return(&pag->pag_ref);
61 }
62 rcu_read_unlock();
63 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
64 return pag;
65}
66
67/*
68 * search from @first to find the next perag with the given tag set.
69 */
70struct xfs_perag *
71xfs_perag_get_tag(
72 struct xfs_mount *mp,
73 xfs_agnumber_t first,
74 int tag)
75{
76 struct xfs_perag *pag;
77 int found;
78 int ref;
79
80 rcu_read_lock();
81 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
82 (void **)&pag, first, 1, tag);
83 if (found <= 0) {
84 rcu_read_unlock();
85 return NULL;
86 }
87 ref = atomic_inc_return(&pag->pag_ref);
88 rcu_read_unlock();
89 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
90 return pag;
91}
92
93void
94xfs_perag_put(
95 struct xfs_perag *pag)
96{
97 int ref;
98
99 ASSERT(atomic_read(&pag->pag_ref) > 0);
100 ref = atomic_dec_return(&pag->pag_ref);
101 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
102}
103
104/*
105 * Check the validity of the SB found.
106 */
107STATIC int
108xfs_mount_validate_sb(
109 xfs_mount_t *mp,
110 xfs_sb_t *sbp,
111 bool check_inprogress,
112 bool check_version)
113{
114
115 /*
116 * If the log device and data device have the
117 * same device number, the log is internal.
118 * Consequently, the sb_logstart should be non-zero. If
119 * we have a zero sb_logstart in this case, we may be trying to mount
120 * a volume filesystem in a non-volume manner.
121 */
122 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
123 xfs_warn(mp, "bad magic number");
Dave Chinner24513372014-06-25 14:58:08 +1000124 return -EWRONGFS;
Dave Chinnerff550682013-08-12 20:49:41 +1000125 }
126
127
128 if (!xfs_sb_good_version(sbp)) {
129 xfs_warn(mp, "bad version");
Dave Chinner24513372014-06-25 14:58:08 +1000130 return -EWRONGFS;
Dave Chinnerff550682013-08-12 20:49:41 +1000131 }
132
133 /*
134 * Version 5 superblock feature mask validation. Reject combinations the
135 * kernel cannot support up front before checking anything else. For
136 * write validation, we don't need to check feature masks.
137 */
138 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
Dave Chinnerff550682013-08-12 20:49:41 +1000139 if (xfs_sb_has_compat_feature(sbp,
140 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
141 xfs_warn(mp,
142"Superblock has unknown compatible features (0x%x) enabled.\n"
143"Using a more recent kernel is recommended.",
144 (sbp->sb_features_compat &
145 XFS_SB_FEAT_COMPAT_UNKNOWN));
146 }
147
148 if (xfs_sb_has_ro_compat_feature(sbp,
149 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
150 xfs_alert(mp,
151"Superblock has unknown read-only compatible features (0x%x) enabled.",
152 (sbp->sb_features_ro_compat &
153 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
154 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
155 xfs_warn(mp,
156"Attempted to mount read-only compatible filesystem read-write.\n"
157"Filesystem can only be safely mounted read only.");
Dave Chinner24513372014-06-25 14:58:08 +1000158 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000159 }
160 }
161 if (xfs_sb_has_incompat_feature(sbp,
162 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
163 xfs_warn(mp,
164"Superblock has unknown incompatible features (0x%x) enabled.\n"
165"Filesystem can not be safely mounted by this kernel.",
166 (sbp->sb_features_incompat &
167 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
Dave Chinner24513372014-06-25 14:58:08 +1000168 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000169 }
170 }
171
172 if (xfs_sb_version_has_pquotino(sbp)) {
173 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
174 xfs_notice(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500175 "Version 5 of Super block has XFS_OQUOTA bits.");
Dave Chinner24513372014-06-25 14:58:08 +1000176 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000177 }
178 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
179 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
180 xfs_notice(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500181"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
Dave Chinner24513372014-06-25 14:58:08 +1000182 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000183 }
184
185 if (unlikely(
186 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
187 xfs_warn(mp,
188 "filesystem is marked as having an external log; "
189 "specify logdev on the mount command line.");
Dave Chinner24513372014-06-25 14:58:08 +1000190 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000191 }
192
193 if (unlikely(
194 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
195 xfs_warn(mp,
196 "filesystem is marked as having an internal log; "
197 "do not specify logdev on the mount command line.");
Dave Chinner24513372014-06-25 14:58:08 +1000198 return -EINVAL;
Dave Chinnerff550682013-08-12 20:49:41 +1000199 }
200
201 /*
202 * More sanity checking. Most of these were stolen directly from
203 * xfs_repair.
204 */
205 if (unlikely(
206 sbp->sb_agcount <= 0 ||
207 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
208 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
209 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
210 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
211 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
212 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
213 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
214 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
215 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
216 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
Eric Sandeene1b05722014-09-09 11:47:24 +1000217 sbp->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG ||
Dave Chinnerff550682013-08-12 20:49:41 +1000218 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
219 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
220 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
221 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
222 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
Eric Sandeene1b05722014-09-09 11:47:24 +1000223 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
Eric Sandeen392c6de2014-02-07 15:26:11 +1100224 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
Dave Chinnerff550682013-08-12 20:49:41 +1000225 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
226 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
227 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
228 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
229 sbp->sb_dblocks == 0 ||
230 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
Dave Chinnerab3e57b2014-05-20 07:47:05 +1000231 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
232 sbp->sb_shared_vn != 0)) {
Eric Sandeen5ef11eb2014-02-19 15:39:35 +1100233 xfs_notice(mp, "SB sanity check failed");
Dave Chinner24513372014-06-25 14:58:08 +1000234 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000235 }
236
237 /*
238 * Until this is fixed only page-sized or smaller data blocks work.
239 */
240 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
241 xfs_warn(mp,
242 "File system with blocksize %d bytes. "
243 "Only pagesize (%ld) or less will currently work.",
244 sbp->sb_blocksize, PAGE_SIZE);
Dave Chinner24513372014-06-25 14:58:08 +1000245 return -ENOSYS;
Dave Chinnerff550682013-08-12 20:49:41 +1000246 }
247
248 /*
249 * Currently only very few inode sizes are supported.
250 */
251 switch (sbp->sb_inodesize) {
252 case 256:
253 case 512:
254 case 1024:
255 case 2048:
256 break;
257 default:
258 xfs_warn(mp, "inode size of %d bytes not supported",
259 sbp->sb_inodesize);
Dave Chinner24513372014-06-25 14:58:08 +1000260 return -ENOSYS;
Dave Chinnerff550682013-08-12 20:49:41 +1000261 }
262
263 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
264 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
265 xfs_warn(mp,
266 "file system too large to be mounted on this system.");
Dave Chinner24513372014-06-25 14:58:08 +1000267 return -EFBIG;
Dave Chinnerff550682013-08-12 20:49:41 +1000268 }
269
270 if (check_inprogress && sbp->sb_inprogress) {
271 xfs_warn(mp, "Offline file system operation in progress!");
Dave Chinner24513372014-06-25 14:58:08 +1000272 return -EFSCORRUPTED;
Dave Chinnerff550682013-08-12 20:49:41 +1000273 }
Dave Chinnerff550682013-08-12 20:49:41 +1000274 return 0;
275}
276
277void
278xfs_sb_quota_from_disk(struct xfs_sb *sbp)
279{
280 /*
281 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
282 * leads to in-core values having two different values for a quota
283 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
284 * NULLFSINO.
285 *
286 * Note that this change affect only the in-core values. These
287 * values are not written back to disk unless any quota information
288 * is written to the disk. Even in that case, sb_pquotino field is
289 * not written to disk unless the superblock supports pquotino.
290 */
291 if (sbp->sb_uquotino == 0)
292 sbp->sb_uquotino = NULLFSINO;
293 if (sbp->sb_gquotino == 0)
294 sbp->sb_gquotino = NULLFSINO;
295 if (sbp->sb_pquotino == 0)
296 sbp->sb_pquotino = NULLFSINO;
297
298 /*
299 * We need to do these manipilations only if we are working
300 * with an older version of on-disk superblock.
301 */
302 if (xfs_sb_version_has_pquotino(sbp))
303 return;
304
305 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
306 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
307 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
308 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
309 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
310 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
311 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
312
313 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
314 /*
315 * In older version of superblock, on-disk superblock only
316 * has sb_gquotino, and in-core superblock has both sb_gquotino
317 * and sb_pquotino. But, only one of them is supported at any
318 * point of time. So, if PQUOTA is set in disk superblock,
319 * copy over sb_gquotino to sb_pquotino.
320 */
321 sbp->sb_pquotino = sbp->sb_gquotino;
322 sbp->sb_gquotino = NULLFSINO;
323 }
324}
325
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000326static void
327__xfs_sb_from_disk(
Dave Chinnerff550682013-08-12 20:49:41 +1000328 struct xfs_sb *to,
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000329 xfs_dsb_t *from,
330 bool convert_xquota)
Dave Chinnerff550682013-08-12 20:49:41 +1000331{
332 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
333 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
334 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
335 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
336 to->sb_rextents = be64_to_cpu(from->sb_rextents);
337 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
338 to->sb_logstart = be64_to_cpu(from->sb_logstart);
339 to->sb_rootino = be64_to_cpu(from->sb_rootino);
340 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
341 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
342 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
343 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
344 to->sb_agcount = be32_to_cpu(from->sb_agcount);
345 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
346 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
347 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
348 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
349 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
350 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
351 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
352 to->sb_blocklog = from->sb_blocklog;
353 to->sb_sectlog = from->sb_sectlog;
354 to->sb_inodelog = from->sb_inodelog;
355 to->sb_inopblog = from->sb_inopblog;
356 to->sb_agblklog = from->sb_agblklog;
357 to->sb_rextslog = from->sb_rextslog;
358 to->sb_inprogress = from->sb_inprogress;
359 to->sb_imax_pct = from->sb_imax_pct;
360 to->sb_icount = be64_to_cpu(from->sb_icount);
361 to->sb_ifree = be64_to_cpu(from->sb_ifree);
362 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
363 to->sb_frextents = be64_to_cpu(from->sb_frextents);
364 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
365 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
366 to->sb_qflags = be16_to_cpu(from->sb_qflags);
367 to->sb_flags = from->sb_flags;
368 to->sb_shared_vn = from->sb_shared_vn;
369 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
370 to->sb_unit = be32_to_cpu(from->sb_unit);
371 to->sb_width = be32_to_cpu(from->sb_width);
372 to->sb_dirblklog = from->sb_dirblklog;
373 to->sb_logsectlog = from->sb_logsectlog;
374 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
375 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
376 to->sb_features2 = be32_to_cpu(from->sb_features2);
377 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
378 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
379 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
380 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
381 to->sb_features_log_incompat =
382 be32_to_cpu(from->sb_features_log_incompat);
Eric Sandeen04dd1a02014-10-02 09:24:11 +1000383 /* crc is only used on disk, not in memory; just init to 0 here. */
384 to->sb_crc = 0;
Dave Chinnerff550682013-08-12 20:49:41 +1000385 to->sb_pad = 0;
386 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
387 to->sb_lsn = be64_to_cpu(from->sb_lsn);
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000388 /* Convert on-disk flags to in-memory flags? */
389 if (convert_xquota)
390 xfs_sb_quota_from_disk(to);
391}
392
393void
394xfs_sb_from_disk(
395 struct xfs_sb *to,
396 xfs_dsb_t *from)
397{
398 __xfs_sb_from_disk(to, from, true);
Dave Chinnerff550682013-08-12 20:49:41 +1000399}
400
Dave Chinner4d11a402015-01-22 09:10:26 +1100401static void
Dave Chinnerff550682013-08-12 20:49:41 +1000402xfs_sb_quota_to_disk(
Dave Chinner4d11a402015-01-22 09:10:26 +1100403 struct xfs_dsb *to,
404 struct xfs_sb *from)
Dave Chinnerff550682013-08-12 20:49:41 +1000405{
406 __uint16_t qflags = from->sb_qflags;
407
Dave Chinner4d11a402015-01-22 09:10:26 +1100408 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
409 if (xfs_sb_version_has_pquotino(from)) {
410 to->sb_qflags = cpu_to_be16(from->sb_qflags);
411 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
412 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
Dave Chinnerff550682013-08-12 20:49:41 +1000413 return;
Dave Chinnerff550682013-08-12 20:49:41 +1000414 }
415
416 /*
Dave Chinner4d11a402015-01-22 09:10:26 +1100417 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
418 * flags, whereas the on-disk version does. So, convert incore
419 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
Dave Chinnerff550682013-08-12 20:49:41 +1000420 */
Dave Chinner4d11a402015-01-22 09:10:26 +1100421 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
422 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
423
424 if (from->sb_qflags &
425 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
426 qflags |= XFS_OQUOTA_ENFD;
427 if (from->sb_qflags &
428 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
429 qflags |= XFS_OQUOTA_CHKD;
430 to->sb_qflags = cpu_to_be16(qflags);
431
432 /*
433 * GQUOTINO and PQUOTINO cannot be used together in versions
434 * of superblock that do not have pquotino. from->sb_flags
435 * tells us which quota is active and should be copied to
436 * disk. If neither are active, we should NULL the inode.
437 *
438 * In all cases, the separate pquotino must remain 0 because it
439 * it beyond the "end" of the valid non-pquotino superblock.
440 */
441 if (from->sb_qflags & XFS_GQUOTA_ACCT)
Dave Chinnerff550682013-08-12 20:49:41 +1000442 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
Dave Chinner4d11a402015-01-22 09:10:26 +1100443 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
Dave Chinnerff550682013-08-12 20:49:41 +1000444 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
Dave Chinner03e01342014-07-15 07:28:41 +1000445 else {
446 /*
447 * We can't rely on just the fields being logged to tell us
448 * that it is safe to write NULLFSINO - we should only do that
449 * if quotas are not actually enabled. Hence only write
450 * NULLFSINO if both in-core quota inodes are NULL.
451 */
452 if (from->sb_gquotino == NULLFSINO &&
453 from->sb_pquotino == NULLFSINO)
454 to->sb_gquotino = cpu_to_be64(NULLFSINO);
455 }
Dave Chinnerff550682013-08-12 20:49:41 +1000456
Dave Chinner4d11a402015-01-22 09:10:26 +1100457 to->sb_pquotino = 0;
Dave Chinnerff550682013-08-12 20:49:41 +1000458}
459
Dave Chinnerff550682013-08-12 20:49:41 +1000460void
461xfs_sb_to_disk(
Dave Chinner4d11a402015-01-22 09:10:26 +1100462 struct xfs_dsb *to,
463 struct xfs_sb *from)
Dave Chinnerff550682013-08-12 20:49:41 +1000464{
Dave Chinner4d11a402015-01-22 09:10:26 +1100465 xfs_sb_quota_to_disk(to, from);
Dave Chinnerff550682013-08-12 20:49:41 +1000466
Dave Chinner4d11a402015-01-22 09:10:26 +1100467 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
468 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
469 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
470 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
471 to->sb_rextents = cpu_to_be64(from->sb_rextents);
472 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
473 to->sb_logstart = cpu_to_be64(from->sb_logstart);
474 to->sb_rootino = cpu_to_be64(from->sb_rootino);
475 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
476 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
477 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
478 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
479 to->sb_agcount = cpu_to_be32(from->sb_agcount);
480 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
481 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
482 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
483 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
484 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
485 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
486 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
487 to->sb_blocklog = from->sb_blocklog;
488 to->sb_sectlog = from->sb_sectlog;
489 to->sb_inodelog = from->sb_inodelog;
490 to->sb_inopblog = from->sb_inopblog;
491 to->sb_agblklog = from->sb_agblklog;
492 to->sb_rextslog = from->sb_rextslog;
493 to->sb_inprogress = from->sb_inprogress;
494 to->sb_imax_pct = from->sb_imax_pct;
495 to->sb_icount = cpu_to_be64(from->sb_icount);
496 to->sb_ifree = cpu_to_be64(from->sb_ifree);
497 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
498 to->sb_frextents = cpu_to_be64(from->sb_frextents);
Dave Chinnerff550682013-08-12 20:49:41 +1000499
Dave Chinner4d11a402015-01-22 09:10:26 +1100500 to->sb_flags = from->sb_flags;
501 to->sb_shared_vn = from->sb_shared_vn;
502 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
503 to->sb_unit = cpu_to_be32(from->sb_unit);
504 to->sb_width = cpu_to_be32(from->sb_width);
505 to->sb_dirblklog = from->sb_dirblklog;
506 to->sb_logsectlog = from->sb_logsectlog;
507 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
508 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
Dave Chinner074e4272015-01-22 09:10:33 +1100509
510 /*
511 * We need to ensure that bad_features2 always matches features2.
512 * Hence we enforce that here rather than having to remember to do it
513 * everywhere else that updates features2.
514 */
515 from->sb_bad_features2 = from->sb_features2;
Dave Chinner4d11a402015-01-22 09:10:26 +1100516 to->sb_features2 = cpu_to_be32(from->sb_features2);
517 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
Dave Chinnerff550682013-08-12 20:49:41 +1000518
Dave Chinner4d11a402015-01-22 09:10:26 +1100519 if (xfs_sb_version_hascrc(from)) {
520 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
521 to->sb_features_ro_compat =
522 cpu_to_be32(from->sb_features_ro_compat);
523 to->sb_features_incompat =
524 cpu_to_be32(from->sb_features_incompat);
525 to->sb_features_log_incompat =
526 cpu_to_be32(from->sb_features_log_incompat);
527 to->sb_pad = 0;
528 to->sb_lsn = cpu_to_be64(from->sb_lsn);
Dave Chinnerff550682013-08-12 20:49:41 +1000529 }
530}
531
532static int
533xfs_sb_verify(
534 struct xfs_buf *bp,
535 bool check_version)
536{
537 struct xfs_mount *mp = bp->b_target->bt_mount;
538 struct xfs_sb sb;
539
Eric Sandeen5ef828c2014-08-04 11:35:44 +1000540 /*
541 * Use call variant which doesn't convert quota flags from disk
542 * format, because xfs_mount_validate_sb checks the on-disk flags.
543 */
544 __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
Dave Chinnerff550682013-08-12 20:49:41 +1000545
546 /*
547 * Only check the in progress field for the primary superblock as
548 * mkfs.xfs doesn't clear it from secondary superblocks.
549 */
550 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
551 check_version);
552}
553
554/*
555 * If the superblock has the CRC feature bit set or the CRC field is non-null,
556 * check that the CRC is valid. We check the CRC field is non-null because a
557 * single bit error could clear the feature bit and unused parts of the
558 * superblock are supposed to be zero. Hence a non-null crc field indicates that
559 * we've potentially lost a feature bit and we should check it anyway.
Eric Sandeen10e6e652013-09-09 15:33:29 -0500560 *
561 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
562 * last field in V4 secondary superblocks. So for secondary superblocks,
563 * we are more forgiving, and ignore CRC failures if the primary doesn't
564 * indicate that the fs version is V5.
Dave Chinnerff550682013-08-12 20:49:41 +1000565 */
566static void
567xfs_sb_read_verify(
568 struct xfs_buf *bp)
569{
570 struct xfs_mount *mp = bp->b_target->bt_mount;
571 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
572 int error;
573
574 /*
575 * open code the version check to avoid needing to convert the entire
576 * superblock from disk order just to check the version number
577 */
578 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
579 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
580 XFS_SB_VERSION_5) ||
581 dsb->sb_crc != 0)) {
582
Eric Sandeen51582172014-02-27 15:17:27 +1100583 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
Eric Sandeen10e6e652013-09-09 15:33:29 -0500584 /* Only fail bad secondaries on a known V5 filesystem */
Eric Sandeen7a01e7072014-02-19 15:33:05 +1100585 if (bp->b_bn == XFS_SB_DADDR ||
Eric Sandeen10e6e652013-09-09 15:33:29 -0500586 xfs_sb_version_hascrc(&mp->m_sb)) {
Dave Chinner24513372014-06-25 14:58:08 +1000587 error = -EFSBADCRC;
Eric Sandeen10e6e652013-09-09 15:33:29 -0500588 goto out_error;
589 }
Dave Chinnerff550682013-08-12 20:49:41 +1000590 }
591 }
592 error = xfs_sb_verify(bp, true);
593
594out_error:
595 if (error) {
Dave Chinnerff550682013-08-12 20:49:41 +1000596 xfs_buf_ioerror(bp, error);
Dave Chinner24513372014-06-25 14:58:08 +1000597 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
Eric Sandeence5028c2014-02-27 15:23:10 +1100598 xfs_verifier_error(bp);
Dave Chinnerff550682013-08-12 20:49:41 +1000599 }
600}
601
602/*
603 * We may be probed for a filesystem match, so we may not want to emit
604 * messages when the superblock buffer is not actually an XFS superblock.
Zhi Yong Wu25337872013-08-12 03:15:02 +0000605 * If we find an XFS superblock, then run a normal, noisy mount because we are
Dave Chinnerff550682013-08-12 20:49:41 +1000606 * really going to mount it and want to know about errors.
607 */
608static void
609xfs_sb_quiet_read_verify(
610 struct xfs_buf *bp)
611{
612 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
613
Dave Chinnerff550682013-08-12 20:49:41 +1000614 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
615 /* XFS filesystem, verify noisily! */
616 xfs_sb_read_verify(bp);
617 return;
618 }
619 /* quietly fail */
Dave Chinner24513372014-06-25 14:58:08 +1000620 xfs_buf_ioerror(bp, -EWRONGFS);
Dave Chinnerff550682013-08-12 20:49:41 +1000621}
622
623static void
624xfs_sb_write_verify(
625 struct xfs_buf *bp)
626{
627 struct xfs_mount *mp = bp->b_target->bt_mount;
628 struct xfs_buf_log_item *bip = bp->b_fspriv;
629 int error;
630
631 error = xfs_sb_verify(bp, false);
632 if (error) {
Dave Chinnerff550682013-08-12 20:49:41 +1000633 xfs_buf_ioerror(bp, error);
Eric Sandeence5028c2014-02-27 15:23:10 +1100634 xfs_verifier_error(bp);
Dave Chinnerff550682013-08-12 20:49:41 +1000635 return;
636 }
637
638 if (!xfs_sb_version_hascrc(&mp->m_sb))
639 return;
640
641 if (bip)
642 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
643
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100644 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
Dave Chinnerff550682013-08-12 20:49:41 +1000645}
646
647const struct xfs_buf_ops xfs_sb_buf_ops = {
648 .verify_read = xfs_sb_read_verify,
649 .verify_write = xfs_sb_write_verify,
650};
651
652const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
653 .verify_read = xfs_sb_quiet_read_verify,
654 .verify_write = xfs_sb_write_verify,
655};
656
657/*
658 * xfs_mount_common
659 *
660 * Mount initialization code establishing various mount
661 * fields from the superblock associated with the given
662 * mount structure
663 */
664void
665xfs_sb_mount_common(
666 struct xfs_mount *mp,
667 struct xfs_sb *sbp)
668{
669 mp->m_agfrotor = mp->m_agirotor = 0;
670 spin_lock_init(&mp->m_agirotor_lock);
671 mp->m_maxagi = mp->m_sb.sb_agcount;
672 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
673 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
674 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
675 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
676 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
677 mp->m_blockmask = sbp->sb_blocksize - 1;
678 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
679 mp->m_blockwmask = mp->m_blockwsize - 1;
680
681 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
682 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
683 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
684 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
685
686 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
687 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
688 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
689 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
690
691 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
692 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
693 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
694 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
695
696 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
697 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
698 sbp->sb_inopblock);
699 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
700}
701
702/*
703 * xfs_initialize_perag_data
704 *
705 * Read in each per-ag structure so we can count up the number of
706 * allocated inodes, free inodes and used filesystem blocks as this
707 * information is no longer persistent in the superblock. Once we have
708 * this information, write it into the in-core superblock structure.
709 */
710int
711xfs_initialize_perag_data(
712 struct xfs_mount *mp,
713 xfs_agnumber_t agcount)
714{
715 xfs_agnumber_t index;
716 xfs_perag_t *pag;
717 xfs_sb_t *sbp = &mp->m_sb;
718 uint64_t ifree = 0;
719 uint64_t ialloc = 0;
720 uint64_t bfree = 0;
721 uint64_t bfreelst = 0;
722 uint64_t btree = 0;
723 int error;
724
725 for (index = 0; index < agcount; index++) {
726 /*
727 * read the agf, then the agi. This gets us
728 * all the information we need and populates the
729 * per-ag structures for us.
730 */
731 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
732 if (error)
733 return error;
734
735 error = xfs_ialloc_pagi_init(mp, NULL, index);
736 if (error)
737 return error;
738 pag = xfs_perag_get(mp, index);
739 ifree += pag->pagi_freecount;
740 ialloc += pag->pagi_count;
741 bfree += pag->pagf_freeblks;
742 bfreelst += pag->pagf_flcount;
743 btree += pag->pagf_btreeblks;
744 xfs_perag_put(pag);
745 }
Dave Chinner5681ca42015-02-23 21:22:31 +1100746
747 /* Overwrite incore superblock counters with just-read data */
Dave Chinnerff550682013-08-12 20:49:41 +1000748 spin_lock(&mp->m_sb_lock);
749 sbp->sb_ifree = ifree;
750 sbp->sb_icount = ialloc;
751 sbp->sb_fdblocks = bfree + bfreelst + btree;
752 spin_unlock(&mp->m_sb_lock);
753
Dave Chinner5681ca42015-02-23 21:22:31 +1100754 xfs_reinit_percpu_counters(mp);
Dave Chinnerff550682013-08-12 20:49:41 +1000755
756 return 0;
757}
758
759/*
Dave Chinner61e63ec2015-01-22 09:10:31 +1100760 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
761 * into the superblock buffer to be logged. It does not provide the higher
762 * level of locking that is needed to protect the in-core superblock from
763 * concurrent access.
Dave Chinnerff550682013-08-12 20:49:41 +1000764 */
765void
Dave Chinner61e63ec2015-01-22 09:10:31 +1100766xfs_log_sb(
Dave Chinner4d11a402015-01-22 09:10:26 +1100767 struct xfs_trans *tp)
Dave Chinnerff550682013-08-12 20:49:41 +1000768{
Dave Chinner4d11a402015-01-22 09:10:26 +1100769 struct xfs_mount *mp = tp->t_mountp;
770 struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
Dave Chinnerff550682013-08-12 20:49:41 +1000771
Dave Chinner501ab322015-02-23 21:19:28 +1100772 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100773 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100774 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
Dave Chinner501ab322015-02-23 21:19:28 +1100775
Dave Chinner4d11a402015-01-22 09:10:26 +1100776 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
Dave Chinnerff550682013-08-12 20:49:41 +1000777 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
Dave Chinner4d11a402015-01-22 09:10:26 +1100778 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
Dave Chinnerff550682013-08-12 20:49:41 +1000779}
Dave Chinner61e63ec2015-01-22 09:10:31 +1100780
781/*
782 * xfs_sync_sb
783 *
784 * Sync the superblock to disk.
785 *
786 * Note that the caller is responsible for checking the frozen state of the
787 * filesystem. This procedure uses the non-blocking transaction allocator and
788 * thus will allow modifications to a frozen fs. This is required because this
789 * code can be called during the process of freezing where use of the high-level
790 * allocator would deadlock.
791 */
792int
793xfs_sync_sb(
794 struct xfs_mount *mp,
795 bool wait)
796{
797 struct xfs_trans *tp;
798 int error;
799
800 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_CHANGE, KM_SLEEP);
801 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
802 if (error) {
803 xfs_trans_cancel(tp, 0);
804 return error;
805 }
806
807 xfs_log_sb(tp);
808 if (wait)
809 xfs_trans_set_sync(tp);
810 return xfs_trans_commit(tp, 0);
811}