]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/zramctl.c
zramctl: add --output-all option
[thirdparty/util-linux.git] / sys-utils / zramctl.c
1 /*
2 * zramctl - control compressed block devices in RAM
3 *
4 * Copyright (c) 2014 Timofey Titovets <Nefelim4ag@gmail.com>
5 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <assert.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28
29 #include <libsmartcols.h>
30
31 #include "c.h"
32 #include "nls.h"
33 #include "closestream.h"
34 #include "strutils.h"
35 #include "xalloc.h"
36 #include "sysfs.h"
37 #include "optutils.h"
38 #include "ismounted.h"
39 #include "strv.h"
40 #include "path.h"
41 #include "pathnames.h"
42
43 /*#define CONFIG_ZRAM_DEBUG*/
44
45 #ifdef CONFIG_ZRAM_DEBUG
46 # define DBG(x) do { fputs("zram: ", stderr); x; fputc('\n', stderr); } while(0)
47 #else
48 # define DBG(x)
49 #endif
50
51 /* status output columns */
52 struct colinfo {
53 const char *name;
54 double whint;
55 int flags;
56 const char *help;
57 };
58
59 enum {
60 COL_NAME = 0,
61 COL_DISKSIZE,
62 COL_ORIG_SIZE,
63 COL_COMP_SIZE,
64 COL_ALGORITHM,
65 COL_STREAMS,
66 COL_ZEROPAGES,
67 COL_MEMTOTAL,
68 COL_MEMLIMIT,
69 COL_MEMUSED,
70 COL_MIGRATED,
71 COL_MOUNTPOINT
72 };
73
74 static const struct colinfo infos[] = {
75 [COL_NAME] = { "NAME", 0.25, 0, N_("zram device name") },
76 [COL_DISKSIZE] = { "DISKSIZE", 5, SCOLS_FL_RIGHT, N_("limit on the uncompressed amount of data") },
77 [COL_ORIG_SIZE] = { "DATA", 5, SCOLS_FL_RIGHT, N_("uncompressed size of stored data") },
78 [COL_COMP_SIZE] = { "COMPR", 5, SCOLS_FL_RIGHT, N_("compressed size of stored data") },
79 [COL_ALGORITHM] = { "ALGORITHM", 3, 0, N_("the selected compression algorithm") },
80 [COL_STREAMS] = { "STREAMS", 3, SCOLS_FL_RIGHT, N_("number of concurrent compress operations") },
81 [COL_ZEROPAGES] = { "ZERO-PAGES", 3, SCOLS_FL_RIGHT, N_("empty pages with no allocated memory") },
82 [COL_MEMTOTAL] = { "TOTAL", 5, SCOLS_FL_RIGHT, N_("all memory including allocator fragmentation and metadata overhead") },
83 [COL_MEMLIMIT] = { "MEM-LIMIT", 5, SCOLS_FL_RIGHT, N_("memory limit used to store compressed data") },
84 [COL_MEMUSED] = { "MEM-USED", 5, SCOLS_FL_RIGHT, N_("memory zram have been consumed to store compressed data") },
85 [COL_MIGRATED] = { "MIGRATED", 5, SCOLS_FL_RIGHT, N_("number of objects migrated by compaction") },
86 [COL_MOUNTPOINT]= { "MOUNTPOINT",0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
87 };
88
89 static int columns[ARRAY_SIZE(infos) * 2] = {-1};
90 static int ncolumns;
91
92 enum {
93 MM_ORIG_DATA_SIZE = 0,
94 MM_COMPR_DATA_SIZE,
95 MM_MEM_USED_TOTAL,
96 MM_MEM_LIMIT,
97 MM_MEM_USED_MAX,
98 MM_ZERO_PAGES,
99 MM_NUM_MIGRATED
100 };
101
102 static const char *mm_stat_names[] = {
103 [MM_ORIG_DATA_SIZE] = "orig_data_size",
104 [MM_COMPR_DATA_SIZE] = "compr_data_size",
105 [MM_MEM_USED_TOTAL] = "mem_used_total",
106 [MM_MEM_LIMIT] = "mem_limit",
107 [MM_MEM_USED_MAX] = "mem_used_max",
108 [MM_ZERO_PAGES] = "zero_pages",
109 [MM_NUM_MIGRATED] = "num_migrated"
110 };
111
112 struct zram {
113 char devname[32];
114 struct sysfs_cxt sysfs;
115 char **mm_stat;
116
117 unsigned int mm_stat_probed : 1,
118 control_probed : 1,
119 has_control : 1; /* has /sys/class/zram-control/ */
120 };
121
122 static unsigned int raw, no_headings, inbytes;
123
124 static int get_column_id(int num)
125 {
126 assert(num < ncolumns);
127 assert(columns[num] < (int) ARRAY_SIZE(infos));
128 return columns[num];
129 }
130
131 static const struct colinfo *get_column_info(int num)
132 {
133 return &infos[ get_column_id(num) ];
134 }
135
136 static int column_name_to_id(const char *name, size_t namesz)
137 {
138 size_t i;
139
140 for (i = 0; i < ARRAY_SIZE(infos); i++) {
141 const char *cn = infos[i].name;
142
143 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
144 return i;
145 }
146 warnx(_("unknown column: %s"), name);
147 return -1;
148 }
149
150 static void zram_reset_stat(struct zram *z)
151 {
152 if (z) {
153 strv_free(z->mm_stat);
154 z->mm_stat = NULL;
155 z->mm_stat_probed = 0;
156 }
157 }
158
159 static void zram_set_devname(struct zram *z, const char *devname, size_t n)
160 {
161 assert(z);
162
163 if (!devname)
164 snprintf(z->devname, sizeof(z->devname), "/dev/zram%zu", n);
165 else {
166 strncpy(z->devname, devname, sizeof(z->devname));
167 z->devname[sizeof(z->devname) - 1] = '\0';
168 }
169
170 DBG(fprintf(stderr, "set devname: %s", z->devname));
171 sysfs_deinit(&z->sysfs);
172 zram_reset_stat(z);
173 }
174
175 static int zram_get_devnum(struct zram *z)
176 {
177 int n;
178
179 assert(z);
180
181 if (sscanf(z->devname, "/dev/zram%d", &n) == 1)
182 return n;
183 return -EINVAL;
184 }
185
186 static struct zram *new_zram(const char *devname)
187 {
188 struct zram *z = xcalloc(1, sizeof(struct zram));
189
190 DBG(fprintf(stderr, "new: %p", z));
191 if (devname)
192 zram_set_devname(z, devname, 0);
193 return z;
194 }
195
196 static void free_zram(struct zram *z)
197 {
198 if (!z)
199 return;
200 DBG(fprintf(stderr, "free: %p", z));
201 sysfs_deinit(&z->sysfs);
202 zram_reset_stat(z);
203 free(z);
204 }
205
206 static struct sysfs_cxt *zram_get_sysfs(struct zram *z)
207 {
208 assert(z);
209
210 if (!z->sysfs.devno) {
211 dev_t devno = sysfs_devname_to_devno(z->devname, NULL);
212 if (!devno)
213 return NULL;
214 if (sysfs_init(&z->sysfs, devno, NULL))
215 return NULL;
216 if (*z->devname != '/') {
217 /* canonicalize the device name according to /sys */
218 char name[PATH_MAX];
219 if (sysfs_get_devname(&z->sysfs, name, sizeof(name)))
220 snprintf(z->devname, sizeof(z->devname), "/dev/%s", name);
221 }
222 }
223
224 return &z->sysfs;
225 }
226
227 static inline int zram_exist(struct zram *z)
228 {
229 assert(z);
230
231 errno = 0;
232 if (zram_get_sysfs(z) == NULL) {
233 errno = ENODEV;
234 return 0;
235 }
236
237 DBG(fprintf(stderr, "%s exists", z->devname));
238 return 1;
239 }
240
241 static int zram_set_u64parm(struct zram *z, const char *attr, uint64_t num)
242 {
243 struct sysfs_cxt *sysfs = zram_get_sysfs(z);
244 if (!sysfs)
245 return -EINVAL;
246 DBG(fprintf(stderr, "%s writing %ju to %s", z->devname, num, attr));
247 return sysfs_write_u64(sysfs, attr, num);
248 }
249
250 static int zram_set_strparm(struct zram *z, const char *attr, const char *str)
251 {
252 struct sysfs_cxt *sysfs = zram_get_sysfs(z);
253 if (!sysfs)
254 return -EINVAL;
255 DBG(fprintf(stderr, "%s writing %s to %s", z->devname, str, attr));
256 return sysfs_write_string(sysfs, attr, str);
257 }
258
259
260 static int zram_used(struct zram *z)
261 {
262 uint64_t size;
263 struct sysfs_cxt *sysfs = zram_get_sysfs(z);
264
265 if (sysfs &&
266 sysfs_read_u64(sysfs, "disksize", &size) == 0 &&
267 size > 0) {
268
269 DBG(fprintf(stderr, "%s used", z->devname));
270 return 1;
271 }
272 DBG(fprintf(stderr, "%s unused", z->devname));
273 return 0;
274 }
275
276 static int zram_has_control(struct zram *z)
277 {
278 if (!z->control_probed) {
279 z->has_control = access(_PATH_SYS_CLASS "/zram-control/", F_OK) == 0 ? 1 : 0;
280 z->control_probed = 1;
281 DBG(fprintf(stderr, "zram-control: %s", z->has_control ? "yes" : "no"));
282 }
283
284 return z->has_control;
285 }
286
287 static int zram_control_add(struct zram *z)
288 {
289 int n;
290
291 if (!zram_has_control(z))
292 return -ENOSYS;
293
294 n = path_read_s32(_PATH_SYS_CLASS "/zram-control/hot_add");
295 if (n < 0)
296 return n;
297
298 DBG(fprintf(stderr, "hot-add: %d", n));
299 zram_set_devname(z, NULL, n);
300 return 0;
301 }
302
303 static int zram_control_remove(struct zram *z)
304 {
305 char str[sizeof stringify_value(INT_MAX)];
306 int n;
307
308 if (!zram_has_control(z))
309 return -ENOSYS;
310
311 n = zram_get_devnum(z);
312 if (n < 0)
313 return n;
314
315 DBG(fprintf(stderr, "hot-remove: %d", n));
316 snprintf(str, sizeof(str), "%d", n);
317 return path_write_str(str, _PATH_SYS_CLASS "/zram-control/hot_remove");
318 }
319
320 static struct zram *find_free_zram(void)
321 {
322 struct zram *z = new_zram(NULL);
323 size_t i;
324 int isfree = 0;
325
326 for (i = 0; isfree == 0; i++) {
327 DBG(fprintf(stderr, "find free: checking zram%zu", i));
328 zram_set_devname(z, NULL, i);
329 if (!zram_exist(z) && zram_control_add(z) != 0)
330 break;
331 isfree = !zram_used(z);
332 }
333 if (!isfree) {
334 free_zram(z);
335 z = NULL;
336 }
337 return z;
338 }
339
340 static char *get_mm_stat(struct zram *z, size_t idx, int bytes)
341 {
342 struct sysfs_cxt *sysfs;
343 const char *name;
344 uint64_t num;
345
346 assert(idx < ARRAY_SIZE(mm_stat_names));
347 assert(z);
348
349 sysfs = zram_get_sysfs(z);
350 if (!sysfs)
351 return NULL;
352
353 /* Linux >= 4.1 uses /sys/block/zram<id>/mm_stat */
354 if (!z->mm_stat && !z->mm_stat_probed) {
355 char *str;
356
357 str = sysfs_strdup(sysfs, "mm_stat");
358 if (str) {
359 z->mm_stat = strv_split(str, " ");
360
361 /* make sure kernel provides mm_stat as expected */
362 if (strv_length(z->mm_stat) < ARRAY_SIZE(mm_stat_names)) {
363 strv_free(z->mm_stat);
364 z->mm_stat = NULL;
365 }
366 }
367 z->mm_stat_probed = 1;
368 free(str);
369
370 }
371
372 if (z->mm_stat) {
373 if (bytes)
374 return xstrdup(z->mm_stat[idx]);
375
376 num = strtou64_or_err(z->mm_stat[idx], _("Failed to parse mm_stat"));
377 return size_to_human_string(SIZE_SUFFIX_1LETTER, num);
378 }
379
380 /* Linux < 4.1 uses /sys/block/zram<id>/<attrname> */
381 name = mm_stat_names[idx];
382 if (bytes)
383 return sysfs_strdup(sysfs, name);
384 else if (sysfs_read_u64(sysfs, name, &num) == 0)
385 return size_to_human_string(SIZE_SUFFIX_1LETTER, num);
386 return NULL;
387 }
388
389 static void fill_table_row(struct libscols_table *tb, struct zram *z)
390 {
391 static struct libscols_line *ln;
392 struct sysfs_cxt *sysfs;
393 size_t i;
394 uint64_t num;
395
396 assert(tb);
397 assert(z);
398
399 DBG(fprintf(stderr, "%s: filling status table", z->devname));
400
401 sysfs = zram_get_sysfs(z);
402 if (!sysfs)
403 return;
404
405 ln = scols_table_new_line(tb, NULL);
406 if (!ln)
407 err(EXIT_FAILURE, _("failed to allocate output line"));
408
409 for (i = 0; i < (size_t) ncolumns; i++) {
410 char *str = NULL;
411
412 switch (get_column_id(i)) {
413 case COL_NAME:
414 str = xstrdup(z->devname);
415 break;
416 case COL_DISKSIZE:
417 if (inbytes)
418 str = sysfs_strdup(sysfs, "disksize");
419 else if (sysfs_read_u64(sysfs, "disksize", &num) == 0)
420 str = size_to_human_string(SIZE_SUFFIX_1LETTER, num);
421 break;
422 case COL_ALGORITHM:
423 {
424 char *alg = sysfs_strdup(sysfs, "comp_algorithm");
425
426 if (alg != NULL) {
427 char* lbr = strrchr(alg, '[');
428 char* rbr = strrchr(alg, ']');
429
430 if (lbr != NULL && rbr != NULL && rbr - lbr > 1)
431 str = xstrndup(lbr + 1, rbr - lbr - 1);
432 free(alg);
433 }
434 break;
435 }
436 case COL_MOUNTPOINT:
437 {
438 char path[PATH_MAX] = { '\0' };
439 int fl;
440
441 check_mount_point(z->devname, &fl, path, sizeof(path));
442 if (*path)
443 str = xstrdup(path);
444 break;
445 }
446 case COL_STREAMS:
447 str = sysfs_strdup(sysfs, "max_comp_streams");
448 break;
449 case COL_ZEROPAGES:
450 str = get_mm_stat(z, MM_ZERO_PAGES, 1);
451 break;
452 case COL_ORIG_SIZE:
453 str = get_mm_stat(z, MM_ORIG_DATA_SIZE, inbytes);
454 break;
455 case COL_COMP_SIZE:
456 str = get_mm_stat(z, MM_COMPR_DATA_SIZE, inbytes);
457 break;
458 case COL_MEMTOTAL:
459 str = get_mm_stat(z, MM_MEM_USED_TOTAL, inbytes);
460 break;
461 case COL_MEMLIMIT:
462 str = get_mm_stat(z, MM_MEM_LIMIT, inbytes);
463 break;
464 case COL_MEMUSED:
465 str = get_mm_stat(z, MM_MEM_USED_MAX, inbytes);
466 break;
467 case COL_MIGRATED:
468 str = get_mm_stat(z, MM_NUM_MIGRATED, inbytes);
469 break;
470 }
471 if (str && scols_line_refer_data(ln, i, str))
472 err(EXIT_FAILURE, _("failed to add output data"));
473 }
474 }
475
476 static void status(struct zram *z)
477 {
478 struct libscols_table *tb;
479 size_t i;
480 DIR *dir;
481 struct dirent *d;
482
483 scols_init_debug(0);
484
485 tb = scols_new_table();
486 if (!tb)
487 err(EXIT_FAILURE, _("failed to allocate output table"));
488
489 scols_table_enable_raw(tb, raw);
490 scols_table_enable_noheadings(tb, no_headings);
491
492 for (i = 0; i < (size_t) ncolumns; i++) {
493 const struct colinfo *col = get_column_info(i);
494
495 if (!scols_table_new_column(tb, col->name, col->whint, col->flags))
496 err(EXIT_FAILURE, _("failed to initialize output column"));
497 }
498
499 if (z) {
500 /* just one device specified */
501 fill_table_row(tb, z);
502 goto print_table;
503 }
504
505 /* list all used devices */
506 z = new_zram(NULL);
507 if (!(dir = opendir(_PATH_DEV)))
508 err(EXIT_FAILURE, _("cannot open %s"), _PATH_DEV);
509
510 while ((d = readdir(dir))) {
511 int n;
512 if (sscanf(d->d_name, "zram%d", &n) != 1)
513 continue;
514 zram_set_devname(z, NULL, n);
515 if (zram_exist(z) && zram_used(z))
516 fill_table_row(tb, z);
517 }
518 closedir(dir);
519 free_zram(z);
520
521 print_table:
522 scols_print_table(tb);
523 scols_unref_table(tb);
524 }
525
526 static void __attribute__((__noreturn__)) usage(void)
527 {
528 FILE *out = stdout;
529 size_t i;
530
531 fputs(USAGE_HEADER, out);
532 fprintf(out, _( " %1$s [options] <device>\n"
533 " %1$s -r <device> [...]\n"
534 " %1$s [options] -f | <device> -s <size>\n"),
535 program_invocation_short_name);
536
537 fputs(USAGE_SEPARATOR, out);
538 fputs(_("Set up and control zram devices.\n"), out);
539
540 fputs(USAGE_OPTIONS, out);
541 fputs(_(" -a, --algorithm lzo|lz4|lz4hc|deflate|842 compression algorithm to use\n"), out);
542 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out);
543 fputs(_(" -f, --find find a free device\n"), out);
544 fputs(_(" -n, --noheadings don't print headings\n"), out);
545 fputs(_(" -o, --output <list> columns to use for status output\n"), out);
546 fputs(_(" --output-all output all columns\n"), out);
547 fputs(_(" --raw use raw status output format\n"), out);
548 fputs(_(" -r, --reset reset all specified devices\n"), out);
549 fputs(_(" -s, --size <size> device size\n"), out);
550 fputs(_(" -t, --streams <number> number of compression streams\n"), out);
551
552 fputs(USAGE_SEPARATOR, out);
553 printf(USAGE_HELP_OPTIONS(27));
554
555 fputs(USAGE_COLUMNS, out);
556 for (i = 0; i < ARRAY_SIZE(infos); i++)
557 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
558
559 printf(USAGE_MAN_TAIL("zramctl(8)"));
560 exit(EXIT_SUCCESS);
561 }
562
563 /* actions */
564 enum {
565 A_NONE = 0,
566 A_STATUS,
567 A_CREATE,
568 A_FINDONLY,
569 A_RESET
570 };
571
572 int main(int argc, char **argv)
573 {
574 uintmax_t size = 0, nstreams = 0;
575 char *algorithm = NULL;
576 int rc = 0, c, find = 0, act = A_NONE;
577 struct zram *zram = NULL;
578
579 enum {
580 OPT_RAW = CHAR_MAX + 1,
581 OPT_LIST_TYPES
582 };
583
584 static const struct option longopts[] = {
585 { "algorithm", required_argument, NULL, 'a' },
586 { "bytes", no_argument, NULL, 'b' },
587 { "find", no_argument, NULL, 'f' },
588 { "help", no_argument, NULL, 'h' },
589 { "output", required_argument, NULL, 'o' },
590 { "output-all",no_argument, NULL, OPT_LIST_TYPES },
591 { "noheadings",no_argument, NULL, 'n' },
592 { "reset", no_argument, NULL, 'r' },
593 { "raw", no_argument, NULL, OPT_RAW },
594 { "size", required_argument, NULL, 's' },
595 { "streams", required_argument, NULL, 't' },
596 { "version", no_argument, NULL, 'V' },
597 { NULL, 0, NULL, 0 }
598 };
599
600 static const ul_excl_t excl[] = {
601 { 'f', 'o', 'r' },
602 { 'o', 'r', 's' },
603 { 0 }
604 };
605 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
606
607 setlocale(LC_ALL, "");
608 bindtextdomain(PACKAGE, LOCALEDIR);
609 textdomain(PACKAGE);
610 atexit(close_stdout);
611
612 while ((c = getopt_long(argc, argv, "a:bfho:nrs:t:V", longopts, NULL)) != -1) {
613
614 err_exclusive_options(c, longopts, excl, excl_st);
615
616 switch (c) {
617 case 'a':
618 algorithm = optarg;
619 break;
620 case 'b':
621 inbytes = 1;
622 break;
623 case 'f':
624 find = 1;
625 break;
626 case 'o':
627 ncolumns = string_to_idarray(optarg,
628 columns, ARRAY_SIZE(columns),
629 column_name_to_id);
630 if (ncolumns < 0)
631 return EXIT_FAILURE;
632 break;
633 case OPT_LIST_TYPES:
634 for (ncolumns = 0; (size_t)ncolumns < ARRAY_SIZE(infos); ncolumns++)
635 columns[ncolumns] = ncolumns;
636 break;
637 case 's':
638 size = strtosize_or_err(optarg, _("failed to parse size"));
639 act = A_CREATE;
640 break;
641 case 't':
642 nstreams = strtou64_or_err(optarg, _("failed to parse streams"));
643 break;
644 case 'r':
645 act = A_RESET;
646 break;
647 case OPT_RAW:
648 raw = 1;
649 break;
650 case 'n':
651 no_headings = 1;
652 break;
653 case 'V':
654 printf(UTIL_LINUX_VERSION);
655 return EXIT_SUCCESS;
656 case 'h':
657 usage();
658 default:
659 errtryhelp(EXIT_FAILURE);
660 }
661 }
662
663 if (find && optind < argc)
664 errx(EXIT_FAILURE, _("option --find is mutually exclusive "
665 "with <device>"));
666 if (act == A_NONE)
667 act = find ? A_FINDONLY : A_STATUS;
668
669 if (act != A_RESET && optind + 1 < argc)
670 errx(EXIT_FAILURE, _("only one <device> at a time is allowed"));
671
672 if ((act == A_STATUS || act == A_FINDONLY) && (algorithm || nstreams))
673 errx(EXIT_FAILURE, _("options --algorithm and --streams "
674 "must be combined with --size"));
675
676 switch (act) {
677 case A_STATUS:
678 if (!ncolumns) { /* default columns */
679 columns[ncolumns++] = COL_NAME;
680 columns[ncolumns++] = COL_ALGORITHM;
681 columns[ncolumns++] = COL_DISKSIZE;
682 columns[ncolumns++] = COL_ORIG_SIZE;
683 columns[ncolumns++] = COL_COMP_SIZE;
684 columns[ncolumns++] = COL_MEMTOTAL;
685 columns[ncolumns++] = COL_STREAMS;
686 columns[ncolumns++] = COL_MOUNTPOINT;
687 }
688 if (optind < argc) {
689 zram = new_zram(argv[optind++]);
690 if (!zram_exist(zram))
691 err(EXIT_FAILURE, "%s", zram->devname);
692 }
693 status(zram);
694 free_zram(zram);
695 break;
696 case A_RESET:
697 if (optind == argc)
698 errx(EXIT_FAILURE, _("no device specified"));
699 while (optind < argc) {
700 zram = new_zram(argv[optind]);
701 if (!zram_exist(zram)
702 || zram_set_u64parm(zram, "reset", 1)) {
703 warn(_("%s: failed to reset"), zram->devname);
704 rc = 1;
705 }
706 zram_control_remove(zram);
707 free_zram(zram);
708 optind++;
709 }
710 break;
711 case A_FINDONLY:
712 zram = find_free_zram();
713 if (!zram)
714 errx(EXIT_FAILURE, _("no free zram device found"));
715 printf("%s\n", zram->devname);
716 free_zram(zram);
717 break;
718 case A_CREATE:
719 if (find) {
720 zram = find_free_zram();
721 if (!zram)
722 errx(EXIT_FAILURE, _("no free zram device found"));
723 } else if (optind == argc)
724 errx(EXIT_FAILURE, _("no device specified"));
725 else {
726 zram = new_zram(argv[optind]);
727 if (!zram_exist(zram))
728 err(EXIT_FAILURE, "%s", zram->devname);
729 }
730
731 if (zram_set_u64parm(zram, "reset", 1))
732 err(EXIT_FAILURE, _("%s: failed to reset"), zram->devname);
733
734 if (nstreams &&
735 zram_set_u64parm(zram, "max_comp_streams", nstreams))
736 err(EXIT_FAILURE, _("%s: failed to set number of streams"), zram->devname);
737
738 if (algorithm &&
739 zram_set_strparm(zram, "comp_algorithm", algorithm))
740 err(EXIT_FAILURE, _("%s: failed to set algorithm"), zram->devname);
741
742 if (zram_set_u64parm(zram, "disksize", size))
743 err(EXIT_FAILURE, _("%s: failed to set disksize (%ju bytes)"),
744 zram->devname, size);
745 if (find)
746 printf("%s\n", zram->devname);
747 free_zram(zram);
748 break;
749 }
750
751 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
752 }