Home | History | Annotate | Line # | Download | only in libsa
ffs_bswap.c revision 1.1.2.2
      1  1.1.2.2  cjep /*	$NetBSD: ffs_bswap.c,v 1.1.2.2 2021/05/31 22:15:21 cjep Exp $	*/
      2  1.1.2.2  cjep 
      3  1.1.2.2  cjep /*
      4  1.1.2.2  cjep  * Copyright (c) 1998 Manuel Bouyer.
      5  1.1.2.2  cjep  *
      6  1.1.2.2  cjep  * Redistribution and use in source and binary forms, with or without
      7  1.1.2.2  cjep  * modification, are permitted provided that the following conditions
      8  1.1.2.2  cjep  * are met:
      9  1.1.2.2  cjep  * 1. Redistributions of source code must retain the above copyright
     10  1.1.2.2  cjep  *    notice, this list of conditions and the following disclaimer.
     11  1.1.2.2  cjep  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1.2.2  cjep  *    notice, this list of conditions and the following disclaimer in the
     13  1.1.2.2  cjep  *    documentation and/or other materials provided with the distribution.
     14  1.1.2.2  cjep  *
     15  1.1.2.2  cjep  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1.2.2  cjep  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1.2.2  cjep  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1.2.2  cjep  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1.2.2  cjep  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1.2.2  cjep  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1.2.2  cjep  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1.2.2  cjep  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1.2.2  cjep  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1.2.2  cjep  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1.2.2  cjep  *
     26  1.1.2.2  cjep  */
     27  1.1.2.2  cjep 
     28  1.1.2.2  cjep /*
     29  1.1.2.2  cjep  * This copy is a minimal version for libsa and it's ufs.c.  The unused
     30  1.1.2.2  cjep  * functions are removed, and additional swapping is performed on the
     31  1.1.2.2  cjep  * di_extb, di_db, and di_ib arrays.
     32  1.1.2.2  cjep  */
     33  1.1.2.2  cjep 
     34  1.1.2.2  cjep #include <sys/cdefs.h>
     35  1.1.2.2  cjep __KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.1.2.2 2021/05/31 22:15:21 cjep Exp $");
     36  1.1.2.2  cjep 
     37  1.1.2.2  cjep #include <sys/param.h>
     38  1.1.2.2  cjep #include <lib/libkern/libkern.h>
     39  1.1.2.2  cjep 
     40  1.1.2.2  cjep #include <ufs/ufs/dinode.h>
     41  1.1.2.2  cjep #include <ufs/ufs/quota.h>
     42  1.1.2.2  cjep #include <ufs/ufs/ufs_bswap.h>
     43  1.1.2.2  cjep #include <ufs/ffs/fs.h>
     44  1.1.2.2  cjep #include <ufs/ffs/ffs_extern.h>
     45  1.1.2.2  cjep 
     46  1.1.2.2  cjep static void
     47  1.1.2.2  cjep libsa_ffs_csumtotal_swap(const struct csum_total *o, struct csum_total *n)
     48  1.1.2.2  cjep {
     49  1.1.2.2  cjep 	n->cs_ndir = bswap64(o->cs_ndir);
     50  1.1.2.2  cjep 	n->cs_nbfree = bswap64(o->cs_nbfree);
     51  1.1.2.2  cjep 	n->cs_nifree = bswap64(o->cs_nifree);
     52  1.1.2.2  cjep 	n->cs_nffree = bswap64(o->cs_nffree);
     53  1.1.2.2  cjep }
     54  1.1.2.2  cjep 
     55  1.1.2.2  cjep void
     56  1.1.2.2  cjep ffs_sb_swap(const struct fs *o, struct fs *n)
     57  1.1.2.2  cjep {
     58  1.1.2.2  cjep 	size_t i;
     59  1.1.2.2  cjep 	const u_int32_t *o32;
     60  1.1.2.2  cjep 	u_int32_t *n32;
     61  1.1.2.2  cjep 
     62  1.1.2.2  cjep 	/*
     63  1.1.2.2  cjep 	 * In order to avoid a lot of lines, as the first N fields (52)
     64  1.1.2.2  cjep 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
     65  1.1.2.2  cjep 	 * here to convert them.
     66  1.1.2.2  cjep 	 */
     67  1.1.2.2  cjep 	o32 = (const u_int32_t *)o;
     68  1.1.2.2  cjep 	n32 = (u_int32_t *)n;
     69  1.1.2.2  cjep 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
     70  1.1.2.2  cjep 		n32[i] = bswap32(o32[i]);
     71  1.1.2.2  cjep 
     72  1.1.2.2  cjep 	n->fs_swuid = bswap64(o->fs_swuid);
     73  1.1.2.2  cjep 	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
     74  1.1.2.2  cjep 	n->fs_old_cpc = bswap32(o->fs_old_cpc);
     75  1.1.2.2  cjep 
     76  1.1.2.2  cjep 	/* These fields overlap with a possible location for the
     77  1.1.2.2  cjep 	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
     78  1.1.2.2  cjep 	 * first half of the historic FS_42POSTBLFMT postbl table.
     79  1.1.2.2  cjep 	 */
     80  1.1.2.2  cjep 	n->fs_maxbsize = bswap32(o->fs_maxbsize);
     81  1.1.2.2  cjep 	/* XXX journal */
     82  1.1.2.2  cjep 	n->fs_quota_magic = bswap32(o->fs_quota_magic);
     83  1.1.2.2  cjep 	for (i = 0; i < MAXQUOTAS; i++)
     84  1.1.2.2  cjep 		n->fs_quotafile[i] = bswap64(o->fs_quotafile[i]);
     85  1.1.2.2  cjep 	n->fs_sblockloc = bswap64(o->fs_sblockloc);
     86  1.1.2.2  cjep 	libsa_ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
     87  1.1.2.2  cjep 	n->fs_time = bswap64(o->fs_time);
     88  1.1.2.2  cjep 	n->fs_size = bswap64(o->fs_size);
     89  1.1.2.2  cjep 	n->fs_dsize = bswap64(o->fs_dsize);
     90  1.1.2.2  cjep 	n->fs_csaddr = bswap64(o->fs_csaddr);
     91  1.1.2.2  cjep 	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
     92  1.1.2.2  cjep 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
     93  1.1.2.2  cjep 
     94  1.1.2.2  cjep 	/* These fields overlap with the second half of the
     95  1.1.2.2  cjep 	 * historic FS_42POSTBLFMT postbl table
     96  1.1.2.2  cjep 	 */
     97  1.1.2.2  cjep 	for (i = 0; i < FSMAXSNAP; i++)
     98  1.1.2.2  cjep 		n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
     99  1.1.2.2  cjep 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
    100  1.1.2.2  cjep 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
    101  1.1.2.2  cjep 	/* fs_sparecon[28] - ignore for now */
    102  1.1.2.2  cjep 	n->fs_flags = bswap32(o->fs_flags);
    103  1.1.2.2  cjep 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
    104  1.1.2.2  cjep 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
    105  1.1.2.2  cjep 	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
    106  1.1.2.2  cjep 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
    107  1.1.2.2  cjep 	n->fs_qbmask = bswap64(o->fs_qbmask);
    108  1.1.2.2  cjep 	n->fs_qfmask = bswap64(o->fs_qfmask);
    109  1.1.2.2  cjep 	n->fs_state = bswap32(o->fs_state);
    110  1.1.2.2  cjep 	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
    111  1.1.2.2  cjep 	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
    112  1.1.2.2  cjep 	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
    113  1.1.2.2  cjep 	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
    114  1.1.2.2  cjep 
    115  1.1.2.2  cjep 	n->fs_magic = bswap32(o->fs_magic);
    116  1.1.2.2  cjep }
    117  1.1.2.2  cjep 
    118  1.1.2.2  cjep void
    119  1.1.2.2  cjep ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
    120  1.1.2.2  cjep {
    121  1.1.2.2  cjep 	size_t i;
    122  1.1.2.2  cjep 
    123  1.1.2.2  cjep 	n->di_mode = bswap16(o->di_mode);
    124  1.1.2.2  cjep 	n->di_nlink = bswap16(o->di_nlink);
    125  1.1.2.2  cjep 	n->di_oldids[0] = bswap16(o->di_oldids[0]);
    126  1.1.2.2  cjep 	n->di_oldids[1] = bswap16(o->di_oldids[1]);
    127  1.1.2.2  cjep 	n->di_size = bswap64(o->di_size);
    128  1.1.2.2  cjep 	n->di_atime = bswap32(o->di_atime);
    129  1.1.2.2  cjep 	n->di_atimensec = bswap32(o->di_atimensec);
    130  1.1.2.2  cjep 	n->di_mtime = bswap32(o->di_mtime);
    131  1.1.2.2  cjep 	n->di_mtimensec = bswap32(o->di_mtimensec);
    132  1.1.2.2  cjep 	n->di_ctime = bswap32(o->di_ctime);
    133  1.1.2.2  cjep 	n->di_ctimensec = bswap32(o->di_ctimensec);
    134  1.1.2.2  cjep 	/* Swap these here, unlike kernel version .*/
    135  1.1.2.2  cjep 	for (i = 0; i < UFS_NDADDR; i++)
    136  1.1.2.2  cjep 		n->di_db[i] = bswap32(o->di_db[i]);
    137  1.1.2.2  cjep 	for (i = 0; i < UFS_NIADDR; i++)
    138  1.1.2.2  cjep 		n->di_ib[i] = bswap32(o->di_ib[i]);
    139  1.1.2.2  cjep 	n->di_flags = bswap32(o->di_flags);
    140  1.1.2.2  cjep 	n->di_blocks = bswap32(o->di_blocks);
    141  1.1.2.2  cjep 	n->di_gen = bswap32(o->di_gen);
    142  1.1.2.2  cjep 	n->di_uid = bswap32(o->di_uid);
    143  1.1.2.2  cjep 	n->di_gid = bswap32(o->di_gid);
    144  1.1.2.2  cjep }
    145  1.1.2.2  cjep 
    146  1.1.2.2  cjep void
    147  1.1.2.2  cjep ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
    148  1.1.2.2  cjep {
    149  1.1.2.2  cjep 	size_t i;
    150  1.1.2.2  cjep 
    151  1.1.2.2  cjep 	n->di_mode = bswap16(o->di_mode);
    152  1.1.2.2  cjep 	n->di_nlink = bswap16(o->di_nlink);
    153  1.1.2.2  cjep 	n->di_uid = bswap32(o->di_uid);
    154  1.1.2.2  cjep 	n->di_gid = bswap32(o->di_gid);
    155  1.1.2.2  cjep 	n->di_blksize = bswap32(o->di_blksize);
    156  1.1.2.2  cjep 	n->di_size = bswap64(o->di_size);
    157  1.1.2.2  cjep 	n->di_blocks = bswap64(o->di_blocks);
    158  1.1.2.2  cjep 	n->di_atime = bswap64(o->di_atime);
    159  1.1.2.2  cjep 	n->di_atimensec = bswap32(o->di_atimensec);
    160  1.1.2.2  cjep 	n->di_mtime = bswap64(o->di_mtime);
    161  1.1.2.2  cjep 	n->di_mtimensec = bswap32(o->di_mtimensec);
    162  1.1.2.2  cjep 	n->di_ctime = bswap64(o->di_ctime);
    163  1.1.2.2  cjep 	n->di_ctimensec = bswap32(o->di_ctimensec);
    164  1.1.2.2  cjep 	n->di_birthtime = bswap64(o->di_birthtime);
    165  1.1.2.2  cjep 	n->di_birthnsec = bswap32(o->di_birthnsec);
    166  1.1.2.2  cjep 	n->di_gen = bswap32(o->di_gen);
    167  1.1.2.2  cjep 	n->di_kernflags = bswap32(o->di_kernflags);
    168  1.1.2.2  cjep 	n->di_flags = bswap32(o->di_flags);
    169  1.1.2.2  cjep 	n->di_extsize = bswap32(o->di_extsize);
    170  1.1.2.2  cjep 	/* Swap these here, unlike kernel version .*/
    171  1.1.2.2  cjep 	for (i = 0; i < UFS_NXADDR; i++)
    172  1.1.2.2  cjep 		n->di_extb[i] = bswap64(o->di_extb[i]);
    173  1.1.2.2  cjep 	for (i = 0; i < UFS_NDADDR; i++)
    174  1.1.2.2  cjep 		n->di_db[i] = bswap64(o->di_db[i]);
    175  1.1.2.2  cjep 	for (i = 0; i < UFS_NIADDR; i++)
    176  1.1.2.2  cjep 		n->di_ib[i] = bswap64(o->di_ib[i]);
    177  1.1.2.2  cjep }
    178