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