2a415ae931a6e03c605de8712ba7a3f817f8d712
[platform/upstream/btrfs-progs.git] / mkfs / main.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 #include "kerncompat.h"
20 #include "androidcompat.h"
21
22 #include <sys/ioctl.h>
23 #include <sys/mount.h>
24 #include "ioctl.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 /* #include <sys/dir.h> included via androidcompat.h */
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <getopt.h>
31 #include <uuid/uuid.h>
32 #include <ctype.h>
33 #include <blkid/blkid.h>
34 #include <ftw.h>
35 #include "ctree.h"
36 #include "disk-io.h"
37 #include "volumes.h"
38 #include "transaction.h"
39 #include "utils.h"
40 #include "list_sort.h"
41 #include "help.h"
42 #include "mkfs/common.h"
43 #include "mkfs/rootdir.h"
44 #include "fsfeatures.h"
45
46 static int verbose = 1;
47
48 struct mkfs_allocation {
49         u64 data;
50         u64 metadata;
51         u64 mixed;
52         u64 system;
53 };
54
55 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
56                                 struct mkfs_allocation *allocation)
57 {
58         struct btrfs_fs_info *fs_info = root->fs_info;
59         struct btrfs_trans_handle *trans;
60         u64 bytes_used;
61         u64 chunk_start = 0;
62         u64 chunk_size = 0;
63         int ret;
64
65         trans = btrfs_start_transaction(root, 1);
66         BUG_ON(IS_ERR(trans));
67         bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
68
69         root->fs_info->system_allocs = 1;
70         ret = btrfs_make_block_group(trans, fs_info, bytes_used,
71                                      BTRFS_BLOCK_GROUP_SYSTEM,
72                                      BTRFS_FIRST_CHUNK_TREE_OBJECTID,
73                                      0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
74         allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
75         if (ret)
76                 return ret;
77
78         if (mixed) {
79                 ret = btrfs_alloc_chunk(trans, fs_info,
80                                         &chunk_start, &chunk_size,
81                                         BTRFS_BLOCK_GROUP_METADATA |
82                                         BTRFS_BLOCK_GROUP_DATA);
83                 if (ret == -ENOSPC) {
84                         error("no space to allocate data/metadata chunk");
85                         goto err;
86                 }
87                 if (ret)
88                         return ret;
89                 ret = btrfs_make_block_group(trans, fs_info, 0,
90                                              BTRFS_BLOCK_GROUP_METADATA |
91                                              BTRFS_BLOCK_GROUP_DATA,
92                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
93                                              chunk_start, chunk_size);
94                 if (ret)
95                         return ret;
96                 allocation->mixed += chunk_size;
97         } else {
98                 ret = btrfs_alloc_chunk(trans, fs_info,
99                                         &chunk_start, &chunk_size,
100                                         BTRFS_BLOCK_GROUP_METADATA);
101                 if (ret == -ENOSPC) {
102                         error("no space to allocate metadata chunk");
103                         goto err;
104                 }
105                 if (ret)
106                         return ret;
107                 ret = btrfs_make_block_group(trans, fs_info, 0,
108                                              BTRFS_BLOCK_GROUP_METADATA,
109                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
110                                              chunk_start, chunk_size);
111                 allocation->metadata += chunk_size;
112                 if (ret)
113                         return ret;
114         }
115
116         root->fs_info->system_allocs = 0;
117         ret = btrfs_commit_transaction(trans, root);
118
119 err:
120         return ret;
121 }
122
123 static int create_data_block_groups(struct btrfs_trans_handle *trans,
124                 struct btrfs_root *root, int mixed,
125                 struct mkfs_allocation *allocation)
126 {
127         struct btrfs_fs_info *fs_info = root->fs_info;
128         u64 chunk_start = 0;
129         u64 chunk_size = 0;
130         int ret = 0;
131
132         if (!mixed) {
133                 ret = btrfs_alloc_chunk(trans, fs_info,
134                                         &chunk_start, &chunk_size,
135                                         BTRFS_BLOCK_GROUP_DATA);
136                 if (ret == -ENOSPC) {
137                         error("no space to allocate data chunk");
138                         goto err;
139                 }
140                 if (ret)
141                         return ret;
142                 ret = btrfs_make_block_group(trans, fs_info, 0,
143                                              BTRFS_BLOCK_GROUP_DATA,
144                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
145                                              chunk_start, chunk_size);
146                 allocation->data += chunk_size;
147                 if (ret)
148                         return ret;
149         }
150
151 err:
152         return ret;
153 }
154
155 static int make_root_dir(struct btrfs_trans_handle *trans,
156                 struct btrfs_root *root)
157 {
158         struct btrfs_key location;
159         int ret;
160
161         ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
162                               BTRFS_ROOT_TREE_DIR_OBJECTID);
163         if (ret)
164                 goto err;
165         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
166         if (ret)
167                 goto err;
168         memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
169         location.offset = (u64)-1;
170         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
171                         "default", 7,
172                         btrfs_super_root_dir(root->fs_info->super_copy),
173                         &location, BTRFS_FT_DIR, 0);
174         if (ret)
175                 goto err;
176
177         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
178                              "default", 7, location.objectid,
179                              BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
180         if (ret)
181                 goto err;
182
183 err:
184         return ret;
185 }
186
187 static int __recow_root(struct btrfs_trans_handle *trans,
188                          struct btrfs_root *root)
189 {
190         struct extent_buffer *tmp;
191         int ret;
192
193         if (trans->transid != btrfs_root_generation(&root->root_item)) {
194                 extent_buffer_get(root->node);
195                 ret = __btrfs_cow_block(trans, root, root->node,
196                                         NULL, 0, &tmp, 0, 0);
197                 if (ret)
198                         return ret;
199                 free_extent_buffer(tmp);
200         }
201
202         return 0;
203 }
204
205 static int recow_roots(struct btrfs_trans_handle *trans,
206                        struct btrfs_root *root)
207 {
208         struct btrfs_fs_info *info = root->fs_info;
209         int ret;
210
211         ret = __recow_root(trans, info->fs_root);
212         if (ret)
213                 return ret;
214         ret = __recow_root(trans, info->tree_root);
215         if (ret)
216                 return ret;
217         ret = __recow_root(trans, info->extent_root);
218         if (ret)
219                 return ret;
220         ret = __recow_root(trans, info->chunk_root);
221         if (ret)
222                 return ret;
223         ret = __recow_root(trans, info->dev_root);
224         if (ret)
225                 return ret;
226         ret = __recow_root(trans, info->csum_root);
227         if (ret)
228                 return ret;
229
230         return 0;
231 }
232
233 static int create_one_raid_group(struct btrfs_trans_handle *trans,
234                               struct btrfs_root *root, u64 type,
235                               struct mkfs_allocation *allocation)
236
237 {
238         struct btrfs_fs_info *fs_info = root->fs_info;
239         u64 chunk_start;
240         u64 chunk_size;
241         int ret;
242
243         ret = btrfs_alloc_chunk(trans, fs_info,
244                                 &chunk_start, &chunk_size, type);
245         if (ret == -ENOSPC) {
246                 error("not enough free space to allocate chunk");
247                 exit(1);
248         }
249         if (ret)
250                 return ret;
251
252         ret = btrfs_make_block_group(trans, fs_info, 0,
253                                      type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
254                                      chunk_start, chunk_size);
255
256         type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
257         if (type == BTRFS_BLOCK_GROUP_DATA) {
258                 allocation->data += chunk_size;
259         } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
260                 allocation->metadata += chunk_size;
261         } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
262                 allocation->system += chunk_size;
263         } else if (type ==
264                         (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
265                 allocation->mixed += chunk_size;
266         } else {
267                 error("unrecognized profile type: 0x%llx",
268                                 (unsigned long long)type);
269                 ret = -EINVAL;
270         }
271
272         return ret;
273 }
274
275 static int create_raid_groups(struct btrfs_trans_handle *trans,
276                               struct btrfs_root *root, u64 data_profile,
277                               u64 metadata_profile, int mixed,
278                               struct mkfs_allocation *allocation)
279 {
280         int ret;
281
282         if (metadata_profile) {
283                 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
284
285                 ret = create_one_raid_group(trans, root,
286                                             BTRFS_BLOCK_GROUP_SYSTEM |
287                                             metadata_profile, allocation);
288                 if (ret)
289                         return ret;
290
291                 if (mixed)
292                         meta_flags |= BTRFS_BLOCK_GROUP_DATA;
293
294                 ret = create_one_raid_group(trans, root, meta_flags |
295                                             metadata_profile, allocation);
296                 if (ret)
297                         return ret;
298
299         }
300         if (!mixed && data_profile) {
301                 ret = create_one_raid_group(trans, root,
302                                             BTRFS_BLOCK_GROUP_DATA |
303                                             data_profile, allocation);
304                 if (ret)
305                         return ret;
306         }
307         ret = recow_roots(trans, root);
308
309         return ret;
310 }
311
312 static int create_tree(struct btrfs_trans_handle *trans,
313                         struct btrfs_root *root, u64 objectid)
314 {
315         struct btrfs_key location;
316         struct btrfs_root_item root_item;
317         struct extent_buffer *tmp;
318         int ret;
319
320         ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
321         if (ret)
322                 return ret;
323
324         memcpy(&root_item, &root->root_item, sizeof(root_item));
325         btrfs_set_root_bytenr(&root_item, tmp->start);
326         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
327         btrfs_set_root_generation(&root_item, trans->transid);
328         free_extent_buffer(tmp);
329
330         location.objectid = objectid;
331         location.type = BTRFS_ROOT_ITEM_KEY;
332         location.offset = 0;
333         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
334                                 &location, &root_item);
335
336         return ret;
337 }
338
339 static void print_usage(int ret)
340 {
341         printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
342         printf("Options:\n");
343         printf("  allocation profiles:\n");
344         printf("\t-d|--data PROFILE       data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
345         printf("\t-m|--metadata PROFILE   metadata profile, values like for data profile\n");
346         printf("\t-M|--mixed              mix metadata and data together\n");
347         printf("  features:\n");
348         printf("\t-n|--nodesize SIZE      size of btree nodes\n");
349         printf("\t-s|--sectorsize SIZE    data block size (may not be mountable by current kernel)\n");
350         printf("\t-O|--features LIST      comma separated list of filesystem features (use '-O list-all' to list features)\n");
351         printf("\t-L|--label LABEL        set the filesystem label\n");
352         printf("\t-U|--uuid UUID          specify the filesystem UUID (must be unique)\n");
353         printf("  creation:\n");
354         printf("\t-b|--byte-count SIZE    set filesystem size to SIZE (on the first device)\n");
355         printf("\t-r|--rootdir DIR        copy files from DIR to the image root directory\n");
356         printf("\t-K|--nodiscard          do not perform whole device TRIM\n");
357         printf("\t-f|--force              force overwrite of existing filesystem\n");
358         printf("  general:\n");
359         printf("\t-q|--quiet              no messages except errors\n");
360         printf("\t-V|--version            print the mkfs.btrfs version and exit\n");
361         printf("\t--help                  print this help and exit\n");
362         printf("  deprecated:\n");
363         printf("\t-A|--alloc-start START  the offset to start the filesystem\n");
364         printf("\t-l|--leafsize SIZE      deprecated, alias for nodesize\n");
365         exit(ret);
366 }
367
368 static u64 parse_profile(const char *s)
369 {
370         if (strcasecmp(s, "raid0") == 0) {
371                 return BTRFS_BLOCK_GROUP_RAID0;
372         } else if (strcasecmp(s, "raid1") == 0) {
373                 return BTRFS_BLOCK_GROUP_RAID1;
374         } else if (strcasecmp(s, "raid5") == 0) {
375                 return BTRFS_BLOCK_GROUP_RAID5;
376         } else if (strcasecmp(s, "raid6") == 0) {
377                 return BTRFS_BLOCK_GROUP_RAID6;
378         } else if (strcasecmp(s, "raid10") == 0) {
379                 return BTRFS_BLOCK_GROUP_RAID10;
380         } else if (strcasecmp(s, "dup") == 0) {
381                 return BTRFS_BLOCK_GROUP_DUP;
382         } else if (strcasecmp(s, "single") == 0) {
383                 return 0;
384         } else {
385                 error("unknown profile %s", s);
386                 exit(1);
387         }
388         /* not reached */
389         return 0;
390 }
391
392 static char *parse_label(const char *input)
393 {
394         int len = strlen(input);
395
396         if (len >= BTRFS_LABEL_SIZE) {
397                 error("label %s is too long (max %d)", input,
398                         BTRFS_LABEL_SIZE - 1);
399                 exit(1);
400         }
401         return strdup(input);
402 }
403
404 static int create_chunks(struct btrfs_trans_handle *trans,
405                          struct btrfs_root *root, u64 num_of_meta_chunks,
406                          u64 size_of_data,
407                          struct mkfs_allocation *allocation)
408 {
409         struct btrfs_fs_info *fs_info = root->fs_info;
410         u64 chunk_start;
411         u64 chunk_size;
412         u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
413         u64 data_type = BTRFS_BLOCK_GROUP_DATA;
414         u64 minimum_data_chunk_size = SZ_8M;
415         u64 i;
416         int ret;
417
418         for (i = 0; i < num_of_meta_chunks; i++) {
419                 ret = btrfs_alloc_chunk(trans, fs_info,
420                                         &chunk_start, &chunk_size, meta_type);
421                 if (ret)
422                         return ret;
423                 ret = btrfs_make_block_group(trans, fs_info, 0,
424                                              meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
425                                              chunk_start, chunk_size);
426                 allocation->metadata += chunk_size;
427                 if (ret)
428                         return ret;
429                 set_extent_dirty(&root->fs_info->free_space_cache,
430                                  chunk_start, chunk_start + chunk_size - 1);
431         }
432
433         if (size_of_data < minimum_data_chunk_size)
434                 size_of_data = minimum_data_chunk_size;
435
436         ret = btrfs_alloc_data_chunk(trans, fs_info,
437                                      &chunk_start, size_of_data, data_type, 0);
438         if (ret)
439                 return ret;
440         ret = btrfs_make_block_group(trans, fs_info, 0,
441                                      data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
442                                      chunk_start, size_of_data);
443         allocation->data += size_of_data;
444         if (ret)
445                 return ret;
446         set_extent_dirty(&root->fs_info->free_space_cache,
447                          chunk_start, chunk_start + size_of_data - 1);
448         return ret;
449 }
450
451 /*
452  * This ignores symlinks with unreadable targets and subdirs that can't
453  * be read.  It's a best-effort to give a rough estimate of the size of
454  * a subdir.  It doesn't guarantee that prepopulating btrfs from this
455  * tree won't still run out of space.
456  */
457 static u64 global_total_size;
458 static u64 fs_block_size;
459 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
460                               int type)
461 {
462         if (type == FTW_F || type == FTW_D)
463                 global_total_size += round_up(st->st_size, fs_block_size);
464
465         return 0;
466 }
467
468 static u64 size_sourcedir(const char *dir_name, u64 sectorsize,
469                           u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
470 {
471         u64 dir_size = 0;
472         u64 total_size = 0;
473         int ret;
474         u64 default_chunk_size = SZ_8M;
475         u64 allocated_meta_size = SZ_8M;
476         u64 allocated_total_size = 20 * SZ_1M;  /* 20MB */
477         u64 num_of_meta_chunks = 0;
478         u64 num_of_data_chunks = 0;
479         u64 num_of_allocated_meta_chunks =
480                         allocated_meta_size / default_chunk_size;
481
482         global_total_size = 0;
483         fs_block_size = sectorsize;
484         ret = ftw(dir_name, ftw_add_entry_size, 10);
485         dir_size = global_total_size;
486         if (ret < 0) {
487                 error("ftw subdir walk of %s failed: %s", dir_name,
488                         strerror(errno));
489                 exit(1);
490         }
491
492         num_of_data_chunks = (dir_size + default_chunk_size - 1) /
493                 default_chunk_size;
494
495         num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
496         if (((dir_size / 2) % default_chunk_size) != 0)
497                 num_of_meta_chunks++;
498         if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
499                 num_of_meta_chunks = 0;
500         else
501                 num_of_meta_chunks -= num_of_allocated_meta_chunks;
502
503         total_size = allocated_total_size +
504                      (num_of_data_chunks * default_chunk_size) +
505                      (num_of_meta_chunks * default_chunk_size);
506
507         *num_of_meta_chunks_ret = num_of_meta_chunks;
508         *size_of_data_ret = num_of_data_chunks * default_chunk_size;
509         return total_size;
510 }
511
512 static int zero_output_file(int out_fd, u64 size)
513 {
514         int loop_num;
515         u64 location = 0;
516         char buf[SZ_4K];
517         int ret = 0, i;
518         ssize_t written;
519
520         memset(buf, 0, SZ_4K);
521
522         /* Only zero out the first 1M */
523         loop_num = SZ_1M / SZ_4K;
524         for (i = 0; i < loop_num; i++) {
525                 written = pwrite64(out_fd, buf, SZ_4K, location);
526                 if (written != SZ_4K)
527                         ret = -EIO;
528                 location += SZ_4K;
529         }
530
531         /* Then enlarge the file to size */
532         written = pwrite64(out_fd, buf, 1, size - 1);
533         if (written < 1)
534                 ret = -EIO;
535         return ret;
536 }
537
538 static int is_ssd(const char *file)
539 {
540         blkid_probe probe;
541         char wholedisk[PATH_MAX];
542         char sysfs_path[PATH_MAX];
543         dev_t devno;
544         int fd;
545         char rotational;
546         int ret;
547
548         probe = blkid_new_probe_from_filename(file);
549         if (!probe)
550                 return 0;
551
552         /* Device number of this disk (possibly a partition) */
553         devno = blkid_probe_get_devno(probe);
554         if (!devno) {
555                 blkid_free_probe(probe);
556                 return 0;
557         }
558
559         /* Get whole disk name (not full path) for this devno */
560         ret = blkid_devno_to_wholedisk(devno,
561                         wholedisk, sizeof(wholedisk), NULL);
562         if (ret) {
563                 blkid_free_probe(probe);
564                 return 0;
565         }
566
567         snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
568                  wholedisk);
569
570         blkid_free_probe(probe);
571
572         fd = open(sysfs_path, O_RDONLY);
573         if (fd < 0) {
574                 return 0;
575         }
576
577         if (read(fd, &rotational, 1) < 1) {
578                 close(fd);
579                 return 0;
580         }
581         close(fd);
582
583         return rotational == '0';
584 }
585
586 static int _cmp_device_by_id(void *priv, struct list_head *a,
587                              struct list_head *b)
588 {
589         return list_entry(a, struct btrfs_device, dev_list)->devid -
590                list_entry(b, struct btrfs_device, dev_list)->devid;
591 }
592
593 static void list_all_devices(struct btrfs_root *root)
594 {
595         struct btrfs_fs_devices *fs_devices;
596         struct btrfs_device *device;
597         int number_of_devices = 0;
598         u64 total_block_count = 0;
599
600         fs_devices = root->fs_info->fs_devices;
601
602         list_for_each_entry(device, &fs_devices->devices, dev_list)
603                 number_of_devices++;
604
605         list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
606
607         printf("Number of devices:  %d\n", number_of_devices);
608         /* printf("Total devices size: %10s\n", */
609                 /* pretty_size(total_block_count)); */
610         printf("Devices:\n");
611         printf("   ID        SIZE  PATH\n");
612         list_for_each_entry(device, &fs_devices->devices, dev_list) {
613                 printf("  %3llu  %10s  %s\n",
614                         device->devid,
615                         pretty_size(device->total_bytes),
616                         device->name);
617                 total_block_count += device->total_bytes;
618         }
619
620         printf("\n");
621 }
622
623 static int is_temp_block_group(struct extent_buffer *node,
624                                struct btrfs_block_group_item *bgi,
625                                u64 data_profile, u64 meta_profile,
626                                u64 sys_profile)
627 {
628         u64 flag = btrfs_disk_block_group_flags(node, bgi);
629         u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
630         u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
631         u64 used = btrfs_disk_block_group_used(node, bgi);
632
633         /*
634          * Chunks meets all the following conditions is a temp chunk
635          * 1) Empty chunk
636          * Temp chunk is always empty.
637          *
638          * 2) profile mismatch with mkfs profile.
639          * Temp chunk is always in SINGLE
640          *
641          * 3) Size differs with mkfs_alloc
642          * Special case for SINGLE/SINGLE btrfs.
643          * In that case, temp data chunk and real data chunk are always empty.
644          * So we need to use mkfs_alloc to be sure which chunk is the newly
645          * allocated.
646          *
647          * Normally, new chunk size is equal to mkfs one (One chunk)
648          * If it has multiple chunks, we just refuse to delete any one.
649          * As they are all single, so no real problem will happen.
650          * So only use condition 1) and 2) to judge them.
651          */
652         if (used != 0)
653                 return 0;
654         switch (flag_type) {
655         case BTRFS_BLOCK_GROUP_DATA:
656         case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
657                 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
658                 if (flag_profile != data_profile)
659                         return 1;
660                 break;
661         case BTRFS_BLOCK_GROUP_METADATA:
662                 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
663                 if (flag_profile != meta_profile)
664                         return 1;
665                 break;
666         case BTRFS_BLOCK_GROUP_SYSTEM:
667                 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
668                 if (flag_profile != sys_profile)
669                         return 1;
670                 break;
671         }
672         return 0;
673 }
674
675 /* Note: if current is a block group, it will skip it anyway */
676 static int next_block_group(struct btrfs_root *root,
677                             struct btrfs_path *path)
678 {
679         struct btrfs_key key;
680         int ret = 0;
681
682         while (1) {
683                 ret = btrfs_next_item(root, path);
684                 if (ret)
685                         goto out;
686
687                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
688                 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
689                         goto out;
690         }
691 out:
692         return ret;
693 }
694
695 /* This function will cleanup  */
696 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
697                                struct mkfs_allocation *alloc,
698                                u64 data_profile, u64 meta_profile,
699                                u64 sys_profile)
700 {
701         struct btrfs_trans_handle *trans = NULL;
702         struct btrfs_block_group_item *bgi;
703         struct btrfs_root *root = fs_info->extent_root;
704         struct btrfs_key key;
705         struct btrfs_key found_key;
706         struct btrfs_path path;
707         int ret = 0;
708
709         btrfs_init_path(&path);
710         trans = btrfs_start_transaction(root, 1);
711         BUG_ON(IS_ERR(trans));
712
713         key.objectid = 0;
714         key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
715         key.offset = 0;
716
717         while (1) {
718                 /*
719                  * as the rest of the loop may modify the tree, we need to
720                  * start a new search each time.
721                  */
722                 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
723                 if (ret < 0)
724                         goto out;
725                 /* Don't pollute ret for >0 case */
726                 if (ret > 0)
727                         ret = 0;
728
729                 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
730                                       path.slots[0]);
731                 if (found_key.objectid < key.objectid)
732                         goto out;
733                 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
734                         ret = next_block_group(root, &path);
735                         if (ret < 0)
736                                 goto out;
737                         if (ret > 0) {
738                                 ret = 0;
739                                 goto out;
740                         }
741                         btrfs_item_key_to_cpu(path.nodes[0], &found_key,
742                                               path.slots[0]);
743                 }
744
745                 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
746                                      struct btrfs_block_group_item);
747                 if (is_temp_block_group(path.nodes[0], bgi,
748                                         data_profile, meta_profile,
749                                         sys_profile)) {
750                         u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
751                                                              bgi);
752
753                         ret = btrfs_free_block_group(trans, fs_info,
754                                         found_key.objectid, found_key.offset);
755                         if (ret < 0)
756                                 goto out;
757
758                         if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
759                             BTRFS_BLOCK_GROUP_DATA)
760                                 alloc->data -= found_key.offset;
761                         else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
762                                  BTRFS_BLOCK_GROUP_METADATA)
763                                 alloc->metadata -= found_key.offset;
764                         else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
765                                  BTRFS_BLOCK_GROUP_SYSTEM)
766                                 alloc->system -= found_key.offset;
767                         else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
768                                  (BTRFS_BLOCK_GROUP_METADATA |
769                                   BTRFS_BLOCK_GROUP_DATA))
770                                 alloc->mixed -= found_key.offset;
771                 }
772                 btrfs_release_path(&path);
773                 key.objectid = found_key.objectid + found_key.offset;
774         }
775 out:
776         if (trans)
777                 btrfs_commit_transaction(trans, root);
778         btrfs_release_path(&path);
779         return ret;
780 }
781
782 int main(int argc, char **argv)
783 {
784         char *file;
785         struct btrfs_root *root;
786         struct btrfs_fs_info *fs_info;
787         struct btrfs_trans_handle *trans;
788         char *label = NULL;
789         u64 block_count = 0;
790         u64 dev_block_count = 0;
791         u64 alloc_start = 0;
792         u64 metadata_profile = 0;
793         u64 data_profile = 0;
794         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
795                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
796         u32 sectorsize = 4096;
797         u32 stripesize = 4096;
798         int zero_end = 1;
799         int fd = -1;
800         int ret;
801         int close_ret;
802         int i;
803         int mixed = 0;
804         int nodesize_forced = 0;
805         int data_profile_opt = 0;
806         int metadata_profile_opt = 0;
807         int discard = 1;
808         int ssd = 0;
809         int force_overwrite = 0;
810         char *source_dir = NULL;
811         int source_dir_set = 0;
812         u64 num_of_meta_chunks = 0;
813         u64 size_of_data = 0;
814         u64 source_dir_size = 0;
815         u64 min_dev_size;
816         int dev_cnt = 0;
817         int saved_optind;
818         char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
819         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
820         struct mkfs_allocation allocation = { 0 };
821         struct btrfs_mkfs_config mkfs_cfg;
822
823         while(1) {
824                 int c;
825                 static const struct option long_options[] = {
826                         { "alloc-start", required_argument, NULL, 'A'},
827                         { "byte-count", required_argument, NULL, 'b' },
828                         { "force", no_argument, NULL, 'f' },
829                         { "leafsize", required_argument, NULL, 'l' },
830                         { "label", required_argument, NULL, 'L'},
831                         { "metadata", required_argument, NULL, 'm' },
832                         { "mixed", no_argument, NULL, 'M' },
833                         { "nodesize", required_argument, NULL, 'n' },
834                         { "sectorsize", required_argument, NULL, 's' },
835                         { "data", required_argument, NULL, 'd' },
836                         { "version", no_argument, NULL, 'V' },
837                         { "rootdir", required_argument, NULL, 'r' },
838                         { "nodiscard", no_argument, NULL, 'K' },
839                         { "features", required_argument, NULL, 'O' },
840                         { "uuid", required_argument, NULL, 'U' },
841                         { "quiet", 0, NULL, 'q' },
842                         { "help", no_argument, NULL, GETOPT_VAL_HELP },
843                         { NULL, 0, NULL, 0}
844                 };
845
846                 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
847                                 long_options, NULL);
848                 if (c < 0)
849                         break;
850                 switch(c) {
851                         case 'A':
852                                 alloc_start = parse_size(optarg);
853                                 break;
854                         case 'f':
855                                 force_overwrite = 1;
856                                 break;
857                         case 'd':
858                                 data_profile = parse_profile(optarg);
859                                 data_profile_opt = 1;
860                                 break;
861                         case 'l':
862                                 warning("--leafsize is deprecated, use --nodesize");
863                                 /* fall through */
864                         case 'n':
865                                 nodesize = parse_size(optarg);
866                                 nodesize_forced = 1;
867                                 break;
868                         case 'L':
869                                 label = parse_label(optarg);
870                                 break;
871                         case 'm':
872                                 metadata_profile = parse_profile(optarg);
873                                 metadata_profile_opt = 1;
874                                 break;
875                         case 'M':
876                                 mixed = 1;
877                                 break;
878                         case 'O': {
879                                 char *orig = strdup(optarg);
880                                 char *tmp = orig;
881
882                                 tmp = btrfs_parse_fs_features(tmp, &features);
883                                 if (tmp) {
884                                         error("unrecognized filesystem feature '%s'",
885                                                         tmp);
886                                         free(orig);
887                                         goto error;
888                                 }
889                                 free(orig);
890                                 if (features & BTRFS_FEATURE_LIST_ALL) {
891                                         btrfs_list_all_fs_features(0);
892                                         goto success;
893                                 }
894                                 break;
895                                 }
896                         case 's':
897                                 sectorsize = parse_size(optarg);
898                                 break;
899                         case 'b':
900                                 block_count = parse_size(optarg);
901                                 zero_end = 0;
902                                 break;
903                         case 'V':
904                                 printf("mkfs.btrfs, part of %s\n",
905                                                 PACKAGE_STRING);
906                                 goto success;
907                         case 'r':
908                                 source_dir = optarg;
909                                 source_dir_set = 1;
910                                 break;
911                         case 'U':
912                                 strncpy(fs_uuid, optarg,
913                                         BTRFS_UUID_UNPARSED_SIZE - 1);
914                                 break;
915                         case 'K':
916                                 discard = 0;
917                                 break;
918                         case 'q':
919                                 verbose = 0;
920                                 break;
921                         case GETOPT_VAL_HELP:
922                         default:
923                                 print_usage(c != GETOPT_VAL_HELP);
924                 }
925         }
926
927         if (verbose) {
928                 printf("%s\n", PACKAGE_STRING);
929                 printf("See %s for more information.\n\n", PACKAGE_URL);
930         }
931
932         sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
933         stripesize = sectorsize;
934         saved_optind = optind;
935         dev_cnt = argc - optind;
936         if (dev_cnt == 0)
937                 print_usage(1);
938
939         if (source_dir_set && dev_cnt > 1) {
940                 error("the option -r is limited to a single device");
941                 goto error;
942         }
943
944         if (*fs_uuid) {
945                 uuid_t dummy_uuid;
946
947                 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
948                         error("could not parse UUID: %s", fs_uuid);
949                         goto error;
950                 }
951                 if (!test_uuid_unique(fs_uuid)) {
952                         error("non-unique UUID: %s", fs_uuid);
953                         goto error;
954                 }
955         }
956
957         while (dev_cnt-- > 0) {
958                 file = argv[optind++];
959                 if (is_block_device(file) == 1)
960                         ret = test_dev_for_mkfs(file, force_overwrite);
961                 else
962                         ret = test_status_for_mkfs(file, force_overwrite);
963
964                 if (ret)
965                         goto error;
966         }
967
968         optind = saved_optind;
969         dev_cnt = argc - optind;
970
971         file = argv[optind++];
972         ssd = is_ssd(file);
973
974         /*
975         * Set default profiles according to number of added devices.
976         * For mixed groups defaults are single/single.
977         */
978         if (!mixed) {
979                 if (!metadata_profile_opt) {
980                         if (dev_cnt == 1 && ssd && verbose)
981                                 printf("Detected a SSD, turning off metadata "
982                                 "duplication.  Mkfs with -m dup if you want to "
983                                 "force metadata duplication.\n");
984
985                         metadata_profile = (dev_cnt > 1) ?
986                                         BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
987                                         0: BTRFS_BLOCK_GROUP_DUP;
988                 }
989                 if (!data_profile_opt) {
990                         data_profile = (dev_cnt > 1) ?
991                                 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
992                 }
993         } else {
994                 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
995
996                 if (metadata_profile_opt || data_profile_opt) {
997                         if (metadata_profile != data_profile) {
998                                 error(
999         "with mixed block groups data and metadata profiles must be the same");
1000                                 goto error;
1001                         }
1002                 }
1003
1004                 if (!nodesize_forced)
1005                         nodesize = best_nodesize;
1006         }
1007
1008         /*
1009          * FS features that can be set by other means than -O
1010          * just set the bit here
1011          */
1012         if (mixed)
1013                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1014
1015         if ((data_profile | metadata_profile) &
1016             (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1017                 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1018         }
1019
1020         if (btrfs_check_nodesize(nodesize, sectorsize,
1021                                  features))
1022                 goto error;
1023
1024         if (sectorsize < sizeof(struct btrfs_super_block)) {
1025                 error("sectorsize smaller than superblock: %u < %zu",
1026                                 sectorsize, sizeof(struct btrfs_super_block));
1027                 goto error;
1028         }
1029
1030         min_dev_size = btrfs_min_dev_size(nodesize, mixed, metadata_profile,
1031                                           data_profile);
1032         /* Check device/block_count after the nodesize is determined */
1033         if (block_count && block_count < min_dev_size) {
1034                 error("size %llu is too small to make a usable filesystem",
1035                         block_count);
1036                 error("minimum size for btrfs filesystem is %llu",
1037                         min_dev_size);
1038                 goto error;
1039         }
1040         for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1041                 char *path;
1042
1043                 path = argv[i];
1044                 ret = test_minimum_size(path, min_dev_size);
1045                 if (ret < 0) {
1046                         error("failed to check size for %s: %s",
1047                                 path, strerror(-ret));
1048                         goto error;
1049                 }
1050                 if (ret > 0) {
1051                         error("'%s' is too small to make a usable filesystem",
1052                                 path);
1053                         error("minimum size for each btrfs device is %llu",
1054                                 min_dev_size);
1055                         goto error;
1056                 }
1057         }
1058         ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1059                         dev_cnt, mixed, ssd);
1060         if (ret)
1061                 goto error;
1062
1063         dev_cnt--;
1064
1065         if (!source_dir_set) {
1066                 /*
1067                  * open without O_EXCL so that the problem should not
1068                  * occur by the following processing.
1069                  * (btrfs_register_one_device() fails if O_EXCL is on)
1070                  */
1071                 fd = open(file, O_RDWR);
1072                 if (fd < 0) {
1073                         error("unable to open %s: %s", file, strerror(errno));
1074                         goto error;
1075                 }
1076                 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1077                                 block_count,
1078                                 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1079                                 (discard ? PREP_DEVICE_DISCARD : 0) |
1080                                 (verbose ? PREP_DEVICE_VERBOSE : 0));
1081                 if (ret) {
1082                         goto error;
1083                 }
1084                 if (block_count && block_count > dev_block_count) {
1085                         error("%s is smaller than requested size, expected %llu, found %llu",
1086                                         file,
1087                                         (unsigned long long)block_count,
1088                                         (unsigned long long)dev_block_count);
1089                         goto error;
1090                 }
1091         } else {
1092                 fd = open(file, O_CREAT | O_RDWR,
1093                                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1094                 if (fd < 0) {
1095                         error("unable to open %s: %s", file, strerror(errno));
1096                         goto error;
1097                 }
1098
1099                 source_dir_size = size_sourcedir(source_dir, sectorsize,
1100                                              &num_of_meta_chunks, &size_of_data);
1101                 if(block_count < source_dir_size)
1102                         block_count = source_dir_size;
1103                 ret = zero_output_file(fd, block_count);
1104                 if (ret) {
1105                         error("unable to zero the output file");
1106                         goto error;
1107                 }
1108                 /* our "device" is the new image file */
1109                 dev_block_count = block_count;
1110         }
1111
1112         /* To create the first block group and chunk 0 in make_btrfs */
1113         if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1114                 error("device is too small to make filesystem, must be at least %llu",
1115                                 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1116                 goto error;
1117         }
1118
1119         if (group_profile_max_safe_loss(metadata_profile) <
1120                 group_profile_max_safe_loss(data_profile)){
1121                 warning("metadata has lower redundancy than data!\n");
1122         }
1123
1124         mkfs_cfg.label = label;
1125         memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1126         mkfs_cfg.num_bytes = dev_block_count;
1127         mkfs_cfg.nodesize = nodesize;
1128         mkfs_cfg.sectorsize = sectorsize;
1129         mkfs_cfg.stripesize = stripesize;
1130         mkfs_cfg.features = features;
1131
1132         ret = make_btrfs(fd, &mkfs_cfg);
1133         if (ret) {
1134                 error("error during mkfs: %s", strerror(-ret));
1135                 goto error;
1136         }
1137
1138         fs_info = open_ctree_fs_info(file, 0, 0, 0,
1139                         OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1140         if (!fs_info) {
1141                 error("open ctree failed");
1142                 goto error;
1143         }
1144         close(fd);
1145         fd = -1;
1146         root = fs_info->fs_root;
1147         fs_info->alloc_start = alloc_start;
1148
1149         ret = create_metadata_block_groups(root, mixed, &allocation);
1150         if (ret) {
1151                 error("failed to create default block groups: %d", ret);
1152                 goto error;
1153         }
1154
1155         trans = btrfs_start_transaction(root, 1);
1156         if (IS_ERR(trans)) {
1157                 error("failed to start transaction");
1158                 goto error;
1159         }
1160
1161         ret = create_data_block_groups(trans, root, mixed, &allocation);
1162         if (ret) {
1163                 error("failed to create default data block groups: %d", ret);
1164                 goto error;
1165         }
1166
1167         ret = make_root_dir(trans, root);
1168         if (ret) {
1169                 error("failed to setup the root directory: %d", ret);
1170                 goto error;
1171         }
1172
1173         ret = btrfs_commit_transaction(trans, root);
1174         if (ret) {
1175                 error("unable to commit transaction: %d", ret);
1176                 goto out;
1177         }
1178
1179         trans = btrfs_start_transaction(root, 1);
1180         if (IS_ERR(trans)) {
1181                 error("failed to start transaction");
1182                 goto error;
1183         }
1184
1185         if (dev_cnt == 0)
1186                 goto raid_groups;
1187
1188         while (dev_cnt-- > 0) {
1189                 file = argv[optind++];
1190
1191                 /*
1192                  * open without O_EXCL so that the problem should not
1193                  * occur by the following processing.
1194                  * (btrfs_register_one_device() fails if O_EXCL is on)
1195                  */
1196                 fd = open(file, O_RDWR);
1197                 if (fd < 0) {
1198                         error("unable to open %s: %s", file, strerror(errno));
1199                         goto error;
1200                 }
1201                 ret = btrfs_device_already_in_root(root, fd,
1202                                                    BTRFS_SUPER_INFO_OFFSET);
1203                 if (ret) {
1204                         error("skipping duplicate device %s in the filesystem",
1205                                 file);
1206                         close(fd);
1207                         continue;
1208                 }
1209                 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1210                                 block_count,
1211                                 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1212                                 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1213                                 (discard ? PREP_DEVICE_DISCARD : 0));
1214                 if (ret) {
1215                         goto error;
1216                 }
1217
1218                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1219                                         sectorsize, sectorsize, sectorsize);
1220                 if (ret) {
1221                         error("unable to add %s to filesystem: %d", file, ret);
1222                         goto out;
1223                 }
1224                 if (verbose >= 2) {
1225                         struct btrfs_device *device;
1226
1227                         device = container_of(fs_info->fs_devices->devices.next,
1228                                         struct btrfs_device, dev_list);
1229                         printf("adding device %s id %llu\n", file,
1230                                 (unsigned long long)device->devid);
1231                 }
1232         }
1233
1234 raid_groups:
1235         if (!source_dir_set) {
1236                 ret = create_raid_groups(trans, root, data_profile,
1237                                  metadata_profile, mixed, &allocation);
1238                 if (ret) {
1239                         error("unable to create raid groups: %d", ret);
1240                         goto out;
1241                 }
1242         }
1243
1244         ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1245         if (ret) {
1246                 error("unable to create data reloc tree: %d", ret);
1247                 goto out;
1248         }
1249
1250         ret = btrfs_commit_transaction(trans, root);
1251         if (ret) {
1252                 error("unable to commit transaction: %d", ret);
1253                 goto out;
1254         }
1255
1256         if (source_dir_set) {
1257                 trans = btrfs_start_transaction(root, 1);
1258                 BUG_ON(IS_ERR(trans));
1259                 ret = create_chunks(trans, root,
1260                                     num_of_meta_chunks, size_of_data,
1261                                     &allocation);
1262                 if (ret) {
1263                         error("unable to create chunks: %d", ret);
1264                         goto out;
1265                 }
1266                 ret = btrfs_commit_transaction(trans, root);
1267                 if (ret) {
1268                         error("transaction commit failed: %d", ret);
1269                         goto out;
1270                 }
1271
1272                 ret = btrfs_mkfs_fill_dir(source_dir, root, verbose);
1273                 if (ret) {
1274                         error("error wihle filling filesystem: %d", ret);
1275                         goto out;
1276                 }
1277         }
1278         ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1279                                   metadata_profile, metadata_profile);
1280         if (ret < 0) {
1281                 error("failed to cleanup temporary chunks: %d", ret);
1282                 goto out;
1283         }
1284
1285         if (verbose) {
1286                 char features_buf[64];
1287
1288                 printf("Label:              %s\n", label);
1289                 printf("UUID:               %s\n", mkfs_cfg.fs_uuid);
1290                 printf("Node size:          %u\n", nodesize);
1291                 printf("Sector size:        %u\n", sectorsize);
1292                 printf("Filesystem size:    %s\n",
1293                         pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1294                 printf("Block group profiles:\n");
1295                 if (allocation.data)
1296                         printf("  Data:             %-8s %16s\n",
1297                                 btrfs_group_profile_str(data_profile),
1298                                 pretty_size(allocation.data));
1299                 if (allocation.metadata)
1300                         printf("  Metadata:         %-8s %16s\n",
1301                                 btrfs_group_profile_str(metadata_profile),
1302                                 pretty_size(allocation.metadata));
1303                 if (allocation.mixed)
1304                         printf("  Data+Metadata:    %-8s %16s\n",
1305                                 btrfs_group_profile_str(data_profile),
1306                                 pretty_size(allocation.mixed));
1307                 printf("  System:           %-8s %16s\n",
1308                         btrfs_group_profile_str(metadata_profile),
1309                         pretty_size(allocation.system));
1310                 printf("SSD detected:       %s\n", ssd ? "yes" : "no");
1311                 btrfs_parse_features_to_string(features_buf, features);
1312                 printf("Incompat features:  %s", features_buf);
1313                 printf("\n");
1314
1315                 list_all_devices(root);
1316         }
1317
1318         /*
1319          * The filesystem is now fully set up, commit the remaining changes and
1320          * fix the signature as the last step before closing the devices.
1321          */
1322         fs_info->finalize_on_close = 1;
1323 out:
1324         close_ret = close_ctree(root);
1325
1326         if (!close_ret) {
1327                 optind = saved_optind;
1328                 dev_cnt = argc - optind;
1329                 while (dev_cnt-- > 0) {
1330                         file = argv[optind++];
1331                         if (is_block_device(file) == 1)
1332                                 btrfs_register_one_device(file);
1333                 }
1334         }
1335
1336         btrfs_close_all_devices();
1337         free(label);
1338
1339         return !!ret;
1340 error:
1341         if (fd > 0)
1342                 close(fd);
1343
1344         free(label);
1345         exit(1);
1346 success:
1347         exit(0);
1348 }