File: lssu.c

package info (click to toggle)
nilfs-tools 2.2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,852 kB
  • sloc: ansic: 15,594; sh: 4,374; perl: 4,174; makefile: 140
file content (393 lines) | stat: -rw-r--r-- 9,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * lssu.c - NILFS command of listing segment usage information.
 *
 * Copyright (C) 2007-2012 Nippon Telegraph and Telephone Corporation.
 *
 * This file is part of NILFS.
 *
 * NILFS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * NILFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Written by Koji Sato.
 *
 * Maintained by Ryusuke Konishi <konishi.ryusuke@gmail.com> from 2008.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif	/* HAVE_CONFIG_H */

#include <stdio.h>

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif	/* HAVE_STDLIB_H */

#if HAVE_ERR_H
#include <err.h>
#endif	/* HAVE_ERR_H */

#if HAVE_STRING_H
#include <string.h>
#endif	/* HAVE_STRING_H */

#if HAVE_LIMITS_H
#include <limits.h>
#endif	/* HAVE_LIMITS_H */

#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif	/* HAVE_SYS_TIME */

#if HAVE_TIME_H
#include <time.h>
#endif	/* HAVE_TIME_H */

#include <errno.h>
#include "nilfs.h"
#include "util.h"
#include "nilfs_gc.h"
#include "cnormap.h"
#include "parser.h"

#ifdef _GNU_SOURCE
#include <getopt.h>
static const struct option long_option[] = {
	{"all",  no_argument, NULL, 'a'},
	{"index", required_argument, NULL, 'i'},
	{"latest-usage", no_argument, NULL, 'l' },
	{"lines", required_argument, NULL, 'n'},
	{"protection-period", required_argument, NULL, 'p'},
	{"help", no_argument, NULL, 'h'},
	{"version", no_argument, NULL, 'V'},
	{NULL, 0, NULL, 0}
};

#define LSSU_USAGE							\
	"Usage: %s [OPTION]... [DEVICE]\n"				\
	"  -a, --all\t\t\tdo not hide clean segments\n"			\
	"  -h, --help\t\t\tdisplay this help and exit\n"		\
	"  -i, --index\t\t\tskip index segments at start of inputs\n"	\
	"  -l, --latest-usage\t\tprint usage status of the moment\n"	\
	"  -n, --lines\t\t\tlist only lines input segments\n"		\
	"  -p, --protection-period\tspecify protection period\n"	\
	"  -V, --version\t\t\tdisplay version and exit\n"
#else	/* !_GNU_SOURCE */
#include <unistd.h>
#define LSSU_USAGE \
	"Usage: %s [-alhV] [-i index] [-n lines] [-p period] [device]\n"
#endif	/* _GNU_SOURCE */

#define LSSU_BUFSIZE	128
#define LSSU_NSEGS	512

enum lssu_mode {
	LSSU_MODE_NORMAL,
	LSSU_MODE_LATEST_USAGE,
};

struct lssu_format {
	char *header;
	char *body;
};

static const struct lssu_format lssu_format[] = {
	{
		"              SEGNUM        DATE     TIME STAT     NBLOCKS",
		"%20llu  %s  %c%c%c  %10u\n"
	},
	{
		"           SEGNUM        DATE     TIME STAT     NBLOCKS       NLIVEBLOCKS",
		"%17llu  %s %c%c%c%c  %10u %10u (%3u%%)\n"
	}
};

static int all;
static int latest;
static int disp_mode;		/* display mode */
static nilfs_cno_t protcno;
static __s64 prottime, now;
static __u64 param_index;
static __u64 param_lines;

static size_t blocks_per_segment;
static struct nilfs_suinfo suinfos[LSSU_NSEGS];

static void lssu_print_header(void)
{
	puts(lssu_format[disp_mode].header);
}

