2N/A/* jfs.c - JFS. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2004,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/charset.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_JFS_MAX_SYMLNK_CNT 8
2N/A#define GRUB_JFS_FILETYPE_MASK 0170000
2N/A#define GRUB_JFS_FILETYPE_REG 0100000
2N/A#define GRUB_JFS_FILETYPE_LNK 0120000
2N/A#define GRUB_JFS_FILETYPE_DIR 0040000
2N/A
2N/A#define GRUB_JFS_SBLOCK 64
2N/A#define GRUB_JFS_AGGR_INODE 2
2N/A#define GRUB_JFS_FS1_INODE_BLK 104
2N/A
2N/A#define GRUB_JFS_TREE_LEAF 2
2N/A
2N/Astruct grub_jfs_sblock
2N/A{
2N/A /* The magic for JFS. It should contain the string "JFS1". */
2N/A grub_uint8_t magic[4];
2N/A grub_uint32_t version;
2N/A grub_uint64_t ag_size;
2N/A
2N/A /* The size of a filesystem block in bytes. XXX: currently only
2N/A 4096 was tested. */
2N/A grub_uint32_t blksz;
2N/A grub_uint16_t log2_blksz;
2N/A
2N/A grub_uint8_t unused[79];
2N/A grub_uint8_t volname[11];
2N/A grub_uint8_t unused2[24];
2N/A grub_uint8_t uuid[16];
2N/A};
2N/A
2N/Astruct grub_jfs_extent
2N/A{
2N/A /* The length of the extent in filesystem blocks. */
2N/A grub_uint16_t length;
2N/A grub_uint8_t length2;
2N/A
2N/A /* The physical offset of the first block on the disk. */
2N/A grub_uint8_t blk1;
2N/A grub_uint32_t blk2;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_jfs_iag
2N/A{
2N/A grub_uint8_t unused[3072];
2N/A struct grub_jfs_extent inodes[128];
2N/A} __attribute__ ((packed));
2N/A
2N/A
2N/A/* The head of the tree used to find extents. */
2N/Astruct grub_jfs_treehead
2N/A{
2N/A grub_uint64_t next;
2N/A grub_uint64_t prev;
2N/A
2N/A grub_uint8_t flags;
2N/A grub_uint8_t unused;
2N/A
2N/A grub_uint16_t count;
2N/A grub_uint16_t max;
2N/A grub_uint8_t unused2[10];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A node in the extent tree. */
2N/Astruct grub_jfs_tree_extent
2N/A{
2N/A grub_uint8_t flags;
2N/A grub_uint16_t unused;
2N/A
2N/A /* The offset is the key used to lookup an extent. */
2N/A grub_uint8_t offset1;
2N/A grub_uint32_t offset2;
2N/A
2N/A struct grub_jfs_extent extent;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The tree of directory entries. */
2N/Astruct grub_jfs_tree_dir
2N/A{
2N/A /* Pointers to the previous and next tree headers of other nodes on
2N/A this level. */
2N/A grub_uint64_t nextb;
2N/A grub_uint64_t prevb;
2N/A
2N/A grub_uint8_t flags;
2N/A
2N/A /* The amount of dirents in this node. */
2N/A grub_uint8_t count;
2N/A grub_uint8_t freecnt;
2N/A grub_uint8_t freelist;
2N/A grub_uint8_t maxslot;
2N/A
2N/A /* The location of the sorted array of pointers to dirents. */
2N/A grub_uint8_t sindex;
2N/A grub_uint8_t unused[10];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* An internal node in the dirents tree. */
2N/Astruct grub_jfs_internal_dirent
2N/A{
2N/A struct grub_jfs_extent ex;
2N/A grub_uint8_t next;
2N/A grub_uint8_t len;
2N/A grub_uint16_t namepart[11];
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A leaf node in the dirents tree. */
2N/Astruct grub_jfs_leaf_dirent
2N/A{
2N/A /* The inode for this dirent. */
2N/A grub_uint32_t inode;
2N/A grub_uint8_t next;
2N/A
2N/A /* The size of the name. */
2N/A grub_uint8_t len;
2N/A grub_uint16_t namepart[11];
2N/A grub_uint32_t index;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A leaf in the dirents tree. This one is used if the previously
2N/A dirent was not big enough to store the name. */
2N/Astruct grub_jfs_leaf_next_dirent
2N/A{
2N/A grub_uint8_t next;
2N/A grub_uint8_t len;
2N/A grub_uint16_t namepart[15];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_jfs_time
2N/A{
2N/A grub_int32_t sec;
2N/A grub_int32_t nanosec;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_jfs_inode
2N/A{
2N/A grub_uint32_t stamp;
2N/A grub_uint32_t fileset;
2N/A grub_uint32_t inode;
2N/A grub_uint8_t unused[12];
2N/A grub_uint64_t size;
2N/A grub_uint8_t unused2[20];
2N/A grub_uint32_t mode;
2N/A struct grub_jfs_time atime;
2N/A struct grub_jfs_time ctime;
2N/A struct grub_jfs_time mtime;
2N/A grub_uint8_t unused3[48];
2N/A grub_uint8_t unused4[96];
2N/A
2N/A union
2N/A {
2N/A /* The tree describing the extents of the file. */
2N/A struct __attribute__ ((packed))
2N/A {
2N/A struct grub_jfs_treehead tree;
2N/A struct grub_jfs_tree_extent extents[16];
2N/A } file;
2N/A union
2N/A {
2N/A /* The tree describing the dirents. */
2N/A struct
2N/A {
2N/A grub_uint8_t unused[16];
2N/A grub_uint8_t flags;
2N/A
2N/A /* Amount of dirents in this node. */
2N/A grub_uint8_t count;
2N/A grub_uint8_t freecnt;
2N/A grub_uint8_t freelist;
2N/A grub_uint32_t idotdot;
2N/A grub_uint8_t sorted[8];
2N/A } header;
2N/A struct grub_jfs_leaf_dirent dirents[8];
2N/A } dir __attribute__ ((packed));
2N/A /* Fast symlink. */
2N/A struct
2N/A {
2N/A grub_uint8_t unused[32];
2N/A grub_uint8_t path[256];
2N/A } symlink;
2N/A } __attribute__ ((packed));
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_jfs_data
2N/A{
2N/A struct grub_jfs_sblock sblock;
2N/A grub_disk_t disk;
2N/A struct grub_jfs_inode fileset;
2N/A struct grub_jfs_inode currinode;
2N/A int pos;
2N/A int linknest;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_jfs_diropen
2N/A{
2N/A int index;
2N/A union
2N/A {
2N/A struct grub_jfs_tree_dir header;
2N/A struct grub_jfs_leaf_dirent dirent[0];
2N/A struct grub_jfs_leaf_next_dirent next_dirent[0];
2N/A grub_uint8_t sorted[0];
2N/A } *dirpage __attribute__ ((packed));
2N/A struct grub_jfs_data *data;
2N/A struct grub_jfs_inode *inode;
2N/A int count;
2N/A grub_uint8_t *sorted;
2N/A struct grub_jfs_leaf_dirent *leaf;
2N/A struct grub_jfs_leaf_next_dirent *next_leaf;
2N/A
2N/A /* The filename and inode of the last read dirent. */
2N/A /* On-disk name is at most 255 UTF-16 codepoints.
2N/A Every UTF-16 codepoint is at most 4 UTF-8 bytes.
2N/A */
2N/A char name[256 * GRUB_MAX_UTF8_PER_UTF16 + 1];
2N/A grub_uint32_t ino;
2N/A} __attribute__ ((packed));
2N/A
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/Astatic grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino);
2N/A
2N/A/* Get the block number for the block BLK in the node INODE in the
2N/A mounted filesystem DATA. */
2N/Astatic grub_int64_t
2N/Agrub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
2N/A grub_uint64_t blk)
2N/A{
2N/A auto grub_int64_t getblk (struct grub_jfs_treehead *treehead,
2N/A struct grub_jfs_tree_extent *extents);
2N/A
2N/A grub_int64_t getblk (struct grub_jfs_treehead *treehead,
2N/A struct grub_jfs_tree_extent *extents)
2N/A {
2N/A int found = -1;
2N/A int i;
2N/A
2N/A for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
2N/A {
2N/A if (treehead->flags & GRUB_JFS_TREE_LEAF)
2N/A {
2N/A /* Read the leafnode. */
2N/A if (grub_le_to_cpu32 (extents[i].offset2) <= blk
2N/A && ((grub_le_to_cpu16 (extents[i].extent.length))
2N/A + (extents[i].extent.length2 << 16)
2N/A + grub_le_to_cpu32 (extents[i].offset2)) > blk)
2N/A return (blk - grub_le_to_cpu32 (extents[i].offset2)
2N/A + grub_le_to_cpu32 (extents[i].extent.blk2));
2N/A }
2N/A else
2N/A if (blk >= grub_le_to_cpu32 (extents[i].offset2))
2N/A found = i;
2N/A }
2N/A
2N/A if (found != -1)
2N/A {
2N/A struct
2N/A {
2N/A struct grub_jfs_treehead treehead;
2N/A struct grub_jfs_tree_extent extents[254];
2N/A } tree;
2N/A
2N/A if (grub_disk_read (data->disk,
2N/A ((grub_disk_addr_t) grub_le_to_cpu32 (extents[found].extent.blk2))
2N/A << (grub_le_to_cpu16 (data->sblock.log2_blksz)
2N/A - GRUB_DISK_SECTOR_BITS), 0,
2N/A sizeof (tree), (char *) &tree))
2N/A return -1;
2N/A
2N/A return getblk (&tree.treehead, &tree.extents[0]);
2N/A }
2N/A
2N/A return -1;
2N/A }
2N/A
2N/A return getblk (&inode->file.tree, &inode->file.extents[0]);
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino,
2N/A struct grub_jfs_inode *inode)
2N/A{
2N/A struct grub_jfs_iag iag;
2N/A grub_uint32_t iagnum = ino / 4096;
2N/A unsigned inoext = (ino % 4096) / 32;
2N/A unsigned inonum = (ino % 4096) % 32;
2N/A grub_uint64_t iagblk;
2N/A grub_uint64_t inoblk;
2N/A
2N/A iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A
2N/A /* Read in the IAG. */
2N/A if (grub_disk_read (data->disk,
2N/A iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
2N/A - GRUB_DISK_SECTOR_BITS), 0,
2N/A sizeof (struct grub_jfs_iag), &iag))
2N/A return grub_errno;
2N/A
2N/A inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2);
2N/A inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
2N/A - GRUB_DISK_SECTOR_BITS);
2N/A inoblk += inonum;
2N/A
2N/A if (grub_disk_read (data->disk, inoblk, 0,
2N/A sizeof (struct grub_jfs_inode), inode))
2N/A return grub_errno;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic struct grub_jfs_data *
2N/Agrub_jfs_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_jfs_data *data = 0;
2N/A
2N/A data = grub_malloc (sizeof (struct grub_jfs_data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A /* Read the superblock. */
2N/A if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
2N/A sizeof (struct grub_jfs_sblock), &data->sblock))
2N/A goto fail;
2N/A
2N/A if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A if (grub_le_to_cpu32 (data->sblock.blksz)
2N/A != (1U << grub_le_to_cpu16 (data->sblock.log2_blksz)))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A data->disk = disk;
2N/A data->pos = 0;
2N/A data->linknest = 0;
2N/A
2N/A /* Read the inode of the first fileset. */
2N/A if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
2N/A sizeof (struct grub_jfs_inode), &data->fileset))
2N/A goto fail;
2N/A
2N/A return data;
2N/A
2N/A fail:
2N/A grub_free (data);
2N/A
2N/A if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
2N/A grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic struct grub_jfs_diropen *
2N/Agrub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
2N/A{
2N/A struct grub_jfs_internal_dirent *de;
2N/A struct grub_jfs_diropen *diro;
2N/A grub_disk_addr_t blk;
2N/A
2N/A de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
2N/A
2N/A if (!((grub_le_to_cpu32 (inode->mode)
2N/A & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
2N/A return 0;
2N/A }
2N/A
2N/A diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
2N/A if (!diro)
2N/A return 0;
2N/A
2N/A diro->data = data;
2N/A diro->inode = inode;
2N/A
2N/A /* Check if the entire tree is contained within the inode. */
2N/A if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
2N/A {
2N/A diro->leaf = inode->dir.dirents;
2N/A diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
2N/A diro->sorted = inode->dir.header.sorted;
2N/A diro->count = inode->dir.header.count;
2N/A
2N/A return diro;
2N/A }
2N/A
2N/A diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
2N/A if (!diro->dirpage)
2N/A {
2N/A grub_free (diro);
2N/A return 0;
2N/A }
2N/A
2N/A blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
2N/A blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
2N/A
2N/A /* Read in the nodes until we are on the leaf node level. */
2N/A do
2N/A {
2N/A int index;
2N/A if (grub_disk_read (data->disk, blk, 0,
2N/A grub_le_to_cpu32 (data->sblock.blksz),
2N/A diro->dirpage->sorted))
2N/A {
2N/A grub_free (diro->dirpage);
2N/A grub_free (diro);
2N/A return 0;
2N/A }
2N/A
2N/A de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
2N/A index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
2N/A blk = (grub_le_to_cpu32 (de[index].ex.blk2)
2N/A << (grub_le_to_cpu16 (data->sblock.log2_blksz)
2N/A - GRUB_DISK_SECTOR_BITS));
2N/A } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
2N/A
2N/A diro->leaf = diro->dirpage->dirent;
2N/A diro->next_leaf = diro->dirpage->next_dirent;
2N/A diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
2N/A diro->count = diro->dirpage->header.count;
2N/A
2N/A return diro;
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Agrub_jfs_closedir (struct grub_jfs_diropen *diro)
2N/A{
2N/A if (!diro)
2N/A return;
2N/A grub_free (diro->dirpage);
2N/A grub_free (diro);
2N/A}
2N/A
2N/A
2N/A/* Read in the next dirent from the directory described by DIRO. */
2N/Astatic grub_err_t
2N/Agrub_jfs_getent (struct grub_jfs_diropen *diro)
2N/A{
2N/A int strpos = 0;
2N/A struct grub_jfs_leaf_dirent *leaf;
2N/A struct grub_jfs_leaf_next_dirent *next_leaf;
2N/A int len;
2N/A int nextent;
2N/A grub_uint16_t filename[256];
2N/A
2N/A auto void addstr (grub_uint16_t *uname, int ulen);
2N/A
2N/A /* Add the unicode string to the utf16 filename buffer. */
2N/A void addstr (grub_uint16_t *name, int ulen)
2N/A {
2N/A while (ulen--)
2N/A filename[strpos++] = grub_le_to_cpu16 (*(name++));
2N/A }
2N/A
2N/A /* The last node, read in more. */
2N/A if (diro->index == diro->count)
2N/A {
2N/A grub_disk_addr_t next;
2N/A
2N/A /* If the inode contains the entry tree or if this was the last
2N/A node, there is nothing to read. */
2N/A if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
2N/A || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
2N/A return GRUB_ERR_OUT_OF_RANGE;
2N/A
2N/A next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
2N/A next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
2N/A - GRUB_DISK_SECTOR_BITS);
2N/A
2N/A if (grub_disk_read (diro->data->disk, next, 0,
2N/A grub_le_to_cpu32 (diro->data->sblock.blksz),
2N/A diro->dirpage->sorted))
2N/A return grub_errno;
2N/A
2N/A diro->leaf = diro->dirpage->dirent;
2N/A diro->next_leaf = diro->dirpage->next_dirent;
2N/A diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
2N/A diro->count = diro->dirpage->header.count;
2N/A diro->index = 0;
2N/A }
2N/A
2N/A leaf = &diro->leaf[diro->sorted[diro->index]];
2N/A next_leaf = &diro->next_leaf[diro->index];
2N/A
2N/A len = leaf->len;
2N/A if (!len)
2N/A {
2N/A diro->index++;
2N/A return grub_jfs_getent (diro);
2N/A }
2N/A
2N/A addstr (leaf->namepart, len < 11 ? len : 11);
2N/A diro->ino = grub_le_to_cpu32 (leaf->inode);
2N/A len -= 11;
2N/A
2N/A /* Move down to the leaf level. */
2N/A nextent = leaf->next;
2N/A if (leaf->next != 255)
2N/A do
2N/A {
2N/A next_leaf = &diro->next_leaf[nextent];
2N/A addstr (next_leaf->namepart, len < 15 ? len : 15 );
2N/A
2N/A len -= 15;
2N/A nextent = next_leaf->next;
2N/A } while (next_leaf->next != 255 && len > 0);
2N/A
2N/A diro->index++;
2N/A
2N/A /* Convert the temporary UTF16 filename to UTF8. */
2N/A *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
2N/A
2N/A return 0;
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_jfs_read_file (struct grub_jfs_data *data,
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 grub_off_t i;
2N/A grub_off_t blockcnt;
2N/A
2N/A blockcnt = (len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
2N/A >> grub_le_to_cpu16 (data->sblock.log2_blksz);
2N/A
2N/A for (i = pos >> grub_le_to_cpu16 (data->sblock.log2_blksz); i < blockcnt; i++)
2N/A {
2N/A grub_disk_addr_t blknr;
2N/A grub_uint32_t blockoff = pos & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
2N/A grub_uint32_t blockend = grub_le_to_cpu32 (data->sblock.blksz);
2N/A
2N/A grub_uint64_t skipfirst = 0;
2N/A
2N/A blknr = grub_jfs_blkno (data, &data->currinode, i);
2N/A if (grub_errno)
2N/A return -1;
2N/A
2N/A /* Last block. */
2N/A if (i == blockcnt - 1)
2N/A {
2N/A blockend = (len + pos) & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
2N/A
2N/A if (!blockend)
2N/A blockend = grub_le_to_cpu32 (data->sblock.blksz);
2N/A }
2N/A
2N/A /* First block. */
2N/A if (i == (pos >> grub_le_to_cpu16 (data->sblock.log2_blksz)))
2N/A {
2N/A skipfirst = blockoff;
2N/A blockend -= skipfirst;
2N/A }
2N/A
2N/A data->disk->read_hook = read_hook;
2N/A grub_disk_read (data->disk,
2N/A blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
2N/A - GRUB_DISK_SECTOR_BITS),
2N/A skipfirst, blockend, buf);
2N/A
2N/A data->disk->read_hook = 0;
2N/A if (grub_errno)
2N/A return -1;
2N/A
2N/A buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
2N/A }
2N/A
2N/A return len;
2N/A}
2N/A
2N/A
2N/A/* Find the file with the pathname PATH on the filesystem described by
2N/A DATA. */
2N/Astatic grub_err_t
2N/Agrub_jfs_find_file (struct grub_jfs_data *data, const char *path,
2N/A grub_uint32_t start_ino)
2N/A{
2N/A char fpath[grub_strlen (path)];
2N/A char *name = fpath;
2N/A char *next;
2N/A struct grub_jfs_diropen *diro;
2N/A
2N/A grub_strncpy (fpath, path, grub_strlen (path) + 1);
2N/A
2N/A if (grub_jfs_read_inode (data, start_ino, &data->currinode))
2N/A return grub_errno;
2N/A
2N/A /* Skip the first slashes. */
2N/A while (*name == '/')
2N/A {
2N/A name++;
2N/A if (!*name)
2N/A return 0;
2N/A }
2N/A
2N/A /* Extract the actual part from the pathname. */
2N/A next = grub_strchr (name, '/');
2N/A if (next)
2N/A {
2N/A while (*next == '/')
2N/A {
2N/A next[0] = '\0';
2N/A next++;
2N/A }
2N/A }
2N/A diro = grub_jfs_opendir (data, &data->currinode);
2N/A if (!diro)
2N/A return grub_errno;
2N/A
2N/A for (;;)
2N/A {
2N/A if (grub_strlen (name) == 0)
2N/A return GRUB_ERR_NONE;
2N/A
2N/A if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
2N/A break;
2N/A
2N/A /* Check if the current direntry matches the current part of the
2N/A pathname. */
2N/A if (!grub_strcmp (name, diro->name))
2N/A {
2N/A grub_uint32_t ino = diro->ino;
2N/A grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode);
2N/A
2N/A grub_jfs_closedir (diro);
2N/A diro = 0;
2N/A
2N/A if (grub_jfs_read_inode (data, ino, &data->currinode))
2N/A break;
2N/A
2N/A /* Check if this is a symlink. */
2N/A if ((grub_le_to_cpu32 (data->currinode.mode)
2N/A & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
2N/A {
2N/A grub_jfs_lookup_symlink (data, dirino);
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A }
2N/A
2N/A if (!next)
2N/A return 0;
2N/A
2N/A name = next;
2N/A next = grub_strchr (name, '/');
2N/A if (next)
2N/A {
2N/A next[0] = '\0';
2N/A next++;
2N/A }
2N/A
2N/A /* Open this directory for reading dirents. */
2N/A diro = grub_jfs_opendir (data, &data->currinode);
2N/A if (!diro)
2N/A return grub_errno;
2N/A
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A grub_jfs_closedir (diro);
2N/A grub_error (GRUB_ERR_FILE_NOT_FOUND, "file `%s' not found", path);
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino)
2N/A{
2N/A grub_size_t size = grub_le_to_cpu64 (data->currinode.size);
2N/A char symlink[size + 1];
2N/A
2N/A if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
2N/A return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
2N/A
2N/A if (size <= sizeof (data->currinode.symlink.path))
2N/A grub_strncpy (symlink, (char *) (data->currinode.symlink.path), size);
2N/A else if (grub_jfs_read_file (data, 0, 0, size, symlink) < 0)
2N/A return grub_errno;
2N/A
2N/A symlink[size] = '\0';
2N/A
2N/A /* The symlink is an absolute path, go back to the root inode. */
2N/A if (symlink[0] == '/')
2N/A ino = 2;
2N/A
2N/A grub_jfs_find_file (data, symlink, ino);
2N/A if (grub_errno)
2N/A grub_error (grub_errno, "cannot follow symlink `%s'", symlink);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_jfs_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_jfs_data *data = 0;
2N/A struct grub_jfs_diropen *diro = 0;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_jfs_mount (device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A if (grub_jfs_find_file (data, path, GRUB_JFS_AGGR_INODE))
2N/A goto fail;
2N/A
2N/A diro = grub_jfs_opendir (data, &data->currinode);
2N/A if (!diro)
2N/A goto fail;
2N/A
2N/A /* Iterate over the dirents in the directory that was found. */
2N/A while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
2N/A {
2N/A struct grub_jfs_inode inode;
2N/A struct grub_dirhook_info info;
2N/A grub_memset (&info, 0, sizeof (info));
2N/A
2N/A if (grub_jfs_read_inode (data, diro->ino, &inode))
2N/A goto fail;
2N/A
2N/A info.dir = (grub_le_to_cpu32 (inode.mode)
2N/A & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
2N/A info.mtimeset = 1;
2N/A info.mtime = grub_le_to_cpu32 (inode.mtime.sec);
2N/A if (hook (diro->name, &info))
2N/A goto fail;
2N/A }
2N/A
2N/A /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
2N/A if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
2N/A grub_errno = 0;
2N/A
2N/A fail:
2N/A grub_jfs_closedir (diro);
2N/A grub_free (data);
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_jfs_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_jfs_data *data;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_jfs_mount (file->device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A grub_jfs_find_file (data, name, GRUB_JFS_AGGR_INODE);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A /* It is only possible for open regular files. */
2N/A if (! ((grub_le_to_cpu32 (data->currinode.mode)
2N/A & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file");
2N/A goto fail;
2N/A }
2N/A
2N/A file->data = data;
2N/A file->size = grub_le_to_cpu64 (data->currinode.size);
2N/A
2N/A return 0;
2N/A
2N/A fail:
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/Astatic grub_ssize_t
2N/Agrub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_jfs_data *data =
2N/A (struct grub_jfs_data *) file->data;
2N/A
2N/A return grub_jfs_read_file (data, file->read_hook, file->offset, len, buf);
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_jfs_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/Astatic grub_err_t
2N/Agrub_jfs_uuid (grub_device_t device, char **uuid)
2N/A{
2N/A struct grub_jfs_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_jfs_mount (disk);
2N/A if (data)
2N/A {
2N/A *uuid = grub_xasprintf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2N/A "%02x%02x%02x%02x%02x%02x",
2N/A data->sblock.uuid[0], data->sblock.uuid[1],
2N/A data->sblock.uuid[2], data->sblock.uuid[3],
2N/A data->sblock.uuid[4], data->sblock.uuid[5],
2N/A data->sblock.uuid[6], data->sblock.uuid[7],
2N/A data->sblock.uuid[8], data->sblock.uuid[9],
2N/A data->sblock.uuid[10], data->sblock.uuid[11],
2N/A data->sblock.uuid[12], data->sblock.uuid[13],
2N/A data->sblock.uuid[14], data->sblock.uuid[15]);
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/Astatic grub_err_t
2N/Agrub_jfs_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_jfs_data *data;
2N/A data = grub_jfs_mount (device->disk);
2N/A
2N/A if (data)
2N/A *label = grub_strndup ((char *) (data->sblock.volname), 11);
2N/A else
2N/A *label = 0;
2N/A
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic struct grub_fs grub_jfs_fs =
2N/A {
2N/A .name = "jfs",
2N/A .dir = grub_jfs_dir,
2N/A .open = grub_jfs_open,
2N/A .read = grub_jfs_read,
2N/A .close = grub_jfs_close,
2N/A .label = grub_jfs_label,
2N/A .uuid = grub_jfs_uuid,
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(jfs)
2N/A{
2N/A grub_fs_register (&grub_jfs_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(jfs)
2N/A{
2N/A grub_fs_unregister (&grub_jfs_fs);
2N/A}