summaryrefslogtreecommitdiffstats
path: root/fs/adfs/inode.c
blob: 6c8814567ab2d74a8d135ad1d7e32c386715a42b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *  linux/fs/adfs/inode.c
 *
 * Copyright (C) 1997 Russell King
 */

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/adfs_fs.h>
#include <linux/sched.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/locks.h>
#include <linux/mm.h>

/*
 * Old Inode numbers:
 *  bit 30 - 16		FragID of parent object
 *  bit 15		0			1
 *  bit 14 - 0		FragID of object	Offset into parent FragID
 *
 * New Inode numbers:
 *  Inode = Frag ID of parent (14) + Frag Offset (8) + (index into directory + 1)(8)
 */
#define inode_frag(ino)		((ino) >> 8)
#define inode_idx(ino)		((ino) & 0xff)
#define inode_dirindex(idx)	(((idx) & 0xff) * 26 - 21)

#define frag_id(x)		(((x) >> 8) & 0x7fff)
#define off(x)			(((x) & 0xff) ? (((x) & 0xff) - 1) << sb->u.adfs_sb.s_dr->log2sharesize : 0)

static inline int adfs_inode_validate_no (struct super_block *sb, unsigned int inode_no)
{
	unsigned long max_frag_id;

	max_frag_id = sb->u.adfs_sb.s_map_size * sb->u.adfs_sb.s_ids_per_zone;

	return (inode_no & 0x800000ff) ||
	       (frag_id (inode_frag (inode_no)) > max_frag_id) ||
	       (frag_id (inode_frag (inode_no)) < 2);
}

int adfs_inode_validate (struct inode *inode)
{
	struct super_block *sb = inode->i_sb;

	return adfs_inode_validate_no (sb, inode->i_ino & 0xffffff00) ||
	       adfs_inode_validate_no (sb, inode->u.adfs_i.file_id << 8);
}

unsigned long adfs_inode_generate (unsigned long parent_id, int diridx)
{
	if (!parent_id)
		return -1;

	if (diridx)
		diridx = (diridx + 21) / 26;

	return (parent_id << 8) | diridx;
}

unsigned long adfs_inode_objid (struct inode *inode)
{
	if (adfs_inode_validate (inode)) {
		adfs_error (inode->i_sb, "adfs_inode_objid",
			    "bad inode number: %lu (%X,%X)",
			    inode->i_ino, inode->i_ino, inode->u.adfs_i.file_id);
		return 0;
	}

	return inode->u.adfs_i.file_id;
}

int adfs_bmap (struct inode *inode, int block)
{
	struct super_block *sb = inode->i_sb;
	unsigned int blk;

	if (adfs_inode_validate (inode)) {
		adfs_error (sb, "adfs_bmap",
			    "bad inode number: %lu (%X,%X)",
			    inode->i_ino, inode->i_ino, inode->u.adfs_i.file_id);
		return 0;
	}

	if (block < 0) {
		adfs_error(sb, "adfs_bmap", "block(%d) < 0", block);
		return 0;
	}

	if (block > inode->i_blocks)
		return 0;

	block += off(inode->u.adfs_i.file_id);

	if (frag_id(inode->u.adfs_i.file_id) == ADFS_ROOT_FRAG)
		blk = sb->u.adfs_sb.s_map_block + block;
	else
		blk = adfs_map_lookup (sb, frag_id(inode->u.adfs_i.file_id), block);
	return blk;
}

unsigned int adfs_parent_bmap (struct inode *inode, int block)
{
	struct super_block *sb = inode->i_sb;
	unsigned int blk, fragment;

	if (adfs_inode_validate_no (sb, inode->i_ino & 0xffffff00)) {
		adfs_error (sb, "adfs_parent_bmap",
			    "bad inode number: %lu (%X,%X)",
			    inode->i_ino, inode->i_ino, inode->u.adfs_i.file_id);
		return 0;
	}

	fragment = inode_frag (inode->i_ino);
	if (frag_id (fragment) == ADFS_ROOT_FRAG)
		blk = sb->u.adfs_sb.s_map_block + off(fragment) + block;
	else
		blk = adfs_map_lookup (sb, frag_id (fragment), off(fragment) + block);
	return blk;	
}

