summaryrefslogtreecommitdiffstats
path: root/fs/efs/inode.c
blob: 81c8e81c786055a591aec04b076cf85620475c4a (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * inode.c
 *
 * Copyright (c) 1999 Al Smith
 *
 * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
 *              and from work (c) 1998 Mike Shaver.
 */

#include <linux/efs_fs.h>
#include <linux/efs_fs_sb.h>

static inline void extent_copy(efs_extent *src, efs_extent *dst) {
	/*
	 * this is slightly evil. it doesn't just copy the
	 * efs_extent from src to dst, it also mangles the bits
	 * so that dst ends up in cpu byte-order.
	 */

	dst->cooked.ex_magic  =  (unsigned int) src->raw[0];
	dst->cooked.ex_bn     = ((unsigned int) src->raw[1] << 16) |
				((unsigned int) src->raw[2] <<  8) |
				((unsigned int) src->raw[3] <<  0);
	dst->cooked.ex_length =  (unsigned int) src->raw[4];
	dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) |
				((unsigned int) src->raw[6] <<  8) |
				((unsigned int) src->raw[7] <<  0);
	return;
}

void efs_read_inode(struct inode *in) {
	int i, extents, inode_index;
	dev_t device;
	struct buffer_head *bh;
	struct efs_sb_info *sbp = (struct efs_sb_info *)&in->i_sb->u.generic_sbp;
	struct efs_inode_info *ini = (struct efs_inode_info *)&in->u.generic_ip;
	efs_block_t block, offset;
	struct efs_dinode *efs_inode;
  
	/*
	** EFS layout:
	**
	** |   cylinder group    |   cylinder group    |   cylinder group ..etc
	** |inodes|data          |inodes|data          |inodes|data       ..etc
	**
	** work out the inode block index, (considering initially that the
	** inodes are stored as consecutive blocks). then work out the block
	** number of that inode given the above layout, and finally the
	** offset of the inode within that block.
	*/

	/* four inodes are stored in one block */
	inode_index = in->i_ino / (EFS_BLOCKSIZE / sizeof(struct efs_dinode));

	block = sbp->fs_start + sbp->first_block + 
		(sbp->group_size * (inode_index / sbp->inode_blocks)) +
		(inode_index % sbp->inode_blocks);

	offset = (in->i_ino % (EFS_BLOCKSIZE / sizeof(struct efs_dinode))) << 7;

	bh = bread(in->i_dev, block, EFS_BLOCKSIZE);
	if (!bh) {
		printk("EFS: bread() failed at block %d\n", block);
		goto read_inode_error;
	}

	efs_inode = (struct efs_dinode *) (bh->b_data + offset);
    
	/* fill in standard inode infos */
	in->i_mode  = be16_to_cpu(efs_inode->di_mode);
	in->i_nlink = be16_to_cpu(efs_inode->di_nlink);
	in->i_uid   = be16_to_cpu(efs_inode->di_uid);
	in->i_gid   = be16_to_cpu(efs_inode->di_gid);
	in->i_size  = be32_to_cpu(efs_inode->di_size);
	in->i_atime = be32_to_cpu(efs_inode->di_atime);
	in->i_mtime = be32_to_cpu(efs_inode->di_mtime);
	in->i_ctime = be32_to_cpu(efs_inode->di_ctime);

	/* this is last valid block in the file */
	in->i_blocks = ((in->i_size - 1) >> EFS_BLOCKSIZE_BITS);

    	device = be32_to_cpu(efs_inode->di_u.di_dev);

	/* The following values are stored in my private part of the Inode.
	   They are necessary for further operations with the file */

	/* get the number of extents for this object */
	extents = be16_to_cpu(efs_inode->di_numextents);

	/* copy the first 12 extents directly from the inode */
	for(i = 0; i < EFS_DIRECTEXTENTS; i++) {
		extent_copy(&(efs_inode->di_u.di_extents[i]), &(ini->extents[i]));
		if (i < extents && ini->extents[i].cooked.ex_magic != 0) {
			printk("EFS: extent %d has bad magic number in inode %lu\n", i, in->i_ino);
			brelse(bh);
			goto read_inode_error;
		}
	}
	ini->numextents = extents;
	ini->lastextent = 0;

	brelse(bh);
   
#ifdef DEBUG
	printk("EFS: efs_read_inode(): inode %lu, extents %d\n",
		in->i_ino, extents);
#endif

	/* Install the filetype Handler */
	switch (in->i_mode & S_IFMT) {
		case S_IFDIR: 
			in->i_op = &efs_dir_inode_operations; 
			break;
		case S_IFREG:
			in->i_op = &efs_file_inode_operations;
			break;
		case S_IFLNK:
			in->i_op = &efs_symlink_inode_operations;
			break;
		case S_IFCHR:
			in->i_rdev = device;
			in->i_op = &chrdev_inode_operations; 
			break;
		case S_IFBLK:
			in->i_rdev = device;
			in->i_op = &blkdev_inode_operations; 
			break;
		case S_IFIFO:
			init_fifo(in);
			break;    
		default:
			printk("EFS: unsupported inode mode %o\n",in->i_mode);
			goto read_inode_error;
			break;
	}

	return;
        
read_inode_error:
	printk("EFS: failed to read inode %lu\n", in->i_ino);
	in->i_mode = S_IFREG;
	in->i_atime = 0;
	in->i_ctime = 0;
	in->i_mtime = 0;
	in->i_nlink = 1;
	in->i_size = 0;
	in->i_blocks = 0;
	in->i_uid = 0;
	in->i_gid = 0;
	in->i_op = NULL;

	return;
}

