46
46
#include "common/units.h"
47
47
#include "common/format-output.h"
48
48
#include "common/tree-search.h"
49
+ #include "common/parse-utils.h"
49
50
#include "cmds/commands.h"
50
51
#include "cmds/qgroup.h"
51
52
@@ -140,28 +141,15 @@ static const char * const cmd_subvolume_create_usage[] = {
140
141
NULL
141
142
};
142
143
143
- static int create_one_subvolume (const char * dst , struct btrfs_qgroup_inherit * inherit ,
144
+ static int create_one_subvolume (const char * dst , struct btrfs_util_qgroup_inherit * inherit ,
144
145
bool create_parents )
145
146
{
146
147
int ret ;
147
- int len ;
148
- int fddst = -1 ;
149
148
char * dupname = NULL ;
150
149
char * dupdir = NULL ;
151
150
const char * newname ;
152
151
char * dstdir ;
153
-
154
- ret = path_is_dir (dst );
155
- if (ret < 0 && ret != - ENOENT ) {
156
- errno = - ret ;
157
- error ("cannot access %s: %m" , dst );
158
- goto out ;
159
- }
160
- if (ret >= 0 ) {
161
- error ("target path already exists: %s" , dst );
162
- ret = - EEXIST ;
163
- goto out ;
164
- }
152
+ enum btrfs_util_error err ;
165
153
166
154
dupname = strdup (dst );
167
155
if (!dupname ) {
@@ -179,19 +167,6 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
179
167
}
180
168
dstdir = path_dirname (dupdir );
181
169
182
- if (!test_issubvolname (newname )) {
183
- error ("invalid subvolume name: %s" , newname );
184
- ret = - EINVAL ;
185
- goto out ;
186
- }
187
-
188
- len = strlen (newname );
189
- if (len > BTRFS_VOL_NAME_MAX ) {
190
- error ("subvolume name too long: %s" , newname );
191
- ret = - EINVAL ;
192
- goto out ;
193
- }
194
-
195
170
if (create_parents ) {
196
171
char p [PATH_MAX ] = { 0 };
197
172
char dstdir_dup [PATH_MAX ];
@@ -223,47 +198,57 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
223
198
}
224
199
}
225
200
226
- fddst = btrfs_open_dir (dstdir );
227
- if (fddst < 0 ) {
228
- ret = fddst ;
201
+ err = btrfs_util_subvolume_create (dst , 0 , NULL , inherit );
202
+ if (err ) {
203
+ error_btrfs_util (err );
204
+ ret = - errno ;
229
205
goto out ;
230
206
}
231
207
232
- if (inherit ) {
233
- struct btrfs_ioctl_vol_args_v2 args ;
208
+ pr_verbose (LOG_DEFAULT , "Create subvolume '%s/%s'\n" , dstdir , newname );
234
209
235
- memset (& args , 0 , sizeof (args ));
236
- strncpy_null (args .name , newname , sizeof (args .name ));
237
- args .flags |= BTRFS_SUBVOL_QGROUP_INHERIT ;
238
- args .size = btrfs_qgroup_inherit_size (inherit );
239
- args .qgroup_inherit = inherit ;
210
+ ret = 0 ;
240
211
241
- ret = ioctl ( fddst , BTRFS_IOC_SUBVOL_CREATE_V2 , & args );
242
- } else {
243
- struct btrfs_ioctl_vol_args args ;
212
+ out :
213
+ free ( dupname );
214
+ free ( dupdir ) ;
244
215
245
- memset (& args , 0 , sizeof (args ));
246
- strncpy_null (args .name , newname , sizeof (args .name ));
247
- ret = ioctl (fddst , BTRFS_IOC_SUBVOL_CREATE , & args );
216
+ return ret ;
217
+ }
218
+
219
+ static int qgroup_inherit_add_group (struct btrfs_util_qgroup_inherit * * inherit ,
220
+ const char * arg )
221
+ {
222
+ enum btrfs_util_error err ;
223
+ u64 qgroupid ;
224
+
225
+ if (!* inherit ) {
226
+ err = btrfs_util_qgroup_inherit_create (0 , inherit );
227
+ if (err ) {
228
+ error_btrfs_util (err );
229
+ return - errno ;
230
+ }
248
231
}
249
232
250
- if (ret < 0 ) {
251
- error ("cannot create subvolume: %m" );
252
- goto out ;
233
+ qgroupid = parse_qgroupid_or_path (optarg );
234
+ if (qgroupid == 0 ) {
235
+ error ("invalid qgroup specification, qgroupid must not be 0" );
236
+ return - EINVAL ;
253
237
}
254
- pr_verbose (LOG_DEFAULT , "Create subvolume '%s/%s'\n" , dstdir , newname );
255
238
256
- out :
257
- close (fddst );
258
- free (dupname );
259
- free (dupdir );
239
+ err = btrfs_util_qgroup_inherit_add_group (inherit , qgroupid );
240
+ if (err ) {
241
+ error_btrfs_util (err );
242
+ return - errno ;
243
+ }
260
244
261
- return ret ;
245
+ return 0 ;
262
246
}
247
+
263
248
static int cmd_subvolume_create (const struct cmd_struct * cmd , int argc , char * * argv )
264
249
{
265
250
int retval , ret ;
266
- struct btrfs_qgroup_inherit * inherit = NULL ;
251
+ struct btrfs_util_qgroup_inherit * inherit = NULL ;
267
252
bool has_error = false;
268
253
bool create_parents = false;
269
254
@@ -281,7 +266,7 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
281
266
282
267
switch (c ) {
283
268
case 'i' :
284
- ret = btrfs_qgroup_inherit_add_group (& inherit , optarg );
269
+ ret = qgroup_inherit_add_group (& inherit , optarg );
285
270
if (ret ) {
286
271
retval = ret ;
287
272
goto out ;
@@ -310,7 +295,7 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
310
295
if (!has_error )
311
296
retval = 0 ;
312
297
out :
313
- free (inherit );
298
+ btrfs_util_qgroup_inherit_destroy (inherit );
314
299
315
300
return retval ;
316
301
}
0 commit comments