10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
230f712c9SDave Chinner /*
330f712c9SDave Chinner * Copyright (c) 2000-2006 Silicon Graphics, Inc.
430f712c9SDave Chinner * All Rights Reserved.
530f712c9SDave Chinner */
630f712c9SDave Chinner #include "xfs.h"
730f712c9SDave Chinner #include "xfs_fs.h"
830f712c9SDave Chinner #include "xfs_shared.h"
930f712c9SDave Chinner #include "xfs_format.h"
1030f712c9SDave Chinner #include "xfs_log_format.h"
1130f712c9SDave Chinner #include "xfs_trans_resv.h"
1230f712c9SDave Chinner #include "xfs_mount.h"
132d6ca832SDave Chinner #include "xfs_ag.h"
1430f712c9SDave Chinner #include "xfs_inode.h"
15e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
1630f712c9SDave Chinner #include "xfs_error.h"
1730f712c9SDave Chinner #include "xfs_icache.h"
1830f712c9SDave Chinner #include "xfs_trans.h"
1930f712c9SDave Chinner #include "xfs_ialloc.h"
20a324cbf1SAmir Goldstein #include "xfs_dir2.h"
21baf44fa5SDarrick J. Wong #include "xfs_health.h"
2230f712c9SDave Chinner
23f0e28280SJeff Layton #include <linux/iversion.h>
24f0e28280SJeff Layton
2530f712c9SDave Chinner /*
2630f712c9SDave Chinner * If we are doing readahead on an inode buffer, we might be in log recovery
2730f712c9SDave Chinner * reading an inode allocation buffer that hasn't yet been replayed, and hence
2830f712c9SDave Chinner * has not had the inode cores stamped into it. Hence for readahead, the buffer
2930f712c9SDave Chinner * may be potentially invalid.
3030f712c9SDave Chinner *
31b79f4a1cSDave Chinner * If the readahead buffer is invalid, we need to mark it with an error and
32b79f4a1cSDave Chinner * clear the DONE status of the buffer so that a followup read will re-read it
33b79f4a1cSDave Chinner * from disk. We don't report the error otherwise to avoid warnings during log
3406734e3cSKeyur Patel * recovery and we don't get unnecessary panics on debug kernels. We use EIO here
35b79f4a1cSDave Chinner * because all we want to do is say readahead failed; there is no-one to report
36b79f4a1cSDave Chinner * the error to, so this will distinguish it from a non-ra verifier failure.
3706734e3cSKeyur Patel * Changes to this readahead error behaviour also need to be reflected in
387d6a13f0SDave Chinner * xfs_dquot_buf_readahead_verify().
3930f712c9SDave Chinner */
4030f712c9SDave Chinner static void
xfs_inode_buf_verify(struct xfs_buf * bp,bool readahead)4130f712c9SDave Chinner xfs_inode_buf_verify(
4230f712c9SDave Chinner struct xfs_buf *bp,
4330f712c9SDave Chinner bool readahead)
4430f712c9SDave Chinner {
45dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount;
4630f712c9SDave Chinner int i;
4730f712c9SDave Chinner int ni;
4830f712c9SDave Chinner
4930f712c9SDave Chinner /*
5030f712c9SDave Chinner * Validate the magic number and version of every inode in the buffer
5130f712c9SDave Chinner */
5230f712c9SDave Chinner ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
5330f712c9SDave Chinner for (i = 0; i < ni; i++) {
54de38db72SChristoph Hellwig struct xfs_dinode *dip;
556a96c565SDarrick J. Wong xfs_agino_t unlinked_ino;
56de38db72SChristoph Hellwig int di_ok;
5730f712c9SDave Chinner
5888ee2df7SChristoph Hellwig dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
596a96c565SDarrick J. Wong unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
6015baadf7SDarrick J. Wong di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
61cf28e17cSDave Chinner xfs_dinode_good_version(mp, dip->di_version) &&
622d6ca832SDave Chinner xfs_verify_agino_or_null(bp->b_pag, unlinked_ino);
6330f712c9SDave Chinner if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
649e24cfd0SDarrick J. Wong XFS_ERRTAG_ITOBP_INOTOBP))) {
6530f712c9SDave Chinner if (readahead) {
6630f712c9SDave Chinner bp->b_flags &= ~XBF_DONE;
67b79f4a1cSDave Chinner xfs_buf_ioerror(bp, -EIO);
6830f712c9SDave Chinner return;
6930f712c9SDave Chinner }
7030f712c9SDave Chinner
7130f712c9SDave Chinner #ifdef DEBUG
7230f712c9SDave Chinner xfs_alert(mp,
7330f712c9SDave Chinner "bad inode magic/vsn daddr %lld #%d (magic=%x)",
749343ee76SDave Chinner (unsigned long long)xfs_buf_daddr(bp), i,
7530f712c9SDave Chinner be16_to_cpu(dip->di_magic));
7630f712c9SDave Chinner #endif
776edb1810SDarrick J. Wong xfs_buf_verifier_error(bp, -EFSCORRUPTED,
786edb1810SDarrick J. Wong __func__, dip, sizeof(*dip),
796edb1810SDarrick J. Wong NULL);
806a96c565SDarrick J. Wong return;
8130f712c9SDave Chinner }
8230f712c9SDave Chinner }
8330f712c9SDave Chinner }
8430f712c9SDave Chinner
8530f712c9SDave Chinner
8630f712c9SDave Chinner static void
xfs_inode_buf_read_verify(struct xfs_buf * bp)8730f712c9SDave Chinner xfs_inode_buf_read_verify(
8830f712c9SDave Chinner struct xfs_buf *bp)
8930f712c9SDave Chinner {
9030f712c9SDave Chinner xfs_inode_buf_verify(bp, false);
9130f712c9SDave Chinner }
9230f712c9SDave Chinner
9330f712c9SDave Chinner static void
xfs_inode_buf_readahead_verify(struct xfs_buf * bp)9430f712c9SDave Chinner xfs_inode_buf_readahead_verify(
9530f712c9SDave Chinner struct xfs_buf *bp)
9630f712c9SDave Chinner {
9730f712c9SDave Chinner xfs_inode_buf_verify(bp, true);
9830f712c9SDave Chinner }
9930f712c9SDave Chinner
10030f712c9SDave Chinner static void
xfs_inode_buf_write_verify(struct xfs_buf * bp)10130f712c9SDave Chinner xfs_inode_buf_write_verify(
10230f712c9SDave Chinner struct xfs_buf *bp)
10330f712c9SDave Chinner {
10430f712c9SDave Chinner xfs_inode_buf_verify(bp, false);
10530f712c9SDave Chinner }
10630f712c9SDave Chinner
10730f712c9SDave Chinner const struct xfs_buf_ops xfs_inode_buf_ops = {
108233135b7SEric Sandeen .name = "xfs_inode",
10915baadf7SDarrick J. Wong .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
1102bfe7069SDarrick J. Wong cpu_to_be16(XFS_DINODE_MAGIC) },
11130f712c9SDave Chinner .verify_read = xfs_inode_buf_read_verify,
11230f712c9SDave Chinner .verify_write = xfs_inode_buf_write_verify,
11330f712c9SDave Chinner };
11430f712c9SDave Chinner
11530f712c9SDave Chinner const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
116e34d3e74SBrian Foster .name = "xfs_inode_ra",
11715baadf7SDarrick J. Wong .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
1182bfe7069SDarrick J. Wong cpu_to_be16(XFS_DINODE_MAGIC) },
11930f712c9SDave Chinner .verify_read = xfs_inode_buf_readahead_verify,
12030f712c9SDave Chinner .verify_write = xfs_inode_buf_write_verify,
12130f712c9SDave Chinner };
12230f712c9SDave Chinner
12330f712c9SDave Chinner
12430f712c9SDave Chinner /*
12530f712c9SDave Chinner * This routine is called to map an inode to the buffer containing the on-disk
12630f712c9SDave Chinner * version of the inode. It returns a pointer to the buffer containing the
127af9dcddeSChristoph Hellwig * on-disk inode in the bpp parameter.
12830f712c9SDave Chinner */
12930f712c9SDave Chinner int
xfs_imap_to_bp(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_imap * imap,struct xfs_buf ** bpp)13030f712c9SDave Chinner xfs_imap_to_bp(
13130f712c9SDave Chinner struct xfs_mount *mp,
13230f712c9SDave Chinner struct xfs_trans *tp,
13330f712c9SDave Chinner struct xfs_imap *imap,
134af9dcddeSChristoph Hellwig struct xfs_buf **bpp)
13530f712c9SDave Chinner {
136baf44fa5SDarrick J. Wong int error;
137baf44fa5SDarrick J. Wong
138baf44fa5SDarrick J. Wong error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
139baf44fa5SDarrick J. Wong imap->im_len, XBF_UNMAPPED, bpp, &xfs_inode_buf_ops);
140baf44fa5SDarrick J. Wong if (xfs_metadata_is_sick(error))
141baf44fa5SDarrick J. Wong xfs_agno_mark_sick(mp, xfs_daddr_to_agno(mp, imap->im_blkno),
1420e24ec3cSDarrick J. Wong XFS_SICK_AG_INODES);
143baf44fa5SDarrick J. Wong return error;
14430f712c9SDave Chinner }
14530f712c9SDave Chinner
xfs_inode_decode_bigtime(uint64_t ts)146f93e5436SDarrick J. Wong static inline struct timespec64 xfs_inode_decode_bigtime(uint64_t ts)
147f93e5436SDarrick J. Wong {
148f93e5436SDarrick J. Wong struct timespec64 tv;
149f93e5436SDarrick J. Wong uint32_t n;
150f93e5436SDarrick J. Wong
151f93e5436SDarrick J. Wong tv.tv_sec = xfs_bigtime_to_unix(div_u64_rem(ts, NSEC_PER_SEC, &n));
152f93e5436SDarrick J. Wong tv.tv_nsec = n;
153f93e5436SDarrick J. Wong
154f93e5436SDarrick J. Wong return tv;
155f93e5436SDarrick J. Wong }
156f93e5436SDarrick J. Wong
1575a0bb066SDarrick J. Wong /* Convert an ondisk timestamp to an incore timestamp. */
1585a0bb066SDarrick J. Wong struct timespec64
xfs_inode_from_disk_ts(struct xfs_dinode * dip,const xfs_timestamp_t ts)1595a0bb066SDarrick J. Wong xfs_inode_from_disk_ts(
160f93e5436SDarrick J. Wong struct xfs_dinode *dip,
1615a0bb066SDarrick J. Wong const xfs_timestamp_t ts)
1625a0bb066SDarrick J. Wong {
1635a0bb066SDarrick J. Wong struct timespec64 tv;
1645a0bb066SDarrick J. Wong struct xfs_legacy_timestamp *lts;
1655a0bb066SDarrick J. Wong
166f93e5436SDarrick J. Wong if (xfs_dinode_has_bigtime(dip))
167f93e5436SDarrick J. Wong return xfs_inode_decode_bigtime(be64_to_cpu(ts));
168f93e5436SDarrick J. Wong
1695a0bb066SDarrick J. Wong lts = (struct xfs_legacy_timestamp *)&ts;
1705a0bb066SDarrick J. Wong tv.tv_sec = (int)be32_to_cpu(lts->t_sec);
1715a0bb066SDarrick J. Wong tv.tv_nsec = (int)be32_to_cpu(lts->t_nsec);
1725a0bb066SDarrick J. Wong
1735a0bb066SDarrick J. Wong return tv;
1745a0bb066SDarrick J. Wong }
1755a0bb066SDarrick J. Wong
176cb7d5859SChristoph Hellwig int
xfs_inode_from_disk(struct xfs_inode * ip,struct xfs_dinode * from)1773987848cSDave Chinner xfs_inode_from_disk(
1783987848cSDave Chinner struct xfs_inode *ip,
179f8d55aa0SDave Chinner struct xfs_dinode *from)
18030f712c9SDave Chinner {
1813987848cSDave Chinner struct inode *inode = VFS_I(ip);
1829229d18eSChristoph Hellwig int error;
1832d6051d4SChristoph Hellwig xfs_failaddr_t fa;
1849229d18eSChristoph Hellwig
1859229d18eSChristoph Hellwig ASSERT(ip->i_cowfp == NULL);
1863987848cSDave Chinner
1872d6051d4SChristoph Hellwig fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
1882d6051d4SChristoph Hellwig if (fa) {
1892d6051d4SChristoph Hellwig xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", from,
1902d6051d4SChristoph Hellwig sizeof(*from), fa);
1912d6051d4SChristoph Hellwig return -EFSCORRUPTED;
1922d6051d4SChristoph Hellwig }
1932d6051d4SChristoph Hellwig
194faeb4e47SDave Chinner /*
1950bce8173SChristoph Hellwig * First get the permanent information that is needed to allocate an
1960bce8173SChristoph Hellwig * inode. If the inode is unused, mode is zero and we shouldn't mess
19706734e3cSKeyur Patel * with the uninitialized part of it.
1980bce8173SChristoph Hellwig */
199ebd9027dSDave Chinner if (!xfs_has_v3inodes(ip->i_mount))
200965e0a1aSChristoph Hellwig ip->i_flushiter = be16_to_cpu(from->di_flushiter);
2010bce8173SChristoph Hellwig inode->i_generation = be32_to_cpu(from->di_gen);
2020bce8173SChristoph Hellwig inode->i_mode = be16_to_cpu(from->di_mode);
2030bce8173SChristoph Hellwig if (!inode->i_mode)
2040bce8173SChristoph Hellwig return 0;
2050bce8173SChristoph Hellwig
2060bce8173SChristoph Hellwig /*
207faeb4e47SDave Chinner * Convert v1 inodes immediately to v2 inode format as this is the
208faeb4e47SDave Chinner * minimum inode version format we support in the rest of the code.
2096471e9c5SChristoph Hellwig * They will also be unconditionally written back to disk as v2 inodes.
210faeb4e47SDave Chinner */
2116471e9c5SChristoph Hellwig if (unlikely(from->di_version == 1)) {
21254d7b5c1SDave Chinner set_nlink(inode, be16_to_cpu(from->di_onlink));
213ceaf603cSChristoph Hellwig ip->i_projid = 0;
214faeb4e47SDave Chinner } else {
21554d7b5c1SDave Chinner set_nlink(inode, be32_to_cpu(from->di_nlink));
216ceaf603cSChristoph Hellwig ip->i_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
217de7a866fSChristoph Hellwig be16_to_cpu(from->di_projid_lo);
218faeb4e47SDave Chinner }
219faeb4e47SDave Chinner
220ba8adad5SChristoph Hellwig i_uid_write(inode, be32_to_cpu(from->di_uid));
221ba8adad5SChristoph Hellwig i_gid_write(inode, be32_to_cpu(from->di_gid));
2223987848cSDave Chinner
2233987848cSDave Chinner /*
2243987848cSDave Chinner * Time is signed, so need to convert to signed 32 bit before
2253987848cSDave Chinner * storing in inode timestamp which may be 64 bit. Otherwise
2263987848cSDave Chinner * a time before epoch is converted to a time long after epoch
2273987848cSDave Chinner * on 64 bit systems.
2283987848cSDave Chinner */
22975d1e312SJeff Layton inode_set_atime_to_ts(inode,
23075d1e312SJeff Layton xfs_inode_from_disk_ts(from, from->di_atime));
23175d1e312SJeff Layton inode_set_mtime_to_ts(inode,
23275d1e312SJeff Layton xfs_inode_from_disk_ts(from, from->di_mtime));
233a0a415e3SJeff Layton inode_set_ctime_to_ts(inode,
234a0a415e3SJeff Layton xfs_inode_from_disk_ts(from, from->di_ctime));
2353987848cSDave Chinner
23613d2c10bSChristoph Hellwig ip->i_disk_size = be64_to_cpu(from->di_size);
2376e73a545SChristoph Hellwig ip->i_nblocks = be64_to_cpu(from->di_nblocks);
238031474c2SChristoph Hellwig ip->i_extsize = be32_to_cpu(from->di_extsize);
2397821ea30SChristoph Hellwig ip->i_forkoff = from->di_forkoff;
240db07349dSChristoph Hellwig ip->i_diflags = be16_to_cpu(from->di_flags);
2414fcc94d6SDave Chinner ip->i_next_unlinked = be32_to_cpu(from->di_next_unlinked);
24230f712c9SDave Chinner
2439b3beb02SChristoph Hellwig if (from->di_dmevmask || from->di_dmstate)
2449b3beb02SChristoph Hellwig xfs_iflags_set(ip, XFS_IPRESERVE_DM_FIELDS);
2459b3beb02SChristoph Hellwig
246ebd9027dSDave Chinner if (xfs_has_v3inodes(ip->i_mount)) {
247f0e28280SJeff Layton inode_set_iversion_queried(inode,
248f0e28280SJeff Layton be64_to_cpu(from->di_changecount));
249e98d5e88SChristoph Hellwig ip->i_crtime = xfs_inode_from_disk_ts(from, from->di_crtime);
2503e09ab8fSChristoph Hellwig ip->i_diflags2 = be64_to_cpu(from->di_flags2);
251b33ce57dSChristoph Hellwig ip->i_cowextsize = be32_to_cpu(from->di_cowextsize);
25230f712c9SDave Chinner }
253cb7d5859SChristoph Hellwig
2549229d18eSChristoph Hellwig error = xfs_iformat_data_fork(ip, from);
2559229d18eSChristoph Hellwig if (error)
2569229d18eSChristoph Hellwig return error;
25709c38eddSChristoph Hellwig if (from->di_forkoff) {
2589229d18eSChristoph Hellwig error = xfs_iformat_attr_fork(ip, from);
2599229d18eSChristoph Hellwig if (error)
2609229d18eSChristoph Hellwig goto out_destroy_data_fork;
2619229d18eSChristoph Hellwig }
2629229d18eSChristoph Hellwig if (xfs_is_reflink_inode(ip))
2639229d18eSChristoph Hellwig xfs_ifork_init_cow(ip);
2649229d18eSChristoph Hellwig return 0;
2659229d18eSChristoph Hellwig
2669229d18eSChristoph Hellwig out_destroy_data_fork:
267ef838512SChristoph Hellwig xfs_idestroy_fork(&ip->i_df);
2689229d18eSChristoph Hellwig return error;
26930f712c9SDave Chinner }
27030f712c9SDave Chinner
2715a0bb066SDarrick J. Wong /* Convert an incore timestamp to an ondisk timestamp. */
2725a0bb066SDarrick J. Wong static inline xfs_timestamp_t
xfs_inode_to_disk_ts(struct xfs_inode * ip,const struct timespec64 tv)2735a0bb066SDarrick J. Wong xfs_inode_to_disk_ts(
274f93e5436SDarrick J. Wong struct xfs_inode *ip,
2755a0bb066SDarrick J. Wong const struct timespec64 tv)
2765a0bb066SDarrick J. Wong {
2775a0bb066SDarrick J. Wong struct xfs_legacy_timestamp *lts;
2785a0bb066SDarrick J. Wong xfs_timestamp_t ts;
2795a0bb066SDarrick J. Wong
280f93e5436SDarrick J. Wong if (xfs_inode_has_bigtime(ip))
281f93e5436SDarrick J. Wong return cpu_to_be64(xfs_inode_encode_bigtime(tv));
282f93e5436SDarrick J. Wong
2835a0bb066SDarrick J. Wong lts = (struct xfs_legacy_timestamp *)&ts;
2845a0bb066SDarrick J. Wong lts->t_sec = cpu_to_be32(tv.tv_sec);
2855a0bb066SDarrick J. Wong lts->t_nsec = cpu_to_be32(tv.tv_nsec);
2865a0bb066SDarrick J. Wong
2875a0bb066SDarrick J. Wong return ts;
2885a0bb066SDarrick J. Wong }
2895a0bb066SDarrick J. Wong
29052a4a148SChandan Babu R static inline void
xfs_inode_to_disk_iext_counters(struct xfs_inode * ip,struct xfs_dinode * to)29152a4a148SChandan Babu R xfs_inode_to_disk_iext_counters(
29252a4a148SChandan Babu R struct xfs_inode *ip,
29352a4a148SChandan Babu R struct xfs_dinode *to)
29452a4a148SChandan Babu R {
29552a4a148SChandan Babu R if (xfs_inode_has_large_extent_counts(ip)) {
29652a4a148SChandan Babu R to->di_big_nextents = cpu_to_be64(xfs_ifork_nextents(&ip->i_df));
2972ed5b09bSDarrick J. Wong to->di_big_anextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_af));
29852a4a148SChandan Babu R /*
29952a4a148SChandan Babu R * We might be upgrading the inode to use larger extent counters
30052a4a148SChandan Babu R * than was previously used. Hence zero the unused field.
30152a4a148SChandan Babu R */
30252a4a148SChandan Babu R to->di_nrext64_pad = cpu_to_be16(0);
30352a4a148SChandan Babu R } else {
30452a4a148SChandan Babu R to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
3052ed5b09bSDarrick J. Wong to->di_anextents = cpu_to_be16(xfs_ifork_nextents(&ip->i_af));
30652a4a148SChandan Babu R }
30752a4a148SChandan Babu R }
30852a4a148SChandan Babu R
30930f712c9SDave Chinner void
xfs_inode_to_disk(struct xfs_inode * ip,struct xfs_dinode * to,xfs_lsn_t lsn)3103987848cSDave Chinner xfs_inode_to_disk(
3113987848cSDave Chinner struct xfs_inode *ip,
31293f958f9SDave Chinner struct xfs_dinode *to,
31393f958f9SDave Chinner xfs_lsn_t lsn)
3143987848cSDave Chinner {
3153987848cSDave Chinner struct inode *inode = VFS_I(ip);
3163987848cSDave Chinner
31793f958f9SDave Chinner to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
318faeb4e47SDave Chinner to->di_onlink = 0;
31993f958f9SDave Chinner
320f7e67b20SChristoph Hellwig to->di_format = xfs_ifork_format(&ip->i_df);
321ba8adad5SChristoph Hellwig to->di_uid = cpu_to_be32(i_uid_read(inode));
322ba8adad5SChristoph Hellwig to->di_gid = cpu_to_be32(i_gid_read(inode));
323ceaf603cSChristoph Hellwig to->di_projid_lo = cpu_to_be16(ip->i_projid & 0xffff);
324ceaf603cSChristoph Hellwig to->di_projid_hi = cpu_to_be16(ip->i_projid >> 16);
3253987848cSDave Chinner
32675d1e312SJeff Layton to->di_atime = xfs_inode_to_disk_ts(ip, inode_get_atime(inode));
32775d1e312SJeff Layton to->di_mtime = xfs_inode_to_disk_ts(ip, inode_get_mtime(inode));
328a0a415e3SJeff Layton to->di_ctime = xfs_inode_to_disk_ts(ip, inode_get_ctime(inode));
32954d7b5c1SDave Chinner to->di_nlink = cpu_to_be32(inode->i_nlink);
3309e9a2674SDave Chinner to->di_gen = cpu_to_be32(inode->i_generation);
331c19b3b05SDave Chinner to->di_mode = cpu_to_be16(inode->i_mode);
3323987848cSDave Chinner
33313d2c10bSChristoph Hellwig to->di_size = cpu_to_be64(ip->i_disk_size);
3346e73a545SChristoph Hellwig to->di_nblocks = cpu_to_be64(ip->i_nblocks);
335031474c2SChristoph Hellwig to->di_extsize = cpu_to_be32(ip->i_extsize);
3367821ea30SChristoph Hellwig to->di_forkoff = ip->i_forkoff;
3372ed5b09bSDarrick J. Wong to->di_aformat = xfs_ifork_format(&ip->i_af);
338db07349dSChristoph Hellwig to->di_flags = cpu_to_be16(ip->i_diflags);
3393987848cSDave Chinner
340ebd9027dSDave Chinner if (xfs_has_v3inodes(ip->i_mount)) {
3416471e9c5SChristoph Hellwig to->di_version = 3;
342f0e28280SJeff Layton to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
343e98d5e88SChristoph Hellwig to->di_crtime = xfs_inode_to_disk_ts(ip, ip->i_crtime);
3443e09ab8fSChristoph Hellwig to->di_flags2 = cpu_to_be64(ip->i_diflags2);
345b33ce57dSChristoph Hellwig to->di_cowextsize = cpu_to_be32(ip->i_cowextsize);
34693f958f9SDave Chinner to->di_ino = cpu_to_be64(ip->i_ino);
34793f958f9SDave Chinner to->di_lsn = cpu_to_be64(lsn);
34893f958f9SDave Chinner memset(to->di_pad2, 0, sizeof(to->di_pad2));
34993f958f9SDave Chinner uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
35052a4a148SChandan Babu R to->di_v3_pad = 0;
3513987848cSDave Chinner } else {
3526471e9c5SChristoph Hellwig to->di_version = 2;
353965e0a1aSChristoph Hellwig to->di_flushiter = cpu_to_be16(ip->i_flushiter);
35452a4a148SChandan Babu R memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
3553987848cSDave Chinner }
35652a4a148SChandan Babu R
35752a4a148SChandan Babu R xfs_inode_to_disk_iext_counters(ip, to);
3583987848cSDave Chinner }
3593987848cSDave Chinner
36023fcb334SDave Chinner static xfs_failaddr_t
xfs_dinode_verify_fork(struct xfs_dinode * dip,struct xfs_mount * mp,int whichfork)36123fcb334SDave Chinner xfs_dinode_verify_fork(
36223fcb334SDave Chinner struct xfs_dinode *dip,
36323fcb334SDave Chinner struct xfs_mount *mp,
36423fcb334SDave Chinner int whichfork)
36523fcb334SDave Chinner {
366dd95a6ceSChandan Babu R xfs_extnum_t di_nextents;
3679feb8f19SChandan Babu R xfs_extnum_t max_extents;
3681eb70f54SDave Chinner mode_t mode = be16_to_cpu(dip->di_mode);
3691eb70f54SDave Chinner uint32_t fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
3701eb70f54SDave Chinner uint32_t fork_format = XFS_DFORK_FORMAT(dip, whichfork);
37123fcb334SDave Chinner
372dd95a6ceSChandan Babu R di_nextents = xfs_dfork_nextents(dip, whichfork);
373dd95a6ceSChandan Babu R
37423fcb334SDave Chinner /*
3751eb70f54SDave Chinner * For fork types that can contain local data, check that the fork
3761eb70f54SDave Chinner * format matches the size of local data contained within the fork.
37723fcb334SDave Chinner */
37823fcb334SDave Chinner if (whichfork == XFS_DATA_FORK) {
37938de5679SDarrick J. Wong /*
38038de5679SDarrick J. Wong * A directory small enough to fit in the inode must be stored
38138de5679SDarrick J. Wong * in local format. The directory sf <-> extents conversion
3821ec9307fSDarrick J. Wong * code updates the directory size accordingly. Directories
3831ec9307fSDarrick J. Wong * being truncated have zero size and are not subject to this
3841ec9307fSDarrick J. Wong * check.
38538de5679SDarrick J. Wong */
38638de5679SDarrick J. Wong if (S_ISDIR(mode)) {
3871ec9307fSDarrick J. Wong if (dip->di_size &&
3881ec9307fSDarrick J. Wong be64_to_cpu(dip->di_size) <= fork_size &&
3891eb70f54SDave Chinner fork_format != XFS_DINODE_FMT_LOCAL)
39023fcb334SDave Chinner return __this_address;
39123fcb334SDave Chinner }
3921eb70f54SDave Chinner
39338de5679SDarrick J. Wong /*
39438de5679SDarrick J. Wong * A symlink with a target small enough to fit in the inode can
39538de5679SDarrick J. Wong * be stored in extents format if xattrs were added (thus
39638de5679SDarrick J. Wong * converting the data fork from shortform to remote format)
39738de5679SDarrick J. Wong * and then removed.
39838de5679SDarrick J. Wong */
39938de5679SDarrick J. Wong if (S_ISLNK(mode)) {
40038de5679SDarrick J. Wong if (be64_to_cpu(dip->di_size) <= fork_size &&
40138de5679SDarrick J. Wong fork_format != XFS_DINODE_FMT_EXTENTS &&
40238de5679SDarrick J. Wong fork_format != XFS_DINODE_FMT_LOCAL)
40338de5679SDarrick J. Wong return __this_address;
40438de5679SDarrick J. Wong }
40538de5679SDarrick J. Wong
40638de5679SDarrick J. Wong /*
40738de5679SDarrick J. Wong * For all types, check that when the size says the fork should
40838de5679SDarrick J. Wong * be in extent or btree format, the inode isn't claiming to be
40938de5679SDarrick J. Wong * in local format.
41038de5679SDarrick J. Wong */
4111eb70f54SDave Chinner if (be64_to_cpu(dip->di_size) > fork_size &&
4121eb70f54SDave Chinner fork_format == XFS_DINODE_FMT_LOCAL)
4131eb70f54SDave Chinner return __this_address;
4141eb70f54SDave Chinner }
4151eb70f54SDave Chinner
4161eb70f54SDave Chinner switch (fork_format) {
4171eb70f54SDave Chinner case XFS_DINODE_FMT_LOCAL:
4181eb70f54SDave Chinner /*
4191eb70f54SDave Chinner * No local regular files yet.
4201eb70f54SDave Chinner */
4211eb70f54SDave Chinner if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
4221eb70f54SDave Chinner return __this_address;
42323fcb334SDave Chinner if (di_nextents)
42423fcb334SDave Chinner return __this_address;
42523fcb334SDave Chinner break;
42623fcb334SDave Chinner case XFS_DINODE_FMT_EXTENTS:
42723fcb334SDave Chinner if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
42823fcb334SDave Chinner return __this_address;
42923fcb334SDave Chinner break;
43023fcb334SDave Chinner case XFS_DINODE_FMT_BTREE:
431df9ad5ccSChandan Babu R max_extents = xfs_iext_max_nextents(
432df9ad5ccSChandan Babu R xfs_dinode_has_large_extent_counts(dip),
433df9ad5ccSChandan Babu R whichfork);
4349feb8f19SChandan Babu R if (di_nextents > max_extents)
43523fcb334SDave Chinner return __this_address;
43623fcb334SDave Chinner break;
43723fcb334SDave Chinner default:
43823fcb334SDave Chinner return __this_address;
43923fcb334SDave Chinner }
44023fcb334SDave Chinner return NULL;
44123fcb334SDave Chinner }
44223fcb334SDave Chinner
443339e1a3fSEric Sandeen static xfs_failaddr_t
xfs_dinode_verify_forkoff(struct xfs_dinode * dip,struct xfs_mount * mp)444339e1a3fSEric Sandeen xfs_dinode_verify_forkoff(
445339e1a3fSEric Sandeen struct xfs_dinode *dip,
446339e1a3fSEric Sandeen struct xfs_mount *mp)
447339e1a3fSEric Sandeen {
44809c38eddSChristoph Hellwig if (!dip->di_forkoff)
449339e1a3fSEric Sandeen return NULL;
450339e1a3fSEric Sandeen
451339e1a3fSEric Sandeen switch (dip->di_format) {
452339e1a3fSEric Sandeen case XFS_DINODE_FMT_DEV:
453339e1a3fSEric Sandeen if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
454339e1a3fSEric Sandeen return __this_address;
455339e1a3fSEric Sandeen break;
456339e1a3fSEric Sandeen case XFS_DINODE_FMT_LOCAL: /* fall through ... */
457339e1a3fSEric Sandeen case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
458339e1a3fSEric Sandeen case XFS_DINODE_FMT_BTREE:
459e9e2eae8SChristoph Hellwig if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
460339e1a3fSEric Sandeen return __this_address;
461339e1a3fSEric Sandeen break;
462339e1a3fSEric Sandeen default:
463339e1a3fSEric Sandeen return __this_address;
464339e1a3fSEric Sandeen }
465339e1a3fSEric Sandeen return NULL;
466339e1a3fSEric Sandeen }
467339e1a3fSEric Sandeen
46852a4a148SChandan Babu R static xfs_failaddr_t
xfs_dinode_verify_nrext64(struct xfs_mount * mp,struct xfs_dinode * dip)46952a4a148SChandan Babu R xfs_dinode_verify_nrext64(
47052a4a148SChandan Babu R struct xfs_mount *mp,
47152a4a148SChandan Babu R struct xfs_dinode *dip)
47252a4a148SChandan Babu R {
47352a4a148SChandan Babu R if (xfs_dinode_has_large_extent_counts(dip)) {
47452a4a148SChandan Babu R if (!xfs_has_large_extent_counts(mp))
47552a4a148SChandan Babu R return __this_address;
47652a4a148SChandan Babu R if (dip->di_nrext64_pad != 0)
47752a4a148SChandan Babu R return __this_address;
47852a4a148SChandan Babu R } else if (dip->di_version >= 3) {
47952a4a148SChandan Babu R if (dip->di_v3_pad != 0)
48052a4a148SChandan Babu R return __this_address;
48152a4a148SChandan Babu R }
48252a4a148SChandan Babu R
48352a4a148SChandan Babu R return NULL;
48452a4a148SChandan Babu R }
48552a4a148SChandan Babu R
486a6a781a5SDarrick J. Wong xfs_failaddr_t
xfs_dinode_verify(struct xfs_mount * mp,xfs_ino_t ino,struct xfs_dinode * dip)48730f712c9SDave Chinner xfs_dinode_verify(
48830f712c9SDave Chinner struct xfs_mount *mp,
489420fbeb4SDarrick J. Wong xfs_ino_t ino,
49030f712c9SDave Chinner struct xfs_dinode *dip)
49130f712c9SDave Chinner {
4927d71a671SDave Chinner xfs_failaddr_t fa;
4933c6f46eaSAmir Goldstein uint16_t mode;
494c8e156acSDarrick J. Wong uint16_t flags;
495c8e156acSDarrick J. Wong uint64_t flags2;
49671493b83SDarrick J. Wong uint64_t di_size;
497dd95a6ceSChandan Babu R xfs_extnum_t nextents;
498dd95a6ceSChandan Babu R xfs_extnum_t naextents;
499dd95a6ceSChandan Babu R xfs_filblks_t nblocks;
500c8e156acSDarrick J. Wong
50130f712c9SDave Chinner if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
502a6a781a5SDarrick J. Wong return __this_address;
50330f712c9SDave Chinner
50450aa90efSDarrick J. Wong /* Verify v3 integrity information first */
50550aa90efSDarrick J. Wong if (dip->di_version >= 3) {
506ebd9027dSDave Chinner if (!xfs_has_v3inodes(mp))
50750aa90efSDarrick J. Wong return __this_address;
50850aa90efSDarrick J. Wong if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
50950aa90efSDarrick J. Wong XFS_DINODE_CRC_OFF))
51050aa90efSDarrick J. Wong return __this_address;
51150aa90efSDarrick J. Wong if (be64_to_cpu(dip->di_ino) != ino)
51250aa90efSDarrick J. Wong return __this_address;
51350aa90efSDarrick J. Wong if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
51450aa90efSDarrick J. Wong return __this_address;
51550aa90efSDarrick J. Wong }
51630f712c9SDave Chinner
517*e21fea4aSDarrick J. Wong /*
518*e21fea4aSDarrick J. Wong * Historical note: xfsprogs in the 3.2 era set up its incore inodes to
519*e21fea4aSDarrick J. Wong * have di_nlink track the link count, even if the actual filesystem
520*e21fea4aSDarrick J. Wong * only supported V1 inodes (i.e. di_onlink). When writing out the
521*e21fea4aSDarrick J. Wong * ondisk inode, it would set both the ondisk di_nlink and di_onlink to
522*e21fea4aSDarrick J. Wong * the the incore di_nlink value, which is why we cannot check for
523*e21fea4aSDarrick J. Wong * di_nlink==0 on a V1 inode. V2/3 inodes would get written out with
524*e21fea4aSDarrick J. Wong * di_onlink==0, so we can check that.
525*e21fea4aSDarrick J. Wong */
526*e21fea4aSDarrick J. Wong if (dip->di_version >= 2) {
52740cb8613SDarrick J. Wong if (dip->di_onlink)
52840cb8613SDarrick J. Wong return __this_address;
52940cb8613SDarrick J. Wong }
53040cb8613SDarrick J. Wong
531ef388e20SDarrick J. Wong /* don't allow invalid i_size */
53271493b83SDarrick J. Wong di_size = be64_to_cpu(dip->di_size);
53371493b83SDarrick J. Wong if (di_size & (1ULL << 63))
534a6a781a5SDarrick J. Wong return __this_address;
535ef388e20SDarrick J. Wong
5363c6f46eaSAmir Goldstein mode = be16_to_cpu(dip->di_mode);
537a324cbf1SAmir Goldstein if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
538a6a781a5SDarrick J. Wong return __this_address;
5393c6f46eaSAmir Goldstein
5401ec9307fSDarrick J. Wong /*
5411ec9307fSDarrick J. Wong * No zero-length symlinks/dirs unless they're unlinked and hence being
5421ec9307fSDarrick J. Wong * inactivated.
5431ec9307fSDarrick J. Wong */
5441ec9307fSDarrick J. Wong if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0) {
5451ec9307fSDarrick J. Wong if (dip->di_version > 1) {
5461ec9307fSDarrick J. Wong if (dip->di_nlink)
547a6a781a5SDarrick J. Wong return __this_address;
5481ec9307fSDarrick J. Wong } else {
5491ec9307fSDarrick J. Wong if (dip->di_onlink)
5501ec9307fSDarrick J. Wong return __this_address;
5511ec9307fSDarrick J. Wong }
5521ec9307fSDarrick J. Wong }
553ef388e20SDarrick J. Wong
55452a4a148SChandan Babu R fa = xfs_dinode_verify_nrext64(mp, dip);
55552a4a148SChandan Babu R if (fa)
55652a4a148SChandan Babu R return fa;
55752a4a148SChandan Babu R
558dd95a6ceSChandan Babu R nextents = xfs_dfork_data_extents(dip);
559dd95a6ceSChandan Babu R naextents = xfs_dfork_attr_extents(dip);
560dd95a6ceSChandan Babu R nblocks = be64_to_cpu(dip->di_nblocks);
561dd95a6ceSChandan Babu R
56271493b83SDarrick J. Wong /* Fork checks carried over from xfs_iformat_fork */
563dd95a6ceSChandan Babu R if (mode && nextents + naextents > nblocks)
56471493b83SDarrick J. Wong return __this_address;
56571493b83SDarrick J. Wong
566038ca189SDave Chinner if (nextents + naextents == 0 && nblocks != 0)
567038ca189SDave Chinner return __this_address;
568038ca189SDave Chinner
56983a21c18SChandan Babu R if (S_ISDIR(mode) && nextents > mp->m_dir_geo->max_extents)
57083a21c18SChandan Babu R return __this_address;
57183a21c18SChandan Babu R
57271493b83SDarrick J. Wong if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
57371493b83SDarrick J. Wong return __this_address;
57471493b83SDarrick J. Wong
57571493b83SDarrick J. Wong flags = be16_to_cpu(dip->di_flags);
57671493b83SDarrick J. Wong
57771493b83SDarrick J. Wong if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
57871493b83SDarrick J. Wong return __this_address;
57971493b83SDarrick J. Wong
580339e1a3fSEric Sandeen /* check for illegal values of forkoff */
581339e1a3fSEric Sandeen fa = xfs_dinode_verify_forkoff(dip, mp);
582339e1a3fSEric Sandeen if (fa)
583339e1a3fSEric Sandeen return fa;
584339e1a3fSEric Sandeen
58571493b83SDarrick J. Wong /* Do we have appropriate data fork formats for the mode? */
58671493b83SDarrick J. Wong switch (mode & S_IFMT) {
58771493b83SDarrick J. Wong case S_IFIFO:
58871493b83SDarrick J. Wong case S_IFCHR:
58971493b83SDarrick J. Wong case S_IFBLK:
59071493b83SDarrick J. Wong case S_IFSOCK:
59171493b83SDarrick J. Wong if (dip->di_format != XFS_DINODE_FMT_DEV)
59271493b83SDarrick J. Wong return __this_address;
59371493b83SDarrick J. Wong break;
59471493b83SDarrick J. Wong case S_IFREG:
59571493b83SDarrick J. Wong case S_IFLNK:
59671493b83SDarrick J. Wong case S_IFDIR:
59723fcb334SDave Chinner fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
59823fcb334SDave Chinner if (fa)
59923fcb334SDave Chinner return fa;
60071493b83SDarrick J. Wong break;
60171493b83SDarrick J. Wong case 0:
60271493b83SDarrick J. Wong /* Uninitialized inode ok. */
60371493b83SDarrick J. Wong break;
60471493b83SDarrick J. Wong default:
60571493b83SDarrick J. Wong return __this_address;
60671493b83SDarrick J. Wong }
60771493b83SDarrick J. Wong
60809c38eddSChristoph Hellwig if (dip->di_forkoff) {
60923fcb334SDave Chinner fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
61023fcb334SDave Chinner if (fa)
61123fcb334SDave Chinner return fa;
612b42db086SEric Sandeen } else {
613b42db086SEric Sandeen /*
614b42db086SEric Sandeen * If there is no fork offset, this may be a freshly-made inode
615b42db086SEric Sandeen * in a new disk cluster, in which case di_aformat is zeroed.
616b42db086SEric Sandeen * Otherwise, such an inode must be in EXTENTS format; this goes
617b42db086SEric Sandeen * for freed inodes as well.
618b42db086SEric Sandeen */
619b42db086SEric Sandeen switch (dip->di_aformat) {
620b42db086SEric Sandeen case 0:
621b42db086SEric Sandeen case XFS_DINODE_FMT_EXTENTS:
622b42db086SEric Sandeen break;
623b42db086SEric Sandeen default:
624b42db086SEric Sandeen return __this_address;
625b42db086SEric Sandeen }
626dd95a6ceSChandan Babu R if (naextents)
627b42db086SEric Sandeen return __this_address;
62871493b83SDarrick J. Wong }
62930f712c9SDave Chinner
6307d71a671SDave Chinner /* extent size hint validation */
6317d71a671SDave Chinner fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
6327d71a671SDave Chinner mode, flags);
6337d71a671SDave Chinner if (fa)
6347d71a671SDave Chinner return fa;
6357d71a671SDave Chinner
63630f712c9SDave Chinner /* only version 3 or greater inodes are extensively verified here */
63730f712c9SDave Chinner if (dip->di_version < 3)
638a6a781a5SDarrick J. Wong return NULL;
63930f712c9SDave Chinner
640c8e156acSDarrick J. Wong flags2 = be64_to_cpu(dip->di_flags2);
641c8e156acSDarrick J. Wong
642c8e156acSDarrick J. Wong /* don't allow reflink/cowextsize if we don't have reflink */
643c8e156acSDarrick J. Wong if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
64438c26bfdSDave Chinner !xfs_has_reflink(mp))
645a6a781a5SDarrick J. Wong return __this_address;
646c8e156acSDarrick J. Wong
64771493b83SDarrick J. Wong /* only regular files get reflink */
64871493b83SDarrick J. Wong if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
64971493b83SDarrick J. Wong return __this_address;
650c8e156acSDarrick J. Wong
651c8e156acSDarrick J. Wong /* don't let reflink and realtime mix */
652c8e156acSDarrick J. Wong if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
653a6a781a5SDarrick J. Wong return __this_address;
654c8e156acSDarrick J. Wong
65502a0fda8SDave Chinner /* COW extent size hint validation */
65602a0fda8SDave Chinner fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
65702a0fda8SDave Chinner mode, flags, flags2);
65802a0fda8SDave Chinner if (fa)
65902a0fda8SDave Chinner return fa;
66002a0fda8SDave Chinner
661f93e5436SDarrick J. Wong /* bigtime iflag can only happen on bigtime filesystems */
662f93e5436SDarrick J. Wong if (xfs_dinode_has_bigtime(dip) &&
663ebd9027dSDave Chinner !xfs_has_bigtime(mp))
664f93e5436SDarrick J. Wong return __this_address;
665f93e5436SDarrick J. Wong
666a6a781a5SDarrick J. Wong return NULL;
66730f712c9SDave Chinner }
66830f712c9SDave Chinner
66930f712c9SDave Chinner void
xfs_dinode_calc_crc(struct xfs_mount * mp,struct xfs_dinode * dip)67030f712c9SDave Chinner xfs_dinode_calc_crc(
67130f712c9SDave Chinner struct xfs_mount *mp,
67230f712c9SDave Chinner struct xfs_dinode *dip)
67330f712c9SDave Chinner {
674c8ce540dSDarrick J. Wong uint32_t crc;
67530f712c9SDave Chinner
67630f712c9SDave Chinner if (dip->di_version < 3)
67730f712c9SDave Chinner return;
67830f712c9SDave Chinner
67938c26bfdSDave Chinner ASSERT(xfs_has_crc(mp));
680cae028dfSDave Chinner crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
68130f712c9SDave Chinner XFS_DINODE_CRC_OFF);
68230f712c9SDave Chinner dip->di_crc = xfs_end_cksum(crc);
68330f712c9SDave Chinner }
68430f712c9SDave Chinner
68530f712c9SDave Chinner /*
6868bb82bc1SDarrick J. Wong * Validate di_extsize hint.
6878bb82bc1SDarrick J. Wong *
6886b69e485SDarrick J. Wong * 1. Extent size hint is only valid for directories and regular files.
6896b69e485SDarrick J. Wong * 2. FS_XFLAG_EXTSIZE is only valid for regular files.
6906b69e485SDarrick J. Wong * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
6916b69e485SDarrick J. Wong * 4. Hint cannot be larger than MAXTEXTLEN.
6926b69e485SDarrick J. Wong * 5. Can be changed on directories at any time.
6936b69e485SDarrick J. Wong * 6. Hint value of 0 turns off hints, clears inode flags.
6946b69e485SDarrick J. Wong * 7. Extent size must be a multiple of the appropriate block size.
6956b69e485SDarrick J. Wong * For realtime files, this is the rt extent size.
6966b69e485SDarrick J. Wong * 8. For non-realtime files, the extent size hint must be limited
6976b69e485SDarrick J. Wong * to half the AG size to avoid alignment extending the extent beyond the
6986b69e485SDarrick J. Wong * limits of the AG.
6998bb82bc1SDarrick J. Wong */
7008bb82bc1SDarrick J. Wong xfs_failaddr_t
xfs_inode_validate_extsize(struct xfs_mount * mp,uint32_t extsize,uint16_t mode,uint16_t flags)7018bb82bc1SDarrick J. Wong xfs_inode_validate_extsize(
7028bb82bc1SDarrick J. Wong struct xfs_mount *mp,
7038bb82bc1SDarrick J. Wong uint32_t extsize,
7048bb82bc1SDarrick J. Wong uint16_t mode,
7058bb82bc1SDarrick J. Wong uint16_t flags)
7068bb82bc1SDarrick J. Wong {
7078bb82bc1SDarrick J. Wong bool rt_flag;
7088bb82bc1SDarrick J. Wong bool hint_flag;
7098bb82bc1SDarrick J. Wong bool inherit_flag;
7108bb82bc1SDarrick J. Wong uint32_t extsize_bytes;
7118bb82bc1SDarrick J. Wong uint32_t blocksize_bytes;
7128bb82bc1SDarrick J. Wong
7138bb82bc1SDarrick J. Wong rt_flag = (flags & XFS_DIFLAG_REALTIME);
7148bb82bc1SDarrick J. Wong hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
7158bb82bc1SDarrick J. Wong inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
7168bb82bc1SDarrick J. Wong extsize_bytes = XFS_FSB_TO_B(mp, extsize);
7178bb82bc1SDarrick J. Wong
718603f000bSDarrick J. Wong /*
719603f000bSDarrick J. Wong * This comment describes a historic gap in this verifier function.
720603f000bSDarrick J. Wong *
72183193e5eSDarrick J. Wong * For a directory with both RTINHERIT and EXTSZINHERIT flags set, this
72283193e5eSDarrick J. Wong * function has never checked that the extent size hint is an integer
72383193e5eSDarrick J. Wong * multiple of the realtime extent size. Since we allow users to set
72483193e5eSDarrick J. Wong * this combination on non-rt filesystems /and/ to change the rt
72583193e5eSDarrick J. Wong * extent size when adding a rt device to a filesystem, the net effect
72683193e5eSDarrick J. Wong * is that users can configure a filesystem anticipating one rt
72783193e5eSDarrick J. Wong * geometry and change their minds later. Directories do not use the
72883193e5eSDarrick J. Wong * extent size hint, so this is harmless for them.
729603f000bSDarrick J. Wong *
730603f000bSDarrick J. Wong * If a directory with a misaligned extent size hint is allowed to
731603f000bSDarrick J. Wong * propagate that hint into a new regular realtime file, the result
732603f000bSDarrick J. Wong * is that the inode cluster buffer verifier will trigger a corruption
73383193e5eSDarrick J. Wong * shutdown the next time it is run, because the verifier has always
73483193e5eSDarrick J. Wong * enforced the alignment rule for regular files.
735603f000bSDarrick J. Wong *
73683193e5eSDarrick J. Wong * Because we allow administrators to set a new rt extent size when
73783193e5eSDarrick J. Wong * adding a rt section, we cannot add a check to this verifier because
73883193e5eSDarrick J. Wong * that will result a new source of directory corruption errors when
73983193e5eSDarrick J. Wong * reading an existing filesystem. Instead, we rely on callers to
74083193e5eSDarrick J. Wong * decide when alignment checks are appropriate, and fix things up as
74183193e5eSDarrick J. Wong * needed.
742603f000bSDarrick J. Wong */
743603f000bSDarrick J. Wong
7448bb82bc1SDarrick J. Wong if (rt_flag)
745a7bcb147SDarrick J. Wong blocksize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
7468bb82bc1SDarrick J. Wong else
7478bb82bc1SDarrick J. Wong blocksize_bytes = mp->m_sb.sb_blocksize;
7488bb82bc1SDarrick J. Wong
7498bb82bc1SDarrick J. Wong if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
7508bb82bc1SDarrick J. Wong return __this_address;
7518bb82bc1SDarrick J. Wong
7528bb82bc1SDarrick J. Wong if (hint_flag && !S_ISREG(mode))
7538bb82bc1SDarrick J. Wong return __this_address;
7548bb82bc1SDarrick J. Wong
7558bb82bc1SDarrick J. Wong if (inherit_flag && !S_ISDIR(mode))
7568bb82bc1SDarrick J. Wong return __this_address;
7578bb82bc1SDarrick J. Wong
7588bb82bc1SDarrick J. Wong if ((hint_flag || inherit_flag) && extsize == 0)
7598bb82bc1SDarrick J. Wong return __this_address;
7608bb82bc1SDarrick J. Wong
761d4a34e16SEric Sandeen /* free inodes get flags set to zero but extsize remains */
762d4a34e16SEric Sandeen if (mode && !(hint_flag || inherit_flag) && extsize != 0)
7638bb82bc1SDarrick J. Wong return __this_address;
7648bb82bc1SDarrick J. Wong
7658bb82bc1SDarrick J. Wong if (extsize_bytes % blocksize_bytes)
7668bb82bc1SDarrick J. Wong return __this_address;
7678bb82bc1SDarrick J. Wong
76895f0b95eSChandan Babu R if (extsize > XFS_MAX_BMBT_EXTLEN)
7698bb82bc1SDarrick J. Wong return __this_address;
7708bb82bc1SDarrick J. Wong
7718bb82bc1SDarrick J. Wong if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
7728bb82bc1SDarrick J. Wong return __this_address;
7738bb82bc1SDarrick J. Wong
7748bb82bc1SDarrick J. Wong return NULL;
7758bb82bc1SDarrick J. Wong }
7768bb82bc1SDarrick J. Wong
7778bb82bc1SDarrick J. Wong /*
7788bb82bc1SDarrick J. Wong * Validate di_cowextsize hint.
7798bb82bc1SDarrick J. Wong *
7806b69e485SDarrick J. Wong * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
7816b69e485SDarrick J. Wong * The inode does not have to have any shared blocks, but it must be a v3.
7826b69e485SDarrick J. Wong * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
7836b69e485SDarrick J. Wong * for a directory, the hint is propagated to new files.
7846b69e485SDarrick J. Wong * 3. Can be changed on files & directories at any time.
7856b69e485SDarrick J. Wong * 4. Hint value of 0 turns off hints, clears inode flags.
7866b69e485SDarrick J. Wong * 5. Extent size must be a multiple of the appropriate block size.
7876b69e485SDarrick J. Wong * 6. The extent size hint must be limited to half the AG size to avoid
7886b69e485SDarrick J. Wong * alignment extending the extent beyond the limits of the AG.
7898bb82bc1SDarrick J. Wong */
7908bb82bc1SDarrick J. Wong xfs_failaddr_t
xfs_inode_validate_cowextsize(struct xfs_mount * mp,uint32_t cowextsize,uint16_t mode,uint16_t flags,uint64_t flags2)7918bb82bc1SDarrick J. Wong xfs_inode_validate_cowextsize(
7928bb82bc1SDarrick J. Wong struct xfs_mount *mp,
7938bb82bc1SDarrick J. Wong uint32_t cowextsize,
7948bb82bc1SDarrick J. Wong uint16_t mode,
7958bb82bc1SDarrick J. Wong uint16_t flags,
7968bb82bc1SDarrick J. Wong uint64_t flags2)
7978bb82bc1SDarrick J. Wong {
7988bb82bc1SDarrick J. Wong bool rt_flag;
7998bb82bc1SDarrick J. Wong bool hint_flag;
8008bb82bc1SDarrick J. Wong uint32_t cowextsize_bytes;
8018bb82bc1SDarrick J. Wong
8028bb82bc1SDarrick J. Wong rt_flag = (flags & XFS_DIFLAG_REALTIME);
8038bb82bc1SDarrick J. Wong hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
8048bb82bc1SDarrick J. Wong cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
8058bb82bc1SDarrick J. Wong
80638c26bfdSDave Chinner if (hint_flag && !xfs_has_reflink(mp))
8078bb82bc1SDarrick J. Wong return __this_address;
8088bb82bc1SDarrick J. Wong
8098bb82bc1SDarrick J. Wong if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
8108bb82bc1SDarrick J. Wong return __this_address;
8118bb82bc1SDarrick J. Wong
8128bb82bc1SDarrick J. Wong if (hint_flag && cowextsize == 0)
8138bb82bc1SDarrick J. Wong return __this_address;
8148bb82bc1SDarrick J. Wong
815d4a34e16SEric Sandeen /* free inodes get flags set to zero but cowextsize remains */
816d4a34e16SEric Sandeen if (mode && !hint_flag && cowextsize != 0)
8178bb82bc1SDarrick J. Wong return __this_address;
8188bb82bc1SDarrick J. Wong
8198bb82bc1SDarrick J. Wong if (hint_flag && rt_flag)
8208bb82bc1SDarrick J. Wong return __this_address;
8218bb82bc1SDarrick J. Wong
8228bb82bc1SDarrick J. Wong if (cowextsize_bytes % mp->m_sb.sb_blocksize)
8238bb82bc1SDarrick J. Wong return __this_address;
8248bb82bc1SDarrick J. Wong
82595f0b95eSChandan Babu R if (cowextsize > XFS_MAX_BMBT_EXTLEN)
8268bb82bc1SDarrick J. Wong return __this_address;
8278bb82bc1SDarrick J. Wong
8288bb82bc1SDarrick J. Wong if (cowextsize > mp->m_sb.sb_agblocks / 2)
8298bb82bc1SDarrick J. Wong return __this_address;
8308bb82bc1SDarrick J. Wong
8318bb82bc1SDarrick J. Wong return NULL;
8328bb82bc1SDarrick J. Wong }
833