static inline efs_block_t
efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sbp, struct efs_inode_info *ini) {
	efs_block_t start;
	efs_block_t length;
	efs_block_t offset;

	/*
	** given an extent and a logical block within a file,
	** can this block be found within this extent ?
	*/
	start  = ptr->cooked.ex_bn;
	length = ptr->cooked.ex_length;
	offset = ptr->cooked.ex_offset;

	if ((block >= offset) && (block < offset+length)) {
		return(sbp->fs_start + start + block - offset);
	} else {
		return 0;
	}
}

/* find the disk block number for a given logical file block number */

efs_block_t efs_read_block(struct inode *inode, efs_block_t block) {
	struct efs_sb_info *sbp = (struct efs_sb_info *) &inode->i_sb->u.generic_sbp;
	struct efs_inode_info *ini = (struct efs_inode_info *) &inode->u.generic_ip;
	struct buffer_head *bh;

	efs_block_t result = 0;
	int indirexts, indirext, imagic;
	efs_block_t istart, iblock, ilen;
	int i, cur, last, total, checked;
	efs_extent *exts;
	efs_extent ext;

	last  = ini->lastextent;
	total = ini->numextents;

	if (total <= EFS_DIRECTEXTENTS) {
		/* first check the last extent we returned */
		if ((result = efs_extent_check(&ini->extents[last], block, sbp, ini)))
			return result;
    
		/* if we only have one extent then nothing can be found */
		if (total == 1) {
			printk("EFS: read_block() failed to map (1 extent)\n");
			return 0;
		}

		/* check the stored extents in the inode */
		/* start with next extent and check forwards */
		for(i = 1; i < total; i++) {
			if ((result = efs_extent_check(&ini->extents[(last + i) % total], block, sbp, ini))) {
				ini->lastextent = i;
				return result;
			}
		}

		printk("EFS: read_block() failed to map for direct extents\n");
		return 0;
	}

#ifdef DEBUG
	printk("EFS: indirect search for logical block %u\n", block);
#endif

	/*
	 * OPT: this lot is inefficient.
	 */

	indirexts = ini->extents[0].cooked.ex_offset;
	checked = 0;

	for(indirext = 0; indirext < indirexts; indirext++) {
		cur = (last + indirext) % indirexts;
		imagic = ini->extents[cur].cooked.ex_magic;
		istart = ini->extents[cur].cooked.ex_bn + sbp->fs_start;
		ilen   = ini->extents[cur].cooked.ex_length;

		for(iblock = istart; iblock < istart + ilen; iblock++) {
			bh = bread(inode->i_dev, iblock, EFS_BLOCKSIZE);
			if (!bh) {
				printk("EFS: bread() failed at block %d\n", block);
				return 0;
			}
#ifdef DEBUG
			printk("EFS: read indirect extent block %d\n", iblock-istart);
#endif

			exts = (efs_extent *) bh->b_data;
			for(i = 0; i < EFS_BLOCKSIZE / sizeof(efs_extent) && checked < total; i++, checked++) {
				/*
				 * this block might be read-cached
				 * so fiddle the endianness in a private copy
				 */
				extent_copy(&(exts[i]), &ext);

				if (ext.cooked.ex_magic != 0) {
					printk("EFS: extent %d has bad magic number in block %d\n", i, iblock);
					brelse(bh);
					return 0;
				}

				if ((result = efs_extent_check(&ext, block, sbp, ini))) {
					brelse(bh);
					ini->lastextent = cur;
					return result;
				}
			}
			brelse(bh);
			/* shouldn't need this if the FS is consistent */
			if (checked == total) {
				printk("EFS: unable to map (checked all extents)\n");
				return 0;
			}
		}
	}
	printk("EFS: unable to map (fell out of loop)\n");
	return 0;
}