The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ext2fs/ext2_inode_cnv.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: MIT-CMU
    3  *
    4  * Copyright (c) 1995 The University of Utah and
    5  * the Computer Systems Laboratory at the University of Utah (CSL).
    6  * All rights reserved.
    7  *
    8  * Permission to use, copy, modify and distribute this software is hereby
    9  * granted provided that (1) source code retains these copyright, permission,
   10  * and disclaimer notices, and (2) redistributions including binaries
   11  * reproduce the notices in supporting documentation, and (3) all advertising
   12  * materials mentioning features or use of this software display the following
   13  * acknowledgement: ``This product includes software developed by the
   14  * Computer Systems Laboratory at the University of Utah.''
   15  *
   16  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
   17  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
   18  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   19  *
   20  * CSL requests users of this software to return to csl-dist@cs.utah.edu any
   21  * improvements that they make and grant CSL redistribution rights.
   22  *
   23  *      Utah $Hdr$
   24  * $FreeBSD$
   25  */
   26 
   27 /*
   28  * routines to convert on disk ext2 inodes into inodes and back
   29  */
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 #include <sys/endian.h>
   33 #include <sys/lock.h>
   34 #include <sys/sdt.h>
   35 #include <sys/stat.h>
   36 #include <sys/vnode.h>
   37 
   38 #include <fs/ext2fs/fs.h>
   39 #include <fs/ext2fs/inode.h>
   40 #include <fs/ext2fs/ext2fs.h>
   41 #include <fs/ext2fs/ext2_dinode.h>
   42 #include <fs/ext2fs/ext2_extern.h>
   43 
   44 SDT_PROVIDER_DECLARE(ext2fs);
   45 /*
   46  * ext2fs trace probe:
   47  * arg0: verbosity. Higher numbers give more verbose messages
   48  * arg1: Textual message
   49  */
   50 SDT_PROBE_DEFINE2(ext2fs, , trace, inode_cnv, "int", "char*");
   51 
   52 #ifdef EXT2FS_PRINT_EXTENTS
   53 void
   54 ext2_print_inode(struct inode *in)
   55 {
   56         int i;
   57         struct ext4_extent_header *ehp;
   58         struct ext4_extent *ep;
   59 
   60         printf("Inode: %5ju", (uintmax_t)in->i_number);
   61         printf( /* "Inode: %5d" */
   62             " Type: %10s Mode: 0x%o Flags: 0x%x  Version: %d acl: 0x%jx\n",
   63             "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl);
   64         printf("User: %5u Group: %5u  Size: %ju\n",
   65             in->i_uid, in->i_gid, (uintmax_t)in->i_size);
   66         printf("Links: %3d Blockcount: %ju\n",
   67             in->i_nlink, (uintmax_t)in->i_blocks);
   68         printf("ctime: 0x%llx ", (unsigned long long)in->i_ctime);
   69         printf("atime: 0x%llx ", (unsigned long long)in->i_atime);
   70         printf("mtime: 0x%llx ", (unsigned long long)in->i_mtime);
   71         if (E2DI_HAS_XTIME(in))
   72                 printf("crtime %llx\n", (unsigned long long)in->i_birthtime);
   73         else
   74                 printf("\n");
   75         if (in->i_flag & IN_E4EXTENTS) {
   76                 printf("Extents:\n");
   77                 ehp = (struct ext4_extent_header *)in->i_db;
   78                 printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n",
   79                     le16toh(ehp->eh_magic), le16toh(ehp->eh_ecount),
   80                     le16toh(ehp->eh_max), le16toh(ehp->eh_depth),
   81                     le32toh(ehp->eh_gen));
   82                 ep = (struct ext4_extent *)(char *)(ehp + 1);
   83                 printf("Index (blk %d len %d start_lo %d start_hi %d)\n",
   84                     le32toh(ep->e_blk),
   85                     le16toh(ep->e_len), le32toh(ep->e_start_lo),
   86                     le16toh(ep->e_start_hi));
   87                 printf("\n");
   88         } else {
   89                 printf("Blocks:");
   90                 for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++)
   91                         printf("  %d", in->i_db[i]);
   92                 printf("\n");
   93         }
   94 }
   95 #endif  /* EXT2FS_PRINT_EXTENTS */
   96 
   97 static inline bool
   98 ext2_old_valid_dev(dev_t dev)
   99 {
  100         return (major(dev) < 256 && minor(dev) < 256);
  101 }
  102 
  103 static inline uint16_t
  104 ext2_old_encode_dev(dev_t dev)
  105 {
  106         return ((major(dev) << 8) | minor(dev));
  107 }
  108 
  109 static inline dev_t
  110 ext2_old_decode_dev(uint16_t val)
  111 {
  112         return (makedev((val >> 8) & 255, val & 255));
  113 }
  114 
  115 static inline uint32_t
  116 ext2_new_encode_dev(dev_t dev)
  117 {
  118         unsigned maj = major(dev);
  119         unsigned min = minor(dev);
  120 
  121         return ((min & 0xff) | (maj << 8) | ((min & ~0xff) << 12));
  122 }
  123 
  124 static inline dev_t
  125 ext2_new_decode_dev(uint32_t dev)
  126 {
  127         unsigned maj = (dev & 0xfff00) >> 8;
  128         unsigned min = (dev & 0xff) | ((dev >> 12) & 0xfff00);
  129 
  130         return (makedev(maj, min));
  131 }
  132 
  133 static inline void
  134 ext2_decode_extra_time(ext_time_t *sec, int32_t *nsec, uint32_t extra)
  135 {
  136         if (extra & htole32(EXT3_EPOCH_MASK))
  137                 *sec += (uint64_t)(le32toh(extra) & EXT3_EPOCH_MASK) << 32;
  138 
  139         *nsec = (le32toh(extra) & EXT3_NSEC_MASK) >> EXT3_EPOCH_BITS;
  140 }
  141 
  142 /*
  143  *      raw ext2 inode LE to host inode conversion
  144  */
  145 int
  146 ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
  147 {
  148         struct m_ext2fs *fs = ip->i_e2fs;
  149         uint32_t ei_flags_host;
  150         uint16_t ei_extra_isize_le;
  151         int i;
  152 
  153         if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) ||
  154             (ip->i_number < EXT2_ROOTINO) ||
  155             (ip->i_number > le32toh(fs->e2fs->e2fs_icount))) {
  156                 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad inode number");
  157                 return (EINVAL);
  158         }
  159 
  160         /*
  161          * Godmar thinks - if the link count is zero, then the inode is
  162          * unused - according to ext2 standards. Ufs marks this fact by
  163          * setting i_mode to zero - why ? I can see that this might lead to
  164          * problems in an undelete.
  165          */
  166         ip->i_nlink = le16toh(ei->e2di_nlink);
  167         ip->i_mode = ip->i_nlink ? le16toh(ei->e2di_mode) : 0;
  168         if (ip->i_number == EXT2_ROOTINO &&
  169             (ip->i_nlink < 2 || !S_ISDIR(ip->i_mode))) {
  170                 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "root inode invalid");
  171                 return (EINVAL);
  172         }
  173 
  174         /* Check extra inode size */
  175         ei_extra_isize_le = le16toh(ei->e2di_extra_isize);
  176         if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) {
  177                 if (E2FS_REV0_INODE_SIZE + ei_extra_isize_le >
  178                     EXT2_INODE_SIZE(fs) || (ei_extra_isize_le & 3)) {
  179                         SDT_PROBE2(ext2fs, , trace, inode_cnv, 1,
  180                             "bad extra inode size");
  181                         return (EINVAL);
  182                 }
  183         }
  184 
  185         ip->i_size = le32toh(ei->e2di_size);
  186         if (S_ISREG(ip->i_mode))
  187                 ip->i_size |= (uint64_t)le32toh(ei->e2di_size_high) << 32;
  188         ip->i_atime = (signed)le32toh(ei->e2di_atime);
  189         ip->i_mtime = (signed)le32toh(ei->e2di_mtime);
  190         ip->i_ctime = (signed)le32toh(ei->e2di_ctime);
  191         if (E2DI_HAS_XTIME(ip)) {
  192                 ext2_decode_extra_time(&ip->i_atime, &ip->i_atimensec,
  193                     ei->e2di_atime_extra);
  194                 ext2_decode_extra_time(&ip->i_mtime, &ip->i_mtimensec,
  195                     ei->e2di_mtime_extra);
  196                 ext2_decode_extra_time(&ip->i_ctime, &ip->i_ctimensec,
  197                     ei->e2di_ctime_extra);
  198                 ip->i_birthtime = (signed)le32toh(ei->e2di_crtime);
  199                 ext2_decode_extra_time(&ip->i_birthtime, &ip->i_birthnsec,
  200                     ei->e2di_crtime_extra);
  201         }
  202         ip->i_flags = 0;
  203         ei_flags_host = le32toh(ei->e2di_flags);
  204         ip->i_flags |= (ei_flags_host & EXT2_APPEND) ? SF_APPEND : 0;
  205         ip->i_flags |= (ei_flags_host & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
  206         ip->i_flags |= (ei_flags_host & EXT2_NODUMP) ? UF_NODUMP : 0;
  207         ip->i_flag |= (ei_flags_host & EXT3_INDEX) ? IN_E3INDEX : 0;
  208         ip->i_flag |= (ei_flags_host & EXT4_EXTENTS) ? IN_E4EXTENTS : 0;
  209         ip->i_blocks = le32toh(ei->e2di_nblock);
  210         ip->i_facl = le32toh(ei->e2di_facl);
  211         if (E2DI_HAS_HUGE_FILE(ip)) {
  212                 ip->i_blocks |= (uint64_t)le16toh(ei->e2di_nblock_high) << 32;
  213                 ip->i_facl |= (uint64_t)le16toh(ei->e2di_facl_high) << 32;
  214                 if (ei_flags_host & EXT4_HUGE_FILE)
  215                         ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks);
  216         }
  217         ip->i_gen = le32toh(ei->e2di_gen);
  218         ip->i_uid = le16toh(ei->e2di_uid);
  219         ip->i_gid = le16toh(ei->e2di_gid);
  220         ip->i_uid |= (uint32_t)le16toh(ei->e2di_uid_high) << 16;
  221         ip->i_gid |= (uint32_t)le16toh(ei->e2di_gid_high) << 16;
  222 
  223         if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
  224                 if (ei->e2di_blocks[0])
  225                         ip->i_rdev = ext2_old_decode_dev(le32toh(ei->e2di_blocks[0]));
  226                 else
  227                         ip->i_rdev = ext2_new_decode_dev(le32toh(ei->e2di_blocks[1]));
  228         } else if ((ip->i_flag & IN_E4EXTENTS)) {
  229                 memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks));
  230         } else {
  231                 for (i = 0; i < EXT2_NDADDR; i++)
  232                         ip->i_db[i] = le32toh(ei->e2di_blocks[i]);
  233                 for (i = 0; i < EXT2_NIADDR; i++)
  234                         ip->i_ib[i] = le32toh(ei->e2di_blocks[EXT2_NDIR_BLOCKS + i]);
  235         }
  236 
  237         /* Verify inode csum. */
  238         return (ext2_ei_csum_verify(ip, ei));
  239 }
  240 
  241 static inline uint32_t
  242 ext2_encode_extra_time(int64_t sec, int32_t nsec)
  243 {
  244         uint32_t extra;
  245 
  246         extra = ((sec - (int32_t)sec) >> 32) & EXT3_EPOCH_MASK;
  247 
  248         return (htole32(extra | (nsec << EXT3_EPOCH_BITS)));
  249 }
  250 
  251 /*
  252  *      inode to raw ext2 LE inode conversion
  253  */
  254 int
  255 ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
  256 {
  257         struct m_ext2fs *fs;
  258         int i;
  259 
  260         fs = ip->i_e2fs;
  261         ei->e2di_mode = htole16(ip->i_mode);
  262         ei->e2di_nlink = htole16(ip->i_nlink);
  263         ei->e2di_size = htole32(ip->i_size);
  264         if (S_ISREG(ip->i_mode))
  265                 ei->e2di_size_high = htole32(ip->i_size >> 32);
  266         ei->e2di_atime = htole32(ip->i_atime);
  267         ei->e2di_mtime = htole32(ip->i_mtime);
  268         ei->e2di_ctime = htole32(ip->i_ctime);
  269         /*
  270          * Godmar thinks: if dtime is nonzero, ext2 says this inode has been
  271          * deleted, this would correspond to a zero link count
  272          */
  273         ei->e2di_dtime = htole32(le16toh(ei->e2di_nlink) ? 0 :
  274             le32toh(ei->e2di_mtime));
  275         if (E2DI_HAS_XTIME(ip)) {
  276                 ei->e2di_ctime_extra = ext2_encode_extra_time(ip->i_ctime,
  277                     ip->i_ctimensec);
  278                 ei->e2di_mtime_extra = ext2_encode_extra_time(ip->i_mtime,
  279                     ip->i_mtimensec);
  280                 ei->e2di_atime_extra = ext2_encode_extra_time(ip->i_atime,
  281                     ip->i_atimensec);
  282                 ei->e2di_crtime = htole32(ip->i_birthtime);
  283                 ei->e2di_crtime_extra = ext2_encode_extra_time(ip->i_birthtime,
  284                     ip->i_birthnsec);
  285         }
  286         /* Keep these in host endian for a while since they change a lot */
  287         ei->e2di_flags = 0;
  288         ei->e2di_flags |= htole32((ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0);
  289         ei->e2di_flags |= htole32((ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0);
  290         ei->e2di_flags |= htole32((ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0);
  291         ei->e2di_flags |= htole32((ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0);
  292         ei->e2di_flags |= htole32((ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0);
  293         if (ip->i_blocks > ~0U &&
  294             !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) {
  295                 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "i_blocks value is out of range");
  296                 return (EIO);
  297         }
  298         if (ip->i_blocks <= 0xffffffffffffULL) {
  299                 ei->e2di_nblock = htole32(ip->i_blocks & 0xffffffff);
  300                 ei->e2di_nblock_high = htole16(ip->i_blocks >> 32 & 0xffff);
  301         } else {
  302                 ei->e2di_flags |= htole32(EXT4_HUGE_FILE);
  303                 ei->e2di_nblock = htole32(dbtofsb(fs, ip->i_blocks));
  304                 ei->e2di_nblock_high = htole16(dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff);
  305         }
  306 
  307         ei->e2di_facl = htole32(ip->i_facl & 0xffffffff);
  308         ei->e2di_facl_high = htole16(ip->i_facl >> 32 & 0xffff);
  309         ei->e2di_gen = htole32(ip->i_gen);
  310         ei->e2di_uid = htole16(ip->i_uid & 0xffff);
  311         ei->e2di_uid_high = htole16(ip->i_uid >> 16 & 0xffff);
  312         ei->e2di_gid = htole16(ip->i_gid & 0xffff);
  313         ei->e2di_gid_high = htole16(ip->i_gid >> 16 & 0xffff);
  314 
  315         if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
  316                 if (ext2_old_valid_dev(ip->i_rdev)) {
  317                         ei->e2di_blocks[0] = htole32(ext2_old_encode_dev(ip->i_rdev));
  318                         ei->e2di_blocks[1] = 0;
  319                 } else {
  320                         ei->e2di_blocks[0] = 0;
  321                         ei->e2di_blocks[1] = htole32(ext2_new_encode_dev(ip->i_rdev));
  322                         ei->e2di_blocks[2] = 0;
  323                 }
  324         } else if ((ip->i_flag & IN_E4EXTENTS)) {
  325                 memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks));
  326         } else {
  327                 for (i = 0; i < EXT2_NDADDR; i++)
  328                         ei->e2di_blocks[i] = htole32(ip->i_db[i]);
  329                 for (i = 0; i < EXT2_NIADDR; i++)
  330                         ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = htole32(ip->i_ib[i]);
  331         }
  332 
  333         /* Set inode csum. */
  334         ext2_ei_csum_set(ip, ei);
  335 
  336         return (0);
  337 }

Cache object: c3767c3df837e254dd142c670de4775c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.