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