static int adfs_atts2mode(struct super_block *sb, unsigned char mode, unsigned int filetype)
{
	int omode = 0;

	if (filetype == 0xfc0 /* LinkFS */) {
		omode = S_IFLNK|S_IRUSR|S_IWUSR|S_IXUSR|
				S_IRGRP|S_IWGRP|S_IXGRP|
				S_IROTH|S_IWOTH|S_IXOTH;
	} else {
		if (mode & ADFS_NDA_DIRECTORY) {
			omode |= S_IRUGO & sb->u.adfs_sb.s_owner_mask;
			omode |= S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH;
		} else
			omode |= S_IFREG;

		if (mode & ADFS_NDA_OWNER_READ) {
			omode |= S_IRUGO & sb->u.adfs_sb.s_owner_mask;
			if (filetype == 0xfe6 /* UnixExec */)
				omode |= S_IXUGO & sb->u.adfs_sb.s_owner_mask;
		}

		if (mode & ADFS_NDA_OWNER_WRITE)
			omode |= S_IWUGO & sb->u.adfs_sb.s_owner_mask;

		if (mode & ADFS_NDA_PUBLIC_READ) {
			omode |= S_IRUGO & sb->u.adfs_sb.s_other_mask;
			if (filetype == 0xfe6 /* UnixExec */)
				omode |= S_IXUGO & sb->u.adfs_sb.s_other_mask;
		}

		if (mode & ADFS_NDA_PUBLIC_WRITE)
			omode |= S_IWUGO & sb->u.adfs_sb.s_other_mask;
	}
	return omode;
}

void adfs_read_inode (struct inode *inode)
{
	struct super_block *sb;
	struct buffer_head *bh[4];
	struct adfs_idir_entry ide;
	int buffers;

	sb = inode->i_sb;
	inode->i_uid = sb->u.adfs_sb.s_uid;
	inode->i_gid = sb->u.adfs_sb.s_gid;
	inode->i_version = ++event;

	if (adfs_inode_validate_no (sb, inode->i_ino & 0xffffff00)) {
		adfs_error (sb, "adfs_read_inode",
			    "bad inode number: %lu", inode->i_ino);
		goto bad;
	}

	if (frag_id(inode_frag (inode->i_ino)) == ADFS_ROOT_FRAG &&
	    inode_idx (inode->i_ino) == 0) {
		/* root dir */
		inode->i_mode	 = S_IRWXUGO | S_IFDIR;
		inode->i_nlink	 = 2;
		inode->i_size	 = ADFS_NEWDIR_SIZE;
		inode->i_blksize = PAGE_SIZE;
		inode->i_blocks  = inode->i_size / sb->s_blocksize;
		inode->i_mtime   =
		inode->i_atime   =
		inode->i_ctime   = 0;
		inode->u.adfs_i.file_id = inode_frag (inode->i_ino);
	} else {
		if (!(buffers = adfs_dir_read_parent (inode, bh)))
			goto bad;

		if (adfs_dir_check (inode, bh, buffers, NULL)) {
			adfs_dir_free (bh, buffers);
			goto bad;
		}

		if (!adfs_dir_find_entry (sb, bh, buffers, inode_dirindex (inode->i_ino), &ide)) {
			adfs_dir_free (bh, buffers);
			goto bad;
		}
		adfs_dir_free (bh, buffers);
		inode->i_mode	 = adfs_atts2mode(sb, ide.mode, ide.filetype);
		inode->i_nlink	 = 2;
		inode->i_size    = ide.size;
		inode->i_blksize = PAGE_SIZE;
		inode->i_blocks	 = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
		inode->i_mtime	 =
		inode->i_atime   =
		inode->i_ctime   = ide.mtime;
		inode->u.adfs_i.file_id = ide.file_id;
	}

	if (S_ISDIR(inode->i_mode))
		inode->i_op	 = &adfs_dir_inode_operations;
	else if (S_ISREG(inode->i_mode))
		inode->i_op	 = &adfs_file_inode_operations;
	return;

bad:
	make_bad_inode(inode);
}