blob: d23c84aeb6dd9f6cf7034e2d639d139d6e59b4c2 [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];
Stuart Swales2f097192011-03-22 16:35:04 -070096
97 /* big directories need allocated buffers */
98 struct buffer_head **bh_fplus;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int pos;
Russell King5ed70bb2019-06-04 14:49:57 +0100101 __u32 parent_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 struct adfs_dirheader dirhead;
104 union adfs_dirtail dirtail;
105};
106
107/*
108 * This is the overall maximum name length
109 */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700110#define ADFS_MAX_NAME_LEN (256 + 4) /* +4 for ,xyz hex filetype suffix */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct object_info {
112 __u32 parent_id; /* parent object id */
Russell King5ed70bb2019-06-04 14:49:57 +0100113 __u32 indaddr; /* indirect disc addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 __u32 loadaddr; /* load address */
115 __u32 execaddr; /* execution address */
116 __u32 size; /* size */
117 __u8 attr; /* RISC OS attributes */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700118 unsigned int name_len; /* name length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 char name[ADFS_MAX_NAME_LEN];/* file name */
120};
121
122struct adfs_dir_ops {
Russell King5ed70bb2019-06-04 14:49:57 +0100123 int (*read)(struct super_block *sb, unsigned int indaddr,
124 unsigned int size, struct adfs_dir *dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int (*setpos)(struct adfs_dir *dir, unsigned int fpos);
126 int (*getnext)(struct adfs_dir *dir, struct object_info *obj);
127 int (*update)(struct adfs_dir *dir, struct object_info *obj);
128 int (*create)(struct adfs_dir *dir, struct object_info *obj);
129 int (*remove)(struct adfs_dir *dir, struct object_info *obj);
Al Viroffdc9062009-06-08 00:44:42 -0400130 int (*sync)(struct adfs_dir *dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 void (*free)(struct adfs_dir *dir);
132};
133
134struct adfs_discmap {
135 struct buffer_head *dm_bh;
136 __u32 dm_startblk;
137 unsigned int dm_startbit;
138 unsigned int dm_endbit;
139};
140
141/* Inode stuff */
142struct inode *adfs_iget(struct super_block *sb, struct object_info *obj);
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100143int adfs_write_inode(struct inode *inode, struct writeback_control *wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144int adfs_notify_change(struct dentry *dentry, struct iattr *attr);
145
146/* map.c */
Russell King5ed70bb2019-06-04 14:49:57 +0100147int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148extern unsigned int adfs_map_free(struct super_block *sb);
Russell Kingf75d3982019-12-09 11:08:28 +0000149struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/* Misc */
Joe Perches19bdd41a52014-08-08 14:22:26 -0700152__printf(3, 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153void __adfs_error(struct super_block *sb, const char *function,
154 const char *fmt, ...);
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700155#define adfs_error(sb, fmt...) __adfs_error(sb, __func__, fmt)
Russell Kingceb3b102019-06-04 14:49:52 +0100156void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* super.c */
159
160/*
161 * Inodes and file operations
162 */
163
164/* dir_*.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800165extern const struct inode_operations adfs_dir_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800166extern const struct file_operations adfs_dir_operations;
Al Viroe16404e2009-02-20 05:55:13 +0000167extern const struct dentry_operations adfs_dentry_operations;
Julia Lawall0125f502015-11-21 16:15:37 +0100168extern const struct adfs_dir_ops adfs_f_dir_ops;
169extern const struct adfs_dir_ops adfs_fplus_dir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Russell King411c49b2019-03-24 12:57:32 +0000171void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj);
Al Viroffdc9062009-06-08 00:44:42 -0400172extern int adfs_dir_update(struct super_block *sb, struct object_info *obj,
173 int wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* file.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800176extern const struct inode_operations adfs_file_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800177extern const struct file_operations adfs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Adrian Bunke0c93142005-08-20 17:20:28 +0100179static inline __u32 signed_asl(__u32 val, signed int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 if (shift >= 0)
182 val <<= shift;
183 else
184 val >>= -shift;
185 return val;
186}
187
188/*
189 * Calculate the address of a block in an object given the block offset
190 * and the object identity.
191 *
192 * The root directory ID should always be looked up in the map [3.4]
193 */
Russell King5ed70bb2019-06-04 14:49:57 +0100194static inline int __adfs_block_map(struct super_block *sb, u32 indaddr,
195 unsigned int block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Russell King5ed70bb2019-06-04 14:49:57 +0100197 if (indaddr & 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned int off;
199
Russell King5ed70bb2019-06-04 14:49:57 +0100200 off = (indaddr & 255) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 block += off << ADFS_SB(sb)->s_log2sharesize;
202 }
203
Russell King5ed70bb2019-06-04 14:49:57 +0100204 return adfs_map_lookup(sb, indaddr >> 8, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Russell King1dfdfc92019-06-04 14:49:30 +0100206
207/* Return the disc record from the map */
208static inline
209struct adfs_discrecord *adfs_map_discrecord(struct adfs_discmap *dm)
210{
211 return (void *)(dm[0].dm_bh->b_data + 4);
212}
Russell King275f5b92019-06-04 14:49:36 +0100213
214static inline u64 adfs_disc_size(const struct adfs_discrecord *dr)
215{
216 return (u64)le32_to_cpu(dr->disc_size_high) << 32 |
217 le32_to_cpu(dr->disc_size);
218}