~ubuntu-branches/debian/jessie/btrfs-tools/jessie

« back to all changes in this revision

Viewing changes to cmds-device.c

  • Committer: Package Import Robot
  • Author(s): Dimitri John Ledkov
  • Date: 2014-04-19 12:12:11 UTC
  • mfrom: (1.2.12) (6.1.37 sid)
  • Revision ID: package-import@ubuntu.com-20140419121211-mski0g757tsdv4x1
Tags: 3.14.1-1
* New upstream release.
* Switch to git-dpm.
* Rebase and cleanup patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
 
86
86
        fdmnt = open_file_or_dir(mntpnt, &dirstream);
87
87
        if (fdmnt < 0) {
88
 
                fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
 
88
                fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
89
89
                return 1;
90
90
        }
91
91
 
111
111
 
112
112
                res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
113
113
                                           0, &mixed, discard);
114
 
                if (res) {
115
 
                        fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
116
 
                        close(devfd);
117
 
                        ret++;
118
 
                        continue;
119
 
                }
120
114
                close(devfd);
 
115
                if (res) {
 
116
                        ret++;
 
117
                        goto error_out;
 
118
                }
121
119
 
122
120
                strncpy_null(ioctl_args.name, argv[i]);
123
121
                res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
130
128
 
131
129
        }
132
130
 
 
131
error_out:
133
132
        close_file_or_dir(fdmnt, dirstream);
134
133
        return !!ret;
135
134
}
153
152
 
154
153
        fdmnt = open_file_or_dir(mntpnt, &dirstream);
155
154
        if (fdmnt < 0) {
156
 
                fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
 
155
                fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
157
156
                return 1;
158
157
        }
159
158
 
161
160
                struct  btrfs_ioctl_vol_args arg;
162
161
                int     res;
163
162
 
 
163
                if (!is_block_device(argv[i])) {
 
164
                        fprintf(stderr,
 
165
                                "ERROR: %s is not a block device\n", argv[i]);
 
166
                        ret++;
 
167
                        continue;
 
168
                }
164
169
                strncpy_null(arg.name, argv[i]);
165
170
                res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
166
171
                e = errno;
182
187
}
183
188
 
184
189
static const char * const cmd_scan_dev_usage[] = {
185
 
        "btrfs device scan [<--all-devices>|<device> [<device>...]]",
 
190
        "btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
186
191
        "Scan devices for a btrfs filesystem",
187
192
        NULL
188
193
};
189
194
 
190
195
static int cmd_scan_dev(int argc, char **argv)
191
196
{
192
 
        int     i, fd, e;
193
 
        int     where = BTRFS_SCAN_LBLKID;
194
 
        int     devstart = 1;
 
197
        int i, fd, e;
 
198
        int where = BTRFS_SCAN_LBLKID;
 
199
        int devstart = 1;
 
200
        int all = 0;
 
201
        int ret = 0;
195
202
 
196
 
        if( argc > 1 && !strcmp(argv[1],"--all-devices")){
197
 
                if (check_argc_max(argc, 2))
 
203
        optind = 1;
 
204
        while (1) {
 
205
                int long_index;
 
206
                static struct option long_options[] = {
 
207
                        { "all-devices", no_argument, NULL, 'd'},
 
208
                        { 0, 0, 0, 0 },
 
209
                };
 
210
                int c = getopt_long(argc, argv, "d", long_options,
 
211
                                    &long_index);
 
212
                if (c < 0)
 
213
                        break;
 
214
                switch (c) {
 
215
                case 'd':
 
216
                        where = BTRFS_SCAN_DEV;
 
217
                        all = 1;
 
218
                        break;
 
219
                default:
198
220
                        usage(cmd_scan_dev_usage);
199
 
 
200
 
                where = BTRFS_SCAN_DEV;
201
 
                devstart += 1;
 
221
                }
202
222
        }
203
223
 
204
 
        if(argc<=devstart){
205
 
                int ret;
 
224
        if (all && check_argc_max(argc, 2))
 
225
                usage(cmd_scan_dev_usage);
 
226
 
 
227
        if (all || argc == 1) {
206
228
                printf("Scanning for Btrfs filesystems\n");
207
229
                ret = scan_for_btrfs(where, BTRFS_UPDATE_KERNEL);
208
 
                if (ret){
 
230
                if (ret)
209
231
                        fprintf(stderr, "ERROR: error %d while scanning\n", ret);
210
 
                        return 1;
211
 
                }
212
 
                return 0;
 
232
                goto out;
213
233
        }
214
234
 
215
235
        fd = open("/dev/btrfs-control", O_RDWR);
216
236
        if (fd < 0) {
217
237
                perror("failed to open /dev/btrfs-control");
218
 
                return 1;
 
238
                ret = 1;
 
239
                goto out;
219
240
        }
220
241
 
221
242
        for( i = devstart ; i < argc ; i++ ){
222
243
                struct btrfs_ioctl_vol_args args;
223
 
                int ret;
224
244
 
 
245
                if (!is_block_device(argv[i])) {
 
246
                        fprintf(stderr,
 
247
                                "ERROR: %s is not a block device\n", argv[i]);
 
248
                        ret = 1;
 
249
                        goto close_out;
 
250
                }
225
251
                printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
226
252
 
227
253
                strncpy_null(args.name, argv[i]);
234
260
                e = errno;
235
261
 
236
262
                if( ret < 0 ){
237
 
                        close(fd);
238
263
                        fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
239
264
                                argv[i], strerror(e));
240
 
                        return 1;
 
265
                        goto close_out;
241
266
                }
242
267
        }
243
268
 
 
269
close_out:
244
270
        close(fd);
245
 
        return 0;
 
271
out:
 
272
        return !!ret;
246
273
}
247
274
 
248
275
static const char * const cmd_ready_dev_usage[] = {
265
292
                perror("failed to open /dev/btrfs-control");
266
293
                return 1;
267
294
        }
 
295
        if (!is_block_device(argv[1])) {
 
296
                fprintf(stderr,
 
297
                        "ERROR: %s is not a block device\n", argv[1]);
 
298
                close(fd);
 
299
                return 1;
 
300
        }
268
301
 
269
302
        strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX);
270
303
        ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
306
339
                        break;
307
340
                case '?':
308
341
                default:
309
 
                        fprintf(stderr, "ERROR: device stat args invalid.\n"
310
 
                                        " device stat [-z] <path>|<device>\n"
311
 
                                        " -z  to reset stats after reading.\n");
312
 
                        return 1;
 
342
                        usage(cmd_dev_stats_usage);
313
343
                }
314
344
        }
315
345
 
316
 
        if (optind + 1 != argc) {
317
 
                fprintf(stderr, "ERROR: device stat needs path|device as single"
318
 
                        " argument\n");
319
 
                return 1;
320
 
        }
 
346
        argc = argc - optind;
 
347
        if (check_argc_exact(argc, 1))
 
348
                usage(cmd_dev_stats_usage);
321
349
 
322
350
        dev_path = argv[optind];
323
351