diff --git a/sys/fs/ext2fs/ext2_inode_cnv.c b/sys/fs/ext2fs/ext2_inode_cnv.c index 3c79e1162896..5ca00f3be050 100644 --- a/sys/fs/ext2fs/ext2_inode_cnv.c +++ b/sys/fs/ext2fs/ext2_inode_cnv.c @@ -1,313 +1,336 @@ /*- * SPDX-License-Identifier: MIT-CMU * * Copyright (c) 1995 The University of Utah and * the Computer Systems Laboratory at the University of Utah (CSL). * All rights reserved. * * Permission to use, copy, modify and distribute this software is hereby * granted provided that (1) source code retains these copyright, permission, * and disclaimer notices, and (2) redistributions including binaries * reproduce the notices in supporting documentation, and (3) all advertising * materials mentioning features or use of this software display the following * acknowledgement: ``This product includes software developed by the * Computer Systems Laboratory at the University of Utah.'' * * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * CSL requests users of this software to return to csl-dist@cs.utah.edu any * improvements that they make and grant CSL redistribution rights. * * Utah $Hdr$ * $FreeBSD$ */ /* * routines to convert on disk ext2 inodes into inodes and back */ #include #include #include #include #include #include #include #include #include #include #include #include SDT_PROVIDER_DECLARE(ext2fs); /* * ext2fs trace probe: * arg0: verbosity. Higher numbers give more verbose messages * arg1: Textual message */ SDT_PROBE_DEFINE2(ext2fs, , trace, inode_cnv, "int", "char*"); #ifdef EXT2FS_PRINT_EXTENTS void ext2_print_inode(struct inode *in) { int i; struct ext4_extent_header *ehp; struct ext4_extent *ep; printf("Inode: %5ju", (uintmax_t)in->i_number); printf( /* "Inode: %5d" */ " Type: %10s Mode: 0x%o Flags: 0x%x Version: %d acl: 0x%jx\n", "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl); printf("User: %5u Group: %5u Size: %ju\n", in->i_uid, in->i_gid, (uintmax_t)in->i_size); printf("Links: %3d Blockcount: %ju\n", in->i_nlink, (uintmax_t)in->i_blocks); printf("ctime: 0x%x ", in->i_ctime); printf("atime: 0x%x ", in->i_atime); printf("mtime: 0x%x ", in->i_mtime); if (E2DI_HAS_XTIME(in)) printf("crtime %#x\n", in->i_birthtime); else printf("\n"); if (in->i_flag & IN_E4EXTENTS) { printf("Extents:\n"); ehp = (struct ext4_extent_header *)in->i_db; printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n", le16toh(ehp->eh_magic), le16toh(ehp->eh_ecount), le16toh(ehp->eh_max), le16toh(ehp->eh_depth), le32toh(ehp->eh_gen)); ep = (struct ext4_extent *)(char *)(ehp + 1); printf("Index (blk %d len %d start_lo %d start_hi %d)\n", le32toh(ep->e_blk), le16toh(ep->e_len), le32toh(ep->e_start_lo), le16toh(ep->e_start_hi)); printf("\n"); } else { printf("Blocks:"); for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++) printf(" %d", in->i_db[i]); printf("\n"); } } #endif /* EXT2FS_PRINT_EXTENTS */ -#define XTIME_TO_NSEC(x) ((le32toh(x) & EXT3_NSEC_MASK) >> 2) - static inline bool ext2_old_valid_dev(dev_t dev) { return (major(dev) < 256 && minor(dev) < 256); } static inline uint16_t ext2_old_encode_dev(dev_t dev) { return ((major(dev) << 8) | minor(dev)); } static inline dev_t ext2_old_decode_dev(uint16_t val) { return (makedev((val >> 8) & 255, val & 255)); } static inline uint32_t ext2_new_encode_dev(dev_t dev) { unsigned maj = major(dev); unsigned min = minor(dev); return ((min & 0xff) | (maj << 8) | ((min & ~0xff) << 12)); } static inline dev_t ext2_new_decode_dev(uint32_t dev) { unsigned maj = (dev & 0xfff00) >> 8; unsigned min = (dev & 0xff) | ((dev >> 12) & 0xfff00); return (makedev(maj, min)); } +static void +ext2_decode_extra_time(ext_time_t *sec, int32_t *nsec, uint32_t extra) +{ + if (extra & htole32(EXT3_EPOCH_MASK)) + *sec += (uint64_t)(le32toh(extra) & EXT3_EPOCH_MASK) << 32; + + *nsec = (le32toh(extra) & EXT3_NSEC_MASK) >> EXT3_EPOCH_BITS; +} + /* * raw ext2 inode LE to host inode conversion */ int ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip) { struct m_ext2fs *fs = ip->i_e2fs; uint32_t ei_flags_host; uint16_t ei_extra_isize_le; int i; if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) || (ip->i_number < EXT2_ROOTINO) || (ip->i_number > le32toh(fs->e2fs->e2fs_icount))) { SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad inode number"); return (EINVAL); } ip->i_nlink = le16toh(ei->e2di_nlink); if (ip->i_number == EXT2_ROOTINO && ip->i_nlink == 0) { SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "root inode unallocated"); return (EINVAL); } /* Check extra inode size */ ei_extra_isize_le = le16toh(ei->e2di_extra_isize); if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) { if (E2FS_REV0_INODE_SIZE + ei_extra_isize_le > EXT2_INODE_SIZE(fs) || (ei_extra_isize_le & 3)) { SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad extra inode size"); return (EINVAL); } } /* * Godmar thinks - if the link count is zero, then the inode is * unused - according to ext2 standards. Ufs marks this fact by * setting i_mode to zero - why ? I can see that this might lead to * problems in an undelete. */ ip->i_mode = ip->i_nlink ? le16toh(ei->e2di_mode) : 0; ip->i_size = le32toh(ei->e2di_size); if (S_ISREG(ip->i_mode)) ip->i_size |= (uint64_t)le32toh(ei->e2di_size_high) << 32; - ip->i_atime = le32toh(ei->e2di_atime); - ip->i_mtime = le32toh(ei->e2di_mtime); - ip->i_ctime = le32toh(ei->e2di_ctime); + ip->i_atime = (signed)le32toh(ei->e2di_atime); + ip->i_mtime = (signed)le32toh(ei->e2di_mtime); + ip->i_ctime = (signed)le32toh(ei->e2di_ctime); if (E2DI_HAS_XTIME(ip)) { - ip->i_atimensec = XTIME_TO_NSEC(ei->e2di_atime_extra); - ip->i_mtimensec = XTIME_TO_NSEC(ei->e2di_mtime_extra); - ip->i_ctimensec = XTIME_TO_NSEC(ei->e2di_ctime_extra); - ip->i_birthtime = le32toh(ei->e2di_crtime); - ip->i_birthnsec = XTIME_TO_NSEC(ei->e2di_crtime_extra); + ext2_decode_extra_time(&ip->i_atime, &ip->i_atimensec, + ei->e2di_atime_extra); + ext2_decode_extra_time(&ip->i_mtime, &ip->i_mtimensec, + ei->e2di_mtime_extra); + ext2_decode_extra_time(&ip->i_ctime, &ip->i_ctimensec, + ei->e2di_ctime_extra); + ip->i_birthtime = (signed)le32toh(ei->e2di_crtime); + ext2_decode_extra_time(&ip->i_birthtime, &ip->i_birthnsec, + ei->e2di_crtime_extra); } ip->i_flags = 0; ei_flags_host = le32toh(ei->e2di_flags); ip->i_flags |= (ei_flags_host & EXT2_APPEND) ? SF_APPEND : 0; ip->i_flags |= (ei_flags_host & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0; ip->i_flags |= (ei_flags_host & EXT2_NODUMP) ? UF_NODUMP : 0; ip->i_flag |= (ei_flags_host & EXT3_INDEX) ? IN_E3INDEX : 0; ip->i_flag |= (ei_flags_host & EXT4_EXTENTS) ? IN_E4EXTENTS : 0; ip->i_blocks = le32toh(ei->e2di_nblock); ip->i_facl = le32toh(ei->e2di_facl); if (E2DI_HAS_HUGE_FILE(ip)) { ip->i_blocks |= (uint64_t)le16toh(ei->e2di_nblock_high) << 32; ip->i_facl |= (uint64_t)le16toh(ei->e2di_facl_high) << 32; if (ei_flags_host & EXT4_HUGE_FILE) ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks); } ip->i_gen = le32toh(ei->e2di_gen); ip->i_uid = le16toh(ei->e2di_uid); ip->i_gid = le16toh(ei->e2di_gid); ip->i_uid |= (uint32_t)le16toh(ei->e2di_uid_high) << 16; ip->i_gid |= (uint32_t)le16toh(ei->e2di_gid_high) << 16; if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) { if (ei->e2di_blocks[0]) ip->i_rdev = ext2_old_decode_dev(le32toh(ei->e2di_blocks[0])); else ip->i_rdev = ext2_new_decode_dev(le32toh(ei->e2di_blocks[1])); } else if ((ip->i_flag & IN_E4EXTENTS)) { memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks)); } else { for (i = 0; i < EXT2_NDADDR; i++) ip->i_db[i] = le32toh(ei->e2di_blocks[i]); for (i = 0; i < EXT2_NIADDR; i++) ip->i_ib[i] = le32toh(ei->e2di_blocks[EXT2_NDIR_BLOCKS + i]); } /* Verify inode csum. */ return (ext2_ei_csum_verify(ip, ei)); } -#define NSEC_TO_XTIME(t) (htole32((t << 2) & EXT3_NSEC_MASK)) +static uint32_t +ext2_encode_extra_time(int64_t sec, int32_t nsec) +{ + uint32_t extra; + + extra = ((sec - (int32_t)sec) >> 32) & EXT3_EPOCH_MASK; + + return (htole32(extra | (nsec << EXT3_EPOCH_BITS))); +} /* * inode to raw ext2 LE inode conversion */ int ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei) { struct m_ext2fs *fs; int i; fs = ip->i_e2fs; ei->e2di_mode = htole16(ip->i_mode); ei->e2di_nlink = htole16(ip->i_nlink); ei->e2di_size = htole32(ip->i_size); if (S_ISREG(ip->i_mode)) ei->e2di_size_high = htole32(ip->i_size >> 32); ei->e2di_atime = htole32(ip->i_atime); ei->e2di_mtime = htole32(ip->i_mtime); ei->e2di_ctime = htole32(ip->i_ctime); /* * Godmar thinks: if dtime is nonzero, ext2 says this inode has been * deleted, this would correspond to a zero link count */ ei->e2di_dtime = htole32(le16toh(ei->e2di_nlink) ? 0 : le32toh(ei->e2di_mtime)); if (E2DI_HAS_XTIME(ip)) { - ei->e2di_ctime_extra = NSEC_TO_XTIME(ip->i_ctimensec); - ei->e2di_mtime_extra = NSEC_TO_XTIME(ip->i_mtimensec); - ei->e2di_atime_extra = NSEC_TO_XTIME(ip->i_atimensec); + ei->e2di_ctime_extra = ext2_encode_extra_time(ip->i_ctime, + ip->i_ctimensec); + ei->e2di_mtime_extra = ext2_encode_extra_time(ip->i_mtime, + ip->i_mtimensec); + ei->e2di_atime_extra = ext2_encode_extra_time(ip->i_atime, + ip->i_atimensec); ei->e2di_crtime = htole32(ip->i_birthtime); - ei->e2di_crtime_extra = NSEC_TO_XTIME(ip->i_birthnsec); + ei->e2di_crtime_extra = ext2_encode_extra_time(ip->i_birthtime, + ip->i_birthnsec); } /* Keep these in host endian for a while since they change a lot */ ei->e2di_flags = 0; ei->e2di_flags |= htole32((ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0); ei->e2di_flags |= htole32((ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0); ei->e2di_flags |= htole32((ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0); ei->e2di_flags |= htole32((ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0); ei->e2di_flags |= htole32((ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0); if (ip->i_blocks > ~0U && !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) { SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "i_blocks value is out of range"); return (EIO); } if (ip->i_blocks <= 0xffffffffffffULL) { ei->e2di_nblock = htole32(ip->i_blocks & 0xffffffff); ei->e2di_nblock_high = htole16(ip->i_blocks >> 32 & 0xffff); } else { ei->e2di_flags |= htole32(EXT4_HUGE_FILE); ei->e2di_nblock = htole32(dbtofsb(fs, ip->i_blocks)); ei->e2di_nblock_high = htole16(dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff); } ei->e2di_facl = htole32(ip->i_facl & 0xffffffff); ei->e2di_facl_high = htole16(ip->i_facl >> 32 & 0xffff); ei->e2di_gen = htole32(ip->i_gen); ei->e2di_uid = htole16(ip->i_uid & 0xffff); ei->e2di_uid_high = htole16(ip->i_uid >> 16 & 0xffff); ei->e2di_gid = htole16(ip->i_gid & 0xffff); ei->e2di_gid_high = htole16(ip->i_gid >> 16 & 0xffff); if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) { if (ext2_old_valid_dev(ip->i_rdev)) { ei->e2di_blocks[0] = htole32(ext2_old_encode_dev(ip->i_rdev)); ei->e2di_blocks[1] = 0; } else { ei->e2di_blocks[0] = 0; ei->e2di_blocks[1] = htole32(ext2_new_encode_dev(ip->i_rdev)); ei->e2di_blocks[2] = 0; } } else if ((ip->i_flag & IN_E4EXTENTS)) { memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks)); } else { for (i = 0; i < EXT2_NDADDR; i++) ei->e2di_blocks[i] = htole32(ip->i_db[i]); for (i = 0; i < EXT2_NIADDR; i++) ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = htole32(ip->i_ib[i]); } /* Set inode csum. */ ext2_ei_csum_set(ip, ei); return (0); } diff --git a/sys/fs/ext2fs/inode.h b/sys/fs/ext2fs/inode.h index e6af6ef8d5b6..25e77acc3a9d 100644 --- a/sys/fs/ext2fs/inode.h +++ b/sys/fs/ext2fs/inode.h @@ -1,199 +1,200 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1982, 1989, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)inode.h 8.9 (Berkeley) 5/14/95 * $FreeBSD$ */ #ifndef _FS_EXT2FS_INODE_H_ #define _FS_EXT2FS_INODE_H_ #include #include #include #include #include #include /* * This must agree with the definition in . */ #define doff_t int32_t #define EXT2_NDADDR 12 /* Direct addresses in inode. */ #define EXT2_NIADDR 3 /* Indirect addresses in inode. */ /* * The size of physical and logical block numbers in EXT2FS. */ typedef uint32_t e2fs_daddr_t; typedef int64_t e2fs_lbn_t; typedef int64_t e4fs_daddr_t; +typedef int64_t ext_time_t; /* * The inode is used to describe each active (or recently active) file in the * EXT2FS filesystem. It is composed of two types of information. The first * part is the information that is needed only while the file is active (such * as the identity of the file and linkage to speed its lookup). The second * part is the permanent meta-data associated with the file which is read in * from the permanent dinode from long term storage when the file becomes * active, and is put back when the file is no longer being used. */ struct inode { struct vnode *i_vnode;/* Vnode associated with this inode. */ struct ext2mount *i_ump; uint32_t i_flag; /* flags, see below */ ino_t i_number; /* The identity of the inode. */ struct m_ext2fs *i_e2fs; /* EXT2FS */ u_quad_t i_modrev; /* Revision level for NFS lease. */ /* * Side effects; used during directory lookup. */ int32_t i_count; /* Size of free slot in directory. */ doff_t i_endoff; /* End of useful stuff in directory. */ doff_t i_diroff; /* Offset in dir, where we found last entry. */ doff_t i_offset; /* Offset of free space in directory. */ uint32_t i_block_group; uint32_t i_next_alloc_block; uint32_t i_next_alloc_goal; /* Fields from struct dinode in UFS. */ uint16_t i_mode; /* IFMT, permissions; see below. */ int32_t i_nlink; /* File link count. */ uint32_t i_uid; /* File owner. */ uint32_t i_gid; /* File group. */ uint64_t i_size; /* File byte count. */ uint64_t i_blocks; /* Blocks actually held. */ - int32_t i_atime; /* Last access time. */ - int32_t i_mtime; /* Last modified time. */ - int32_t i_ctime; /* Last inode change time. */ - int32_t i_birthtime; /* Inode creation time. */ + ext_time_t i_atime; /* Last access time. */ + ext_time_t i_mtime; /* Last modified time. */ + ext_time_t i_ctime; /* Last inode change time. */ + ext_time_t i_birthtime; /* Inode creation time. */ int32_t i_mtimensec; /* Last modified time. */ int32_t i_atimensec; /* Last access time. */ int32_t i_ctimensec; /* Last inode change time. */ int32_t i_birthnsec; /* Inode creation time. */ uint32_t i_gen; /* Generation number. */ uint64_t i_facl; /* EA block number. */ uint32_t i_flags; /* Status flags (chflags). */ dev_t i_rdev; /* Major/minor inode values. */ union { struct { uint32_t i_db[EXT2_NDADDR]; /* Direct disk blocks. */ uint32_t i_ib[EXT2_NIADDR]; /* Indirect disk blocks. */ }; uint32_t i_data[EXT2_NDADDR + EXT2_NIADDR]; }; struct ext4_extent_cache i_ext_cache; /* cache for ext4 extent */ struct vn_clusterw i_clusterw; /* Buffer clustering information */ }; /* * The di_db fields may be overlaid with other information for * file types that do not have associated disk storage. Block * and character devices overlay the first data block with their * dev_t value. Short symbolic links place their path in the * di_db area. */ #define i_shortlink i_db /* File permissions. */ #define IEXEC 0000100 /* Executable. */ #define IWRITE 0000200 /* Writeable. */ #define IREAD 0000400 /* Readable. */ #define ISVTX 0001000 /* Sticky bit. */ #define ISGID 0002000 /* Set-gid. */ #define ISUID 0004000 /* Set-uid. */ /* File types. */ #define IFMT 0170000 /* Mask of file type. */ #define IFIFO 0010000 /* Named pipe (fifo). */ #define IFCHR 0020000 /* Character device. */ #define IFDIR 0040000 /* Directory file. */ #define IFBLK 0060000 /* Block device. */ #define IFREG 0100000 /* Regular file. */ #define IFLNK 0120000 /* Symbolic link. */ #define IFSOCK 0140000 /* UNIX domain socket. */ #define IFWHT 0160000 /* Whiteout. */ /* These flags are kept in i_flag. */ #define IN_ACCESS 0x0001 /* Access time update request. */ #define IN_CHANGE 0x0002 /* Inode change time update request. */ #define IN_UPDATE 0x0004 /* Modification time update request. */ #define IN_MODIFIED 0x0008 /* Inode has been modified. */ #define IN_RENAME 0x0010 /* Inode is being renamed. */ #define IN_HASHED 0x0020 /* Inode is on hash list */ #define IN_LAZYMOD 0x0040 /* Modified, but don't write yet. */ #define IN_SPACECOUNTED 0x0080 /* Blocks to be freed in free count. */ #define IN_LAZYACCESS 0x0100 /* Process IN_ACCESS after the suspension finished */ /* * These are translation flags for some attributes that Ext4 * passes as inode flags but that we cannot pass directly. */ #define IN_E3INDEX 0x010000 #define IN_E4EXTENTS 0x020000 #define i_devvp i_ump->um_devvp #ifdef _KERNEL /* * Structure used to pass around logical block paths generated by * ext2_getlbns and used by truncate and bmap code. */ struct indir { e2fs_lbn_t in_lbn; /* Logical block number. */ int in_off; /* Offset in buffer. */ }; /* Convert between inode pointers and vnode pointers. */ #define VTOI(vp) ((struct inode *)(vp)->v_data) #define ITOV(ip) ((ip)->i_vnode) /* This overlays the fid structure (see mount.h). */ struct ufid { uint16_t ufid_len; /* Length of structure. */ uint16_t ufid_pad; /* Force 32-bit alignment. */ ino_t ufid_ino; /* File number (ino). */ uint32_t ufid_gen; /* Generation number. */ }; #endif /* _KERNEL */ #endif /* !_FS_EXT2FS_INODE_H_ */