]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/zramctl.c
zramctl: remove extra \n from usage()
[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
27 #include <libsmartcols.h>
28
29 #include "c.h"
30 #include "nls.h"
31 #include "closestream.h"
32 #include "strutils.h"
33 #include "xalloc.h"
34 #include "sysfs.h"
35 #include "optutils.h"
36 #include "ismounted.h"
37
38 /*#define CONFIG_ZRAM_DEBUG*/
39
40 #ifdef CONFIG_ZRAM_DEBUG
41 # define DBG(x) do { fputs("zram: ", stderr); x; fputc('\n', stderr); } while(0)
42 #else
43 # define DBG(x)
44 #endif
45
46 /* status output columns */
47 struct colinfo {
48 const char *name;
49 double whint;
50 int flags;
51 const char *help;
52 };
53
54 enum {
55 COL_NAME = 0,
56 COL_DISKSIZE,
57 COL_ORIG_SIZE,
58 COL_COMP_SIZE,
59 COL_ALGORITHM,
60 COL_STREAMS,
61 COL_ZEROPAGES,
62 COL_MEMTOTAL,
63 COL_MOUNTPOINT
64 };
65
66 static const struct colinfo infos[] = {
67 [COL_NAME] = { "NAME", 0.25, 0, N_("zram device name") },
68 [COL_DISKSIZE] = { "DISKSIZE", 5, SCOLS_FL_RIGHT, N_("limit on the uncompressed amount of data") },
69 [COL_ORIG_SIZE] = { "DATA", 5, SCOLS_FL_RIGHT, N_("uncompressed size of stored data") },
70 [COL_COMP_SIZE] = { "COMPR", 5, SCOLS_FL_RIGHT, N_("compressed size of stored data") },
71 [COL_ALGORITHM] = { "ALGORITHM", 3, 0, N_("the selected compression algorithm") },
72 [COL_STREAMS] = { "STREAMS", 3, SCOLS_FL_RIGHT, N_("number of concurrent compress operations") },
73 [COL_ZEROPAGES] = { "ZERO-PAGES", 3, SCOLS_FL_RIGHT, N_("empty pages with no allocated memory") },
74 [COL_MEMTOTAL] = { "TOTAL", 5, SCOLS_FL_RIGHT, N_("all memory including allocator fragmentation and metadata overhead") },
75 [COL_MOUNTPOINT]= { "MOUNTPOINT",0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
76 };
77
78 static int columns[ARRAY_SIZE(infos) * 2] = {-1};
79 static int ncolumns;
80
81 struct zram {
82 char devname[32];
83 struct sysfs_cxt sysfs;
84 };
85
86 #define ZRAM_EMPTY { .devname = { '\0' }, .sysfs = UL_SYSFSCXT_EMPTY }
87
88 static unsigned int raw, no_headings, inbytes;
89
90
91 static int get_column_id(int num)
92 {
93 assert(num < ncolumns);
94 assert(columns[num] < (int) ARRAY_SIZE(infos));
95 return columns[num];
96 }
97
98 static const struct colinfo *get_column_info(int num)
99 {
100 return &infos[ get_column_id(num) ];
101 }
102
103 static int column_name_to_id(const char *name, size_t namesz)
104 {
105 size_t i;
106
107 for (i = 0; i < ARRAY_SIZE(infos); i++) {
108 const char *cn = infos[i].name;
109
110 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
111 return i;
112 }
113 warnx(_("unknown column: %s"), name);
114 return -1;
115 }
116
117 static void zram_set_devname(struct zram *z, const char *devname, size_t n)
118 {
119 assert(z);
120
121 if (!devname)
122 snprintf(z->devname, sizeof(z->devname), "/dev/zram%zu", n);
123 else {
124 strncpy(z->devname, devname, sizeof(z->devname));
125 z->devname[sizeof(z->devname) - 1] = '\0';
126 }
127
128 DBG(fprintf(stderr, "set devname: %s", z->devname));
129 sysfs_deinit(&z->sysfs);
130 }
131
132 static struct zram *new_zram(const char *devname)
133 {
134 struct zram *z = xcalloc(1, sizeof(struct zram));
135
136 DBG(fprintf(stderr, "new: %p", z));
137 if (devname)
138 zram_set_devname(z, devname, 0);
139 return z;
140 }
141
142 static void free_zram(struct zram *z)
143 {
144 if (!z)
145 return;
146 DBG(fprintf(stderr, "free: %p", z));
147 sysfs_deinit(&z->sysfs);
148 free(z);
149 }
150
151 static struct sysfs_cxt *zram_get_sysfs(struct zram *z)
152 {
153 assert(z);
154
155 if (!z->sysfs.devno) {
156 dev_t devno = sysfs_devname_to_devno(z->devname, NULL);
157 if (!devno)
158 return NULL;
159 if (sysfs_init(&z->sysfs, devno, NULL))
160 return NULL;
161 }
162
163 return &z->sysfs;
164 }
165
166 static inline int zram_exist(struct zram *z)
167 {
168 assert(z);
169
170 if (zram_get_sysfs(z) == NULL)
171 return 0;
172
173 DBG(fprintf(stderr, "%s exists", z->devname));
174 return 1;
175 }
176
177 static int zram_set_u64parm(struct zram *z, const char *attr, uint64_t num)
178 {
179 struct sysfs_cxt *sysfs = zram_get_sysfs(z);
180 if (!sysfs)
181 return -EINVAL;
182 DBG(fprintf(stderr, "%s writing %ju to %s", z->devname, num, attr));
183 return sysfs_write_u64(sysfs, attr, num);
184 }
185
186 static int zram_set_strparm(struct zram *z, const char *attr, const char *str)
187 {
188 struct sysfs_cxt *sysfs = zram_get_sysfs(z);
189 if (!sysfs)
190 return -EINVAL;
191 DBG(fprintf(stderr, "%s writing %s to %s", z->devname, str, attr));
192 return sysfs_write_string(sysfs, attr, str);
193 }
194
195
196 static int zram_used(struct zram *z)
197 {
198 uint64_t size;
199 struct sysfs_cxt *sysfs = zram_get_sysfs(z);
200
201 if (sysfs &&
202 sysfs_read_u64(sysfs, "disksize", &size) == 0 &&
203 size > 0) {
204
205 DBG(fprintf(stderr, "%s used", z->devname));
206 return 1;
207 }
208 DBG(fprintf(stderr, "%s unused", z->devname));
209 return 0;
210 }
211
212 static struct zram *find_free_zram(void)
213 {
214 struct zram *z = new_zram(NULL);
215 size_t i;
216 int isfree = 0;
217
218 for (i = 0; isfree == 0; i++) {
219 DBG(fprintf(stderr, "find free: checking zram%zu", i));
220 zram_set_devname(z, NULL, i);
221 if (!zram_exist(z))
222 break;
223 isfree = !zram_used(z);
224 }
225 if (!isfree) {
226 free_zram(z);
227 z = NULL;
228 }
229 return z;
230 }
231
232 static void fill_table_row(struct libscols_table *tb, struct zram *z)
233 {
234 static struct libscols_line *ln;
235 struct sysfs_cxt *sysfs;
236 size_t i;
237 uint64_t num;
238
239 assert(tb);
240 assert(z);
241
242 DBG(fprintf(stderr, "%s: filling status table", z->devname));
243
244 sysfs = zram_get_sysfs(z);
245 if (!sysfs)
246 return;
247
248 ln = scols_table_new_line(tb, NULL);
249 if (!ln)
250 err(EXIT_FAILURE, _("failed to initialize output line"));
251
252 for (i = 0; i < (size_t) ncolumns; i++) {
253 char *str = NULL;
254
255 switch (get_column_id(i)) {
256 case COL_NAME:
257 str = xstrdup(z->devname);
258 break;
259 case COL_DISKSIZE:
260 if (inbytes)
261 str = sysfs_strdup(sysfs, "disksize");
262 else if (sysfs_read_u64(sysfs, "disksize", &num) == 0)
263 str = size_to_human_string(SIZE_SUFFIX_1LETTER, num);
264 break;
265 case COL_ORIG_SIZE:
266 if (inbytes)
267 str = sysfs_strdup(sysfs, "orig_data_size");
268 else if (sysfs_read_u64(sysfs, "orig_data_size", &num) == 0)
269 str = size_to_human_string(SIZE_SUFFIX_1LETTER, num);
270 break;
271 case COL_COMP_SIZE:
272 if (inbytes)
273 str = sysfs_strdup(sysfs, "compr_data_size");
274 else if (sysfs_read_u64(sysfs, "compr_data_size", &num) == 0)
275 str = size_to_human_string(SIZE_SUFFIX_1LETTER, num);
276 break;
277 case COL_ALGORITHM:
278 {
279 char *alg = sysfs_strdup(sysfs, "comp_algorithm");
280 if (!alg)
281 break;
282 if (strstr(alg, "[lzo]") == NULL) {
283 if (strstr(alg, "[lz4]") == NULL)
284 ;
285 else
286 str = xstrdup("lz4");
287 } else
288 str = xstrdup("lzo");
289 free(alg);
290 break;
291 }
292 case COL_MOUNTPOINT:
293 {
294 char path[PATH_MAX] = { '\0' };
295 int fl;
296
297 check_mount_point(z->devname, &fl, path, sizeof(path));
298 if (*path)
299 str = xstrdup(path);
300 break;
301 }
302 case COL_STREAMS:
303 str = sysfs_strdup(sysfs, "max_comp_streams");
304 break;
305 case COL_ZEROPAGES:
306 str = sysfs_strdup(sysfs, "zero_pages");
307 break;
308 case COL_MEMTOTAL:
309 if (inbytes)
310 str = sysfs_strdup(sysfs, "mem_used_total");
311 else if (sysfs_read_u64(sysfs, "mem_used_total", &num) == 0)
312 str = size_to_human_string(SIZE_SUFFIX_1LETTER, num);
313 break;
314 }
315
316 if (str)
317 scols_line_refer_data(ln, i, str);
318 }
319 }
320
321 static void status(struct zram *z)
322 {
323 struct libscols_table *tb;
324 size_t i;
325
326 scols_init_debug(0);
327
328 tb = scols_new_table();
329 if (!tb)
330 err(EXIT_FAILURE, _("failed to initialize output table"));
331
332 scols_table_enable_raw(tb, raw);
333 scols_table_enable_noheadings(tb, no_headings);
334
335 for (i = 0; i < (size_t) ncolumns; i++) {
336 const struct colinfo *col = get_column_info(i);
337
338 if (!scols_table_new_column(tb, col->name, col->whint, col->flags))
339 err(EXIT_FAILURE, _("failed to initialize output column"));
340 }
341
342 if (z)
343 fill_table_row(tb, z); /* just one device specified */
344 else {
345 size_t i; /* list all used devices */
346 z = new_zram(NULL);
347
348 for (i = 0; ; i++) {
349 zram_set_devname(z, NULL, i);
350 if (!zram_exist(z))
351 break;
352 if (zram_used(z))
353 fill_table_row(tb, z);
354 }
355 free_zram(z);
356 }
357
358 scols_print_table(tb);
359 scols_unref_table(tb);
360 }
361
362 static void __attribute__ ((__noreturn__)) usage(FILE * out)
363 {
364 size_t i;
365
366 fputs(USAGE_HEADER, out);
367 fprintf(out, _( " %1$s [options] <device>\n"
368 " %1$s -r <device> [...]\n"
369 " %1$s [options] -f | <device> -s <size>\n"),
370 program_invocation_short_name);
371
372 fputs(USAGE_OPTIONS, out);
373 fputs(_(" -a, --algorithm lzo|lz4 compression algorithm to use\n"), out);
374 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out);
375 fputs(_(" -f, --find find a free device\n"), out);
376 fputs(_(" -n, --noheadings don't print headings\n"), out);
377 fputs(_(" -o, --output <list> columns to use for status output\n"), out);
378 fputs(_(" --raw use raw status output format\n"), out);
379 fputs(_(" -r, --reset reset all specified devices\n"), out);
380 fputs(_(" -s, --size <size> device size\n"), out);
381 fputs(_(" -t, --streams <number> number of compression streams\n"), out);
382
383 fputs(USAGE_SEPARATOR, out);
384 fputs(USAGE_HELP, out);
385 fputs(USAGE_VERSION, out);
386
387 fputs(_("\nAvailable columns (for --output):\n"), out);
388 for (i = 0; i < ARRAY_SIZE(infos); i++)
389 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
390
391 fprintf(out, USAGE_MAN_TAIL("zramctl(8)"));
392 exit(out == stderr ? 1 : EXIT_SUCCESS);
393 }
394
395 /* actions */
396 enum {
397 A_NONE = 0,
398 A_STATUS,
399 A_CREATE,
400 A_FINDONLY,
401 A_RESET
402 };
403
404 int main(int argc, char **argv)
405 {
406 uintmax_t size = 0, nstreams = 0;
407 char *algorithm = NULL;
408 int rc = 0, c, find = 0, act = A_NONE;
409 struct zram *zram = NULL;
410
411 enum { OPT_RAW = CHAR_MAX + 1 };
412
413 static const struct option longopts[] = {
414 { "algorithm", required_argument, NULL, 'a' },
415 { "bytes", no_argument, NULL, 'b' },
416 { "find", no_argument, NULL, 'f' },
417 { "help", no_argument, NULL, 'h' },
418 { "output", required_argument, NULL, 'o' },
419 { "noheadings",no_argument, NULL, 'n' },
420 { "reset", no_argument, NULL, 'r' },
421 { "raw", no_argument, NULL, OPT_RAW },
422 { "size", required_argument, NULL, 's' },
423 { "streams", required_argument, NULL, 't' },
424 { "version", no_argument, NULL, 'V' },
425 { NULL, 0, NULL, 0 }
426 };
427
428 static const ul_excl_t excl[] = {
429 { 'f', 'o', 'r' },
430 { 'o', 'r', 's' },
431 { 0 }
432 };
433 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
434
435 setlocale(LC_ALL, "");
436 bindtextdomain(PACKAGE, LOCALEDIR);
437 textdomain(PACKAGE);
438 atexit(close_stdout);
439
440 while ((c = getopt_long(argc, argv, "a:bfho:nrs:t:V", longopts, NULL)) != -1) {
441
442 err_exclusive_options(c, longopts, excl, excl_st);
443
444 switch (c) {
445 case 'a':
446 if (strcmp(optarg,"lzo") && strcmp(optarg,"lz4"))
447 errx(EXIT_FAILURE, _("unsupported algorithm: %s"),
448 optarg);
449 algorithm = optarg;
450 break;
451 case 'b':
452 inbytes = 1;
453 break;
454 case 'f':
455 find = 1;
456 break;
457 case 'o':
458 ncolumns = string_to_idarray(optarg,
459 columns, ARRAY_SIZE(columns),
460 column_name_to_id);
461 if (ncolumns < 0)
462 return EXIT_FAILURE;
463 break;
464 case 's':
465 size = strtosize_or_err(optarg, _("failed to parse size"));
466 act = A_CREATE;
467 break;
468 case 't':
469 nstreams = strtou64_or_err(optarg, _("failed to parse streams"));
470 break;
471 case 'r':
472 act = A_RESET;
473 break;
474 case OPT_RAW:
475 raw = 1;
476 break;
477 case 'n':
478 no_headings = 1;
479 break;
480 case 'V':
481 printf(UTIL_LINUX_VERSION);
482 return EXIT_SUCCESS;
483 case 'h':
484 usage(stdout);
485 default:
486 usage(stderr);
487 }
488 }
489
490 if (find && optind < argc)
491 errx(EXIT_FAILURE, _("option --find is mutually exclusive "
492 "with <device>"));
493 if (act == A_NONE)
494 act = find ? A_FINDONLY : A_STATUS;
495
496 if (act != A_RESET && optind + 1 < argc)
497 usage(stderr); /* only --reset holds more devices */
498
499 switch (act) {
500 case A_STATUS:
501 if (algorithm || find || nstreams)
502 errx(EXIT_FAILURE, _("options --algorithm, --find and "
503 "--streams are mutually exclusive"));
504 if (!ncolumns) { /* default columns */
505 columns[ncolumns++] = COL_NAME;
506 columns[ncolumns++] = COL_ALGORITHM;
507 columns[ncolumns++] = COL_DISKSIZE;
508 columns[ncolumns++] = COL_ORIG_SIZE;
509 columns[ncolumns++] = COL_COMP_SIZE;
510 columns[ncolumns++] = COL_MEMTOTAL;
511 columns[ncolumns++] = COL_STREAMS;
512 columns[ncolumns++] = COL_MOUNTPOINT;
513 }
514 if (optind < argc)
515 zram = new_zram(argv[optind++]);
516 status(zram);
517 free_zram(zram);
518 break;
519 case A_RESET:
520 if (optind == argc)
521 errx(EXIT_FAILURE, _("no device specified"));
522 while (optind < argc) {
523 zram = new_zram(argv[optind]);
524 if (zram_set_u64parm(zram, "reset", 1)) {
525 warn(_("%s: failed to reset"), zram->devname);
526 rc = 1;
527 }
528 free_zram(zram);
529 optind++;
530 }
531 break;
532 case A_FINDONLY:
533 zram = find_free_zram();
534 if (!zram)
535 errx(EXIT_FAILURE, _("no free zram device found"));
536 printf("%s\n", zram->devname);
537 free_zram(zram);
538 break;
539 case A_CREATE:
540 if (find) {
541 zram = find_free_zram();
542 if (!zram)
543 errx(EXIT_FAILURE, _("no free zram device found"));
544 } else if (optind == argc)
545 errx(EXIT_FAILURE, _("no device specified"));
546 else
547 zram = new_zram(argv[optind]);
548
549 if (zram_set_u64parm(zram, "reset", 1))
550 err(EXIT_FAILURE, _("%s: failed to reset"), zram->devname);
551
552 if (nstreams &&
553 zram_set_u64parm(zram, "max_comp_streams", nstreams))
554 err(EXIT_FAILURE, _("%s: failed to set number of streams"), zram->devname);
555
556 if (algorithm &&
557 zram_set_strparm(zram, "comp_algorithm", algorithm))
558 err(EXIT_FAILURE, _("%s: failed to set algorithm"), zram->devname);
559
560 if (zram_set_u64parm(zram, "disksize", size))
561 err(EXIT_FAILURE, _("%s: failed to set disksize (%ju bytes)"),
562 zram->devname, size);
563 if (find)
564 printf("%s\n", zram->devname);
565 free_zram(zram);
566 break;
567 }
568
569 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
570 }