blob: 92cbc4b1d902d378d6c4429c91c92f5461d007cf [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#include "dir_f.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
Al Viro608ba502009-06-16 14:52:13 -040032 * adfs file system inode data in memory
33 */
34struct adfs_inode_info {
35 loff_t mmu_private;
Russell King5ed70bb2019-06-04 14:49:57 +010036 __u32 parent_id; /* parent indirect disc address */
Al Viro608ba502009-06-16 14:52:13 -040037 __u32 loadaddr; /* RISC OS load address */
38 __u32 execaddr; /* RISC OS exec address */
Al Viro608ba502009-06-16 14:52:13 -040039 unsigned int attr; /* RISC OS permissions */
Al Viro608ba502009-06-16 14:52:13 -040040 struct inode vfs_inode;
41};
42
Russell Kingb4ed8f72019-06-04 14:50:24 +010043static inline struct adfs_inode_info *ADFS_I(struct inode *inode)
44{
45 return container_of(inode, struct adfs_inode_info, vfs_inode);
46}
47
48static inline bool adfs_inode_is_stamped(struct inode *inode)
49{
50 return (ADFS_I(inode)->loadaddr & 0xfff00000) == 0xfff00000;
51}
52
Al Viro608ba502009-06-16 14:52:13 -040053/*
54 * Forward-declare this
55 */
56struct adfs_discmap;
57struct adfs_dir_ops;
58
59/*
60 * ADFS file system superblock data in memory
61 */
62struct adfs_sb_info {
Al Viro2d1d9b52013-10-03 12:37:18 -040063 union { struct {
Andrew Morton90d6cd52016-01-20 15:01:16 -080064 struct adfs_discmap *s_map; /* bh list containing map */
65 const struct adfs_dir_ops *s_dir; /* directory operations */
Al Viro2d1d9b52013-10-03 12:37:18 -040066 };
Andrew Morton90d6cd52016-01-20 15:01:16 -080067 struct rcu_head rcu; /* used only at shutdown time */
Al Viro2d1d9b52013-10-03 12:37:18 -040068 };
Andrew Morton90d6cd52016-01-20 15:01:16 -080069 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 Swalesda23ef02011-03-22 16:35:06 -070073 int s_ftsuffix; /* ,xyz hex filetype suffix option */
Al Viro608ba502009-06-16 14:52:13 -040074
Andrew Morton90d6cd52016-01-20 15:01:16 -080075 __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 Morton90d6cd52016-01-20 15:01:16 -080078 signed int s_map2blk; /* shift left by this for map->sector*/
79 unsigned int s_log2sharesize;/* log2 share size */
Al Viro608ba502009-06-16 14:52:13 -040080 unsigned int s_namelen; /* maximum number of characters in name */
81};
82
83static inline struct adfs_sb_info *ADFS_SB(struct super_block *sb)
84{
85 return sb->s_fs_info;
86}
87
Al Viro608ba502009-06-16 14:52:13 -040088/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Directory handling
90 */
91struct adfs_dir {
92 struct super_block *sb;
93
94 int nr_buffers;
95 struct buffer_head *bh[4];
Russell King71b26122019-12-09 11:09:10 +000096 struct buffer_head **bhs;
Stuart Swales2f097192011-03-22 16:35:04 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned int pos;
Russell King5ed70bb2019-06-04 14:49:57 +010099 __u32 parent_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 struct adfs_dirheader dirhead;
102 union adfs_dirtail dirtail;
103};
104
105/*
106 * This is the overall maximum name length
107 */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700108#define ADFS_MAX_NAME_LEN (256 + 4) /* +4 for ,xyz hex filetype suffix */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct object_info {
110 __u32 parent_id; /* parent object id */
Russell King5ed70bb2019-06-04 14:49:57 +0100111 __u32 indaddr; /* indirect disc addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 __u32 loadaddr; /* load address */
113 __u32 execaddr; /* execution address */
114 __u32 size; /* size */
115 __u8 attr; /* RISC OS attributes */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700116 unsigned int name_len; /* name length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 char name[ADFS_MAX_NAME_LEN];/* file name */
118};
119
120struct adfs_dir_ops {
Russell King5ed70bb2019-06-04 14:49:57 +0100121 int (*read)(struct super_block *sb, unsigned int indaddr,
122 unsigned int size, struct adfs_dir *dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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 Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130struct 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 */
138struct inode *adfs_iget(struct super_block *sb, struct object_info *obj);
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100139int adfs_write_inode(struct inode *inode, struct writeback_control *wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140int adfs_notify_change(struct dentry *dentry, struct iattr *attr);
141
142/* map.c */
Russell King5ed70bb2019-06-04 14:49:57 +0100143int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset);
Russell Kinge6160e42019-12-09 11:08:34 +0000144void adfs_map_statfs(struct super_block *sb, struct kstatfs *buf);
Russell Kingf75d3982019-12-09 11:08:28 +0000145struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr);
Russell King7b195262019-12-09 11:08:44 +0000146void adfs_free_map(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* Misc */
Joe Perches19bdd41a52014-08-08 14:22:26 -0700149__printf(3, 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150void __adfs_error(struct super_block *sb, const char *function,
151 const char *fmt, ...);
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700152#define adfs_error(sb, fmt...) __adfs_error(sb, __func__, fmt)
Russell Kingceb3b102019-06-04 14:49:52 +0100153void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/* super.c */
156
157/*
158 * Inodes and file operations
159 */
160
161/* dir_*.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800162extern const struct inode_operations adfs_dir_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800163extern const struct file_operations adfs_dir_operations;
Al Viroe16404e2009-02-20 05:55:13 +0000164extern const struct dentry_operations adfs_dentry_operations;
Julia Lawall0125f502015-11-21 16:15:37 +0100165extern const struct adfs_dir_ops adfs_f_dir_ops;
166extern const struct adfs_dir_ops adfs_fplus_dir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Russell Kinga3171202019-12-09 11:09:30 +0000168int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
169 size_t len);
170int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
171 size_t len);
Russell King1dd9f5b2019-12-09 11:09:20 +0000172void adfs_dir_relse(struct adfs_dir *dir);
Russell King411c49b2019-03-24 12:57:32 +0000173void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj);
Al Viroffdc9062009-06-08 00:44:42 -0400174extern int adfs_dir_update(struct super_block *sb, struct object_info *obj,
175 int wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* file.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800178extern const struct inode_operations adfs_file_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800179extern const struct file_operations adfs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Adrian Bunke0c93142005-08-20 17:20:28 +0100181static inline __u32 signed_asl(__u32 val, signed int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
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 King5ed70bb2019-06-04 14:49:57 +0100196static inline int __adfs_block_map(struct super_block *sb, u32 indaddr,
197 unsigned int block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Russell King5ed70bb2019-06-04 14:49:57 +0100199 if (indaddr & 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned int off;
201
Russell King5ed70bb2019-06-04 14:49:57 +0100202 off = (indaddr & 255) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 block += off << ADFS_SB(sb)->s_log2sharesize;
204 }
205
Russell King5ed70bb2019-06-04 14:49:57 +0100206 return adfs_map_lookup(sb, indaddr >> 8, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
Russell King1dfdfc92019-06-04 14:49:30 +0100208
209/* Return the disc record from the map */
210static inline
211struct adfs_discrecord *adfs_map_discrecord(struct adfs_discmap *dm)
212{
213 return (void *)(dm[0].dm_bh->b_data + 4);
214}
Russell King275f5b92019-06-04 14:49:36 +0100215
216static 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}