5
13
2014
37

pcmfs的mkfs分析

在文章最前面说一下mkfs的意义:即在dev设备上建立相应的数据布局。也就是你输入mkfs指令后,要跟上一个设备参数,于是乎,mkfs执行完毕后,就有了常见的数据布局。

在我们这个mkfs函数中,最终数据布局如下

 

 

一:函数流程图

下面是mkfs.pcmfs.c函数流程图

          图1 流程图

解释:

1.根据参数获取blocksize

   在输入mkfs指令时,要输入设备参数。如mkfs.pcmfs /dev/sdb1。该指令就是把/dev/sdb1设备格式化为pcmfs文件系统。Main函数会获取参数的设备名,然后通过get_size函数计算出该设备的字节数。在计算出设备的字节数后,在根据blocksize大小计算出该设备的block总数。    get_size函数获取设备大小主要通过count_block函数实现。基本思想是通过read函数返回值得正负来判断是否越界。该函数先利用二乘的思想来确定该dev的粗略的范围,即n未越界,则下次判断2n是否越界。若2n越界则可判断该设备大小在n与2n之间,粗略判断后在通过在n和2n之间的二分查找确定该设备的具体大小。

2.计算block数目

  在上述操作后获取了该设备的字节数,于是可以通过BLOCKS=get_size(device_name)/blocksize,来得到该设备包含的block数目。

3.初始化superblock以及bitmap所需要的数据结构

    Mkfs的目的是格式化非易失介质,即将非意识介质上文件系统的必要的信息准备好,而superblock以及bitmap的结构是文件系统格式化时写入非易失介质的重要信息,将该信息写入到非意识介质中有两步,第一步是在内存中申请一个对应的空间,将该空间的内容赋值成我们所需要写入非易失介质中的数据;第二步就是通过调用write函数将该数据写入到介质当中。

    mkfs.pcmfs.c文件通过setup_tables函数在内存中准备好要写入非易失介质中的superblock信息以及bitmap区域的信息,其中bitmap有inode的bitmap以及block的bitmap。分别对应着IMAPS以及ZMAPS。

    Superblock的该文件系统整体信息的赋值,如第一个data区域所在的block编号,IMAP、ZMAP所占的block数目的赋值等。在该pcmfs文件系统中inode的bitmap占两个block,block的bitmap占8个block。

    同样在内存中申请IMAPS和ZMAPS所占的空间大小,然后通过memset的函数把该区域的数据置0。

4. 初始化根节点inode的数据结构

    在初始化文件系统时,必须初始化根节点的信息,整个系统才有了开始索引、操作的入口。根节点所指向的数据区域是整个data区的第一个数据块。即inode->i_zone[0] =Super.s_firstdatazone;

    由于根节点是一个目录,所以其数据区其实就是一个个的目录项。每个目录项占16个字节。目录项的定义在头文件Pcmfs_fs.h中。目录项如下

struct pcmfs_dir_entry {

       __u16 inode;

       char name[0];

}

   初始化是需要在目录项中写入两个内容。即当前目录的inode号,以及父目录的inode号。由于为根节点,故父目录的inode号与当前目录inode号实则是一样的。写入的两项内容如下

#define ROOT_INO_STRING "\001\000"

static char root_dentry[BLOCK_SIZE] =

ROOT_INO_STRING ".\0\0\0\0\0\0\0\0\0\0\0\0\0"

ROOT_INO_STRING "..\0\0\0\0\0\0\0\0\0\0\0\0";

其中ROOT_INO_STRING是inode号,".\0\0\0\0\0\0\0\0\0\0\0\0\0"为该inode号对应内容。这里实则填充了两项,表示的是当前目录 . 以及父目录 .. 的inode号。为了方便理解,见下图

图2 dentry结构图

如上图,每个目录的数据区都可理解为上述表,每一个表项有16个字节,前两个表项的文件名是固定的,分别是当前目录对应的inode号以及父目录对应的inode号。上述初始化实则初始前两项。在内存中准备好该数据后,在通过write函数写到非易失介质中。

5.将初始化的数据写到介质中

   该功能更通过write_tables函数实现。该函数主要通过调用write函数将准备好在内存中的超级块的内容以及bitmap的内容写入到介质中。这里指的注意的是write函数要按顺序写。由于pcmfs文件系统的整体结构图如下

图3 pcmfs文件系统结构图

    仔细观察上图。inode bitmap 占2个block;data bitmap占8个block。故该文件系统设计中,理想情况,每个inode索引节点只是的文件占4个block(8/2=4),这样就不会出现data bitmap用完,而inode bitmap和inode table未用完 或者 inode bitmap和inode table用完,而data bitmap未用完的情况。

   故在调用write函数,要按顺序写入,即先写superblock,然后写bitmap区域。因为write函数写的区域是设备上当前指针所指向的区域。在写过程中,指针后移,故要按顺序写入。

   另外注意一点的是图3中若block大小为1k,(1024B),则inode bitmap的起始block为第3个block。若为blokc大小大于1k,如为2k,3k等等,则预留字节和super block的字节都可以写在第一个block中,即inode bitmap的起始block为第2个block

   至此,设备的mkfs工作完毕。

 

Category: 文件系统 | Tags: | Read Count: 1422
Avatar_small
لی ویوسوہ ہو یار 说:
2021年7月26日 00:22

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. soap2day

Avatar_small
لی ویوسوہ ہو یار 说:
2021年7月27日 00:53

I found your this post while searching for information about blog-related research ... It's a good post .. keep posting and updating information. refurbished gaming pc

Avatar_small
لی ویوسوہ ہو یار 说:
2021年7月27日 15:03

