]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_subvolume.c
Subvolume commands
[bcachefs-tools-debian] / cmd_subvolume.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <libgen.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include "libbcachefs/bcachefs.h"
16 #include "libbcachefs/bcachefs_ioctl.h"
17 #include "cmds.h"
18 #include "libbcachefs.h"
19 #include "libbcachefs/opts.h"
20 #include "tools-util.h"
21
22 static void subvolume_create_usage(void)
23 {
24         puts("bcachefs subvolume create - create a new subvolume\n"
25              "Usage: bcachefs subvolume create [OPTION]... path\n"
26              "\n"
27              "Options:\n"
28              "  -h, --help                  Display this help and exit\n"
29              "\n"
30              "Report bugs to <linux-bcachefs@vger.kernel.org>");
31 }
32
33 int cmd_subvolume_create(int argc, char *argv[])
34 {
35         static const struct option longopts[] = {
36                 { "help",               no_argument,            NULL, 'h' },
37                 { NULL }
38         };
39         char *path;
40         int opt;
41
42         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
43                 switch (opt) {
44                 case 'h':
45                         subvolume_create_usage();
46                         exit(EXIT_SUCCESS);
47                 }
48         args_shift(optind);
49
50         while ((path = arg_pop())) {
51                 char *dir = dirname(strdup(path));
52
53                 struct bchfs_handle fs = bcache_fs_open(dir);
54
55                 struct bch_ioctl_subvolume i = {
56                         .dirfd          = AT_FDCWD,
57                         .mode           = 0777,
58                         .dst_ptr        = (unsigned long)path,
59                 };
60
61                 xioctl(fs.ioctl_fd, BCH_IOCTL_SUBVOLUME_CREATE, &i);
62                 bcache_fs_close(fs);
63         }
64
65         return 0;
66 }
67
68 static void subvolume_delete_usage(void)
69 {
70         puts("bcachefs subvolume delete - delete an existing subvolume\n"
71              "Usage: bcachefs subvolume delete [OPTION]... path\n"
72              "\n"
73              "Options:\n"
74              "  -h, --help                  Display this help and exit\n"
75              "\n"
76              "Report bugs to <linux-bcachefs@vger.kernel.org>");
77 }
78
79 int cmd_subvolume_delete(int argc, char *argv[])
80 {
81         static const struct option longopts[] = {
82                 { "help",               no_argument,            NULL, 'h' },
83                 { NULL }
84         };
85         char *path;
86         int opt;
87
88         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
89                 switch (opt) {
90                 case 'h':
91                         subvolume_delete_usage();
92                         exit(EXIT_SUCCESS);
93                 }
94         args_shift(optind);
95
96         while ((path = arg_pop())) {
97                 char *dir = dirname(strdup(path));
98
99                 struct bchfs_handle fs = bcache_fs_open(dir);
100
101                 struct bch_ioctl_subvolume i = {
102                         .dirfd          = AT_FDCWD,
103                         .mode           = 0777,
104                         .dst_ptr        = (unsigned long)path,
105                 };
106
107                 xioctl(fs.ioctl_fd, BCH_IOCTL_SUBVOLUME_DESTROY, &i);
108                 bcache_fs_close(fs);
109         }
110
111         return 0;
112 }
113
114 static void snapshot_create_usage(void)
115 {
116         puts("bcachefs subvolume snapshot - create a snapshot \n"
117              "Usage: bcachefs subvolume snapshot [OPTION]... <source> <dest>\n"
118              "\n"
119              "Create a snapshot of <source> at <dest>. If specified, <source> must be a subvolume;\n"
120              "if not specified the snapshot will be of the subvolme containing <dest>.\n"
121              "Options:\n"
122              "  -r                          Make snapshot read only\n"
123              "  -h, --help                  Display this help and exit\n"
124              "\n"
125              "Report bugs to <linux-bcachefs@vger.kernel.org>");
126 }
127
128 int cmd_subvolume_snapshot(int argc, char *argv[])
129 {
130         static const struct option longopts[] = {
131                 { "help",               no_argument,            NULL, 'h' },
132                 { NULL }
133         };
134         unsigned flags = BCH_SUBVOL_SNAPSHOT_CREATE;
135         int opt;
136
137         while ((opt = getopt_long(argc, argv, "rh", longopts, NULL)) != -1)
138                 switch (opt) {
139                 case 'r':
140                         flags |= BCH_SUBVOL_SNAPSHOT_RO;
141                         break;
142                 case 'h':
143                         snapshot_create_usage();
144                         exit(EXIT_SUCCESS);
145                 }
146         args_shift(optind);
147
148         char *src = arg_pop();
149         char *dst = arg_pop();
150
151         if (argc)
152                 die("Too many arguments");
153
154         if (!dst)
155                 swap(src, dst);
156         if (!dst)
157                 die("Please specify a path to create");
158
159         char *dir = dirname(strdup(dst));
160
161         struct bchfs_handle fs = bcache_fs_open(dir);
162
163         struct bch_ioctl_subvolume i = {
164                 .flags          = flags,
165                 .dirfd          = AT_FDCWD,
166                 .mode           = 0777,
167                 .src_ptr        = (unsigned long)src,
168                 .dst_ptr        = (unsigned long)dst,
169         };
170
171         xioctl(fs.ioctl_fd, BCH_IOCTL_SUBVOLUME_CREATE, &i);
172         bcache_fs_close(fs);
173         return 0;
174 }