File: /usr/src/linux/fs/efs/inode.c

1     /*
2      * inode.c
3      *
4      * Copyright (c) 1999 Al Smith
5      *
6      * Portions derived from work (c) 1995,1996 Christian Vogelgsang,
7      *              and from work (c) 1998 Mike Shaver.
8      */
9     
10     #include <linux/efs_fs.h>
11     #include <linux/efs_fs_sb.h>
12     
13     extern int efs_get_block(struct inode *, long, struct buffer_head *, int);
14     static int efs_readpage(struct file *file, struct page *page)
15     {
16     	return block_read_full_page(page,efs_get_block);
17     }
18     static int _efs_bmap(struct address_space *mapping, long block)
19     {
20     	return generic_block_bmap(mapping,block,efs_get_block);
21     }
22     struct address_space_operations efs_aops = {
23     	readpage: efs_readpage,
24     	sync_page: block_sync_page,
25     	bmap: _efs_bmap
26     };
27     
28     static inline void extent_copy(efs_extent *src, efs_extent *dst) {
29     	/*
30     	 * this is slightly evil. it doesn't just copy
31     	 * efs_extent from src to dst, it also mangles
32     	 * the bits so that dst ends up in cpu byte-order.
33     	 */
34     
35     	dst->cooked.ex_magic  =  (unsigned int) src->raw[0];
36     	dst->cooked.ex_bn     = ((unsigned int) src->raw[1] << 16) |
37     				((unsigned int) src->raw[2] <<  8) |
38     				((unsigned int) src->raw[3] <<  0);
39     	dst->cooked.ex_length =  (unsigned int) src->raw[4];
40     	dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) |
41     				((unsigned int) src->raw[6] <<  8) |
42     				((unsigned int) src->raw[7] <<  0);
43     	return;
44     }
45     
46     void efs_read_inode(struct inode *inode) {
47     	int i, inode_index;
48     	dev_t device;
49     	struct buffer_head *bh;
50     	struct efs_sb_info    *sb = SUPER_INFO(inode->i_sb);
51     	struct efs_inode_info *in = INODE_INFO(inode);
52     	efs_block_t block, offset;
53     	struct efs_dinode *efs_inode;
54       
55     	/*
56     	** EFS layout:
57     	**
58     	** |   cylinder group    |   cylinder group    |   cylinder group ..etc
59     	** |inodes|data          |inodes|data          |inodes|data       ..etc
60     	**
61     	** work out the inode block index, (considering initially that the
62     	** inodes are stored as consecutive blocks). then work out the block
63     	** number of that inode given the above layout, and finally the
64     	** offset of the inode within that block.
65     	*/
66     
67     	inode_index = inode->i_ino /
68     		(EFS_BLOCKSIZE / sizeof(struct efs_dinode));
69     
70     	block = sb->fs_start + sb->first_block + 
71     		(sb->group_size * (inode_index / sb->inode_blocks)) +
72     		(inode_index % sb->inode_blocks);
73     
74     	offset = (inode->i_ino %
75     			(EFS_BLOCKSIZE / sizeof(struct efs_dinode))) *
76     		sizeof(struct efs_dinode);
77     
78     	bh = bread(inode->i_dev, block, EFS_BLOCKSIZE);
79     	if (!bh) {
80     		printk(KERN_WARNING "EFS: bread() failed at block %d\n", block);
81     		goto read_inode_error;
82     	}
83     
84     	efs_inode = (struct efs_dinode *) (bh->b_data + offset);
85         
86     	inode->i_mode  = be16_to_cpu(efs_inode->di_mode);
87     	inode->i_nlink = be16_to_cpu(efs_inode->di_nlink);
88     	inode->i_uid   = (uid_t)be16_to_cpu(efs_inode->di_uid);
89     	inode->i_gid   = (gid_t)be16_to_cpu(efs_inode->di_gid);
90     	inode->i_size  = be32_to_cpu(efs_inode->di_size);
91     	inode->i_atime = be32_to_cpu(efs_inode->di_atime);
92     	inode->i_mtime = be32_to_cpu(efs_inode->di_mtime);
93     	inode->i_ctime = be32_to_cpu(efs_inode->di_ctime);
94     
95     	/* this is the number of blocks in the file */
96     	if (inode->i_size == 0) {
97     		inode->i_blocks = 0;
98     	} else {
99     		inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1;
100     	}
101     
102     	/*
103     	 * BUG: irix dev_t is 32-bits. linux dev_t is only 16-bits.
104     	 *
105     	 * apparently linux will change to 32-bit dev_t sometime during
106     	 * linux 2.3.
107     	 *
108     	 * as is, this code maps devices that can't be represented in
109     	 * 16-bits (ie major > 255 or minor > 255) to major = minor = 255.
110     	 *
111     	 * during 2.3 when 32-bit dev_t become available, we should test
112     	 * to see whether odev contains 65535. if this is the case then we
113     	 * should then do device = be32_to_cpu(efs_inode->di_u.di_dev.ndev).
114     	 */
115         	device = be16_to_cpu(efs_inode->di_u.di_dev.odev);
116     
117     	/* get the number of extents for this object */
118     	in->numextents = be16_to_cpu(efs_inode->di_numextents);
119     	in->lastextent = 0;
120     
121     	/* copy the extents contained within the inode to memory */
122     	for(i = 0; i < EFS_DIRECTEXTENTS; i++) {
123     		extent_copy(&(efs_inode->di_u.di_extents[i]), &(in->extents[i]));
124     		if (i < in->numextents && in->extents[i].cooked.ex_magic != 0) {
125     			printk(KERN_WARNING "EFS: extent %d has bad magic number in inode %lu\n", i, inode->i_ino);
126     			brelse(bh);
127     			goto read_inode_error;
128     		}
129     	}
130     
131     	brelse(bh);
132        
133     #ifdef DEBUG
134     	printk(KERN_DEBUG "EFS: read_inode(): inode %lu, extents %d, mode %o\n",
135     		inode->i_ino, in->numextents, inode->i_mode);
136     #endif
137     
138     	switch (inode->i_mode & S_IFMT) {
139     		case S_IFDIR: 
140     			inode->i_op = &efs_dir_inode_operations; 
141     			inode->i_fop = &efs_dir_operations; 
142     			break;
143     		case S_IFREG:
144     			inode->i_fop = &generic_ro_fops;
145     			inode->i_data.a_ops = &efs_aops;
146     			break;
147     		case S_IFLNK:
148     			inode->i_op = &page_symlink_inode_operations;
149     			inode->i_data.a_ops = &efs_symlink_aops;
150     			break;
151     		case S_IFCHR:
152     		case S_IFBLK:
153     		case S_IFIFO:
154     			init_special_inode(inode, inode->i_mode, device);
155     			break;
156     		default:
157     			printk(KERN_WARNING "EFS: unsupported inode mode %o\n", inode->i_mode);
158     			goto read_inode_error;
159     			break;
160     	}
161     
162     	return;
163             
164     read_inode_error:
165     	printk(KERN_WARNING "EFS: failed to read inode %lu\n", inode->i_ino);
166     	make_bad_inode(inode);
167     
168     	return;
169     }
170     
171     static inline efs_block_t
172     efs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) {
173     	efs_block_t start;
174     	efs_block_t length;
175     	efs_block_t offset;
176     
177     	/*
178     	 * given an extent and a logical block within a file,
179     	 * can this block be found within this extent ?
180     	 */
181     	start  = ptr->cooked.ex_bn;
182     	length = ptr->cooked.ex_length;
183     	offset = ptr->cooked.ex_offset;
184     
185     	if ((block >= offset) && (block < offset+length)) {
186     		return(sb->fs_start + start + block - offset);
187     	} else {
188     		return 0;
189     	}
190     }
191     
192     efs_block_t efs_map_block(struct inode *inode, efs_block_t block) {
193     	struct efs_sb_info    *sb = SUPER_INFO(inode->i_sb);
194     	struct efs_inode_info *in = INODE_INFO(inode);
195     	struct buffer_head    *bh = NULL;
196     
197     	int cur, last, first = 1;
198     	int ibase, ioffset, dirext, direxts, indext, indexts;
199     	efs_block_t iblock, result = 0, lastblock = 0;
200     	efs_extent ext, *exts;
201     
202     	last = in->lastextent;
203     
204     	if (in->numextents <= EFS_DIRECTEXTENTS) {
205     		/* first check the last extent we returned */
206     		if ((result = efs_extent_check(&in->extents[last], block, sb)))
207     			return result;
208         
209     		/* if we only have one extent then nothing can be found */
210     		if (in->numextents == 1) {
211     			printk(KERN_ERR "EFS: map_block() failed to map (1 extent)\n");
212     			return 0;
213     		}
214     
215     		direxts = in->numextents;
216     
217     		/*
218     		 * check the stored extents in the inode
219     		 * start with next extent and check forwards
220     		 */
221     		for(dirext = 1; dirext < direxts; dirext++) {
222     			cur = (last + dirext) % in->numextents;
223     			if ((result = efs_extent_check(&in->extents[cur], block, sb))) {
224     				in->lastextent = cur;
225     				return result;
226     			}
227     		}
228     
229     		printk(KERN_ERR "EFS: map_block() failed to map block %u (dir)\n", block);
230     		return 0;
231     	}
232     
233     #ifdef DEBUG
234     	printk(KERN_DEBUG "EFS: map_block(): indirect search for logical block %u\n", block);
235     #endif
236     	direxts = in->extents[0].cooked.ex_offset;
237     	indexts = in->numextents;
238     
239     	for(indext = 0; indext < indexts; indext++) {
240     		cur = (last + indext) % indexts;
241     
242     		/*
243     		 * work out which direct extent contains `cur'.
244     		 *
245     		 * also compute ibase: i.e. the number of the first
246     		 * indirect extent contained within direct extent `cur'.
247     		 *
248     		 */
249     		ibase = 0;
250     		for(dirext = 0; cur < ibase && dirext < direxts; dirext++) {
251     			ibase += in->extents[dirext].cooked.ex_length *
252     				(EFS_BLOCKSIZE / sizeof(efs_extent));
253     		}
254     
255     		if (dirext == direxts) {
256     			/* should never happen */
257     			printk(KERN_ERR "EFS: couldn't find direct extent for indirect extent %d (block %u)\n", cur, block);
258     			if (bh) brelse(bh);
259     			return 0;
260     		}
261     		
262     		/* work out block number and offset of this indirect extent */
263     		iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn +
264     			(cur - ibase) /
265     			(EFS_BLOCKSIZE / sizeof(efs_extent));
266     		ioffset = (cur - ibase) %
267     			(EFS_BLOCKSIZE / sizeof(efs_extent));
268     
269     		if (first || lastblock != iblock) {
270     			if (bh) brelse(bh);
271     
272     			bh = bread(inode->i_dev, iblock, EFS_BLOCKSIZE);
273     			if (!bh) {
274     				printk(KERN_ERR "EFS: bread() failed at block %d\n", iblock);
275     				return 0;
276     			}
277     #ifdef DEBUG
278     			printk(KERN_DEBUG "EFS: map_block(): read indirect extent block %d\n", iblock);
279     #endif
280     			first = 0;
281     			lastblock = iblock;
282     		}
283     
284     		exts = (efs_extent *) bh->b_data;
285     
286     		extent_copy(&(exts[ioffset]), &ext);
287     
288     		if (ext.cooked.ex_magic != 0) {
289     			printk(KERN_ERR "EFS: extent %d has bad magic number in block %d\n", cur, iblock);
290     			if (bh) brelse(bh);
291     			return 0;
292     		}
293     
294     		if ((result = efs_extent_check(&ext, block, sb))) {
295     			if (bh) brelse(bh);
296     			in->lastextent = cur;
297     			return result;
298     		}
299     	}
300     	if (bh) brelse(bh);
301     	printk(KERN_ERR "EFS: map_block() failed to map block %u (indir)\n", block);
302     	return 0;
303     }  
304     
305