inode.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,432 行 · 第 1/3 页
C
1,432 行
#ifdef DEBUG printk("isofs_bmap: mapped inode:block %x:%d to block %lu\n", inode->i_ino, block, (b_off - offset + firstext) >> ISOFS_BUFFER_BITS(inode));#endif return (b_off - offset + firstext) >> ISOFS_BUFFER_BITS(inode);}static void test_and_set_uid(uid_t *p, uid_t value){ if(value) { *p = value;#if 0 printk("Resetting to %d\n", value);#endif }}static int isofs_read_level3_size(struct inode * inode){ unsigned long ino = inode->i_ino; unsigned long bufsize = ISOFS_BUFFER_SIZE(inode); int high_sierra = inode->i_sb->u.isofs_sb.s_high_sierra; struct buffer_head * bh = NULL; int block = 0; int i = 0; void *cpnt; struct iso_directory_record * raw_inode; inode->i_size = 0; inode->u.isofs_i.i_next_section_ino = 0; do { unsigned char *pnt; unsigned int reclen; int offset = (ino & (bufsize - 1)); cpnt = NULL; /* Check whether to update our buffer */ if (block != ino >> ISOFS_BUFFER_BITS(inode)) { block = ino >> ISOFS_BUFFER_BITS(inode); brelse(bh); bh = bread(inode->i_dev, block, bufsize); if (!bh) goto out_noread; } pnt = ((unsigned char *) bh->b_data + offset); raw_inode = ((struct iso_directory_record *) pnt); /* * Note: this is invariant even if the record * spans buffers and must be copied ... */ reclen = *pnt; /* N.B. this test doesn't trigger the i++ code ... */ if(reclen == 0) { ino = (ino & ~(ISOFS_BLOCK_SIZE - 1)) + ISOFS_BLOCK_SIZE; continue; } /* Check whether the raw inode spans the buffer ... */ if (offset + reclen > bufsize){ int frag1 = bufsize - offset; cpnt = kmalloc(reclen, GFP_KERNEL); if (cpnt == NULL) goto out_nomem; memcpy(cpnt, pnt, frag1); brelse(bh); bh = bread(inode->i_dev, ++block, bufsize); if (!bh) goto out_noread; offset += reclen - bufsize; memcpy((char *)cpnt+frag1, bh->b_data, offset); raw_inode = ((struct iso_directory_record *) cpnt); } inode->i_size += isonum_733 (raw_inode->size); if(i == 1) inode->u.isofs_i.i_next_section_ino = ino; ino += reclen; if (cpnt) kfree (cpnt); i++; if(i > 100) goto out_toomany; } while(raw_inode->flags[-high_sierra] & 0x80);out: brelse(bh); return 0;out_nomem: printk(KERN_INFO "ISOFS: NoMem ISO inode %lu\n", inode->i_ino); brelse(bh); return 1;out_noread: printk(KERN_INFO "ISOFS: unable to read i-node block %d\n", block); if (cpnt) kfree(cpnt); return 1;out_toomany: printk(KERN_INFO "isofs_read_level3_size: " "More than 100 file sections ?!?, aborting...\n" "isofs_read_level3_size: inode=%lu ino=%lu\n", inode->i_ino, ino); goto out;}void isofs_read_inode(struct inode * inode){ struct super_block *sb = inode->i_sb; unsigned long bufsize = ISOFS_BUFFER_SIZE(inode); int block = inode->i_ino >> ISOFS_BUFFER_BITS(inode); int high_sierra = sb->u.isofs_sb.s_high_sierra; struct buffer_head * bh; struct iso_directory_record * raw_inode; unsigned char *pnt; int volume_seq_no, i; bh = bread(inode->i_dev, block, bufsize); if (!bh) { printk(KERN_WARNING "ISOFS: unable to read i-node block\n"); goto fail; } pnt = ((unsigned char *) bh->b_data + (inode->i_ino & (bufsize - 1))); raw_inode = ((struct iso_directory_record *) pnt); if (raw_inode->flags[-high_sierra] & 2) { inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR; inode->i_nlink = 1; /* Set to 1. We know there are 2, but the find utility tries to optimize if it is 2, and it screws up. It is easier to give 1 which tells find to do it the hard way. */ } else { /* Everybody gets to read the file. */ inode->i_mode = inode->i_sb->u.isofs_sb.s_mode; inode->i_nlink = 1; inode->i_mode |= S_IFREG; /* If there are no periods in the name, * then set the execute permission bit */ for(i=0; i< raw_inode->name_len[0]; i++) if(raw_inode->name[i]=='.' || raw_inode->name[i]==';') break; if(i == raw_inode->name_len[0] || raw_inode->name[i] == ';') inode->i_mode |= S_IXUGO; /* execute permission */ } inode->i_uid = inode->i_sb->u.isofs_sb.s_uid; inode->i_gid = inode->i_sb->u.isofs_sb.s_gid; inode->i_blocks = inode->i_blksize = 0; inode->u.isofs_i.i_section_size = isonum_733 (raw_inode->size); if(raw_inode->flags[-high_sierra] & 0x80) { if(isofs_read_level3_size(inode)) goto fail; } else { inode->i_size = isonum_733 (raw_inode->size); } /* There are defective discs out there - we do this to protect ourselves. A cdrom will never contain more than 800Mb .. but a DVD may be up to 1Gig (Ulrich Habel) */ if((inode->i_size < 0 || inode->i_size > 1073741824) && inode->i_sb->u.isofs_sb.s_cruft == 'n') { printk("Warning: defective cdrom. Enabling \"cruft\" mount option.\n"); inode->i_sb->u.isofs_sb.s_cruft = 'y'; }/* Some dipshit decided to store some other bit of information in the high byte of the file length. Catch this and holler. WARNING: this will make it impossible for a file to be > 16Mb on the CDROM!!!*/ if(inode->i_sb->u.isofs_sb.s_cruft == 'y' && inode->i_size & 0xff000000){/* printk("Illegal format on cdrom. Pester manufacturer.\n"); */ inode->i_size &= 0x00ffffff; } if (raw_inode->interleave[0]) { printk("Interleaved files not (yet) supported.\n"); inode->i_size = 0; } /* I have no idea what file_unit_size is used for, so we will flag it for now */ if(raw_inode->file_unit_size[0] != 0){ printk("File unit size != 0 for ISO file (%ld).\n",inode->i_ino); } /* I have no idea what other flag bits are used for, so we will flag it for now */#ifdef DEBUG if((raw_inode->flags[-high_sierra] & ~2)!= 0){ printk("Unusual flag settings for ISO file (%ld %x).\n", inode->i_ino, raw_inode->flags[-high_sierra]); }#endif#ifdef DEBUG printk("Get inode %x: %d %d: %d\n",inode->i_ino, block, ((int)pnt) & 0x3ff, inode->i_size);#endif inode->i_mtime = inode->i_atime = inode->i_ctime = iso_date(raw_inode->date, high_sierra); inode->u.isofs_i.i_first_extent = (isonum_733 (raw_inode->extent) + isonum_711 (raw_inode->ext_attr_length)) << inode -> i_sb -> u.isofs_sb.s_log_zone_size;/* Now test for possible Rock Ridge extensions which will override some of these numbers in the inode structure. */ if (!high_sierra) { parse_rock_ridge_inode(raw_inode, inode); /* hmm..if we want uid or gid set, override the rock ridge setting */ test_and_set_uid(&inode->i_uid, inode->i_sb->u.isofs_sb.s_uid); }#ifdef DEBUG printk("Inode: %x extent: %x\n",inode->i_ino, inode->u.isofs_i.i_first_extent);#endif /* get the volume sequence number */ volume_seq_no = isonum_723 (raw_inode->volume_sequence_number) ; /* * All done with buffer ... no more references to buffer memory! */ brelse(bh); /* * Disable checking if we see any volume number other than 0 or 1. * We could use the cruft option, but that has multiple purposes, one * of which is limiting the file size to 16Mb. Thus we silently allow * volume numbers of 0 to go through without complaining. */ if (inode->i_sb->u.isofs_sb.s_cruft == 'n' && (volume_seq_no != 0) && (volume_seq_no != 1)) { printk("Warning: defective cdrom (volume sequence number). Enabling \"cruft\" mount option.\n"); inode->i_sb->u.isofs_sb.s_cruft = 'y'; } /* Install the inode operations vector */ inode->i_op = NULL;#ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS if (inode->i_sb->u.isofs_sb.s_cruft != 'y' && (volume_seq_no != 0) && (volume_seq_no != 1)) { printk("Multi volume CD somehow got mounted.\n"); } else#endif IGNORE_WRONG_MULTI_VOLUME_SPECS { if (S_ISREG(inode->i_mode)) inode->i_op = &isofs_file_inode_operations; else if (S_ISDIR(inode->i_mode)) inode->i_op = &isofs_dir_inode_operations; else if (S_ISLNK(inode->i_mode)) inode->i_op = &isofs_symlink_inode_operations; else if (S_ISCHR(inode->i_mode)) inode->i_op = &chrdev_inode_operations; else if (S_ISBLK(inode->i_mode)) inode->i_op = &blkdev_inode_operations; else if (S_ISFIFO(inode->i_mode)) init_fifo(inode); } return; fail: /* With a data error we return this information */ inode->i_mtime = inode->i_atime = inode->i_ctime = 0; inode->u.isofs_i.i_first_extent = 0; inode->i_size = 0; inode->i_blocks = inode->i_blksize = 0; inode->i_nlink = 1; inode->i_uid = inode->i_gid = 0; inode->i_mode = S_IFREG; /*Regular file, no one gets to read*/ inode->i_op = NULL; return;}/* There are times when we need to know the inode number of a parent of a particular directory. When control passes through a routine that has access to the parent information, it fills it into the inode structure, but sometimes the inode gets flushed out of the queue, and someone remembers the number. When they try to open up again, we have lost the information. The '..' entry on the disc points to the data area for a particular inode, so we can follow these links back up, but since we do not know the inode number, we do not actually know how large the directory is. The disc is almost always correct, and there is enough error checking on the drive itself, but an open ended search makes me a little nervous. The BSD iso filesystem uses the extent number for an inode, and this would work really nicely for us except that the read_inode function would not have any clean way of finding the actual directory record that goes with the file. If we had such info, then it would pay to change the inode numbers and eliminate this function.*/int isofs_lookup_grandparent(struct inode * parent, int extent){ unsigned long bufsize = ISOFS_BUFFER_SIZE(parent); unsigned char bufbits = ISOFS_BUFFER_BITS(parent); unsigned int block,offset; int parent_dir, inode_number; int result; int directory_size; struct buffer_head * bh; struct iso_directory_record * de; offset = 0; block = extent << (ISOFS_ZONE_BITS(parent) - bufbits); if (!(bh = bread(parent->i_dev, block, bufsize))) return -1; while (1 == 1) { de = (struct iso_directory_record *) (bh->b_data + offset); if (*((unsigned char *) de) == 0) { brelse(bh); printk("Directory .. not found\n"); return -1; } offset += *((unsigned char *) de); if (offset >= bufsize) { printk(".. Directory not in first block" " of directory.\n"); brelse(bh); return -1; } if (de->name_len[0] == 1 && de->name[0] == 1) { parent_dir = find_rock_ridge_relocation(de, parent); directory_size = isonum_733 (de->size); brelse(bh); break; } }#ifdef DEBUG printk("Parent dir:%x\n",parent_dir);#endif /* Now we know the extent where the parent dir starts on. */ result = -1; offset = 0; block = parent_dir << (ISOFS_ZONE_BITS(parent) - bufbits); if (!block || !(bh = bread(parent->i_dev,block, bufsize))) { return -1; } for(;;) { de = (struct iso_directory_record *) (bh->b_data + offset); inode_number = (block << bufbits)+(offset & (bufsize - 1)); /* If the length byte is zero, we should move on to the next CDROM sector. If we are at the end of the directory, we kick out of the while loop. */ if ((*((unsigned char *) de) == 0) || (offset == bufsize) ) { brelse(bh); offset = 0; block++; directory_size -= bufsize; if(directory_size < 0) return -1; if((block & 1) && (ISOFS_ZONE_BITS(parent) - bufbits) == 1) { return -1; } if((block & 3) && (ISOFS_ZONE_BITS(parent) - bufbits) == 2) { return -1; } if (!block || !(bh = bread(parent->i_dev,block, bufsize))) { return -1; } continue; } /* Make sure that the entire directory record is in the current bh block. If not, we malloc a buffer, and put the two halves together, so that we can cleanly read the block. */ offset += *((unsigned char *) de); if (offset > bufsize) { printk("Directory overrun\n"); goto out; } if (find_rock_ridge_relocation(de, parent) == extent){ result = inode_number; goto out; } } /* We go here for any condition we cannot handle. We also drop through to here at the end of the directory. */ out: brelse(bh);#ifdef DEBUG printk("Resultant Inode %d\n",result);#endif return result;}#ifdef LEAK_CHECK#undef malloc#undef free_s#undef bread#undef brelsevoid * leak_check_malloc(unsigned int size){ void * tmp; check_malloc++; tmp = kmalloc(size, GFP_KERNEL); return tmp;}void leak_check_free_s(void * obj, int size){ check_malloc--; return kfree_s(obj, size);}struct buffer_head * leak_check_bread(int dev, int block, int size){ check_bread++; return bread(dev, block, size);}void leak_check_brelse(struct buffer_head * bh){ check_bread--; return brelse(bh);}#endifstatic struct file_system_type iso9660_fs_type = { "iso9660", FS_REQUIRES_DEV, isofs_read_super, NULL};__initfunc(int init_iso9660_fs(void)){ return register_filesystem(&iso9660_fs_type);}#ifdef MODULEEXPORT_NO_SYMBOLS;int init_module(void){ return init_iso9660_fs();}void cleanup_module(void){ unregister_filesystem(&iso9660_fs_type);}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?