2N/A/* xfs.c - XFS. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/file.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/types.h>
2N/A#include <grub/fshelp.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define XFS_INODE_EXTENTS 9
2N/A
2N/A#define XFS_INODE_FORMAT_INO 1
2N/A#define XFS_INODE_FORMAT_EXT 2
2N/A#define XFS_INODE_FORMAT_BTREE 3
2N/A
2N/A
2N/Astruct grub_xfs_sblock
2N/A{
2N/A grub_uint8_t magic[4];
2N/A grub_uint32_t bsize;
2N/A grub_uint8_t unused1[24];
2N/A grub_uint16_t uuid[8];
2N/A grub_uint8_t unused2[8];
2N/A grub_uint64_t rootino;
2N/A grub_uint8_t unused3[20];
2N/A grub_uint32_t agsize;
2N/A grub_uint8_t unused4[20];
2N/A grub_uint8_t label[12];
2N/A grub_uint8_t log2_bsize;
2N/A grub_uint8_t log2_sect;
2N/A grub_uint8_t log2_inode;
2N/A grub_uint8_t log2_inop;
2N/A grub_uint8_t log2_agblk;
2N/A grub_uint8_t unused6[67];
2N/A grub_uint8_t log2_dirblk;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_dir_header
2N/A{
2N/A grub_uint8_t count;
2N/A grub_uint8_t smallino;
2N/A union
2N/A {
2N/A grub_uint32_t i4;
2N/A grub_uint64_t i8;
2N/A } parent __attribute__ ((packed));
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_dir_entry
2N/A{
2N/A grub_uint8_t len;
2N/A grub_uint16_t offset;
2N/A char name[1];
2N/A /* Inode number follows, 32 bits. */
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_dir2_entry
2N/A{
2N/A grub_uint64_t inode;
2N/A grub_uint8_t len;
2N/A} __attribute__ ((packed));
2N/A
2N/Atypedef grub_uint32_t grub_xfs_extent[4];
2N/A
2N/Astruct grub_xfs_btree_node
2N/A{
2N/A grub_uint8_t magic[4];
2N/A grub_uint16_t level;
2N/A grub_uint16_t numrecs;
2N/A grub_uint64_t left;
2N/A grub_uint64_t right;
2N/A grub_uint64_t keys[1];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_btree_root
2N/A{
2N/A grub_uint16_t level;
2N/A grub_uint16_t numrecs;
2N/A grub_uint64_t keys[1];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_time
2N/A{
2N/A grub_uint32_t sec;
2N/A grub_uint32_t nanosec;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_inode
2N/A{
2N/A grub_uint8_t magic[2];
2N/A grub_uint16_t mode;
2N/A grub_uint8_t version;
2N/A grub_uint8_t format;
2N/A grub_uint8_t unused2[26];
2N/A struct grub_xfs_time atime;
2N/A struct grub_xfs_time mtime;
2N/A struct grub_xfs_time ctime;
2N/A grub_uint64_t size;
2N/A grub_uint64_t nblocks;
2N/A grub_uint32_t extsize;
2N/A grub_uint32_t nextents;
2N/A grub_uint16_t unused3;
2N/A grub_uint8_t fork_offset;
2N/A grub_uint8_t unused4[17];
2N/A union
2N/A {
2N/A char raw[156];
2N/A struct dir
2N/A {
2N/A struct grub_xfs_dir_header dirhead;
2N/A struct grub_xfs_dir_entry direntry[1];
2N/A } dir;
2N/A grub_xfs_extent extents[XFS_INODE_EXTENTS];
2N/A struct grub_xfs_btree_root btree;
2N/A } data __attribute__ ((packed));
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_xfs_dirblock_tail
2N/A{
2N/A grub_uint32_t leaf_count;
2N/A grub_uint32_t leaf_stale;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_fshelp_node
2N/A{
2N/A struct grub_xfs_data *data;
2N/A grub_uint64_t ino;
2N/A int inode_read;
2N/A struct grub_xfs_inode inode;
2N/A};
2N/A
2N/Astruct grub_xfs_data
2N/A{
2N/A struct grub_xfs_sblock sblock;
2N/A grub_disk_t disk;
2N/A int pos;
2N/A int bsize;
2N/A grub_uint32_t agsize;
2N/A struct grub_fshelp_node diropen;
2N/A};
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/A
2N/A
2N/A/* Filetype information as used in inodes. */
2N/A#define FILETYPE_INO_MASK 0170000
2N/A#define FILETYPE_INO_REG 0100000
2N/A#define FILETYPE_INO_DIRECTORY 0040000
2N/A#define FILETYPE_INO_SYMLINK 0120000
2N/A
2N/Astatic inline int
2N/AGRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
2N/A{
2N/A return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
2N/A}
2N/A
2N/Astatic inline grub_uint64_t
2N/AGRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
2N/A grub_uint64_t ino)
2N/A{
2N/A return (grub_be_to_cpu64 (ino) & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
2N/A}
2N/A
2N/Astatic inline grub_uint64_t
2N/AGRUB_XFS_INO_AG (struct grub_xfs_data *data,
2N/A grub_uint64_t ino)
2N/A{
2N/A return (grub_be_to_cpu64 (ino) >> GRUB_XFS_INO_AGBITS (data));
2N/A}
2N/A
2N/Astatic inline grub_disk_addr_t
2N/AGRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
2N/A{
2N/A return ((fsb >> data->sblock.log2_agblk) * data->agsize
2N/A + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
2N/A}
2N/A
2N/Astatic inline grub_uint64_t
2N/AGRUB_XFS_EXTENT_OFFSET (grub_xfs_extent *exts, int ex)
2N/A{
2N/A return ((grub_be_to_cpu32 (exts[ex][0]) & ~(1 << 31)) << 23
2N/A | grub_be_to_cpu32 (exts[ex][1]) >> 9);
2N/A}
2N/A
2N/Astatic inline grub_uint64_t
2N/AGRUB_XFS_EXTENT_BLOCK (grub_xfs_extent *exts, int ex)
2N/A{
2N/A return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex][1])
2N/A & (0x1ff)) << 43
2N/A | (grub_uint64_t) grub_be_to_cpu32 (exts[ex][2]) << 11
2N/A | grub_be_to_cpu32 (exts[ex][3]) >> 21);
2N/A}
2N/A
2N/Astatic inline grub_uint64_t
2N/AGRUB_XFS_EXTENT_SIZE (grub_xfs_extent *exts, int ex)
2N/A{
2N/A return (grub_be_to_cpu32 (exts[ex][3]) & ((1 << 20) - 1));
2N/A}
2N/A
2N/Astatic inline int
2N/AGRUB_XFS_ROUND_TO_DIRENT (int pos)
2N/A{
2N/A return ((((pos) + 8 - 1) / 8) * 8);
2N/A}
2N/A
2N/Astatic inline int
2N/AGRUB_XFS_NEXT_DIRENT (int pos, int len)
2N/A{
2N/A return (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2);
2N/A}
2N/A
2N/A
2N/Astatic inline grub_uint64_t
2N/Agrub_xfs_inode_block (struct grub_xfs_data *data,
2N/A grub_uint64_t ino)
2N/A{
2N/A long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
2N/A long long ag = GRUB_XFS_INO_AG (data, ino);
2N/A long long block;
2N/A
2N/A block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
2N/A block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
2N/A return block;
2N/A}
2N/A
2N/A
2N/Astatic inline int
2N/Agrub_xfs_inode_offset (struct grub_xfs_data *data,
2N/A grub_uint64_t ino)
2N/A{
2N/A int inoag = GRUB_XFS_INO_INOINAG (data, ino);
2N/A return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
2N/A data->sblock.log2_inode);
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
2N/A struct grub_xfs_inode *inode)
2N/A{
2N/A grub_uint64_t block = grub_xfs_inode_block (data, ino);
2N/A int offset = grub_xfs_inode_offset (data, ino);
2N/A
2N/A /* Read the inode. */
2N/A if (grub_disk_read (data->disk, block, offset,
2N/A 1 << data->sblock.log2_inode, inode))
2N/A return grub_errno;
2N/A
2N/A if (grub_strncmp ((char *) inode->magic, "IN", 2))
2N/A return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic grub_disk_addr_t
2N/Agrub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
2N/A{
2N/A struct grub_xfs_btree_node *leaf = 0;
2N/A int ex, nrec;
2N/A grub_xfs_extent *exts;
2N/A grub_uint64_t ret = 0;
2N/A
2N/A if (node->inode.format == XFS_INODE_FORMAT_BTREE)
2N/A {
2N/A grub_uint64_t *keys;
2N/A int recoffset;
2N/A
2N/A leaf = grub_malloc (node->data->bsize);
2N/A if (leaf == 0)
2N/A return 0;
2N/A
2N/A nrec = grub_be_to_cpu16 (node->inode.data.btree.numrecs);
2N/A keys = &node->inode.data.btree.keys[0];
2N/A if (node->inode.fork_offset)
2N/A recoffset = (node->inode.fork_offset
2N/A - ((char *) &node->inode.data.btree.keys - (char *) &node->inode))
2N/A / (2 * sizeof (grub_uint64_t));
2N/A else
2N/A recoffset = ((1 << node->data->sblock.log2_inode)
2N/A - ((char *) &node->inode.data.btree.keys
2N/A - (char *) &node->inode))
2N/A / (2 * sizeof (grub_uint64_t));
2N/A do
2N/A {
2N/A int i;
2N/A
2N/A for (i = 0; i < nrec; i++)
2N/A {
2N/A if (fileblock < grub_be_to_cpu64 (keys[i]))
2N/A break;
2N/A }
2N/A
2N/A /* Sparse block. */
2N/A if (i == 0)
2N/A {
2N/A grub_free (leaf);
2N/A return 0;
2N/A }
2N/A if (grub_disk_read (node->data->disk,
2N/A GRUB_XFS_FSB_TO_BLOCK (node->data, grub_be_to_cpu64 (keys[i - 1 + recoffset])) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
2N/A 0, node->data->bsize, leaf))
2N/A return 0;
2N/A
2N/A if (grub_strncmp ((char *) leaf->magic, "BMAP", 4))
2N/A {
2N/A grub_free (leaf);
2N/A grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
2N/A return 0;
2N/A }
2N/A
2N/A nrec = grub_be_to_cpu16 (leaf->numrecs);
2N/A keys = &leaf->keys[0];
2N/A recoffset = ((node->data->bsize - ((char *) &leaf->keys
2N/A - (char *) leaf))
2N/A / (2 * sizeof (grub_uint64_t)));
2N/A }
2N/A while (leaf->level);
2N/A exts = (grub_xfs_extent *) keys;
2N/A }
2N/A else if (node->inode.format == XFS_INODE_FORMAT_EXT)
2N/A {
2N/A nrec = grub_be_to_cpu32 (node->inode.nextents);
2N/A exts = &node->inode.data.extents[0];
2N/A }
2N/A else
2N/A {
2N/A grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "XFS does not support inode format %d yet",
2N/A node->inode.format);
2N/A return 0;
2N/A }
2N/A
2N/A /* Iterate over each extent to figure out which extent has
2N/A the block we are looking for. */
2N/A for (ex = 0; ex < nrec; ex++)
2N/A {
2N/A grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
2N/A grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
2N/A grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
2N/A
2N/A /* Sparse block. */
2N/A if (fileblock < offset)
2N/A break;
2N/A else if (fileblock < offset + size)
2N/A {
2N/A ret = (fileblock - offset + start);
2N/A break;
2N/A }
2N/A }
2N/A
2N/A grub_free (leaf);
2N/A
2N/A return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
2N/A}
2N/A
2N/A
2N/A/* Read LEN bytes from the file described by DATA starting with byte
2N/A POS. Return the amount of read bytes in READ. */
2N/Astatic grub_ssize_t
2N/Agrub_xfs_read_file (grub_fshelp_node_t node,
2N/A void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
2N/A unsigned offset, unsigned length),
2N/A grub_off_t pos, grub_size_t len, char *buf)
2N/A{
2N/A return grub_fshelp_read_file (node->data->disk, node, read_hook,
2N/A pos, len, buf, grub_xfs_read_block,
2N/A grub_be_to_cpu64 (node->inode.size),
2N/A node->data->sblock.log2_bsize
2N/A - GRUB_DISK_SECTOR_BITS);
2N/A}
2N/A
2N/A
2N/Astatic char *
2N/Agrub_xfs_read_symlink (grub_fshelp_node_t node)
2N/A{
2N/A int size = grub_be_to_cpu64 (node->inode.size);
2N/A
2N/A switch (node->inode.format)
2N/A {
2N/A case XFS_INODE_FORMAT_INO:
2N/A return grub_strndup (node->inode.data.raw, size);
2N/A
2N/A case XFS_INODE_FORMAT_EXT:
2N/A {
2N/A char *symlink;
2N/A grub_ssize_t numread;
2N/A
2N/A symlink = grub_malloc (size + 1);
2N/A if (!symlink)
2N/A return 0;
2N/A
2N/A numread = grub_xfs_read_file (node, 0, 0, size, symlink);
2N/A if (numread != size)
2N/A {
2N/A grub_free (symlink);
2N/A return 0;
2N/A }
2N/A symlink[size] = '\0';
2N/A return symlink;
2N/A }
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic enum grub_fshelp_filetype
2N/Agrub_xfs_mode_to_filetype (grub_uint16_t mode)
2N/A{
2N/A if ((grub_be_to_cpu16 (mode)
2N/A & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
2N/A return GRUB_FSHELP_DIR;
2N/A else if ((grub_be_to_cpu16 (mode)
2N/A & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
2N/A return GRUB_FSHELP_SYMLINK;
2N/A else if ((grub_be_to_cpu16 (mode)
2N/A & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
2N/A return GRUB_FSHELP_REG;
2N/A return GRUB_FSHELP_UNKNOWN;
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Agrub_xfs_iterate_dir (grub_fshelp_node_t dir,
2N/A int NESTED_FUNC_ATTR
2N/A (*hook) (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node))
2N/A{
2N/A struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
2N/A auto int NESTED_FUNC_ATTR call_hook (grub_uint64_t ino, const char *filename);
2N/A
2N/A int NESTED_FUNC_ATTR call_hook (grub_uint64_t ino, const char *filename)
2N/A {
2N/A struct grub_fshelp_node *fdiro;
2N/A
2N/A fdiro = grub_malloc (sizeof (struct grub_fshelp_node)
2N/A - sizeof (struct grub_xfs_inode)
2N/A + (1 << diro->data->sblock.log2_inode));
2N/A if (!fdiro)
2N/A return 0;
2N/A
2N/A /* The inode should be read, otherwise the filetype can
2N/A not be determined. */
2N/A fdiro->ino = ino;
2N/A fdiro->inode_read = 1;
2N/A fdiro->data = diro->data;
2N/A grub_xfs_read_inode (diro->data, ino, &fdiro->inode);
2N/A
2N/A return hook (filename,
2N/A grub_xfs_mode_to_filetype (fdiro->inode.mode),
2N/A fdiro);
2N/A }
2N/A
2N/A switch (diro->inode.format)
2N/A {
2N/A case XFS_INODE_FORMAT_INO:
2N/A {
2N/A struct grub_xfs_dir_entry *de = &diro->inode.data.dir.direntry[0];
2N/A int smallino = !diro->inode.data.dir.dirhead.smallino;
2N/A int i;
2N/A grub_uint64_t parent;
2N/A
2N/A /* If small inode numbers are used to pack the direntry, the
2N/A parent inode number is small too. */
2N/A if (smallino)
2N/A {
2N/A parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent.i4);
2N/A parent = grub_cpu_to_be64 (parent);
2N/A /* The header is a bit smaller than usual. */
2N/A de = (struct grub_xfs_dir_entry *) ((char *) de - 4);
2N/A }
2N/A else
2N/A {
2N/A parent = diro->inode.data.dir.dirhead.parent.i8;
2N/A }
2N/A
2N/A /* Synthesize the direntries for `.' and `..'. */
2N/A if (call_hook (diro->ino, "."))
2N/A return 1;
2N/A
2N/A if (call_hook (parent, ".."))
2N/A return 1;
2N/A
2N/A for (i = 0; i < diro->inode.data.dir.dirhead.count; i++)
2N/A {
2N/A grub_uint64_t ino;
2N/A grub_uint8_t *inopos = (((grub_uint8_t *) de)
2N/A + sizeof (struct grub_xfs_dir_entry)
2N/A + de->len - 1);
2N/A char name[de->len + 1];
2N/A
2N/A /* inopos might be unaligned. */
2N/A if (smallino)
2N/A ino = (((grub_uint32_t) inopos[0]) << 24)
2N/A | (((grub_uint32_t) inopos[1]) << 16)
2N/A | (((grub_uint32_t) inopos[2]) << 8)
2N/A | (((grub_uint32_t) inopos[3]) << 0);
2N/A else
2N/A ino = (((grub_uint64_t) inopos[0]) << 56)
2N/A | (((grub_uint64_t) inopos[1]) << 48)
2N/A | (((grub_uint64_t) inopos[2]) << 40)
2N/A | (((grub_uint64_t) inopos[3]) << 32)
2N/A | (((grub_uint64_t) inopos[4]) << 24)
2N/A | (((grub_uint64_t) inopos[5]) << 16)
2N/A | (((grub_uint64_t) inopos[6]) << 8)
2N/A | (((grub_uint64_t) inopos[7]) << 0);
2N/A ino = grub_cpu_to_be64 (ino);
2N/A
2N/A grub_memcpy (name, de->name, de->len);
2N/A name[de->len] = '\0';
2N/A if (call_hook (ino, name))
2N/A return 1;
2N/A
2N/A de = ((struct grub_xfs_dir_entry *)
2N/A (((char *) de)+ sizeof (struct grub_xfs_dir_entry) + de->len
2N/A + ((smallino ? sizeof (grub_uint32_t)
2N/A : sizeof (grub_uint64_t))) - 1));
2N/A }
2N/A break;
2N/A }
2N/A
2N/A case XFS_INODE_FORMAT_BTREE:
2N/A case XFS_INODE_FORMAT_EXT:
2N/A {
2N/A grub_ssize_t numread;
2N/A char *dirblock;
2N/A grub_uint64_t blk;
2N/A int dirblk_size, dirblk_log2;
2N/A
2N/A dirblk_log2 = (dir->data->sblock.log2_bsize
2N/A + dir->data->sblock.log2_dirblk);
2N/A dirblk_size = 1 << dirblk_log2;
2N/A
2N/A dirblock = grub_malloc (dirblk_size);
2N/A if (! dirblock)
2N/A return 0;
2N/A
2N/A /* Iterate over every block the directory has. */
2N/A for (blk = 0;
2N/A blk < (grub_be_to_cpu64 (dir->inode.size)
2N/A >> dirblk_log2);
2N/A blk++)
2N/A {
2N/A /* The header is skipped, the first direntry is stored
2N/A from byte 16. */
2N/A int pos = 16;
2N/A int entries;
2N/A int tail_start = (dirblk_size
2N/A - sizeof (struct grub_xfs_dirblock_tail));
2N/A
2N/A struct grub_xfs_dirblock_tail *tail;
2N/A tail = (struct grub_xfs_dirblock_tail *) &dirblock[tail_start];
2N/A
2N/A numread = grub_xfs_read_file (dir, 0,
2N/A blk << dirblk_log2,
2N/A dirblk_size, dirblock);
2N/A if (numread != dirblk_size)
2N/A return 0;
2N/A
2N/A entries = (grub_be_to_cpu32 (tail->leaf_count)
2N/A - grub_be_to_cpu32 (tail->leaf_stale));
2N/A
2N/A /* Iterate over all entries within this block. */
2N/A while (pos < (dirblk_size
2N/A - (int) sizeof (struct grub_xfs_dir2_entry)))
2N/A {
2N/A struct grub_xfs_dir2_entry *direntry;
2N/A grub_uint8_t *freetag;
2N/A char *filename;
2N/A
2N/A direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos];
2N/A freetag = (grub_uint8_t *) direntry;
2N/A
2N/A if (grub_get_unaligned16 (freetag) == 0XFFFF)
2N/A {
2N/A grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
2N/A
2N/A /* This entry is not used, go to the next one. */
2N/A pos += grub_be_to_cpu16 (grub_get_unaligned16 (skip));
2N/A
2N/A continue;
2N/A }
2N/A
2N/A filename = &dirblock[pos + sizeof (*direntry)];
2N/A /* The byte after the filename is for the tag, which
2N/A is not used by GRUB. So it can be overwritten. */
2N/A filename[direntry->len] = '\0';
2N/A
2N/A if (call_hook (direntry->inode, filename))
2N/A {
2N/A grub_free (dirblock);
2N/A return 1;
2N/A }
2N/A
2N/A /* Check if last direntry in this block is
2N/A reached. */
2N/A entries--;
2N/A if (!entries)
2N/A break;
2N/A
2N/A /* Select the next directory entry. */
2N/A pos = GRUB_XFS_NEXT_DIRENT (pos, direntry->len);
2N/A pos = GRUB_XFS_ROUND_TO_DIRENT (pos);
2N/A }
2N/A }
2N/A grub_free (dirblock);
2N/A break;
2N/A }
2N/A
2N/A default:
2N/A grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "XFS does not support inode format %d yet",
2N/A diro->inode.format);
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic struct grub_xfs_data *
2N/Agrub_xfs_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_xfs_data *data = 0;
2N/A
2N/A data = grub_zalloc (sizeof (struct grub_xfs_data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A /* Read the superblock. */
2N/A if (grub_disk_read (disk, 0, 0,
2N/A sizeof (struct grub_xfs_sblock), &data->sblock))
2N/A goto fail;
2N/A
2N/A if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A data = grub_realloc (data,
2N/A sizeof (struct grub_xfs_data)
2N/A - sizeof (struct grub_xfs_inode)
2N/A + (1 << data->sblock.log2_inode));
2N/A
2N/A if (! data)
2N/A goto fail;
2N/A
2N/A data->diropen.data = data;
2N/A data->diropen.ino = data->sblock.rootino;
2N/A data->diropen.inode_read = 1;
2N/A data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
2N/A data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
2N/A
2N/A data->disk = disk;
2N/A data->pos = 0;
2N/A
2N/A grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
2N/A
2N/A return data;
2N/A fail:
2N/A
2N/A if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
2N/A grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
2N/A
2N/A grub_free (data);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_xfs_dir (grub_device_t device, const char *path,
2N/A int (*hook) (const char *filename,
2N/A const struct grub_dirhook_info *info))
2N/A{
2N/A struct grub_xfs_data *data = 0;
2N/A struct grub_fshelp_node *fdiro = 0;
2N/A
2N/A auto int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node);
2N/A
2N/A int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node)
2N/A {
2N/A struct grub_dirhook_info info;
2N/A grub_memset (&info, 0, sizeof (info));
2N/A if (node->inode_read)
2N/A {
2N/A info.mtimeset = 1;
2N/A info.mtime = grub_be_to_cpu32 (node->inode.mtime.sec);
2N/A }
2N/A info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
2N/A grub_free (node);
2N/A return hook (filename, &info);
2N/A }
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_xfs_mount (device->disk);
2N/A if (!data)
2N/A goto mount_fail;
2N/A
2N/A grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
2N/A grub_xfs_read_symlink, GRUB_FSHELP_DIR);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A grub_xfs_iterate_dir (fdiro, iterate);
2N/A
2N/A fail:
2N/A if (fdiro != &data->diropen)
2N/A grub_free (fdiro);
2N/A grub_free (data);
2N/A
2N/A mount_fail:
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/A/* Open a file named NAME and initialize FILE. */
2N/Astatic grub_err_t
2N/Agrub_xfs_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_xfs_data *data;
2N/A struct grub_fshelp_node *fdiro = 0;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_xfs_mount (file->device->disk);
2N/A if (!data)
2N/A goto mount_fail;
2N/A
2N/A grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
2N/A grub_xfs_read_symlink, GRUB_FSHELP_REG);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A if (!fdiro->inode_read)
2N/A {
2N/A grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
2N/A if (grub_errno)
2N/A goto fail;
2N/A }
2N/A
2N/A if (fdiro != &data->diropen)
2N/A {
2N/A grub_memcpy (&data->diropen, fdiro,
2N/A sizeof (struct grub_fshelp_node)
2N/A - sizeof (struct grub_xfs_inode)
2N/A + (1 << data->sblock.log2_inode));
2N/A grub_free (fdiro);
2N/A }
2N/A
2N/A file->size = grub_be_to_cpu64 (data->diropen.inode.size);
2N/A file->data = data;
2N/A file->offset = 0;
2N/A
2N/A return 0;
2N/A
2N/A fail:
2N/A if (fdiro != &data->diropen)
2N/A grub_free (fdiro);
2N/A grub_free (data);
2N/A
2N/A mount_fail:
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_xfs_data *data =
2N/A (struct grub_xfs_data *) file->data;
2N/A
2N/A return grub_xfs_read_file (&data->diropen, file->read_hook,
2N/A file->offset, len, buf);
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_xfs_close (grub_file_t file)
2N/A{
2N/A grub_free (file->data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_xfs_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_xfs_data *data;
2N/A grub_disk_t disk = device->disk;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_xfs_mount (disk);
2N/A if (data)
2N/A *label = grub_strndup ((char *) (data->sblock.label), 12);
2N/A else
2N/A *label = 0;
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_xfs_uuid (grub_device_t device, char **uuid)
2N/A{
2N/A struct grub_xfs_data *data;
2N/A grub_disk_t disk = device->disk;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_xfs_mount (disk);
2N/A if (data)
2N/A {
2N/A *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
2N/A grub_be_to_cpu16 (data->sblock.uuid[0]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[1]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[2]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[3]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[4]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[5]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[6]),
2N/A grub_be_to_cpu16 (data->sblock.uuid[7]));
2N/A }
2N/A else
2N/A *uuid = NULL;
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/A
2N/Astatic struct grub_fs grub_xfs_fs =
2N/A {
2N/A .name = "xfs",
2N/A .dir = grub_xfs_dir,
2N/A .open = grub_xfs_open,
2N/A .read = grub_xfs_read,
2N/A .close = grub_xfs_close,
2N/A .label = grub_xfs_label,
2N/A .uuid = grub_xfs_uuid,
2N/A#ifdef GRUB_UTIL
2N/A .reserved_first_sector = 0,
2N/A#endif
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(xfs)
2N/A{
2N/A grub_fs_register (&grub_xfs_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(xfs)
2N/A{
2N/A grub_fs_unregister (&grub_xfs_fs);
2N/A}