]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/libxfs_priv.h
48a84a1089ee47dd763b6003fe545791203633d6
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 * This header is effectively a "namespace multiplexor" for the
9 * user level XFS code. It provides all of the necessary stuff
10 * such that we can build some parts of the XFS kernel code in
11 * user space in a controlled fashion, and translates the names
12 * used in the kernel into the names which libxfs is going to
13 * make available to user tools.
15 * It should only ever be #include'd by XFS "kernel" code being
16 * compiled in user space.
18 * Our goals here are to...
19 * o "share" large amounts of complex code between user and
21 * o shield the user tools from changes in the bleeding
22 * edge kernel code, merging source changes when
23 * convenient and not immediately (no symlinks);
24 * o i.e. be able to merge changes to the kernel source back
25 * into the affected user tools in a controlled fashion;
26 * o provide a _minimalist_ life-support system for kernel
27 * code in user land, not the "everything + the kitchen
28 * sink" model which libsim had mutated into;
29 * o allow the kernel code to be completely free of code
30 * specifically there to support the user level build.
34 * define a guard and something we can check to determine what include context
35 * we are running from.
37 #ifndef __LIBXFS_INTERNAL_XFS_H__
38 #define __LIBXFS_INTERNAL_XFS_H__
40 /* CONFIG_XFS_* must be defined to 1 to work with IS_ENABLED() */
41 #define CONFIG_XFS_RT 1
42 #define CONFIG_XFS_BTREE_IN_MEM 1
44 #include "libxfs_api_defs.h"
45 #include "platform_defs.h"
53 #include "libfrog/radix-tree.h"
54 #include "libfrog/bitmask.h"
55 #include "libfrog/div64.h"
56 #include "libfrog/util.h"
59 #include "linux-err.h"
61 #include "xfs_types.h"
65 #include "libfrog/crc32c.h"
67 #include <sys/xattr.h>
68 #ifdef HAVE_GETRANDOM_NONBLOCK
69 #include <sys/random.h>
72 /* Zones used in libxfs allocations that aren't in shared header files */
73 extern struct kmem_cache
*xfs_buf_item_cache
;
74 extern struct kmem_cache
*xfs_ili_cache
;
75 extern struct kmem_cache
*xfs_buf_cache
;
76 extern struct kmem_cache
*xfs_inode_cache
;
77 extern struct kmem_cache
*xfs_trans_cache
;
79 /* fake up iomap, (not) used in xfs_bmap.[ch] */
80 #define IOMAP_F_SHARED 0x04
81 #define xfs_bmbt_to_iomap(a, b, c, d, e, f) ((void) 0)
83 /* CRC stuff, buffer API dependent on it */
84 #define crc32c(c,p,l) crc32c_le((c),(unsigned char const *)(p),(l))
86 /* fake up kernel's iomap, (not) used in xfs_bmap.[ch] */
88 unsigned long long offset
; /* do not use */
89 unsigned long long length
; /* do not use */
92 #define cancel_delayed_work_sync(work) do { } while(0)
94 #include "xfs_cksum.h"
97 * This mirrors the kernel include for xfs_buf.h - it's implicitly included in
98 * every files via a similar include in the kernel xfs_linux.h.
100 #include "libxfs_io.h"
102 /* for all the support code that uses progname in error messages */
103 extern char *progname
;
106 #define ASSERT(ex) assert(ex)
109 * We have no need for the "linux" dev_t in userspace, so these
110 * are no-ops, and an xfs_dev_t is stored in VFS_I(ip)->i_rdev
112 #define xfs_to_linux_dev_t(dev) dev
113 #define linux_to_xfs_dev_t(dev) dev
116 #define EWRONGFS EINVAL
119 #define xfs_error_level 0
121 #define STATIC static
124 * Starting in Linux 4.15, the %p (raw pointer value) printk modifier
125 * prints a hashed version of the pointer to avoid leaking kernel
126 * pointers into dmesg. If we're trying to debug the kernel we want the
127 * raw values, so override this behavior as best we can.
129 * In userspace we don't have this problem.
133 #define XFS_IGET_CREATE 0x1
134 #define XFS_IGET_UNTRUSTED 0x2
136 extern void cmn_err(int, char *, ...);
137 enum ce
{ CE_DEBUG
, CE_CONT
, CE_NOTE
, CE_WARN
, CE_ALERT
, CE_PANIC
};
139 #define xfs_info(mp,fmt,args...) cmn_err(CE_CONT, _(fmt), ## args)
140 #define xfs_info_ratelimited(mp,fmt,args...) cmn_err(CE_CONT, _(fmt), ## args)
141 #define xfs_notice(mp,fmt,args...) cmn_err(CE_NOTE, _(fmt), ## args)
142 #define xfs_warn(mp,fmt,args...) cmn_err((mp) ? CE_WARN : CE_WARN, _(fmt), ## args)
143 #define xfs_err(mp,fmt,args...) cmn_err(CE_ALERT, _(fmt), ## args)
144 #define xfs_alert(mp,fmt,args...) cmn_err(CE_ALERT, _(fmt), ## args)
146 #define xfs_buf_ioerror_alert(bp,f) ((void) 0);
148 #define xfs_hex_dump(d,n) ((void) 0)
149 #define xfs_stack_trace() ((void) 0)
152 #define xfs_force_shutdown(d,n) ((void) 0)
153 #define xfs_mod_delalloc(a,b,c) ((void) 0)
154 #define xfs_mod_sb_delalloc(sb, d) ((void) 0)
156 /* stop unused var warnings by assigning mp to itself */
158 #define xfs_corruption_error(e,l,mp,b,sz,fi,ln,fa) do { \
160 cmn_err(CE_ALERT, "%s: XFS_CORRUPTION_ERROR", (e)); \
163 #define XFS_CORRUPTION_ERROR(e, lvl, mp, buf, bufsize) do { \
165 cmn_err(CE_ALERT, "%s: XFS_CORRUPTION_ERROR", (e)); \
168 #define XFS_ERROR_REPORT(e,l,mp) do { \
170 cmn_err(CE_ALERT, "%s: XFS_ERROR_REPORT", (e)); \
173 #define XFS_WARN_CORRUPT(mp, expr) \
174 ( xfs_is_reporting_corruption(mp) ? \
175 (printf("%s: XFS_WARN_CORRUPT at %s:%d", #expr, \
176 __func__, __LINE__), true) : true)
178 #define XFS_IS_CORRUPT(mp, expr) \
179 (unlikely(expr) ? XFS_WARN_CORRUPT((mp), (expr)) : false)
181 #define XFS_ERRLEVEL_LOW 1
182 #define XFS_ILOCK_EXCL 0
183 #define XFS_ILOCK_SHARED 0
184 #define XFS_IOLOCK_EXCL 0
185 #define XFS_STATS_INC(mp, count) do { (mp) = (mp); } while (0)
186 #define XFS_STATS_DEC(mp, count, x) do { (mp) = (mp); } while (0)
187 #define XFS_STATS_ADD(mp, count, x) do { (mp) = (mp); } while (0)
188 #define XFS_TEST_ERROR(expr,a,b) ( expr )
190 #define __section(section) __attribute__((__section__(section)))
192 #define xfs_printk_once(func, dev, fmt, ...) \
194 static bool __section(".data.once") __print_once; \
195 bool __ret_print_once = !__print_once; \
197 if (!__print_once) { \
198 __print_once = true; \
199 func(dev, fmt, ##__VA_ARGS__); \
201 unlikely(__ret_print_once); \
204 #define xfs_info_once(dev, fmt, ...) \
205 xfs_printk_once(xfs_info, dev, fmt, ##__VA_ARGS__)
207 /* Need to be able to handle this bare or in control flow */
208 static inline bool WARN_ON(bool expr
) {
212 #define WARN_ON_ONCE(e) WARN_ON(e)
214 #define percpu_counter_read_positive(x) ((*x) > 0 ? (*x) : 0)
215 #define percpu_counter_sum_positive(x) ((*x) > 0 ? (*x) : 0)
217 #ifdef HAVE_GETRANDOM_NONBLOCK
218 uint32_t get_random_u32(void);
220 #define get_random_u32() (0)
223 #define PAGE_SIZE getpagesize()
224 extern unsigned int PAGE_SHIFT
;
226 #define inode_peek_iversion(inode) (inode)->i_version
227 #define inode_set_iversion_queried(inode, version) do { \
228 (inode)->i_version = (version); \
234 void inode_init_owner(struct mnt_idmap
*idmap
, struct inode
*inode
,
235 const struct inode
*dir
, umode_t mode
);
237 #define min_t(type,x,y) \
238 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
239 #define max_t(type,x,y) \
240 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
243 * swap - swap values of @a and @b
248 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
250 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
251 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
252 #define round_down(x, y) ((x) & ~__round_mask(x, y))
253 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
256 * Handling for kernel bitmap types.
258 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBBY * sizeof(long))
259 #define DECLARE_BITMAP(name,bits) \
260 unsigned long name[BITS_TO_LONGS(bits)]
261 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
264 * This is a common helper function for find_next_bit and
265 * find_next_zero_bit. The difference is the "invert" argument, which
266 * is XORed with each fetched word before searching it for one bits.
268 static inline unsigned long
269 _find_next_bit(const unsigned long *addr
, unsigned long nbits
,
270 unsigned long start
, unsigned long invert
)
274 if (!nbits
|| start
>= nbits
)
277 tmp
= addr
[start
/ BITS_PER_LONG
] ^ invert
;
279 /* Handle 1st word. */
280 tmp
&= BITMAP_FIRST_WORD_MASK(start
);
281 start
= round_down(start
, BITS_PER_LONG
);
284 start
+= BITS_PER_LONG
;
288 tmp
= addr
[start
/ BITS_PER_LONG
] ^ invert
;
291 return min(start
+ ffs(tmp
), nbits
);
295 * Find the next set bit in a memory region.
297 static inline unsigned long
298 find_next_bit(const unsigned long *addr
, unsigned long size
,
299 unsigned long offset
)
301 return _find_next_bit(addr
, size
, offset
, 0UL);
303 static inline unsigned long
304 find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
305 unsigned long offset
)
307 return _find_next_bit(addr
, size
, offset
, ~0UL);
309 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
312 * xfs_iroundup: round up argument to next power of two
315 roundup_pow_of_two(uint v
)
320 if ((v
& (v
- 1)) == 0)
322 ASSERT((v
& 0x80000000) == 0);
323 if ((v
& (v
+ 1)) == 0)
325 for (i
= 0, m
= 1; i
< 31; i
++, m
<<= 1) {
329 if ((v
& (v
+ 1)) == 0)
336 /* If @b is a power of 2, return log2(b). Else return -1. */
337 static inline int8_t log2_if_power2(unsigned long b
)
339 unsigned long mask
= 1;
341 unsigned int ret
= 1;
343 if (!is_power_of_2(b
))
346 for (i
= 0; i
< NBBY
* sizeof(unsigned long); i
++, mask
<<= 1) {
354 /* If @b is a power of 2, return a mask of the lower bits, else return zero. */
355 static inline unsigned long long mask64_if_power2(unsigned long b
)
357 return is_power_of_2(b
) ? b
- 1 : 0;
360 /* buffer management */
361 #define XBF_TRYLOCK 0
363 #define xfs_buf_stale(bp) ((bp)->b_flags |= LIBXFS_B_STALE)
364 #define XFS_BUF_UNDELAYWRITE(bp) ((bp)->b_flags &= ~LIBXFS_B_DIRTY)
366 /* buffer type flags for write callbacks */
367 #define _XBF_INODES 0 /* inode buffer */
368 #define _XBF_DQUOTS 0 /* dquot buffer */
369 #define _XBF_LOGRECOVERY 0 /* log recovery buffer */
373 struct xfs_buftarg
*target
,
376 xfs_buf_flags_t flags
,
377 struct xfs_buf
**bpp
)
383 #define xfs_buf_oneshot(bp) ((void) 0)
385 #define xfs_buf_zero(bp, off, len) \
386 memset((bp)->b_addr + off, 0, len);
388 void __xfs_buf_mark_corrupt(struct xfs_buf
*bp
, xfs_failaddr_t fa
);
389 #define xfs_buf_mark_corrupt(bp) __xfs_buf_mark_corrupt((bp), __this_address)
392 #define xfs_trans_set_sync(tp) ((void) 0)
393 #define xfs_trans_buf_set_type(tp, bp, t) ({ \
395 __t = __t; /* no set-but-unused warning */ \
396 tp = tp; /* no set-but-unused warning */ \
399 #define xfs_trans_buf_copy_type(dbp, sbp)
403 struct xfs_buftarg
*target
,
406 const struct xfs_buf_ops
*ops
)
409 #define xfs_buf_readahead_map(a,b,c,ops) ((void) 0) /* no readahead */
411 #define xfs_sort qsort
413 #define xfs_ilock(ip,mode) ((void) 0)
414 #define xfs_ilock_data_map_shared(ip) (0)
415 #define xfs_ilock_attr_map_shared(ip) (0)
416 #define xfs_iunlock(ip,mode) ({ \
417 typeof(mode) __mode = mode; \
418 __mode = __mode; /* no set-but-unused warning */ \
420 #define xfs_lock_two_inodes(ip0,mode0,ip1,mode1) ((void) 0)
421 #define xfs_assert_ilocked(ip, flags) ((void) 0)
422 #define xfs_lock_inodes(i_tab, nr, mode) ((void) 0)
423 #define xfs_sort_inodes(i_tab, nr) ((void) 0)
425 /* space allocation */
426 #define XFS_EXTENT_BUSY_DISCARDED 0x01 /* undergoing a discard op. */
427 #define XFS_EXTENT_BUSY_SKIP_DISCARD 0x02 /* do not discard */
429 #define xfs_extent_busy_reuse(...) ((void) 0)
430 /* avoid unused variable warning */
431 #define xfs_extent_busy_insert(tp,xg,bno,len,flags)({ \
432 struct xfs_group *__foo = xg; \
433 __foo = __foo; /* no set-but-unused warning */ \
435 #define xfs_extent_busy_trim(group,minlen,maxlen,bno,len,busy_gen) ({ \
436 unsigned __foo = *(busy_gen); \
437 *(busy_gen) = __foo; \
440 #define xfs_extent_busy_flush(tp,pag,busy_gen,alloc_flags) ((int)(0))
442 #define xfs_rotorstep 1
443 #define xfs_bmap_rtalloc(a) (-ENOSYS)
444 #define xfs_inode_is_filestream(ip) (0)
445 #define xfs_filestream_lookup_ag(ip) (0)
446 #define xfs_filestream_new_ag(ip,ag) (0)
447 #define xfs_filestream_select_ag(...) (-ENOSYS)
449 #define xfs_trans_inode_buf(tp, bp) ((void) 0)
452 #define xfs_trans_mod_dquot_byino(t,i,f,d) ({ \
454 _f = _f; /* shut up gcc */ \
456 #define xfs_trans_reserve_quota_nblks(t,i,b,n,f) (0)
458 /* hack too silence gcc */
459 static inline int retzero(void) { return 0; }
460 #define xfs_trans_unreserve_quota_nblks(t,i,b,n,f) retzero()
461 #define xfs_quota_unreserve_blkres(i,b) retzero()
463 #define xfs_quota_reserve_blkres(i,b) (0)
464 #define xfs_qm_dqattach(i) (0)
466 #define uuid_copy(s,d) platform_uuid_copy((s),(d))
467 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
469 #define xfs_icreate_log(tp, agno, agbno, cnt, isize, len, gen) ((void) 0)
470 #define xfs_sb_validate_fsb_count(sbp, nblks) (0)
471 #define xlog_calc_iovec_len(len) roundup(len, sizeof(uint32_t))
473 #define xfs_zoned_add_available(mp, rtxnum) do { } while (0)
476 * Prototypes for kernel static functions that are aren't in their
477 * associated header files.
480 struct xfs_bmap_free
;
481 struct xfs_bmap_free_item
;
489 struct xfs_buf_log_item
;
493 int xfs_attr_rmtval_get(struct xfs_da_args
*);
496 void xfs_bmap_del_free(struct xfs_bmap_free
*, struct xfs_bmap_free_item
*);
499 void xfs_mount_common(struct xfs_mount
*, struct xfs_sb
*);
502 * logitem.c and trans.c prototypes
504 void xfs_trans_init(struct xfs_mount
*);
505 int xfs_trans_roll(struct xfs_trans
**);
507 /* xfs_trans_item.c */
508 void xfs_trans_add_item(struct xfs_trans
*, struct xfs_log_item
*);
509 void xfs_trans_del_item(struct xfs_log_item
*);
511 /* xfs_inode_item.c */
512 void xfs_inode_item_init(struct xfs_inode
*, struct xfs_mount
*);
515 void xfs_buf_item_init(struct xfs_buf
*, struct xfs_mount
*);
516 void xfs_buf_item_log(struct xfs_buf_log_item
*, uint
, uint
);
518 /* xfs_trans_buf.c */
519 struct xfs_buf
*xfs_trans_buf_item_match(struct xfs_trans
*,
520 struct xfs_buftarg
*, struct xfs_buf_map
*, int);
522 /* local source files */
523 #define xfs_add_fdblocks(mp, delta) \
524 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FDBLOCKS, delta, false)
525 #define xfs_dec_fdblocks(mp, delta, rsvd) \
526 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FDBLOCKS, -(int64_t)(delta), rsvd)
527 #define xfs_add_frextents(mp, delta) \
528 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FREXTENTS, delta, 0)
529 #define xfs_dec_frextents(mp, delta) \
530 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FREXTENTS, -(int64_t)(delta), 0)
531 int libxfs_mod_incore_sb(struct xfs_mount
*, int, int64_t, int);
532 /* percpu counters in mp are #defined to the superblock sb_ counters */
533 #define xfs_reinit_percpu_counters(mp)
535 void xfs_trans_mod_sb(struct xfs_trans
*, uint
, long);
537 void xfs_verifier_error(struct xfs_buf
*bp
, int error
,
538 xfs_failaddr_t failaddr
);
539 void xfs_inode_verifier_error(struct xfs_inode
*ip
, int error
,
540 const char *name
, void *buf
, size_t bufsz
,
541 xfs_failaddr_t failaddr
);
543 #define xfs_buf_verifier_error(bp,e,n,bu,bus,fa) \
544 xfs_verifier_error(bp, e, fa)
546 xfs_buf_corruption_error(struct xfs_buf
*bp
, xfs_failaddr_t fa
);
548 int libxfs_zero_extent(struct xfs_inode
*ip
, xfs_fsblock_t start_fsb
,
549 xfs_off_t count_fsb
);
553 bool xfs_log_check_lsn(struct xfs_mount
*, xfs_lsn_t
);
554 void xfs_log_item_init(struct xfs_mount
*mp
, struct xfs_log_item
*lip
, int type
,
555 const struct xfs_item_ops
*ops
);
556 #define xfs_attr_use_log_assist(mp) (0)
557 #define xlog_drop_incompat_feat(log) do { } while (0)
558 #define xfs_log_in_recovery(mp) (false)
561 #define xfs_inode_clear_cowblocks_tag(ip) do { } while (0)
562 #define xfs_inode_set_cowblocks_tag(ip) do { } while (0)
563 #define xfs_inode_set_eofblocks_tag(ip) do { } while (0)
566 #define XFS_STATS_CALC_INDEX(member) 0
567 #define XFS_STATS_INC_OFF(mp, off)
568 #define XFS_STATS_ADD_OFF(mp, off, val)
570 typedef unsigned char u8
;
571 unsigned int hweight8(unsigned int w
);
572 unsigned int hweight32(unsigned int w
);
573 unsigned int hweight64(__u64 w
);
575 #define xfs_buf_cache_init(bch) (0)
576 #define xfs_buf_cache_destroy(bch) ((void)0)
578 static inline int xfs_iunlink_init(struct xfs_perag
*pag
) { return 0; }
579 static inline void xfs_iunlink_destroy(struct xfs_perag
*pag
) { }
581 xfs_agnumber_t
xfs_set_inode_alloc(struct xfs_mount
*mp
,
582 xfs_agnumber_t agcount
);
584 /* Keep static checkers quiet about nonstatic functions by exporting */
585 /* xfs_bmap_util.h */
587 int xfs_bmap_extsize_align(struct xfs_mount
*mp
, struct xfs_bmbt_irec
*gotp
,
588 struct xfs_bmbt_irec
*prevp
, xfs_extlen_t extsz
,
589 int rt
, int eof
, int delay
, int convert
,
590 xfs_fileoff_t
*offp
, xfs_extlen_t
*lenp
);
591 bool xfs_bmap_adjacent(struct xfs_bmalloca
*ap
);
592 int xfs_bmap_last_extent(struct xfs_trans
*tp
, struct xfs_inode
*ip
,
593 int whichfork
, struct xfs_bmbt_irec
*rec
,
597 #define xfs_iflags_set(ip, flags) do { } while (0)
598 #define xfs_finish_inode_setup(ip) ((void) 0)
600 /* linux/wordpart.h */
603 * upper_32_bits - return bits 32-63 of a number
604 * @n: the number we're accessing
606 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
607 * the "right shift count >= width of type" warning when that quantity is
610 #define upper_32_bits(n) ((uint32_t)(((n) >> 16) >> 16))
613 * lower_32_bits - return bits 0-31 of a number
614 * @n: the number we're accessing
616 #define lower_32_bits(n) ((uint32_t)((n) & 0xffffffff))
618 #define cond_resched() ((void)0)
621 #define irix_sgid_inherit (false)
622 #define vfsgid_in_group_p(...) (false)
624 #endif /* __LIBXFS_INTERNAL_XFS_H__ */