xfs: relocate sparse inode mount warning
[firefly-linux-kernel-4.4.55.git] / fs / xfs / libxfs / xfs_sb.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_mount.h"
27 #include "xfs_inode.h"
28 #include "xfs_ialloc.h"
29 #include "xfs_alloc.h"
30 #include "xfs_error.h"
31 #include "xfs_trace.h"
32 #include "xfs_cksum.h"
33 #include "xfs_trans.h"
34 #include "xfs_buf_item.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_alloc_btree.h"
37 #include "xfs_ialloc_btree.h"
38
39 /*
40  * Physical superblock buffer manipulations. Shared with libxfs in userspace.
41  */
42
43 /*
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  */
48 struct xfs_perag *
49 xfs_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  */
70 struct xfs_perag *
71 xfs_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
93 void
94 xfs_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  */
107 STATIC int
108 xfs_mount_validate_sb(
109         xfs_mount_t     *mp,
110         xfs_sb_t        *sbp,
111         bool            check_inprogress,
112         bool            check_version)
113 {
114         if (sbp->sb_magicnum != XFS_SB_MAGIC) {
115                 xfs_warn(mp, "bad magic number");
116                 return -EWRONGFS;
117         }
118
119
120         if (!xfs_sb_good_version(sbp)) {
121                 xfs_warn(mp, "bad version");
122                 return -EWRONGFS;
123         }
124
125         /*
126          * Version 5 superblock feature mask validation. Reject combinations the
127          * kernel cannot support up front before checking anything else. For
128          * write validation, we don't need to check feature masks.
129          */
130         if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
131                 if (xfs_sb_has_compat_feature(sbp,
132                                         XFS_SB_FEAT_COMPAT_UNKNOWN)) {
133                         xfs_warn(mp,
134 "Superblock has unknown compatible features (0x%x) enabled.\n"
135 "Using a more recent kernel is recommended.",
136                                 (sbp->sb_features_compat &
137                                                 XFS_SB_FEAT_COMPAT_UNKNOWN));
138                 }
139
140                 if (xfs_sb_has_ro_compat_feature(sbp,
141                                         XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
142                         xfs_alert(mp,
143 "Superblock has unknown read-only compatible features (0x%x) enabled.",
144                                 (sbp->sb_features_ro_compat &
145                                                 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
146                         if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
147                                 xfs_warn(mp,
148 "Attempted to mount read-only compatible filesystem read-write.\n"
149 "Filesystem can only be safely mounted read only.");
150                                 return -EINVAL;
151                         }
152                 }
153                 if (xfs_sb_has_incompat_feature(sbp,
154                                         XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
155                         xfs_warn(mp,
156 "Superblock has unknown incompatible features (0x%x) enabled.\n"
157 "Filesystem can not be safely mounted by this kernel.",
158                                 (sbp->sb_features_incompat &
159                                                 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
160                         return -EINVAL;
161                 }
162         }
163
164         if (xfs_sb_version_has_pquotino(sbp)) {
165                 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
166                         xfs_notice(mp,
167                            "Version 5 of Super block has XFS_OQUOTA bits.");
168                         return -EFSCORRUPTED;
169                 }
170         } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
171                                 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
172                         xfs_notice(mp,
173 "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
174                         return -EFSCORRUPTED;
175         }
176
177         /*
178          * Full inode chunks must be aligned to inode chunk size when
179          * sparse inodes are enabled to support the sparse chunk
180          * allocation algorithm and prevent overlapping inode records.
181          */
182         if (xfs_sb_version_hassparseinodes(sbp)) {
183                 uint32_t        align;
184
185                 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
186                                 >> sbp->sb_blocklog;
187                 if (sbp->sb_inoalignmt != align) {
188                         xfs_warn(mp,
189 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
190                                  sbp->sb_inoalignmt, align);
191                         return -EINVAL;
192                 }
193         }
194
195         if (unlikely(
196             sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
197                 xfs_warn(mp,
198                 "filesystem is marked as having an external log; "
199                 "specify logdev on the mount command line.");
200                 return -EINVAL;
201         }
202
203         if (unlikely(
204             sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
205                 xfs_warn(mp,
206                 "filesystem is marked as having an internal log; "
207                 "do not specify logdev on the mount command line.");
208                 return -EINVAL;
209         }
210
211         /*
212          * More sanity checking.  Most of these were stolen directly from
213          * xfs_repair.
214          */
215         if (unlikely(
216             sbp->sb_agcount <= 0                                        ||
217             sbp->sb_sectsize < XFS_MIN_SECTORSIZE                       ||
218             sbp->sb_sectsize > XFS_MAX_SECTORSIZE                       ||
219             sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG                    ||
220             sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG                    ||
221             sbp->sb_sectsize != (1 << sbp->sb_sectlog)                  ||
222             sbp->sb_blocksize < XFS_MIN_BLOCKSIZE                       ||
223             sbp->sb_blocksize > XFS_MAX_BLOCKSIZE                       ||
224             sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG                    ||
225             sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG                    ||
226             sbp->sb_blocksize != (1 << sbp->sb_blocklog)                ||
227             sbp->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG                   ||
228             sbp->sb_inodesize < XFS_DINODE_MIN_SIZE                     ||
229             sbp->sb_inodesize > XFS_DINODE_MAX_SIZE                     ||
230             sbp->sb_inodelog < XFS_DINODE_MIN_LOG                       ||
231             sbp->sb_inodelog > XFS_DINODE_MAX_LOG                       ||
232             sbp->sb_inodesize != (1 << sbp->sb_inodelog)                ||
233             sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE                    ||
234             sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
235             (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)   ||
236             (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)  ||
237             (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)  ||
238             (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)    ||
239             sbp->sb_dblocks == 0                                        ||
240             sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)                      ||
241             sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp)                      ||
242             sbp->sb_shared_vn != 0)) {
243                 xfs_notice(mp, "SB sanity check failed");
244                 return -EFSCORRUPTED;
245         }
246
247         /*
248          * Until this is fixed only page-sized or smaller data blocks work.
249          */
250         if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
251                 xfs_warn(mp,
252                 "File system with blocksize %d bytes. "
253                 "Only pagesize (%ld) or less will currently work.",
254                                 sbp->sb_blocksize, PAGE_SIZE);
255                 return -ENOSYS;
256         }
257
258         /*
259          * Currently only very few inode sizes are supported.
260          */
261         switch (sbp->sb_inodesize) {
262         case 256:
263         case 512:
264         case 1024:
265         case 2048:
266                 break;
267         default:
268                 xfs_warn(mp, "inode size of %d bytes not supported",
269                                 sbp->sb_inodesize);
270                 return -ENOSYS;
271         }
272
273         if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
274             xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
275                 xfs_warn(mp,
276                 "file system too large to be mounted on this system.");
277                 return -EFBIG;
278         }
279
280         if (check_inprogress && sbp->sb_inprogress) {
281                 xfs_warn(mp, "Offline file system operation in progress!");
282                 return -EFSCORRUPTED;
283         }
284         return 0;
285 }
286
287 void
288 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
289 {
290         /*
291          * older mkfs doesn't initialize quota inodes to NULLFSINO. This
292          * leads to in-core values having two different values for a quota
293          * inode to be invalid: 0 and NULLFSINO. Change it to a single value
294          * NULLFSINO.
295          *
296          * Note that this change affect only the in-core values. These
297          * values are not written back to disk unless any quota information
298          * is written to the disk. Even in that case, sb_pquotino field is
299          * not written to disk unless the superblock supports pquotino.
300          */
301         if (sbp->sb_uquotino == 0)
302                 sbp->sb_uquotino = NULLFSINO;
303         if (sbp->sb_gquotino == 0)
304                 sbp->sb_gquotino = NULLFSINO;
305         if (sbp->sb_pquotino == 0)
306                 sbp->sb_pquotino = NULLFSINO;
307
308         /*
309          * We need to do these manipilations only if we are working
310          * with an older version of on-disk superblock.
311          */
312         if (xfs_sb_version_has_pquotino(sbp))
313                 return;
314
315         if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
316                 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
317                                         XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
318         if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
319                 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
320                                         XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
321         sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
322
323         if (sbp->sb_qflags & XFS_PQUOTA_ACCT)  {
324                 /*
325                  * In older version of superblock, on-disk superblock only
326                  * has sb_gquotino, and in-core superblock has both sb_gquotino
327                  * and sb_pquotino. But, only one of them is supported at any
328                  * point of time. So, if PQUOTA is set in disk superblock,
329                  * copy over sb_gquotino to sb_pquotino.
330                  */
331                 sbp->sb_pquotino = sbp->sb_gquotino;
332                 sbp->sb_gquotino = NULLFSINO;
333         }
334 }
335
336 static void
337 __xfs_sb_from_disk(
338         struct xfs_sb   *to,
339         xfs_dsb_t       *from,
340         bool            convert_xquota)
341 {
342         to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
343         to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
344         to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
345         to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
346         to->sb_rextents = be64_to_cpu(from->sb_rextents);
347         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
348         to->sb_logstart = be64_to_cpu(from->sb_logstart);
349         to->sb_rootino = be64_to_cpu(from->sb_rootino);
350         to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
351         to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
352         to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
353         to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
354         to->sb_agcount = be32_to_cpu(from->sb_agcount);
355         to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
356         to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
357         to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
358         to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
359         to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
360         to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
361         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
362         to->sb_blocklog = from->sb_blocklog;
363         to->sb_sectlog = from->sb_sectlog;
364         to->sb_inodelog = from->sb_inodelog;
365         to->sb_inopblog = from->sb_inopblog;
366         to->sb_agblklog = from->sb_agblklog;
367         to->sb_rextslog = from->sb_rextslog;
368         to->sb_inprogress = from->sb_inprogress;
369         to->sb_imax_pct = from->sb_imax_pct;
370         to->sb_icount = be64_to_cpu(from->sb_icount);
371         to->sb_ifree = be64_to_cpu(from->sb_ifree);
372         to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
373         to->sb_frextents = be64_to_cpu(from->sb_frextents);
374         to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
375         to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
376         to->sb_qflags = be16_to_cpu(from->sb_qflags);
377         to->sb_flags = from->sb_flags;
378         to->sb_shared_vn = from->sb_shared_vn;
379         to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
380         to->sb_unit = be32_to_cpu(from->sb_unit);
381         to->sb_width = be32_to_cpu(from->sb_width);
382         to->sb_dirblklog = from->sb_dirblklog;
383         to->sb_logsectlog = from->sb_logsectlog;
384         to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
385         to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
386         to->sb_features2 = be32_to_cpu(from->sb_features2);
387         to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
388         to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
389         to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
390         to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
391         to->sb_features_log_incompat =
392                                 be32_to_cpu(from->sb_features_log_incompat);
393         /* crc is only used on disk, not in memory; just init to 0 here. */
394         to->sb_crc = 0;
395         to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
396         to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
397         to->sb_lsn = be64_to_cpu(from->sb_lsn);
398         /*
399          * sb_meta_uuid is only on disk if it differs from sb_uuid and the
400          * feature flag is set; if not set we keep it only in memory.
401          */
402         if (xfs_sb_version_hasmetauuid(to))
403                 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
404         else
405                 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
406         /* Convert on-disk flags to in-memory flags? */
407         if (convert_xquota)
408                 xfs_sb_quota_from_disk(to);
409 }
410
411 void
412 xfs_sb_from_disk(
413         struct xfs_sb   *to,
414         xfs_dsb_t       *from)
415 {
416         __xfs_sb_from_disk(to, from, true);
417 }
418
419 static void
420 xfs_sb_quota_to_disk(
421         struct xfs_dsb  *to,
422         struct xfs_sb   *from)
423 {
424         __uint16_t      qflags = from->sb_qflags;
425
426         to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
427         if (xfs_sb_version_has_pquotino(from)) {
428                 to->sb_qflags = cpu_to_be16(from->sb_qflags);
429                 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
430                 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
431                 return;
432         }
433
434         /*
435          * The in-core version of sb_qflags do not have XFS_OQUOTA_*
436          * flags, whereas the on-disk version does.  So, convert incore
437          * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
438          */
439         qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
440                         XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
441
442         if (from->sb_qflags &
443                         (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
444                 qflags |= XFS_OQUOTA_ENFD;
445         if (from->sb_qflags &
446                         (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
447                 qflags |= XFS_OQUOTA_CHKD;
448         to->sb_qflags = cpu_to_be16(qflags);
449
450         /*
451          * GQUOTINO and PQUOTINO cannot be used together in versions
452          * of superblock that do not have pquotino. from->sb_flags
453          * tells us which quota is active and should be copied to
454          * disk. If neither are active, we should NULL the inode.
455          *
456          * In all cases, the separate pquotino must remain 0 because it
457          * it beyond the "end" of the valid non-pquotino superblock.
458          */
459         if (from->sb_qflags & XFS_GQUOTA_ACCT)
460                 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
461         else if (from->sb_qflags & XFS_PQUOTA_ACCT)
462                 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
463         else {
464                 /*
465                  * We can't rely on just the fields being logged to tell us
466                  * that it is safe to write NULLFSINO - we should only do that
467                  * if quotas are not actually enabled. Hence only write
468                  * NULLFSINO if both in-core quota inodes are NULL.
469                  */
470                 if (from->sb_gquotino == NULLFSINO &&
471                     from->sb_pquotino == NULLFSINO)
472                         to->sb_gquotino = cpu_to_be64(NULLFSINO);
473         }
474
475         to->sb_pquotino = 0;
476 }
477
478 void
479 xfs_sb_to_disk(
480         struct xfs_dsb  *to,
481         struct xfs_sb   *from)
482 {
483         xfs_sb_quota_to_disk(to, from);
484
485         to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
486         to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
487         to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
488         to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
489         to->sb_rextents = cpu_to_be64(from->sb_rextents);
490         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
491         to->sb_logstart = cpu_to_be64(from->sb_logstart);
492         to->sb_rootino = cpu_to_be64(from->sb_rootino);
493         to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
494         to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
495         to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
496         to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
497         to->sb_agcount = cpu_to_be32(from->sb_agcount);
498         to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
499         to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
500         to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
501         to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
502         to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
503         to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
504         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
505         to->sb_blocklog = from->sb_blocklog;
506         to->sb_sectlog = from->sb_sectlog;
507         to->sb_inodelog = from->sb_inodelog;
508         to->sb_inopblog = from->sb_inopblog;
509         to->sb_agblklog = from->sb_agblklog;
510         to->sb_rextslog = from->sb_rextslog;
511         to->sb_inprogress = from->sb_inprogress;
512         to->sb_imax_pct = from->sb_imax_pct;
513         to->sb_icount = cpu_to_be64(from->sb_icount);
514         to->sb_ifree = cpu_to_be64(from->sb_ifree);
515         to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
516         to->sb_frextents = cpu_to_be64(from->sb_frextents);
517
518         to->sb_flags = from->sb_flags;
519         to->sb_shared_vn = from->sb_shared_vn;
520         to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
521         to->sb_unit = cpu_to_be32(from->sb_unit);
522         to->sb_width = cpu_to_be32(from->sb_width);
523         to->sb_dirblklog = from->sb_dirblklog;
524         to->sb_logsectlog = from->sb_logsectlog;
525         to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
526         to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
527
528         /*
529          * We need to ensure that bad_features2 always matches features2.
530          * Hence we enforce that here rather than having to remember to do it
531          * everywhere else that updates features2.
532          */
533         from->sb_bad_features2 = from->sb_features2;
534         to->sb_features2 = cpu_to_be32(from->sb_features2);
535         to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
536
537         if (xfs_sb_version_hascrc(from)) {
538                 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
539                 to->sb_features_ro_compat =
540                                 cpu_to_be32(from->sb_features_ro_compat);
541                 to->sb_features_incompat =
542                                 cpu_to_be32(from->sb_features_incompat);
543                 to->sb_features_log_incompat =
544                                 cpu_to_be32(from->sb_features_log_incompat);
545                 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
546                 to->sb_lsn = cpu_to_be64(from->sb_lsn);
547                 if (xfs_sb_version_hasmetauuid(from))
548                         uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
549         }
550 }
551
552 static int
553 xfs_sb_verify(
554         struct xfs_buf  *bp,
555         bool            check_version)
556 {
557         struct xfs_mount *mp = bp->b_target->bt_mount;
558         struct xfs_sb   sb;
559
560         /*
561          * Use call variant which doesn't convert quota flags from disk 
562          * format, because xfs_mount_validate_sb checks the on-disk flags.
563          */
564         __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
565
566         /*
567          * Only check the in progress field for the primary superblock as
568          * mkfs.xfs doesn't clear it from secondary superblocks.
569          */
570         return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
571                                      check_version);
572 }
573
574 /*
575  * If the superblock has the CRC feature bit set or the CRC field is non-null,
576  * check that the CRC is valid.  We check the CRC field is non-null because a
577  * single bit error could clear the feature bit and unused parts of the
578  * superblock are supposed to be zero. Hence a non-null crc field indicates that
579  * we've potentially lost a feature bit and we should check it anyway.
580  *
581  * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
582  * last field in V4 secondary superblocks.  So for secondary superblocks,
583  * we are more forgiving, and ignore CRC failures if the primary doesn't
584  * indicate that the fs version is V5.
585  */
586 static void
587 xfs_sb_read_verify(
588         struct xfs_buf  *bp)
589 {
590         struct xfs_mount *mp = bp->b_target->bt_mount;
591         struct xfs_dsb  *dsb = XFS_BUF_TO_SBP(bp);
592         int             error;
593
594         /*
595          * open code the version check to avoid needing to convert the entire
596          * superblock from disk order just to check the version number
597          */
598         if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
599             (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
600                                                 XFS_SB_VERSION_5) ||
601              dsb->sb_crc != 0)) {
602
603                 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
604                         /* Only fail bad secondaries on a known V5 filesystem */
605                         if (bp->b_bn == XFS_SB_DADDR ||
606                             xfs_sb_version_hascrc(&mp->m_sb)) {
607                                 error = -EFSBADCRC;
608                                 goto out_error;
609                         }
610                 }
611         }
612         error = xfs_sb_verify(bp, true);
613
614 out_error:
615         if (error) {
616                 xfs_buf_ioerror(bp, error);
617                 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
618                         xfs_verifier_error(bp);
619         }
620 }
621
622 /*
623  * We may be probed for a filesystem match, so we may not want to emit
624  * messages when the superblock buffer is not actually an XFS superblock.
625  * If we find an XFS superblock, then run a normal, noisy mount because we are
626  * really going to mount it and want to know about errors.
627  */
628 static void
629 xfs_sb_quiet_read_verify(
630         struct xfs_buf  *bp)
631 {
632         struct xfs_dsb  *dsb = XFS_BUF_TO_SBP(bp);
633
634         if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
635                 /* XFS filesystem, verify noisily! */
636                 xfs_sb_read_verify(bp);
637                 return;
638         }
639         /* quietly fail */
640         xfs_buf_ioerror(bp, -EWRONGFS);
641 }
642
643 static void
644 xfs_sb_write_verify(
645         struct xfs_buf          *bp)
646 {
647         struct xfs_mount        *mp = bp->b_target->bt_mount;
648         struct xfs_buf_log_item *bip = bp->b_fspriv;
649         int                     error;
650
651         error = xfs_sb_verify(bp, false);
652         if (error) {
653                 xfs_buf_ioerror(bp, error);
654                 xfs_verifier_error(bp);
655                 return;
656         }
657
658         if (!xfs_sb_version_hascrc(&mp->m_sb))
659                 return;
660
661         if (bip)
662                 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
663
664         xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
665 }
666
667 const struct xfs_buf_ops xfs_sb_buf_ops = {
668         .verify_read = xfs_sb_read_verify,
669         .verify_write = xfs_sb_write_verify,
670 };
671
672 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
673         .verify_read = xfs_sb_quiet_read_verify,
674         .verify_write = xfs_sb_write_verify,
675 };
676
677 /*
678  * xfs_mount_common
679  *
680  * Mount initialization code establishing various mount
681  * fields from the superblock associated with the given
682  * mount structure
683  */
684 void
685 xfs_sb_mount_common(
686         struct xfs_mount *mp,
687         struct xfs_sb   *sbp)
688 {
689         mp->m_agfrotor = mp->m_agirotor = 0;
690         spin_lock_init(&mp->m_agirotor_lock);
691         mp->m_maxagi = mp->m_sb.sb_agcount;
692         mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
693         mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
694         mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
695         mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
696         mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
697         mp->m_blockmask = sbp->sb_blocksize - 1;
698         mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
699         mp->m_blockwmask = mp->m_blockwsize - 1;
700
701         mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
702         mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
703         mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
704         mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
705
706         mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
707         mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
708         mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
709         mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
710
711         mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
712         mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
713         mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
714         mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
715
716         mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
717         mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
718                                         sbp->sb_inopblock);
719         mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
720
721         if (sbp->sb_spino_align)
722                 mp->m_ialloc_min_blks = sbp->sb_spino_align;
723         else
724                 mp->m_ialloc_min_blks = mp->m_ialloc_blks;
725 }
726
727 /*
728  * xfs_initialize_perag_data
729  *
730  * Read in each per-ag structure so we can count up the number of
731  * allocated inodes, free inodes and used filesystem blocks as this
732  * information is no longer persistent in the superblock. Once we have
733  * this information, write it into the in-core superblock structure.
734  */
735 int
736 xfs_initialize_perag_data(
737         struct xfs_mount *mp,
738         xfs_agnumber_t  agcount)
739 {
740         xfs_agnumber_t  index;
741         xfs_perag_t     *pag;
742         xfs_sb_t        *sbp = &mp->m_sb;
743         uint64_t        ifree = 0;
744         uint64_t        ialloc = 0;
745         uint64_t        bfree = 0;
746         uint64_t        bfreelst = 0;
747         uint64_t        btree = 0;
748         int             error;
749
750         for (index = 0; index < agcount; index++) {
751                 /*
752                  * read the agf, then the agi. This gets us
753                  * all the information we need and populates the
754                  * per-ag structures for us.
755                  */
756                 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
757                 if (error)
758                         return error;
759
760                 error = xfs_ialloc_pagi_init(mp, NULL, index);
761                 if (error)
762                         return error;
763                 pag = xfs_perag_get(mp, index);
764                 ifree += pag->pagi_freecount;
765                 ialloc += pag->pagi_count;
766                 bfree += pag->pagf_freeblks;
767                 bfreelst += pag->pagf_flcount;
768                 btree += pag->pagf_btreeblks;
769                 xfs_perag_put(pag);
770         }
771
772         /* Overwrite incore superblock counters with just-read data */
773         spin_lock(&mp->m_sb_lock);
774         sbp->sb_ifree = ifree;
775         sbp->sb_icount = ialloc;
776         sbp->sb_fdblocks = bfree + bfreelst + btree;
777         spin_unlock(&mp->m_sb_lock);
778
779         xfs_reinit_percpu_counters(mp);
780
781         return 0;
782 }
783
784 /*
785  * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
786  * into the superblock buffer to be logged.  It does not provide the higher
787  * level of locking that is needed to protect the in-core superblock from
788  * concurrent access.
789  */
790 void
791 xfs_log_sb(
792         struct xfs_trans        *tp)
793 {
794         struct xfs_mount        *mp = tp->t_mountp;
795         struct xfs_buf          *bp = xfs_trans_getsb(tp, mp, 0);
796
797         mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
798         mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
799         mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
800
801         xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
802         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
803         xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
804 }
805
806 /*
807  * xfs_sync_sb
808  *
809  * Sync the superblock to disk.
810  *
811  * Note that the caller is responsible for checking the frozen state of the
812  * filesystem. This procedure uses the non-blocking transaction allocator and
813  * thus will allow modifications to a frozen fs. This is required because this
814  * code can be called during the process of freezing where use of the high-level
815  * allocator would deadlock.
816  */
817 int
818 xfs_sync_sb(
819         struct xfs_mount        *mp,
820         bool                    wait)
821 {
822         struct xfs_trans        *tp;
823         int                     error;
824
825         tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_CHANGE, KM_SLEEP);
826         error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
827         if (error) {
828                 xfs_trans_cancel(tp);
829                 return error;
830         }
831
832         xfs_log_sb(tp);
833         if (wait)
834                 xfs_trans_set_sync(tp);
835         return xfs_trans_commit(tp);
836 }