d327d81fedcbd41a6fc1893517700722fc6ba419
[dragonfly.git] / sbin / hammer / cmd_expand.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com> and
6  * Michael Neumann <mneumann@ntecs.de>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 /*
37  * Expand a HAMMER filesystem.
38  */
39
40 #include "hammer.h"
41 #include <string.h>
42
43 static uint64_t check_volume(const char *vol_name);
44 static void expand_usage(int exit_code);
45
46 /*
47  * expand <filesystem> <device>
48  */
49 void
50 hammer_cmd_expand(char **av, int ac)
51 {
52         struct hammer_ioc_expand expand;
53         struct statfs sfs;
54         int fd;
55
56         if (ac != 2)
57                 expand_usage(1);
58         fd = open(av[0], O_RDONLY);
59         if (fd < 0) {
60                 fprintf(stderr, "hammer expand: unable to access %s: %s\n",
61                         av[0], strerror(errno));
62                 exit(1);
63         }
64
65         /*
66          * Make sure we aren't trying to expand the root filesystem.  The
67          * kernel can't handle multi-volume root mounts.
68          */
69         if (fstatfs(fd, &sfs) < 0) {
70                 fprintf(stderr, "hammer expand: statvfs failed on %s: %s\n",
71                         av[0], strerror(errno));
72                 exit(1);
73         }
74         if (strcmp(sfs.f_mntonname, "/") == 0 || sfs.f_mntonname[0] == 0) {
75                 fprintf(stderr,
76                         "hammer expand: Refused attempt to expand root fs.\n"
77                         "The kernel is unable to boot from multi-volume\n"
78                         "HAMMER root filesystems.\n");
79                 exit(1);
80         }
81
82         /*
83          * Expansion ioctl
84          */
85         bzero(&expand, sizeof(expand));
86         strncpy(expand.device_name, av[1], MAXPATHLEN);
87         expand.vol_size = check_volume(av[1]);
88         expand.boot_area_size = HAMMER_BOOT_NOMBYTES;
89         expand.mem_area_size = HAMMER_MEM_NOMBYTES;
90
91         if (ioctl(fd, HAMMERIOC_EXPAND, &expand) < 0) {
92                 fprintf(stderr, "hammer expand ioctl: %s\n", strerror(errno));
93                 exit(1);
94         }
95
96         close(fd);
97 }
98
99 static
100 void
101 expand_usage(int exit_code)
102 {
103         fprintf(stderr, "hammer expand <filesystem> <device>\n");
104         exit(exit_code);
105 }
106
107 /*
108  * Check basic volume characteristics.  HAMMER filesystems use a minimum
109  * of a 16KB filesystem buffer size.
110  *
111  * Returns the size of the device.
112  *
113  * From newfs_hammer.c
114  */
115 static
116 uint64_t
117 check_volume(const char *vol_name)
118 {
119         struct partinfo pinfo;
120         int fd;
121
122         /*
123          * Get basic information about the volume
124          */
125         fd = open(vol_name, O_RDWR);
126         if (fd < 0)
127                 errx(1, "Unable to open %s R+W", vol_name);
128
129         if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
130                 errx(1, "No block device: %s", vol_name);
131         }
132         /*
133          * When formatting a block device as a HAMMER volume the
134          * sector size must be compatible. HAMMER uses 16384 byte
135          * filesystem buffers.
136          */
137         if (pinfo.reserved_blocks) {
138                 errx(1, "HAMMER cannot be placed in a partition "
139                         "which overlaps the disklabel or MBR");
140         }
141         if (pinfo.media_blksize > 16384 ||
142             16384 % pinfo.media_blksize) {
143                 errx(1, "A media sector size of %d is not supported",
144                      pinfo.media_blksize);
145         }
146
147         close(fd);
148         return pinfo.media_size;
149 }