blob: c05555252fecf7fefb3b7581c9ae5286e23a4224 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Russell King1dfdfc92019-06-04 14:49:30 +01002#include <linux/buffer_head.h>
Al Viro608ba502009-06-16 14:52:13 -04003#include <linux/fs.h>
4#include <linux/adfs_fs.h>
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006/* 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 Kingb4ed8f72019-06-04 14:50:24 +010012#define ADFS_FILETYPE_NONE ((u16)~0)
13
14/* RISC OS 12-bit filetype is stored in load_address[19:8] */
15static inline u16 adfs_filetype(u32 loadaddr)
16{
17 return (loadaddr & 0xfff00000) == 0xfff00000 ?
18 (loadaddr >> 8) & 0xfff : ADFS_FILETYPE_NONE;
19}
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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 Torvalds1da177e2005-04-16 15:20:36 -070029/*
Al Viro608ba502009-06-16 14:52:13 -040030 * adfs file system inode data in memory
31 */
32struct adfs_inode_info {
33 loff_t mmu_private;
Russell King5ed70bb2019-06-04 14:49:57 +010034 __u32 parent_id; /* parent indirect disc address */
Al Viro608ba502009-06-16 14:52:13 -040035 __u32 loadaddr; /* RISC OS load address */
36 __u32 execaddr; /* RISC OS exec address */
Al Viro608ba502009-06-16 14:52:13 -040037 unsigned int attr; /* RISC OS permissions */
Al Viro608ba502009-06-16 14:52:13 -040038 struct inode vfs_inode;
39};
40
Russell Kingb4ed8f72019-06-04 14:50:24 +010041static inline struct adfs_inode_info *ADFS_I(struct inode *inode)
42{
43 return container_of(inode, struct adfs_inode_info, vfs_inode);
44}
45
46static inline bool adfs_inode_is_stamped(struct inode *inode)
47{
48 return (ADFS_I(inode)->loadaddr & 0xfff00000) == 0xfff00000;
49}
50
Al Viro608ba502009-06-16 14:52:13 -040051/*
52 * Forward-declare this
53 */
54struct adfs_discmap;
55struct adfs_dir_ops;
56
57/*
58 * ADFS file system superblock data in memory
59 */
60struct adfs_sb_info {
Al Viro2d1d9b52013-10-03 12:37:18 -040061 union { struct {
Andrew Morton90d6cd52016-01-20 15:01:16 -080062 struct adfs_discmap *s_map; /* bh list containing map */
63 const struct adfs_dir_ops *s_dir; /* directory operations */
Al Viro2d1d9b52013-10-03 12:37:18 -040064 };
Andrew Morton90d6cd52016-01-20 15:01:16 -080065 struct rcu_head rcu; /* used only at shutdown time */
Al Viro2d1d9b52013-10-03 12:37:18 -040066 };
Andrew Morton90d6cd52016-01-20 15:01:16 -080067 kuid_t s_uid; /* owner uid */
68 kgid_t s_gid; /* owner gid */
69 umode_t s_owner_mask; /* ADFS owner perm -> unix perm */
70 umode_t s_other_mask; /* ADFS other perm -> unix perm */
Stuart Swalesda23ef02011-03-22 16:35:06 -070071 int s_ftsuffix; /* ,xyz hex filetype suffix option */
Al Viro608ba502009-06-16 14:52:13 -040072
Andrew Morton90d6cd52016-01-20 15:01:16 -080073 __u32 s_ids_per_zone; /* max. no ids in one zone */
74 __u32 s_idlen; /* length of ID in map */
75 __u32 s_map_size; /* sector size of a map */
Andrew Morton90d6cd52016-01-20 15:01:16 -080076 signed int s_map2blk; /* shift left by this for map->sector*/
77 unsigned int s_log2sharesize;/* log2 share size */
Al Viro608ba502009-06-16 14:52:13 -040078 unsigned int s_namelen; /* maximum number of characters in name */
79};
80
81static inline struct adfs_sb_info *ADFS_SB(struct super_block *sb)
82{
83 return sb->s_fs_info;
84}
85
Al Viro608ba502009-06-16 14:52:13 -040086/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Directory handling
88 */
89struct adfs_dir {
90 struct super_block *sb;
91
92 int nr_buffers;
93 struct buffer_head *bh[4];
Russell King71b26122019-12-09 11:09:10 +000094 struct buffer_head **bhs;
Stuart Swales2f097192011-03-22 16:35:04 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned int pos;
Russell King5ed70bb2019-06-04 14:49:57 +010097 __u32 parent_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Russell King016936b2019-12-09 11:10:21 +000099 union {
100 struct adfs_dirheader *dirhead;
101 struct adfs_bigdirheader *bighead;
102 };
103 union {
104 struct adfs_newdirtail *newtail;
105 struct adfs_bigdirtail *bigtail;
106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109/*
110 * This is the overall maximum name length
111 */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700112#define ADFS_MAX_NAME_LEN (256 + 4) /* +4 for ,xyz hex filetype suffix */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113struct object_info {
114 __u32 parent_id; /* parent object id */
Russell King5ed70bb2019-06-04 14:49:57 +0100115 __u32 indaddr; /* indirect disc addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 __u32 loadaddr; /* load address */
117 __u32 execaddr; /* execution address */
118 __u32 size; /* size */
119 __u8 attr; /* RISC OS attributes */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700120 unsigned int name_len; /* name length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 char name[ADFS_MAX_NAME_LEN];/* file name */
122};
123
124struct adfs_dir_ops {
Russell King5ed70bb2019-06-04 14:49:57 +0100125 int (*read)(struct super_block *sb, unsigned int indaddr,
126 unsigned int size, struct adfs_dir *dir);
Russell King4287e4d2019-12-09 11:10:16 +0000127 int (*iterate)(struct adfs_dir *dir, struct dir_context *ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int (*setpos)(struct adfs_dir *dir, unsigned int fpos);
129 int (*getnext)(struct adfs_dir *dir, struct object_info *obj);
130 int (*update)(struct adfs_dir *dir, struct object_info *obj);
131 int (*create)(struct adfs_dir *dir, struct object_info *obj);
132 int (*remove)(struct adfs_dir *dir, struct object_info *obj);
Russell Kingaacc9542019-12-09 11:10:47 +0000133 int (*commit)(struct adfs_dir *dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct adfs_discmap {
137 struct buffer_head *dm_bh;
138 __u32 dm_startblk;
139 unsigned int dm_startbit;
140 unsigned int dm_endbit;
141};
142
143/* Inode stuff */
144struct inode *adfs_iget(struct super_block *sb, struct object_info *obj);
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100145int adfs_write_inode(struct inode *inode, struct writeback_control *wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146int adfs_notify_change(struct dentry *dentry, struct iattr *attr);
147
148/* map.c */
Russell King5ed70bb2019-06-04 14:49:57 +0100149int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset);
Russell Kinge6160e42019-12-09 11:08:34 +0000150void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf);
Russell Kingf75d3982019-12-09 11:08:28 +0000151struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr);
Russell King7b195262019-12-09 11:08:44 +0000152void adfs_free_map(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/* Misc */
Joe Perches19bdd41a52014-08-08 14:22:26 -0700155__printf(3, 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156void __adfs_error(struct super_block *sb, const char *function,
157 const char *fmt, ...);
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700158#define adfs_error(sb, fmt...) __adfs_error(sb, __func__, fmt)
Russell Kingceb3b102019-06-04 14:49:52 +0100159void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/* super.c */
162
163/*
164 * Inodes and file operations
165 */
166
167/* dir_*.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800168extern const struct inode_operations adfs_dir_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800169extern const struct file_operations adfs_dir_operations;
Al Viroe16404e2009-02-20 05:55:13 +0000170extern const struct dentry_operations adfs_dentry_operations;
Julia Lawall0125f502015-11-21 16:15:37 +0100171extern const struct adfs_dir_ops adfs_f_dir_ops;
172extern const struct adfs_dir_ops adfs_fplus_dir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Russell Kinga3171202019-12-09 11:09:30 +0000174int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
175 size_t len);
176int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
177 size_t len);
Russell King1dd9f5b2019-12-09 11:09:20 +0000178void adfs_dir_relse(struct adfs_dir *dir);
Russell King419a6e52019-12-09 11:09:35 +0000179int adfs_dir_read_buffers(struct super_block *sb, u32 indaddr,
180 unsigned int size, struct adfs_dir *dir);
Russell King411c49b2019-03-24 12:57:32 +0000181void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj);
Al Viroffdc9062009-06-08 00:44:42 -0400182extern int adfs_dir_update(struct super_block *sb, struct object_info *obj,
183 int wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* file.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800186extern const struct inode_operations adfs_file_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800187extern const struct file_operations adfs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Adrian Bunke0c93142005-08-20 17:20:28 +0100189static inline __u32 signed_asl(__u32 val, signed int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 if (shift >= 0)
192 val <<= shift;
193 else
194 val >>= -shift;
195 return val;
196}
197
198/*
199 * Calculate the address of a block in an object given the block offset
200 * and the object identity.
201 *
202 * The root directory ID should always be looked up in the map [3.4]
203 */
Russell King5ed70bb2019-06-04 14:49:57 +0100204static inline int __adfs_block_map(struct super_block *sb, u32 indaddr,
205 unsigned int block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Russell King5ed70bb2019-06-04 14:49:57 +0100207 if (indaddr & 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int off;
209
Russell King5ed70bb2019-06-04 14:49:57 +0100210 off = (indaddr & 255) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 block += off << ADFS_SB(sb)->s_log2sharesize;
212 }
213
Russell King5ed70bb2019-06-04 14:49:57 +0100214 return adfs_map_lookup(sb, indaddr >> 8, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Russell King1dfdfc92019-06-04 14:49:30 +0100216
217/* Return the disc record from the map */
218static inline
219struct adfs_discrecord *adfs_map_discrecord(struct adfs_discmap *dm)
220{
221 return (void *)(dm[0].dm_bh->b_data + 4);
222}
Russell King275f5b92019-06-04 14:49:36 +0100223
224static inline u64 adfs_disc_size(const struct adfs_discrecord *dr)
225{
226 return (u64)le32_to_cpu(dr->disc_size_high) << 32 |
227 le32_to_cpu(dr->disc_size);
228}