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