ext2文件系统源代码之ialloc.c
2016-03-17 22:47
309 查看
今天我们来看一个和inode的分配有关的文件,ialloc.c,这个文件有点大,做好准备哦。
我们开始吧
我们开始吧
/* 文件作者,又是他,看来ext2文件系统的code都是他写的 * linux/fs/ext2/ialloc.c * * Copyright (C) 1992, 1993, 1994, 1995 * Remy Card (card@masi.ibp.fr) * Laboratoire MASI - Institut Blaise Pascal * Universite Pierre et Marie Curie (Paris VI) * * BSD ufs-inspired inode and directory allocation by * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 * Big-endian to little-endian byte-swapping/bitmaps by * David S. Miller (davem@caip.rutgers.edu), 1995 */ #include <linux/quotaops.h> #include <linux/sched.h> #include <linux/backing-dev.h> #include <linux/buffer_head.h> #include <linux/random.h> #include "ext2.h" #include "xattr.h" #include "acl.h" /*ialloc.c包含了inode的分配和销毁的函数 */ /*inode位图管理着所有的空闲inode块,一个文件系统由若干个块组组成,每一个组包含一个数据块位图和一个inode位图,N个块用来做inode table和数据块,块组描述符在超级块的后边,每一个块组描述符都记录着空闲块的数目和数据块位图的位置*/ /*read_inode_bitmap函数在一个给定的组内阅读inode位图,返回的buffer_head就是对应的inode位图的缓冲区*/ static struct buffer_head * read_inode_bitmap(struct super_block * sb, unsigned long block_group) { struct ext2_group_desc *desc; struct buffer_head *bh = NULL; /*之前讲过的函数,获得block_group组的组描述符*/ desc = ext2_get_group_desc(sb, block_group, NULL); if (!desc) goto error_out; /*调用设备驱动层的函数,bg_inode_bitmap字段是组内的inode位图的块号,读取这个块的数据*/ bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap)); /*如果读取失败,报严重错误*/ if (!bh) ext2_error(sb, "read_inode_bitmap", "Cannot read inode bitmap - " "block_group = %lu, inode_bitmap = %u", block_group, le32_to_cpu(desc->bg_inode_bitmap)); error_out: /*返回找到的缓冲区结构体*/ return bh; } /*释放inode结构体,group是释放的inode在的组号,dir标记释放的inode是不是目录*/ static void ext2_release_inode(struct super_block *sb, int group, int dir) { struct ext2_group_desc * desc; struct buffer_head *bh; /*先获得组描述符*/ desc = ext2_get_group_desc(sb, group, &bh); if (!desc) { ext2_error(sb, "ext2_release_inode", "can't get descriptor for group %d", group); return; } spin_lock(sb_bgl_lock(EXT2_SB(sb), group)); /*记录空闲块数目的变量bg_free_inodes_count加一*/ desc->bg_free_inodes_count = cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1); /*如果删除的是目录的inode,记录目录数目的变量bg_used_dirs_count还要减一*/ if (dir) desc->bg_used_dirs_count = cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1); spin_unlock(sb_bgl_lock(EXT2_SB(sb), group)); /*超级块的记录变量也要减一*/ if (dir) percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter); /*这个缓冲区已经脏了,超级块也脏了*/ sb->s_dirt = 1; mark_buffer_dirty(bh); } /*释放inode的内存资源,传入的inode已经不再hash链表上了,并且他所在的目录也把他删除了,在我们在位图上标记未使用之前,一定要调用clear_inode函数清理*/ void ext2_free_inode (struct inode * inode) { struct super_block * sb = inode->i_sb; int is_directory; unsigned long ino; struct buffer_head *bitmap_bh = NULL; unsigned long block_group; unsigned long bit; struct ext2_super_block * es; /*要删除的inode编号*/ ino = inode->i_ino; ext2_debug ("freeing inode %lu\n", ino); /*如果inode没有问题*/ if (!is_bad_inode(inode)) { /* Quota is already initialized in iput() */ /*释放inode有关的属性资源*/ ext2_xattr_delete_inode(inode); /*释放配额资源*/ DQUOT_FREE_INODE(inode); DQUOT_DROP(inode); } /*获得ext2硬盘的结构体ext2_super_block*/ es = EXT2_SB(sb)->s_es; /*看看要删除的inode是不是目录*/ is_directory = S_ISDIR(inode->i_mode); /*clear_inode函数必须在标记inode位图之前调用,不然会出现两个inode占用同一个inode号码的情况,clear_inode函数真正释放inode的内存*/ clear_inode (inode); /*如果要删除的inode编号小于最小的或是大于最大的,也就是说不合法,报严重错误*/ if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { ext2_error (sb, "ext2_free_inode", "reserved or nonexistent inode %lu", ino); goto error_return; } /*block_group得到inode所在的块组*/ block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb); /*bit是inode在块组的inodetable内的偏移,主要是用来在inode位图上的偏移*/ bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb); brelse(bitmap_bh); /*上边刚讲过,获得块组的inode位图*/ bitmap_bh = read_inode_bitmap(sb, block_group); if (!bitmap_bh) goto error_return; /* 好啦,现在我们可以更新inode位图啦.... */ if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group), bit, (void *) bitmap_bh->b_data)) /*如果之前就是0,说明这个inode之前就已经被释放了,这说明出错啦*/ ext2_error (sb, "ext2_free_inode", "bit already cleared for inode %lu", ino); else /*释放inode,主要是做一些描述符,超级块的记录工作*/ ext2_release_inode(sb, block_group, is_directory); /*标记缓冲区为脏,如果文件系统是要求立即更新的,就立即同步*/ mark_buffer_dirty(bitmap_bh); if (sb->s_flags & MS_SYNCHRONOUS) sync_dirty_buffer(bitmap_bh); error_return: brelse(bitmap_bh); } /*ext2预读取inode,在ext2里,我们在创建一个inode的时候,会预读取这个inode的块*/ static void ext2_preread_inode(struct inode *inode) { unsigned long block_group; unsigned long offset; unsigned long block; struct buffer_head *bh; struct ext2_group_desc * gdp; struct backing_dev_info *bdi; /*获得inode对应的缓冲区的预读写结构体,判断是不是有读写拥挤的情况出现*/ bdi = inode->i_mapping->backing_dev_info; if (bdi_read_congested(bdi)) return; if (bdi_write_congested(bdi)) return; /*组编号*/ block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); /*gdp获得这个组的组描述符*/ gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh); if (gdp == NULL) return; /*offset是inode在inodetable内的偏移*/ offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) * EXT2_INODE_SIZE(inode->i_sb); /*block是得到inode的块号码*/ block = le32_to_cpu(gdp->bg_inode_table) + (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb)); /*提前读取block块到缓冲区*/ sb_breadahead(inode->i_sb, block); } /*对于分配新的inode有两个策略,当分配的是目录的inode的时候,就是向前寻找一个空闲块多的,并且目录块占比例比较少的块组,如果没有找到的话,就找一个空闲空间在平均以上的,块组的目录最少的块组就会被选择,当新创建的inode不是目录的inode,就会在他的父目录的块组找一个空闲的inode块*/ static int find_group_dir(struct super_block *sb, struct inode *parent) { /*获得组的数目*/ int ngroups = EXT2_SB(sb)->s_groups_count; /*avefreei代表平均每一组拥有的空闲inode数目*/ int avefreei = ext2_count_free_inodes(sb) / ngroups; struct ext2_group_desc *desc, *best_desc = NULL; struct buffer_head *bh, *best_bh = NULL; int group, best_group = -1; /*遍历每一组*/ for (group = 0; group < ngroups; group++) { /*先获得这一组的组描述符*/ desc = ext2_get_group_desc (sb, group, &bh); /*如果组描述符还没在内存里或者是没有空闲的inode了,就找下一个*/ if (!desc || !desc->bg_free_inodes_count) continue; /*如果空闲的inode小于平均值,也找下一个*/ if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) continue; /*如果之前还没有找到最好的组,或者当前组的空闲块数目大于之前找到的最好的组的空闲块数目,就把best_group指向新的最好的组号,best_desc指向新的最好的组描述符,best_bh指向新的缓冲区结构体*/ if (!best_desc || (le16_to_cpu(desc->bg_free_blocks_count) > le16_to_cpu(best_desc->bg_free_blocks_count))) { best_group = group; best_desc = desc; best_bh = bh; } } /*如果没有找到的话,返回-1*/ if (!best_desc) return -1; /*找到了就返回组号*/ return best_group; } /* * Orlov的分配规则.,orlov估计是一个人名,以自己名字命名 * 如果有一个组,拥有比平均数目更多的充足的inode和充足的空闲块,我们返回其中目录项最少的,否则我们返回一个随机的 * 其他的规则: * 一般来说,把一个目录放入一个组是没问题的除非这个组已经有太多的目录超出max_dirs限制,或者有太少的空闲inode小于min_inode字段的值,或者是又太少的空闲块小于min_blocks字段值,或者是负载过大max_debt字段大。一般来说,最好去父目录虽在的块组,如果不满足的话,我们就去找一个有比平均的空闲块多的块组就可以了 * debt是会逐渐增加的,当我们分配一个目录就回加一,当我们创建一个inode就会减一,值在0-255之间 */ #define INODE_COST 64 #define BLOCK_COST 256 /*sb是文件系统的vfs层的超级块,parent是父目录的inode*/ static int find_group_orlov(struct super_block *sb, struct inode *parent) { /*父目录所在的组编号*/ int parent_group = EXT2_I(parent)->i_block_group; /*存在内存里的ext2_sb_info信息结构体*/ struct ext2_sb_info *sbi = EXT2_SB(sb); /*ext2文件系统的硬盘存储的超级块*/ struct ext2_super_block *es = sbi->s_es; /*拥有块组的数目*/ int ngroups = sbi->s_groups_count; /*每一个组拥有的inode数目*/ int inodes_per_group = EXT2_INODES_PER_GROUP(sb); int freei; int avefreei; int free_blocks; int avefreeb; int blocks_per_dir; int ndirs; int max_debt, max_dirs, min_blocks, min_inodes; int group = -1, i; struct ext2_group_desc *desc; struct buffer_head *bh; /*计算平均每一个组的空闲inode数目*/ freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); avefreei = freei / ngroups; /*计算平均每一个组的空闲块数目*/ free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); avefreeb = free_blocks / ngroups; /*文件系统的所有的目录数目*/ ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); /*如果是在文件系统的第一级创建目录,尽量分散开来*/ if ((parent == sb->s_root->d_inode) || (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) { struct ext2_group_desc *best_desc = NULL; struct buffer_head *best_bh = NULL; int best_ndir = inodes_per_group; int best_group = -1; /*随机选一个组作为遍历的开始点*/ get_random_bytes(&group, sizeof(group)); parent_group = (unsigned)group % ngroups; /*遍历每一个组*/ for (i = 0; i < ngroups; i++) { group = (parent_group + i) % ngroups; /*获得组描述符*/ desc = ext2_get_group_desc (sb, group, &bh); /*如果组描述符为NULL或者空闲inode为0,找下一个*/ if (!desc || !desc->bg_free_inodes_count) continue; /*如果使用的目录太多也不行*/ if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) continue; /*空闲的inode数目小于平均值*/ if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) continue; /*空闲的块数目小于平均值也不行*/ if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) continue; /*best的四个变量指向找到的最优组号*/ best_group = group; best_ndir = le16_to_cpu(desc->bg_used_dirs_count); best_desc = desc; best_bh = bh; } /*如果找到啦,就使desc,bh,group指向找到的最优组,跳转到found*/ if (best_group >= 0) { desc = best_desc; bh = best_bh; group = best_group; goto found; } /*没找到,跳找到寻找失败*/ goto fallback; } /*如果文件系统没有一个目录,这不太可能.....*/ if (ndirs == 0) ndirs = 1; /* percpu_counters are approximate... */ /*这个变量值是平均每一个目录的负载的块数目*/ blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs; /*粗略计算出每一个块组最多的目录数目,最少的inode数目和最少的block数目*/ max_dirs = ndirs / ngroups + inodes_per_group / 16; min_inodes = avefreei - inodes_per_group / 4; min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4; /*BLOCK_COST是一个目录对应的块的开销,每一个组的最多的块数目除以开销就是最多的块数目*/ max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST); /*将max_debt字段和inode的块数目比较,如果大的话,就设置为inode的块数除以inode的开销*/ if (max_debt * INODE_COST > inodes_per_group) max_debt = inodes_per_group / INODE_COST; if (max_debt > 255) max_debt = 255; if (max_debt == 0) max_debt = 1; /*遍历组*/ for (i = 0; i < ngroups; i++) { /*从父节点的组开始遍历*/ group = (parent_group + i) % ngroups; /*获得当前遍历到的组的组描述符*/ desc = ext2_get_group_desc (sb, group, &bh); /*如果组描述符为NULL或者是空闲inode为0,下一位*/ if (!desc || !desc->bg_free_inodes_count) continue; /*如果当前组的负载过大,下一位*/ if (sbi->s_debts[group] >= max_debt) continue; /*如果使用的目录过多,下一位*/ if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) continue; /*如果空闲inode少,下一位*/ if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) continue; /*如果空闲block小于最小值,下一位*/ if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) continue; /*都符合,找到啦!*/ goto found; } fallback: /*运行到这里,说明没有找到我们想要的组,那只能勉为其难,降标准啦*/ for (i = 0; i < ngroups; i++) { /*遍历组,获得当前遍历到的组的组描述符*/ group = (parent_group + i) % ngroups; desc = ext2_get_group_desc (sb, group, &bh); /*检查当前遍历到的组是不是合法*/ if (!desc || !desc->bg_free_inodes_count) continue; /*只要求这个组的空闲inode比平均值大就可以,如果符合,也可以接受*/ if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) goto found; } /*还没找到,那只能随便找一个只要有空闲inode空间就可以,把avefreei变成0,再去寻找*/ if (avefreei) { avefreei = 0; goto fallback; } /*实在找不到,GG吧*/ return -1; found: /*找到了,返回组号*/ return group; } /*为一个目录找一个自目录项的节点对应的组,相对比较简单的查找策略,没有负载这么一说,只要有空间,就放*/ static int find_group_other(struct super_block *sb, struct inode *parent) { /*父节点的组号*/ int parent_group = EXT2_I(parent)->i_block_group; /*组的数目*/ int ngroups = EXT2_SB(sb)->s_groups_count; struct ext2_group_desc *desc; struct buffer_head *bh; int group, i; /*现场时可不可以把新节点放到父目录里*/ group = parent_group; /*父节点的组描述符*/ desc = ext2_get_group_desc (sb, group, &bh); /*如果父节点的组有空闲的inode和block就可以*/ if (desc && le16_to_cpu(desc->bg_free_inodes_count) && le16_to_cpu(desc->bg_free_blocks_count)) goto found; /*随机找一个和父目录不同的组*/ group = (group + parent->i_ino) % ngroups; /*使用二次哈希找一个有充足的空闲inode和block的组*/ /*按照依次乘以二的偏移来寻找*/ for (i = 1; i < ngroups; i <<= 1) { group += i; if (group >= ngroups) group -= ngroups; /*当前寻找到的组的组描述符*/ desc = ext2_get_group_desc (sb, group, &bh); /*有空间就找到了*/ if (desc && le16_to_cpu(desc->bg_free_inodes_count) && le16_to_cpu(desc->bg_free_blocks_count)) goto found; } /*运行到此,说明查找失败,那就近找一个空闲inode吧,即使这个组没有充足的inode和block*/ group = parent_group; /*从父节点开始,就近寻找*/ for (i = 0; i < ngroups; i++) { /*一个一个搜寻,到边界就变为0*/ if (++group >= ngroups) group = 0; /*组描述符*/ desc = ext2_get_group_desc (sb, group, &bh); /*只要有空闲的inode就可以*/ if (desc && le16_to_cpu(desc->bg_free_inodes_count)) goto found; } return -1; found: return group; } /*重头戏来啦,新创建一个inode,dir参数是父目录的inode,mode是文件创建的模式*/ struct inode *ext2_new_inode(struct inode *dir, int mode) { struct super_block *sb; struct buffer_head *bitmap_bh = NULL; struct buffer_head *bh2; int group, i; ino_t ino = 0; struct inode * inode; struct ext2_group_desc *gdp; struct ext2_super_block *es; struct ext2_inode_info *ei; struct ext2_sb_info *sbi; int err; /*获得父目录的超级块结构体*/ sb = dir->i_sb; /*new_inode定义在inode.c文件,这个函数做了分配一个inode的底层空间操作*/ inode = new_inode(sb); if (!inode) return ERR_PTR(-ENOMEM); /*获得它的ext2_inode_info,ext2_sb_info和ext2_super_block*/ ei = EXT2_I(inode); sbi = EXT2_SB(sb); es = sbi->s_es; /*判断你要创建的inode是什么东西*/ if (S_ISDIR(mode)) { /*要创建一个目录,先查看超级块,看看文件系统的挂载选项的OLDALLOC位,如果是1,说明使用老旧的分配策略*/ if (test_opt(sb, OLDALLOC)) /*之前讲过,比较老的inode分配函数*/ group = find_group_dir(sb, dir); else /*比较新的高效的分配策略函数*/ group = find_group_orlov(sb, dir); } else /*一般的文件的分配策略*/ group = find_group_other(sb, dir); /*说明分配失败*/ if (group == -1) { err = -ENOSPC; goto fail; } /*遍历每一个组*/ for (i = 0; i < sbi->s_groups_count; i++) { /*获得当前遍历到的组的组描述符*/ gdp = ext2_get_group_desc(sb, group, &bh2); brelse(bitmap_bh); /*这个组的inode位图*/ bitmap_bh = read_inode_bitmap(sb, group); if (!bitmap_bh) { err = -EIO; goto fail; } ino = 0; repeat_in_this_group: /*在这个组内的inode位图寻找为0的位*/ ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data, EXT2_INODES_PER_GROUP(sb), ino); /*如果没找到,说明这个组没有空闲的inode,那就continue,找下一个吧*/ if (ino >= EXT2_INODES_PER_GROUP(sb)) { /*找下一个块*/ if (++group == sbi->s_groups_count) group = 0; continue; } /*标记这个位已经为1,代表这个inode已经被使用啦,返回值是当前的值*/ if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group), ino, bitmap_bh->b_data)) { /*说明这个inode已经被占用了*/ /* 如果这个组的inode已经被用完了,就尝试下一个组*/ if (++ino >= EXT2_INODES_PER_GROUP(sb)) { if (++group == sbi->s_groups_count) group = 0; continue; } /* 这个inode已经被占用了,我们去其他组看看吧 */ goto repeat_in_this_group; } /*找到了*/ goto got; } /*扫描所有块组*/ err = -ENOSPC; goto fail; got: /*找到我们想要的块组了,因为我们修改了inode位图的值,所以标记脏了*/ mark_buffer_dirty(bitmap_bh); /*如果文件系统要求修改立即同步,就同步*/ if (sb->s_flags & MS_SYNCHRONOUS) sync_dirty_buffer(bitmap_bh); brelse(bitmap_bh); /*原来的ino是位图内的位偏移位置,现在的到的是inode的编号*/ ino += group * EXT2_INODES_PER_GROUP(sb) + 1; /*如果inode编号大于inode数目,小于最小号码,报错误*/ if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { ext2_error (sb, "ext2_new_inode", "reserved inode or inode > inodes count - " "block_group = %d,inode=%lu", group, (unsigned long) ino); err = -EIO; goto fail; } /*空闲块少了一个,记录下来*/ percpu_counter_mod(&sbi->s_freeinodes_counter, -1); /*如果是目录,记录目录多了一个*/ if (S_ISDIR(mode)) percpu_counter_inc(&sbi->s_dirs_counter); /*对于ext2_sb_info的访问要上锁*/ spin_lock(sb_bgl_lock(sbi, group)); /*修改组描述符内的空闲inode数目*/ gdp->bg_free_inodes_count = cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1); /*如果是目录,还要修改ext2_sb_info内的负载记录,多一个目录++,多一个非目录文件--,还有目录使用数量也要更新*/ if (S_ISDIR(mode)) { if (sbi->s_debts[group] < 255) sbi->s_debts[group]++; gdp->bg_used_dirs_count = cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1); } else { if (sbi->s_debts[group]) sbi->s_debts[group]--; } spin_unlock(sb_bgl_lock(sbi, group)); /*我们的超级块已经脏了,bh2存放找到的组描述符*/ sb->s_dirt = 1; mark_buffer_dirty(bh2); /*uid是创建者的uid,current指向当前进程*/ inode->i_uid = current->fsuid; /*如果文件系统有组id,赋值,如果文件的打开模式有S_ISGID也要赋值*/ if (test_opt (sb, GRPID)) inode->i_gid = dir->i_gid; else if (dir->i_mode & S_ISGID) { inode->i_gid = dir->i_gid; if (S_ISDIR(mode)) mode |= S_ISGID; } else inode->i_gid = current->fsgid; /*依次给i_mode字段,inode编号,拥有的块数目,修改时间等字段赋值*/ inode->i_mode = mode; inode->i_ino = ino; inode->i_blocks = 0; inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; /*修改这个新创建的inode对应的内存信息结构体ext2_inode_info的各项值*/ memset(ei->i_data, 0, sizeof(ei->i_data)); /*赋值父目录的flag,但不是btree格式*/ ei->i_flags = EXT2_I(dir)->i_flags & ~EXT2_BTREE_FL; /*如果这是一个软链接,他不可一世不可修改或者不可添加的*/ if (S_ISLNK(mode)) ei->i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL); /* 目录同步flag只能在目录里有 */ if (!S_ISDIR(mode)) ei->i_flags &= ~EXT2_DIRSYNC_FL; /*各个字段初始化为0*/ ei->i_faddr = 0; ei->i_frag_no = 0; ei->i_frag_size = 0; ei->i_file_acl = 0; ei->i_dir_acl = 0; ei->i_dtime = 0; /*inode所在的块组号码*/ ei->i_block_group = group; ei->i_next_alloc_block = 0; ei->i_next_alloc_goal = 0; ei->i_prealloc_block = 0; ei->i_prealloc_count = 0; ei->i_dir_start_lookup = 0; /*状态,新创建*/ ei->i_state = EXT2_STATE_NEW; /*从ext2_inode_info结构体里的flag设置inode结构体的flag*/ ext2_set_inode_flags(inode); spin_lock(&sbi->s_next_gen_lock); inode->i_generation = sbi->s_next_generation++; spin_unlock(&sbi->s_next_gen_lock); /*新创建的inode要插入到hash列表里*/ insert_inode_hash(inode); /*检查用户配额够不够*/ if (DQUOT_ALLOC_INODE(inode)) { err = -EDQUOT; goto fail_drop; } /*之前讲过,acl控制的初始化*/ err = ext2_init_acl(inode, dir); if (err) goto fail_free_drop; /*属性安全操作*/ err = ext2_init_security(inode,dir); if (err) goto fail_free_drop; /*标记inode脏了,要记得和硬盘同步*/ mark_inode_dirty(inode); ext2_debug("allocating inode %lu\n", inode->i_ino); /*之前讲过,预读取,减少多次硬盘读写的开销*/ ext2_preread_inode(inode); return inode; fail_free_drop: /*预额分配失败,把分配的inode释放吧*/ DQUOT_FREE_INODE(inode); fail_drop: /*初始化acl失败*/ DQUOT_DROP(inode); /*标记没有配额了,释放inode*/ inode->i_flags |= S_NOQUOTA; inode->i_nlink = 0; iput(inode); return ERR_PTR(err); fail: make_bad_inode(inode); iput(inode); return ERR_PTR(err); } /*之前讲过类似的函数,ext2的计算空闲的inode的函数*/ unsigned long ext2_count_free_inodes (struct super_block * sb) { struct ext2_group_desc *desc; unsigned long desc_count = 0; int i; /*如果配置了EXT2FS_DEBUG宏,就采用下边的操作,否则就采用另一套操作*/ #ifdef EXT2FS_DEBUG struct ext2_super_block *es; unsigned long bitmap_count = 0; struct buffer_head *bitmap_bh = NULL; /*获得ext2硬盘上存储的ext2_super_block结构体超级块*/ es = EXT2_SB(sb)->s_es; /*遍历每一个组*/ for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { unsigned x; /*获得当前遍历到的组的组描述符*/ desc = ext2_get_group_desc (sb, i, NULL); if (!desc) continue; /*统计加上当前组的空闲inode数目*/ desc_count += le16_to_cpu(desc->bg_free_inodes_count); brelse(bitmap_bh); /*获得这个组的位图*/ bitmap_bh = read_inode_bitmap(sb, i); if (!bitmap_bh) continue; /*在这个位图上查找为0的位数目*/ x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8); printk("group %d: stored = %d, counted = %u\n", i, le16_to_cpu(desc->bg_free_inodes_count), x); /*统计加上位图上获得的空闲inode数目*/ bitmap_count += x; } brelse(bitmap_bh); /*即打印记录的空闲inode数目,也打印inode位图上获得的空闲inode数目,确保万无一失吧...*/ printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n", percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter), desc_count, bitmap_count); /*最后返回的是组描述符上记录相加得到的*/ return desc_count; #else /*只是统计每一个组描述符的空闲inode和*/ for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { desc = ext2_get_group_desc (sb, i, NULL); if (!desc) continue; desc_count += le16_to_cpu(desc->bg_free_inodes_count); } return desc_count; #endif } /* 在挂载的时候调用,超级块会被锁住,这个函数的作用是统计ext2文件系统的目录数目 */ unsigned long ext2_count_dirs (struct super_block * sb) { unsigned long count = 0; int i; /*遍历每一个组*/ for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { /*获得当前的组的组描述符*/ struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL); if (!gdp) continue; /*统计每一个组的目录数目,加起来*/ count += le16_to_cpu(gdp->bg_used_dirs_count); } return count; }
相关文章推荐