btrfs-progs: mkfs: create root directory with 755 permissions
[platform/upstream/btrfs-progs.git] / utils.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 600
20 #define __USE_XOPEN2K
21 #include <stdio.h>
22 #include <stdlib.h>
23 #ifndef __CHECKER__
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
26 #endif
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <uuid/uuid.h>
30 #include <dirent.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <mntent.h>
34 #include <linux/loop.h>
35 #include <linux/major.h>
36 #include <linux/kdev_t.h>
37 #include <limits.h>
38 #include "kerncompat.h"
39 #include "radix-tree.h"
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43 #include "crc32c.h"
44 #include "utils.h"
45 #include "volumes.h"
46 #include "ioctl.h"
47
48 #ifdef __CHECKER__
49 #define BLKGETSIZE64 0
50 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
51 #endif
52
53 #ifndef BLKDISCARD
54 #define BLKDISCARD      _IO(0x12,119)
55 #endif
56
57 static int
58 discard_blocks(int fd, u64 start, u64 len)
59 {
60         u64 range[2] = { start, len };
61
62         if (ioctl(fd, BLKDISCARD, &range) < 0)
63                 return errno;
64         return 0;
65 }
66
67 static u64 reference_root_table[] = {
68         [1] =   BTRFS_ROOT_TREE_OBJECTID,
69         [2] =   BTRFS_EXTENT_TREE_OBJECTID,
70         [3] =   BTRFS_CHUNK_TREE_OBJECTID,
71         [4] =   BTRFS_DEV_TREE_OBJECTID,
72         [5] =   BTRFS_FS_TREE_OBJECTID,
73         [6] =   BTRFS_CSUM_TREE_OBJECTID,
74 };
75
76 int make_btrfs(int fd, const char *device, const char *label,
77                u64 blocks[7], u64 num_bytes, u32 nodesize,
78                u32 leafsize, u32 sectorsize, u32 stripesize)
79 {
80         struct btrfs_super_block super;
81         struct extent_buffer *buf;
82         struct btrfs_root_item root_item;
83         struct btrfs_disk_key disk_key;
84         struct btrfs_extent_item *extent_item;
85         struct btrfs_inode_item *inode_item;
86         struct btrfs_chunk *chunk;
87         struct btrfs_dev_item *dev_item;
88         struct btrfs_dev_extent *dev_extent;
89         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
90         u8 *ptr;
91         int i;
92         int ret;
93         u32 itemoff;
94         u32 nritems = 0;
95         u64 first_free;
96         u64 ref_root;
97         u32 array_size;
98         u32 item_size;
99
100         first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
101         first_free &= ~((u64)sectorsize - 1);
102
103         memset(&super, 0, sizeof(super));
104
105         num_bytes = (num_bytes / sectorsize) * sectorsize;
106         uuid_generate(super.fsid);
107         uuid_generate(super.dev_item.uuid);
108         uuid_generate(chunk_tree_uuid);
109
110         btrfs_set_super_bytenr(&super, blocks[0]);
111         btrfs_set_super_num_devices(&super, 1);
112         strncpy((char *)&super.magic, BTRFS_MAGIC, sizeof(super.magic));
113         btrfs_set_super_generation(&super, 1);
114         btrfs_set_super_root(&super, blocks[1]);
115         btrfs_set_super_chunk_root(&super, blocks[3]);
116         btrfs_set_super_total_bytes(&super, num_bytes);
117         btrfs_set_super_bytes_used(&super, 6 * leafsize);
118         btrfs_set_super_sectorsize(&super, sectorsize);
119         btrfs_set_super_leafsize(&super, leafsize);
120         btrfs_set_super_nodesize(&super, nodesize);
121         btrfs_set_super_stripesize(&super, stripesize);
122         btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
123         btrfs_set_super_chunk_root_generation(&super, 1);
124         btrfs_set_super_cache_generation(&super, -1);
125         if (label)
126                 strncpy(super.label, label, BTRFS_LABEL_SIZE - 1);
127
128         buf = malloc(sizeof(*buf) + max(sectorsize, leafsize));
129
130         /* create the tree of root objects */
131         memset(buf->data, 0, leafsize);
132         buf->len = leafsize;
133         btrfs_set_header_bytenr(buf, blocks[1]);
134         btrfs_set_header_nritems(buf, 4);
135         btrfs_set_header_generation(buf, 1);
136         btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
137         btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
138         write_extent_buffer(buf, super.fsid, (unsigned long)
139                             btrfs_header_fsid(buf), BTRFS_FSID_SIZE);
140
141         write_extent_buffer(buf, chunk_tree_uuid, (unsigned long)
142                             btrfs_header_chunk_tree_uuid(buf),
143                             BTRFS_UUID_SIZE);
144
145         /* create the items for the root tree */
146         memset(&root_item, 0, sizeof(root_item));
147         inode_item = &root_item.inode;
148         btrfs_set_stack_inode_generation(inode_item, 1);
149         btrfs_set_stack_inode_size(inode_item, 3);
150         btrfs_set_stack_inode_nlink(inode_item, 1);
151         btrfs_set_stack_inode_nbytes(inode_item, leafsize);
152         btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
153         btrfs_set_root_refs(&root_item, 1);
154         btrfs_set_root_used(&root_item, leafsize);
155         btrfs_set_root_generation(&root_item, 1);
156
157         memset(&disk_key, 0, sizeof(disk_key));
158         btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
159         btrfs_set_disk_key_offset(&disk_key, 0);
160         nritems = 0;
161
162         itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
163         btrfs_set_root_bytenr(&root_item, blocks[2]);
164         btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
165         btrfs_set_item_key(buf, &disk_key, nritems);
166         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
167         btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
168                             sizeof(root_item));
169         write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
170                             nritems), sizeof(root_item));
171         nritems++;
172
173         itemoff = itemoff - sizeof(root_item);
174         btrfs_set_root_bytenr(&root_item, blocks[4]);
175         btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
176         btrfs_set_item_key(buf, &disk_key, nritems);
177         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
178         btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
179                             sizeof(root_item));
180         write_extent_buffer(buf, &root_item,
181                             btrfs_item_ptr_offset(buf, nritems),
182                             sizeof(root_item));
183         nritems++;
184
185         itemoff = itemoff - sizeof(root_item);
186         btrfs_set_root_bytenr(&root_item, blocks[5]);
187         btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
188         btrfs_set_item_key(buf, &disk_key, nritems);
189         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
190         btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
191                             sizeof(root_item));
192         write_extent_buffer(buf, &root_item,
193                             btrfs_item_ptr_offset(buf, nritems),
194                             sizeof(root_item));
195         nritems++;
196
197         itemoff = itemoff - sizeof(root_item);
198         btrfs_set_root_bytenr(&root_item, blocks[6]);
199         btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
200         btrfs_set_item_key(buf, &disk_key, nritems);
201         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
202         btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
203                             sizeof(root_item));
204         write_extent_buffer(buf, &root_item,
205                             btrfs_item_ptr_offset(buf, nritems),
206                             sizeof(root_item));
207         nritems++;
208
209
210         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
211         ret = pwrite(fd, buf->data, leafsize, blocks[1]);
212         BUG_ON(ret != leafsize);
213
214         /* create the items for the extent tree */
215         memset(buf->data+sizeof(struct btrfs_header), 0,
216                 leafsize-sizeof(struct btrfs_header));
217         nritems = 0;
218         itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize);
219         for (i = 1; i < 7; i++) {
220                 BUG_ON(blocks[i] < first_free);
221                 BUG_ON(blocks[i] < blocks[i - 1]);
222
223                 /* create extent item */
224                 itemoff -= sizeof(struct btrfs_extent_item) +
225                            sizeof(struct btrfs_tree_block_info);
226                 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
227                 btrfs_set_disk_key_offset(&disk_key, leafsize);
228                 btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY);
229                 btrfs_set_item_key(buf, &disk_key, nritems);
230                 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
231                                       itemoff);
232                 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems),
233                                     sizeof(struct btrfs_extent_item) +
234                                     sizeof(struct btrfs_tree_block_info));
235                 extent_item = btrfs_item_ptr(buf, nritems,
236                                              struct btrfs_extent_item);
237                 btrfs_set_extent_refs(buf, extent_item, 1);
238                 btrfs_set_extent_generation(buf, extent_item, 1);
239                 btrfs_set_extent_flags(buf, extent_item,
240                                        BTRFS_EXTENT_FLAG_TREE_BLOCK);
241                 nritems++;
242
243                 /* create extent ref */
244                 ref_root = reference_root_table[i];
245                 btrfs_set_disk_key_objectid(&disk_key, blocks[i]);
246                 btrfs_set_disk_key_offset(&disk_key, ref_root);
247                 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
248                 btrfs_set_item_key(buf, &disk_key, nritems);
249                 btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems),
250                                       itemoff);
251                 btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), 0);
252                 nritems++;
253         }
254         btrfs_set_header_bytenr(buf, blocks[2]);
255         btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
256         btrfs_set_header_nritems(buf, nritems);
257         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
258         ret = pwrite(fd, buf->data, leafsize, blocks[2]);
259         BUG_ON(ret != leafsize);
260
261         /* create the chunk tree */
262         memset(buf->data+sizeof(struct btrfs_header), 0,
263                 leafsize-sizeof(struct btrfs_header));
264         nritems = 0;
265         item_size = sizeof(*dev_item);
266         itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - item_size;
267
268         /* first device 1 (there is no device 0) */
269         btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
270         btrfs_set_disk_key_offset(&disk_key, 1);
271         btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
272         btrfs_set_item_key(buf, &disk_key, nritems);
273         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
274         btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), item_size);
275
276         dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
277         btrfs_set_device_id(buf, dev_item, 1);
278         btrfs_set_device_generation(buf, dev_item, 0);
279         btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
280         btrfs_set_device_bytes_used(buf, dev_item,
281                                     BTRFS_MKFS_SYSTEM_GROUP_SIZE);
282         btrfs_set_device_io_align(buf, dev_item, sectorsize);
283         btrfs_set_device_io_width(buf, dev_item, sectorsize);
284         btrfs_set_device_sector_size(buf, dev_item, sectorsize);
285         btrfs_set_device_type(buf, dev_item, 0);
286
287         write_extent_buffer(buf, super.dev_item.uuid,
288                             (unsigned long)btrfs_device_uuid(dev_item),
289                             BTRFS_UUID_SIZE);
290         write_extent_buffer(buf, super.fsid,
291                             (unsigned long)btrfs_device_fsid(dev_item),
292                             BTRFS_UUID_SIZE);
293         read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
294                            sizeof(*dev_item));
295
296         nritems++;
297         item_size = btrfs_chunk_item_size(1);
298         itemoff = itemoff - item_size;
299
300         /* then we have chunk 0 */
301         btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
302         btrfs_set_disk_key_offset(&disk_key, 0);
303         btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
304         btrfs_set_item_key(buf, &disk_key, nritems);
305         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
306         btrfs_set_item_size(buf, btrfs_item_nr(buf,  nritems), item_size);
307
308         chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
309         btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
310         btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
311         btrfs_set_chunk_stripe_len(buf, chunk, 64 * 1024);
312         btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
313         btrfs_set_chunk_io_align(buf, chunk, sectorsize);
314         btrfs_set_chunk_io_width(buf, chunk, sectorsize);
315         btrfs_set_chunk_sector_size(buf, chunk, sectorsize);
316         btrfs_set_chunk_num_stripes(buf, chunk, 1);
317         btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
318         btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
319         nritems++;
320
321         write_extent_buffer(buf, super.dev_item.uuid,
322                             (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
323                             BTRFS_UUID_SIZE);
324
325         /* copy the key for the chunk to the system array */
326         ptr = super.sys_chunk_array;
327         array_size = sizeof(disk_key);
328
329         memcpy(ptr, &disk_key, sizeof(disk_key));
330         ptr += sizeof(disk_key);
331
332         /* copy the chunk to the system array */
333         read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
334         array_size += item_size;
335         ptr += item_size;
336         btrfs_set_super_sys_array_size(&super, array_size);
337
338         btrfs_set_header_bytenr(buf, blocks[3]);
339         btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
340         btrfs_set_header_nritems(buf, nritems);
341         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
342         ret = pwrite(fd, buf->data, leafsize, blocks[3]);
343
344         /* create the device tree */
345         memset(buf->data+sizeof(struct btrfs_header), 0,
346                 leafsize-sizeof(struct btrfs_header));
347         nritems = 0;
348         itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
349                 sizeof(struct btrfs_dev_extent);
350
351         btrfs_set_disk_key_objectid(&disk_key, 1);
352         btrfs_set_disk_key_offset(&disk_key, 0);
353         btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
354         btrfs_set_item_key(buf, &disk_key, nritems);
355         btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff);
356         btrfs_set_item_size(buf, btrfs_item_nr(buf,  nritems),
357                             sizeof(struct btrfs_dev_extent));
358         dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
359         btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
360                                         BTRFS_CHUNK_TREE_OBJECTID);
361         btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
362                                         BTRFS_FIRST_CHUNK_TREE_OBJECTID);
363         btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
364
365         write_extent_buffer(buf, chunk_tree_uuid,
366                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
367                     BTRFS_UUID_SIZE);
368
369         btrfs_set_dev_extent_length(buf, dev_extent,
370                                     BTRFS_MKFS_SYSTEM_GROUP_SIZE);
371         nritems++;
372
373         btrfs_set_header_bytenr(buf, blocks[4]);
374         btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
375         btrfs_set_header_nritems(buf, nritems);
376         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
377         ret = pwrite(fd, buf->data, leafsize, blocks[4]);
378
379         /* create the FS root */
380         memset(buf->data+sizeof(struct btrfs_header), 0,
381                 leafsize-sizeof(struct btrfs_header));
382         btrfs_set_header_bytenr(buf, blocks[5]);
383         btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
384         btrfs_set_header_nritems(buf, 0);
385         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
386         ret = pwrite(fd, buf->data, leafsize, blocks[5]);
387         BUG_ON(ret != leafsize);
388
389         /* finally create the csum root */
390         memset(buf->data+sizeof(struct btrfs_header), 0,
391                 leafsize-sizeof(struct btrfs_header));
392         btrfs_set_header_bytenr(buf, blocks[6]);
393         btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
394         btrfs_set_header_nritems(buf, 0);
395         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
396         ret = pwrite(fd, buf->data, leafsize, blocks[6]);
397         BUG_ON(ret != leafsize);
398
399         /* and write out the super block */
400         BUG_ON(sizeof(super) > sectorsize);
401         memset(buf->data, 0, sectorsize);
402         memcpy(buf->data, &super, sizeof(super));
403         buf->len = sectorsize;
404         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
405         ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
406         BUG_ON(ret != sectorsize);
407
408
409         free(buf);
410         return 0;
411 }
412
413 static u64 device_size(int fd, struct stat *st)
414 {
415         u64 size;
416         if (S_ISREG(st->st_mode)) {
417                 return st->st_size;
418         }
419         if (!S_ISBLK(st->st_mode)) {
420                 return 0;
421         }
422         if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
423                 return size;
424         }
425         return 0;
426 }
427
428 static int zero_blocks(int fd, off_t start, size_t len)
429 {
430         char *buf = malloc(len);
431         int ret = 0;
432         ssize_t written;
433
434         if (!buf)
435                 return -ENOMEM;
436         memset(buf, 0, len);
437         written = pwrite(fd, buf, len, start);
438         if (written != len)
439                 ret = -EIO;
440         free(buf);
441         return ret;
442 }
443
444 static int zero_dev_start(int fd)
445 {
446         off_t start = 0;
447         size_t len = 2 * 1024 * 1024;
448
449 #ifdef __sparc__
450         /* don't overwrite the disk labels on sparc */
451         start = 1024;
452         len -= 1024;
453 #endif
454         return zero_blocks(fd, start, len);
455 }
456
457 static int zero_dev_end(int fd, u64 dev_size)
458 {
459         size_t len = 2 * 1024 * 1024;
460         off_t start = dev_size - len;
461
462         return zero_blocks(fd, start, len);
463 }
464
465 int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
466                       struct btrfs_root *root, int fd, char *path,
467                       u64 block_count, u32 io_width, u32 io_align,
468                       u32 sectorsize)
469 {
470         struct btrfs_super_block *disk_super;
471         struct btrfs_super_block *super = &root->fs_info->super_copy;
472         struct btrfs_device *device;
473         struct btrfs_dev_item *dev_item;
474         char *buf;
475         u64 total_bytes;
476         u64 num_devs;
477         int ret;
478
479         device = kmalloc(sizeof(*device), GFP_NOFS);
480         if (!device)
481                 return -ENOMEM;
482         buf = kmalloc(sectorsize, GFP_NOFS);
483         if (!buf) {
484                 kfree(device);
485                 return -ENOMEM;
486         }
487         BUG_ON(sizeof(*disk_super) > sectorsize);
488         memset(buf, 0, sectorsize);
489
490         disk_super = (struct btrfs_super_block *)buf;
491         dev_item = &disk_super->dev_item;
492
493         uuid_generate(device->uuid);
494         device->devid = 0;
495         device->type = 0;
496         device->io_width = io_width;
497         device->io_align = io_align;
498         device->sector_size = sectorsize;
499         device->fd = fd;
500         device->writeable = 1;
501         device->total_bytes = block_count;
502         device->bytes_used = 0;
503         device->total_ios = 0;
504         device->dev_root = root->fs_info->dev_root;
505
506         ret = btrfs_add_device(trans, root, device);
507         BUG_ON(ret);
508
509         total_bytes = btrfs_super_total_bytes(super) + block_count;
510         btrfs_set_super_total_bytes(super, total_bytes);
511
512         num_devs = btrfs_super_num_devices(super) + 1;
513         btrfs_set_super_num_devices(super, num_devs);
514
515         memcpy(disk_super, super, sizeof(*disk_super));
516
517         printf("adding device %s id %llu\n", path,
518                (unsigned long long)device->devid);
519
520         btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
521         btrfs_set_stack_device_id(dev_item, device->devid);
522         btrfs_set_stack_device_type(dev_item, device->type);
523         btrfs_set_stack_device_io_align(dev_item, device->io_align);
524         btrfs_set_stack_device_io_width(dev_item, device->io_width);
525         btrfs_set_stack_device_sector_size(dev_item, device->sector_size);
526         btrfs_set_stack_device_total_bytes(dev_item, device->total_bytes);
527         btrfs_set_stack_device_bytes_used(dev_item, device->bytes_used);
528         memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
529
530         ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
531         BUG_ON(ret != sectorsize);
532
533         kfree(buf);
534         list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
535         device->fs_devices = root->fs_info->fs_devices;
536         return 0;
537 }
538
539 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
540                          int *mixed)
541 {
542         /* discard by default when called from 'device add' */
543         return __btrfs_prepare_device(fd, file, zero_end, block_count_ret, mixed, 0);
544 }
545 int __btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
546                          int *mixed, int nodiscard)
547 {
548         u64 block_count;
549         u64 bytenr;
550         struct stat st;
551         int i, ret;
552
553         ret = fstat(fd, &st);
554         if (ret < 0) {
555                 fprintf(stderr, "unable to stat %s\n", file);
556                 exit(1);
557         }
558
559         block_count = device_size(fd, &st);
560         if (block_count == 0) {
561                 fprintf(stderr, "unable to find %s size\n", file);
562                 exit(1);
563         }
564         if (*block_count_ret)
565                 block_count = min(block_count, *block_count_ret);
566         zero_end = 1;
567
568         if (block_count < 1024 * 1024 * 1024 && !(*mixed)) {
569                 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
570                 *mixed = 1;
571         }
572
573         if (!nodiscard) {
574                 /*
575                  * We intentionally ignore errors from the discard ioctl.  It is
576                  * not necessary for the mkfs functionality but just an optimization.
577                  */
578                 discard_blocks(fd, 0, block_count);
579         }
580
581         ret = zero_dev_start(fd);
582         if (ret) {
583                 fprintf(stderr, "failed to zero device start %d\n", ret);
584                 exit(1);
585         }
586
587         for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
588                 bytenr = btrfs_sb_offset(i);
589                 if (bytenr >= block_count)
590                         break;
591                 zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
592         }
593
594         if (zero_end) {
595                 ret = zero_dev_end(fd, block_count);
596                 if (ret) {
597                         fprintf(stderr, "failed to zero device end %d\n", ret);
598                         exit(1);
599                 }
600         }
601         *block_count_ret = block_count;
602         return 0;
603 }
604
605 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
606                         struct btrfs_root *root, u64 objectid)
607 {
608         int ret;
609         struct btrfs_inode_item inode_item;
610         time_t now = time(NULL);
611
612         memset(&inode_item, 0, sizeof(inode_item));
613         btrfs_set_stack_inode_generation(&inode_item, trans->transid);
614         btrfs_set_stack_inode_size(&inode_item, 0);
615         btrfs_set_stack_inode_nlink(&inode_item, 1);
616         btrfs_set_stack_inode_nbytes(&inode_item, root->leafsize);
617         btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
618         btrfs_set_stack_timespec_sec(&inode_item.atime, now);
619         btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
620         btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
621         btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
622         btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
623         btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
624         btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
625         btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
626
627         if (root->fs_info->tree_root == root)
628                 btrfs_set_super_root_dir(&root->fs_info->super_copy, objectid);
629
630         ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
631         if (ret)
632                 goto error;
633
634         ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
635         if (ret)
636                 goto error;
637
638         btrfs_set_root_dirid(&root->root_item, objectid);
639         ret = 0;
640 error:
641         return ret;
642 }
643
644 /* checks if a device is a loop device */
645 int is_loop_device (const char* device) {
646         struct stat statbuf;
647
648         if(stat(device, &statbuf) < 0)
649                 return -errno;
650
651         return (S_ISBLK(statbuf.st_mode) &&
652                 MAJOR(statbuf.st_rdev) == LOOP_MAJOR);
653 }
654
655
656 /* Takes a loop device path (e.g. /dev/loop0) and returns
657  * the associated file (e.g. /images/my_btrfs.img) */
658 int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
659 {
660         int loop_fd;
661         int ret_ioctl;
662         struct loop_info loopinfo;
663
664         if ((loop_fd = open(loop_dev, O_RDONLY)) < 0)
665                 return -errno;
666
667         ret_ioctl = ioctl(loop_fd, LOOP_GET_STATUS, &loopinfo);
668         close(loop_fd);
669
670         if (ret_ioctl == 0) {
671                 strncpy(loop_file, loopinfo.lo_name, max_len);
672                 if (max_len > 0)
673                         loop_file[max_len-1] = 0;
674         } else
675                 return -errno;
676
677         return 0;
678 }
679
680 /* Checks whether a and b are identical or device
681  * files associated with the same block device
682  */
683 int is_same_blk_file(const char* a, const char* b)
684 {
685         struct stat st_buf_a, st_buf_b;
686         char real_a[PATH_MAX];
687         char real_b[PATH_MAX];
688
689         if(!realpath(a, real_a) ||
690            !realpath(b, real_b))
691         {
692                 return -errno;
693         }
694
695         /* Identical path? */
696         if(strcmp(real_a, real_b) == 0)
697                 return 1;
698
699         if(stat(a, &st_buf_a) < 0 ||
700            stat(b, &st_buf_b) < 0)
701         {
702                 if (errno == ENOENT)
703                         return 0;
704                 return -errno;
705         }
706
707         /* Same blockdevice? */
708         if(S_ISBLK(st_buf_a.st_mode) &&
709            S_ISBLK(st_buf_b.st_mode) &&
710            st_buf_a.st_rdev == st_buf_b.st_rdev)
711         {
712                 return 1;
713         }
714
715         /* Hardlink? */
716         if (st_buf_a.st_dev == st_buf_b.st_dev &&
717             st_buf_a.st_ino == st_buf_b.st_ino)
718         {
719                 return 1;
720         }
721
722         return 0;
723 }
724
725 /* checks if a and b are identical or device
726  * files associated with the same block device or
727  * if one file is a loop device that uses the other
728  * file.
729  */
730 int is_same_loop_file(const char* a, const char* b)
731 {
732         char res_a[PATH_MAX];
733         char res_b[PATH_MAX];
734         const char* final_a;
735         const char* final_b;
736         int ret;
737
738         /* Resolve a if it is a loop device */
739         if((ret = is_loop_device(a)) < 0) {
740                 if (ret == -ENOENT)
741                         return 0;
742                 return ret;
743         } else if (ret) {
744                 if ((ret = resolve_loop_device(a, res_a, sizeof(res_a))) < 0)
745                         return ret;
746
747                 final_a = res_a;
748         } else {
749                 final_a = a;
750         }
751
752         /* Resolve b if it is a loop device */
753         if ((ret = is_loop_device(b)) < 0) {
754                 if (ret == -ENOENT)
755                         return 0;
756                 return ret;
757         } else if (ret) {
758                 if((ret = resolve_loop_device(b, res_b, sizeof(res_b))) < 0)
759                         return ret;
760
761                 final_b = res_b;
762         } else {
763                 final_b = b;
764         }
765
766         return is_same_blk_file(final_a, final_b);
767 }
768
769 /* Checks if a file exists and is a block or regular file*/
770 int is_existing_blk_or_reg_file(const char* filename)
771 {
772         struct stat st_buf;
773
774         if(stat(filename, &st_buf) < 0) {
775                 if(errno == ENOENT)
776                         return 0;
777                 else
778                         return -errno;
779         }
780
781         return (S_ISBLK(st_buf.st_mode) || S_ISREG(st_buf.st_mode));
782 }
783
784 /* Checks if a file is used (directly or indirectly via a loop device)
785  * by a device in fs_devices
786  */
787 int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices, const char* file)
788 {
789         int ret;
790         struct list_head *head;
791         struct list_head *cur;
792         struct btrfs_device *device;
793
794         head = &fs_devices->devices;
795         list_for_each(cur, head) {
796                 device = list_entry(cur, struct btrfs_device, dev_list);
797
798                 if((ret = is_same_loop_file(device->name, file)))
799                         return ret;
800         }
801
802         return 0;
803 }
804
805 /*
806  * returns 1 if the device was mounted, < 0 on error or 0 if everything
807  * is safe to continue.
808  */
809 int check_mounted(const char* file)
810 {
811         int fd;
812         int ret;
813
814         fd = open(file, O_RDONLY);
815         if (fd < 0) {
816                 fprintf (stderr, "check_mounted(): Could not open %s\n", file);
817                 return -errno;
818         }
819
820         ret =  check_mounted_where(fd, file, NULL, 0, NULL);
821         close(fd);
822
823         return ret;
824 }
825
826 int check_mounted_where(int fd, const char *file, char *where, int size,
827                         struct btrfs_fs_devices **fs_dev_ret)
828 {
829         int ret;
830         u64 total_devs = 1;
831         int is_btrfs;
832         struct btrfs_fs_devices *fs_devices_mnt = NULL;
833         FILE *f;
834         struct mntent *mnt;
835
836         /* scan the initial device */
837         ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
838                                     &total_devs, BTRFS_SUPER_INFO_OFFSET);
839         is_btrfs = (ret >= 0);
840
841         /* scan other devices */
842         if (is_btrfs && total_devs > 1) {
843                 if((ret = btrfs_scan_for_fsid(fs_devices_mnt, total_devs, 1)))
844                         return ret;
845         }
846
847         /* iterate over the list of currently mountes filesystems */
848         if ((f = setmntent ("/proc/mounts", "r")) == NULL)
849                 return -errno;
850
851         while ((mnt = getmntent (f)) != NULL) {
852                 if(is_btrfs) {
853                         if(strcmp(mnt->mnt_type, "btrfs") != 0)
854                                 continue;
855
856                         ret = blk_file_in_dev_list(fs_devices_mnt, mnt->mnt_fsname);
857                 } else {
858                         /* ignore entries in the mount table that are not
859                            associated with a file*/
860                         if((ret = is_existing_blk_or_reg_file(mnt->mnt_fsname)) < 0)
861                                 goto out_mntloop_err;
862                         else if(!ret)
863                                 continue;
864
865                         ret = is_same_loop_file(file, mnt->mnt_fsname);
866                 }
867
868                 if(ret < 0)
869                         goto out_mntloop_err;
870                 else if(ret)
871                         break;
872         }
873
874         /* Did we find an entry in mnt table? */
875         if (mnt && size && where) {
876                 strncpy(where, mnt->mnt_dir, size);
877                 where[size-1] = 0;
878         }
879         if (fs_dev_ret)
880                 *fs_dev_ret = fs_devices_mnt;
881
882         ret = (mnt != NULL);
883
884 out_mntloop_err:
885         endmntent (f);
886
887         return ret;
888 }
889
890 /* Gets the mount point of btrfs filesystem that is using the specified device.
891  * Returns 0 is everything is good, <0 if we have an error.
892  * TODO: Fix this fucntion and check_mounted to work with multiple drive BTRFS
893  * setups.
894  */
895 int get_mountpt(char *dev, char *mntpt, size_t size)
896 {
897        struct mntent *mnt;
898        FILE *f;
899        int ret = 0;
900
901        f = setmntent("/proc/mounts", "r");
902        if (f == NULL)
903                return -errno;
904
905        while ((mnt = getmntent(f)) != NULL )
906        {
907                if (strcmp(dev, mnt->mnt_fsname) == 0)
908                {
909                        strncpy(mntpt, mnt->mnt_dir, size);
910                        if (size)
911                                 mntpt[size-1] = 0;
912                        break;
913                }
914        }
915
916        if (mnt == NULL)
917        {
918                /* We didn't find an entry so lets report an error */
919                ret = -1;
920        }
921
922        return ret;
923 }
924
925 struct pending_dir {
926         struct list_head list;
927         char name[256];
928 };
929
930 void btrfs_register_one_device(char *fname)
931 {
932         struct btrfs_ioctl_vol_args args;
933         int fd;
934         int ret;
935         int e;
936
937         fd = open("/dev/btrfs-control", O_RDONLY);
938         if (fd < 0) {
939                 fprintf(stderr, "failed to open /dev/btrfs-control "
940                         "skipping device registration\n");
941                 return;
942         }
943         strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
944         args.name[BTRFS_PATH_NAME_MAX-1] = 0;
945         ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
946         e = errno;
947         if(ret<0){
948                 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
949                         fname, strerror(e));
950         }
951         close(fd);
952 }
953
954 int btrfs_scan_one_dir(char *dirname, int run_ioctl)
955 {
956         DIR *dirp = NULL;
957         struct dirent *dirent;
958         struct pending_dir *pending;
959         struct stat st;
960         int ret;
961         int fd;
962         int dirname_len;
963         int pathlen;
964         char *fullpath;
965         struct list_head pending_list;
966         struct btrfs_fs_devices *tmp_devices;
967         u64 num_devices;
968
969         INIT_LIST_HEAD(&pending_list);
970
971         pending = malloc(sizeof(*pending));
972         if (!pending)
973                 return -ENOMEM;
974         strcpy(pending->name, dirname);
975
976 again:
977         dirname_len = strlen(pending->name);
978         pathlen = 1024;
979         fullpath = malloc(pathlen);
980         dirname = pending->name;
981
982         if (!fullpath) {
983                 ret = -ENOMEM;
984                 goto fail;
985         }
986         dirp = opendir(dirname);
987         if (!dirp) {
988                 fprintf(stderr, "Unable to open %s for scanning\n", dirname);
989                 return -ENOENT;
990         }
991         while(1) {
992                 dirent = readdir(dirp);
993                 if (!dirent)
994                         break;
995                 if (dirent->d_name[0] == '.')
996                         continue;
997                 if (dirname_len + strlen(dirent->d_name) + 2 > pathlen) {
998                         ret = -EFAULT;
999                         goto fail;
1000                 }
1001                 snprintf(fullpath, pathlen, "%s/%s", dirname, dirent->d_name);
1002                 ret = lstat(fullpath, &st);
1003                 if (ret < 0) {
1004                         fprintf(stderr, "failed to stat %s\n", fullpath);
1005                         continue;
1006                 }
1007                 if (S_ISLNK(st.st_mode))
1008                         continue;
1009                 if (S_ISDIR(st.st_mode)) {
1010                         struct pending_dir *next = malloc(sizeof(*next));
1011                         if (!next) {
1012                                 ret = -ENOMEM;
1013                                 goto fail;
1014                         }
1015                         strcpy(next->name, fullpath);
1016                         list_add_tail(&next->list, &pending_list);
1017                 }
1018                 if (!S_ISBLK(st.st_mode)) {
1019                         continue;
1020                 }
1021                 fd = open(fullpath, O_RDONLY);
1022                 if (fd < 0) {
1023                         fprintf(stderr, "failed to read %s: %s\n", fullpath,
1024                                         strerror(errno));
1025                         continue;
1026                 }
1027                 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1028                                             &num_devices,
1029                                             BTRFS_SUPER_INFO_OFFSET);
1030                 if (ret == 0 && run_ioctl > 0) {
1031                         btrfs_register_one_device(fullpath);
1032                 }
1033                 close(fd);
1034         }
1035         if (!list_empty(&pending_list)) {
1036                 free(pending);
1037                 pending = list_entry(pending_list.next, struct pending_dir,
1038                                      list);
1039                 list_del(&pending->list);
1040                 closedir(dirp);
1041                 dirp = NULL;
1042                 goto again;
1043         }
1044         ret = 0;
1045 fail:
1046         free(pending);
1047         if (dirp)
1048                 closedir(dirp);
1049         return ret;
1050 }
1051
1052 int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
1053                         int run_ioctls)
1054 {
1055         int ret;
1056
1057         ret = btrfs_scan_block_devices(run_ioctls);
1058         if (ret)
1059                 ret = btrfs_scan_one_dir("/dev", run_ioctls);
1060         return ret;
1061 }
1062
1063 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
1064                                  int super_offset)
1065 {
1066         struct btrfs_super_block *disk_super;
1067         char *buf;
1068         int ret = 0;
1069
1070         buf = malloc(BTRFS_SUPER_INFO_SIZE);
1071         if (!buf) {
1072                 ret = -ENOMEM;
1073                 goto out;
1074         }
1075         ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, super_offset);
1076         if (ret != BTRFS_SUPER_INFO_SIZE)
1077                 goto brelse;
1078
1079         ret = 0;
1080         disk_super = (struct btrfs_super_block *)buf;
1081         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1082             sizeof(disk_super->magic)))
1083                 goto brelse;
1084
1085         if (!memcmp(disk_super->fsid, root->fs_info->super_copy.fsid,
1086                     BTRFS_FSID_SIZE))
1087                 ret = 1;
1088 brelse:
1089         free(buf);
1090 out:
1091         return ret;
1092 }
1093
1094 static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
1095                             "PB", "EB", "ZB", "YB"};
1096 char *pretty_sizes(u64 size)
1097 {
1098         int num_divs = 0;
1099         int pretty_len = 16;
1100         u64 last_size = size;
1101         u64 fract_size = size;
1102         float fraction;
1103         char *pretty;
1104
1105         while(size > 0) {
1106                 fract_size = last_size;
1107                 last_size = size;
1108                 size /= 1024;
1109                 num_divs++;
1110         }
1111         if (num_divs == 0)
1112                 num_divs = 1;
1113         if (num_divs > ARRAY_SIZE(size_strs))
1114                 return NULL;
1115
1116         fraction = (float)fract_size / 1024;
1117         pretty = malloc(pretty_len);
1118         snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs-1]);
1119         return pretty;
1120 }
1121
1122 /*
1123  * Checks to make sure that the label matches our requirements.
1124  * Returns:
1125        0    if everything is safe and usable
1126       -1    if the label is too long
1127       -2    if the label contains an invalid character
1128  */
1129 int check_label(char *input)
1130 {
1131        int i;
1132        int len = strlen(input);
1133
1134        if (len > BTRFS_LABEL_SIZE) {
1135                return -1;
1136        }
1137
1138        for (i = 0; i < len; i++) {
1139                if (input[i] == '/' || input[i] == '\\') {
1140                        return -2;
1141                }
1142        }
1143
1144        return 0;
1145 }
1146
1147 int btrfs_scan_block_devices(int run_ioctl)
1148 {
1149
1150         struct stat st;
1151         int ret;
1152         int fd;
1153         struct btrfs_fs_devices *tmp_devices;
1154         u64 num_devices;
1155         FILE *proc_partitions;
1156         int i;
1157         char buf[1024];
1158         char fullpath[110];
1159         int scans = 0;
1160         int special;
1161
1162 scan_again:
1163         proc_partitions = fopen("/proc/partitions","r");
1164         if (!proc_partitions) {
1165                 fprintf(stderr, "Unable to open '/proc/partitions' for scanning\n");
1166                 return -ENOENT;
1167         }
1168         /* skip the header */
1169         for(i=0; i < 2 ; i++)
1170                 if(!fgets(buf, 1023, proc_partitions)){
1171                 fprintf(stderr, "Unable to read '/proc/partitions' for scanning\n");
1172                 fclose(proc_partitions);
1173                 return -ENOENT;
1174         }
1175
1176         strcpy(fullpath,"/dev/");
1177         while(fgets(buf, 1023, proc_partitions)) {
1178                 i = sscanf(buf," %*d %*d %*d %99s", fullpath+5);
1179
1180                 /*
1181                  * multipath and MD devices may register as a btrfs filesystem
1182                  * both through the original block device and through
1183                  * the special (/dev/mapper or /dev/mdX) entry.
1184                  * This scans the special entries last
1185                  */
1186                 special = strncmp(fullpath, "/dev/dm-", strlen("/dev/dm-")) == 0;
1187                 if (!special)
1188                         special = strncmp(fullpath, "/dev/md", strlen("/dev/md")) == 0;
1189
1190                 if (scans == 0 && special)
1191                         continue;
1192                 if (scans > 0 && !special)
1193                         continue;
1194
1195                 ret = lstat(fullpath, &st);
1196                 if (ret < 0) {
1197                         fprintf(stderr, "failed to stat %s\n", fullpath);
1198                         continue;
1199                 }
1200                 if (!S_ISBLK(st.st_mode)) {
1201                         continue;
1202                 }
1203
1204                 fd = open(fullpath, O_RDONLY);
1205                 if (fd < 0) {
1206                         fprintf(stderr, "failed to read %s\n", fullpath);
1207                         continue;
1208                 }
1209                 ret = btrfs_scan_one_device(fd, fullpath, &tmp_devices,
1210                                             &num_devices,
1211                                             BTRFS_SUPER_INFO_OFFSET);
1212                 if (ret == 0 && run_ioctl > 0) {
1213                         btrfs_register_one_device(fullpath);
1214                 }
1215                 close(fd);
1216         }
1217
1218         fclose(proc_partitions);
1219
1220         if (scans == 0) {
1221                 scans++;
1222                 goto scan_again;
1223         }
1224         return 0;
1225 }
1226