aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/mkfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/mkfs.c')
-rw-r--r--fs/btrfs/mkfs.c44
1 files changed, 37 insertions, 7 deletions
diff --git a/fs/btrfs/mkfs.c b/fs/btrfs/mkfs.c
index 9aa900811c33..1cac5ab114dd 100644
--- a/fs/btrfs/mkfs.c
+++ b/fs/btrfs/mkfs.c
@@ -42,7 +42,7 @@ int mkfs(int fd, u64 num_blocks, u32 blocksize)
42 btrfs_set_header_parentid(&empty_leaf->header, 42 btrfs_set_header_parentid(&empty_leaf->header,
43 BTRFS_ROOT_TREE_OBJECTID); 43 BTRFS_ROOT_TREE_OBJECTID);
44 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1); 44 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1);
45 btrfs_set_header_nritems(&empty_leaf->header, 2); 45 btrfs_set_header_nritems(&empty_leaf->header, 3);
46 46
47 /* create the items for the root tree */ 47 /* create the items for the root tree */
48 btrfs_set_root_blocknr(&root_item, start_block + 2); 48 btrfs_set_root_blocknr(&root_item, start_block + 2);
@@ -61,17 +61,25 @@ int mkfs(int fd, u64 num_blocks, u32 blocksize)
61 btrfs_set_root_blocknr(&root_item, start_block + 3); 61 btrfs_set_root_blocknr(&root_item, start_block + 3);
62 itemoff = itemoff - sizeof(root_item); 62 itemoff = itemoff - sizeof(root_item);
63 btrfs_set_item_offset(&item, itemoff); 63 btrfs_set_item_offset(&item, itemoff);
64 btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID); 64 btrfs_set_disk_key_objectid(&item.key, BTRFS_INODE_MAP_OBJECTID);
65 memcpy(empty_leaf->items + 1, &item, sizeof(item)); 65 memcpy(empty_leaf->items + 1, &item, sizeof(item));
66 memcpy(btrfs_leaf_data(empty_leaf) + itemoff, 66 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
67 &root_item, sizeof(root_item)); 67 &root_item, sizeof(root_item));
68
69 btrfs_set_root_blocknr(&root_item, start_block + 4);
70 itemoff = itemoff - sizeof(root_item);
71 btrfs_set_item_offset(&item, itemoff);
72 btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
73 memcpy(empty_leaf->items + 2, &item, sizeof(item));
74 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
75 &root_item, sizeof(root_item));
68 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize); 76 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize);
69 77
70 /* create the items for the extent tree */ 78 /* create the items for the extent tree */
71 btrfs_set_header_parentid(&empty_leaf->header, 79 btrfs_set_header_parentid(&empty_leaf->header,
72 BTRFS_EXTENT_TREE_OBJECTID); 80 BTRFS_EXTENT_TREE_OBJECTID);
73 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2); 81 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2);
74 btrfs_set_header_nritems(&empty_leaf->header, 4); 82 btrfs_set_header_nritems(&empty_leaf->header, 5);
75 83
76 /* item1, reserve blocks 0-16 */ 84 /* item1, reserve blocks 0-16 */
77 btrfs_set_disk_key_objectid(&item.key, 0); 85 btrfs_set_disk_key_objectid(&item.key, 0);
@@ -108,12 +116,12 @@ int mkfs(int fd, u64 num_blocks, u32 blocksize)
108 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item), 116 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
109 &extent_item, btrfs_item_size(&item)); 117 &extent_item, btrfs_item_size(&item));
110 118
111 /* item4, give block 19 to the FS root */ 119 /* item4, give block 19 to the inode map */
112 btrfs_set_disk_key_objectid(&item.key, start_block + 3); 120 btrfs_set_disk_key_objectid(&item.key, start_block + 3);
113 btrfs_set_disk_key_offset(&item.key, 1); 121 btrfs_set_disk_key_offset(&item.key, 1);
114 itemoff = itemoff - sizeof(struct btrfs_extent_item); 122 itemoff = itemoff - sizeof(struct btrfs_extent_item);
115 btrfs_set_item_offset(&item, itemoff); 123 btrfs_set_item_offset(&item, itemoff);
116 btrfs_set_extent_owner(&extent_item, BTRFS_FS_TREE_OBJECTID); 124 btrfs_set_extent_owner(&extent_item, BTRFS_INODE_MAP_OBJECTID);
117 memcpy(empty_leaf->items + 3, &item, sizeof(item)); 125 memcpy(empty_leaf->items + 3, &item, sizeof(item));
118 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item), 126 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
119 &extent_item, btrfs_item_size(&item)); 127 &extent_item, btrfs_item_size(&item));
@@ -121,12 +129,34 @@ int mkfs(int fd, u64 num_blocks, u32 blocksize)
121 if (ret != blocksize) 129 if (ret != blocksize)
122 return -1; 130 return -1;
123 131
124 /* finally create the FS root */ 132 /* item5, give block 20 to the FS root */
125 btrfs_set_header_parentid(&empty_leaf->header, BTRFS_FS_TREE_OBJECTID); 133 btrfs_set_disk_key_objectid(&item.key, start_block + 4);
134 btrfs_set_disk_key_offset(&item.key, 1);
135 itemoff = itemoff - sizeof(struct btrfs_extent_item);
136 btrfs_set_item_offset(&item, itemoff);
137 btrfs_set_extent_owner(&extent_item, BTRFS_FS_TREE_OBJECTID);
138 memcpy(empty_leaf->items + 4, &item, sizeof(item));
139 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
140 &extent_item, btrfs_item_size(&item));
141 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
142 if (ret != blocksize)
143 return -1;
144
145 /* create the inode map */
146 btrfs_set_header_parentid(&empty_leaf->header,
147 BTRFS_INODE_MAP_OBJECTID);
126 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3); 148 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3);
127 btrfs_set_header_nritems(&empty_leaf->header, 0); 149 btrfs_set_header_nritems(&empty_leaf->header, 0);
128 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize); 150 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize);
129 if (ret != blocksize) 151 if (ret != blocksize)
130 return -1; 152 return -1;
153
154 /* finally create the FS root */
155 btrfs_set_header_parentid(&empty_leaf->header, BTRFS_FS_TREE_OBJECTID);
156 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 4);
157 btrfs_set_header_nritems(&empty_leaf->header, 0);
158 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 4) * blocksize);
159 if (ret != blocksize)
160 return -1;
131 return 0; 161 return 0;
132} 162}