Reading ext2 superblock in ext2_super_block structure questions

I saw some questions on reading the superblock of the ext2 section, but I have some questions that were not answered in them.
Here they come:
1. Using read () to read from disk into the ext2_super_block structure should require that all fields in the structure be compiled in the order in which they are presented in the code, as well as the need for no structure laying (or proper struct padding ) How is this guaranteed?
2. How does Linux work when trying to read from a device when it is unprivileged? There must be an initial offset for reading (more precisely, a mapping that prohibits access to the first N bytes), because the program I wrote only works when run with root privileges. In any case, how does Linux behave in this situation?
3. Where can I find good documentation on working with ext2 / ext3? So far I have read / usr / include / linux / ext 2_fs.h and some random documents on the Internet, but haven’t found anything yet.

I would also like to hear suggestions / corrections on the code below, which still works fine on my machine (also omitted for brevity, the program prints "ef53"):

int main() {
  int fd;
  char boot[1024];
  struct ext2_super_block super_block;

  fd = open("/dev/sda1", O_RDONLY);

  /* Reads the boot section and the superblock */
  read(fd, boot, 1024);
  read(fd, &super_block, sizeof(struct ext2_super_block));

  /* Prints the Magic Number */
  printf("%x\n", super_block.s_magic);

  close(fd);

  return 0;
}

Thanks in advance.

+3
source share
3 answers

all fields in the structure are compiled in the order in which they are presented in the code

I do not know the C compiler that reorders fields in structures. I do not think this is allowed by the C standard. For alingnemt and padding, look at the declaration of the structure. There are several macros.

Linux , ?

ls:

j@linux:~> ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 15. Mai 17:51 /dev/sda

root disk.

ext2/ext3?

, luke. , , ​​Linux ext2/3.

0

.

  • ext2_super_block struct / . .

  • open() 0, , printf("Error: %s\n", strerror(errno)); . - "Permission denied", sudo chmod 777 /dev/yourdevice, Ubuntu .

  • , , , .

0

;

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <./linux/ext2_fs.h> 
int main(int argc, char *argv[])
{
struct ext2_super_block es;
int f;
char but[1024];

  f = open("/dev/sda1", O_RDONLY);
  read(f, but, 1024);  
  read(f, &es, sizeof(struct ext2_super_block));
  printf("El tamano de int es %d byts\n", sizeof(int));
  printf("El tamano de char es %d byts\n", sizeof(char));
  printf("Número de inodos: %d \n", es.s_inodes_count);
  printf("Número de bloques: %d \n", es.s_blocks_count); 
  printf("Número de bloques libres: %d \n", es.s_free_blocks_count); 
  printf("Número de inodos libres: %d \n", es.s_free_inodes_count);
  printf("Tamano del bloque: %d \n", es.s_log_block_size);
  printf("Bloques en un grupo: %d \n", es.s_blocks_per_group);
  printf("Fragmentos en un grupo: %d \n", es.s_frags_per_group);
  printf("NM: %x\n", es.s_magic);

return (0);
}
0
source

All Articles