summaryrefslogtreecommitdiff
path: root/cmds-fi-du.c
blob: b413b611867ca2e9fbce0aa72970e0026de32dc6 (plain)
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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <getopt.h>
#include <fcntl.h>
#include <dirent.h>

#include <sys/ioctl.h>
#include <linux/fs.h>
#include <linux/fiemap.h>

#include "utils.h"
#include "commands.h"
#include "kerncompat.h"
#include "rbtree.h"

static int summarize = 0;
static unsigned unit_mode = UNITS_RAW;
static char path[PATH_MAX] = { 0, };
static char *pathp = path;
static char *path_max = &path[PATH_MAX - 1];

/* Track which inodes we've seen for the purposes of hardlink detection. */
struct seen_inode {
	struct rb_node	i_node;
	u64		i_ino;
	u64		i_subvol;
};
static struct rb_root seen_inodes = RB_ROOT;

static int cmp_si(struct seen_inode *si, u64 ino, u64 subvol)
{
	if (ino < si->i_ino)
		return -1;
	else if (ino > si->i_ino)
		return 1;
	if (subvol < si->i_subvol)
		return -1;
	else if (subvol > si->i_subvol)
		return 1;
	return 0;
}

static int mark_inode_seen(u64 ino, u64 subvol)
{
	int cmp;
	struct rb_node **p = &seen_inodes.rb_node;
	struct rb_node *parent = NULL;
	struct seen_inode *si;

	while (*p) {
		parent = *p;

		si = rb_entry(parent, struct seen_inode, i_node);
		cmp = cmp_si(si, ino, subvol);
		if (cmp < 0)
			p = &(*p)->rb_left;
		else if (cmp > 0)
			p = &(*p)->rb_right;
		else
			BUG();
	}

	si = calloc(1, sizeof(*si));
	if (!si)
		return ENOMEM;

	si->i_ino = ino;
	si->i_subvol = subvol;

	rb_link_node(&si->i_node, parent, p);
	rb_insert_color(&si->i_node, &seen_inodes);

	return 0;
}

static int inode_seen(u64 ino, u64 subvol)
{
	int cmp;
	struct rb_node *n = seen_inodes.rb_node;
	struct seen_inode *si;

	while (n) {
		si = rb_entry(n, struct seen_inode, i_node);

		cmp = cmp_si(si, ino, subvol);
		if (cmp < 0)
			n = n->rb_left;
		else if (cmp > 0)
			n = n->rb_right;
		else
			return EEXIST;
	}
	return 0;

}

const char * const cmd_filesystem_du_usage[] = {
	"btrfs filesystem du [options] <path> [<path>..]",
	"Summarize disk usage of each file.",
	"-h|--human-readable",
	"                   human friendly numbers, base 1024 (default)",
	"-s                 display only a total for each argument",
	NULL
};

/*
 * Inline extents are skipped because they do not take data space,
 * delalloc and unknown are skipped because we do not know how much
 * space they will use yet.
 */
#define	SKIP_FLAGS	(FIEMAP_EXTENT_UNKNOWN|FIEMAP_EXTENT_DELALLOC|FIEMAP_EXTENT_DATA_INLINE)
static int du_calc_file_space(int dirfd, const char *filename,
			      uint64_t *ret_total, uint64_t *ret_shared)
{
	char buf[16384];
	struct fiemap *fiemap = (struct fiemap *)buf;
	struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
	int count = (sizeof(buf) - sizeof(*fiemap)) /
			sizeof(struct fiemap_extent);
	unsigned int i, ret;
	int last = 0;
	int rc;
	u64 ext_len;
	int fd;
	u64 file_total = 0;
	u64 file_shared = 0;
	u32 flags;

	memset(fiemap, 0, sizeof(struct fiemap));

	fd = openat(dirfd, filename, O_RDONLY);
	if (fd == -1) {
		ret = errno;
		fprintf(stderr, "ERROR: can't access '%s': %s\n",
			filename, strerror(ret));
		return ret;
	}

	do {
		fiemap->fm_length = ~0ULL;
		fiemap->fm_extent_count = count;
		rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
		if (rc < 0) {
			ret = errno;
			goto out_close;
		}

		/* If 0 extents are returned, then more ioctls are not needed */
		if (fiemap->fm_mapped_extents == 0)
			break;

		for (i = 0; i < fiemap->fm_mapped_extents; i++) {
			ext_len = fm_ext[i].fe_length;
			flags = fm_ext[i].fe_flags;

			if (flags & FIEMAP_EXTENT_LAST)
				last = 1;

			if (flags & SKIP_FLAGS)
				continue;

			file_total += ext_len;
			if (flags & FIEMAP_EXTENT_SHARED)
				file_shared += ext_len;
		}

		fiemap->fm_start = (fm_ext[i - 1].fe_logical +
				    fm_ext[i - 1].fe_length);
	} while (last == 0);

	*ret_total = file_total;
	*ret_shared = file_shared;

	ret = 0;
out_close:
	close(fd);
	return ret;
}

