2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
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.
27 * It should only ever be #include'd by XFS "kernel" code being
28 * compiled in user space.
30 * Our goals here are to...
31 * o "share" large amounts of complex code between user and
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.
46 * define a guard and something we can check to determine what include context
47 * we are running from.
49 #ifndef __LIBXFS_INTERNAL_XFS_H__
50 #define __LIBXFS_INTERNAL_XFS_H__
52 #include "libxfs_api_defs.h"
53 #include "platform_defs.h"
61 #include "radix-tree.h"
64 #include "xfs_types.h"
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
);
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))
76 #include "xfs_cksum.h"
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.
82 #include "libxfs_io.h"
84 /* for all the support code that uses progname in error messages */
85 extern char *progname
;
88 #define ASSERT(ex) assert(ex)
91 * We have no need for the "linux" dev_t in userspace, so these
92 * are no-ops, and an xfs_dev_t is stored in VFS_I(ip)->i_rdev
94 #define xfs_to_linux_dev_t(dev) dev
95 #define linux_to_xfs_dev_t(dev) dev
98 #define EWRONGFS EINVAL
101 #define xfs_error_level 0
103 #define STATIC static
106 * Starting in Linux 4.15, the %p (raw pointer value) printk modifier
107 * prints a hashed version of the pointer to avoid leaking kernel
108 * pointers into dmesg. If we're trying to debug the kernel we want the
109 * raw values, so override this behavior as best we can.
111 * In userspace we don't have this problem.
115 /* XXX: need to push these out to make LIBXFS_ATTR defines */
116 #define ATTR_ROOT 0x0002
117 #define ATTR_SECURE 0x0008
118 #define ATTR_CREATE 0x0010
119 #define ATTR_REPLACE 0x0020
120 #define ATTR_KERNOTIME 0
121 #define ATTR_KERNOVAL 0
123 #define XFS_IGET_CREATE 0x1
124 #define XFS_IGET_UNTRUSTED 0x2
126 extern void cmn_err(int, char *, ...);
127 enum ce
{ CE_DEBUG
, CE_CONT
, CE_NOTE
, CE_WARN
, CE_ALERT
, CE_PANIC
};
129 #define xfs_notice(mp,fmt,args...) cmn_err(CE_NOTE,fmt, ## args)
130 #define xfs_warn(mp,fmt,args...) cmn_err(CE_WARN,fmt, ## args)
131 #define xfs_hex_dump(d,n) ((void) 0)
133 #define xfs_force_shutdown(d,n) ((void) 0)
135 /* stop unused var warnings by assigning mp to itself */
137 #define xfs_corruption_error(fu,e,l,mp,fi,ln,fa) do { \
139 cmn_err(CE_ALERT, "%s: XFS_CORRUPTION_ERROR", (e)); \
142 #define XFS_CORRUPTION_ERROR(e,l,mp,m) do { \
144 cmn_err(CE_ALERT, "%s: XFS_CORRUPTION_ERROR", (e)); \
147 #define XFS_ERROR_REPORT(e,l,mp) do { \
149 cmn_err(CE_ALERT, "%s: XFS_ERROR_REPORT", (e)); \
152 #define XFS_ERRLEVEL_LOW 1
153 #define XFS_FORCED_SHUTDOWN(mp) 0
154 #define XFS_ILOCK_EXCL 0
155 #define XFS_STATS_INC(mp, count) do { (mp) = (mp); } while (0)
156 #define XFS_STATS_DEC(mp, count, x) do { (mp) = (mp); } while (0)
157 #define XFS_STATS_ADD(mp, count, x) do { (mp) = (mp); } while (0)
158 #define XFS_TEST_ERROR(expr,a,b) ( expr )
159 #define XFS_WANT_CORRUPTED_GOTO(mp, expr, l) \
162 if ((mp)->m_flags & LIBXFS_MOUNT_WANT_CORRUPTED) \
163 printf("WANT_CORRUPTED_GOTO at %s:%d\n", \
164 __func__, __LINE__); \
165 error = -EFSCORRUPTED; \
169 #define XFS_WANT_CORRUPTED_RETURN(mp, expr) \
172 if ((mp)->m_flags & LIBXFS_MOUNT_WANT_CORRUPTED) \
173 printf("WANT_CORRUPTED_RETURN at %s:%d\n", \
174 __func__, __LINE__); \
175 return -EFSCORRUPTED; \
180 #define __return_address __builtin_return_address(0)
183 * Return the address of a label. Use barrier() so that the optimizer
184 * won't reorder code to refactor the error jumpouts into a single
185 * return, which throws off the reported address.
187 #define __this_address ({ __label__ __here; __here: barrier(); &&__here; })
188 /* Optimization barrier */
190 /* The "volatile" is due to gcc bugs */
191 #define barrier() __asm__ __volatile__("": : :"memory")
194 /* Optimization barrier */
196 # define barrier() __memory_barrier()
199 /* miscellaneous kernel routines not in user space */
200 #define down_read(a) ((void) 0)
201 #define up_read(a) ((void) 0)
202 #define spin_lock_init(a) ((void) 0)
203 #define spin_lock(a) ((void) 0)
204 #define spin_unlock(a) ((void) 0)
205 #define likely(x) (x)
206 #define unlikely(x) (x)
207 #define rcu_read_lock() ((void) 0)
208 #define rcu_read_unlock() ((void) 0)
209 /* Need to be able to handle this bare or in control flow */
210 static inline bool WARN_ON_ONCE(bool expr
) {
214 #define percpu_counter_read(x) (*x)
215 #define percpu_counter_read_positive(x) ((*x) > 0 ? (*x) : 0)
216 #define percpu_counter_sum(x) (*x)
219 * prandom_u32 is used for di_gen inode allocation, it must be zero for libxfs
220 * or all sorts of badness can occur!
222 #define prandom_u32() 0
224 #define PAGE_SIZE getpagesize()
226 #define inode_peek_iversion(inode) (inode)->i_version
227 #define inode_set_iversion_queried(inode, version) do { \
228 (inode)->i_version = (version); \
231 static inline int __do_div(unsigned long long *n
, unsigned base
)
234 __res
= (int)(((unsigned long) *n
) % (unsigned) base
);
235 *n
= ((unsigned long) *n
) / (unsigned) base
;
239 #define do_div(n,base) (__do_div((unsigned long long *)&(n), (base)))
240 #define do_mod(a, b) ((a) % (b))
241 #define rol32(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
244 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
245 * @dividend: unsigned 64bit dividend
246 * @divisor: unsigned 32bit divisor
247 * @remainder: pointer to unsigned 32bit remainder
249 * Return: sets ``*remainder``, then returns dividend / divisor
251 * This is commonly provided by 32bit archs to provide an optimized 64bit
254 static inline uint64_t
255 div_u64_rem(uint64_t dividend
, uint32_t divisor
, uint32_t *remainder
)
257 *remainder
= dividend
% divisor
;
258 return dividend
/ divisor
;
261 #define min_t(type,x,y) \
262 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
263 #define max_t(type,x,y) \
264 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
267 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
268 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
269 #define round_down(x, y) ((x) & ~__round_mask(x, y))
270 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
273 * Handling for kernel bitmap types.
275 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBBY * sizeof(long))
276 #define DECLARE_BITMAP(name,bits) \
277 unsigned long name[BITS_TO_LONGS(bits)]
278 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
281 * This is a common helper function for find_next_bit and
282 * find_next_zero_bit. The difference is the "invert" argument, which
283 * is XORed with each fetched word before searching it for one bits.
285 static inline unsigned long
286 _find_next_bit(const unsigned long *addr
, unsigned long nbits
,
287 unsigned long start
, unsigned long invert
)
291 if (!nbits
|| start
>= nbits
)
294 tmp
= addr
[start
/ BITS_PER_LONG
] ^ invert
;
296 /* Handle 1st word. */
297 tmp
&= BITMAP_FIRST_WORD_MASK(start
);
298 start
= round_down(start
, BITS_PER_LONG
);
301 start
+= BITS_PER_LONG
;
305 tmp
= addr
[start
/ BITS_PER_LONG
] ^ invert
;
308 return min(start
+ ffs(tmp
), nbits
);
312 * Find the next set bit in a memory region.
314 static inline unsigned long
315 find_next_bit(const unsigned long *addr
, unsigned long size
,
316 unsigned long offset
)
318 return _find_next_bit(addr
, size
, offset
, 0UL);
320 static inline unsigned long
321 find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
322 unsigned long offset
)
324 return _find_next_bit(addr
, size
, offset
, ~0UL);
326 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
328 static inline __attribute__((const))
329 int is_power_of_2(unsigned long n
)
331 return (n
!= 0 && ((n
& (n
- 1)) == 0));
335 * xfs_iroundup: round up argument to next power of two
338 roundup_pow_of_two(uint v
)
343 if ((v
& (v
- 1)) == 0)
345 ASSERT((v
& 0x80000000) == 0);
346 if ((v
& (v
+ 1)) == 0)
348 for (i
= 0, m
= 1; i
< 31; i
++, m
<<= 1) {
352 if ((v
& (v
+ 1)) == 0)
359 static inline uint64_t
360 roundup_64(uint64_t x
, uint32_t y
)
367 /* buffer management */
368 #define XBF_TRYLOCK 0
369 #define XBF_UNMAPPED 0
371 #define xfs_buf_stale(bp) ((bp)->b_flags |= LIBXFS_B_STALE)
372 #define XFS_BUF_UNDELAYWRITE(bp) ((bp)->b_flags &= ~LIBXFS_B_DIRTY)
373 #define XFS_BUF_SET_BDSTRAT_FUNC(a,b) ((void) 0)
375 /* avoid gcc warning */
376 #define xfs_buf_incore(bt,blkno,len,lockit) ({ \
377 typeof(blkno) __foo = (blkno); \
378 typeof(len) __bar = (len); \
380 (len) = __bar; /* no set-but-unused warning */ \
383 #define xfs_buf_relse(bp) libxfs_putbuf(bp)
384 #define xfs_buf_get(devp,blkno,len,f) (libxfs_getbuf((devp), (blkno), (len)))
385 #define xfs_bwrite(bp) libxfs_writebuf((bp), 0)
386 #define xfs_buf_delwri_queue(bp, bl) libxfs_writebuf((bp), 0)
388 #define XBRW_READ LIBXFS_BREAD
389 #define XBRW_WRITE LIBXFS_BWRITE
390 #define xfs_buf_iomove(bp,off,len,data,f) libxfs_iomove(bp,off,len,data,f)
391 #define xfs_buf_zero(bp,off,len) libxfs_iomove(bp,off,len,0,LIBXFS_BZERO)
394 #define XFS_MOUNT_32BITINODES LIBXFS_MOUNT_32BITINODES
395 #define XFS_MOUNT_ATTR2 LIBXFS_MOUNT_ATTR2
396 #define XFS_MOUNT_SMALL_INUMS 0 /* ignored in userspace */
397 #define XFS_MOUNT_WSYNC 0 /* ignored in userspace */
398 #define XFS_MOUNT_NOALIGN 0 /* ignored in userspace */
399 #define XFS_MOUNT_IKEEP 0 /* ignored in userspace */
400 #define XFS_MOUNT_SWALLOC 0 /* ignored in userspace */
401 #define XFS_MOUNT_RDONLY 0 /* ignored in userspace */
404 #define xfs_trans_set_sync(tp) ((void) 0)
405 #define xfs_trans_agblocks_delta(tp, d)
406 #define xfs_trans_agflist_delta(tp, d)
407 #define xfs_trans_agbtree_delta(tp, d)
408 #define xfs_trans_buf_set_type(tp, bp, t) ({ \
410 __t = __t; /* no set-but-unused warning */ \
411 tp = tp; /* no set-but-unused warning */ \
414 #define xfs_trans_buf_copy_type(dbp, sbp)
416 /* no readahead, need to avoid set-but-unused var warnings. */
417 #define xfs_buf_readahead(a,d,c,ops) ({ \
418 xfs_daddr_t __d = d; \
419 __d = __d; /* no set-but-unused warning */ \
421 #define xfs_buf_readahead_map(a,b,c,ops) ((void) 0) /* no readahead */
423 #define xfs_warn(mp,fmt,args...) cmn_err(CE_WARN,fmt, ## args)
424 #define xfs_alert(mp,fmt,args...) cmn_err(CE_ALERT,fmt, ## args)
425 #define xfs_alert_tag(mp,tag,fmt,args...) cmn_err(CE_ALERT,fmt, ## args)
427 #define xfs_sort qsort
429 #define xfs_ilock(ip,mode) ((void) 0)
430 #define xfs_ilock_nowait(ip,mode) ((void) 0)
431 #define xfs_ilock_demote(ip,mode) ((void) 0)
432 #define xfs_ilock_data_map_shared(ip) (0)
433 #define xfs_ilock_attr_map_shared(ip) (0)
434 #define xfs_iunlock(ip,mode) ({ \
435 typeof(mode) __mode = mode; \
436 __mode = __mode; /* no set-but-unused warning */ \
439 /* space allocation */
440 #define XFS_EXTENT_BUSY_DISCARDED 0x01 /* undergoing a discard op. */
441 #define XFS_EXTENT_BUSY_SKIP_DISCARD 0x02 /* do not discard */
443 #define xfs_extent_busy_reuse(mp,ag,bno,len,user) ((void) 0)
444 /* avoid unused variable warning */
445 #define xfs_extent_busy_insert(tp,ag,bno,len,flags)({ \
446 xfs_agnumber_t __foo = ag; \
447 __foo = __foo; /* no set-but-unused warning */ \
449 #define xfs_extent_busy_trim(args,bno,len,busy_gen) ({ \
450 unsigned __foo = *(busy_gen); \
451 *(busy_gen) = __foo; \
454 #define xfs_extent_busy_flush(mp,pag,busy_gen) ((void)(0))
456 #define xfs_rotorstep 1
457 #define xfs_bmap_rtalloc(a) (-ENOSYS)
458 #define xfs_get_extsz_hint(ip) (0)
459 #define xfs_get_cowextsz_hint(ip) (0)
460 #define xfs_inode_is_filestream(ip) (0)
461 #define xfs_filestream_lookup_ag(ip) (0)
462 #define xfs_filestream_new_ag(ip,ag) (0)
464 #define xfs_log_force(mp,flags) ((void) 0)
465 #define XFS_LOG_SYNC 1
468 #define xfs_trans_mod_dquot_byino(t,i,f,d) ((void) 0)
469 #define xfs_trans_reserve_quota_nblks(t,i,b,n,f) (0)
470 #define xfs_trans_unreserve_quota_nblks(t,i,b,n,f) ((void) 0)
471 #define xfs_qm_dqattach(i) (0)
473 #define uuid_copy(s,d) platform_uuid_copy((s),(d))
474 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
476 #define xfs_icreate_log(tp, agno, agbno, cnt, isize, len, gen) ((void) 0)
477 #define xfs_sb_validate_fsb_count(sbp, nblks) (0)
480 * Prototypes for kernel static functions that are aren't in their
481 * associated header files.
484 struct xfs_bmap_free
;
485 struct xfs_bmap_free_item
;
493 struct xfs_buf_log_item
;
497 int xfs_attr_rmtval_get(struct xfs_da_args
*);
500 void xfs_bmap_del_free(struct xfs_bmap_free
*, struct xfs_bmap_free_item
*);
503 int xfs_initialize_perag_data(struct xfs_mount
*, xfs_agnumber_t
);
504 void xfs_mount_common(struct xfs_mount
*, struct xfs_sb
*);
507 * logitem.c and trans.c prototypes
509 void xfs_trans_init(struct xfs_mount
*);
510 int xfs_trans_roll(struct xfs_trans
**);
512 /* xfs_trans_item.c */
513 void xfs_trans_add_item(struct xfs_trans
*, struct xfs_log_item
*);
514 void xfs_trans_del_item(struct xfs_log_item
*);
516 /* xfs_inode_item.c */
517 void xfs_inode_item_init(struct xfs_inode
*, struct xfs_mount
*);
520 void xfs_buf_item_init(struct xfs_buf
*, struct xfs_mount
*);
521 void xfs_buf_item_log(struct xfs_buf_log_item
*, uint
, uint
);
523 /* xfs_trans_buf.c */
524 struct xfs_buf
*xfs_trans_buf_item_match(struct xfs_trans
*,
525 struct xfs_buftarg
*, struct xfs_buf_map
*, int);
527 /* local source files */
528 #define xfs_mod_fdblocks(mp, delta, rsvd) \
529 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FDBLOCKS, delta, rsvd)
530 #define xfs_mod_frextents(mp, delta) \
531 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FREXTENTS, delta, 0)
532 int libxfs_mod_incore_sb(struct xfs_mount
*, int, int64_t, int);
533 /* percpu counters in mp are #defined to the superblock sb_ counters */
534 #define xfs_reinit_percpu_counters(mp)
536 void xfs_trans_mod_sb(struct xfs_trans
*, uint
, long);
538 void xfs_verifier_error(struct xfs_buf
*bp
, int error
,
539 xfs_failaddr_t failaddr
);
540 void xfs_inode_verifier_error(struct xfs_inode
*ip
, int error
,
541 const char *name
, void *buf
, size_t bufsz
,
542 xfs_failaddr_t failaddr
);
544 #define xfs_buf_verifier_error(bp,e,n,bu,bus,fa) \
545 xfs_verifier_error(bp, e, fa)
547 /* XXX: this is clearly a bug - a shared header needs to export this */
549 int libxfs_rtfree_extent(struct xfs_trans
*, xfs_rtblock_t
, xfs_extlen_t
);
550 bool libxfs_verify_rtbno(struct xfs_mount
*mp
, xfs_rtblock_t rtbno
);
552 struct xfs_rtalloc_rec
{
553 xfs_rtblock_t ar_startblock
;
554 xfs_rtblock_t ar_blockcount
;
557 typedef int (*xfs_rtalloc_query_range_fn
)(
558 struct xfs_trans
*tp
,
559 struct xfs_rtalloc_rec
*rec
,
562 int libxfs_zero_extent(struct xfs_inode
*ip
, xfs_fsblock_t start_fsb
,
563 xfs_off_t count_fsb
);
565 bool xfs_log_check_lsn(struct xfs_mount
*, xfs_lsn_t
);
568 #define xfs_inode_set_cowblocks_tag(ip) do { } while (0)
569 #define xfs_inode_set_eofblocks_tag(ip) do { } while (0)
572 #define XFS_STATS_CALC_INDEX(member) 0
573 #define XFS_STATS_INC_OFF(mp, off)
574 #define XFS_STATS_ADD_OFF(mp, off, val)
576 typedef unsigned char u8
;
577 unsigned int hweight8(unsigned int w
);
579 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
580 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
582 static inline void set_bit(int nr
, volatile unsigned long *addr
)
584 unsigned long mask
= BIT_MASK(nr
);
585 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
590 static inline void clear_bit(int nr
, volatile unsigned long *addr
)
592 unsigned long mask
= BIT_MASK(nr
);
593 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
598 static inline int test_bit(int nr
, const volatile unsigned long *addr
)
600 unsigned long mask
= BIT_MASK(nr
);
601 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
607 #endif /* __LIBXFS_INTERNAL_XFS_H__ */