2017-07-19 16:31:08 +01:00
|
|
|
|
// /***************************************************************************
|
2016-09-02 20:05:55 +01:00
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : XFS.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : XFS filesystem plugin.
|
2016-09-02 20:05:55 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Identifies the XFS filesystem and shows information.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-09-02 20:05:55 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
2017-07-19 16:31:08 +01:00
|
|
|
|
using DiscImageChef.CommonTypes;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
|
|
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
using DiscImageChef.Console;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using Schemas;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filesystems
|
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
public class XFS : IFilesystem
|
2016-09-02 20:05:55 +01:00
|
|
|
|
{
|
2017-12-22 08:43:22 +00:00
|
|
|
|
const uint XFS_MAGIC = 0x58465342;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-12-26 08:01:40 +00:00
|
|
|
|
public FileSystemType XmlFsType { get; private set; }
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public Encoding Encoding { get; private set; }
|
|
|
|
|
|
public string Name => "XFS Filesystem Plugin";
|
|
|
|
|
|
public Guid Id => new Guid("1D8CD8B8-27E6-410F-9973-D16409225FBA");
|
2018-08-29 22:15:43 +01:00
|
|
|
|
public string Author => "Natalia Portillo";
|
2017-10-12 23:54:02 +01:00
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(IMediaImage imagePlugin, Partition partition)
|
2016-09-02 20:05:55 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(imagePlugin.Info.SectorSize < 512) return false;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-08-06 15:46:59 +01:00
|
|
|
|
// Misaligned
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
|
2017-08-06 15:46:59 +01:00
|
|
|
|
{
|
|
|
|
|
|
XFS_Superblock xfsSb = new XFS_Superblock();
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
uint sbSize = (uint)((Marshal.SizeOf(xfsSb) + 0x400) / imagePlugin.Info.SectorSize);
|
|
|
|
|
|
if((Marshal.SizeOf(xfsSb) + 0x400) % imagePlugin.Info.SectorSize != 0) sbSize++;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-08-09 04:46:14 +01:00
|
|
|
|
byte[] sector = imagePlugin.ReadSectors(partition.Start, sbSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(sector.Length < Marshal.SizeOf(xfsSb)) return false;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
byte[] sbpiece = new byte[Marshal.SizeOf(xfsSb)];
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(int location in new[] {0, 0x200, 0x400})
|
2017-08-06 15:46:59 +01:00
|
|
|
|
{
|
|
|
|
|
|
Array.Copy(sector, location, sbpiece, 0, Marshal.SizeOf(xfsSb));
|
|
|
|
|
|
|
|
|
|
|
|
xfsSb = BigEndianMarshal.ByteArrayToStructureBigEndian<XFS_Superblock>(sbpiece);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("XFS plugin", "magic at 0x{0:X3} = 0x{1:X8} (expected 0x{2:X8})",
|
2017-12-22 08:43:22 +00:00
|
|
|
|
location, xfsSb.magicnum, XFS_MAGIC);
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
if(xfsSb.magicnum == XFS_MAGIC) return true;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2018-06-20 22:22:21 +01:00
|
|
|
|
foreach(int i in new[] {0, 1, 2})
|
2017-08-06 15:46:59 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
ulong location = (ulong)i;
|
|
|
|
|
|
XFS_Superblock xfsSb = new XFS_Superblock();
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
uint sbSize = (uint)(Marshal.SizeOf(xfsSb) / imagePlugin.Info.SectorSize);
|
|
|
|
|
|
if(Marshal.SizeOf(xfsSb) % imagePlugin.Info.SectorSize != 0) sbSize++;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
byte[] sector = imagePlugin.ReadSectors(partition.Start + location, sbSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(sector.Length < Marshal.SizeOf(xfsSb)) return false;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
xfsSb = BigEndianMarshal.ByteArrayToStructureBigEndian<XFS_Superblock>(sector);
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("XFS plugin", "magic at {0} = 0x{1:X8} (expected 0x{2:X8})", location,
|
2017-12-22 08:43:22 +00:00
|
|
|
|
xfsSb.magicnum, XFS_MAGIC);
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
if(xfsSb.magicnum == XFS_MAGIC) return true;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 08:01:40 +00:00
|
|
|
|
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Encoding encoding)
|
2016-09-02 20:05:55 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
|
2016-09-02 20:05:55 +01:00
|
|
|
|
information = "";
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(imagePlugin.Info.SectorSize < 512) return;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
|
|
|
|
|
XFS_Superblock xfsSb = new XFS_Superblock();
|
|
|
|
|
|
|
2017-08-06 15:46:59 +01:00
|
|
|
|
// Misaligned
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(imagePlugin.Info.XmlMediaType == XmlMediaType.OpticalDisc)
|
2017-08-06 15:46:59 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
uint sbSize = (uint)((Marshal.SizeOf(xfsSb) + 0x400) / imagePlugin.Info.SectorSize);
|
|
|
|
|
|
if((Marshal.SizeOf(xfsSb) + 0x400) % imagePlugin.Info.SectorSize != 0) sbSize++;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-08-09 04:46:14 +01:00
|
|
|
|
byte[] sector = imagePlugin.ReadSectors(partition.Start, sbSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(sector.Length < Marshal.SizeOf(xfsSb)) return;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
byte[] sbpiece = new byte[Marshal.SizeOf(xfsSb)];
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(int location in new[] {0, 0x200, 0x400})
|
2017-08-06 15:46:59 +01:00
|
|
|
|
{
|
|
|
|
|
|
Array.Copy(sector, location, sbpiece, 0, Marshal.SizeOf(xfsSb));
|
|
|
|
|
|
|
|
|
|
|
|
xfsSb = BigEndianMarshal.ByteArrayToStructureBigEndian<XFS_Superblock>(sbpiece);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("XFS plugin", "magic at 0x{0:X3} = 0x{1:X8} (expected 0x{2:X8})",
|
2017-12-22 08:43:22 +00:00
|
|
|
|
location, xfsSb.magicnum, XFS_MAGIC);
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
if(xfsSb.magicnum == XFS_MAGIC) break;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2018-06-20 22:22:21 +01:00
|
|
|
|
foreach(int i in new[] {0, 1, 2})
|
2017-08-06 15:46:59 +01:00
|
|
|
|
{
|
2018-06-20 22:22:21 +01:00
|
|
|
|
ulong location = (ulong)i;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
uint sbSize = (uint)(Marshal.SizeOf(xfsSb) / imagePlugin.Info.SectorSize);
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(Marshal.SizeOf(xfsSb) % imagePlugin.Info.SectorSize != 0) sbSize++;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
byte[] sector = imagePlugin.ReadSectors(partition.Start + location, sbSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(sector.Length < Marshal.SizeOf(xfsSb)) return;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
xfsSb = BigEndianMarshal.ByteArrayToStructureBigEndian<XFS_Superblock>(sector);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DicConsole.DebugWriteLine("XFS plugin", "magic at {0} = 0x{1:X8} (expected 0x{2:X8})", location,
|
2017-12-22 08:43:22 +00:00
|
|
|
|
xfsSb.magicnum, XFS_MAGIC);
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
if(xfsSb.magicnum == XFS_MAGIC) break;
|
2017-08-06 15:46:59 +01:00
|
|
|
|
}
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
if(xfsSb.magicnum != XFS_MAGIC) return;
|
2016-09-02 20:05:55 +01:00
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
sb.AppendLine("XFS filesystem");
|
|
|
|
|
|
sb.AppendFormat("Filesystem version {0}", xfsSb.version & 0xF).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} bytes per sector", xfsSb.sectsize).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} bytes per block", xfsSb.blocksize).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} bytes per inode", xfsSb.inodesize).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} data blocks in volume, {1} free", xfsSb.dblocks, xfsSb.fdblocks).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} blocks per allocation group", xfsSb.agblocks).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} allocation groups in volume", xfsSb.agcount).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("{0} inodes in volume, {1} free", xfsSb.icount, xfsSb.ifree).AppendLine();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(xfsSb.inprogress > 0) sb.AppendLine("fsck in progress");
|
2017-12-26 08:01:40 +00:00
|
|
|
|
sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(xfsSb.fname, Encoding)).AppendLine();
|
2016-09-02 20:05:55 +01:00
|
|
|
|
sb.AppendFormat("Volume UUID: {0}", xfsSb.uuid).AppendLine();
|
|
|
|
|
|
|
|
|
|
|
|
information = sb.ToString();
|
|
|
|
|
|
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType = new FileSystemType
|
2017-12-22 08:43:22 +00:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Type = "XFS filesystem",
|
|
|
|
|
|
ClusterSize = (int)xfsSb.blocksize,
|
|
|
|
|
|
Clusters = (long)xfsSb.dblocks,
|
|
|
|
|
|
FreeClusters = (long)xfsSb.fdblocks,
|
2017-12-22 08:43:22 +00:00
|
|
|
|
FreeClustersSpecified = true,
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Files = (long)(xfsSb.icount - xfsSb.ifree),
|
|
|
|
|
|
FilesSpecified = true,
|
|
|
|
|
|
Dirty = xfsSb.inprogress > 0,
|
|
|
|
|
|
VolumeName = StringHandlers.CToString(xfsSb.fname, Encoding),
|
|
|
|
|
|
VolumeSerial = xfsSb.uuid.ToString()
|
2017-12-22 08:43:22 +00:00
|
|
|
|
};
|
2016-09-02 20:05:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-24 02:37:41 +00:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
struct XFS_Superblock
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public uint magicnum;
|
|
|
|
|
|
public uint blocksize;
|
|
|
|
|
|
public ulong dblocks;
|
|
|
|
|
|
public ulong rblocks;
|
|
|
|
|
|
public ulong rextents;
|
|
|
|
|
|
public Guid uuid;
|
|
|
|
|
|
public ulong logstat;
|
|
|
|
|
|
public ulong rootino;
|
|
|
|
|
|
public ulong rbmino;
|
|
|
|
|
|
public ulong rsumino;
|
|
|
|
|
|
public uint rextsize;
|
|
|
|
|
|
public uint agblocks;
|
|
|
|
|
|
public uint agcount;
|
|
|
|
|
|
public uint rbmblocks;
|
|
|
|
|
|
public uint logblocks;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
public ushort version;
|
|
|
|
|
|
public ushort sectsize;
|
|
|
|
|
|
public ushort inodesize;
|
|
|
|
|
|
public ushort inopblock;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
|
|
|
|
|
|
public byte[] fname;
|
|
|
|
|
|
public byte blocklog;
|
|
|
|
|
|
public byte sectlog;
|
|
|
|
|
|
public byte inodelog;
|
|
|
|
|
|
public byte inopblog;
|
|
|
|
|
|
public byte agblklog;
|
|
|
|
|
|
public byte rextslog;
|
|
|
|
|
|
public byte inprogress;
|
|
|
|
|
|
public byte imax_pct;
|
|
|
|
|
|
public ulong icount;
|
|
|
|
|
|
public ulong ifree;
|
|
|
|
|
|
public ulong fdblocks;
|
|
|
|
|
|
public ulong frextents;
|
|
|
|
|
|
public ulong uquotino;
|
|
|
|
|
|
public ulong gquotino;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
public ushort qflags;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public byte flags;
|
|
|
|
|
|
public byte shared_vn;
|
|
|
|
|
|
public ulong inoalignmt;
|
|
|
|
|
|
public ulong unit;
|
|
|
|
|
|
public ulong width;
|
|
|
|
|
|
public byte dirblklog;
|
|
|
|
|
|
public byte logsectlog;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
public ushort logsectsize;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public uint logsunit;
|
|
|
|
|
|
public uint features2;
|
|
|
|
|
|
public uint bad_features2;
|
|
|
|
|
|
public uint features_compat;
|
|
|
|
|
|
public uint features_ro_compat;
|
|
|
|
|
|
public uint features_incompat;
|
|
|
|
|
|
public uint features_log_incompat;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
// This field is little-endian while rest of superblock is big-endian
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public uint crc;
|
|
|
|
|
|
public uint spino_align;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
public ulong pquotino;
|
|
|
|
|
|
public ulong lsn;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public Guid meta_uuid;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
}
|
2016-09-02 20:05:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|