Blame view

fs/nilfs2/bmap.h 7.78 KB
ae98043f5   Ryusuke Konishi   nilfs2: convert t...
1
  /* SPDX-License-Identifier: GPL-2.0+ */
bdb265eae   Koji Sato   nilfs2: integrate...
2
3
4
5
6
  /*
   * bmap.h - NILFS block mapping.
   *
   * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
   *
4b420ab4e   Ryusuke Konishi   nilfs2: clean up ...
7
   * Written by Koji Sato.
bdb265eae   Koji Sato   nilfs2: integrate...
8
9
10
11
12
13
14
15
   */
  
  #ifndef _NILFS_BMAP_H
  #define _NILFS_BMAP_H
  
  #include <linux/types.h>
  #include <linux/fs.h>
  #include <linux/buffer_head.h>
e63e88bc5   Ryusuke Konishi   nilfs2: move ioct...
16
  #include <linux/nilfs2_ondisk.h>	/* nilfs_binfo, nilfs_inode, etc */
bdb265eae   Koji Sato   nilfs2: integrate...
17
  #include "alloc.h"
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
18
  #include "dat.h"
bdb265eae   Koji Sato   nilfs2: integrate...
19
20
  
  #define NILFS_BMAP_INVALID_PTR	0
bdb265eae   Koji Sato   nilfs2: integrate...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #define nilfs_bmap_keydiff_abs(diff)	((diff) < 0 ? -(diff) : (diff))
  
  
  struct nilfs_bmap;
  
  /**
   * union nilfs_bmap_ptr_req - request for bmap ptr
   * @bpr_ptr: bmap pointer
   * @bpr_req: request for persistent allocator
   */
  union nilfs_bmap_ptr_req {
  	__u64 bpr_ptr;
  	struct nilfs_palloc_req bpr_req;
  };
  
  /**
   * struct nilfs_bmap_stats - bmap statistics
   * @bs_nblocks: number of blocks created or deleted
   */
  struct nilfs_bmap_stats {
  	unsigned int bs_nblocks;
  };
  
  /**
   * struct nilfs_bmap_operations - bmap operation table
   */
  struct nilfs_bmap_operations {
  	int (*bop_lookup)(const struct nilfs_bmap *, __u64, int, __u64 *);
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
49
  	int (*bop_lookup_contig)(const struct nilfs_bmap *, __u64, __u64 *,
0c6c44cb9   Ryusuke Konishi   nilfs2: avoid bar...
50
  				 unsigned int);
bdb265eae   Koji Sato   nilfs2: integrate...
51
52
53
  	int (*bop_insert)(struct nilfs_bmap *, __u64, __u64);
  	int (*bop_delete)(struct nilfs_bmap *, __u64);
  	void (*bop_clear)(struct nilfs_bmap *);
583ada476   Ryusuke Konishi   nilfs2: remove co...
54
  	int (*bop_propagate)(struct nilfs_bmap *, struct buffer_head *);
bdb265eae   Koji Sato   nilfs2: integrate...
55
56
57
58
59
60
61
62
  	void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
  					 struct list_head *);
  
  	int (*bop_assign)(struct nilfs_bmap *,
  			  struct buffer_head **,
  			  sector_t,
  			  union nilfs_binfo *);
  	int (*bop_mark)(struct nilfs_bmap *, __u64, int);
5b20384fb   Ryusuke Konishi   nilfs2: add bmap ...
63
  	int (*bop_seek_key)(const struct nilfs_bmap *, __u64, __u64 *);
bdb265eae   Koji Sato   nilfs2: integrate...
64
  	int (*bop_last_key)(const struct nilfs_bmap *, __u64 *);
5b20384fb   Ryusuke Konishi   nilfs2: add bmap ...
65
66
  
  	/* The following functions are internal use only. */
bdb265eae   Koji Sato   nilfs2: integrate...
67
68
69
70
  	int (*bop_check_insert)(const struct nilfs_bmap *, __u64);
  	int (*bop_check_delete)(struct nilfs_bmap *, __u64);
  	int (*bop_gather_data)(struct nilfs_bmap *, __u64 *, __u64 *, int);
  };
