1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef __XFS_BTREE_H__
19#define __XFS_BTREE_H__
20
21struct xfs_buf;
22struct xfs_defer_ops;
23struct xfs_inode;
24struct xfs_mount;
25struct xfs_trans;
26
27extern kmem_zone_t *xfs_btree_cur_zone;
28
29
30
31
32
33
34
35union xfs_btree_ptr {
36 __be32 s;
37 __be64 l;
38};
39
40
41
42
43
44
45union xfs_btree_key {
46 struct xfs_bmbt_key bmbt;
47 xfs_bmdr_key_t bmbr;
48 xfs_alloc_key_t alloc;
49 struct xfs_inobt_key inobt;
50 struct xfs_rmap_key rmap;
51 struct xfs_rmap_key __rmap_bigkey[2];
52 struct xfs_refcount_key refc;
53};
54
55union xfs_btree_rec {
56 struct xfs_bmbt_rec bmbt;
57 xfs_bmdr_rec_t bmbr;
58 struct xfs_alloc_rec alloc;
59 struct xfs_inobt_rec inobt;
60 struct xfs_rmap_rec rmap;
61 struct xfs_refcount_rec refc;
62};
63
64
65
66
67#define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
68#define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
69#define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
70
71#define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
72#define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
73#define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
74#define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
75#define XFS_BTNUM_FINO ((xfs_btnum_t)XFS_BTNUM_FINOi)
76#define XFS_BTNUM_RMAP ((xfs_btnum_t)XFS_BTNUM_RMAPi)
77#define XFS_BTNUM_REFC ((xfs_btnum_t)XFS_BTNUM_REFCi)
78
79
80
81
82#define XFS_BB_MAGIC (1 << 0)
83#define XFS_BB_LEVEL (1 << 1)
84#define XFS_BB_NUMRECS (1 << 2)
85#define XFS_BB_LEFTSIB (1 << 3)
86#define XFS_BB_RIGHTSIB (1 << 4)
87#define XFS_BB_BLKNO (1 << 5)
88#define XFS_BB_LSN (1 << 6)
89#define XFS_BB_UUID (1 << 7)
90#define XFS_BB_OWNER (1 << 8)
91#define XFS_BB_NUM_BITS 5
92#define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
93#define XFS_BB_NUM_BITS_CRC 9
94#define XFS_BB_ALL_BITS_CRC ((1 << XFS_BB_NUM_BITS_CRC) - 1)
95
96
97
98
99#define XFS_BTREE_STATS_INC(cur, stat) \
100 XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat)
101#define XFS_BTREE_STATS_ADD(cur, stat, val) \
102 XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
103
104#define XFS_BTREE_MAXLEVELS 9
105
106struct xfs_btree_ops {
107
108 size_t key_len;
109 size_t rec_len;
110
111
112 struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
113 void (*update_cursor)(struct xfs_btree_cur *src,
114 struct xfs_btree_cur *dst);
115
116
117 void (*set_root)(struct xfs_btree_cur *cur,
118 union xfs_btree_ptr *nptr, int level_change);
119
120
121 int (*alloc_block)(struct xfs_btree_cur *cur,
122 union xfs_btree_ptr *start_bno,
123 union xfs_btree_ptr *new_bno,
124 int *stat);
125 int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
126
127
128 void (*update_lastrec)(struct xfs_btree_cur *cur,
129 struct xfs_btree_block *block,
130 union xfs_btree_rec *rec,
131 int ptr, int reason);
132
133
134 int (*get_minrecs)(struct xfs_btree_cur *cur, int level);
135 int (*get_maxrecs)(struct xfs_btree_cur *cur, int level);
136
137
138 int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
139
140
141 void (*init_key_from_rec)(union xfs_btree_key *key,
142 union xfs_btree_rec *rec);
143 void (*init_rec_from_cur)(struct xfs_btree_cur *cur,
144 union xfs_btree_rec *rec);
145 void (*init_ptr_from_cur)(struct xfs_btree_cur *cur,
146 union xfs_btree_ptr *ptr);
147 void (*init_high_key_from_rec)(union xfs_btree_key *key,
148 union xfs_btree_rec *rec);
149
150
151 __int64_t (*key_diff)(struct xfs_btree_cur *cur,
152 union xfs_btree_key *key);
153
154
155
156
157
158 __int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
159 union xfs_btree_key *key1,
160 union xfs_btree_key *key2);
161
162 const struct xfs_buf_ops *buf_ops;
163
164#if defined(DEBUG) || defined(XFS_WARN)
165
166 int (*keys_inorder)(struct xfs_btree_cur *cur,
167 union xfs_btree_key *k1,
168 union xfs_btree_key *k2);
169
170
171 int (*recs_inorder)(struct xfs_btree_cur *cur,
172 union xfs_btree_rec *r1,
173 union xfs_btree_rec *r2);
174#endif
175};
176
177
178
179
180#define LASTREC_UPDATE 0
181#define LASTREC_INSREC 1
182#define LASTREC_DELREC 2
183
184
185union xfs_btree_irec {
186 struct xfs_alloc_rec_incore a;
187 struct xfs_bmbt_irec b;
188 struct xfs_inobt_rec_incore i;
189 struct xfs_rmap_irec r;
190 struct xfs_refcount_irec rc;
191};
192
193
194union xfs_btree_cur_private {
195 struct {
196 unsigned long nr_ops;
197 int shape_changes;
198 } refc;
199};
200
201
202
203
204
205typedef struct xfs_btree_cur
206{
207 struct xfs_trans *bc_tp;
208 struct xfs_mount *bc_mp;
209 const struct xfs_btree_ops *bc_ops;
210 uint bc_flags;
211 union xfs_btree_irec bc_rec;
212 struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS];
213 int bc_ptrs[XFS_BTREE_MAXLEVELS];
214 __uint8_t bc_ra[XFS_BTREE_MAXLEVELS];
215#define XFS_BTCUR_LEFTRA 1
216#define XFS_BTCUR_RIGHTRA 2
217 __uint8_t bc_nlevels;
218 __uint8_t bc_blocklog;
219 xfs_btnum_t bc_btnum;
220 int bc_statoff;
221 union {
222 struct {
223 struct xfs_buf *agbp;
224 struct xfs_defer_ops *dfops;
225 xfs_agnumber_t agno;
226 union xfs_btree_cur_private priv;
227 } a;
228 struct {
229 struct xfs_inode *ip;
230 struct xfs_defer_ops *dfops;
231 xfs_fsblock_t firstblock;
232 int allocated;
233 short forksize;
234 char whichfork;
235 char flags;
236#define XFS_BTCUR_BPRV_WASDEL 1
237 } b;
238 } bc_private;
239} xfs_btree_cur_t;
240
241
242#define XFS_BTREE_LONG_PTRS (1<<0)
243#define XFS_BTREE_ROOT_IN_INODE (1<<1)
244#define XFS_BTREE_LASTREC_UPDATE (1<<2)
245#define XFS_BTREE_CRC_BLOCKS (1<<3)
246#define XFS_BTREE_OVERLAPPING (1<<4)
247
248
249#define XFS_BTREE_NOERROR 0
250#define XFS_BTREE_ERROR 1
251
252
253
254
255#define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr))
256
257
258
259
260
261int
262xfs_btree_check_block(
263 struct xfs_btree_cur *cur,
264 struct xfs_btree_block *block,
265 int level,
266 struct xfs_buf *bp);
267
268
269
270
271int
272xfs_btree_check_lptr(
273 struct xfs_btree_cur *cur,
274 xfs_fsblock_t ptr,
275 int level);
276
277
278
279
280void
281xfs_btree_del_cursor(
282 xfs_btree_cur_t *cur,
283 int error);
284
285
286
287
288
289int
290xfs_btree_dup_cursor(
291 xfs_btree_cur_t *cur,
292 xfs_btree_cur_t **ncur);
293
294
295
296
297
298struct xfs_buf *
299xfs_btree_get_bufl(
300 struct xfs_mount *mp,
301 struct xfs_trans *tp,
302 xfs_fsblock_t fsbno,
303 uint lock);
304
305
306
307
308
309struct xfs_buf *
310xfs_btree_get_bufs(
311 struct xfs_mount *mp,
312 struct xfs_trans *tp,
313 xfs_agnumber_t agno,
314 xfs_agblock_t agbno,
315 uint lock);
316
317
318
319
320int
321xfs_btree_islastblock(
322 xfs_btree_cur_t *cur,
323 int level);
324
325
326
327
328
329void
330xfs_btree_offsets(
331 __int64_t fields,
332 const short *offsets,
333 int nbits,
334 int *first,
335 int *last);
336
337
338
339
340
341int
342xfs_btree_read_bufl(
343 struct xfs_mount *mp,
344 struct xfs_trans *tp,
345 xfs_fsblock_t fsbno,
346 uint lock,
347 struct xfs_buf **bpp,
348 int refval,
349 const struct xfs_buf_ops *ops);
350
351
352
353
354
355void
356xfs_btree_reada_bufl(
357 struct xfs_mount *mp,
358 xfs_fsblock_t fsbno,
359 xfs_extlen_t count,
360 const struct xfs_buf_ops *ops);
361
362
363
364
365
366void
367xfs_btree_reada_bufs(
368 struct xfs_mount *mp,
369 xfs_agnumber_t agno,
370 xfs_agblock_t agbno,
371 xfs_extlen_t count,
372 const struct xfs_buf_ops *ops);
373
374
375
376
377void
378xfs_btree_init_block(
379 struct xfs_mount *mp,
380 struct xfs_buf *bp,
381 __u32 magic,
382 __u16 level,
383 __u16 numrecs,
384 __u64 owner,
385 unsigned int flags);
386
387void
388xfs_btree_init_block_int(
389 struct xfs_mount *mp,
390 struct xfs_btree_block *buf,
391 xfs_daddr_t blkno,
392 __u32 magic,
393 __u16 level,
394 __u16 numrecs,
395 __u64 owner,
396 unsigned int flags);
397
398
399
400
401int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
402int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
403int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
404int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
405int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
406int xfs_btree_insert(struct xfs_btree_cur *, int *);
407int xfs_btree_delete(struct xfs_btree_cur *, int *);
408int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
409int xfs_btree_change_owner(struct xfs_btree_cur *cur, __uint64_t new_owner,
410 struct list_head *buffer_list);
411
412
413
414
415void xfs_btree_lblock_calc_crc(struct xfs_buf *);
416bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
417void xfs_btree_sblock_calc_crc(struct xfs_buf *);
418bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
419
420
421
422
423void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
424void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
425
426
427
428
429static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
430{
431 return be16_to_cpu(block->bb_numrecs);
432}
433
434static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
435 __uint16_t numrecs)
436{
437 block->bb_numrecs = cpu_to_be16(numrecs);
438}
439
440static inline int xfs_btree_get_level(struct xfs_btree_block *block)
441{
442 return be16_to_cpu(block->bb_level);
443}
444
445
446
447
448
449#define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
450#define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
451#define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
452#define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
453#define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
454#define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
455#define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
456#define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
457
458#define XFS_FSB_SANITY_CHECK(mp,fsb) \
459 (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
460 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
461
462
463
464
465
466
467
468
469
470
471
472#define XFS_BTREE_TRACE_ARGBI(c, b, i)
473#define XFS_BTREE_TRACE_ARGBII(c, b, i, j)
474#define XFS_BTREE_TRACE_ARGI(c, i)
475#define XFS_BTREE_TRACE_ARGIPK(c, i, p, s)
476#define XFS_BTREE_TRACE_ARGIPR(c, i, p, r)
477#define XFS_BTREE_TRACE_ARGIK(c, i, k)
478#define XFS_BTREE_TRACE_ARGR(c, r)
479#define XFS_BTREE_TRACE_CURSOR(c, t)
480
481bool xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
482bool xfs_btree_sblock_verify(struct xfs_buf *bp, unsigned int max_recs);
483uint xfs_btree_compute_maxlevels(struct xfs_mount *mp, uint *limits,
484 unsigned long len);
485xfs_extlen_t xfs_btree_calc_size(struct xfs_mount *mp, uint *limits,
486 unsigned long long len);
487
488
489#define XFS_BTREE_QUERY_RANGE_CONTINUE 0
490#define XFS_BTREE_QUERY_RANGE_ABORT 1
491typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
492 union xfs_btree_rec *rec, void *priv);
493
494int xfs_btree_query_range(struct xfs_btree_cur *cur,
495 union xfs_btree_irec *low_rec, union xfs_btree_irec *high_rec,
496 xfs_btree_query_range_fn fn, void *priv);
497
498typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
499 void *data);
500int xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
501 xfs_btree_visit_blocks_fn fn, void *data);
502
503int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
504
505#endif
506