No doubt this is an excellent post I got a lot of knowledge after reading good luck. Theme of blog is excellent there is almost everything to read, Brilliant post. hts 선물옵션

Avatar_small
لی ویوسوہ ہو یار 说:
2021年7月30日 23:28

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! איתור נזילות

Avatar_small
لی ویوسوہ ہو یار 说:
2021年7月31日 21:03

I really like your take on the issue. I now have a clear idea on what this matter is all about.. rantaiqq pkv

Avatar_small
لی ویوسوہ ہو یار 说:
2021年7月31日 21:26

I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing. kctapp

Avatar_small
seoservvise 说:
2021年8月05日 21:06 Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. gamingkk.com
Avatar_small
لی ویوسوہ ہو یار 说:
2021年8月24日 14:20

I recommend only good and reliable information, so see it: link to website

Avatar_small
seoservise 说:
2021年12月09日 22:00

Thanks for sharing this quality information with us. I really enjoyed reading. Will surely going to share this URL with my friends. lottery sambad

Avatar_small
Alice 说:
2022年2月22日 20:43

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me.<a href="https://triberr.com/Rearandeas">Find the best Font</a>

Avatar_small
Website 说:
2022年9月27日 19:46

hair beauty products online
This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information.

Avatar_small
Oldsmar dentist 说:
2022年10月16日 23:18

These are some great tools that I definitely use for SEO work. This is a great list to use in the future.

Avatar_small
app development 说:
2022年11月01日 03:55

Wonderful article. Thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here.

Avatar_small
foglightpi 说:
2022年11月20日 04:38

Are you looking for an expert private detective agent for counter surveillance? Foglight Private Investigation is the once can help you with it.<a href='https://foglightpi.com/services/counter-surveillance/'>Counter Surveillance </a>

Avatar_small
Website 说:
2022年11月22日 20:07

Buy YouTube PVA Accounts
These are some great tools that I definitely use for SEO work. This is a great list to use in the future.

Avatar_small
Website 说:
2022年11月22日 23:48

Buy Gmail PVA Accounts
Thank you for taking the time to publish this information very useful.

Avatar_small
Website 说:
2022年11月23日 00:13

Buy Instagram PVA Accounts
I would like to say that this blog really convinced me to do it! Thanks. Very good post.

Avatar_small
Website 说:
2022年11月25日 21:21

UX/UI Consultancy
I am definitely enjoying your website. You definitely have some great insight and great stories.

Avatar_small
Web Agency 说:
2022年11月26日 04:21

Chicago Web Design
Great article lots of information to Read. Great Man Keep Posting and update to People. Thanks.

Avatar_small
Website 说:
2022年12月16日 04:51

laser hair removal safety glasses
It's a great pleasure reading your post. It's full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.

Avatar_small
rho inc 说:
2022年12月17日 06:55

I think this is an informative post and it is very useful and knowledgeable. Therefore, I would like to thank you for the efforts you have made in writing this article.

Avatar_small
gm web solution 说:
2022年12月27日 05:12

Your blog was found very well and I can get all the information that I need, If you are looking to get more interesting content visit Foglight Investigations GM Blogger GM Web Solutions

Avatar_small
Water Damage 说:
2023年2月23日 05:00

Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting. Maybe we can see more on this. Are you aware of any other websites on this subject?

Avatar_small
Website 说:
2023年3月19日 23:33

Jamaica all inclusive resorts
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have.

Avatar_small
Website 说:
2023年3月23日 00:35

anunturi matrimoniale
I really appreciate the kind of topics you post here. Thanks for sharing great information that is actually helpful. Good day!

Avatar_small
vintage clothing 说:
2023年4月02日 04:12

Hi! Thanks for the great information you have provided! You have touched on crucial points!

Avatar_small
Website 说:
2023年4月07日 05:28

lawyer for bank disputes
Yes, I am totally agreed with this article and I just want to say that this article is a very nice and very informative article. I will make sure to be reading your blog more. You made a good point but I can't help but wonder. what about the other side? Thanks!

Avatar_small
side hustle 说:
2023年4月16日 21:27

This is the first time I am visiting here and I found so much interesting stuff in your blog, especially its discussion. Thank you.

Avatar_small
Website 说:
2023年4月18日 07:04

https://fumigar-plagas-sevilla.es/eliminar-termitas-exterminar-termitas-matar-termitas-en-sevilla/
Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.

Avatar_small
dvmoviemaking 说:
2023年4月23日 02:01

This is the first time I am visiting here and I found so much interesting stuff in your blog, especially its discussion. Thank you.

Avatar_small
infowaveindia 说:
2023年4月23日 13:41

Thank you for the update. Very nice site.

Avatar_small
soundcuesystem 说:
2023年4月24日 16:05

I really appreciate this wonderful post that you have provided for us. I assure you this would be beneficial for most people.

Avatar_small
Website 说:
2023年5月11日 03:24

puzzle games online
Your blog provided us with valuable information to work with. Each & every tip of your post is awesome. Thanks a lot for sharing. Keep blogging.

Avatar_small
GameFi 说:
2023年5月19日 02:25

Wonderful illustrated information. I thank you for that. No doubt it will be very useful for my future projects. Would like to see some other posts on the same subject!

Avatar_small
Website 说:
2023年12月06日 02:41

Kenyan Children's Book Illustrations
Thanks for the blog post buddy! Keep them coming.

Avatar_small
Website 说:
2024年2月10日 00:40

Umbilical Hernia Repair cost
The information you have posted is very useful. The sites you have referred were good. Thanks for sharing.

Avatar_small
Website 说:
2024年2月13日 14:00

حكم تعدين العملات الرقمية بالعراق
Thanks for the post and great tips. Even, I also think that hard work is the most important aspect of getting success.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com