bdb265eae   Koji Sato   nilfs2: integrate...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  #define NILFS_BMAP_SIZE		(NILFS_INODE_BMAP_SIZE * sizeof(__le64))
  #define NILFS_BMAP_KEY_BIT	(sizeof(unsigned long) * 8 /* CHAR_BIT */)
  #define NILFS_BMAP_NEW_PTR_INIT	\
  	(1UL << (sizeof(unsigned long) * 8 /* CHAR_BIT */ - 1))
  
  static inline int nilfs_bmap_is_new_ptr(unsigned long ptr)
  {
  	return !!(ptr & NILFS_BMAP_NEW_PTR_INIT);
  }
  
  
  /**
   * struct nilfs_bmap - bmap structure
   * @b_u: raw data
   * @b_sem: semaphore
   * @b_inode: owner of bmap
   * @b_ops: bmap operation table
bdb265eae   Koji Sato   nilfs2: integrate...
88
89
   * @b_last_allocated_key: last allocated key for data block
   * @b_last_allocated_ptr: last allocated ptr for data block
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
90
   * @b_ptr_type: pointer type
bdb265eae   Koji Sato   nilfs2: integrate...
91
   * @b_state: state
5ad2686e9   Ryusuke Konishi   nilfs2: get maxim...
92
   * @b_nchildren_per_block: maximum number of child nodes for non-root nodes
bdb265eae   Koji Sato   nilfs2: integrate...
93
94
95
96
97
98
99
100
101
   */
  struct nilfs_bmap {
  	union {
  		__u8 u_flags;
  		__le64 u_data[NILFS_BMAP_SIZE / sizeof(__le64)];
  	} b_u;
  	struct rw_semaphore b_sem;
  	struct inode *b_inode;
  	const struct nilfs_bmap_operations *b_ops;
bdb265eae   Koji Sato   nilfs2: integrate...
102
103
  	__u64 b_last_allocated_key;
  	__u64 b_last_allocated_ptr;
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
104
  	int b_ptr_type;
bdb265eae   Koji Sato   nilfs2: integrate...
105
  	int b_state;
5ad2686e9   Ryusuke Konishi   nilfs2: get maxim...
106
  	__u16 b_nchildren_per_block;
bdb265eae   Koji Sato   nilfs2: integrate...
107
  };
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
108
109
  /* pointer type */
  #define NILFS_BMAP_PTR_P	0	/* physical block number (i.e. LBN) */
076a378ba   Ryusuke Konishi   nilfs2: fix block...
110
111
112
113
114
115
116
117
  #define NILFS_BMAP_PTR_VS	1	/*
  					 * virtual block number (single
  					 * version)
  					 */
  #define NILFS_BMAP_PTR_VM	2	/*
  					 * virtual block number (has multiple
  					 * versions)
  					 */
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
118
119
120
  #define NILFS_BMAP_PTR_U	(-1)	/* never perform pointer operations */
  
  #define NILFS_BMAP_USE_VBN(bmap)	((bmap)->b_ptr_type > 0)
bdb265eae   Koji Sato   nilfs2: integrate...
121
122
  /* state */
  #define NILFS_BMAP_DIRTY	0x00000001
f5974c8f8   Vyacheslav Dubeyko   nilfs2: add omitt...
123
124
125
126
127
128
129
  /**
   * struct nilfs_bmap_store - shadow copy of bmap state
   * @data: cached raw block mapping of on-disk inode
   * @last_allocated_key: cached value of last allocated key for data block
   * @last_allocated_ptr: cached value of last allocated ptr for data block
   * @state: cached value of state field of bmap structure
   */
a8070dd36   Ryusuke Konishi   nilfs2: add routi...
130
131
132
133
134
135
  struct nilfs_bmap_store {
  	__le64 data[NILFS_BMAP_SIZE / sizeof(__le64)];
  	__u64 last_allocated_key;
  	__u64 last_allocated_ptr;
  	int state;
  };
bdb265eae   Koji Sato   nilfs2: integrate...
136
137
138
139
  
  int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *);
  int nilfs_bmap_read(struct nilfs_bmap *, struct nilfs_inode *);
  void nilfs_bmap_write(struct nilfs_bmap *, struct nilfs_inode *);
0c6c44cb9   Ryusuke Konishi   nilfs2: avoid bar...
140
  int nilfs_bmap_lookup_contig(struct nilfs_bmap *, __u64, __u64 *, unsigned int);
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
141
142
  int nilfs_bmap_insert(struct nilfs_bmap *bmap, __u64 key, unsigned long rec);
  int nilfs_bmap_delete(struct nilfs_bmap *bmap, __u64 key);
5b20384fb   Ryusuke Konishi   nilfs2: add bmap ...
143
  int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, __u64 start, __u64 *keyp);
3568a13f4   Ryusuke Konishi   nilfs2: unify typ...
144
145
  int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp);
  int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key);
bdb265eae   Koji Sato   nilfs2: integrate...
146
147
148
149
150
151
152
153
154
  void nilfs_bmap_clear(struct nilfs_bmap *);
  int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
  void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct list_head *);
  int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
  		      unsigned long, union nilfs_binfo *);
  int nilfs_bmap_lookup_at_level(struct nilfs_bmap *, __u64, int, __u64 *);
  int nilfs_bmap_mark(struct nilfs_bmap *, __u64, int);
  
  void nilfs_bmap_init_gc(struct nilfs_bmap *);
bdb265eae   Koji Sato   nilfs2: integrate...
155

a8070dd36   Ryusuke Konishi   nilfs2: add routi...
156
157
  void nilfs_bmap_save(const struct nilfs_bmap *, struct nilfs_bmap_store *);
  void nilfs_bmap_restore(struct nilfs_bmap *, const struct nilfs_bmap_store *);
bdb265eae   Koji Sato   nilfs2: integrate...
158

