Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Russell King | 1dfdfc9 | 2019-06-04 14:49:30 +0100 | [diff] [blame] | 2 | #include <linux/buffer_head.h> |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 3 | #include <linux/fs.h> |
| 4 | #include <linux/adfs_fs.h> |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | /* Internal data structures for ADFS */ |
| 7 | |
| 8 | #define ADFS_FREE_FRAG 0 |
| 9 | #define ADFS_BAD_FRAG 1 |
| 10 | #define ADFS_ROOT_FRAG 2 |
| 11 | |
Russell King | b4ed8f7 | 2019-06-04 14:50:24 +0100 | [diff] [blame] | 12 | #define ADFS_FILETYPE_NONE ((u16)~0) |
| 13 | |
| 14 | /* RISC OS 12-bit filetype is stored in load_address[19:8] */ |
| 15 | static inline u16 adfs_filetype(u32 loadaddr) |
| 16 | { |
| 17 | return (loadaddr & 0xfff00000) == 0xfff00000 ? |
| 18 | (loadaddr >> 8) & 0xfff : ADFS_FILETYPE_NONE; |
| 19 | } |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define ADFS_NDA_OWNER_READ (1 << 0) |
| 22 | #define ADFS_NDA_OWNER_WRITE (1 << 1) |
| 23 | #define ADFS_NDA_LOCKED (1 << 2) |
| 24 | #define ADFS_NDA_DIRECTORY (1 << 3) |
| 25 | #define ADFS_NDA_EXECUTE (1 << 4) |
| 26 | #define ADFS_NDA_PUBLIC_READ (1 << 5) |
| 27 | #define ADFS_NDA_PUBLIC_WRITE (1 << 6) |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "dir_f.h" |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 32 | * adfs file system inode data in memory |
| 33 | */ |
| 34 | struct adfs_inode_info { |
| 35 | loff_t mmu_private; |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 36 | __u32 parent_id; /* parent indirect disc address */ |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 37 | __u32 loadaddr; /* RISC OS load address */ |
| 38 | __u32 execaddr; /* RISC OS exec address */ |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 39 | unsigned int attr; /* RISC OS permissions */ |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 40 | struct inode vfs_inode; |
| 41 | }; |
| 42 | |
Russell King | b4ed8f7 | 2019-06-04 14:50:24 +0100 | [diff] [blame] | 43 | static inline struct adfs_inode_info *ADFS_I(struct inode *inode) |
| 44 | { |
| 45 | return container_of(inode, struct adfs_inode_info, vfs_inode); |
| 46 | } |
| 47 | |
| 48 | static inline bool adfs_inode_is_stamped(struct inode *inode) |
| 49 | { |
| 50 | return (ADFS_I(inode)->loadaddr & 0xfff00000) == 0xfff00000; |
| 51 | } |
| 52 | |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 53 | /* |
| 54 | * Forward-declare this |
| 55 | */ |
| 56 | struct adfs_discmap; |
| 57 | struct adfs_dir_ops; |
| 58 | |
| 59 | /* |
| 60 | * ADFS file system superblock data in memory |
| 61 | */ |
| 62 | struct adfs_sb_info { |
Al Viro | 2d1d9b5 | 2013-10-03 12:37:18 -0400 | [diff] [blame] | 63 | union { struct { |
Andrew Morton | 90d6cd5 | 2016-01-20 15:01:16 -0800 | [diff] [blame] | 64 | struct adfs_discmap *s_map; /* bh list containing map */ |
| 65 | const struct adfs_dir_ops *s_dir; /* directory operations */ |
Al Viro | 2d1d9b5 | 2013-10-03 12:37:18 -0400 | [diff] [blame] | 66 | }; |
Andrew Morton | 90d6cd5 | 2016-01-20 15:01:16 -0800 | [diff] [blame] | 67 | struct rcu_head rcu; /* used only at shutdown time */ |
Al Viro | 2d1d9b5 | 2013-10-03 12:37:18 -0400 | [diff] [blame] | 68 | }; |
Andrew Morton | 90d6cd5 | 2016-01-20 15:01:16 -0800 | [diff] [blame] | 69 | kuid_t s_uid; /* owner uid */ |
| 70 | kgid_t s_gid; /* owner gid */ |
| 71 | umode_t s_owner_mask; /* ADFS owner perm -> unix perm */ |
| 72 | umode_t s_other_mask; /* ADFS other perm -> unix perm */ |
Stuart Swales | da23ef0 | 2011-03-22 16:35:06 -0700 | [diff] [blame] | 73 | int s_ftsuffix; /* ,xyz hex filetype suffix option */ |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 74 | |
Andrew Morton | 90d6cd5 | 2016-01-20 15:01:16 -0800 | [diff] [blame] | 75 | __u32 s_ids_per_zone; /* max. no ids in one zone */ |
| 76 | __u32 s_idlen; /* length of ID in map */ |
| 77 | __u32 s_map_size; /* sector size of a map */ |
Andrew Morton | 90d6cd5 | 2016-01-20 15:01:16 -0800 | [diff] [blame] | 78 | signed int s_map2blk; /* shift left by this for map->sector*/ |
| 79 | unsigned int s_log2sharesize;/* log2 share size */ |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 80 | unsigned int s_namelen; /* maximum number of characters in name */ |
| 81 | }; |
| 82 | |
| 83 | static inline struct adfs_sb_info *ADFS_SB(struct super_block *sb) |
| 84 | { |
| 85 | return sb->s_fs_info; |
| 86 | } |
| 87 | |
Al Viro | 608ba50 | 2009-06-16 14:52:13 -0400 | [diff] [blame] | 88 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * Directory handling |
| 90 | */ |
| 91 | struct adfs_dir { |
| 92 | struct super_block *sb; |
| 93 | |
| 94 | int nr_buffers; |
| 95 | struct buffer_head *bh[4]; |
Russell King | 71b2612 | 2019-12-09 11:09:10 +0000 | [diff] [blame] | 96 | struct buffer_head **bhs; |
Stuart Swales | 2f09719 | 2011-03-22 16:35:04 -0700 | [diff] [blame] | 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | unsigned int pos; |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 99 | __u32 parent_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | struct adfs_dirheader dirhead; |
| 102 | union adfs_dirtail dirtail; |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * This is the overall maximum name length |
| 107 | */ |
Stuart Swales | da23ef0 | 2011-03-22 16:35:06 -0700 | [diff] [blame] | 108 | #define ADFS_MAX_NAME_LEN (256 + 4) /* +4 for ,xyz hex filetype suffix */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | struct object_info { |
| 110 | __u32 parent_id; /* parent object id */ |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 111 | __u32 indaddr; /* indirect disc addr */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | __u32 loadaddr; /* load address */ |
| 113 | __u32 execaddr; /* execution address */ |
| 114 | __u32 size; /* size */ |
| 115 | __u8 attr; /* RISC OS attributes */ |
Stuart Swales | da23ef0 | 2011-03-22 16:35:06 -0700 | [diff] [blame] | 116 | unsigned int name_len; /* name length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | char name[ADFS_MAX_NAME_LEN];/* file name */ |
| 118 | }; |
| 119 | |
| 120 | struct adfs_dir_ops { |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 121 | int (*read)(struct super_block *sb, unsigned int indaddr, |
| 122 | unsigned int size, struct adfs_dir *dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | int (*setpos)(struct adfs_dir *dir, unsigned int fpos); |
| 124 | int (*getnext)(struct adfs_dir *dir, struct object_info *obj); |
| 125 | int (*update)(struct adfs_dir *dir, struct object_info *obj); |
| 126 | int (*create)(struct adfs_dir *dir, struct object_info *obj); |
| 127 | int (*remove)(struct adfs_dir *dir, struct object_info *obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct adfs_discmap { |
| 131 | struct buffer_head *dm_bh; |
| 132 | __u32 dm_startblk; |
| 133 | unsigned int dm_startbit; |
| 134 | unsigned int dm_endbit; |
| 135 | }; |
| 136 | |
| 137 | /* Inode stuff */ |
| 138 | struct inode *adfs_iget(struct super_block *sb, struct object_info *obj); |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 139 | int adfs_write_inode(struct inode *inode, struct writeback_control *wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | int adfs_notify_change(struct dentry *dentry, struct iattr *attr); |
| 141 | |
| 142 | /* map.c */ |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 143 | int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset); |
Russell King | e6160e4 | 2019-12-09 11:08:34 +0000 | [diff] [blame] | 144 | void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf); |
Russell King | f75d398 | 2019-12-09 11:08:28 +0000 | [diff] [blame] | 145 | struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr); |
Russell King | 7b19526 | 2019-12-09 11:08:44 +0000 | [diff] [blame] | 146 | void adfs_free_map(struct super_block *sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* Misc */ |
Joe Perches | 19bdd41a5 | 2014-08-08 14:22:26 -0700 | [diff] [blame] | 149 | __printf(3, 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | void __adfs_error(struct super_block *sb, const char *function, |
| 151 | const char *fmt, ...); |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 152 | #define adfs_error(sb, fmt...) __adfs_error(sb, __func__, fmt) |
Russell King | ceb3b10 | 2019-06-04 14:49:52 +0100 | [diff] [blame] | 153 | void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* super.c */ |
| 156 | |
| 157 | /* |
| 158 | * Inodes and file operations |
| 159 | */ |
| 160 | |
| 161 | /* dir_*.c */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 162 | extern const struct inode_operations adfs_dir_inode_operations; |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 163 | extern const struct file_operations adfs_dir_operations; |
Al Viro | e16404e | 2009-02-20 05:55:13 +0000 | [diff] [blame] | 164 | extern const struct dentry_operations adfs_dentry_operations; |
Julia Lawall | 0125f50 | 2015-11-21 16:15:37 +0100 | [diff] [blame] | 165 | extern const struct adfs_dir_ops adfs_f_dir_ops; |
| 166 | extern const struct adfs_dir_ops adfs_fplus_dir_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Russell King | a317120 | 2019-12-09 11:09:30 +0000 | [diff] [blame^] | 168 | int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset, |
| 169 | size_t len); |
| 170 | int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src, |
| 171 | size_t len); |
Russell King | 1dd9f5b | 2019-12-09 11:09:20 +0000 | [diff] [blame] | 172 | void adfs_dir_relse(struct adfs_dir *dir); |
Russell King | 411c49b | 2019-03-24 12:57:32 +0000 | [diff] [blame] | 173 | void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj); |
Al Viro | ffdc906 | 2009-06-08 00:44:42 -0400 | [diff] [blame] | 174 | extern int adfs_dir_update(struct super_block *sb, struct object_info *obj, |
| 175 | int wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* file.c */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 178 | extern const struct inode_operations adfs_file_inode_operations; |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 179 | extern const struct file_operations adfs_file_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Adrian Bunk | e0c9314 | 2005-08-20 17:20:28 +0100 | [diff] [blame] | 181 | static inline __u32 signed_asl(__u32 val, signed int shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | if (shift >= 0) |
| 184 | val <<= shift; |
| 185 | else |
| 186 | val >>= -shift; |
| 187 | return val; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Calculate the address of a block in an object given the block offset |
| 192 | * and the object identity. |
| 193 | * |
| 194 | * The root directory ID should always be looked up in the map [3.4] |
| 195 | */ |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 196 | static inline int __adfs_block_map(struct super_block *sb, u32 indaddr, |
| 197 | unsigned int block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 199 | if (indaddr & 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | unsigned int off; |
| 201 | |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 202 | off = (indaddr & 255) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | block += off << ADFS_SB(sb)->s_log2sharesize; |
| 204 | } |
| 205 | |
Russell King | 5ed70bb | 2019-06-04 14:49:57 +0100 | [diff] [blame] | 206 | return adfs_map_lookup(sb, indaddr >> 8, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Russell King | 1dfdfc9 | 2019-06-04 14:49:30 +0100 | [diff] [blame] | 208 | |
| 209 | /* Return the disc record from the map */ |
| 210 | static inline |
| 211 | struct adfs_discrecord *adfs_map_discrecord(struct adfs_discmap *dm) |
| 212 | { |
| 213 | return (void *)(dm[0].dm_bh->b_data + 4); |
| 214 | } |
Russell King | 275f5b9 | 2019-06-04 14:49:36 +0100 | [diff] [blame] | 215 | |
| 216 | static inline u64 adfs_disc_size(const struct adfs_discrecord *dr) |
| 217 | { |
| 218 | return (u64)le32_to_cpu(dr->disc_size_high) << 32 | |
| 219 | le32_to_cpu(dr->disc_size); |
| 220 | } |