struct du_dir_ctxt {
	uint64_t	bytes_total;
	uint64_t	bytes_shared;
};

static int du_add_file(const char *filename, int dirfd, uint64_t *ret_total,
		       uint64_t *ret_shared, int top_level);

static int du_walk_dir(struct du_dir_ctxt *ctxt)
{
	int fd, ret, type;
	DIR *dirstream = NULL;
	struct dirent *entry;

	fd = open_file_or_dir(path, &dirstream);
	if (fd < 0)
		return fd;

	ret = 0;
	do {
		uint64_t tot, shr;

		errno = 0;
		entry = readdir(dirstream);
		if (entry) {
			if (strcmp(entry->d_name, ".") == 0
			    || strcmp(entry->d_name, "..") == 0)
				continue;

			type = entry->d_type;
			if (type == DT_REG || type == DT_DIR) {
				tot = shr = 0;

				ret = du_add_file(entry->d_name,
						  dirfd(dirstream), &tot,
						  &shr, 0);
				if (ret)
					break;

				ctxt->bytes_total += tot;
				ctxt->bytes_shared += shr;
			}
		}
	} while (entry != NULL);

	close_file_or_dir(fd, dirstream);
	return ret;
}

static int du_add_file(const char *filename, int dirfd, uint64_t *ret_total,
		       uint64_t *ret_shared, int top_level)
{
	int ret, len = strlen(filename);
	char *pathtmp;
	struct stat st;
	struct du_dir_ctxt dir;
	uint64_t file_total = 0;
	uint64_t file_shared = 0;
	u64 subvol;
	int fd;
	DIR *dirstream = NULL;

	ret = fstatat(dirfd, filename, &st, 0);
	if (ret) {
		ret = errno;
		return ret;
	}

	if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
		return 0;

	if (len > (path_max - pathp)) {
		fprintf(stderr, "ERROR: Path max exceeded: %s %s\n", path,
			filename);
		return ENAMETOOLONG;
	}

	pathtmp = pathp;
	if (pathp == path)
		ret = sprintf(pathp, "%s", filename);
	else
		ret = sprintf(pathp, "/%s", filename);
	pathp += ret;

	fd = open_file_or_dir(path, &dirstream);
	if (fd < 0) {
		ret = fd;
		goto out;
	}

	ret = lookup_ino_rootid(fd, &subvol);
	if (ret)
		goto out_close;

	if (inode_seen(st.st_ino, subvol))
		goto out_close;

	ret = mark_inode_seen(st.st_ino, subvol);
	if (ret)
		goto out_close;

	if (S_ISREG(st.st_mode)) {
		ret = du_calc_file_space(dirfd, filename, &file_total,
					 &file_shared);
		if (ret)
			goto out_close;
	} else if (S_ISDIR(st.st_mode)) {
		memset(&dir, 0, sizeof(dir));

		ret = du_walk_dir(&dir);
		*pathp = '\0';
		if (ret)
			goto out_close;

		file_total = dir.bytes_total;
		file_shared = dir.bytes_shared;
	}

	if (!summarize || top_level) {
		printf("%s\t%s\t%s\n", pretty_size_mode(file_total, unit_mode),
		       pretty_size_mode((file_total - file_shared), unit_mode),
		       path);
	}

	if (ret_total)
		*ret_total = file_total;
	if (ret_shared)
		*ret_shared = file_shared;

out_close:
	close_file_or_dir(fd, dirstream);
out:
	/* reset path to just before this element */
	pathp = pathtmp;

	return ret;
}

int cmd_filesystem_du(int argc, char **argv)
{
	int ret = 0, error = 0;
	int i;

	optind = 1;
	while (1) {
		int long_index;
		static const struct option long_options[] = {
			{ "summarize", no_argument, NULL, 's'},
			{ "human-readable", no_argument, NULL, 'h'},
			{ NULL, 0, NULL, 0 }
		};
		int c = getopt_long(argc, argv, "sh", long_options,
				&long_index);

		if (c < 0)
			break;
		switch (c) {
		case 'h':
			unit_mode = UNITS_HUMAN;
			break;
		case 's':
			summarize = 1;
			break;
		default:
			usage(cmd_filesystem_du_usage);
		}
	}

	if (check_argc_min(argc - optind, 1))
		usage(cmd_filesystem_du_usage);

	printf("total\texclusive\tfilename\n");

	for (i = optind; i < argc; i++) {
		ret = du_add_file(argv[i], AT_FDCWD, NULL, NULL, 1);
		if (ret) {
			fprintf(stderr, "ERROR: can't check space of '%s': %s\n",
				argv[i], strerror(ret));
			error = 1;
		}
	}

	return error;
}