^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/befs/endian.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Partially based on similar funtions in the sysv driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #ifndef LINUX_BEFS_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define LINUX_BEFS_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static inline u64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) fs64_to_cpu(const struct super_block *sb, fs64 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return le64_to_cpu((__force __le64)n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return be64_to_cpu((__force __be64)n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static inline fs64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) cpu_to_fs64(const struct super_block *sb, u64 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return (__force fs64)cpu_to_le64(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return (__force fs64)cpu_to_be64(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static inline u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) fs32_to_cpu(const struct super_block *sb, fs32 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return le32_to_cpu((__force __le32)n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return be32_to_cpu((__force __be32)n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static inline fs32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) cpu_to_fs32(const struct super_block *sb, u32 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return (__force fs32)cpu_to_le32(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return (__force fs32)cpu_to_be32(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static inline u16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) fs16_to_cpu(const struct super_block *sb, fs16 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return le16_to_cpu((__force __le16)n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return be16_to_cpu((__force __be16)n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline fs16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) cpu_to_fs16(const struct super_block *sb, u16 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return (__force fs16)cpu_to_le16(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return (__force fs16)cpu_to_be16(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Composite types below here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline befs_block_run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fsrun_to_cpu(const struct super_block *sb, befs_disk_block_run n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) befs_block_run run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) run.allocation_group = le32_to_cpu((__force __le32)n.allocation_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) run.start = le16_to_cpu((__force __le16)n.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) run.len = le16_to_cpu((__force __le16)n.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) run.allocation_group = be32_to_cpu((__force __be32)n.allocation_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) run.start = be16_to_cpu((__force __be16)n.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) run.len = be16_to_cpu((__force __be16)n.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static inline befs_disk_block_run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cpu_to_fsrun(const struct super_block *sb, befs_block_run n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) befs_disk_block_run run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (BEFS_SB(sb)->byte_order == BEFS_BYTESEX_LE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) run.allocation_group = cpu_to_le32(n.allocation_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) run.start = cpu_to_le16(n.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) run.len = cpu_to_le16(n.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) run.allocation_group = cpu_to_be32(n.allocation_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) run.start = cpu_to_be16(n.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) run.len = cpu_to_be16(n.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline befs_data_stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fsds_to_cpu(const struct super_block *sb, const befs_disk_data_stream *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) befs_data_stream data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) for (i = 0; i < BEFS_NUM_DIRECT_BLOCKS; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) data.direct[i] = fsrun_to_cpu(sb, n->direct[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) data.max_direct_range = fs64_to_cpu(sb, n->max_direct_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) data.indirect = fsrun_to_cpu(sb, n->indirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) data.max_indirect_range = fs64_to_cpu(sb, n->max_indirect_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) data.double_indirect = fsrun_to_cpu(sb, n->double_indirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) data.max_double_indirect_range = fs64_to_cpu(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) n->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) max_double_indirect_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) data.size = fs64_to_cpu(sb, n->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif //LINUX_BEFS_ENDIAN