]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/libxfs_priv.h
xfs: convert a few more directory asserts to corruption
[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 /*
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
93 */
94 #define xfs_to_linux_dev_t(dev) dev
95 #define linux_to_xfs_dev_t(dev) dev
96
97 #ifndef EWRONGFS
98 #define EWRONGFS EINVAL
99 #endif
100
101 #define xfs_error_level 0
102
103 #define STATIC static
104
105 /*
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.
110 *
111 * In userspace we don't have this problem.
112 */
113 #define PTR_FMT "%p"
114
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
122
123 #define XFS_IGET_CREATE 0x1
124 #define XFS_IGET_UNTRUSTED 0x2
125
126 extern void cmn_err(int, char *, ...);
127 enum ce { CE_DEBUG, CE_CONT, CE_NOTE, CE_WARN, CE_ALERT, CE_PANIC };
128
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)
132
133 #define xfs_force_shutdown(d,n) ((void) 0)
134
135 /* stop unused var warnings by assigning mp to itself */
136 #define XFS_CORRUPTION_ERROR(e,l,mp,m) do { \
137 (mp) = (mp); \
138 cmn_err(CE_ALERT, "%s: XFS_CORRUPTION_ERROR", (e)); \
139 } while (0)
140
141 #define XFS_ERROR_REPORT(e,l,mp) do { \
142 (mp) = (mp); \
143 cmn_err(CE_ALERT, "%s: XFS_ERROR_REPORT", (e)); \
144 } while (0)
145
146 #define XFS_ERRLEVEL_LOW 1
147 #define XFS_FORCED_SHUTDOWN(mp) 0
148 #define XFS_ILOCK_EXCL 0
149 #define XFS_STATS_INC(mp, count) do { (mp) = (mp); } while (0)
150 #define XFS_STATS_DEC(mp, count, x) do { (mp) = (mp); } while (0)
151 #define XFS_STATS_ADD(mp, count, x) do { (mp) = (mp); } while (0)
152 #define XFS_TEST_ERROR(expr,a,b) ( expr )
153 #define XFS_WANT_CORRUPTED_GOTO(mp, expr, l) \
154 { \
155 if (!(expr)) { \
156 if ((mp)->m_flags & LIBXFS_MOUNT_WANT_CORRUPTED) \
157 printf("WANT_CORRUPTED_GOTO at %s:%d\n", \
158 __func__, __LINE__); \
159 error = -EFSCORRUPTED; \
160 goto l; \
161 } \
162 }
163 #define XFS_WANT_CORRUPTED_RETURN(mp, expr) \
164 { \
165 if (!(expr)) { \
166 if ((mp)->m_flags & LIBXFS_MOUNT_WANT_CORRUPTED) \
167 printf("WANT_CORRUPTED_RETURN at %s:%d\n", \
168 __func__, __LINE__); \
169 return -EFSCORRUPTED; \
170 } \
171 }
172
173 #ifdef __GNUC__
174 #define __return_address __builtin_return_address(0)
175
176 /*
177 * Return the address of a label. Use barrier() so that the optimizer
178 * won't reorder code to refactor the error jumpouts into a single
179 * return, which throws off the reported address.
180 */
181 #define __this_address ({ __label__ __here; __here: barrier(); &&__here; })
182 /* Optimization barrier */
183
184 /* The "volatile" is due to gcc bugs */
185 #define barrier() __asm__ __volatile__("": : :"memory")
186 #endif
187
188 /* Optimization barrier */
189 #ifndef barrier
190 # define barrier() __memory_barrier()
191 #endif
192
193 #define XFS_DQUOT_CLUSTER_SIZE_FSB (xfs_filblks_t)1
194
195 /* miscellaneous kernel routines not in user space */
196 #define down_read(a) ((void) 0)
197 #define up_read(a) ((void) 0)
198 #define spin_lock_init(a) ((void) 0)
199 #define spin_lock(a) ((void) 0)
200 #define spin_unlock(a) ((void) 0)
201 #define likely(x) (x)
202 #define unlikely(x) (x)
203 #define rcu_read_lock() ((void) 0)
204 #define rcu_read_unlock() ((void) 0)
205 /* Need to be able to handle this bare or in control flow */
206 static inline bool WARN_ON_ONCE(bool expr) {
207 return (expr);
208 }
209
210 #define percpu_counter_read(x) (*x)
211 #define percpu_counter_read_positive(x) ((*x) > 0 ? (*x) : 0)
212 #define percpu_counter_sum(x) (*x)
213
214 /*
215 * prandom_u32 is used for di_gen inode allocation, it must be zero for libxfs
216 * or all sorts of badness can occur!
217 */
218 #define prandom_u32() 0
219
220 #define PAGE_SIZE getpagesize()
221
222 #define inode_peek_iversion(inode) (inode)->i_version
223 #define inode_set_iversion_queried(inode, version) do { \
224 (inode)->i_version = (version); \
225 } while (0)
226
227 static inline int __do_div(unsigned long long *n, unsigned base)
228 {
229 int __res;
230 __res = (int)(((unsigned long) *n) % (unsigned) base);
231 *n = ((unsigned long) *n) / (unsigned) base;
232 return __res;
233 }
234
235 #define do_div(n,base) (__do_div((unsigned long long *)&(n), (base)))
236 #define do_mod(a, b) ((a) % (b))
237 #define rol32(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
238
239 /**
240 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
241 * @dividend: unsigned 64bit dividend
242 * @divisor: unsigned 32bit divisor
243 * @remainder: pointer to unsigned 32bit remainder
244 *
245 * Return: sets ``*remainder``, then returns dividend / divisor
246 *
247 * This is commonly provided by 32bit archs to provide an optimized 64bit
248 * divide.
249 */
250 static inline uint64_t
251 div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
252 {
253 *remainder = dividend % divisor;
254 return dividend / divisor;
255 }
256
257 #define min_t(type,x,y) \
258 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
259 #define max_t(type,x,y) \
260 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
261
262
263 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
264 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
265 #define round_down(x, y) ((x) & ~__round_mask(x, y))
266 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
267
268 /*
269 * Handling for kernel bitmap types.
270 */
271 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBBY * sizeof(long))
272 #define DECLARE_BITMAP(name,bits) \
273 unsigned long name[BITS_TO_LONGS(bits)]
274 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
275
276 /*
277 * This is a common helper function for find_next_bit and
278 * find_next_zero_bit. The difference is the "invert" argument, which
279 * is XORed with each fetched word before searching it for one bits.
280 */
281 static inline unsigned long
282 _find_next_bit(const unsigned long *addr, unsigned long nbits,
283 unsigned long start, unsigned long invert)
284 {
285 unsigned long tmp;
286
287 if (!nbits || start >= nbits)
288 return nbits;
289
290 tmp = addr[start / BITS_PER_LONG] ^ invert;
291
292 /* Handle 1st word. */
293 tmp &= BITMAP_FIRST_WORD_MASK(start);
294 start = round_down(start, BITS_PER_LONG);
295
296 while (!tmp) {
297 start += BITS_PER_LONG;
298 if (start >= nbits)
299 return nbits;
300
301 tmp = addr[start / BITS_PER_LONG] ^ invert;
302 }
303
304 return min(start + ffs(tmp), nbits);
305 }
306
307 /*
308 * Find the next set bit in a memory region.
309 */
310 static inline unsigned long
311 find_next_bit(const unsigned long *addr, unsigned long size,
312 unsigned long offset)
313 {
314 return _find_next_bit(addr, size, offset, 0UL);
315 }
316 static inline unsigned long
317 find_next_zero_bit(const unsigned long *addr, unsigned long size,
318 unsigned long offset)
319 {
320 return _find_next_bit(addr, size, offset, ~0UL);
321 }
322 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
323
324 static inline __attribute__((const))
325 int is_power_of_2(unsigned long n)
326 {
327 return (n != 0 && ((n & (n - 1)) == 0));
328 }
329
330 /*
331 * xfs_iroundup: round up argument to next power of two
332 */
333 static inline uint
334 roundup_pow_of_two(uint v)
335 {
336 int i;
337 uint m;
338
339 if ((v & (v - 1)) == 0)
340 return v;
341 ASSERT((v & 0x80000000) == 0);
342 if ((v & (v + 1)) == 0)
343 return v + 1;
344 for (i = 0, m = 1; i < 31; i++, m <<= 1) {
345 if (v & m)
346 continue;
347 v |= m;
348 if ((v & (v + 1)) == 0)
349 return v + 1;
350 }
351 ASSERT(0);
352 return 0;
353 }
354
355 static inline uint64_t
356 roundup_64(uint64_t x, uint32_t y)
357 {
358 x += y - 1;
359 do_div(x, y);
360 return x * y;
361 }
362
363 /* buffer management */
364 #define XBF_TRYLOCK 0
365 #define XBF_UNMAPPED 0
366 #define XBF_DONE 0
367 #define xfs_buf_stale(bp) ((bp)->b_flags |= LIBXFS_B_STALE)
368 #define XFS_BUF_UNDELAYWRITE(bp) ((bp)->b_flags &= ~LIBXFS_B_DIRTY)
369 #define XFS_BUF_SET_BDSTRAT_FUNC(a,b) ((void) 0)
370
371 /* avoid gcc warning */
372 #define xfs_incore(bt,blkno,len,lockit) ({ \
373 typeof(blkno) __foo = (blkno); \
374 typeof(len) __bar = (len); \
375 (blkno) = __foo; \
376 (len) = __bar; /* no set-but-unused warning */ \
377 NULL; \
378 })
379 #define xfs_buf_relse(bp) libxfs_putbuf(bp)
380 #define xfs_buf_get(devp,blkno,len,f) (libxfs_getbuf((devp), (blkno), (len)))
381 #define xfs_bwrite(bp) libxfs_writebuf((bp), 0)
382 #define xfs_buf_delwri_queue(bp, bl) libxfs_writebuf((bp), 0)
383
384 #define XBRW_READ LIBXFS_BREAD
385 #define XBRW_WRITE LIBXFS_BWRITE
386 #define xfs_buf_iomove(bp,off,len,data,f) libxfs_iomove(bp,off,len,data,f)
387 #define xfs_buf_zero(bp,off,len) libxfs_iomove(bp,off,len,0,LIBXFS_BZERO)
388
389 /* mount stuff */
390 #define XFS_MOUNT_32BITINODES LIBXFS_MOUNT_32BITINODES
391 #define XFS_MOUNT_ATTR2 LIBXFS_MOUNT_ATTR2
392 #define XFS_MOUNT_SMALL_INUMS 0 /* ignored in userspace */
393 #define XFS_MOUNT_WSYNC 0 /* ignored in userspace */
394 #define XFS_MOUNT_NOALIGN 0 /* ignored in userspace */
395 #define XFS_MOUNT_IKEEP 0 /* ignored in userspace */
396 #define XFS_MOUNT_SWALLOC 0 /* ignored in userspace */
397 #define XFS_MOUNT_RDONLY 0 /* ignored in userspace */
398
399
400 #define xfs_trans_set_sync(tp) ((void) 0)
401 #define xfs_trans_agblocks_delta(tp, d)
402 #define xfs_trans_agflist_delta(tp, d)
403 #define xfs_trans_agbtree_delta(tp, d)
404 #define xfs_trans_buf_set_type(tp, bp, t) ({ \
405 int __t = (t); \
406 __t = __t; /* no set-but-unused warning */ \
407 tp = tp; /* no set-but-unused warning */ \
408 })
409
410 #define xfs_trans_buf_copy_type(dbp, sbp)
411
412 /* no readahead, need to avoid set-but-unused var warnings. */
413 #define xfs_buf_readahead(a,d,c,ops) ({ \
414 xfs_daddr_t __d = d; \
415 __d = __d; /* no set-but-unused warning */ \
416 })
417 #define xfs_buf_readahead_map(a,b,c,ops) ((void) 0) /* no readahead */
418
419 #define xfs_warn(mp,fmt,args...) cmn_err(CE_WARN,fmt, ## args)
420 #define xfs_alert(mp,fmt,args...) cmn_err(CE_ALERT,fmt, ## args)
421 #define xfs_alert_tag(mp,tag,fmt,args...) cmn_err(CE_ALERT,fmt, ## args)
422
423 #define xfs_sort qsort
424
425 #define xfs_ilock(ip,mode) ((void) 0)
426 #define xfs_ilock_nowait(ip,mode) ((void) 0)
427 #define xfs_ilock_demote(ip,mode) ((void) 0)
428 #define xfs_ilock_data_map_shared(ip) (0)
429 #define xfs_ilock_attr_map_shared(ip) (0)
430 #define xfs_iunlock(ip,mode) ({ \
431 typeof(mode) __mode = mode; \
432 __mode = __mode; /* no set-but-unused warning */ \
433 })
434
435 /* space allocation */
436 #define xfs_extent_busy_reuse(mp,ag,bno,len,user) ((void) 0)
437 /* avoid unused variable warning */
438 #define xfs_extent_busy_insert(tp,ag,bno,len,flags)({ \
439 xfs_agnumber_t __foo = ag; \
440 __foo = __foo; /* no set-but-unused warning */ \
441 })
442 #define xfs_extent_busy_trim(args,bno,len,busy_gen) ({ \
443 unsigned __foo = *(busy_gen); \
444 *(busy_gen) = __foo; \
445 false; \
446 })
447 #define xfs_extent_busy_flush(mp,pag,busy_gen) ((void)(0))
448
449 #define xfs_rotorstep 1
450 #define xfs_bmap_rtalloc(a) (-ENOSYS)
451 #define xfs_get_extsz_hint(ip) (0)
452 #define xfs_get_cowextsz_hint(ip) (0)
453 #define xfs_inode_is_filestream(ip) (0)
454 #define xfs_filestream_lookup_ag(ip) (0)
455 #define xfs_filestream_new_ag(ip,ag) (0)
456
457 #define xfs_log_force(mp,flags) ((void) 0)
458 #define XFS_LOG_SYNC 1
459
460 /* quota bits */
461 #define xfs_trans_mod_dquot_byino(t,i,f,d) ((void) 0)
462 #define xfs_trans_reserve_quota_nblks(t,i,b,n,f) (0)
463 #define xfs_trans_unreserve_quota_nblks(t,i,b,n,f) ((void) 0)
464 #define xfs_qm_dqattach(i,f) (0)
465
466 #define uuid_copy(s,d) platform_uuid_copy((s),(d))
467 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
468
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
472 /*
473 * Prototypes for kernel static functions that are aren't in their
474 * associated header files.
475 */
476 struct xfs_da_args;
477 struct xfs_bmap_free;
478 struct xfs_bmap_free_item;
479 struct xfs_mount;
480 struct xfs_sb;
481 struct xfs_trans;
482 struct xfs_inode;
483 struct xfs_log_item;
484 struct xfs_buf;
485 struct xfs_buf_map;
486 struct xfs_buf_log_item;
487 struct xfs_buftarg;
488
489 /* xfs_attr.c */
490 int xfs_attr_rmtval_get(struct xfs_da_args *);
491
492 /* xfs_bmap.c */
493 void xfs_bmap_del_free(struct xfs_bmap_free *, struct xfs_bmap_free_item *);
494
495 /* xfs_mount.c */
496 int xfs_initialize_perag_data(struct xfs_mount *, xfs_agnumber_t);
497 void xfs_mount_common(struct xfs_mount *, struct xfs_sb *);
498
499 /*
500 * logitem.c and trans.c prototypes
501 */
502 void xfs_trans_init(struct xfs_mount *);
503 int xfs_trans_roll(struct xfs_trans **);
504
505 /* xfs_trans_item.c */
506 void xfs_trans_add_item(struct xfs_trans *, struct xfs_log_item *);
507 void xfs_trans_del_item(struct xfs_log_item *);
508
509 /* xfs_inode_item.c */
510 void xfs_inode_item_init(struct xfs_inode *, struct xfs_mount *);
511
512 /* xfs_buf_item.c */
513 void xfs_buf_item_init(struct xfs_buf *, struct xfs_mount *);
514 void xfs_buf_item_log(struct xfs_buf_log_item *, uint, uint);
515
516 /* xfs_trans_buf.c */
517 struct xfs_buf *xfs_trans_buf_item_match(struct xfs_trans *,
518 struct xfs_buftarg *, struct xfs_buf_map *, int);
519
520 /* local source files */
521 #define xfs_mod_fdblocks(mp, delta, rsvd) \
522 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FDBLOCKS, delta, rsvd)
523 #define xfs_mod_frextents(mp, delta) \
524 libxfs_mod_incore_sb(mp, XFS_TRANS_SB_FREXTENTS, delta, 0)
525 int libxfs_mod_incore_sb(struct xfs_mount *, int, int64_t, int);
526 /* percpu counters in mp are #defined to the superblock sb_ counters */
527 #define xfs_reinit_percpu_counters(mp)
528
529 void xfs_trans_mod_sb(struct xfs_trans *, uint, long);
530 void xfs_verifier_error(struct xfs_buf *bp, int error,
531 xfs_failaddr_t failaddr);
532 void xfs_inode_verifier_error(struct xfs_inode *ip, int error,
533 const char *name, void *buf, size_t bufsz,
534 xfs_failaddr_t failaddr);
535
536 /* XXX: this is clearly a bug - a shared header needs to export this */
537 /* xfs_rtalloc.c */
538 int libxfs_rtfree_extent(struct xfs_trans *, xfs_rtblock_t, xfs_extlen_t);
539
540 struct xfs_rtalloc_rec {
541 xfs_rtblock_t ar_startblock;
542 xfs_rtblock_t ar_blockcount;
543 };
544
545 typedef int (*xfs_rtalloc_query_range_fn)(
546 struct xfs_trans *tp,
547 struct xfs_rtalloc_rec *rec,
548 void *priv);
549
550 int libxfs_zero_extent(struct xfs_inode *ip, xfs_fsblock_t start_fsb,
551 xfs_off_t count_fsb);
552
553 bool xfs_log_check_lsn(struct xfs_mount *, xfs_lsn_t);
554
555 /* xfs_icache.c */
556 #define xfs_inode_set_cowblocks_tag(ip) do { } while (0)
557 #define xfs_inode_set_eofblocks_tag(ip) do { } while (0)
558
559 /* xfs_stats.h */
560 #define XFS_STATS_CALC_INDEX(member) 0
561 #define XFS_STATS_INC_OFF(mp, off)
562 #define XFS_STATS_ADD_OFF(mp, off, val)
563
564 typedef unsigned char u8;
565 unsigned int hweight8(unsigned int w);
566
567 #endif /* __LIBXFS_INTERNAL_XFS_H__ */