2005-04-17 05:20:36 +07:00
|
|
|
/*
|
|
|
|
* linux/fs/hfs/btree.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001
|
|
|
|
* Brad Boyer (flar@allandria.com)
|
|
|
|
* (C) 2003 Ardis Technologies <roman@ardistech.com>
|
|
|
|
*
|
|
|
|
* Handle opening/closing btree
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/pagemap.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
|
|
|
#include <linux/slab.h>
|
2007-05-08 14:24:30 +07:00
|
|
|
#include <linux/log2.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
#include "btree.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, btree_keycmp keycmp)
|
|
|
|
{
|
|
|
|
struct hfs_btree *tree;
|
|
|
|
struct hfs_btree_header_rec *head;
|
|
|
|
struct address_space *mapping;
|
|
|
|
struct page *page;
|
|
|
|
unsigned int size;
|
|
|
|
|
2006-09-27 15:49:37 +07:00
|
|
|
tree = kzalloc(sizeof(*tree), GFP_KERNEL);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (!tree)
|
|
|
|
return NULL;
|
|
|
|
|
2010-09-07 21:33:08 +07:00
|
|
|
mutex_init(&tree->tree_lock);
|
2005-04-17 05:20:36 +07:00
|
|
|
spin_lock_init(&tree->hash_lock);
|
|
|
|
/* Set the correct compare function */
|
|
|
|
tree->sb = sb;
|
|
|
|
tree->cnid = id;
|
|
|
|
tree->keycmp = keycmp;
|
|
|
|
|
|
|
|
tree->inode = iget_locked(sb, id);
|
|
|
|
if (!tree->inode)
|
|
|
|
goto free_tree;
|
2006-03-26 23:26:51 +07:00
|
|
|
BUG_ON(!(tree->inode->i_state & I_NEW));
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
|
|
|
|
HFS_I(tree->inode)->flags = 0;
|
2008-07-25 15:46:35 +07:00
|
|
|
mutex_init(&HFS_I(tree->inode)->extents_lock);
|
2005-04-17 05:20:36 +07:00
|
|
|
switch (id) {
|
|
|
|
case HFS_EXT_CNID:
|
|
|
|
hfs_inode_read_fork(tree->inode, mdb->drXTExtRec, mdb->drXTFlSize,
|
|
|
|
mdb->drXTFlSize, be32_to_cpu(mdb->drXTClpSiz));
|
2011-11-03 03:38:01 +07:00
|
|
|
if (HFS_I(tree->inode)->alloc_blocks >
|
|
|
|
HFS_I(tree->inode)->first_blocks) {
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_err("invalid btree extent records\n");
|
2011-11-03 03:38:01 +07:00
|
|
|
unlock_new_inode(tree->inode);
|
|
|
|
goto free_inode;
|
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
tree->inode->i_mapping->a_ops = &hfs_btree_aops;
|
|
|
|
break;
|
|
|
|
case HFS_CAT_CNID:
|
|
|
|
hfs_inode_read_fork(tree->inode, mdb->drCTExtRec, mdb->drCTFlSize,
|
|
|
|
mdb->drCTFlSize, be32_to_cpu(mdb->drCTClpSiz));
|
2011-11-03 03:38:01 +07:00
|
|
|
|
|
|
|
if (!HFS_I(tree->inode)->first_blocks) {
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_err("invalid btree extent records (0 size)\n");
|
2011-11-03 03:38:01 +07:00
|
|
|
unlock_new_inode(tree->inode);
|
|
|
|
goto free_inode;
|
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
tree->inode->i_mapping->a_ops = &hfs_btree_aops;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unlock_new_inode(tree->inode);
|
|
|
|
|
|
|
|
mapping = tree->inode->i_mapping;
|
2006-06-23 16:05:08 +07:00
|
|
|
page = read_mapping_page(mapping, 0, NULL);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (IS_ERR(page))
|
2008-01-18 06:21:09 +07:00
|
|
|
goto free_inode;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/* 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;
|
2007-05-08 14:24:30 +07:00
|
|
|
if (!is_power_of_2(size))
|
2005-04-17 05:20:36 +07:00
|
|
|
goto fail_page;
|
|
|
|
if (!tree->node_count)
|
|
|
|
goto fail_page;
|
2008-02-06 16:37:10 +07:00
|
|
|
switch (id) {
|
|
|
|
case HFS_EXT_CNID:
|
|
|
|
if (tree->max_key_len != HFS_MAX_EXT_KEYLEN) {
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_err("invalid extent max_key_len %d\n",
|
|
|
|
tree->max_key_len);
|
2008-02-06 16:37:10 +07:00
|
|
|
goto fail_page;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HFS_CAT_CNID:
|
|
|
|
if (tree->max_key_len != HFS_MAX_CAT_KEYLEN) {
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_err("invalid catalog max_key_len %d\n",
|
|
|
|
tree->max_key_len);
|
2008-02-06 16:37:10 +07:00
|
|
|
goto fail_page;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
2008-01-09 06:33:20 +07:00
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
tree->node_size_shift = ffs(size) - 1;
|
|
|
|
tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
|
|
|
|
|
|
|
|
kunmap(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
return tree;
|
|
|
|
|
2008-01-18 06:21:09 +07:00
|
|
|
fail_page:
|
2005-04-17 05:20:36 +07:00
|
|
|
page_cache_release(page);
|
2008-01-18 06:21:09 +07:00
|
|
|
free_inode:
|
2008-01-09 06:33:20 +07:00
|
|
|
tree->inode->i_mapping->a_ops = &hfs_aops;
|
2005-04-17 05:20:36 +07:00
|
|
|
iput(tree->inode);
|
2008-01-18 06:21:09 +07:00
|
|
|
free_tree:
|
2005-04-17 05:20:36 +07:00
|
|
|
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))
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_err("node %d:%d still has %d user(s)!\n",
|
|
|
|
node->tree->cnid, node->this,
|
|
|
|
atomic_read(&node->refcnt));
|
2005-04-17 05:20:36 +07:00
|
|
|
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;
|
|
|
|
__be32 cnid;
|
|
|
|
|
|
|
|
node = hfs_bnode_create(tree, idx);
|
|
|
|
if (IS_ERR(node))
|
|
|
|
return node;
|
|
|
|
|
|
|
|
if (!tree->free_nodes)
|
|
|
|
panic("FIXME!!!");
|
|
|
|
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;
|
2008-04-30 14:54:53 +07:00
|
|
|
unsigned off;
|
|
|
|
u16 off16;
|
|
|
|
u16 len;
|
2005-04-17 05:20:36 +07:00
|
|
|
u8 *data, byte, m;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (!tree->free_nodes) {
|
|
|
|
struct inode *inode = tree->inode;
|
|
|
|
u32 count;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = hfs_extend_file(inode);
|
|
|
|
if (res)
|
|
|
|
return ERR_PTR(res);
|
|
|
|
HFS_I(inode)->phys_size = inode->i_size =
|
|
|
|
(loff_t)HFS_I(inode)->alloc_blocks *
|
|
|
|
HFS_SB(tree->sb)->alloc_blksz;
|
|
|
|
HFS_I(inode)->fs_blocks = inode->i_size >>
|
|
|
|
tree->sb->s_blocksize_bits;
|
|
|
|
inode_set_bytes(inode, inode->i_size);
|
|
|
|
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;
|
2008-04-30 14:54:53 +07:00
|
|
|
len = hfs_brec_lenoff(node, 2, &off16);
|
|
|
|
off = off16;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
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);
|
|
|
|
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) {
|
2013-05-01 05:27:55 +07:00
|
|
|
printk(KERN_DEBUG "create new bmap node...\n");
|
2005-04-17 05:20:36 +07:00
|
|
|
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;
|
|
|
|
|
2008-04-30 14:54:53 +07:00
|
|
|
len = hfs_brec_lenoff(node, 0, &off16);
|
|
|
|
off = off16;
|
2005-04-17 05:20:36 +07:00
|
|
|
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;
|
|
|
|
|
2013-05-01 05:27:54 +07:00
|
|
|
hfs_dbg(BNODE_MOD, "btree_free_node: %u\n", node->this);
|
2005-04-17 05:20:36 +07:00
|
|
|
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 */;
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_crit("unable to free bnode %u. bmap not found!\n",
|
|
|
|
node->this);
|
2005-04-17 05:20:36 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
node = hfs_bnode_find(tree, i);
|
|
|
|
if (IS_ERR(node))
|
|
|
|
return;
|
|
|
|
if (node->type != HFS_NODE_MAP) {
|
|
|
|
/* panic */;
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_crit("invalid bmap found! (%u,%d)\n",
|
|
|
|
node->this, node->type);
|
2005-04-17 05:20:36 +07:00
|
|
|
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)) {
|
2013-05-01 05:27:55 +07:00
|
|
|
pr_crit("trying to free free bnode %u(%d)\n",
|
|
|
|
node->this, node->type);
|
2005-04-17 05:20:36 +07:00
|
|
|
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);
|
|
|
|
}
|