~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/efs/inode.c

Version: ~ [ linux-6.0-rc1 ] ~ [ linux-5.19.1 ] ~ [ linux-5.18.17 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.60 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.136 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.210 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.255 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.290 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.325 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp