blob: 9eb9bea1cef2d7e56089bc8fa5bdc13898d185ea [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
12#define ADFS_NDA_OWNER_READ (1 << 0)
13#define ADFS_NDA_OWNER_WRITE (1 << 1)
14#define ADFS_NDA_LOCKED (1 << 2)
15#define ADFS_NDA_DIRECTORY (1 << 3)
16#define ADFS_NDA_EXECUTE (1 << 4)
17#define ADFS_NDA_PUBLIC_READ (1 << 5)
18#define ADFS_NDA_PUBLIC_WRITE (1 << 6)
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "dir_f.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
Al Viro608ba502009-06-16 14:52:13 -040023 * adfs file system inode data in memory
24 */
25struct adfs_inode_info {
26 loff_t mmu_private;
Russell King5ed70bb2019-06-04 14:49:57 +010027 __u32 parent_id; /* parent indirect disc address */
Al Viro608ba502009-06-16 14:52:13 -040028 __u32 loadaddr; /* RISC OS load address */
29 __u32 execaddr; /* RISC OS exec address */
30 unsigned int filetype; /* RISC OS file type */
31 unsigned int attr; /* RISC OS permissions */
32 unsigned int stamped:1; /* RISC OS file has date/time */
33 struct inode vfs_inode;
34};
35
36/*
37 * Forward-declare this
38 */
39struct adfs_discmap;
40struct adfs_dir_ops;
41
42/*
43 * ADFS file system superblock data in memory
44 */
45struct adfs_sb_info {
Al Viro2d1d9b52013-10-03 12:37:18 -040046 union { struct {
Andrew Morton90d6cd52016-01-20 15:01:16 -080047 struct adfs_discmap *s_map; /* bh list containing map */
48 const struct adfs_dir_ops *s_dir; /* directory operations */
Al Viro2d1d9b52013-10-03 12:37:18 -040049 };
Andrew Morton90d6cd52016-01-20 15:01:16 -080050 struct rcu_head rcu; /* used only at shutdown time */
Al Viro2d1d9b52013-10-03 12:37:18 -040051 };
Andrew Morton90d6cd52016-01-20 15:01:16 -080052 kuid_t s_uid; /* owner uid */
53 kgid_t s_gid; /* owner gid */
54 umode_t s_owner_mask; /* ADFS owner perm -> unix perm */
55 umode_t s_other_mask; /* ADFS other perm -> unix perm */
Stuart Swalesda23ef02011-03-22 16:35:06 -070056 int s_ftsuffix; /* ,xyz hex filetype suffix option */
Al Viro608ba502009-06-16 14:52:13 -040057
Andrew Morton90d6cd52016-01-20 15:01:16 -080058 __u32 s_ids_per_zone; /* max. no ids in one zone */
59 __u32 s_idlen; /* length of ID in map */
60 __u32 s_map_size; /* sector size of a map */
Andrew Morton90d6cd52016-01-20 15:01:16 -080061 signed int s_map2blk; /* shift left by this for map->sector*/
62 unsigned int s_log2sharesize;/* log2 share size */
Al Viro608ba502009-06-16 14:52:13 -040063 unsigned int s_namelen; /* maximum number of characters in name */
64};
65
66static inline struct adfs_sb_info *ADFS_SB(struct super_block *sb)
67{
68 return sb->s_fs_info;
69}
70
71static inline struct adfs_inode_info *ADFS_I(struct inode *inode)
72{
73 return container_of(inode, struct adfs_inode_info, vfs_inode);
74}
75
76/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * Directory handling
78 */
79struct adfs_dir {
80 struct super_block *sb;
81
82 int nr_buffers;
83 struct buffer_head *bh[4];
Stuart Swales2f097192011-03-22 16:35:04 -070084
85 /* big directories need allocated buffers */
86 struct buffer_head **bh_fplus;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned int pos;
Russell King5ed70bb2019-06-04 14:49:57 +010089 __u32 parent_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 struct adfs_dirheader dirhead;
92 union adfs_dirtail dirtail;
93};
94
95/*
96 * This is the overall maximum name length
97 */
Stuart Swalesda23ef02011-03-22 16:35:06 -070098#define ADFS_MAX_NAME_LEN (256 + 4) /* +4 for ,xyz hex filetype suffix */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct object_info {
100 __u32 parent_id; /* parent object id */
Russell King5ed70bb2019-06-04 14:49:57 +0100101 __u32 indaddr; /* indirect disc addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 __u32 loadaddr; /* load address */
103 __u32 execaddr; /* execution address */
104 __u32 size; /* size */
105 __u8 attr; /* RISC OS attributes */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700106 unsigned int name_len; /* name length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 char name[ADFS_MAX_NAME_LEN];/* file name */
Stuart Swalesda23ef02011-03-22 16:35:06 -0700108
109 /* RISC OS file type (12-bit: derived from loadaddr) */
110 __u16 filetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct adfs_dir_ops {
Russell King5ed70bb2019-06-04 14:49:57 +0100114 int (*read)(struct super_block *sb, unsigned int indaddr,
115 unsigned int size, struct adfs_dir *dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int (*setpos)(struct adfs_dir *dir, unsigned int fpos);
117 int (*getnext)(struct adfs_dir *dir, struct object_info *obj);
118 int (*update)(struct adfs_dir *dir, struct object_info *obj);
119 int (*create)(struct adfs_dir *dir, struct object_info *obj);
120 int (*remove)(struct adfs_dir *dir, struct object_info *obj);
Al Viroffdc9062009-06-08 00:44:42 -0400121 int (*sync)(struct adfs_dir *dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 void (*free)(struct adfs_dir *dir);
123};
124
125struct adfs_discmap {
126 struct buffer_head *dm_bh;
127 __u32 dm_startblk;
128 unsigned int dm_startbit;
129 unsigned int dm_endbit;
130};
131
132/* Inode stuff */
133struct inode *adfs_iget(struct super_block *sb, struct object_info *obj);
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100134int adfs_write_inode(struct inode *inode, struct writeback_control *wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135int adfs_notify_change(struct dentry *dentry, struct iattr *attr);
136
137/* map.c */
Russell King5ed70bb2019-06-04 14:49:57 +0100138int adfs_map_lookup(struct super_block *sb, u32 frag_id, unsigned int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139extern unsigned int adfs_map_free(struct super_block *sb);
140
141/* Misc */
Joe Perches19bdd41a52014-08-08 14:22:26 -0700142__printf(3, 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143void __adfs_error(struct super_block *sb, const char *function,
144 const char *fmt, ...);
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700145#define adfs_error(sb, fmt...) __adfs_error(sb, __func__, fmt)
Russell Kingceb3b102019-06-04 14:49:52 +0100146void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* super.c */
149
150/*
151 * Inodes and file operations
152 */
153
154/* dir_*.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800155extern const struct inode_operations adfs_dir_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800156extern const struct file_operations adfs_dir_operations;
Al Viroe16404e2009-02-20 05:55:13 +0000157extern const struct dentry_operations adfs_dentry_operations;
Julia Lawall0125f502015-11-21 16:15:37 +0100158extern const struct adfs_dir_ops adfs_f_dir_ops;
159extern const struct adfs_dir_ops adfs_fplus_dir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Russell King411c49b2019-03-24 12:57:32 +0000161void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj);
Al Viroffdc9062009-06-08 00:44:42 -0400162extern int adfs_dir_update(struct super_block *sb, struct object_info *obj,
163 int wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* file.c */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800166extern const struct inode_operations adfs_file_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800167extern const struct file_operations adfs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Adrian Bunke0c93142005-08-20 17:20:28 +0100169static inline __u32 signed_asl(__u32 val, signed int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (shift >= 0)
172 val <<= shift;
173 else
174 val >>= -shift;
175 return val;
176}
177
178/*
179 * Calculate the address of a block in an object given the block offset
180 * and the object identity.
181 *
182 * The root directory ID should always be looked up in the map [3.4]
183 */
Russell King5ed70bb2019-06-04 14:49:57 +0100184static inline int __adfs_block_map(struct super_block *sb, u32 indaddr,
185 unsigned int block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Russell King5ed70bb2019-06-04 14:49:57 +0100187 if (indaddr & 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned int off;
189
Russell King5ed70bb2019-06-04 14:49:57 +0100190 off = (indaddr & 255) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 block += off << ADFS_SB(sb)->s_log2sharesize;
192 }
193
Russell King5ed70bb2019-06-04 14:49:57 +0100194 return adfs_map_lookup(sb, indaddr >> 8, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Russell King1dfdfc92019-06-04 14:49:30 +0100196
197/* Return the disc record from the map */
198static inline
199struct adfs_discrecord *adfs_map_discrecord(struct adfs_discmap *dm)
200{
201 return (void *)(dm[0].dm_bh->b_data + 4);
202}
Russell King275f5b92019-06-04 14:49:36 +0100203
204static inline u64 adfs_disc_size(const struct adfs_discrecord *dr)
205{
206 return (u64)le32_to_cpu(dr->disc_size_high) << 32 |
207 le32_to_cpu(dr->disc_size);
208}