]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/libxfs_priv.h
xfs: don't rely on extent indices in xfs_bmap_collapse_extents
[thirdparty/xfsprogs-dev.git] / libxfs / libxfs_priv.h
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * This header is effectively a "namespace multiplexor" for the
21 * user level XFS code. It provides all of the necessary stuff
22 * such that we can build some parts of the XFS kernel code in
23 * user space in a controlled fashion, and translates the names
24 * used in the kernel into the names which libxfs is going to
25 * make available to user tools.
26 *
27 * It should only ever be #include'd by XFS "kernel" code being
28 * compiled in user space.
29 *
30 * Our goals here are to...
31 * o "share" large amounts of complex code between user and
32 * kernel space;
33 * o shield the user tools from changes in the bleeding
34 * edge kernel code, merging source changes when
35 * convenient and not immediately (no symlinks);
36 * o i.e. be able to merge changes to the kernel source back
37 * into the affected user tools in a controlled fashion;
38 * o provide a _minimalist_ life-support system for kernel
39 * code in user land, not the "everything + the kitchen
40 * sink" model which libsim had mutated into;
41 * o allow the kernel code to be completely free of code
42 * specifically there to support the user level build.
43 */
44
45 /*
46 * define a guard and something we can check to determine what include context
47 * we are running from.
48 */
49 #ifndef __LIBXFS_INTERNAL_XFS_H__
50 #define __LIBXFS_INTERNAL_XFS_H__
51
52 #include "libxfs_api_defs.h"
53 #include "platform_defs.h"
54 #include "xfs.h"
55
56 #include "list.h"
57 #include "hlist.h"
58 #include "cache.h"
59 #include "bitops.h"
60 #include "kmem.h"
61 #include "radix-tree.h"
62 #include "atomic.h"
63
64 #include "xfs_types.h"
65 #include "xfs_arch.h"
66
67 #include "xfs_fs.h"
68
69 /* CRC stuff, buffer API dependent on it */
70 extern uint32_t crc32_le(uint32_t crc, unsigned char const *p, size_t len);
71 extern uint32_t crc32c_le(uint32_t crc, unsigned char const *p, size_t len);
72
73 #define crc32(c,p,l) crc32_le((c),(unsigned char const *)(p),(l))
74 #define crc32c(c,p,l) crc32c_le((c),(unsigned char const *)(p),(l))
75
76 #include "xfs_cksum.h"
77
78 /*
79 * This mirrors the kernel include for xfs_buf.h - it's implicitly included in
80 * every files via a similar include in the kernel xfs_linux.h.
81 */
82 #include "libxfs_io.h"
83
84 /* for all the support code that uses progname in error messages */
85 extern char *progname;
86
87 #undef ASSERT
88 #define ASSERT(ex) assert(ex)
89
90 #define MKDEV(major, minor) IRIX_MKDEV(major, minor)
91 #define sysv_major(dev) IRIX_DEV_MAJOR(dev)
92 #define sysv_minor(dev) IRIX_DEV_MINOR(dev)
93
94 static inline uint32_t sysv_encode_dev(dev_t dev)
95 {
96 return IRIX_DEV_MINOR(dev) | (IRIX_DEV_MAJOR(dev) << 18);
97 }
98
99 #ifndef EWRONGFS
100 #define EWRONGFS EINVAL
101 #endif
102
103 #define xfs_error_level 0
104
105 #define STATIC static
106
107 /* XXX: need to push these out to make LIBXFS_ATTR defines */
108 #define ATTR_ROOT 0x0002
109 #define ATTR_SECURE 0x0008
110 #define ATTR_CREATE 0x0010
111 #define ATTR_REPLACE 0x0020
112 #define ATTR_KERNOTIME 0
113 #define ATTR_KERNOVAL 0
114
115 #define XFS_IGET_CREATE 0x1
116 #define XFS_IGET_UNTRUSTED 0x2
117
118 extern void cmn_err(int, char *, ...);
119 enum ce { CE_DEBUG, CE_CONT, CE_NOTE, CE_WARN, CE_ALERT, CE_PANIC };
120
121 #define xfs_notice(mp,fmt,args...) cmn_err(CE_NOTE,fmt, ## args)
122 #define xfs_warn(mp,fmt,args...) cmn_err(CE_WARN,fmt, ## args)
123 #define xfs_hex_dump(d,n) ((void) 0)
124
125 #define xfs_force_shutdown(d,n) ((void) 0)
126
127 /* stop unused var warnings by assigning mp to itself */
128 #define XFS_CORRUPTION_ERROR(e,l,mp,m) do { \
129 (mp) = (mp); \
130 cmn_err(CE_ALERT, "%s: XFS_CORRUPTION_ERROR", (e)); \
131 } while (0)
132
133 #define XFS_ERROR_REPORT(e,l,mp) do { \
134 (mp) = (mp); \
135 cmn_err(CE_ALERT, "%s: XFS_ERROR_REPORT", (e)); \
136 } while (0)
137
138 #define XFS_ERRLEVEL_LOW 1
139 #define XFS_FORCED_SHUTDOWN(mp) 0
140 #define XFS_ILOCK_EXCL 0
141 #define XFS_STATS_INC(mp, count) do { (mp) = (mp); } while (0)
142 #define XFS_STATS_DEC(mp, count, x) do { (mp) = (mp); } while (0)
143 #define XFS_STATS_ADD(mp, count, x) do { (mp) = (mp); } while (0)
144 #define XFS_TEST_ERROR(expr,a,b) ( expr )
145 #define XFS_WANT_CORRUPTED_GOTO(mp, expr, l) \
146 { \
147 if (!(expr)) { \
148 if ((mp)->m_flags & LIBXFS_MOUNT_WANT_CORRUPTED) \
149 printf("WANT_CORRUPTED_GOTO at %s:%d\n", \
150 __func__, __LINE__); \
151 error = -EFSCORRUPTED; \
152 goto l; \
153 } \
154 }
155 #define XFS_WANT_CORRUPTED_RETURN(mp, expr) \
156 { \
157 if (!(expr)) { \
158 if ((mp)->m_flags & LIBXFS_MOUNT_WANT_CORRUPTED) \
159 printf("WANT_CORRUPTED_RETURN at %s:%d\n", \
160 __func__, __LINE__); \
161 return -EFSCORRUPTED; \
162 } \
163 }
164
165 #ifdef __GNUC__
166 #define __return_address __builtin_return_address(0)
167
168 /*
169 * Return the address of a label. Use barrier() so that the optimizer
170 * won't reorder code to refactor the error jumpouts into a single
171 * return, which throws off the reported address.
172 */
173 #define __this_address ({ __label__ __here; __here: barrier(); &&__here; })
174 /* Optimization barrier */
175
176 /* The "volatile" is due to gcc bugs */
177 #define barrier() __asm__ __volatile__("": : :"memory")
178 #endif
179
180 /* Optimization barrier */
181 #ifndef barrier
182 # define barrier() __memory_barrier()
183 #endif
184
185 #define XFS_DQUOT_CLUSTER_SIZE_FSB (xfs_filblks_t)1
186
187 /* miscellaneous kernel routines not in user space */
188 #define down_read(a) ((void) 0)
189 #define up_read(a) ((void) 0)
190 #define spin_lock_init(a) ((void) 0)
191 #define spin_lock(a) ((void) 0)
192 #define spin_unlock(a) ((void) 0)
193 #define likely(x) (x)
194 #define unlikely(x) (x)
195 #define rcu_read_lock() ((void) 0)
196 #define rcu_read_unlock() ((void) 0)
197 /* Need to be able to handle this bare or in control flow */
198 static inline bool WARN_ON_ONCE(bool expr) {
199 return (expr);
200 }
201
202 #define percpu_counter_read(x) (*x)
203 #define percpu_counter_read_positive(x) ((*x) > 0 ? (*x) : 0)
204 #define percpu_counter_sum(x) (*x)
205
206 /*
207 * prandom_u32 is used for di_gen inode allocation, it must be zero for libxfs
208 * or all sorts of badness can occur!
209 */
210 #define prandom_u32() 0
211
212 #define PAGE_SIZE getpagesize()
213
214 static inline int __do_div(unsigned long long *n, unsigned base)
215 {
216 int __res;
217 __res = (int)(((unsigned long) *n) % (unsigned) base);
218 *n = ((unsigned long) *n) / (unsigned) base;
219 return __res;
220 }
221
222 #define do_div(n,base) (__do_div((unsigned long long *)&(n), (base)))
223 #define do_mod(a, b) ((a) % (b))
224 #define rol32(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
225
226 #define min_t(type,x,y) \
227 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
228 #define max_t(type,x,y) \
229 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
230
231
232 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
233 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
234 #define round_down(x, y) ((x) & ~__round_mask(x, y))
235 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
236
237 /*
238 * Handling for kernel bitmap types.
239 */
240 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBBY * sizeof(long))
241 #define DECLARE_BITMAP(name,bits) \
242 unsigned long name[BITS_TO_LONGS(bits)]
243 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
244
245 /*
246 * This is a common helper function for find_next_bit and
247 * find_next_zero_bit. The difference is the "invert" argument, which
248 * is XORed with each fetched word before searching it for one bits.
249 */
250 static inline unsigned long
251 _find_next_bit(const unsigned long *addr, unsigned long nbits,
252 unsigned long start, unsigned long invert)
253 {
254 unsigned long tmp;
255
256 if (!nbits || start >= nbits)
257 return nbits;
258
259 tmp = addr[start / BITS_PER_LONG] ^ invert;
260
261 /* Handle 1st word. */
262 tmp &= BITMAP_FIRST_WORD_MASK(start);
263 start = round_down(start, BITS_PER_LONG);
264
265 while (!tmp) {
266 start += BITS_PER_LONG;
267 if (start >= nbits)
268 return nbits;
269
270 tmp = addr[start / BITS_PER_LONG] ^ invert;
271 }
272
273 return min(start + ffs(tmp), nbits);
274 }
275
276 /*
277 * Find the next set bit in a memory region.
278 */
279 static inline unsigned long
280 find_next_bit(const unsigned long *addr, unsigned long size,
281 unsigned long offset)
282 {
283 return _find_next_bit(addr, size, offset, 0UL);
284 }
285 static inline unsigned long
286 find_next_zero_bit(const unsigned long *addr, unsigned long size,
287 unsigned long offset)
288 {
289 return _find_next_bit(addr, size, offset, ~0UL);
290 }
291 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
292
293 static inline __attribute__((const))
294 int is_power_of_2(unsigned long n)
295 {
296 return (n != 0 && ((n & (n - 1)) == 0));
297 }
298
299 /*
300 * xfs_iroundup: round up argument to next power of two
301 */
302 static inline uint
303 roundup_pow_of_two(uint v)
304 {
305 int i;
306 uint m;
307
308 if ((v & (v - 1)) == 0)
309 return v;
310 ASSERT((v & 0x80000000) == 0);
311 if ((v & (v + 1)) == 0)
312 return v + 1;
313 for (i = 0, m = 1; i < 31; i++, m <<= 1) {
314 if (v & m)
315 continue;
316 v |= m;
317 if ((v & (v + 1)) == 0)
318 return v + 1;
319 }
320 ASSERT(0);
321 return 0;
322 }
323
324 static inline uint64_t
325 roundup_64(uint64_t x, uint32_t y)
326 {
327 x += y - 1;
328 do_div(x, y);
329 return x * y;
330 }
331
332 /* buffer management */
333 #define XBF_TRYLOCK 0
334 #define XBF_UNMAPPED 0
335 #define XBF_DONE 0
336 #define xfs_buf_stale(bp) ((bp)->b_flags |= LIBXFS_B_STALE)
337 #define XFS_BUF_UNDELAYWRITE(bp) ((bp)->b_flags &= ~LIBXFS_B_DIRTY)
338 #define XFS_BUF_SET_BDSTRAT_FUNC(a,b) ((void) 0)
339
340 /* avoid gcc warning */
341 #define xfs_incore(bt,blkno,len,lockit) ({ \
342 typeof(blkno) __foo = (blkno); \
343 typeof(len) __bar = (len); \
344 (blkno) = __foo; \
345 (len) = __bar; /* no set-but-unused warning */ \
346 NULL; \
347 })
348 #define xfs_buf_relse(bp) libxfs_putbuf(bp)
349 #define xfs_buf_get(devp,blkno,len,f) (libxfs_getbuf((devp), (blkno), (len)))
350 #define xfs_bwrite(bp) libxfs_writebuf((bp), 0)
351 #define xfs_buf_delwri_queue(bp, bl) libxfs_writebuf((bp), 0)
352
353 #define XBRW_READ LIBXFS_BREAD
354 #define XBRW_WRITE LIBXFS_BWRITE
355 #define xfs_buf_iomove(bp,off,len,data,f) libxfs_iomove(bp,off,len,data,f)
356 #define xfs_buf_zero(bp,off,len) libxfs_iomove(bp,off,len,0,LIBXFS_BZERO)
357
358 /* mount stuff */
359 #define XFS_MOUNT_32BITINODES LIBXFS_MOUNT_32BITINODES
360 #define XFS_MOUNT_ATTR2 LIBXFS_MOUNT_ATTR2
361 #define XFS_MOUNT_SMALL_INUMS 0 /* ignored in userspace */
362 #define XFS_MOUNT_WSYNC 0 /* ignored in userspace */
363 #define XFS_MOUNT_NOALIGN 0 /* ignored in userspace */
364 #define XFS_MOUNT_IKEEP 0 /* ignored in userspace */
365 #define XFS_MOUNT_SWALLOC 0 /* ignored in userspace */
366 #define XFS_MOUNT_RDONLY 0 /* ignored in userspace */
367
368
369 #define xfs_trans_set_sync(tp) ((void) 0)
370 #define xfs_trans_agblocks_delta(tp, d)
371 #define xfs_trans_agflist_delta(tp, d)
372 #define xfs_trans_agbtree_delta(tp, d)
373 #define xfs_trans_buf_set_type(tp, bp, t) ({ \
374 int __t = (t); \
375 __t = __t; /* no set-but-unused warning */ \
376 tp = tp; /* no set-but-unused warning */ \
377 })
378
379 #define xfs_trans_buf_copy_type(dbp, sbp)
380
381 /* no readahead, need to avoid set-but-unused var warnings. */
382 #define xfs_buf_readahead(a,d,c,ops) ({ \
383 xfs_daddr_t __d = d; \
384 __d = __d; /* no set-but-unused warning */ \
385 })
386 #define xfs_buf_readahead_map(a,b,c,ops) ((void) 0) /* no readahead */
387
388 #define xfs_warn(mp,fmt,args...) cmn_err(CE_WARN,fmt, ## args)
389 #define xfs_alert(mp,fmt,args...) cmn_err(CE_ALERT,fmt, ## args)
390 #define xfs_alert_tag(mp,tag,fmt,args...) cmn_err(CE_ALERT,fmt, ## args)
391
392 #define xfs_sort qsort
393
394 #define xfs_ilock(ip,mode) ((void) 0)
395 #define xfs_ilock_nowait(ip,mode) ((void) 0)
396 #define xfs_ilock_demote(ip,mode) ((void) 0)
397 #define xfs_ilock_data_map_shared(ip) (0)
398 #define xfs_ilock_attr_map_shared(ip) (0)
399 #define xfs_iunlock(ip,mode) ({ \
400 typeof(mode) __mode = mode; \
401 __mode = __mode; /* no set-but-unused warning */ \
402 })
403
404 /* space allocation */
405 #define xfs_extent_busy_reuse(mp,ag,bno,len,user) ((void) 0)
406 /* avoid unused variable warning */
407 #define xfs_extent_busy_insert(tp,ag,bno,len,flags)({ \
408 xfs_agnumber_t __foo = ag; \
409 __foo = __foo; /* no set-but-unused warning */ \
410 })
411 #define xfs_extent_busy_trim(args,bno,len,busy_gen) ({ \
412 unsigned __foo = *(busy_gen); \
413 *(busy_gen) = __foo; \
414 false; \
415 })
416 #define xfs_extent_busy_flush(mp,pag,busy_gen) ((void)(0))
417
418 #define xfs_rotorstep 1
419 #define xfs_bmap_rtalloc(a) (-ENOSYS)
420 #define xfs_get_extsz_hint(ip) (0)
421 #define xfs_get_cowextsz_hint(ip) (0)
422 #define xfs_inode_is_filestream(ip) (0)
423 #define xfs_filestream_lookup_ag(ip) (0)
424 #define xfs_filestream_new_ag(ip,ag) (0)
425
426 #define xfs_log_force(mp,flags) ((void) 0)
427 #define XFS_LOG_SYNC 1
428
429 /* quota bits */
430 #define xfs_trans_mod_dquot_byino(t,i,f,d) ((void) 0)
431 #define xfs_trans_reserve_quota_nblks(t,i,b,n,f) (0)
432 #define xfs_trans_unreserve_quota_nblks(t,i,b,n,f) ((void) 0)
433 #define xfs_qm_dqattach(i,f) (0)
434
435 #define uuid_copy(s,d) platform_uuid_copy((s),(d))
436 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
437
438 #define xfs_icreate_log(tp, agno, agbno, cnt, isize, len, gen) ((void) 0)
439 #define xfs_sb_validate_fsb_count(sbp, nblks) (0)
440
441 /*
442 * Prototypes for kernel static functions that are aren't in their
443 * associated header files.
444 */
445 struct xfs_da_args;
446 struct xfs_bmap_free;
447 struct xfs_bmap_free_item;
448 struct xfs_mount;
449 struct xfs_sb;
450 struct xfs_trans;
451 struct xfs_inode;
452 struct xfs_log_item;
453 struct xfs_buf;
454 struct xfs_buf_map;
455 struct xfs_buf_log_item;
456 struct xfs_buftarg;
457
458 /* xfs_attr.c */
459 int xfs_attr_rmtval_get(struct xfs_da_args *);
460
461 /* xfs_bmap.c */
462 void xfs_bmap_del_free(struct xfs_bmap_free *, struct xfs_bmap_free_item *);
463
464 /* xfs_mount.c */
465 int xfs_initialize_perag_data(struct xfs_mount *, xfs_agnumber_t);
466 void xfs_mount_common(struct xfs_mount *, struct xfs_sb *);
467
468 /*
469 * logitem.c and trans.c prototypes
470 */
471 void xfs_trans_init(struct xfs_mount *);
472 int xfs_trans_roll(struct xfs_trans **);
473
474 /* xfs_trans_item.c */
475 void xfs_trans_add_item(struct xfs_trans *, struct xfs_log_item *);
476 void xfs_trans_del_item(struct xfs_log_item *);
477
478 /* xfs_inode_item.c */
479 void xfs_inode_item_init(struct xfs_inode *, struct xfs_mount *);
480
481 /* xfs_buf_item.c */
482 void xfs_buf_item_init(struct xfs_buf *, struct xfs_mount *);
483 void xfs_buf_item_log(struct xfs_buf_log_item *, uint, uint);
484
485 /* xfs_trans_buf.c */
486 struct xfs_buf *xfs_trans_buf_item_match(struct xfs_trans *,
487 struct xfs_buftarg *, struct xfs_buf_map *, int);
488
489 /* local source files */
490 #define xfs_mod_fdblocks(mp, delta, rsvd) \
491 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FDBLOCKS, delta, rsvd)
492 #define xfs_mod_frextents(mp, delta) \
493 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FREXTENTS, delta, 0)
494 int libxfs_mod_incore_sb(struct xfs_mount *, int, int64_t, int);
495 /* percpu counters in mp are #defined to the superblock sb_ counters */
496 #define xfs_reinit_percpu_counters(mp)
497
498 void xfs_trans_mod_sb(struct xfs_trans *, uint, long);
499 void xfs_verifier_error(struct xfs_buf *bp);
500
501 /* XXX: this is clearly a bug - a shared header needs to export this */
502 /* xfs_rtalloc.c */
503 int libxfs_rtfree_extent(struct xfs_trans *, xfs_rtblock_t, xfs_extlen_t);
504
505 struct xfs_rtalloc_rec {
506 xfs_rtblock_t ar_startblock;
507 xfs_rtblock_t ar_blockcount;
508 };
509
510 typedef int (*xfs_rtalloc_query_range_fn)(
511 struct xfs_trans *tp,
512 struct xfs_rtalloc_rec *rec,
513 void *priv);
514
515 int libxfs_zero_extent(struct xfs_inode *ip, xfs_fsblock_t start_fsb,
516 xfs_off_t count_fsb);
517
518 bool xfs_log_check_lsn(struct xfs_mount *, xfs_lsn_t);
519
520 /* xfs_icache.c */
521 #define xfs_inode_set_cowblocks_tag(ip) do { } while (0)
522 #define xfs_inode_set_eofblocks_tag(ip) do { } while (0)
523
524 /* xfs_stats.h */
525 #define XFS_STATS_CALC_INDEX(member) 0
526 #define XFS_STATS_INC_OFF(mp, off)
527 #define XFS_STATS_ADD_OFF(mp, off, val)
528
529 typedef unsigned char u8;
530
531 #endif /* __LIBXFS_INTERNAL_XFS_H__ */