{"id":1007,"date":"2020-08-24T20:17:33","date_gmt":"2020-08-24T21:17:33","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=1007"},"modified":"2020-08-24T20:17:33","modified_gmt":"2020-08-24T21:17:33","slug":"ext2-files","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=1007","title":{"rendered":"EXT2 Files"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Finding a File in an EXT2 File System<\/h3>\n\n\n\n<p>A Linux filename has the same format as all Unix filenames have.<br>It is a series of directory names separated by forward slashes (&#8220;\/&#8221;) and<br>ending in the file&#8217;s name. One example filename would be \/home\/rusling\/.cshrc where \/home and \/rusling are directory names and the file&#8217;s name is .cshrc. Like all other Unix \u00a0systems, Linux does not care about the format of the filename itself; it<br>can be any length and consist of any of the printable characters. To find the inode representing this file within an EXT2 file system the system must parse the filename a directory at a time until we get to the file itself.<\/p>\n\n\n\n<p>The first inode we need is the inode for the root of the file system and we<br>find its number in the file system&#8217;s superblock.<br>To read an EXT2 inode we must look for it in the inode table of the appropriate<br>Block Group.<br>If, for example, the root inode number is 42, then we need the 42nd inode from<br>the inode table of Block Group 0.<br>The root inode is for an EXT2 directory, in other words the mode of the root<br>inode describes it as a directory and it&#8217;s data blocks contain EXT2 directory entries.<\/p>\n\n\n\n<p><br>home is just one of the many directory entries and this directory entry gives us<br>the number of the inode describing the \/home directory.<br>We have to read this directory (by first reading its inode and then<br>reading the directory entries from the data blocks described by its inode)<br>to find the rusling entry which gives us the number of the inode describing the<br>\/home\/rusling directory.<br>Finally we read the directory entries pointed at by the inode describing the<br>\/home\/rusling directory to find the inode number of the .cshrc file and<br>from this we get the data blocks containing the information in the file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Changing the Size of a File in an EXT2 File System<\/h3>\n\n\n\n<p>One common problem with a file system is its tendency to fragment.<br>The blocks that hold the file&#8217;s data get spread all over the file system<br>and this makes sequentially accessing the data blocks of a file more and more<br>inefficient the further apart the data blocks are.<br>The EXT2 file system tries to overcome this by allocating the new blocks for a<br>file physically close to its current data blocks or at least in the same Block Group<br>as its current data blocks.<br>Only when this fails does it allocate data blocks in another Block Group.<\/p>\n\n\n\n<p><br>Whenever a process attempts to write data into a file the Linux file system checks to<br>see if the data has gone off the end of the file&#8217;s last allocated block.<br>If it has, then it must allocate a new data block for this file.<br>Until the allocation is complete, the process cannot run; it must wait for<br>the file system to allocate a new data block and write the rest of the data to it before<br>it can continue.<br>The first thing that the EXT2 block allocation routines do is to lock the<br>EXT2 Superblock for this file system. Allocating and deallocating changes fields<br>within the superblock, and the Linux file system cannot allow more than one process<br>to do this at the same time.<br>If another process needs to allocate more data blocks, it will have to wait until<br>this process has finished.<br>Processes waiting for the superblock are suspended, unable to run, until<br>control of the superblock is relinquished by its current user.<br>Access to the superblock is granted on a first come, first<br>served basis and once a process has control of the superblock, it keeps control<br>until it has finished.<br>Having locked the superblock, the process checks that there are enough free blocks<br>left in this file system. If there are not enough free blocks, then this attempt to<br>allocate more will fail and the process will relinquish control of this file system&#8217;s<br>superblock.<\/p>\n\n\n\n<p><br>If there are enough free blocks in the file system, the process tries to<br>allocate one.<\/p>\n\n\n\n<p><br>If the EXT2 file system has been built to preallocate data blocks then we may be able<br>to take one of those.<br>The preallocated blocks do not actually exist, they are just reserved within the<br>allocated block bitmap.<br>The VFS inode representing the file that we are trying to allocate a new data block<br>for has two EXT2 specific fields, prealloc_block and prealloc_count, which<br>are the block number of the first preallocated data block and how many of them there<br>are, respectively.<br>If there were no preallocated blocks or block preallocation is not enabled, the EXT2<br>file system must allocate a new block.<br>The EXT2 file system first looks to see if the data block after the last data block<br>in the file is free. Logically, this is the most efficient block to allocate as<br>it makes sequential accesses much quicker.<br>If this block is not free, then the search widens and it looks for a data block within<br>64 blocks of the of the ideal block.<br>This block, although not ideal is at least fairly close and within the same Block Group<br>as the other data blocks belonging to this file.<\/p>\n\n\n\n<p><br>If even that block is not free, the process starts looking in all of the<br>other Block Groups in turn until it finds some free blocks.<br>The block allocation code looks for a<br>cluster of eight free data blocks somewhere in one of the Block Groups.<br>If it cannot find eight together, it will settle for less.<br>If block preallocation is wanted and enabled it will update prealloc_block and<br>prealloc_count accordingly.<\/p>\n\n\n\n<p><br>Wherever it finds the free block, the block allocation code updates the Block Group&#8217;s<br>block bitmap and allocates a data buffer in the buffer cache.<br>That data buffer is uniquely identified by the file system&#8217;s supporting device identifier<br>and the block number of the allocated block.<br>The data in the buffer is zero&#8217;d and the buffer is marked as <code>dirty'' to show that it's contents have not been written to the physical disk. Finally, the superblock itself is marked as<\/code>dirty&#8221; to show that it has been changed<br>and it is unlocked.<br>If there were any processes waiting for the superblock, the first one in the queue<br>is allowed to run again and will gain exclusive control of the superblock for its<br>file operations.<br>The process&#8217;s data is written to the new data block and, if that data block is filled,<br>the entire process is repeated and another data block allocated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finding a File in an EXT2 File System A Linux filename has the same format as all Unix filenames have.It is a series of directory names separated by forward slashes (&#8220;\/&#8221;) andending in the file&#8217;s name. One example filename would &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=1007\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1007","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/1007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1007"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/1007\/revisions"}],"predecessor-version":[{"id":1008,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/1007\/revisions\/1008"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}