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_bmap_free;
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
40union xfs_btree_key {
41 xfs_bmbt_key_t bmbt;
42 xfs_bmdr_key_t bmbr;
43 xfs_alloc_key_t alloc;
44 xfs_inobt_key_t inobt;
45};
46
47union xfs_btree_rec {
48 xfs_bmbt_rec_t bmbt;
49 xfs_bmdr_rec_t bmbr;
50 xfs_alloc_rec_t alloc;
51 xfs_inobt_rec_t inobt;
52};
53
54
55
56
57#define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
58#define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
59#define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
60
61#define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
62#define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
63#define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
64#define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
65#define XFS_BTNUM_FINO ((xfs_btnum_t)XFS_BTNUM_FINOi)
66
67
68
69
70#define XFS_BB_MAGIC (1 << 0)
71#define XFS_BB_LEVEL (1 << 1)
72#define XFS_BB_NUMRECS (1 << 2)
73#define XFS_BB_LEFTSIB (1 << 3)
74#define XFS_BB_RIGHTSIB (1 << 4)
75#define XFS_BB_BLKNO (1 << 5)
76#define XFS_BB_LSN (1 << 6)
77#define XFS_BB_UUID (1 << 7)
78#define XFS_BB_OWNER (1 << 8)
79#define XFS_BB_NUM_BITS 5
80#define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
81#define XFS_BB_NUM_BITS_CRC 9
82#define XFS_BB_ALL_BITS_CRC ((1 << XFS_BB_NUM_BITS_CRC) - 1)
83
84
85
86
87#define __XFS_BTREE_STATS_INC(type, stat) \
88 XFS_STATS_INC(xs_ ## type ## _2_ ## stat)
89#define XFS_BTREE_STATS_INC(cur, stat) \
90do { \
91 switch (cur->bc_btnum) { \
92 case XFS_BTNUM_BNO: __XFS_BTREE_STATS_INC(abtb, stat); break; \
93 case XFS_BTNUM_CNT: __XFS_BTREE_STATS_INC(abtc, stat); break; \
94 case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_INC(bmbt, stat); break; \
95 case XFS_BTNUM_INO: __XFS_BTREE_STATS_INC(ibt, stat); break; \
96 case XFS_BTNUM_FINO: __XFS_BTREE_STATS_INC(fibt, stat); break; \
97 case XFS_BTNUM_MAX: ASSERT(0); ; break; \
98 } \
99} while (0)
100
101#define __XFS_BTREE_STATS_ADD(type, stat, val) \
102 XFS_STATS_ADD(xs_ ## type ## _2_ ## stat, val)
103#define XFS_BTREE_STATS_ADD(cur, stat, val) \
104do { \
105 switch (cur->bc_btnum) { \
106 case XFS_BTNUM_BNO: __XFS_BTREE_STATS_ADD(abtb, stat, val); break; \
107 case XFS_BTNUM_CNT: __XFS_BTREE_STATS_ADD(abtc, stat, val); break; \
108 case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_ADD(bmbt, stat, val); break; \
109 case XFS_BTNUM_INO: __XFS_BTREE_STATS_ADD(ibt, stat, val); break; \
110 case XFS_BTNUM_FINO: __XFS_BTREE_STATS_ADD(fibt, stat, val); break; \
111 case XFS_BTNUM_MAX: ASSERT(0); ; break; \
112 } \
113} while (0)
114
115#define XFS_BTREE_MAXLEVELS 8
116
117struct xfs_btree_ops {
118
119 size_t key_len;
120 size_t rec_len;
121
122
123 struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
124 void (*update_cursor)(struct xfs_btree_cur *src,
125 struct xfs_btree_cur *dst);
126
127
128 void (*set_root)(struct xfs_btree_cur *cur,
129 union xfs_btree_ptr *nptr, int level_change);
130
131
132 int (*alloc_block)(struct xfs_btree_cur *cur,
133 union xfs_btree_ptr *start_bno,
134 union xfs_btree_ptr *new_bno,
135 int *stat);
136 int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
137
138
139 void (*update_lastrec)(struct xfs_btree_cur *cur,
140 struct xfs_btree_block *block,
141 union xfs_btree_rec *rec,
142 int ptr, int reason);
143
144
145 int (*get_minrecs)(struct xfs_btree_cur *cur, int level);
146 int (*get_maxrecs)(struct xfs_btree_cur *cur, int level);
147
148
149 int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
150
151
152 void (*init_key_from_rec)(union xfs_btree_key *key,
153 union xfs_btree_rec *rec);
154 void (*init_rec_from_key)(union xfs_btree_key *key,
155 union xfs_btree_rec *rec);
156 void (*init_rec_from_cur)(struct xfs_btree_cur *cur,
157 union xfs_btree_rec *rec);
158 void (*init_ptr_from_cur)(struct xfs_btree_cur *cur,
159 union xfs_btree_ptr *ptr);
160
161
162 __int64_t (*key_diff)(struct xfs_btree_cur *cur,
163 union xfs_btree_key *key);
164
165 const struct xfs_buf_ops *buf_ops;
166
167#if defined(DEBUG) || defined(XFS_WARN)
168
169 int (*keys_inorder)(struct xfs_btree_cur *cur,
170 union xfs_btree_key *k1,
171 union xfs_btree_key *k2);
172
173
174 int (*recs_inorder)(struct xfs_btree_cur *cur,
175 union xfs_btree_rec *r1,
176 union xfs_btree_rec *r2);
177#endif
178};
179
180
181
182
183#define LASTREC_UPDATE 0
184#define LASTREC_INSREC 1
185#define LASTREC_DELREC 2
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 {
199 xfs_alloc_rec_incore_t a;
200 xfs_bmbt_irec_t b;
201 xfs_inobt_rec_incore_t i;
202 } bc_rec;
203 struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS];
204 int bc_ptrs[XFS_BTREE_MAXLEVELS];
205 __uint8_t bc_ra[XFS_BTREE_MAXLEVELS];
206#define XFS_BTCUR_LEFTRA 1
207#define XFS_BTCUR_RIGHTRA 2
208 __uint8_t bc_nlevels;
209 __uint8_t bc_blocklog;
210 xfs_btnum_t bc_btnum;
211 union {
212 struct {
213 struct xfs_buf *agbp;
214 xfs_agnumber_t agno;
215 } a;
216 struct {
217 struct xfs_inode *ip;
218 struct xfs_bmap_free *flist;
219 xfs_fsblock_t firstblock;
220 int allocated;
221 short forksize;
222 char whichfork;
223 char flags;
224#define XFS_BTCUR_BPRV_WASDEL 1
225 } b;
226 } bc_private;
227} xfs_btree_cur_t;
228
229
230#define XFS_BTREE_LONG_PTRS (1<<0)
231#define XFS_BTREE_ROOT_IN_INODE (1<<1)
232#define XFS_BTREE_LASTREC_UPDATE (1<<2)
233#define XFS_BTREE_CRC_BLOCKS (1<<3)
234
235
236#define XFS_BTREE_NOERROR 0
237#define XFS_BTREE_ERROR 1
238
239
240
241
242#define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr))
243
244
245
246
247
248int
249xfs_btree_check_block(
250 struct xfs_btree_cur *cur,
251 struct xfs_btree_block *block,
252 int level,
253 struct xfs_buf *bp);
254
255
256
257
258int
259xfs_btree_check_lptr(
260 struct xfs_btree_cur *cur,
261 xfs_dfsbno_t ptr,
262 int level);
263
264
265
266
267void
268xfs_btree_del_cursor(
269 xfs_btree_cur_t *cur,
270 int error);
271
272
273
274
275
276int
277xfs_btree_dup_cursor(
278 xfs_btree_cur_t *cur,
279 xfs_btree_cur_t **ncur);
280
281
282
283
284
285struct xfs_buf *
286xfs_btree_get_bufl(
287 struct xfs_mount *mp,
288 struct xfs_trans *tp,
289 xfs_fsblock_t fsbno,
290 uint lock);
291
292
293
294
295
296struct xfs_buf *
297xfs_btree_get_bufs(
298 struct xfs_mount *mp,
299 struct xfs_trans *tp,
300 xfs_agnumber_t agno,
301 xfs_agblock_t agbno,
302 uint lock);
303
304
305
306
307int
308xfs_btree_islastblock(
309 xfs_btree_cur_t *cur,
310 int level);
311
312
313
314
315
316void
317xfs_btree_offsets(
318 __int64_t fields,
319 const short *offsets,
320 int nbits,
321 int *first,
322 int *last);
323
324
325
326
327
328int
329xfs_btree_read_bufl(
330 struct xfs_mount *mp,
331 struct xfs_trans *tp,
332 xfs_fsblock_t fsbno,
333 uint lock,
334 struct xfs_buf **bpp,
335 int refval,
336 const struct xfs_buf_ops *ops);
337
338
339
340
341
342void
343xfs_btree_reada_bufl(
344 struct xfs_mount *mp,
345 xfs_fsblock_t fsbno,
346 xfs_extlen_t count,
347 const struct xfs_buf_ops *ops);
348
349
350
351
352
353void
354xfs_btree_reada_bufs(
355 struct xfs_mount *mp,
356 xfs_agnumber_t agno,
357 xfs_agblock_t agbno,
358 xfs_extlen_t count,
359 const struct xfs_buf_ops *ops);
360
361
362
363
364void
365xfs_btree_init_block(
366 struct xfs_mount *mp,
367 struct xfs_buf *bp,
368 __u32 magic,
369 __u16 level,
370 __u16 numrecs,
371 __u64 owner,
372 unsigned int flags);
373
374void
375xfs_btree_init_block_int(
376 struct xfs_mount *mp,
377 struct xfs_btree_block *buf,
378 xfs_daddr_t blkno,
379 __u32 magic,
380 __u16 level,
381 __u16 numrecs,
382 __u64 owner,
383 unsigned int flags);
384
385
386
387
388int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
389int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
390int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
391int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
392int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
393int xfs_btree_insert(struct xfs_btree_cur *, int *);
394int xfs_btree_delete(struct xfs_btree_cur *, int *);
395int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
396int xfs_btree_change_owner(struct xfs_btree_cur *cur, __uint64_t new_owner,
397 struct list_head *buffer_list);
398
399
400
401
402void xfs_btree_lblock_calc_crc(struct xfs_buf *);
403bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
404void xfs_btree_sblock_calc_crc(struct xfs_buf *);
405bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
406
407
408
409
410void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
411void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
412
413
414
415
416static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
417{
418 return be16_to_cpu(block->bb_numrecs);
419}
420
421static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
422 __uint16_t numrecs)
423{
424 block->bb_numrecs = cpu_to_be16(numrecs);
425}
426
427static inline int xfs_btree_get_level(struct xfs_btree_block *block)
428{
429 return be16_to_cpu(block->bb_level);
430}
431
432
433
434
435
436#define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
437#define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
438#define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
439#define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
440#define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
441#define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
442#define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
443#define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
444
445#define XFS_FSB_SANITY_CHECK(mp,fsb) \
446 (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
447 XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
448
449
450
451
452
453
454
455
456
457
458
459#define XFS_BTREE_TRACE_ARGBI(c, b, i)
460#define XFS_BTREE_TRACE_ARGBII(c, b, i, j)
461#define XFS_BTREE_TRACE_ARGI(c, i)
462#define XFS_BTREE_TRACE_ARGIPK(c, i, p, s)
463#define XFS_BTREE_TRACE_ARGIPR(c, i, p, r)
464#define XFS_BTREE_TRACE_ARGIK(c, i, k)
465#define XFS_BTREE_TRACE_ARGR(c, r)
466#define XFS_BTREE_TRACE_CURSOR(c, t)
467
468#endif
469