1N/A/*
1N/A libparted - a library for manipulating disk partitions
1N/A Copyright (C) 2001, 2007, 2009-2010 Free Software Foundation, Inc.
1N/A
1N/A This program is free software; you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>.
1N/A
1N/A Contributor: Ben Collins <bcollins@debian.org>
1N/A*/
1N/A
1N/A#include <config.h>
1N/A
1N/A#include <parted/parted.h>
1N/A#include <parted/endian.h>
1N/A#include <parted/debug.h>
1N/A
1N/A#if ENABLE_NLS
1N/A# include <libintl.h>
1N/A# define _(String) dgettext (PACKAGE, String)
1N/A#else
1N/A# define _(String) (String)
1N/A#endif /* ENABLE_NLS */
1N/A
1N/A#include <unistd.h>
1N/A#include <string.h>
1N/A
1N/A#define SUN_UFS_BLOCK_SIZES ((int[2]){512, 0})
1N/A#define HP_UFS_BLOCK_SIZES ((int[2]){512, 0})
1N/A
1N/A
1N/A/* taken from ufs_fs.h in Linux */
1N/A#define UFS_MAXNAMLEN 255
1N/A#define UFS_MAXMNTLEN 512
1N/A#define UFS_MAXCSBUFS 31
1N/A#define UFS_LINK_MAX 32000
1N/A
1N/A#define UFS_MAGIC 0x00011954
1N/A#define UFS_MAGIC_LFN 0x00095014
1N/A#define UFS_MAGIC_FEA 0x00195612
1N/A#define UFS_MAGIC_4GB 0x05231994
1N/A
1N/Astruct ufs_csum {
1N/A uint32_t cs_ndir; /* number of directories */
1N/A uint32_t cs_nbfree; /* number of free blocks */
1N/A uint32_t cs_nifree; /* number of free inodes */
1N/A uint32_t cs_nffree; /* number of free frags */
1N/A};
1N/A
1N/Astruct ufs_super_block {
1N/A uint32_t fs_link; /* UNUSED */
1N/A uint32_t fs_rlink; /* UNUSED */
1N/A uint32_t fs_sblkno; /* addr of super-block in filesys */
1N/A uint32_t fs_cblkno; /* offset of cyl-block in filesys */
1N/A uint32_t fs_iblkno; /* offset of inode-blocks in filesys */
1N/A uint32_t fs_dblkno; /* offset of first data after cg */
1N/A uint32_t fs_cgoffset; /* cylinder group offset in cylinder */
1N/A uint32_t fs_cgmask; /* used to calc mod fs_ntrak */
1N/A uint32_t fs_time; /* last time written -- time_t */
1N/A uint32_t fs_size; /* number of blocks in fs */
1N/A uint32_t fs_dsize; /* number of data blocks in fs */
1N/A uint32_t fs_ncg; /* number of cylinder groups */
1N/A uint32_t fs_bsize; /* size of basic blocks in fs */
1N/A uint32_t fs_fsize; /* size of frag blocks in fs */
1N/A uint32_t fs_frag; /* number of frags in a block in fs */
1N/A/* these are configuration parameters */
1N/A uint32_t fs_minfree; /* minimum percentage of free blocks */
1N/A uint32_t fs_rotdelay; /* num of ms for optimal next block */
1N/A uint32_t fs_rps; /* disk revolutions per second */
1N/A/* these fields can be computed from the others */
1N/A uint32_t fs_bmask; /* ``blkoff'' calc of blk offsets */
1N/A uint32_t fs_fmask; /* ``fragoff'' calc of frag offsets */
1N/A uint32_t fs_bshift; /* ``lblkno'' calc of logical blkno */
1N/A uint32_t fs_fshift; /* ``numfrags'' calc number of frags */
1N/A/* these are configuration parameters */
1N/A uint32_t fs_maxcontig; /* max number of contiguous blks */
1N/A uint32_t fs_maxbpg; /* max number of blks per cyl group */
1N/A/* these fields can be computed from the others */
1N/A uint32_t fs_fragshift; /* block to frag shift */
1N/A uint32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
1N/A uint32_t fs_sbsize; /* actual size of super block */
1N/A uint32_t fs_csmask; /* csum block offset */
1N/A uint32_t fs_csshift; /* csum block number */
1N/A uint32_t fs_nindir; /* value of NINDIR */
1N/A uint32_t fs_inopb; /* value of INOPB */
1N/A uint32_t fs_nspf; /* value of NSPF */
1N/A/* yet another configuration parameter */
1N/A uint32_t fs_optim; /* optimization preference, see below */
1N/A/* these fields are derived from the hardware */
1N/A union {
1N/A struct {
1N/A uint32_t fs_npsect; /* # sectors/track including spares */
1N/A } fs_sun;
1N/A struct {
1N/A int32_t fs_state; /* file system state time stamp */
1N/A } fs_sunx86;
1N/A } fs_u1;
1N/A uint32_t fs_interleave; /* hardware sector interleave */
1N/A uint32_t fs_trackskew; /* sector 0 skew, per track */
1N/A/* a unique id for this file system (currently unused and unmaintained) */
1N/A/* In 4.3 Tahoe this space is used by fs_headswitch and fs_trkseek */
1N/A/* Neither of those fields is used in the Tahoe code right now but */
1N/A/* there could be problems if they are. */
1N/A uint32_t fs_id[2]; /* file system id */
1N/A/* sizes determined by number of cylinder groups and their sizes */
1N/A uint32_t fs_csaddr; /* blk addr of cyl grp summary area */
1N/A uint32_t fs_cssize; /* size of cyl grp summary area */
1N/A uint32_t fs_cgsize; /* cylinder group size */
1N/A/* these fields are derived from the hardware */
1N/A uint32_t fs_ntrak; /* tracks per cylinder */
1N/A uint32_t fs_nsect; /* sectors per track */
1N/A uint32_t fs_spc; /* sectors per cylinder */
1N/A/* this comes from the disk driver partitioning */
1N/A uint32_t fs_ncyl; /* cylinders in file system */
1N/A/* these fields can be computed from the others */
1N/A uint32_t fs_cpg; /* cylinders per group */
1N/A uint32_t fs_ipg; /* inodes per group */
1N/A uint32_t fs_fpg; /* blocks per group * fs_frag */
1N/A/* this data must be re-computed after crashes */
1N/A struct ufs_csum fs_cstotal; /* cylinder summary information */
1N/A/* these fields are cleared at mount time */
1N/A int8_t fs_fmod; /* super block modified flag */
1N/A int8_t fs_clean; /* file system is clean flag */
1N/A int8_t fs_ronly; /* mounted read-only flag */
1N/A int8_t fs_flags; /* currently unused flag */
1N/A int8_t fs_fsmnt[UFS_MAXMNTLEN]; /* name mounted on */
1N/A/* these fields retain the current block allocation info */
1N/A uint32_t fs_cgrotor; /* last cg searched */
1N/A uint32_t fs_csp[UFS_MAXCSBUFS]; /* list of fs_cs info buffers */
1N/A uint32_t fs_maxcluster;
1N/A uint32_t fs_cpc; /* cyl per cycle in postbl */
1N/A uint16_t fs_opostbl[16][8]; /* old rotation block list head */
1N/A union {
1N/A struct {
1N/A int32_t fs_sparecon[53];/* reserved for future constants */
1N/A int32_t fs_reclaim;
1N/A int32_t fs_sparecon2[1];
1N/A int32_t fs_state; /* file system state time stamp */
1N/A uint32_t fs_qbmask[2]; /* ~usb_bmask */
1N/A uint32_t fs_qfmask[2]; /* ~usb_fmask */
1N/A } fs_sun;
1N/A struct {
1N/A int32_t fs_sparecon[53];/* reserved for future constants */
1N/A int32_t fs_reclaim;
1N/A int32_t fs_sparecon2[1];
1N/A uint32_t fs_npsect; /* # sectors/track including spares */
1N/A uint32_t fs_qbmask[2]; /* ~usb_bmask */
1N/A uint32_t fs_qfmask[2]; /* ~usb_fmask */
1N/A } fs_sunx86;
1N/A struct {
1N/A int32_t fs_sparecon[50];/* reserved for future constants */
1N/A int32_t fs_contigsumsize;/* size of cluster summary array */
1N/A int32_t fs_maxsymlinklen;/* max length of an internal symlink */
1N/A int32_t fs_inodefmt; /* format of on-disk inodes */
1N/A uint32_t fs_maxfilesize[2]; /* max representable file size */
1N/A uint32_t fs_qbmask[2]; /* ~usb_bmask */
1N/A uint32_t fs_qfmask[2]; /* ~usb_fmask */
1N/A int32_t fs_state; /* file system state time stamp */
1N/A } fs_44;
1N/A } fs_u2;
1N/A int32_t fs_postblformat; /* format of positional layout tables */
1N/A int32_t fs_nrpos; /* number of rotational positions */
1N/A int32_t fs_postbloff; /* (__s16) rotation block list head */
1N/A int32_t fs_rotbloff; /* (uint8_t) blocks for each rotation */
1N/A int32_t fs_magic; /* magic number */
1N/A uint8_t fs_space[4]; /* list of blocks for each rotation */
1N/A};
1N/A
1N/Astatic PedGeometry*
1N/Aufs_probe_sun (PedGeometry* geom)
1N/A{
1N/A int8_t buf[512 * 3];
1N/A struct ufs_super_block *sb;
1N/A
1N/A if (geom->length < 5)
1N/A return 0;
1N/A if (!ped_geometry_read (geom, buf, 16, 3))
1N/A return 0;
1N/A
1N/A sb = (struct ufs_super_block *)buf;
1N/A
1N/A if (PED_BE32_TO_CPU(sb->fs_magic) == UFS_MAGIC) {
1N/A PedSector block_size = PED_BE32_TO_CPU(sb->fs_bsize) / 512;
1N/A PedSector block_count = PED_BE32_TO_CPU(sb->fs_size);
1N/A return ped_geometry_new (geom->dev, geom->start,
1N/A block_size * block_count);
1N/A }
1N/A if (PED_LE32_TO_CPU(sb->fs_magic) == UFS_MAGIC) {
1N/A PedSector block_size = PED_LE32_TO_CPU(sb->fs_bsize) / 512;
1N/A PedSector block_count = PED_LE32_TO_CPU(sb->fs_size);
1N/A return ped_geometry_new (geom->dev, geom->start,
1N/A block_size * block_count);
1N/A }
1N/A return NULL;
1N/A}
1N/A
1N/Astatic PedGeometry*
1N/Aufs_probe_hp (PedGeometry* geom)
1N/A{
1N/A int8_t buf[1536];
1N/A struct ufs_super_block *sb;
1N/A PedSector block_size;
1N/A PedSector block_count;
1N/A
1N/A if (geom->length < 5)
1N/A return 0;
1N/A if (!ped_geometry_read (geom, buf, 16, 3))
1N/A return 0;
1N/A
1N/A sb = (struct ufs_super_block *)buf;
1N/A
1N/A /* Try sane bytesex */
1N/A switch (PED_BE32_TO_CPU(sb->fs_magic)) {
1N/A case UFS_MAGIC_LFN:
1N/A case UFS_MAGIC_FEA:
1N/A case UFS_MAGIC_4GB:
1N/A block_size = PED_BE32_TO_CPU(sb->fs_bsize) / 512;
1N/A block_count = PED_BE32_TO_CPU(sb->fs_size);
1N/A return ped_geometry_new (geom->dev, geom->start,
1N/A block_size * block_count);
1N/A }
1N/A
1N/A /* Try perverted bytesex */
1N/A switch (PED_LE32_TO_CPU(sb->fs_magic)) {
1N/A case UFS_MAGIC_LFN:
1N/A case UFS_MAGIC_FEA:
1N/A case UFS_MAGIC_4GB:
1N/A block_size = PED_LE32_TO_CPU(sb->fs_bsize) / 512;
1N/A block_count = PED_LE32_TO_CPU(sb->fs_size);
1N/A return ped_geometry_new (geom->dev, geom->start,
1N/A block_size * block_count);
1N/A }
1N/A return NULL;
1N/A}
1N/A
1N/A#ifndef DISCOVER_ONLY
1N/Astatic int
1N/Aufs_clobber (PedGeometry* geom)
1N/A{
1N/A char buf[1536];
1N/A
1N/A if (!ped_geometry_read (geom, buf, 16, 3))
1N/A return 0;
1N/A
1N/A memset (buf, 0, sizeof(struct ufs_super_block));
1N/A
1N/A return ped_geometry_write (geom, buf, 16, 3);
1N/A}
1N/A#endif /* !DISCOVER_ONLY */
1N/A
1N/Astatic PedFileSystemOps ufs_ops_sun = {
1N/A .probe = ufs_probe_sun,
1N/A#ifndef DISCOVER_ONLY
1N/A .clobber = ufs_clobber,
1N/A#else
1N/A .clobber = NULL,
1N/A#endif
1N/A .open = NULL,
1N/A .create = NULL,
1N/A .close = NULL,
1N/A .check = NULL,
1N/A .copy = NULL,
1N/A .resize = NULL,
1N/A .get_create_constraint = NULL,
1N/A .get_resize_constraint = NULL,
1N/A .get_copy_constraint = NULL
1N/A};
1N/A
1N/Astatic PedFileSystemOps ufs_ops_hp = {
1N/A .probe = ufs_probe_hp,
1N/A#ifndef DISCOVER_ONLY
1N/A .clobber = ufs_clobber,
1N/A#else
1N/A .clobber = NULL,
1N/A#endif
1N/A .open = NULL,
1N/A .create = NULL,
1N/A .close = NULL,
1N/A .check = NULL,
1N/A .copy = NULL,
1N/A .resize = NULL,
1N/A .get_create_constraint = NULL,
1N/A .get_resize_constraint = NULL,
1N/A .get_copy_constraint = NULL
1N/A};
1N/A
1N/Astatic PedFileSystemType ufs_type_sun = {
1N/A .next = NULL,
1N/A .ops = &ufs_ops_sun,
1N/A .name = "sun-ufs",
1N/A .block_sizes = SUN_UFS_BLOCK_SIZES
1N/A};
1N/A
1N/Astatic PedFileSystemType ufs_type_hp = {
1N/A .next = NULL,
1N/A .ops = &ufs_ops_hp,
1N/A .name = "hp-ufs",
1N/A .block_sizes = HP_UFS_BLOCK_SIZES
1N/A};
1N/A
1N/Avoid
1N/Aped_file_system_ufs_init ()
1N/A{
1N/A PED_ASSERT (sizeof (struct ufs_super_block) == 1380, return);
1N/A
1N/A ped_file_system_type_register (&ufs_type_sun);
1N/A ped_file_system_type_register (&ufs_type_hp);
1N/A}
1N/A
1N/Avoid
1N/Aped_file_system_ufs_done ()
1N/A{
1N/A ped_file_system_type_unregister (&ufs_type_hp);
1N/A ped_file_system_type_unregister (&ufs_type_sun);
1N/A}