Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
0004  * All Rights Reserved.
0005  */
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_log_format.h"
0011 #include "xfs_trans_resv.h"
0012 #include "xfs_mount.h"
0013 #include "xfs_ag.h"
0014 #include "xfs_inode.h"
0015 #include "xfs_errortag.h"
0016 #include "xfs_error.h"
0017 #include "xfs_icache.h"
0018 #include "xfs_trans.h"
0019 #include "xfs_ialloc.h"
0020 #include "xfs_dir2.h"
0021 
0022 #include <linux/iversion.h>
0023 
0024 /*
0025  * If we are doing readahead on an inode buffer, we might be in log recovery
0026  * reading an inode allocation buffer that hasn't yet been replayed, and hence
0027  * has not had the inode cores stamped into it. Hence for readahead, the buffer
0028  * may be potentially invalid.
0029  *
0030  * If the readahead buffer is invalid, we need to mark it with an error and
0031  * clear the DONE status of the buffer so that a followup read will re-read it
0032  * from disk. We don't report the error otherwise to avoid warnings during log
0033  * recovery and we don't get unnecessary panics on debug kernels. We use EIO here
0034  * because all we want to do is say readahead failed; there is no-one to report
0035  * the error to, so this will distinguish it from a non-ra verifier failure.
0036  * Changes to this readahead error behaviour also need to be reflected in
0037  * xfs_dquot_buf_readahead_verify().
0038  */
0039 static void
0040 xfs_inode_buf_verify(
0041     struct xfs_buf  *bp,
0042     bool        readahead)
0043 {
0044     struct xfs_mount *mp = bp->b_mount;
0045     int     i;
0046     int     ni;
0047 
0048     /*
0049      * Validate the magic number and version of every inode in the buffer
0050      */
0051     ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
0052     for (i = 0; i < ni; i++) {
0053         struct xfs_dinode   *dip;
0054         xfs_agino_t     unlinked_ino;
0055         int         di_ok;
0056 
0057         dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
0058         unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
0059         di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
0060             xfs_dinode_good_version(mp, dip->di_version) &&
0061             xfs_verify_agino_or_null(bp->b_pag, unlinked_ino);
0062         if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
0063                         XFS_ERRTAG_ITOBP_INOTOBP))) {
0064             if (readahead) {
0065                 bp->b_flags &= ~XBF_DONE;
0066                 xfs_buf_ioerror(bp, -EIO);
0067                 return;
0068             }
0069 
0070 #ifdef DEBUG
0071             xfs_alert(mp,
0072                 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
0073                 (unsigned long long)xfs_buf_daddr(bp), i,
0074                 be16_to_cpu(dip->di_magic));
0075 #endif
0076             xfs_buf_verifier_error(bp, -EFSCORRUPTED,
0077                     __func__, dip, sizeof(*dip),
0078                     NULL);
0079             return;
0080         }
0081     }
0082 }
0083 
0084 
0085 static void
0086 xfs_inode_buf_read_verify(
0087     struct xfs_buf  *bp)
0088 {
0089     xfs_inode_buf_verify(bp, false);
0090 }
0091 
0092 static void
0093 xfs_inode_buf_readahead_verify(
0094     struct xfs_buf  *bp)
0095 {
0096     xfs_inode_buf_verify(bp, true);
0097 }
0098 
0099 static void
0100 xfs_inode_buf_write_verify(
0101     struct xfs_buf  *bp)
0102 {
0103     xfs_inode_buf_verify(bp, false);
0104 }
0105 
0106 const struct xfs_buf_ops xfs_inode_buf_ops = {
0107     .name = "xfs_inode",
0108     .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
0109              cpu_to_be16(XFS_DINODE_MAGIC) },
0110     .verify_read = xfs_inode_buf_read_verify,
0111     .verify_write = xfs_inode_buf_write_verify,
0112 };
0113 
0114 const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
0115     .name = "xfs_inode_ra",
0116     .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
0117              cpu_to_be16(XFS_DINODE_MAGIC) },
0118     .verify_read = xfs_inode_buf_readahead_verify,
0119     .verify_write = xfs_inode_buf_write_verify,
0120 };
0121 
0122 
0123 /*
0124  * This routine is called to map an inode to the buffer containing the on-disk
0125  * version of the inode.  It returns a pointer to the buffer containing the
0126  * on-disk inode in the bpp parameter.
0127  */
0128 int
0129 xfs_imap_to_bp(
0130     struct xfs_mount    *mp,
0131     struct xfs_trans    *tp,
0132     struct xfs_imap     *imap,
0133     struct xfs_buf      **bpp)
0134 {
0135     return xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
0136                    imap->im_len, XBF_UNMAPPED, bpp,
0137                    &xfs_inode_buf_ops);
0138 }
0139 
0140 static inline struct timespec64 xfs_inode_decode_bigtime(uint64_t ts)
0141 {
0142     struct timespec64   tv;
0143     uint32_t        n;
0144 
0145     tv.tv_sec = xfs_bigtime_to_unix(div_u64_rem(ts, NSEC_PER_SEC, &n));
0146     tv.tv_nsec = n;
0147 
0148     return tv;
0149 }
0150 
0151 /* Convert an ondisk timestamp to an incore timestamp. */
0152 struct timespec64
0153 xfs_inode_from_disk_ts(
0154     struct xfs_dinode       *dip,
0155     const xfs_timestamp_t       ts)
0156 {
0157     struct timespec64       tv;
0158     struct xfs_legacy_timestamp *lts;
0159 
0160     if (xfs_dinode_has_bigtime(dip))
0161         return xfs_inode_decode_bigtime(be64_to_cpu(ts));
0162 
0163     lts = (struct xfs_legacy_timestamp *)&ts;
0164     tv.tv_sec = (int)be32_to_cpu(lts->t_sec);
0165     tv.tv_nsec = (int)be32_to_cpu(lts->t_nsec);
0166 
0167     return tv;
0168 }
0169 
0170 int
0171 xfs_inode_from_disk(
0172     struct xfs_inode    *ip,
0173     struct xfs_dinode   *from)
0174 {
0175     struct inode        *inode = VFS_I(ip);
0176     int         error;
0177     xfs_failaddr_t      fa;
0178 
0179     ASSERT(ip->i_cowfp == NULL);
0180 
0181     fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
0182     if (fa) {
0183         xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", from,
0184                 sizeof(*from), fa);
0185         return -EFSCORRUPTED;
0186     }
0187 
0188     /*
0189      * First get the permanent information that is needed to allocate an
0190      * inode. If the inode is unused, mode is zero and we shouldn't mess
0191      * with the uninitialized part of it.
0192      */
0193     if (!xfs_has_v3inodes(ip->i_mount))
0194         ip->i_flushiter = be16_to_cpu(from->di_flushiter);
0195     inode->i_generation = be32_to_cpu(from->di_gen);
0196     inode->i_mode = be16_to_cpu(from->di_mode);
0197     if (!inode->i_mode)
0198         return 0;
0199 
0200     /*
0201      * Convert v1 inodes immediately to v2 inode format as this is the
0202      * minimum inode version format we support in the rest of the code.
0203      * They will also be unconditionally written back to disk as v2 inodes.
0204      */
0205     if (unlikely(from->di_version == 1)) {
0206         set_nlink(inode, be16_to_cpu(from->di_onlink));
0207         ip->i_projid = 0;
0208     } else {
0209         set_nlink(inode, be32_to_cpu(from->di_nlink));
0210         ip->i_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
0211                     be16_to_cpu(from->di_projid_lo);
0212     }
0213 
0214     i_uid_write(inode, be32_to_cpu(from->di_uid));
0215     i_gid_write(inode, be32_to_cpu(from->di_gid));
0216 
0217     /*
0218      * Time is signed, so need to convert to signed 32 bit before
0219      * storing in inode timestamp which may be 64 bit. Otherwise
0220      * a time before epoch is converted to a time long after epoch
0221      * on 64 bit systems.
0222      */
0223     inode->i_atime = xfs_inode_from_disk_ts(from, from->di_atime);
0224     inode->i_mtime = xfs_inode_from_disk_ts(from, from->di_mtime);
0225     inode->i_ctime = xfs_inode_from_disk_ts(from, from->di_ctime);
0226 
0227     ip->i_disk_size = be64_to_cpu(from->di_size);
0228     ip->i_nblocks = be64_to_cpu(from->di_nblocks);
0229     ip->i_extsize = be32_to_cpu(from->di_extsize);
0230     ip->i_forkoff = from->di_forkoff;
0231     ip->i_diflags = be16_to_cpu(from->di_flags);
0232     ip->i_next_unlinked = be32_to_cpu(from->di_next_unlinked);
0233 
0234     if (from->di_dmevmask || from->di_dmstate)
0235         xfs_iflags_set(ip, XFS_IPRESERVE_DM_FIELDS);
0236 
0237     if (xfs_has_v3inodes(ip->i_mount)) {
0238         inode_set_iversion_queried(inode,
0239                        be64_to_cpu(from->di_changecount));
0240         ip->i_crtime = xfs_inode_from_disk_ts(from, from->di_crtime);
0241         ip->i_diflags2 = be64_to_cpu(from->di_flags2);
0242         ip->i_cowextsize = be32_to_cpu(from->di_cowextsize);
0243     }
0244 
0245     error = xfs_iformat_data_fork(ip, from);
0246     if (error)
0247         return error;
0248     if (from->di_forkoff) {
0249         error = xfs_iformat_attr_fork(ip, from);
0250         if (error)
0251             goto out_destroy_data_fork;
0252     }
0253     if (xfs_is_reflink_inode(ip))
0254         xfs_ifork_init_cow(ip);
0255     return 0;
0256 
0257 out_destroy_data_fork:
0258     xfs_idestroy_fork(&ip->i_df);
0259     return error;
0260 }
0261 
0262 /* Convert an incore timestamp to an ondisk timestamp. */
0263 static inline xfs_timestamp_t
0264 xfs_inode_to_disk_ts(
0265     struct xfs_inode        *ip,
0266     const struct timespec64     tv)
0267 {
0268     struct xfs_legacy_timestamp *lts;
0269     xfs_timestamp_t         ts;
0270 
0271     if (xfs_inode_has_bigtime(ip))
0272         return cpu_to_be64(xfs_inode_encode_bigtime(tv));
0273 
0274     lts = (struct xfs_legacy_timestamp *)&ts;
0275     lts->t_sec = cpu_to_be32(tv.tv_sec);
0276     lts->t_nsec = cpu_to_be32(tv.tv_nsec);
0277 
0278     return ts;
0279 }
0280 
0281 static inline void
0282 xfs_inode_to_disk_iext_counters(
0283     struct xfs_inode    *ip,
0284     struct xfs_dinode   *to)
0285 {
0286     if (xfs_inode_has_large_extent_counts(ip)) {
0287         to->di_big_nextents = cpu_to_be64(xfs_ifork_nextents(&ip->i_df));
0288         to->di_big_anextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_af));
0289         /*
0290          * We might be upgrading the inode to use larger extent counters
0291          * than was previously used. Hence zero the unused field.
0292          */
0293         to->di_nrext64_pad = cpu_to_be16(0);
0294     } else {
0295         to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
0296         to->di_anextents = cpu_to_be16(xfs_ifork_nextents(&ip->i_af));
0297     }
0298 }
0299 
0300 void
0301 xfs_inode_to_disk(
0302     struct xfs_inode    *ip,
0303     struct xfs_dinode   *to,
0304     xfs_lsn_t       lsn)
0305 {
0306     struct inode        *inode = VFS_I(ip);
0307 
0308     to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
0309     to->di_onlink = 0;
0310 
0311     to->di_format = xfs_ifork_format(&ip->i_df);
0312     to->di_uid = cpu_to_be32(i_uid_read(inode));
0313     to->di_gid = cpu_to_be32(i_gid_read(inode));
0314     to->di_projid_lo = cpu_to_be16(ip->i_projid & 0xffff);
0315     to->di_projid_hi = cpu_to_be16(ip->i_projid >> 16);
0316 
0317     to->di_atime = xfs_inode_to_disk_ts(ip, inode->i_atime);
0318     to->di_mtime = xfs_inode_to_disk_ts(ip, inode->i_mtime);
0319     to->di_ctime = xfs_inode_to_disk_ts(ip, inode->i_ctime);
0320     to->di_nlink = cpu_to_be32(inode->i_nlink);
0321     to->di_gen = cpu_to_be32(inode->i_generation);
0322     to->di_mode = cpu_to_be16(inode->i_mode);
0323 
0324     to->di_size = cpu_to_be64(ip->i_disk_size);
0325     to->di_nblocks = cpu_to_be64(ip->i_nblocks);
0326     to->di_extsize = cpu_to_be32(ip->i_extsize);
0327     to->di_forkoff = ip->i_forkoff;
0328     to->di_aformat = xfs_ifork_format(&ip->i_af);
0329     to->di_flags = cpu_to_be16(ip->i_diflags);
0330 
0331     if (xfs_has_v3inodes(ip->i_mount)) {
0332         to->di_version = 3;
0333         to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
0334         to->di_crtime = xfs_inode_to_disk_ts(ip, ip->i_crtime);
0335         to->di_flags2 = cpu_to_be64(ip->i_diflags2);
0336         to->di_cowextsize = cpu_to_be32(ip->i_cowextsize);
0337         to->di_ino = cpu_to_be64(ip->i_ino);
0338         to->di_lsn = cpu_to_be64(lsn);
0339         memset(to->di_pad2, 0, sizeof(to->di_pad2));
0340         uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
0341         to->di_v3_pad = 0;
0342     } else {
0343         to->di_version = 2;
0344         to->di_flushiter = cpu_to_be16(ip->i_flushiter);
0345         memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
0346     }
0347 
0348     xfs_inode_to_disk_iext_counters(ip, to);
0349 }
0350 
0351 static xfs_failaddr_t
0352 xfs_dinode_verify_fork(
0353     struct xfs_dinode   *dip,
0354     struct xfs_mount    *mp,
0355     int         whichfork)
0356 {
0357     xfs_extnum_t        di_nextents;
0358     xfs_extnum_t        max_extents;
0359     mode_t          mode = be16_to_cpu(dip->di_mode);
0360     uint32_t        fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
0361     uint32_t        fork_format = XFS_DFORK_FORMAT(dip, whichfork);
0362 
0363     di_nextents = xfs_dfork_nextents(dip, whichfork);
0364 
0365     /*
0366      * For fork types that can contain local data, check that the fork
0367      * format matches the size of local data contained within the fork.
0368      *
0369      * For all types, check that when the size says the should be in extent
0370      * or btree format, the inode isn't claiming it is in local format.
0371      */
0372     if (whichfork == XFS_DATA_FORK) {
0373         if (S_ISDIR(mode) || S_ISLNK(mode)) {
0374             if (be64_to_cpu(dip->di_size) <= fork_size &&
0375                 fork_format != XFS_DINODE_FMT_LOCAL)
0376                 return __this_address;
0377         }
0378 
0379         if (be64_to_cpu(dip->di_size) > fork_size &&
0380             fork_format == XFS_DINODE_FMT_LOCAL)
0381             return __this_address;
0382     }
0383 
0384     switch (fork_format) {
0385     case XFS_DINODE_FMT_LOCAL:
0386         /*
0387          * No local regular files yet.
0388          */
0389         if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
0390             return __this_address;
0391         if (di_nextents)
0392             return __this_address;
0393         break;
0394     case XFS_DINODE_FMT_EXTENTS:
0395         if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
0396             return __this_address;
0397         break;
0398     case XFS_DINODE_FMT_BTREE:
0399         max_extents = xfs_iext_max_nextents(
0400                     xfs_dinode_has_large_extent_counts(dip),
0401                     whichfork);
0402         if (di_nextents > max_extents)
0403             return __this_address;
0404         break;
0405     default:
0406         return __this_address;
0407     }
0408     return NULL;
0409 }
0410 
0411 static xfs_failaddr_t
0412 xfs_dinode_verify_forkoff(
0413     struct xfs_dinode   *dip,
0414     struct xfs_mount    *mp)
0415 {
0416     if (!dip->di_forkoff)
0417         return NULL;
0418 
0419     switch (dip->di_format)  {
0420     case XFS_DINODE_FMT_DEV:
0421         if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
0422             return __this_address;
0423         break;
0424     case XFS_DINODE_FMT_LOCAL:  /* fall through ... */
0425     case XFS_DINODE_FMT_EXTENTS:    /* fall through ... */
0426     case XFS_DINODE_FMT_BTREE:
0427         if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
0428             return __this_address;
0429         break;
0430     default:
0431         return __this_address;
0432     }
0433     return NULL;
0434 }
0435 
0436 static xfs_failaddr_t
0437 xfs_dinode_verify_nrext64(
0438     struct xfs_mount    *mp,
0439     struct xfs_dinode   *dip)
0440 {
0441     if (xfs_dinode_has_large_extent_counts(dip)) {
0442         if (!xfs_has_large_extent_counts(mp))
0443             return __this_address;
0444         if (dip->di_nrext64_pad != 0)
0445             return __this_address;
0446     } else if (dip->di_version >= 3) {
0447         if (dip->di_v3_pad != 0)
0448             return __this_address;
0449     }
0450 
0451     return NULL;
0452 }
0453 
0454 xfs_failaddr_t
0455 xfs_dinode_verify(
0456     struct xfs_mount    *mp,
0457     xfs_ino_t       ino,
0458     struct xfs_dinode   *dip)
0459 {
0460     xfs_failaddr_t      fa;
0461     uint16_t        mode;
0462     uint16_t        flags;
0463     uint64_t        flags2;
0464     uint64_t        di_size;
0465     xfs_extnum_t        nextents;
0466     xfs_extnum_t        naextents;
0467     xfs_filblks_t       nblocks;
0468 
0469     if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
0470         return __this_address;
0471 
0472     /* Verify v3 integrity information first */
0473     if (dip->di_version >= 3) {
0474         if (!xfs_has_v3inodes(mp))
0475             return __this_address;
0476         if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
0477                       XFS_DINODE_CRC_OFF))
0478             return __this_address;
0479         if (be64_to_cpu(dip->di_ino) != ino)
0480             return __this_address;
0481         if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
0482             return __this_address;
0483     }
0484 
0485     /* don't allow invalid i_size */
0486     di_size = be64_to_cpu(dip->di_size);
0487     if (di_size & (1ULL << 63))
0488         return __this_address;
0489 
0490     mode = be16_to_cpu(dip->di_mode);
0491     if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
0492         return __this_address;
0493 
0494     /* No zero-length symlinks/dirs. */
0495     if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
0496         return __this_address;
0497 
0498     fa = xfs_dinode_verify_nrext64(mp, dip);
0499     if (fa)
0500         return fa;
0501 
0502     nextents = xfs_dfork_data_extents(dip);
0503     naextents = xfs_dfork_attr_extents(dip);
0504     nblocks = be64_to_cpu(dip->di_nblocks);
0505 
0506     /* Fork checks carried over from xfs_iformat_fork */
0507     if (mode && nextents + naextents > nblocks)
0508         return __this_address;
0509 
0510     if (S_ISDIR(mode) && nextents > mp->m_dir_geo->max_extents)
0511         return __this_address;
0512 
0513     if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
0514         return __this_address;
0515 
0516     flags = be16_to_cpu(dip->di_flags);
0517 
0518     if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
0519         return __this_address;
0520 
0521     /* check for illegal values of forkoff */
0522     fa = xfs_dinode_verify_forkoff(dip, mp);
0523     if (fa)
0524         return fa;
0525 
0526     /* Do we have appropriate data fork formats for the mode? */
0527     switch (mode & S_IFMT) {
0528     case S_IFIFO:
0529     case S_IFCHR:
0530     case S_IFBLK:
0531     case S_IFSOCK:
0532         if (dip->di_format != XFS_DINODE_FMT_DEV)
0533             return __this_address;
0534         break;
0535     case S_IFREG:
0536     case S_IFLNK:
0537     case S_IFDIR:
0538         fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
0539         if (fa)
0540             return fa;
0541         break;
0542     case 0:
0543         /* Uninitialized inode ok. */
0544         break;
0545     default:
0546         return __this_address;
0547     }
0548 
0549     if (dip->di_forkoff) {
0550         fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
0551         if (fa)
0552             return fa;
0553     } else {
0554         /*
0555          * If there is no fork offset, this may be a freshly-made inode
0556          * in a new disk cluster, in which case di_aformat is zeroed.
0557          * Otherwise, such an inode must be in EXTENTS format; this goes
0558          * for freed inodes as well.
0559          */
0560         switch (dip->di_aformat) {
0561         case 0:
0562         case XFS_DINODE_FMT_EXTENTS:
0563             break;
0564         default:
0565             return __this_address;
0566         }
0567         if (naextents)
0568             return __this_address;
0569     }
0570 
0571     /* extent size hint validation */
0572     fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
0573             mode, flags);
0574     if (fa)
0575         return fa;
0576 
0577     /* only version 3 or greater inodes are extensively verified here */
0578     if (dip->di_version < 3)
0579         return NULL;
0580 
0581     flags2 = be64_to_cpu(dip->di_flags2);
0582 
0583     /* don't allow reflink/cowextsize if we don't have reflink */
0584     if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
0585          !xfs_has_reflink(mp))
0586         return __this_address;
0587 
0588     /* only regular files get reflink */
0589     if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
0590         return __this_address;
0591 
0592     /* don't let reflink and realtime mix */
0593     if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
0594         return __this_address;
0595 
0596     /* COW extent size hint validation */
0597     fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
0598             mode, flags, flags2);
0599     if (fa)
0600         return fa;
0601 
0602     /* bigtime iflag can only happen on bigtime filesystems */
0603     if (xfs_dinode_has_bigtime(dip) &&
0604         !xfs_has_bigtime(mp))
0605         return __this_address;
0606 
0607     return NULL;
0608 }
0609 
0610 void
0611 xfs_dinode_calc_crc(
0612     struct xfs_mount    *mp,
0613     struct xfs_dinode   *dip)
0614 {
0615     uint32_t        crc;
0616 
0617     if (dip->di_version < 3)
0618         return;
0619 
0620     ASSERT(xfs_has_crc(mp));
0621     crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
0622                   XFS_DINODE_CRC_OFF);
0623     dip->di_crc = xfs_end_cksum(crc);
0624 }
0625 
0626 /*
0627  * Validate di_extsize hint.
0628  *
0629  * 1. Extent size hint is only valid for directories and regular files.
0630  * 2. FS_XFLAG_EXTSIZE is only valid for regular files.
0631  * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
0632  * 4. Hint cannot be larger than MAXTEXTLEN.
0633  * 5. Can be changed on directories at any time.
0634  * 6. Hint value of 0 turns off hints, clears inode flags.
0635  * 7. Extent size must be a multiple of the appropriate block size.
0636  *    For realtime files, this is the rt extent size.
0637  * 8. For non-realtime files, the extent size hint must be limited
0638  *    to half the AG size to avoid alignment extending the extent beyond the
0639  *    limits of the AG.
0640  */
0641 xfs_failaddr_t
0642 xfs_inode_validate_extsize(
0643     struct xfs_mount        *mp,
0644     uint32_t            extsize,
0645     uint16_t            mode,
0646     uint16_t            flags)
0647 {
0648     bool                rt_flag;
0649     bool                hint_flag;
0650     bool                inherit_flag;
0651     uint32_t            extsize_bytes;
0652     uint32_t            blocksize_bytes;
0653 
0654     rt_flag = (flags & XFS_DIFLAG_REALTIME);
0655     hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
0656     inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
0657     extsize_bytes = XFS_FSB_TO_B(mp, extsize);
0658 
0659     /*
0660      * This comment describes a historic gap in this verifier function.
0661      *
0662      * For a directory with both RTINHERIT and EXTSZINHERIT flags set, this
0663      * function has never checked that the extent size hint is an integer
0664      * multiple of the realtime extent size.  Since we allow users to set
0665      * this combination  on non-rt filesystems /and/ to change the rt
0666      * extent size when adding a rt device to a filesystem, the net effect
0667      * is that users can configure a filesystem anticipating one rt
0668      * geometry and change their minds later.  Directories do not use the
0669      * extent size hint, so this is harmless for them.
0670      *
0671      * If a directory with a misaligned extent size hint is allowed to
0672      * propagate that hint into a new regular realtime file, the result
0673      * is that the inode cluster buffer verifier will trigger a corruption
0674      * shutdown the next time it is run, because the verifier has always
0675      * enforced the alignment rule for regular files.
0676      *
0677      * Because we allow administrators to set a new rt extent size when
0678      * adding a rt section, we cannot add a check to this verifier because
0679      * that will result a new source of directory corruption errors when
0680      * reading an existing filesystem.  Instead, we rely on callers to
0681      * decide when alignment checks are appropriate, and fix things up as
0682      * needed.
0683      */
0684 
0685     if (rt_flag)
0686         blocksize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
0687     else
0688         blocksize_bytes = mp->m_sb.sb_blocksize;
0689 
0690     if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
0691         return __this_address;
0692 
0693     if (hint_flag && !S_ISREG(mode))
0694         return __this_address;
0695 
0696     if (inherit_flag && !S_ISDIR(mode))
0697         return __this_address;
0698 
0699     if ((hint_flag || inherit_flag) && extsize == 0)
0700         return __this_address;
0701 
0702     /* free inodes get flags set to zero but extsize remains */
0703     if (mode && !(hint_flag || inherit_flag) && extsize != 0)
0704         return __this_address;
0705 
0706     if (extsize_bytes % blocksize_bytes)
0707         return __this_address;
0708 
0709     if (extsize > XFS_MAX_BMBT_EXTLEN)
0710         return __this_address;
0711 
0712     if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
0713         return __this_address;
0714 
0715     return NULL;
0716 }
0717 
0718 /*
0719  * Validate di_cowextsize hint.
0720  *
0721  * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
0722  *    The inode does not have to have any shared blocks, but it must be a v3.
0723  * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
0724  *    for a directory, the hint is propagated to new files.
0725  * 3. Can be changed on files & directories at any time.
0726  * 4. Hint value of 0 turns off hints, clears inode flags.
0727  * 5. Extent size must be a multiple of the appropriate block size.
0728  * 6. The extent size hint must be limited to half the AG size to avoid
0729  *    alignment extending the extent beyond the limits of the AG.
0730  */
0731 xfs_failaddr_t
0732 xfs_inode_validate_cowextsize(
0733     struct xfs_mount        *mp,
0734     uint32_t            cowextsize,
0735     uint16_t            mode,
0736     uint16_t            flags,
0737     uint64_t            flags2)
0738 {
0739     bool                rt_flag;
0740     bool                hint_flag;
0741     uint32_t            cowextsize_bytes;
0742 
0743     rt_flag = (flags & XFS_DIFLAG_REALTIME);
0744     hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
0745     cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
0746 
0747     if (hint_flag && !xfs_has_reflink(mp))
0748         return __this_address;
0749 
0750     if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
0751         return __this_address;
0752 
0753     if (hint_flag && cowextsize == 0)
0754         return __this_address;
0755 
0756     /* free inodes get flags set to zero but cowextsize remains */
0757     if (mode && !hint_flag && cowextsize != 0)
0758         return __this_address;
0759 
0760     if (hint_flag && rt_flag)
0761         return __this_address;
0762 
0763     if (cowextsize_bytes % mp->m_sb.sb_blocksize)
0764         return __this_address;
0765 
0766     if (cowextsize > XFS_MAX_BMBT_EXTLEN)
0767         return __this_address;
0768 
0769     if (cowextsize > mp->m_sb.sb_agblocks / 2)
0770         return __this_address;
0771 
0772     return NULL;
0773 }