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);
362 switch (XFS_DFORK_FORMAT(dip, whichfork)) {
363 case XFS_DINODE_FMT_LOCAL:
365 * no local regular files yet
367 if (whichfork == XFS_DATA_FORK) {
368 if (S_ISREG(be16_to_cpu(dip->di_mode)))
369 return __this_address;
370 if (be64_to_cpu(dip->di_size) >
371 XFS_DFORK_SIZE(dip, mp, whichfork))
372 return __this_address;
375 return __this_address;
377 case XFS_DINODE_FMT_EXTENTS:
378 if (di_nextents > XFS_DFORK_MAXEXT(dip, mp, whichfork))
379 return __this_address;
381 case XFS_DINODE_FMT_BTREE:
382 if (whichfork == XFS_ATTR_FORK) {
383 if (di_nextents > MAXAEXTNUM)
384 return __this_address;
385 } else if (di_nextents > MAXEXTNUM) {
386 return __this_address;
390 return __this_address;
395 static xfs_failaddr_t
396 xfs_dinode_verify_forkoff(
397 struct xfs_dinode *dip,
398 struct xfs_mount *mp)
400 if (!dip->di_forkoff)
403 switch (dip->di_format) {
404 case XFS_DINODE_FMT_DEV:
405 if (dip->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3))
406 return __this_address;
408 case XFS_DINODE_FMT_LOCAL: /* fall through ... */
409 case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
410 case XFS_DINODE_FMT_BTREE:
411 if (dip->di_forkoff >= (XFS_LITINO(mp) >> 3))
412 return __this_address;
415 return __this_address;
422 struct xfs_mount *mp,
424 struct xfs_dinode *dip)
432 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
433 return __this_address;
435 /* Verify v3 integrity information first */
436 if (dip->di_version >= 3) {
437 if (!xfs_sb_version_has_v3inode(&mp->m_sb))
438 return __this_address;
439 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
441 return __this_address;
442 if (be64_to_cpu(dip->di_ino) != ino)
443 return __this_address;
444 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_meta_uuid))
445 return __this_address;
448 /* don't allow invalid i_size */
449 di_size = be64_to_cpu(dip->di_size);
450 if (di_size & (1ULL << 63))
451 return __this_address;
453 mode = be16_to_cpu(dip->di_mode);
454 if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
455 return __this_address;
457 /* No zero-length symlinks/dirs. */
458 if ((S_ISLNK(mode) || S_ISDIR(mode)) && di_size == 0)
459 return __this_address;
461 /* Fork checks carried over from xfs_iformat_fork */
463 be32_to_cpu(dip->di_nextents) + be16_to_cpu(dip->di_anextents) >
464 be64_to_cpu(dip->di_nblocks))
465 return __this_address;
467 if (mode && XFS_DFORK_BOFF(dip) > mp->m_sb.sb_inodesize)
468 return __this_address;
470 flags = be16_to_cpu(dip->di_flags);
472 if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
473 return __this_address;
475 /* check for illegal values of forkoff */
476 fa = xfs_dinode_verify_forkoff(dip, mp);
480 /* Do we have appropriate data fork formats for the mode? */
481 switch (mode & S_IFMT) {
486 if (dip->di_format != XFS_DINODE_FMT_DEV)
487 return __this_address;
492 fa = xfs_dinode_verify_fork(dip, mp, XFS_DATA_FORK);
497 /* Uninitialized inode ok. */
500 return __this_address;
503 if (dip->di_forkoff) {
504 fa = xfs_dinode_verify_fork(dip, mp, XFS_ATTR_FORK);
509 * If there is no fork offset, this may be a freshly-made inode
510 * in a new disk cluster, in which case di_aformat is zeroed.
511 * Otherwise, such an inode must be in EXTENTS format; this goes
512 * for freed inodes as well.
514 switch (dip->di_aformat) {
516 case XFS_DINODE_FMT_EXTENTS:
519 return __this_address;
521 if (dip->di_anextents)
522 return __this_address;
525 /* extent size hint validation */
526 fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
531 /* only version 3 or greater inodes are extensively verified here */
532 if (dip->di_version < 3)
535 flags2 = be64_to_cpu(dip->di_flags2);
537 /* don't allow reflink/cowextsize if we don't have reflink */
538 if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
539 !xfs_sb_version_hasreflink(&mp->m_sb))
540 return __this_address;
542 /* only regular files get reflink */
543 if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
544 return __this_address;
546 /* don't let reflink and realtime mix */
547 if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME))
548 return __this_address;
550 /* COW extent size hint validation */
551 fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
552 mode, flags, flags2);
556 /* bigtime iflag can only happen on bigtime filesystems */
557 if (xfs_dinode_has_bigtime(dip) &&
558 !xfs_sb_version_hasbigtime(&mp->m_sb))
559 return __this_address;
566 struct xfs_mount *mp,
567 struct xfs_dinode *dip)
571 if (dip->di_version < 3)
574 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
575 crc = xfs_start_cksum_update((char *)dip, mp->m_sb.sb_inodesize,
577 dip->di_crc = xfs_end_cksum(crc);
581 * Validate di_extsize hint.
583 * The rules are documented at xfs_ioctl_setattr_check_extsize().
584 * These functions must be kept in sync with each other.
587 xfs_inode_validate_extsize(
588 struct xfs_mount *mp,
596 uint32_t extsize_bytes;
597 uint32_t blocksize_bytes;
599 rt_flag = (flags & XFS_DIFLAG_REALTIME);
600 hint_flag = (flags & XFS_DIFLAG_EXTSIZE);
601 inherit_flag = (flags & XFS_DIFLAG_EXTSZINHERIT);
602 extsize_bytes = XFS_FSB_TO_B(mp, extsize);
605 blocksize_bytes = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
607 blocksize_bytes = mp->m_sb.sb_blocksize;
609 if ((hint_flag || inherit_flag) && !(S_ISDIR(mode) || S_ISREG(mode)))
610 return __this_address;
612 if (hint_flag && !S_ISREG(mode))
613 return __this_address;
615 if (inherit_flag && !S_ISDIR(mode))
616 return __this_address;
618 if ((hint_flag || inherit_flag) && extsize == 0)
619 return __this_address;
621 /* free inodes get flags set to zero but extsize remains */
622 if (mode && !(hint_flag || inherit_flag) && extsize != 0)
623 return __this_address;
625 if (extsize_bytes % blocksize_bytes)
626 return __this_address;
628 if (extsize > MAXEXTLEN)
629 return __this_address;
631 if (!rt_flag && extsize > mp->m_sb.sb_agblocks / 2)
632 return __this_address;
638 * Validate di_cowextsize hint.
640 * The rules are documented at xfs_ioctl_setattr_check_cowextsize().
641 * These functions must be kept in sync with each other.
644 xfs_inode_validate_cowextsize(
645 struct xfs_mount *mp,
653 uint32_t cowextsize_bytes;
655 rt_flag = (flags & XFS_DIFLAG_REALTIME);
656 hint_flag = (flags2 & XFS_DIFLAG2_COWEXTSIZE);
657 cowextsize_bytes = XFS_FSB_TO_B(mp, cowextsize);
659 if (hint_flag && !xfs_sb_version_hasreflink(&mp->m_sb))
660 return __this_address;
662 if (hint_flag && !(S_ISDIR(mode) || S_ISREG(mode)))
663 return __this_address;
665 if (hint_flag && cowextsize == 0)
666 return __this_address;
668 /* free inodes get flags set to zero but cowextsize remains */
669 if (mode && !hint_flag && cowextsize != 0)
670 return __this_address;
672 if (hint_flag && rt_flag)
673 return __this_address;
675 if (cowextsize_bytes % mp->m_sb.sb_blocksize)
676 return __this_address;
678 if (cowextsize > MAXEXTLEN)
679 return __this_address;
681 if (cowextsize > mp->m_sb.sb_agblocks / 2)
682 return __this_address;