login
Header Space

 
 

[PATCH take 2 11/28] UBIFS: add key helpers

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: LKML <linux-kernel@...>
Cc: Adrian Hunter <ext-adrian.hunter@...>, Artem Bityutskiy <Artem.Bityutskiy@...>
Date: Tuesday, May 6, 2008 - 6:35 am

This file implement various helper functions to work with UBIFS keys.
The keys are part of the UBIFS index which is a B-tree. For example,
directory entry key consists of the parent inode number and directory
entry hash.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: Adrian Hunter <ext-adrian.hunter@nokia.com>
---
 fs/ubifs/key.h |  507 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 507 insertions(+), 0 deletions(-)

diff --git a/fs/ubifs/key.h b/fs/ubifs/key.h
new file mode 100644
index 0000000..7c5bed7
--- /dev/null
+++ b/fs/ubifs/key.h
@@ -0,0 +1,507 @@
+/*
+ * This file is part of UBIFS.
+ *
+ * Copyright (C) 2006-2008 Nokia Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Artem Bityutskiy (Битюцкий Артём)
+ *          Adrian Hunter
+ */
+
+/*
+ * This header contains various key-related definitions and helper function.
+ * UBIFS allows several key schemes, so we access key fields only via these
+ * helpers. At the moment only one key scheme is supported.
+ *
+ * Simple key scheme
+ * ~~~~~~~~~~~~~~~~~
+ *
+ * Keys are 64-bits long. First 32-bits are inode number (parent inode number
+ * in case of direntry key). Next 3 bits are node type. The last 29 bits are
+ * 4KiB offset in case of inode node, and direntry hash in case of a direntry
+ * node. We use "r5" hash borrowed from reiserfs.
+ */
+
+#ifndef __UBIFS_KEY_H__
+#define __UBIFS_KEY_H__
+
+/**
+ * key_r5_hash - R5 hash function (borrowed from reiserfs).
+ * @s: direntry name
+ * @len: name length
+ */
+static inline uint32_t key_r5_hash(const char *s, int len)
+{
+	uint32_t a = 0;
+	const signed char *str = (const signed char *)s;
+
+	while (*str) {
+		a += *str << 4;
+		a += *str >> 4;
+		a *= 11;
+		str++;
+	}
+
+	/*
+	 * We use hash values as offset in directories, so values %0 and %1 are
+	 * reserved for "." and "..". Value %2 is also reserved for purposes of
+	 * 'ubifs_readdir()'.
+	 */
+	if (unlikely(a >= 0 && a <= 2))
+		a += 3;
+	return a;
+}
+
+/**
+ * key_test_hash - testing hash function.
+ * @str: direntry name
+ * @len: name length
+ */
+static inline uint32_t key_test_hash(const char *str, int len)
+{
+	uint32_t a = 0;
+
+	len = min_t(uint32_t, len, 4);
+	memcpy(&a, str, len);
+	if (unlikely(a >= 0 && a <= 2))
+		a += 3;
+	return a;
+}
+
+/**
+ * ino_key_init - initialize inode key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: inode number
+ */
+static inline void ino_key_init(const struct ubifs_info *c,
+				union ubifs_key *key, ino_t inum)
+{
+	key->u32[0] = inum;
+	key->u32[1] = UBIFS_INO_KEY << 29;
+}
+
+/**
+ * ino_key_init_flash - initialize on-flash inode key.
+ * @c: UBIFS file-system description object
+ * @k: key to initialize
+ * @inum: inode number
+ */
+static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
+				      ino_t inum)
+{
+	union ubifs_key *key = k;
+
+	key->j32[0] = cpu_to_le32(inum);
+	key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << 29);
+	memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
+}
+
+/**
+ * lowest_ino_key - get the lowest possible inode key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: inode number
+ */
+static inline void lowest_ino_key(const struct ubifs_info *c,
+				union ubifs_key *key, ino_t inum)
+{
+	key->u32[0] = inum;
+	key->u32[1] = 0;
+}
+
+/**
+ * highest_ino_key - get the highest possible inode key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: inode number
+ */
+static inline void highest_ino_key(const struct ubifs_info *c,
+				union ubifs_key *key, ino_t inum)
+{
+	key->u32[0] = inum;
+	key->u32[1] = 0xffffffff;
+}
+
+/**
+ * dent_key_init - initialize directory entry key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: parent inode number
+ * @nm: direntry name and length
+ */
+static inline void dent_key_init(const struct ubifs_info *c,
+				 union ubifs_key *key, ino_t inum,
+				 const struct qstr *nm)
+{
+	uint32_t hash = c->key_hash(nm->name, nm->len);
+
+	key->u32[0] = inum;
+	key->u32[1] = (hash & 0x01FFFFFF) | (UBIFS_DENT_KEY << 29);
+}
+
+/**
+ * dent_key_init_hash - initialize directory entry key without re-calculating
+ *                      hash function.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: parent inode number
+ * @hash: direntry name hash
+ */
+static inline void dent_key_init_hash(const struct ubifs_info *c,
+				      union ubifs_key *key, ino_t inum,
+				      uint32_t hash)
+{
+	key->u32[0] = inum;
+	key->u32[1] = (hash & 0x01FFFFFF) | (UBIFS_DENT_KEY << 29);
+}
+
+/**
+ * dent_key_init_flash - initialize on-flash directory entry key.
+ * @c: UBIFS file-system description object
+ * @k: key to initialize
+ * @inum: parent inode number
+ * @nm: direntry name and length
+ */
+static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
+				       ino_t inum, const struct qstr *nm)
+{
+	union ubifs_key *key = k;
+	uint32_t hash = c->key_hash(nm->name, nm->len);
+
+	key->j32[0] = cpu_to_le32(inum);
+	key->j32[1] = cpu_to_le32((hash & 0x01FFFFFF) | (UBIFS_DENT_KEY << 29));
+	memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
+}
+
+/**
+ * lowest_dent_key - get the lowest possible directory entry key.
+ * @c: UBIFS file-system description object
+ * @key: where to store the lowest key
+ * @inum: parent inode number
+ */
+static inline void lowest_dent_key(const struct ubifs_info *c,
+				   union ubifs_key *key, ino_t inum)
+{
+	key->u32[0] = inum;
+	key->u32[1] = UBIFS_DENT_KEY << 29;
+}
+
+/**
+ * xent_key_init - initialize extended attribute entry key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: host inode number
+ * @nm: extended attribute entry name and length
+ */
+static inline void xent_key_init(const struct ubifs_info *c,
+				 union ubifs_key *key, ino_t inum,
+				 const struct qstr *nm)
+{
+	uint32_t hash = c->key_hash(nm->name, nm->len);
+
+	key->u32[0] = inum;
+	key->u32[1] = (hash & 0x01FFFFFF) | (UBIFS_XENT_KEY << 29);
+}
+
+/**
+ * xent_key_init_hash - initialize extended attribute entry key without
+ *                      re-calculating hash function.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: host inode number
+ * @hash: extended attribute entry name hash
+ */
+static inline void xent_key_init_hash(const struct ubifs_info *c,
+				      union ubifs_key *key, ino_t inum,
+				      uint32_t hash)
+{
+	key->u32[0] = inum;
+	key->u32[1] = (hash & 0x01FFFFFF) | (UBIFS_XENT_KEY << 29);
+}
+
+/**
+ * xent_key_init_flash - initialize on-flash extended attribute entry key.
+ * @c: UBIFS file-system description object
+ * @k: key to initialize
+ * @inum: host inode number
+ * @nm: extended attribute entry name and length
+ */
+static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
+				       ino_t inum, const struct qstr *nm)
+{
+	union ubifs_key *key = k;
+	uint32_t hash = c->key_hash(nm->name, nm->len);
+
+	key->j32[0] = cpu_to_le32(inum);
+	key->j32[1] = cpu_to_le32((hash & 0x01FFFFFF) | (UBIFS_XENT_KEY << 29));
+	memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
+}
+
+/**
+ * lowest_xent_key - get the lowest possible extended attribute entry key.
+ * @c: UBIFS file-system description object
+ * @key: where to store the lowest key
+ * @inum: host inode number
+ */
+static inline void lowest_xent_key(const struct ubifs_info *c,
+				   union ubifs_key *key, ino_t inum)
+{
+	key->u32[0] = inum;
+	key->u32[1] = UBIFS_XENT_KEY << 29;
+}
+
+/**
+ * data_key_init - initialize data key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: inode number
+ * @block: block number
+ */
+static inline void data_key_init(const struct ubifs_info *c,
+				 union ubifs_key *key, ino_t inum,
+				 unsigned int block)
+{
+	key->u32[0] = inum;
+	key->u32[1] = (block & 0x01FFFFFF) | (UBIFS_DATA_KEY << 29);
+}
+
+/**
+ * data_key_init_flash - initialize on-flash data key.
+ * @c: UBIFS file-system description object
+ * @k: key to initialize
+ * @inum: inode number
+ * @block: block number
+ */
+static inline void data_key_init_flash(const struct ubifs_info *c, void *k,
+				       ino_t inum, unsigned int block)
+{
+	union ubifs_key *key = k;
+
+	key->j32[0] = cpu_to_le32(inum);
+	key->j32[1] = cpu_to_le32((block & 0x01FFFFFF) |
+				  (UBIFS_DATA_KEY << 29));
+	memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
+}
+
+/**
+ * trun_key_init - initialize truncation node key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ * @inum: inode number
+ */
+static inline void trun_key_init(const struct ubifs_info *c,
+				 union ubifs_key *key, ino_t inum)
+{
+	key->u32[0] = inum;
+	key->u32[1] = UBIFS_TRUN_KEY << 29;
+}
+
+/**
+ * trun_key_init_flash - initialize on-flash truncation node key.
+ * @c: UBIFS file-system description object
+ * @k: key to initialize
+ * @inum: inode number
+ */
+static inline void trun_key_init_flash(const struct ubifs_info *c, void *k,
+				       ino_t inum)
+{
+	union ubifs_key *key = k;
+
+	key->j32[0] = cpu_to_le32(inum);
+	key->j32[1] = cpu_to_le32(UBIFS_TRUN_KEY << 29);
+	memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
+}
+
+/**
+ * key_type - get key type.
+ * @c: UBIFS file-system description object
+ * @key: key to get type of
+ */
+static inline int key_type(const struct ubifs_info *c,
+			   const union ubifs_key *key)
+{
+	return key->u32[1] >> 29;
+}
+
+/**
+ * key_type_flash - get type of a on-flash formatted key.
+ * @c: UBIFS file-system description object
+ * @k: key to get type of
+ */
+static inline int key_type_flash(const struct ubifs_info *c, const void *k)
+{
+	const union ubifs_key *key = k;
+
+	return le32_to_cpu(key->u32[1]) >> 29;
+}
+
+/**
+ * key_ino - fetch inode number from key.
+ * @c: UBIFS file-system description object
+ * @k: key to fetch inode number from
+ */
+static inline ino_t key_ino(const struct ubifs_info *c, const void *k)
+{
+	const union ubifs_key *key = k;
+
+	return key->u32[0];
+}
+
+/**
+ * key_ino_flash - fetch inode number from an on-flash formatted key.
+ * @c: UBIFS file-system description object
+ * @k: key to fetch inode number from
+ */
+static inline ino_t key_ino_flash(const struct ubifs_info *c, const void *k)
+{
+	const union ubifs_key *key = k;
+
+	return le32_to_cpu(key->j32[0]);
+}
+
+/**
+ * key_hash - get directory entry hash.
+ * @c: UBIFS file-system description object
+ * @key: the key to get hash from
+ */
+static inline int key_hash(const struct ubifs_info *c,
+			   const union ubifs_key *key)
+{
+	return key->u32[1] & 0x01FFFFFF;
+}
+
+/**
+ * key_hash_flash - get directory entry hash from an on-flash formatted key.
+ * @c: UBIFS file-system description object
+ * @k: the key to get hash from
+ */
+static inline int key_hash_flash(const struct ubifs_info *c, const void *k)
+{
+	const union ubifs_key *key = k;
+
+	return le32_to_cpu(key->j32[1]) & 0x01FFFFFF;
+}
+
+/**
+ * key_block - get data block number.
+ * @c: UBIFS file-system description object
+ * @key: the key to get the block number from
+ */
+static inline unsigned int key_block(const struct ubifs_info *c,
+				     const union ubifs_key *key)
+{
+	return key->u32[1] & 0x01FFFFFF;
+}
+
+/**
+ * key_read - transform a key to in-memory format.
+ * @c: UBIFS file-system description object
+ * @from: the key to transform
+ * @to: the key to store the result
+ */
+static inline void key_read(const struct ubifs_info *c, const void *from,
+			    union ubifs_key *to)
+{
+	const union ubifs_key *f = from;
+
+	to->u32[0] = le32_to_cpu(f->j32[0]);
+	to->u32[1] = le32_to_cpu(f->j32[1]);
+}
+
+/**
+ * key_write - transform a key from in-memory format.
+ * @c: UBIFS file-system description object
+ * @from: the key to transform
+ * @to: the key to store the result
+ */
+static inline void key_write(const struct ubifs_info *c,
+			     const union ubifs_key *from, void *to)
+{
+	union ubifs_key *t = to;
+
+	t->j32[0] = cpu_to_le32(from->u32[0]);
+	t->j32[1] = cpu_to_le32(from->u32[1]);
+	memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
+}
+
+/**
+ * key_write_idx - transform a key from in-memory format for the index.
+ * @c: UBIFS file-system description object
+ * @from: the key to transform
+ * @to: the key to store the result
+ */
+static inline void key_write_idx(const struct ubifs_info *c,
+				 const union ubifs_key *from, void *to)
+{
+	union ubifs_key *t = to;
+
+	t->j32[0] = cpu_to_le32(from->u32[0]);
+	t->j32[1] = cpu_to_le32(from->u32[1]);
+}
+
+/**
+ * key_copy - copy a key.
+ * @c: UBIFS file-system description object
+ * @from: the key to copy from
+ * @to: the key to copy to
+ */
+static inline void key_copy(const struct ubifs_info *c,
+			    const union ubifs_key *from, union ubifs_key *to)
+{
+	to->u64[0] = from->u64[0];
+}
+
+/**
+ * keys_cmp - compare keys.
+ * @c: UBIFS file-system description object
+ * @key1: the first key to compare
+ * @key2: the second key to compare
+ *
+ * This function compares 2 keys and returns %-1 if @key1 is less than
+ * @key2, 0 if the keys are equivalent and %1 if @key1 is greater than @key2.
+ */
+static inline int keys_cmp(const struct ubifs_info *c,
+			   const union ubifs_key *key1,
+			   const union ubifs_key *key2)
+{
+	if (key1->u32[0] < key2->u32[0])
+		return -1;
+	if (key1->u32[0] > key2->u32[0])
+		return 1;
+	if (key1->u32[1] < key2->u32[1])
+		return -1;
+	if (key1->u32[1] > key2->u32[1])
+		return 1;
+
+	return 0;
+}
+
+/**
+ * is_hash_key - is a key vulnerable to hash collisions.
+ * @c: UBIFS file-system description object
+ * @key: key
+ *
+ * This function returns %1 if @key is a hashed key or %0 otherwise.
+ */
+static inline int is_hash_key(const struct ubifs_info *c,
+			      const union ubifs_key *key)
+{
+	int type = key_type(c, key);
+
+	return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
+}
+
+#endif /* !__UBIFS_KEY_H__ */
-- 
1.5.4.1

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Tue May 6, 6:35 am)
Re: [PATCH take 2] UBIFS - new flash file system, Christoph Hellwig, (Fri May 16, 6:40 am)
Re: [PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Fri May 23, 11:18 am)
Re: [PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Mon May 19, 7:30 am)
Re: [PATCH take 2] UBIFS - new flash file system, Andi Kleen, (Mon May 19, 8:36 am)
Re: [PATCH take 2] UBIFS - new flash file system, Adrian Hunter, (Fri May 16, 9:10 am)
Re: [PATCH take 2] UBIFS - new flash file system, Christoph Hellwig, (Wed May 7, 4:01 am)
Re: [PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Wed May 7, 4:07 am)
Re: [PATCH take 2] UBIFS - new flash file system, Christoph Hellwig, (Wed May 7, 4:10 am)
Re: [PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Wed May 7, 6:31 am)
Re: [PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Wed May 7, 4:32 am)
Re: [PATCH take 2] UBIFS - new flash file system, Artem Bityutskiy, (Wed May 7, 4:11 am)
[PATCH take 2 21/28] UBIFS: add Garbage Collector, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 27/28] UBIFS: add debugging stuff, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 26/28] UBIFS: add header files, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 18/28] UBIFS: add LEB properties tree, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 23/28] UBIFS: add budgeting, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 22/28] UBIFS: add VFS operations, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 24/28] UBIFS: add extended attribute support, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 28/28] UBIFS: include FS to compilation, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 25/28] UBIFS: add orphans handling sub-system, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 20/28] UBIFS: add LEB find subsystem, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 19/28] UBIFS: add LEB properties tree, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 17/28] UBIFS: add LEB properties, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 15/28] UBIFS: add TNC commit implementation, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 02/28] do_mounts: allow UBI root device name, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Tue May 6, 6:35 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Tue May 20, 8:45 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Thu May 22, 6:53 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Thu May 22, 7:00 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Wed May 7, 12:55 pm)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Christoph Hellwig, (Wed May 7, 4:00 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Tue May 13, 4:31 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Christoph Hellwig, (Tue May 13, 4:59 am)
Re: [PATCH take 2 01/28] VFS: introduce writeback_inodes_sb(), Artem Bityutskiy, (Tue May 13, 5:22 am)
[PATCH take 2 05/28] UBIFS: add flash scanning, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 03/28] UBIFS: add brief documentation, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 16/28] UBIFS: add TNC shrinker, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 14/28] UBIFS: add TNC implementation, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 12/28] UBIFS: add the journal, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 13/28] UBIFS: add commit functionality, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 11/28] UBIFS: add key helpers, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 09/28] UBIFS: add file-system recovery, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 08/28] UBIFS: add superblock and master node, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 04/28] UBIFS: add I/O sub-system, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 07/28] UBIFS: add file-system build, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 10/28] UBIFS: add compression support, Artem Bityutskiy, (Tue May 6, 6:35 am)
[PATCH take 2 06/28] UBIFS: add journal replay, Artem Bityutskiy, (Tue May 6, 6:35 am)
Re: [PATCH take 2 06/28] UBIFS: add journal replay, Marcin Slusarz, (Tue May 6, 6:05 pm)
Re: [PATCH take 2 06/28] UBIFS: add journal replay, Artem Bityutskiy, (Wed May 7, 1:57 am)
speck-geostationary