xref: /linux/fs/nilfs2/btree.h (revision 17c76b0104e4a6513983777e1a17e0297a12b0c4)
1*17c76b01SKoji Sato /*
2*17c76b01SKoji Sato  * btree.h - NILFS B-tree.
3*17c76b01SKoji Sato  *
4*17c76b01SKoji Sato  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5*17c76b01SKoji Sato  *
6*17c76b01SKoji Sato  * This program is free software; you can redistribute it and/or modify
7*17c76b01SKoji Sato  * it under the terms of the GNU General Public License as published by
8*17c76b01SKoji Sato  * the Free Software Foundation; either version 2 of the License, or
9*17c76b01SKoji Sato  * (at your option) any later version.
10*17c76b01SKoji Sato  *
11*17c76b01SKoji Sato  * This program is distributed in the hope that it will be useful,
12*17c76b01SKoji Sato  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*17c76b01SKoji Sato  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*17c76b01SKoji Sato  * GNU General Public License for more details.
15*17c76b01SKoji Sato  *
16*17c76b01SKoji Sato  * You should have received a copy of the GNU General Public License
17*17c76b01SKoji Sato  * along with this program; if not, write to the Free Software
18*17c76b01SKoji Sato  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19*17c76b01SKoji Sato  *
20*17c76b01SKoji Sato  * Written by Koji Sato <koji@osrg.net>.
21*17c76b01SKoji Sato  */
22*17c76b01SKoji Sato 
23*17c76b01SKoji Sato #ifndef _NILFS_BTREE_H
24*17c76b01SKoji Sato #define _NILFS_BTREE_H
25*17c76b01SKoji Sato 
26*17c76b01SKoji Sato #include <linux/types.h>
27*17c76b01SKoji Sato #include <linux/buffer_head.h>
28*17c76b01SKoji Sato #include <linux/list.h>
29*17c76b01SKoji Sato #include <linux/nilfs2_fs.h>
30*17c76b01SKoji Sato #include "btnode.h"
31*17c76b01SKoji Sato #include "bmap.h"
32*17c76b01SKoji Sato 
33*17c76b01SKoji Sato struct nilfs_btree;
34*17c76b01SKoji Sato struct nilfs_btree_path;
35*17c76b01SKoji Sato 
36*17c76b01SKoji Sato /**
37*17c76b01SKoji Sato  * struct nilfs_btree_operations - B-tree operation table
38*17c76b01SKoji Sato  */
39*17c76b01SKoji Sato struct nilfs_btree_operations {
40*17c76b01SKoji Sato 	__u64 (*btop_find_target)(const struct nilfs_btree *,
41*17c76b01SKoji Sato 				  const struct nilfs_btree_path *, __u64);
42*17c76b01SKoji Sato 	void (*btop_set_target)(struct nilfs_btree *, __u64, __u64);
43*17c76b01SKoji Sato 
44*17c76b01SKoji Sato 	struct the_nilfs *(*btop_get_nilfs)(struct nilfs_btree *);
45*17c76b01SKoji Sato 
46*17c76b01SKoji Sato 	int (*btop_propagate)(struct nilfs_btree *,
47*17c76b01SKoji Sato 			      struct nilfs_btree_path *,
48*17c76b01SKoji Sato 			      int,
49*17c76b01SKoji Sato 			      struct buffer_head *);
50*17c76b01SKoji Sato 	int (*btop_assign)(struct nilfs_btree *,
51*17c76b01SKoji Sato 			   struct nilfs_btree_path *,
52*17c76b01SKoji Sato 			   int,
53*17c76b01SKoji Sato 			   struct buffer_head **,
54*17c76b01SKoji Sato 			   sector_t,
55*17c76b01SKoji Sato 			   union nilfs_binfo *);
56*17c76b01SKoji Sato };
57*17c76b01SKoji Sato 
58*17c76b01SKoji Sato /**
59*17c76b01SKoji Sato  * struct nilfs_btree_node - B-tree node
60*17c76b01SKoji Sato  * @bn_flags: flags
61*17c76b01SKoji Sato  * @bn_level: level
62*17c76b01SKoji Sato  * @bn_nchildren: number of children
63*17c76b01SKoji Sato  * @bn_pad: padding
64*17c76b01SKoji Sato  */
65*17c76b01SKoji Sato struct nilfs_btree_node {
66*17c76b01SKoji Sato 	__u8 bn_flags;
67*17c76b01SKoji Sato 	__u8 bn_level;
68*17c76b01SKoji Sato 	__le16 bn_nchildren;
69*17c76b01SKoji Sato 	__le32 bn_pad;
70*17c76b01SKoji Sato };
71*17c76b01SKoji Sato 
72*17c76b01SKoji Sato /* flags */
73*17c76b01SKoji Sato #define NILFS_BTREE_NODE_ROOT	0x01
74*17c76b01SKoji Sato 
75*17c76b01SKoji Sato /* level */
76*17c76b01SKoji Sato #define NILFS_BTREE_LEVEL_DATA		0
77*17c76b01SKoji Sato #define NILFS_BTREE_LEVEL_NODE_MIN	(NILFS_BTREE_LEVEL_DATA + 1)
78*17c76b01SKoji Sato #define NILFS_BTREE_LEVEL_MAX		14
79*17c76b01SKoji Sato 
80*17c76b01SKoji Sato /**
81*17c76b01SKoji Sato  * struct nilfs_btree - B-tree structure
82*17c76b01SKoji Sato  * @bt_bmap: bmap base structure
83*17c76b01SKoji Sato  * @bt_ops: B-tree operation table
84*17c76b01SKoji Sato  */
85*17c76b01SKoji Sato struct nilfs_btree {
86*17c76b01SKoji Sato 	struct nilfs_bmap bt_bmap;
87*17c76b01SKoji Sato 
88*17c76b01SKoji Sato 	/* B-tree-specific members */
89*17c76b01SKoji Sato 	const struct nilfs_btree_operations *bt_ops;
90*17c76b01SKoji Sato };
91*17c76b01SKoji Sato 
92*17c76b01SKoji Sato 
93*17c76b01SKoji Sato #define NILFS_BTREE_ROOT_SIZE		NILFS_BMAP_SIZE
94*17c76b01SKoji Sato #define NILFS_BTREE_ROOT_NCHILDREN_MAX					\
95*17c76b01SKoji Sato 	((NILFS_BTREE_ROOT_SIZE - sizeof(struct nilfs_btree_node)) /	\
96*17c76b01SKoji Sato 	 (sizeof(__le64 /* dkey */) + sizeof(__le64 /* dptr */)))
97*17c76b01SKoji Sato #define NILFS_BTREE_ROOT_NCHILDREN_MIN	0
98*17c76b01SKoji Sato #define NILFS_BTREE_NODE_EXTRA_PAD_SIZE	(sizeof(__le64))
99*17c76b01SKoji Sato #define NILFS_BTREE_NODE_NCHILDREN_MAX(nodesize)			\
100*17c76b01SKoji Sato 	(((nodesize) - sizeof(struct nilfs_btree_node) -		\
101*17c76b01SKoji Sato 		NILFS_BTREE_NODE_EXTRA_PAD_SIZE) /			\
102*17c76b01SKoji Sato 	 (sizeof(__le64 /* dkey */) + sizeof(__le64 /* dptr */)))
103*17c76b01SKoji Sato #define NILFS_BTREE_NODE_NCHILDREN_MIN(nodesize)			\
104*17c76b01SKoji Sato 	((NILFS_BTREE_NODE_NCHILDREN_MAX(nodesize) - 1) / 2 + 1)
105*17c76b01SKoji Sato #define NILFS_BTREE_KEY_MIN	((__u64)0)
106*17c76b01SKoji Sato #define NILFS_BTREE_KEY_MAX	(~(__u64)0)
107*17c76b01SKoji Sato 
108*17c76b01SKoji Sato 
109*17c76b01SKoji Sato int nilfs_btree_path_cache_init(void);
110*17c76b01SKoji Sato void nilfs_btree_path_cache_destroy(void);
111*17c76b01SKoji Sato int nilfs_btree_init(struct nilfs_bmap *, __u64, __u64);
112*17c76b01SKoji Sato int nilfs_btree_convert_and_insert(struct nilfs_bmap *, __u64, __u64,
113*17c76b01SKoji Sato 				   const __u64 *, const __u64 *,
114*17c76b01SKoji Sato 				   int, __u64, __u64);
115*17c76b01SKoji Sato void nilfs_btree_init_gc(struct nilfs_bmap *);
116*17c76b01SKoji Sato 
117*17c76b01SKoji Sato #endif	/* _NILFS_BTREE_H */
118