0f3fe33b3   Ryusuke Konishi   nilfs2: convert n...
159
160
161
162
163
  static inline int nilfs_bmap_lookup(struct nilfs_bmap *bmap, __u64 key,
  				    __u64 *ptr)
  {
  	return nilfs_bmap_lookup_at_level(bmap, key, 1, ptr);
  }
bdb265eae   Koji Sato   nilfs2: integrate...
164
165
166
  /*
   * Internal use only
   */
c3a7abf06   Ryusuke Konishi   nilfs2: support c...
167
  struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *);
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
168
169
  
  static inline int nilfs_bmap_prepare_alloc_ptr(struct nilfs_bmap *bmap,
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
170
171
  					       union nilfs_bmap_ptr_req *req,
  					       struct inode *dat)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
172
  {
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
173
174
  	if (dat)
  		return nilfs_dat_prepare_alloc(dat, &req->bpr_req);
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
175
176
177
178
179
180
  	/* ignore target ptr */
  	req->bpr_ptr = bmap->b_last_allocated_ptr++;
  	return 0;
  }
  
  static inline void nilfs_bmap_commit_alloc_ptr(struct nilfs_bmap *bmap,
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
181
182
  					       union nilfs_bmap_ptr_req *req,
  					       struct inode *dat)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
183
  {
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
184
185
  	if (dat)
  		nilfs_dat_commit_alloc(dat, &req->bpr_req);
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
186
187
188
  }
  
  static inline void nilfs_bmap_abort_alloc_ptr(struct nilfs_bmap *bmap,
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
189
190
  					      union nilfs_bmap_ptr_req *req,
  					      struct inode *dat)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
191
  {
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
192
193
  	if (dat)
  		nilfs_dat_abort_alloc(dat, &req->bpr_req);
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
194
195
196
  	else
  		bmap->b_last_allocated_ptr--;
  }
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
197
  static inline int nilfs_bmap_prepare_end_ptr(struct nilfs_bmap *bmap,
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
198
199
  					     union nilfs_bmap_ptr_req *req,
  					     struct inode *dat)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
200
  {
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
201
  	return dat ? nilfs_dat_prepare_end(dat, &req->bpr_req) : 0;
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
202
203
204
  }
  
  static inline void nilfs_bmap_commit_end_ptr(struct nilfs_bmap *bmap,
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
205
206
  					     union nilfs_bmap_ptr_req *req,
  					     struct inode *dat)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
207
  {
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
208
209
210
  	if (dat)
  		nilfs_dat_commit_end(dat, &req->bpr_req,
  				     bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
211
212
213
  }
  
  static inline void nilfs_bmap_abort_end_ptr(struct nilfs_bmap *bmap,
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
214
215
  					    union nilfs_bmap_ptr_req *req,
  					    struct inode *dat)
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
216
  {
2e0c2c739   Ryusuke Konishi   nilfs2: allow btr...
217
218
  	if (dat)
  		nilfs_dat_abort_end(dat, &req->bpr_req);
d4b961576   Ryusuke Konishi   nilfs2: remove bm...
219
  }
bdb265eae   Koji Sato   nilfs2: integrate...
220

dc935be2a   Ryusuke Konishi   nilfs2: unify bma...
221
222
223
224
225
226
  static inline void nilfs_bmap_set_target_v(struct nilfs_bmap *bmap, __u64 key,
  					   __u64 ptr)
  {
  	bmap->b_last_allocated_key = key;
  	bmap->b_last_allocated_ptr = ptr;
  }
bdb265eae   Koji Sato   nilfs2: integrate...
227
228
229
230
231
  __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *,
  			      const struct buffer_head *);
  
  __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *, __u64);
  __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *);
bdb265eae   Koji Sato   nilfs2: integrate...
232

bdb265eae   Koji Sato   nilfs2: integrate...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  /* Assume that bmap semaphore is locked. */
  static inline int nilfs_bmap_dirty(const struct nilfs_bmap *bmap)
  {
  	return !!(bmap->b_state & NILFS_BMAP_DIRTY);
  }
  
  /* Assume that bmap semaphore is locked. */
  static inline void nilfs_bmap_set_dirty(struct nilfs_bmap *bmap)
  {
  	bmap->b_state |= NILFS_BMAP_DIRTY;
  }
  
  /* Assume that bmap semaphore is locked. */
  static inline void nilfs_bmap_clear_dirty(struct nilfs_bmap *bmap)
  {
  	bmap->b_state &= ~NILFS_BMAP_DIRTY;
  }
  
  
  #define NILFS_BMAP_LARGE	0x1
  
  #define NILFS_BMAP_SMALL_LOW	NILFS_DIRECT_KEY_MIN
  #define NILFS_BMAP_SMALL_HIGH	NILFS_DIRECT_KEY_MAX
  #define NILFS_BMAP_LARGE_LOW	NILFS_BTREE_ROOT_NCHILDREN_MAX
  #define NILFS_BMAP_LARGE_HIGH	NILFS_BTREE_KEY_MAX
  
  #endif	/* _NILFS_BMAP_H */