static ssize_t lssu_get_latest_usage(struct nilfs *nilfs,
				     __u64 segnum, __u64 protseq,
				     nilfs_cno_t protcno)
{
	struct nilfs_reclaim_stat stat;
	struct nilfs_reclaim_params params = {
		.flags = NILFS_RECLAIM_PARAM_PROTSEQ,
		.protseq = protseq
	};
	__u64 segnums[1];
	int ret;

	if (protcno != NILFS_CNO_MAX) {
		params.flags |= NILFS_RECLAIM_PARAM_PROTCNO;
		params.protcno = protcno;
	}

	memset(&stat, 0, sizeof(stat));
	segnums[0] = segnum;

	ret = nilfs_assess_segment(nilfs, segnums, 1, &params, &stat);
	if (unlikely(ret < 0))
		return -1;

	if (stat.protected_segs > 0)
		return -2;

	return stat.live_blks;
}

static ssize_t lssu_print_suinfo(struct nilfs *nilfs, __u64 segnum,
				 ssize_t nsi, __u64 protseq)
{
	struct tm tm;
	time_t t;
	char timebuf[LSSU_BUFSIZE];
	ssize_t i, n = 0, ret;
	int ratio;
	int protected;
	size_t nliveblks;

	for (i = 0; i < nsi; i++, segnum++) {
		if (!all && nilfs_suinfo_clean(&suinfos[i]))
			continue;

		t = (time_t)suinfos[i].sui_lastmod;
		if (t != 0) {
			localtime_r(&t, &tm);
			strftime(timebuf, LSSU_BUFSIZE, "%F %T", &tm);
		} else
			snprintf(timebuf, LSSU_BUFSIZE,
				 "---------- --:--:--");

		switch (disp_mode) {
		case LSSU_MODE_NORMAL:
			printf(lssu_format[disp_mode].body,
			       (unsigned long long)segnum,
			       timebuf,
			       nilfs_suinfo_active(&suinfos[i]) ? 'a' : '-',
			       nilfs_suinfo_dirty(&suinfos[i]) ? 'd' : '-',
			       nilfs_suinfo_error(&suinfos[i]) ? 'e' : '-',
			       suinfos[i].sui_nblocks);
			break;
		case LSSU_MODE_LATEST_USAGE:
			nliveblks = 0;
			ratio = 0;
			protected = (t >= prottime && t <= now);

			if (!nilfs_suinfo_dirty(&suinfos[i]) ||
			    nilfs_suinfo_error(&suinfos[i]))
				goto skip_scan;

			ret = lssu_get_latest_usage(nilfs, segnum, protseq,
						    protcno);
			if (ret >= 0) {
				nliveblks = ret;
				ratio = (ret * 100 + 99) / blocks_per_segment;
			} else if (likely(ret == -2)) {
				nliveblks = suinfos[i].sui_nblocks;
				ratio = 100;
				protected = 1;
			} else {
				warn("failed to get usage");
				return -1;
			}

skip_scan:
			printf(lssu_format[disp_mode].body,
			       (unsigned long long)segnum,
			       timebuf,
			       nilfs_suinfo_active(&suinfos[i]) ? 'a' : '-',
			       nilfs_suinfo_dirty(&suinfos[i]) ? 'd' : '-',
			       nilfs_suinfo_error(&suinfos[i]) ? 'e' : '-',
			       protected ? 'p' : '-',
			       suinfos[i].sui_nblocks, nliveblks, ratio);
			break;
		}
		n++;
	}
	return n;
}

static int lssu_list_suinfo(struct nilfs *nilfs)
{
	struct nilfs_sustat sustat;
	__u64 segnum, rest, count;
	ssize_t nsi, n;
	int ret;

	lssu_print_header();
	ret = nilfs_get_sustat(nilfs, &sustat);
	if (unlikely(ret < 0))
		return EXIT_FAILURE;
	segnum = param_index;
	rest = param_lines && param_lines < sustat.ss_nsegs ? param_lines :
		sustat.ss_nsegs;

	for ( ; rest > 0 && segnum < sustat.ss_nsegs; rest -= n) {
		count = min_t(__u64, rest, LSSU_NSEGS);
		nsi = nilfs_get_suinfo(nilfs, segnum, suinfos, count);
		if (unlikely(nsi < 0))
			return EXIT_FAILURE;

		n = lssu_print_suinfo(nilfs, segnum, nsi, sustat.ss_prot_seq);
		if (unlikely(n < 0))
			return EXIT_FAILURE;
		segnum += nsi;
	}

	return EXIT_SUCCESS;
}

