xv6, line by line
kernel/fs.h

kernel/fs.h

C header · 61 lines · annotated 100% · kernel · upstream

Included by 16 fileskernel/bio.c, kernel/console.c, kernel/file.c, kernel/fs.c, kernel/log.c, kernel/pipe.c, kernel/printk.c, kernel/sysfile.c, kernel/virtio_disk.c, kernel/vm.c, mkfs/mkfs.c, user/grind.c, user/init.c, user/ls.c, user/stressfs.c, user/usertests.c
About this file

The on-disk format of xv6’s file system: how the disk is divided into regions (disk layout (xv6 file system)), and the exact byte layout of the structures stored on it (the superblock, the on-disk inode dinode, and the directory entry dirent).

Three different programs must agree on these definitions, so they live in one header:

Changing anything here changes the disk format, and an fs.img made with the old definitions would then be misread.

Read next: kernel/fs.c, which implements files and directories on top of this layout.

1// On-disk file system format.
2// Both the kernel and user programs use this header file.
4#define ROOTINO 1 // root i-number
5#define BSIZE 1024 // block size
7// Disk layout:
8// [ boot block | super block | log | inode blocks |
9// free bit map | data blocks]
10//
11// mkfs computes the super block and builds an initial file system. The
12// super block describes the disk layout:
13struct superblock {
14 uint magic; // Must be FSMAGIC
15 uint size; // Size of file system image (blocks)
16 uint nblocks; // Number of data blocks
17 uint ninodes; // Number of inodes.
18 uint nlog; // Number of log blocks
19 uint logstart; // Block number of first log block
20 uint inodestart; // Block number of first inode block
21 uint bmapstart; // Block number of first free map block
22};
24#define FSMAGIC 0x10203040
26#define NDIRECT 12
27#define NINDIRECT (BSIZE / sizeof(uint))
29#define NLINK_MAX 32767 // nlink is a short; refuse links past its maximum
31// On-disk inode structure
32struct dinode {
33 short type; // File type
34 short major; // Major device number (T_DEVICE only)
35 short minor; // Minor device number (T_DEVICE only)
36 short nlink; // Number of links to inode in file system
37 uint size; // Size of file (bytes)
38 uint addrs[NDIRECT + 1]; // Data block addresses
39};
41// Inodes per block.
42#define IPB (BSIZE / sizeof(struct dinode))
44// Block containing inode i
45#define IBLOCK(i, sb) ((i) / IPB + sb.inodestart)
47// Bitmap bits per block
48#define BPB (BSIZE * 8)
50// Block of free map containing bit for block b
51#define BBLOCK(b, sb) ((b) / BPB + sb.bmapstart)
53// Directory is a file containing a sequence of dirent structures.
54#define DIRSIZ 14
56// The name field may have DIRSIZ characters and not end in a NUL
57// character.
58struct dirent {
60 char name[DIRSIZ] __attribute__((nonstring));
61};