1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_errortag.h"
15 #include "xfs_error.h"
16 #include "xfs_icache.h"
17 #include "xfs_trans.h"
18 #include "xfs_ialloc.h"
21 #include <linux/iversion.h>
24 * If we are doing readahead on an inode buffer, we might be in log recovery
25 * reading an inode allocation buffer that hasn't yet been replayed, and hence
26 * has not had the inode cores stamped into it. Hence for readahead, the buffer
27 * may be potentially invalid.
29 * If the readahead buffer is invalid, we need to mark it with an error and
30 * clear the DONE status of the buffer so that a followup read will re-read it
31 * from disk. We don't report the error otherwise to avoid warnings during log
32 * recovery and we don't get unnecessary panics on debug kernels. We use EIO here
33 * because all we want to do is say readahead failed; there is no-one to report
34 * the error to, so this will distinguish it from a non-ra verifier failure.
35 * Changes to this readahead error behaviour also need to be reflected in
36 * xfs_dquot_buf_readahead_verify().
43 struct xfs_mount *mp = bp->b_mount;
49 * Validate the magic number and version of every inode in the buffer
51 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
52 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
53 for (i = 0; i < ni; i++) {
56 xfs_agino_t unlinked_ino;
58 dip = xfs_buf_offset(bp, (i << mp->m_sb.sb_inodelog));
59 unlinked_ino = be32_to_cpu(dip->di_next_unlinked);
60 di_ok = xfs_verify_magic16(bp, dip->di_magic) &&
61 xfs_dinode_good_version(&mp->m_sb, dip->di_version) &&
62 xfs_verify_agino_or_null(mp, agno, unlinked_ino);
63 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
64 XFS_ERRTAG_ITOBP_INOTOBP))) {
66 bp->b_flags &= ~XBF_DONE;
67 xfs_buf_ioerror(bp, -EIO);
73 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
74 (unsigned long long)bp->b_bn, i,
75 be16_to_cpu(dip->di_magic));
77 xfs_buf_verifier_error(bp, -EFSCORRUPTED,
78 __func__, dip, sizeof(*dip),
87 xfs_inode_buf_read_verify(
90 xfs_inode_buf_verify(bp, false);
94 xfs_inode_buf_readahead_verify(
97 xfs_inode_buf_verify(bp, true);
101 xfs_inode_buf_write_verify(
104 xfs_inode_buf_verify(bp, false);
107 const struct xfs_buf_ops xfs_inode_buf_ops = {
109 .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
110 cpu_to_be16(XFS_DINODE_MAGIC) },
111 .verify_read = xfs_inode_buf_read_verify,
112 .verify_write = xfs_inode_buf_write_verify,
115 const struct xfs_buf_ops xfs_inode_buf_ra_ops = {
116 .name = "xfs_inode_ra",
117 .magic16 = { cpu_to_be16(XFS_DINODE_MAGIC),
118 cpu_to_be16(XFS_DINODE_MAGIC) },
119 .verify_read = xfs_inode_buf_readahead_verify,
120 .verify_write = xfs_inode_buf_write_verify,
125 * This routine is called to map an inode to the buffer containing the on-disk
126 * version of the inode. It returns a pointer to the buffer containing the
127 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
128 * pointer to the on-disk inode within that buffer.
130 * If a non-zero error is returned, then the contents of bpp and dipp are
135 struct xfs_mount *mp,
136 struct xfs_trans *tp,
137 struct xfs_imap *imap,
138 struct xfs_dinode **dipp,
139 struct xfs_buf **bpp,
145 buf_flags |= XBF_UNMAPPED;
146 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
147 (int)imap->im_len, buf_flags, &bp,
150 ASSERT(error != -EAGAIN || (buf_flags & XBF_TRYLOCK));
156 *dipp = xfs_buf_offset(bp, imap->im_boffset);
160 static inline struct timespec64 xfs_inode_decode_bigtime(uint64_t ts)
162 struct timespec64 tv;
165 tv.tv_sec = xfs_bigtime_to_unix(div_u64_rem(ts, NSEC_PER_SEC, &n));
171 /* Convert an ondisk timestamp to an incore timestamp. */
173 xfs_inode_from_disk_ts(
174 struct xfs_dinode *dip,
175 const xfs_timestamp_t ts)
177 struct timespec64 tv;
178 struct xfs_legacy_timestamp *lts;
180 if (xfs_dinode_has_bigtime(dip))
181 return xfs_inode_decode_bigtime(be64_to_cpu(ts));
183 lts = (struct xfs_legacy_timestamp *)&ts;
184 tv.tv_sec = (int)be32_to_cpu(lts->t_sec);
185 tv.tv_nsec = (int)be32_to_cpu(lts->t_nsec);
192 struct xfs_inode *ip,
193 struct xfs_dinode *from)
195 struct xfs_icdinode *to = &ip->i_d;
196 struct inode *inode = VFS_I(ip);
200 ASSERT(ip->i_cowfp == NULL);
201 ASSERT(ip->i_afp == NULL);
203 fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
205 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "dinode", from,
207 return -EFSCORRUPTED;
211 * First get the permanent information that is needed to allocate an
212 * inode. If the inode is unused, mode is zero and we shouldn't mess
213 * with the uninitialized part of it.
215 to->di_flushiter = be16_to_cpu(from->di_flushiter);
216 inode->i_generation = be32_to_cpu(from->di_gen);
217 inode->i_mode = be16_to_cpu(from->di_mode);
222 * Convert v1 inodes immediately to v2 inode format as this is the
223 * minimum inode version format we support in the rest of the code.
224 * They will also be unconditionally written back to disk as v2 inodes.
226 if (unlikely(from->di_version == 1)) {
227 set_nlink(inode, be16_to_cpu(from->di_onlink));
230 set_nlink(inode, be32_to_cpu(from->di_nlink));
231 to->di_projid = (prid_t)be16_to_cpu(from->di_projid_hi) << 16 |
232 be16_to_cpu(from->di_projid_lo);
235 i_uid_write(inode, be32_to_cpu(from->di_uid));
236 i_gid_write(inode, be32_to_cpu(from->di_gid));
239 * Time is signed, so need to convert to signed 32 bit before
240 * storing in inode timestamp which may be 64 bit. Otherwise
241 * a time before epoch is converted to a time long after epoch
244 inode->i_atime = xfs_inode_from_disk_ts(from, from->di_atime);
245 inode->i_mtime = xfs_inode_from_disk_ts(from, from->di_mtime);
246 inode->i_ctime = xfs_inode_from_disk_ts(from, from->di_ctime);
248 to->di_size = be64_to_cpu(from->di_size);
249 to->di_nblocks = be64_to_cpu(from->di_nblocks);
250 to->di_extsize = be32_to_cpu(from->di_extsize);
251 to->di_forkoff = from->di_forkoff;
252 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
253 to->di_dmstate = be16_to_cpu(from->di_dmstate);
254 to->di_flags = be16_to_cpu(from->di_flags);
256 if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
257 inode_set_iversion_queried(inode,
258 be64_to_cpu(from->di_changecount));
259 to->di_crtime = xfs_inode_from_disk_ts(from, from->di_crtime);
260 to->di_flags2 = be64_to_cpu(from->di_flags2);
261 to->di_cowextsize = be32_to_cpu(from->di_cowextsize);
264 error = xfs_iformat_data_fork(ip, from);
267 if (from->di_forkoff) {
268 error = xfs_iformat_attr_fork(ip, from);
270 goto out_destroy_data_fork;
272 if (xfs_is_reflink_inode(ip))
273 xfs_ifork_init_cow(ip);
276 out_destroy_data_fork:
277 xfs_idestroy_fork(&ip->i_df);
281 /* Convert an incore timestamp to an ondisk timestamp. */
282 static inline xfs_timestamp_t
283 xfs_inode_to_disk_ts(
284 struct xfs_inode *ip,
285 const struct timespec64 tv)
287 struct xfs_legacy_timestamp *lts;
290 if (xfs_inode_has_bigtime(ip))
291 return cpu_to_be64(xfs_inode_encode_bigtime(tv));
293 lts = (struct xfs_legacy_timestamp *)&ts;
294 lts->t_sec = cpu_to_be32(tv.tv_sec);
295 lts->t_nsec = cpu_to_be32(tv.tv_nsec);
302 struct xfs_inode *ip,
303 struct xfs_dinode *to,
306 struct xfs_icdinode *from = &ip->i_d;
307 struct inode *inode = VFS_I(ip);
309 to->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
312 to->di_format = xfs_ifork_format(&ip->i_df);
313 to->di_uid = cpu_to_be32(i_uid_read(inode));
314 to->di_gid = cpu_to_be32(i_gid_read(inode));
315 to->di_projid_lo = cpu_to_be16(from->di_projid & 0xffff);
316 to->di_projid_hi = cpu_to_be16(from->di_projid >> 16);
318 memset(to->di_pad, 0, sizeof(to->di_pad));
319 to->di_atime = xfs_inode_to_disk_ts(ip, inode->i_atime);
320 to->di_mtime = xfs_inode_to_disk_ts(ip, inode->i_mtime);
321 to->di_ctime = xfs_inode_to_disk_ts(ip, inode->i_ctime);
322 to->di_nlink = cpu_to_be32(inode->i_nlink);
323 to->di_gen = cpu_to_be32(inode->i_generation);
324 to->di_mode = cpu_to_be16(inode->i_mode);
326 to->di_size = cpu_to_be64(from->di_size);
327 to->di_nblocks = cpu_to_be64(from->di_nblocks);
328 to->di_extsize = cpu_to_be32(from->di_extsize);
329 to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
330 to->di_anextents = cpu_to_be16(xfs_ifork_nextents(ip->i_afp));
331 to->di_forkoff = from->di_forkoff;
332 to->di_aformat = xfs_ifork_format(ip->i_afp);
333 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
334 to->di_dmstate = cpu_to_be16(from->di_dmstate);
335 to->di_flags = cpu_to_be16(from->di_flags);
337 if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
339 to->di_changecount = cpu_to_be64(inode_peek_iversion(inode));
340 to->di_crtime = xfs_inode_to_disk_ts(ip, from->di_crtime);
341 to->di_flags2 = cpu_to_be64(from->di_flags2);
342 to->di_cowextsize = cpu_to_be32(from->di_cowextsize);
343 to->di_ino = cpu_to_be64(ip->i_ino);
344 to->di_lsn = cpu_to_be64(lsn);
345 memset(to->di_pad2, 0, sizeof(to->di_pad2));
346 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
347 to->di_flushiter = 0;
350 to->di_flushiter = cpu_to_be16(from->di_flushiter);
354 static xfs_failaddr_t
355 xfs_dinode_verify_fork(
356 struct xfs_dinode *dip,
357 struct xfs_mount *mp,
360 uint32_t di_nextents = XFS_DFORK_NEXTENTS(dip, whichfork);
361 mode_t mode = be16_to_cpu(dip->di_mode);
362 uint32_t fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
363 uint32_t fork_format = XFS_DFORK_FORMAT(dip, whichfork);
366 * For fork types that can contain local data, check that the fork
367 * format matches the size of local data contained within the fork.
369 * For all types, check that when the size says the should be in extent
370 * or btree format, the inode isn't claiming it is in local format.
372 if (whichfork == XFS_DATA_FORK) {
373 if (S_ISDIR(mode) || S_ISLNK(mode)) {
374 if (be64_to_cpu(dip->di_size) <= fork_size &&
375 fork_format != XFS_DINODE_FMT_LOCAL)
376 return __this_address;
379 if (be64_to_cpu(dip->di_size) > fork_size &&
380 fork_format == XFS_DINODE_FMT_LOCAL)
381 return __this_address;
384 switch (fork_format) {
385 case XFS_DINODE_FMT_LOCAL:
387 * No local regular files yet.
389 if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
390 return __this_address;
392 return __this_address;
394 case XFS_DINODE_FMT_EXTENTS:
395 if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
396 return __this_address;
398 case XFS_DINODE_FMT_BTREE:
399 if (whichfork == XFS_ATTR_FORK) {
400 if (di_nextents > MAXAEXTNUM)
401 return __this_address;
402 } else if (di_nextents > MAXEXTNUM) {
403 return __this_address;
407 return __this_address;
412 static xfs_failaddr_t
413 xfs_dinode_verify_forkoff(
414 struct xfs_dinode *dip,
415 struct xfs_mount *mp)
417 if (!dip->di_forkoff)
420 switch (dip->di_format) {
421 case XFS_DINODE_FMT_DEV:
422 if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
423 return __this_address;
425 case XFS_DINODE_FMT_LOCAL: /* fall through ... */
426 case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
427 case XFS_DINODE_FMT_BTREE:
428 if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
429 return __this_address;
432 return __this_address;
439 struct xfs_mount *mp,
441 struct xfs_dinode *dip)
449 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
450 return __this_address;
452 /* Verify v3 integrity information first */
453 if (dip->di_version >= 3) {
454 if (!xfs_sb_version_has_v3inode(&mp->m_sb))
455 return __this_address;
456 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
458 return __this_address;
459 if (be64_to_cpu(dip->di_ino) != ino)
460 return __this_address;
461 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
462 return __this_address;
465 /* don't allow invalid i_size */
466 di_size = be64_to_cpu(dip->di_size);
467 if (di_size & (1ULL << 63))
468 return __this_address;
470 mode = be16_to_cpu(dip->di_mode);
471 if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
472 return __this_address;
474 /* No zero-length symlinks/dirs. */
475 if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
476 return __this_address;
478 /* Fork checks carried over from xfs_iformat_fork */
480 be32_to_cpu(dip->di_nextents) + be16_to_cpu(dip->di_anextents) >
481 be64_to_cpu(dip->di_nblocks))
482 return __this_address;
484 if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
485 return __this_address;
487 flags = be16_to_cpu(dip->di_flags);
489 if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
490 return __this_address;
492 /* check for illegal values of forkoff */
493 fa = xfs_dinode_verify_forkoff(dip, mp);
497 /* Do we have appropriate data fork formats for the mode? */
498 switch (mode & S_IFMT) {
503 if (dip->di_format != XFS_DINODE_FMT_DEV)
504 return __this_address;
509 fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
514 /* Uninitialized inode ok. */
517 return __this_address;
520 if (dip->di_forkoff) {
521 fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
526 * If there is no fork offset, this may be a freshly-made inode
527 * in a new disk cluster, in which case di_aformat is zeroed.
528 * Otherwise, such an inode must be in EXTENTS format; this goes
529 * for freed inodes as well.
531 switch (dip->di_aformat) {
533 case XFS_DINODE_FMT_EXTENTS:
536 return __this_address;
538 if (dip->di_anextents)
539 return __this_address;
542 /* extent size hint validation */
543 fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
548 /* only version 3 or greater inodes are extensively verified here */
549 if (dip->di_version < 3)
552 flags2 = be64_to_cpu(dip->di_flags2);
554 /* don't allow reflink/cowextsize if we don't have reflink */
555 if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
556 !xfs_sb_version_hasreflink(&mp->m_sb))
557 return __this_address;
559 /* only regular files get reflink */
560 if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
561 return __this_address;
563 /* don't let reflink and realtime mix */
564 if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
565 return __this_address;
567 /* don't let reflink and dax mix */
568 if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags2 & XFS_DIFLAG2_DAX))
569 return __this_address;
571 /* COW extent size hint validation */
572 fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
573 mode, flags, flags2);
577 /* bigtime iflag can only happen on bigtime filesystems */
578 if (xfs_dinode_has_bigtime(dip) &&
579 !xfs_sb_version_hasbigtime(&mp->m_sb))
580 return __this_address;
587 struct xfs_mount *mp,
588 struct xfs_dinode *dip)
592 if (dip->di_version < 3)
595 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
596 crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
598 dip->di_crc = xfs_end_cksum(crc);
602 * Validate di_extsize hint.
604 * The rules are documented at xfs_ioctl_setattr_check_extsize().
605 * These functions must be kept in sync with each other.
608 xfs_inode_validate_extsize(
609 struct xfs_mount *mp,
617 uint32_t extsize_bytes;
618 uint32_t blocksize_bytes;
620 rt_flag = (flags & XFS_DIFLAG_REALTIME);
621 hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
622 inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
623 extsize_bytes = XFS_FSB_TO_B(mp, extsize);
626 blocksize_bytes = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
628 blocksize_bytes = mp->m_sb.sb_blocksize;
630 if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
631 return __this_address;
633 if (hint_flag && !S_ISREG(mode))
634 return __this_address;
636 if (inherit_flag && !S_ISDIR(mode))
637 return __this_address;
639 if ((hint_flag || inherit_flag) && extsize == 0)
640 return __this_address;
642 /* free inodes get flags set to zero but extsize remains */
643 if (mode && !(hint_flag || inherit_flag) && extsize != 0)
644 return __this_address;
646 if (extsize_bytes % blocksize_bytes)
647 return __this_address;
649 if (extsize > MAXEXTLEN)
650 return __this_address;
652 if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
653 return __this_address;
659 * Validate di_cowextsize hint.
661 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
662 * These functions must be kept in sync with each other.
665 xfs_inode_validate_cowextsize(
666 struct xfs_mount *mp,
674 uint32_t cowextsize_bytes;
676 rt_flag = (flags & XFS_DIFLAG_REALTIME);
677 hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
678 cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
680 if (hint_flag && !xfs_sb_version_hasreflink(&mp->m_sb))
681 return __this_address;
683 if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
684 return __this_address;
686 if (hint_flag && cowextsize == 0)
687 return __this_address;
689 /* free inodes get flags set to zero but cowextsize remains */
690 if (mode && !hint_flag && cowextsize != 0)
691 return __this_address;
693 if (hint_flag && rt_flag)
694 return __this_address;
696 if (cowextsize_bytes % mp->m_sb.sb_blocksize)
697 return __this_address;
699 if (cowextsize > MAXEXTLEN)
700 return __this_address;
702 if (cowextsize > mp->m_sb.sb_agblocks / 2)
703 return __this_address;