static int lssu_get_protcno(struct nilfs *nilfs,
			    unsigned long protection_period,
			    __s64 *prottimep, nilfs_cno_t *protcnop)
{
	struct nilfs_cnormap *cnormap;
	int ret;

	if (protection_period == ULONG_MAX) {
		*protcnop = NILFS_CNO_MAX;
		*prottimep = now;
		return 0;
	}

	*prottimep = now - protection_period;

	cnormap = nilfs_cnormap_create(nilfs);
	if (unlikely(!cnormap)) {
		warn("failed to create checkpoint number reverse mapper");
		return -1;
	}

	ret = nilfs_cnormap_track_back(cnormap, protection_period, protcnop);
	if (unlikely(ret < 0))
		warn("failed to get checkpoint number from protection period (%lu)",
		     protection_period);

	nilfs_cnormap_destroy(cnormap);
	return ret;
}

int main(int argc, char *argv[])
{
	struct nilfs *nilfs;
	char *dev, *progname;
	int c, status;
	int open_flags;
	unsigned long protection_period = ULONG_MAX;
	int ret;
#ifdef _GNU_SOURCE
	int option_index;
#endif	/* _GNU_SOURCE */

	progname = strrchr(argv[0], '/');
	if (progname == NULL)
		progname = argv[0];
	else
		progname++;

#ifdef _GNU_SOURCE
	while ((c = getopt_long(argc, argv, "ai:ln:hp:V",
				long_option, &option_index)) >= 0) {
#else	/* !_GNU_SOURCE */
	while ((c = getopt(argc, argv, "ai:ln:hp:V")) >= 0) {
#endif	/* _GNU_SOURCE */

		switch (c) {
		case 'a':
			all = 1;
			break;
		case 'i':
			param_index = (__u64)atoll(optarg);
			break;
		case 'l':
			latest = 1;
			break;
		case 'n':
			param_lines = (__u64)atoll(optarg);
			break;
		case 'h':
			fprintf(stderr, LSSU_USAGE, progname);
			exit(EXIT_SUCCESS);
		case 'p':
			ret = nilfs_parse_protection_period(
				optarg, &protection_period);
			if (!ret)
				break;

			if (errno == ERANGE)
				errx(EXIT_FAILURE, "too large period: %s",
				     optarg);

			errx(EXIT_FAILURE, "invalid protection period: %s",
			     optarg);
		case 'V':
			printf("%s (%s %s)\n", progname, PACKAGE,
			       PACKAGE_VERSION);
			exit(EXIT_SUCCESS);
		default:
			exit(EXIT_FAILURE);
		}
	}

	if (optind > argc - 1)
		dev = NULL;
	else if (optind == argc - 1)
		dev = argv[optind++];
	else
		errx(EXIT_FAILURE, "too many arguments");

	open_flags = NILFS_OPEN_RDONLY;
	if (latest)
		open_flags |= NILFS_OPEN_RAW | NILFS_OPEN_GCLK;

	nilfs = nilfs_open(dev, NULL, open_flags);
	if (nilfs == NULL)
		err(EXIT_FAILURE, "cannot open NILFS on %s", dev ? : "device");

	if (latest) {
		struct timeval tv;

		ret = gettimeofday(&tv, NULL);
		if (unlikely(ret < 0)) {
			warn("cannot get current time");
			status = EXIT_FAILURE;
			goto out_close_nilfs;
		}
		now = tv.tv_sec;

		blocks_per_segment = nilfs_get_blocks_per_segment(nilfs);
		disp_mode = LSSU_MODE_LATEST_USAGE;

		ret = lssu_get_protcno(nilfs, protection_period, &prottime,
				       &protcno);
		if (unlikely(ret < 0)) {
			status = EXIT_FAILURE;
			goto out_close_nilfs;
		}
	}

	status = lssu_list_suinfo(nilfs);

out_close_nilfs:
	nilfs_close(nilfs);
	exit(status);
}