Commit 91556682 authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] HFS+ support

From: Roman Zippel <zippel@linux-m68k.org>

This driver adds full read/write support for HFS+ and is based on the
readonly driver by Brad Broyer.

Thanks to Ethan Benson <erbenson@alaska.net> for a number of patches to
make the driver more compliant with the spec.
parent d1081202
......@@ -951,6 +951,18 @@ config HFS_FS
To compile this file system support as a module, choose M here: the
module will be called hfs.
config HFSPLUS_FS
tristate "Apple Extended HFS file system support"
select NLS
help
If you say Y here, you will be able to mount extended format
Macintosh-formatted hard drive partitions with full read-write access.
This file system is often called HFS+ and was introduced with
MacOS 8. It includes all Mac specific filesystem data such as
data forks and creator codes, but it also has several UNIX
style features such as file ownership and permissions.
config BEFS_FS
tristate "BeOS file systemv(BeFS) support (read only) (EXPERIMENTAL)"
depends on EXPERIMENTAL
......
......@@ -62,6 +62,7 @@ obj-$(CONFIG_VFAT_FS) += vfat/
obj-$(CONFIG_BFS_FS) += bfs/
obj-$(CONFIG_ISO9660_FS) += isofs/
obj-$(CONFIG_DEVFS_FS) += devfs/
obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
obj-$(CONFIG_HFS_FS) += hfs/
obj-$(CONFIG_VXFS_FS) += freevxfs/
obj-$(CONFIG_NFS_FS) += nfs/
......
#
## Makefile for the linux hfsplus filesystem routines.
#
obj-$(CONFIG_HFSPLUS_FS) += hfsplus.o
hfsplus-objs := super.o options.o inode.o ioctl.o extents.o catalog.o dir.o btree.o \
bnode.o brec.o bfind.o tables.o unicode.o wrapper.o bitmap.o part_tbl.o
/*
* linux/fs/hfsplus/bfind.c
*
* Copyright (C) 2001
* Brad Boyer (flar@allandria.com)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Search routines for btrees
*/
#include <linux/slab.h>
#include "hfsplus_fs.h"
int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
{
void *ptr;
fd->tree = tree;
fd->bnode = NULL;
ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
if (!ptr)
return -ENOMEM;
fd->search_key = ptr;
fd->key = ptr + tree->max_key_len + 2;
dprint(DBG_BNODE_REFS, "find_init: %d (%p)\n", tree->cnid, __builtin_return_address(0));
down(&tree->tree_lock);
return 0;
}
void hfs_find_exit(struct hfs_find_data *fd)
{
hfs_bnode_put(fd->bnode);
kfree(fd->search_key);
dprint(DBG_BNODE_REFS, "find_exit: %d (%p)\n", fd->tree->cnid, __builtin_return_address(0));
up(&fd->tree->tree_lock);
fd->tree = NULL;
}
/* Find the record in bnode that best matches key (not greater than...)*/
int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
{
int cmpval;
u16 off, len, keylen;
int rec;
int b, e;
int res;
b = 0;
e = bnode->num_recs - 1;
res = -ENOENT;
do {
rec = (e + b) / 2;
len = hfs_brec_lenoff(bnode, rec, &off);
keylen = hfs_brec_keylen(bnode, rec);
hfs_bnode_read(bnode, fd->key, off, keylen);
cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
if (!cmpval) {
e = rec;
res = 0;
goto done;
}
if (cmpval < 0)
b = rec + 1;
else
e = rec - 1;
} while (b <= e);
//printk("%d: %d,%d,%d\n", bnode->this, b, e, rec);
if (rec != e && e >= 0) {
len = hfs_brec_lenoff(bnode, e, &off);
keylen = hfs_brec_keylen(bnode, e);
hfs_bnode_read(bnode, fd->key, off, keylen);
}
done:
fd->record = e;
fd->keyoffset = off;
fd->keylength = keylen;
fd->entryoffset = off + keylen;
fd->entrylength = len - keylen;
return res;
}
/* Traverse a B*Tree from the root to a leaf finding best fit to key */
/* Return allocated copy of node found, set recnum to best record */
int hfs_brec_find(struct hfs_find_data *fd)
{
struct hfs_btree *tree;
struct hfs_bnode *bnode;
u32 data, nidx, parent;
int height, res;
tree = fd->tree;
if (fd->bnode)
hfs_bnode_put(fd->bnode);
fd->bnode = NULL;
nidx = tree->root;
if (!nidx)
return -ENOENT;
height = tree->depth;
res = 0;
parent = 0;
for (;;) {
bnode = hfs_bnode_find(tree, nidx);
if (IS_ERR(bnode)) {
res = PTR_ERR(bnode);
bnode = NULL;
break;
}
if (bnode->height != height)
goto invalid;
if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
goto invalid;
bnode->parent = parent;
res = __hfs_brec_find(bnode, fd);
if (!height)
break;
if (fd->record < 0)
goto release;
parent = nidx;
hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
nidx = be32_to_cpu(data);
hfs_bnode_put(bnode);
}
fd->bnode = bnode;
return res;
invalid:
printk("HFS+-fs: inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
height, bnode->height, bnode->type, nidx, parent);
res = -EIO;
release:
hfs_bnode_put(bnode);
return res;
}
int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
{
int res;
res = hfs_brec_find(fd);
if (res)
return res;
if (fd->entrylength > rec_len)
return -EINVAL;
hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
return 0;
}
int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
{
struct hfs_btree *tree;
struct hfs_bnode *bnode;
int idx, res = 0;
u16 off, len, keylen;
bnode = fd->bnode;
tree = bnode->tree;
if (cnt < 0) {
cnt = -cnt;
while (cnt > fd->record) {
cnt -= fd->record + 1;
fd->record = bnode->num_recs - 1;
idx = bnode->prev;
if (!idx) {
res = -ENOENT;
goto out;
}
hfs_bnode_put(bnode);
bnode = hfs_bnode_find(tree, idx);
if (IS_ERR(bnode)) {
res = PTR_ERR(bnode);
bnode = NULL;
goto out;
}
}
fd->record -= cnt;
} else {
while (cnt >= bnode->num_recs - fd->record) {
cnt -= bnode->num_recs - fd->record;
fd->record = 0;
idx = bnode->next;
if (!idx) {
res = -ENOENT;
goto out;
}
hfs_bnode_put(bnode);
bnode = hfs_bnode_find(tree, idx);
if (IS_ERR(bnode)) {
res = PTR_ERR(bnode);
bnode = NULL;
goto out;
}
}
fd->record += cnt;
}
len = hfs_brec_lenoff(bnode, fd->record, &off);
keylen = hfs_brec_keylen(bnode, fd->record);
fd->keyoffset = off;
fd->keylength = keylen;
fd->entryoffset = off + keylen;
fd->entrylength = len - keylen;
hfs_bnode_read(bnode, fd->key, off, keylen);
out:
fd->bnode = bnode;
return res;
}
/*
* linux/fs/hfsplus/bitmap.c
*
* Copyright (C) 2001
* Brad Boyer (flar@allandria.com)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Handling of allocation file
*/
#include <linux/pagemap.h>
#include "hfsplus_fs.h"
#include "hfsplus_raw.h"
#define PAGE_CACHE_BITS (PAGE_CACHE_SIZE * 8)
int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset, u32 *max)
{
struct page *page;
struct address_space *mapping;
u32 *pptr, *curr, *end;
u32 val, mask, start, len;
int i;
len = *max;
if (!len)
return size;
dprint(DBG_BITMAP, "block_allocate: %u,%u,%u\n", size, offset, len);
down(&HFSPLUS_SB(sb).alloc_file->i_sem);
mapping = HFSPLUS_SB(sb).alloc_file->i_mapping;
page = read_cache_page(mapping, offset / PAGE_CACHE_BITS,
(filler_t *)mapping->a_ops->readpage, NULL);
pptr = kmap(page);
curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
i = offset % 32;
offset &= ~(PAGE_CACHE_BITS - 1);
if ((size ^ offset) / PAGE_CACHE_BITS)
end = pptr + PAGE_CACHE_BITS / 32;
else
end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
/* scan the first partial u32 for zero bits */
val = *curr;
if (~val) {
val = be32_to_cpu(val);
mask = (1U << 31) >> i;
for (; i < 32; mask >>= 1, i++) {
if (!(val & mask))
goto found;
}
}
curr++;
/* scan complete u32s for the first zero bit */
while (1) {
while (curr < end) {
val = *curr;
if (~val) {
val = be32_to_cpu(val);
mask = 1 << 31;
for (i = 0; i < 32; mask >>= 1, i++) {
if (!(val & mask))
goto found;
}
}
curr++;
}
kunmap(page);
offset += PAGE_CACHE_BITS;
if (offset >= size)
break;
page = read_cache_page(mapping, offset / PAGE_CACHE_BITS,
(filler_t *)mapping->a_ops->readpage, NULL);
curr = pptr = kmap(page);
if ((size ^ offset) / PAGE_CACHE_BITS)
end = pptr + PAGE_CACHE_BITS / 32;
else
end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
}
dprint(DBG_BITMAP, "bitmap full\n");
start = size;
goto out;
found:
start = offset + (curr - pptr) * 32 + i;
if (start >= size) {
dprint(DBG_BITMAP, "bitmap full\n");
goto out;
}
/* do any partial u32 at the start */
len = min(size - start, len);
while (1) {
val |= mask;
if (++i >= 32)
break;
mask >>= 1;
if (!--len || val & mask)
goto done;
}
if (!--len)
goto done;
*curr++ = cpu_to_be32(val);
/* do full u32s */
while (1) {
while (curr < end) {
val = be32_to_cpu(*curr);
if (len < 32)
goto last;
if (val) {
len = 32;
goto last;
}
*curr++ = 0xffffffffU;
len -= 32;
}
set_page_dirty(page);
kunmap(page);
offset += PAGE_CACHE_BITS;
page = read_cache_page(mapping, offset / PAGE_CACHE_BITS,
(filler_t *)mapping->a_ops->readpage, NULL);
pptr = kmap(page);
curr = pptr;
end = pptr + PAGE_CACHE_BITS / 32;
}
last:
/* do any partial u32 at end */
mask = 1U << 31;
for (i = 0; i < len; i++) {
if (val & mask)
break;
val |= mask;
mask >>= 1;
}
done:
*curr = cpu_to_be32(val);
set_page_dirty(page);
kunmap(page);
*max = offset + (curr - pptr) * 32 + i - start;
HFSPLUS_SB(sb).free_blocks -= *max;
sb->s_dirt = 1;
dprint(DBG_BITMAP, "-> %u,%u\n", start, *max);
out:
up(&HFSPLUS_SB(sb).alloc_file->i_sem);
return start;
}
int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)
{
struct page *page;
struct address_space *mapping;
u32 *pptr, *curr, *end;
u32 mask, len, pnr;
int i;
/* is there any actual work to be done? */
if (!count)
return 0;
dprint(DBG_BITMAP, "block_free: %u,%u\n", offset, count);
/* are all of the bits in range? */
if ((offset + count) > HFSPLUS_SB(sb).total_blocks)
return -2;
down(&HFSPLUS_SB(sb).alloc_file->i_sem);
mapping = HFSPLUS_SB(sb).alloc_file->i_mapping;
pnr = offset / PAGE_CACHE_BITS;
page = read_cache_page(mapping, pnr, (filler_t *)mapping->a_ops->readpage, NULL);
pptr = kmap(page);
curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
end = pptr + PAGE_CACHE_BITS / 32;
len = count;
/* do any partial u32 at the start */
i = offset % 32;
if (i) {
int j = 32 - i;
mask = 0xffffffffU << j;
if (j > count) {
mask |= 0xffffffffU >> (i + count);
*curr++ &= cpu_to_be32(mask);
goto out;
}
*curr++ &= cpu_to_be32(mask);
count -= j;
}
/* do full u32s */
while (1) {
while (curr < end) {
if (count < 32)
goto done;
*curr++ = 0;
count -= 32;
}
if (!count)
break;
set_page_dirty(page);
kunmap(page);
page = read_cache_page(mapping, ++pnr, (filler_t *)mapping->a_ops->readpage, NULL);
pptr = kmap(page);
curr = pptr;
end = pptr + PAGE_CACHE_BITS / 32;
}
done:
/* do any partial u32 at end */
if (count) {
mask = 0xffffffffU >> count;
*curr &= cpu_to_be32(mask);
}
out:
set_page_dirty(page);
kunmap(page);
HFSPLUS_SB(sb).free_blocks += len;
sb->s_dirt = 1;
up(&HFSPLUS_SB(sb).alloc_file->i_sem);
return 0;
}
This diff is collapsed.
This diff is collapsed.
/*
* linux/fs/hfsplus/btree.c
*
* Copyright (C) 2001
* Brad Boyer (flar@allandria.com)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Handle opening/closing btree
*/
#include <linux/slab.h>
#include <linux/pagemap.h>
#include "hfsplus_fs.h"
#include "hfsplus_raw.h"
/* Get a reference to a B*Tree and do some initial checks */
struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
{
struct hfs_btree *tree;
struct hfs_btree_header_rec *head;
struct address_space *mapping;
struct page *page;
unsigned int shift, size;
tree = kmalloc(sizeof(*tree), GFP_KERNEL);
if (!tree)
return NULL;
memset(tree, 0, sizeof(*tree));
init_MUTEX(&tree->tree_lock);
spin_lock_init(&tree->hash_lock);
/* Set the correct compare function */
tree->sb = sb;
tree->cnid = id;
if (id == HFSPLUS_EXT_CNID) {
tree->keycmp = hfsplus_ext_cmp_key;
} else if (id == HFSPLUS_CAT_CNID) {
tree->keycmp = hfsplus_cat_cmp_key;
} else {
printk("HFS+-fs: unknown B*Tree requested\n");
goto free_tree;
}
tree->inode = iget(sb, id);
if (!tree->inode)
goto free_tree;
mapping = tree->inode->i_mapping;
page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage, NULL);
if (IS_ERR(page))
goto free_tree;
/* Load the header */
head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
tree->root = be32_to_cpu(head->root);
tree->leaf_count = be32_to_cpu(head->leaf_count);
tree->leaf_head = be32_to_cpu(head->leaf_head);
tree->leaf_tail = be32_to_cpu(head->leaf_tail);
tree->node_count = be32_to_cpu(head->node_count);
tree->free_nodes = be32_to_cpu(head->free_nodes);
tree->attributes = be32_to_cpu(head->attributes);
tree->node_size = be16_to_cpu(head->node_size);
tree->max_key_len = be16_to_cpu(head->max_key_len);
tree->depth = be16_to_cpu(head->depth);
size = tree->node_size;
if (!size || size & (size - 1))
goto fail_page;
if (!tree->node_count)
goto fail_page;
for (shift = 0; size >>= 1; shift += 1)
;
tree->node_size_shift = shift;
tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
kunmap(page);
page_cache_release(page);
return tree;
fail_page:
tree->inode->i_mapping->a_ops = &hfsplus_aops;
page_cache_release(page);
free_tree:
iput(tree->inode);
kfree(tree);
return NULL;
}
/* Release resources used by a btree */
void hfs_btree_close(struct hfs_btree *tree)
{
struct hfs_bnode *node;
int i;
if (!tree)
return;
for (i = 0; i < NODE_HASH_SIZE; i++) {
while ((node = tree->node_hash[i])) {
tree->node_hash[i] = node->next_hash;
if (atomic_read(&node->refcnt))
printk("HFS+: node %d:%d still has %d user(s)!\n",
node->tree->cnid, node->this, atomic_read(&node->refcnt));
hfs_bnode_free(node);
tree->node_hash_cnt--;
}
}
iput(tree->inode);
kfree(tree);
}
void hfs_btree_write(struct hfs_btree *tree)
{
struct hfs_btree_header_rec *head;
struct hfs_bnode *node;
struct page *page;
node = hfs_bnode_find(tree, 0);
if (IS_ERR(node))
/* panic? */
return;
/* Load the header */
page = node->page[0];
head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
head->root = cpu_to_be32(tree->root);
head->leaf_count = cpu_to_be32(tree->leaf_count);
head->leaf_head = cpu_to_be32(tree->leaf_head);
head->leaf_tail = cpu_to_be32(tree->leaf_tail);
head->node_count = cpu_to_be32(tree->node_count);
head->free_nodes = cpu_to_be32(tree->free_nodes);
head->attributes = cpu_to_be32(tree->attributes);
head->depth = cpu_to_be16(tree->depth);
kunmap(page);
set_page_dirty(page);
hfs_bnode_put(node);
}
static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
{
struct hfs_btree *tree = prev->tree;
struct hfs_bnode *node;
struct hfs_bnode_desc desc;
u32 cnid;
node = hfs_bnode_create(tree, idx);
if (IS_ERR(node))
return node;
tree->free_nodes--;
prev->next = idx;
cnid = cpu_to_be32(idx);
hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
node->type = HFS_NODE_MAP;
node->num_recs = 1;
hfs_bnode_clear(node, 0, tree->node_size);
desc.next = 0;
desc.prev = 0;
desc.type = HFS_NODE_MAP;
desc.height = 0;
desc.num_recs = cpu_to_be16(1);
desc.reserved = 0;
hfs_bnode_write(node, &desc, 0, sizeof(desc));
hfs_bnode_write_u16(node, 14, 0x8000);
hfs_bnode_write_u16(node, tree->node_size - 2, 14);
hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
return node;
}
struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
{
struct hfs_bnode *node, *next_node;
struct page **pagep;
u32 nidx, idx;
u16 off, len;
u8 *data, byte, m;
int i;
while (!tree->free_nodes) {
struct inode *inode = tree->inode;
u32 count;
int res;
res = hfsplus_file_extend(inode);
if (res)
return ERR_PTR(res);
inode->i_blocks = HFSPLUS_I(inode).alloc_blocks <<
HFSPLUS_SB(tree->sb).fs_shift;
HFSPLUS_I(inode).phys_size = inode->i_size =
(loff_t)inode->i_blocks << tree->sb->s_blocksize_bits;
count = inode->i_size >> tree->node_size_shift;
tree->free_nodes = count - tree->node_count;
tree->node_count = count;
}
nidx = 0;
node = hfs_bnode_find(tree, nidx);
if (IS_ERR(node))
return node;
len = hfs_brec_lenoff(node, 2, &off);
off += node->page_offset;
pagep = node->page + (off >> PAGE_CACHE_SHIFT);
data = kmap(*pagep);
off &= ~PAGE_CACHE_MASK;
idx = 0;
for (;;) {
while (len) {
byte = data[off];
if (byte != 0xff) {
for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
if (!(byte & m)) {
idx += i;
data[off] |= m;
set_page_dirty(*pagep);
kunmap(*pagep);
tree->free_nodes--;
mark_inode_dirty(tree->inode);
hfs_bnode_put(node);
if (!idx) {
printk("unexpected idx %u (%u)\n", idx, node->this);
BUG();
}
return hfs_bnode_create(tree, idx);
}
}
}
if (++off >= PAGE_CACHE_SIZE) {
kunmap(*pagep);
data = kmap(*++pagep);
off = 0;
}
idx += 8;
len--;
}
kunmap(*pagep);
nidx = node->next;
if (!nidx) {
printk("create new bmap node...\n");
next_node = hfs_bmap_new_bmap(node, idx);
} else
next_node = hfs_bnode_find(tree, nidx);
hfs_bnode_put(node);
if (IS_ERR(next_node))
return next_node;
node = next_node;
len = hfs_brec_lenoff(node, 0, &off);
off += node->page_offset;
pagep = node->page + (off >> PAGE_CACHE_SHIFT);
data = kmap(*pagep);
off &= ~PAGE_CACHE_MASK;
}
}
void hfs_bmap_free(struct hfs_bnode *node)
{
struct hfs_btree *tree;
struct page *page;
u16 off, len;
u32 nidx;
u8 *data, byte, m;
dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
if (!node->this)
BUG();
tree = node->tree;
nidx = node->this;
node = hfs_bnode_find(tree, 0);
if (IS_ERR(node))
return;
len = hfs_brec_lenoff(node, 2, &off);
while (nidx >= len * 8) {
u32 i;
nidx -= len * 8;
i = node->next;
hfs_bnode_put(node);
if (!i) {
/* panic */;
printk("HFS: unable to free bnode %u. bmap not found!\n", node->this);
return;
}
node = hfs_bnode_find(tree, i);
if (IS_ERR(node))
return;
if (node->type != HFS_NODE_MAP) {
/* panic */;
printk("HFS: invalid bmap found! (%u,%d)\n", node->this, node->type);
hfs_bnode_put(node);
return;
}
len = hfs_brec_lenoff(node, 0, &off);
}
off += node->page_offset + nidx / 8;
page = node->page[off >> PAGE_CACHE_SHIFT];
data = kmap(page);
off &= ~PAGE_CACHE_MASK;
m = 1 << (~nidx & 7);
byte = data[off];
if (!(byte & m)) {
printk("HFS: trying to free free bnode %u(%d)\n", node->this, node->type);
kunmap(page);
hfs_bnode_put(node);
return;
}
data[off] = byte & ~m;
set_page_dirty(page);
kunmap(page);
hfs_bnode_put(node);
tree->free_nodes++;
mark_inode_dirty(tree->inode);
}
/*
* linux/fs/hfsplus/catalog.c
*
* Copyright (C) 2001
* Brad Boyer (flar@allandria.com)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Handling of catalog records
*/
#include <linux/sched.h>
#include "hfsplus_fs.h"
#include "hfsplus_raw.h"
int hfsplus_cat_cmp_key(hfsplus_btree_key *k1, hfsplus_btree_key *k2)
{
u32 k1p, k2p;
k1p = k1->cat.parent;
k2p = k2->cat.parent;
if (k1p != k2p)
return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
return hfsplus_unistrcmp(&k1->cat.name, &k2->cat.name);
}
void hfsplus_cat_build_key(hfsplus_btree_key *key, u32 parent,
struct qstr *str)
{
int len;
key->cat.parent = cpu_to_be32(parent);
if (str) {
hfsplus_asc2uni(&key->cat.name, str->name, str->len);
len = be16_to_cpu(key->cat.name.length);
} else
len = key->cat.name.length = 0;
key->key_len = cpu_to_be16(6 + 2 * len);
}
static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
struct hfsplus_unistr *name)
{
int ustrlen;
ustrlen = be16_to_cpu(name->length);
key->cat.parent = cpu_to_be32(parent);
key->cat.name.length = cpu_to_be16(ustrlen);
ustrlen *= 2;
memcpy(key->cat.name.unicode, name->unicode, ustrlen);
key->key_len = cpu_to_be16(6 + ustrlen);
}
static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
{
if (inode->i_flags & S_IMMUTABLE)
perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
else
perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
if (inode->i_flags & S_APPEND)
perms->rootflags |= HFSPLUS_FLG_APPEND;
else
perms->rootflags &= ~HFSPLUS_FLG_APPEND;
HFSPLUS_I(inode).rootflags = perms->rootflags;
HFSPLUS_I(inode).userflags = perms->userflags;
perms->mode = cpu_to_be16(inode->i_mode);
perms->owner = cpu_to_be32(inode->i_uid);
perms->group = cpu_to_be32(inode->i_gid);
}
static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
{
if (S_ISDIR(inode->i_mode)) {
struct hfsplus_cat_folder *folder;
folder = &entry->folder;
memset(folder, 0, sizeof(*folder));
folder->type = cpu_to_be16(HFSPLUS_FOLDER);
folder->id = cpu_to_be32(inode->i_ino);
folder->create_date = folder->content_mod_date =
folder->attribute_mod_date = folder->access_date = hfsp_now2mt();
hfsplus_set_perms(inode, &folder->permissions);
if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
/* invisible and namelocked */
folder->user_info.frFlags = cpu_to_be16(0x5000);
return sizeof(*folder);
} else {
struct hfsplus_cat_file *file;
file = &entry->file;
memset(file, 0, sizeof(*file));
file->type = cpu_to_be16(HFSPLUS_FILE);
file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
file->id = cpu_to_be32(cnid);
file->create_date = file->content_mod_date =
file->attribute_mod_date = file->access_date = hfsp_now2mt();
if (cnid == inode->i_ino) {
hfsplus_set_perms(inode, &file->permissions);
file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
} else {
file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
file->user_info.fdFlags = cpu_to_be16(0x100);
file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
}
return sizeof(*file);
}
}
static int hfsplus_fill_cat_thread(hfsplus_cat_entry *entry, int type,
u32 parentid, struct qstr *str)
{
entry->type = cpu_to_be16(type);
entry->thread.reserved = 0;
entry->thread.parentID = cpu_to_be32(parentid);
hfsplus_asc2uni(&entry->thread.nodeName, str->name, str->len);
return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
}
/* Try to get a catalog entry for given catalog id */
int hfsplus_find_cat(struct super_block *sb, u32 cnid,
struct hfs_find_data *fd)
{
hfsplus_cat_entry tmp;
int err;
u16 type;
hfsplus_cat_build_key(fd->search_key, cnid, NULL);
err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
if (err)
return err;
type = be16_to_cpu(tmp.type);
if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
printk("HFS+-fs: Found bad thread record in catalog\n");
return -EIO;
}
hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
&tmp.thread.nodeName);
return hfs_brec_find(fd);
}
int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
{
struct hfs_find_data fd;
struct super_block *sb;
hfsplus_cat_entry entry;
int entry_size;
int err;
dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
sb = dir->i_sb;
hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
hfsplus_cat_build_key(fd.search_key, cnid, NULL);
entry_size = hfsplus_fill_cat_thread(&entry, S_ISDIR(inode->i_mode) ?
HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
dir->i_ino, str);
err = hfs_brec_find(&fd);
if (err != -ENOENT) {
if (!err)
err = -EEXIST;
goto out;
}
err = hfs_brec_insert(&fd, &entry, entry_size);
if (err)
goto out;
hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
err = hfs_brec_find(&fd);
if (err != -ENOENT) {
/* panic? */
if (!err)
err = -EEXIST;
goto out;
}
err = hfs_brec_insert(&fd, &entry, entry_size);
if (!err) {
dir->i_size++;
mark_inode_dirty(dir);
}
out:
hfs_find_exit(&fd);
return err;
}
int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
{
struct super_block *sb;
struct hfs_find_data fd;
struct hfsplus_fork_raw fork;
struct list_head *pos;
int err, off;
u16 type;
dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
sb = dir->i_sb;
hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
if (!str) {
int len;
hfsplus_cat_build_key(fd.search_key, cnid, NULL);
err = hfs_brec_find(&fd);
if (err)
goto out;
off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
fd.search_key->key_len = cpu_to_be16(6 + len);
} else
hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
err = hfs_brec_find(&fd);
if (err)
goto out;
type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
if (type == HFSPLUS_FILE) {
#if 0
off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
#endif
off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
}
list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
struct hfsplus_readdir_data *rd =
list_entry(pos, struct hfsplus_readdir_data, list);
if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
rd->file->f_pos--;
}
err = hfs_brec_remove(&fd);
if (err)
goto out;
hfsplus_cat_build_key(fd.search_key, cnid, NULL);
err = hfs_brec_find(&fd);
if (err)
goto out;
err = hfs_brec_remove(&fd);
if (err)
goto out;
dir->i_size--;
mark_inode_dirty(dir);
out:
hfs_find_exit(&fd);
return err;
}
int hfsplus_rename_cat(u32 cnid,
struct inode *src_dir, struct qstr *src_name,
struct inode *dst_dir, struct qstr *dst_name)
{
struct super_block *sb;
struct hfs_find_data src_fd, dst_fd;
hfsplus_cat_entry entry;
int entry_size, type;
int err = 0;
dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
dst_dir->i_ino, dst_name->name);
sb = src_dir->i_sb;
hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
dst_fd = src_fd;
/* find the old dir entry and read the data */
hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
err = hfs_brec_find(&src_fd);
if (err)
goto out;
hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
src_fd.entrylength);
/* create new dir entry with the data from the old entry */
hfsplus_cat_build_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
err = hfs_brec_find(&dst_fd);
if (err != -ENOENT) {
if (!err)
err = -EEXIST;
goto out;
}
err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
if (err)
goto out;
dst_dir->i_size++;
mark_inode_dirty(dst_dir);
/* finally remove the old entry */
hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
err = hfs_brec_find(&src_fd);
if (err)
goto out;
err = hfs_brec_remove(&src_fd);
if (err)
goto out;
src_dir->i_size--;
mark_inode_dirty(src_dir);
/* remove old thread entry */
hfsplus_cat_build_key(src_fd.search_key, cnid, NULL);
err = hfs_brec_find(&src_fd);
if (err)
goto out;
type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
err = hfs_brec_remove(&src_fd);
if (err)
goto out;
/* create new thread entry */
hfsplus_cat_build_key(dst_fd.search_key, cnid, NULL);
entry_size = hfsplus_fill_cat_thread(&entry, type, dst_dir->i_ino, dst_name);
err = hfs_brec_find(&dst_fd);
if (err != -ENOENT) {
if (!err)
err = -EEXIST;
goto out;
}
err = hfs_brec_insert(&dst_fd, &entry, entry_size);
out:
hfs_bnode_put(dst_fd.bnode);
hfs_find_exit(&src_fd);
return err;
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
* linux/include/linux/hfsplus_raw.h
*
* Copyright (C) 1999
* Brad Boyer (flar@pants.nu)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Format of structures on disk
* Information taken from Apple Technote #1150 (HFS Plus Volume Format)
*
*/
#ifndef _LINUX_HFSPLUS_RAW_H
#define _LINUX_HFSPLUS_RAW_H
#include <linux/types.h>
#define __packed __attribute__ ((packed))
/* Some constants */
#define HFSPLUS_SECTOR_SIZE 512
#define HFSPLUS_SECTOR_SHIFT 9
#define HFSPLUS_VOLHEAD_SECTOR 2
#define HFSPLUS_VOLHEAD_SIG 0x482b
#define HFSPLUS_SUPER_MAGIC 0x482b
#define HFSPLUS_CURRENT_VERSION 4
#define HFSP_WRAP_MAGIC 0x4244
#define HFSP_WRAP_ATTRIB_SLOCK 0x8000
#define HFSP_WRAP_ATTRIB_SPARED 0x0200
#define HFSP_WRAPOFF_SIG 0x00
#define HFSP_WRAPOFF_ATTRIB 0x0A
#define HFSP_WRAPOFF_ABLKSIZE 0x14
#define HFSP_WRAPOFF_ABLKSTART 0x1C
#define HFSP_WRAPOFF_EMBEDSIG 0x7C
#define HFSP_WRAPOFF_EMBEDEXT 0x7E
#define HFSP_HIDDENDIR_NAME "\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80HFS+ Private Data"
#define HFSP_HARDLINK_TYPE 0x686c6e6b /* 'hlnk' */
#define HFSP_HFSPLUS_CREATOR 0x6866732b /* 'hfs+' */
#define HFSP_MOUNT_VERSION 0x482b4c78 /* 'H+Lx' */
/* Structures used on disk */
typedef u32 hfsplus_cnid;
typedef u16 hfsplus_unichr;
/* A "string" as used in filenames, etc. */
struct hfsplus_unistr {
u16 length;
hfsplus_unichr unicode[255];
} __packed;
#define HFSPLUS_MAX_STRLEN 255
/* POSIX permissions */
struct hfsplus_perm {
u32 owner;
u32 group;
u8 rootflags;
u8 userflags;
u16 mode;
u32 dev;
} __packed;
#define HFSPLUS_FLG_NODUMP 0x01
#define HFSPLUS_FLG_IMMUTABLE 0x02
#define HFSPLUS_FLG_APPEND 0x04
/* A single contiguous area of a file */
struct hfsplus_extent {
u32 start_block;
u32 block_count;
} __packed;
typedef struct hfsplus_extent hfsplus_extent_rec[8];
/* Information for a "Fork" in a file */
struct hfsplus_fork_raw {
u64 total_size;
u32 clump_size;
u32 total_blocks;
hfsplus_extent_rec extents;
} __packed;
/* HFS+ Volume Header */
struct hfsplus_vh {
u16 signature;
u16 version;
u32 attributes;
u32 last_mount_vers;
u32 reserved;
u32 create_date;
u32 modify_date;
u32 backup_date;
u32 checked_date;
u32 file_count;
u32 folder_count;
u32 blocksize;
u32 total_blocks;
u32 free_blocks;
u32 next_alloc;
u32 rsrc_clump_sz;
u32 data_clump_sz;
hfsplus_cnid next_cnid;
u32 write_count;
u64 encodings_bmp;
u8 finder_info[32];
struct hfsplus_fork_raw alloc_file;
struct hfsplus_fork_raw ext_file;
struct hfsplus_fork_raw cat_file;
struct hfsplus_fork_raw attr_file;
struct hfsplus_fork_raw start_file;
} __packed;
/* HFS+ volume attributes */
#define HFSPLUS_VOL_UNMNT (1 << 8)
#define HFSPLUS_VOL_SPARE_BLK (1 << 9)
#define HFSPLUS_VOL_NOCACHE (1 << 10)
#define HFSPLUS_VOL_INCNSTNT (1 << 11)
#define HFSPLUS_VOL_SOFTLOCK (1 << 15)
/* HFS+ BTree node descriptor */
struct hfs_bnode_desc {
u32 next;
u32 prev;
s8 type;
u8 height;
u16 num_recs;
u16 reserved;
} __packed;
/* HFS+ BTree node types */
#define HFS_NODE_INDEX 0x00
#define HFS_NODE_HEADER 0x01
#define HFS_NODE_MAP 0x02
#define HFS_NODE_LEAF 0xFF
/* HFS+ BTree header */
struct hfs_btree_header_rec {
u16 depth;
u32 root;
u32 leaf_count;
u32 leaf_head;
u32 leaf_tail;
u16 node_size;
u16 max_key_len;
u32 node_count;
u32 free_nodes;
u16 reserved1;
u32 clump_size;
u8 btree_type;
u8 reserved2;
u32 attributes;
u32 reserved3[16];
} __packed;
/* BTree attributes */
#define HFS_TREE_BIGKEYS 2
#define HFS_TREE_VARIDXKEYS 4
/* HFS+ BTree misc info */
#define HFSPLUS_TREE_HEAD 0
#define HFSPLUS_NODE_MXSZ 32768
/* Some special File ID numbers (stolen from hfs.h) */
#define HFSPLUS_POR_CNID 1 /* Parent Of the Root */
#define HFSPLUS_ROOT_CNID 2 /* ROOT directory */
#define HFSPLUS_EXT_CNID 3 /* EXTents B-tree */
#define HFSPLUS_CAT_CNID 4 /* CATalog B-tree */
#define HFSPLUS_BAD_CNID 5 /* BAD blocks file */
#define HFSPLUS_ALLOC_CNID 6 /* ALLOCation file */
#define HFSPLUS_START_CNID 7 /* STARTup file */
#define HFSPLUS_ATTR_CNID 8 /* ATTRibutes file */
#define HFSPLUS_EXCH_CNID 15 /* ExchangeFiles temp id */
#define HFSPLUS_FIRSTUSER_CNID 16 /* first available user id */
/* HFS+ catalog entry key */
struct hfsplus_cat_key {
u16 key_len;
hfsplus_cnid parent;
struct hfsplus_unistr name;
} __packed;
/* Structs from hfs.h */
struct hfsp_point {
u16 v;
u16 h;
} __packed;
struct hfsp_rect {
u16 top;
u16 left;
u16 bottom;
u16 right;
} __packed;
/* HFS directory info (stolen from hfs.h */
struct DInfo {
struct hfsp_rect frRect;
u16 frFlags;
struct hfsp_point frLocation;
u16 frView;
} __packed;
struct DXInfo {
struct hfsp_point frScroll;
u32 frOpenChain;
u16 frUnused;
u16 frComment;
u32 frPutAway;
} __packed;
/* HFS+ folder data (part of an hfsplus_cat_entry) */
struct hfsplus_cat_folder {
s16 type;
u16 flags;
u32 valence;
hfsplus_cnid id;
u32 create_date;
u32 content_mod_date;
u32 attribute_mod_date;
u32 access_date;
u32 backup_date;
struct hfsplus_perm permissions;
struct DInfo user_info;
struct DXInfo finder_info;
u32 text_encoding;
u32 reserved;
} __packed;
/* HFS file info (stolen from hfs.h) */
struct FInfo {
u32 fdType;
u32 fdCreator;
u16 fdFlags;
struct hfsp_point fdLocation;
u16 fdFldr;
} __packed;
struct FXInfo {
u16 fdIconID;
u8 fdUnused[8];
u16 fdComment;
u32 fdPutAway;
} __packed;
/* HFS+ file data (part of a cat_entry) */
struct hfsplus_cat_file {
s16 type;
u16 flags;
u32 reserved1;
hfsplus_cnid id;
u32 create_date;
u32 content_mod_date;
u32 attribute_mod_date;
u32 access_date;
u32 backup_date;
struct hfsplus_perm permissions;
struct FInfo user_info;
struct FXInfo finder_info;
u32 text_encoding;
u32 reserved2;
struct hfsplus_fork_raw data_fork;
struct hfsplus_fork_raw rsrc_fork;
} __packed;
/* File attribute bits */
#define HFSPLUS_FILE_LOCKED 0x0001
#define HFSPLUS_FILE_THREAD_EXISTS 0x0002
/* HFS+ catalog thread (part of a cat_entry) */
struct hfsplus_cat_thread {
s16 type;
s16 reserved;
hfsplus_cnid parentID;
struct hfsplus_unistr nodeName;
} __packed;
#define HFSPLUS_MIN_THREAD_SZ 10
/* A data record in the catalog tree */
typedef union {
s16 type;
struct hfsplus_cat_folder folder;
struct hfsplus_cat_file file;
struct hfsplus_cat_thread thread;
} __packed hfsplus_cat_entry;
/* HFS+ catalog entry type */
#define HFSPLUS_FOLDER 0x0001
#define HFSPLUS_FILE 0x0002
#define HFSPLUS_FOLDER_THREAD 0x0003
#define HFSPLUS_FILE_THREAD 0x0004
/* HFS+ extents tree key */
struct hfsplus_ext_key {
u16 key_len;
u8 fork_type;
u8 pad;
hfsplus_cnid cnid;
u32 start_block;
} __packed;
#define HFSPLUS_EXT_KEYLEN 12
/* HFS+ generic BTree key */
typedef union {
u16 key_len;
struct hfsplus_cat_key cat;
struct hfsplus_ext_key ext;
} __packed hfsplus_btree_key;
#endif
This diff is collapsed.
/*
* linux/fs/hfsplus/ioctl.c
*
* Copyright (C) 2003
* Ethan Benson <erbenson@alaska.net>
* partially derived from linux/fs/ext2/ioctl.c
* Copyright (C) 1993, 1994, 1995
* Remy Card (card@masi.ibp.fr)
* Laboratoire MASI - Institut Blaise Pascal
* Universite Pierre et Marie Curie (Paris VI)
*
* hfsplus ioctls
*/
#include <linux/fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include "hfsplus_fs.h"
int hfsplus_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
unsigned long arg)
{
unsigned int flags;
switch (cmd) {
case HFSPLUS_IOC_EXT2_GETFLAGS:
flags = 0;
if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_IMMUTABLE)
flags |= EXT2_FLAG_IMMUTABLE; /* EXT2_IMMUTABLE_FL */
if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_APPEND)
flags |= EXT2_FLAG_APPEND; /* EXT2_APPEND_FL */
if (HFSPLUS_I(inode).userflags & HFSPLUS_FLG_NODUMP)
flags |= EXT2_FLAG_NODUMP; /* EXT2_NODUMP_FL */
return put_user(flags, (int *)arg);
case HFSPLUS_IOC_EXT2_SETFLAGS: {
if (IS_RDONLY(inode))
return -EROFS;
if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
return -EACCES;
if (get_user(flags, (int *)arg))
return -EFAULT;
if (flags & (EXT2_FLAG_IMMUTABLE|EXT2_FLAG_APPEND) ||
HFSPLUS_I(inode).rootflags & (HFSPLUS_FLG_IMMUTABLE|HFSPLUS_FLG_APPEND)) {
if (!capable(CAP_LINUX_IMMUTABLE))
return -EPERM;
}
/* don't silently ignore unsupported ext2 flags */
if (flags & ~(EXT2_FLAG_IMMUTABLE|EXT2_FLAG_APPEND|
EXT2_FLAG_NODUMP))
return -EOPNOTSUPP;
if (flags & EXT2_FLAG_IMMUTABLE) { /* EXT2_IMMUTABLE_FL */
inode->i_flags |= S_IMMUTABLE;
HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_IMMUTABLE;
} else {
inode->i_flags &= ~S_IMMUTABLE;
HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
}
if (flags & EXT2_FLAG_APPEND) { /* EXT2_APPEND_FL */
inode->i_flags |= S_APPEND;
HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_APPEND;
} else {
inode->i_flags &= ~S_APPEND;
HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_APPEND;
}
if (flags & EXT2_FLAG_NODUMP) /* EXT2_NODUMP_FL */
HFSPLUS_I(inode).userflags |= HFSPLUS_FLG_NODUMP;
else
HFSPLUS_I(inode).userflags &= ~HFSPLUS_FLG_NODUMP;
inode->i_ctime = CURRENT_TIME;
mark_inode_dirty(inode);
return 0;
}
default:
return -ENOTTY;
}
}
/*
* linux/fs/hfsplus/options.c
*
* Copyright (C) 2001
* Brad Boyer (flar@allandria.com)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Option parsing
*/
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include "hfsplus_fs.h"
/* Initialize an options object to reasonable defaults */
void fill_defaults(struct hfsplus_sb_info *opts)
{
if (!opts)
return;
opts->creator = HFSPLUS_DEF_CR_TYPE;
opts->type = HFSPLUS_DEF_CR_TYPE;
opts->umask = current->fs->umask;
opts->uid = current->uid;
opts->gid = current->gid;
opts->part = -1;
opts->session = -1;
}
/* convert a "four byte character" to a 32 bit int with error checks */
static int fill_fourchar(u32 *result, char *input)
{
u32 out;
int i;
if (!result || !input || !*input || (strlen(input) != 4))
return 0;
for (out = 0, i = 0; i < 4; i++) {
out <<= 8;
out |= ((int)(input[i])) & 0xFF;
}
*result = out;
return 1;
}
/* convert a string to int with error checks */
static int fill_int(int *result, char *input, int base)
{
char *tmp = input;
int intval;
if (!result || !input || !*input)
return 0;
intval = simple_strtoul(tmp, &tmp, base);
if (*tmp)
return 0;
*result = intval;
return 1;
}
/* Parse options from mount. Returns 0 on failure */
/* input is the options passed to mount() as a string */
int parse_options(char *input, struct hfsplus_sb_info *results)
{
char *curropt, *value;
int tmp;
if (!input)
return 1;
while ((curropt = strsep(&input,",")) != NULL) {
if (!*curropt)
continue;
if ((value = strchr(curropt, '=')) != NULL)
*value++ = '\0';
if (!strcmp(curropt, "creator")) {
if (!fill_fourchar(&(results->creator), value)) {
printk("HFS+-fs: creator requires a 4 character value\n");
return 0;
}
} else if (!strcmp(curropt, "type")) {
if (!fill_fourchar(&(results->type), value)) {
printk("HFS+-fs: type requires a 4 character value\n");
return 0;
}
} else if (!strcmp(curropt, "umask")) {
if (!fill_int(&tmp, value, 8)) {
printk("HFS+-fs: umask requires a value\n");
return 0;
}
results->umask = (umode_t)tmp;
} else if (!strcmp(curropt, "uid")) {
if (!fill_int(&tmp, value, 0)) {
printk("HFS+-fs: uid requires an argument\n");
return 0;
}
results->uid = (uid_t)tmp;
} else if (!strcmp(curropt, "gid")) {
if (!fill_int(&tmp, value, 0)) {
printk("HFS+-fs: gid requires an argument\n");
return 0;
}
results->gid = (gid_t)tmp;
} else if (!strcmp(curropt, "part")) {
if (!fill_int(&results->part, value, 0)) {
printk("HFS+-fs: part requires an argument\n");
return 0;
}
} else if (!strcmp(curropt, "session")) {
if (!fill_int(&results->session, value, 0)) {
printk("HFS+-fs: session requires an argument\n");
return 0;
}
} else {
printk("HFS+-fs: unknown option %s\n", curropt);
return 0;
}
}
return 1;
}
/*
* linux/fs/hfs/part_tbl.c
*
* Copyright (C) 1996-1997 Paul H. Hargrove
* This file may be distributed under the terms of the GNU General Public License.
*
* Original code to handle the new style Mac partition table based on
* a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
*
* In function preconditions the term "valid" applied to a pointer to
* a structure means that the pointer is non-NULL and the structure it
* points to has all fields initialized to consistent values.
*
*/
#include "hfsplus_fs.h"
/* offsets to various blocks */
#define HFS_DD_BLK 0 /* Driver Descriptor block */
#define HFS_PMAP_BLK 1 /* First block of partition map */
#define HFS_MDB_BLK 2 /* Block (w/i partition) of MDB */
/* magic numbers for various disk blocks */
#define HFS_DRVR_DESC_MAGIC 0x4552 /* "ER": driver descriptor map */
#define HFS_OLD_PMAP_MAGIC 0x5453 /* "TS": old-type partition map */
#define HFS_NEW_PMAP_MAGIC 0x504D /* "PM": new-type partition map */
#define HFS_SUPER_MAGIC 0x4244 /* "BD": HFS MDB (super block) */
#define HFS_MFS_SUPER_MAGIC 0xD2D7 /* MFS MDB (super block) */
/*
* The new style Mac partition map
*
* For each partition on the media there is a physical block (512-byte
* block) containing one of these structures. These blocks are
* contiguous starting at block 1.
*/
struct new_pmap {
u16 pmSig; /* signature */
u16 reSigPad; /* padding */
u32 pmMapBlkCnt; /* partition blocks count */
u32 pmPyPartStart; /* physical block start of partition */
u32 pmPartBlkCnt; /* physical block count of partition */
u8 pmPartName[32]; /* (null terminated?) string
giving the name of this
partition */
u8 pmPartType[32]; /* (null terminated?) string
giving the type of this
partition */
/* a bunch more stuff we don't need */
} __packed;
/*
* The old style Mac partition map
*
* The partition map consists for a 2-byte signature followed by an
* array of these structures. The map is terminated with an all-zero
* one of these.
*/
struct old_pmap {
u16 pdSig; /* Signature bytes */
struct old_pmap_entry {
u32 pdStart;
u32 pdSize;
u32 pdFSID;
} pdEntry[42];
} __packed;
/*
* hfs_part_find()
*
* Parse the partition map looking for the
* start and length of the 'part'th HFS partition.
*/
int hfs_part_find(struct super_block *sb,
sector_t *part_start, sector_t *part_size)
{
struct buffer_head *bh;
u16 *data;
int i, size, res;
res = -ENOENT;
bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK, data);
if (!bh)
return -EIO;
switch (be16_to_cpu(*data)) {
case HFS_OLD_PMAP_MAGIC:
{
struct old_pmap *pm;
struct old_pmap_entry *p;
pm = (struct old_pmap *)bh->b_data;
p = pm->pdEntry;
size = 42;
for (i = 0; i < size; p++, i++) {
if (p->pdStart && p->pdSize &&
p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
(HFSPLUS_SB(sb).part < 0 || HFSPLUS_SB(sb).part == i)) {
*part_start += be32_to_cpu(p->pdStart);
*part_size = be32_to_cpu(p->pdSize);
res = 0;
}
}
break;
}
case HFS_NEW_PMAP_MAGIC:
{
struct new_pmap *pm;
pm = (struct new_pmap *)bh->b_data;
size = be32_to_cpu(pm->pmMapBlkCnt);
for (i = 0; i < size;) {
if (!memcmp(pm->pmPartType,"Apple_HFS", 9) &&
(HFSPLUS_SB(sb).part < 0 || HFSPLUS_SB(sb).part == i)) {
*part_start += be32_to_cpu(pm->pmPyPartStart);
*part_size = be32_to_cpu(pm->pmPartBlkCnt);
res = 0;
break;
}
brelse(bh);
bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK + ++i, pm);
if (!bh)
return -EIO;
if (pm->pmSig != cpu_to_be16(HFS_NEW_PMAP_MAGIC))
break;
}
break;
}
}
brelse(bh);
return res;
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment