]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/stat.c
cde7780a5fcec31abb521dfbb93114ca0e368fce
[thirdparty/xfsprogs-dev.git] / io / stat.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 * Copyright (C) 2015, 2017 Red Hat, Inc.
6 * Portions of statx support written by David Howells (dhowells@redhat.com)
7 */
8
9 #include "command.h"
10 #include "input.h"
11 #include "init.h"
12 #include "io.h"
13 #include "statx.h"
14 #include "libxfs.h"
15
16 #include <fcntl.h>
17
18 static cmdinfo_t stat_cmd;
19 static cmdinfo_t statfs_cmd;
20 static cmdinfo_t statx_cmd;
21
22 off64_t
23 filesize(void)
24 {
25 struct stat st;
26
27 if (fstat(file->fd, &st) < 0) {
28 perror("fstat");
29 return -1;
30 }
31 return st.st_size;
32 }
33
34 static char *
35 filetype(mode_t mode)
36 {
37 switch (mode & S_IFMT) {
38 case S_IFSOCK:
39 return _("socket");
40 case S_IFDIR:
41 return _("directory");
42 case S_IFCHR:
43 return _("char device");
44 case S_IFBLK:
45 return _("block device");
46 case S_IFREG:
47 return _("regular file");
48 case S_IFLNK:
49 return _("symbolic link");
50 case S_IFIFO:
51 return _("fifo");
52 }
53 return NULL;
54 }
55
56 static int
57 dump_raw_stat(struct stat *st)
58 {
59 printf("stat.blksize = %lu\n", (unsigned long)st->st_blksize);
60 printf("stat.nlink = %lu\n", (unsigned long)st->st_nlink);
61 printf("stat.uid = %u\n", st->st_uid);
62 printf("stat.gid = %u\n", st->st_gid);
63 printf("stat.mode: 0%o\n", st->st_mode);
64 printf("stat.ino = %llu\n", (unsigned long long)st->st_ino);
65 printf("stat.size = %lld\n", (long long)st->st_size);
66 printf("stat.blocks = %lld\n", (long long)st->st_blocks);
67 printf("stat.atime.tv_sec = %ld\n", st->st_atim.tv_sec);
68 printf("stat.atime.tv_nsec = %ld\n", st->st_atim.tv_nsec);
69 printf("stat.ctime.tv_sec = %ld\n", st->st_ctim.tv_sec);
70 printf("stat.ctime.tv_nsec = %ld\n", st->st_ctim.tv_nsec);
71 printf("stat.mtime.tv_sec = %ld\n", st->st_mtim.tv_sec);
72 printf("stat.mtime.tv_nsec = %ld\n", st->st_mtim.tv_nsec);
73 printf("stat.rdev_major = %u\n", major(st->st_rdev));
74 printf("stat.rdev_minor = %u\n", minor(st->st_rdev));
75 printf("stat.dev_major = %u\n", major(st->st_dev));
76 printf("stat.dev_minor = %u\n", minor(st->st_dev));
77 return 0;
78 }
79
80 void print_file_info(void)
81 {
82 printf(_("fd.path = \"%s\"\n"), file->name);
83 printf(_("fd.flags = %s,%s,%s%s%s%s%s\n"),
84 file->flags & IO_OSYNC ? _("sync") : _("non-sync"),
85 file->flags & IO_DIRECT ? _("direct") : _("non-direct"),
86 file->flags & IO_READONLY ? _("read-only") : _("read-write"),
87 file->flags & IO_REALTIME ? _(",real-time") : "",
88 file->flags & IO_APPEND ? _(",append-only") : "",
89 file->flags & IO_NONBLOCK ? _(",non-block") : "",
90 file->flags & IO_TMPFILE ? _(",tmpfile") : "");
91 }
92
93 void print_xfs_info(int verbose)
94 {
95 struct dioattr dio;
96 struct fsxattr fsx, fsxa;
97
98 if ((xfsctl(file->name, file->fd, FS_IOC_FSGETXATTR, &fsx)) < 0 ||
99 (xfsctl(file->name, file->fd, XFS_IOC_FSGETXATTRA, &fsxa)) < 0) {
100 perror("FS_IOC_FSGETXATTR");
101 } else {
102 printf(_("fsxattr.xflags = 0x%x "), fsx.fsx_xflags);
103 printxattr(fsx.fsx_xflags, verbose, 0, file->name, 1, 1);
104 printf(_("fsxattr.projid = %u\n"), fsx.fsx_projid);
105 printf(_("fsxattr.extsize = %u\n"), fsx.fsx_extsize);
106 printf(_("fsxattr.cowextsize = %u\n"), fsx.fsx_cowextsize);
107 printf(_("fsxattr.nextents = %u\n"), fsx.fsx_nextents);
108 printf(_("fsxattr.naextents = %u\n"), fsxa.fsx_nextents);
109 }
110 if ((xfsctl(file->name, file->fd, XFS_IOC_DIOINFO, &dio)) < 0) {
111 perror("XFS_IOC_DIOINFO");
112 } else {
113 printf(_("dioattr.mem = 0x%x\n"), dio.d_mem);
114 printf(_("dioattr.miniosz = %u\n"), dio.d_miniosz);
115 printf(_("dioattr.maxiosz = %u\n"), dio.d_maxiosz);
116 }
117 }
118
119 int
120 stat_f(
121 int argc,
122 char **argv)
123 {
124 struct stat st;
125 int c, verbose = 0, raw = 0;
126
127 while ((c = getopt(argc, argv, "rv")) != EOF) {
128 switch (c) {
129 case 'r':
130 raw = 1;
131 break;
132 case 'v':
133 verbose = 1;
134 break;
135 default:
136 return command_usage(&stat_cmd);
137 }
138 }
139
140 if (raw && verbose)
141 return command_usage(&stat_cmd);
142
143 if (fstat(file->fd, &st) < 0) {
144 perror("fstat");
145 return 0;
146 }
147
148 if (raw)
149 return dump_raw_stat(&st);
150
151 print_file_info();
152
153 printf(_("stat.ino = %lld\n"), (long long)st.st_ino);
154 printf(_("stat.type = %s\n"), filetype(st.st_mode));
155 printf(_("stat.size = %lld\n"), (long long)st.st_size);
156 printf(_("stat.blocks = %lld\n"), (long long)st.st_blocks);
157 if (verbose) {
158 printf(_("stat.atime = %s"), ctime(&st.st_atime));
159 printf(_("stat.mtime = %s"), ctime(&st.st_mtime));
160 printf(_("stat.ctime = %s"), ctime(&st.st_ctime));
161 }
162
163 if (file->flags & IO_FOREIGN)
164 return 0;
165
166 print_xfs_info(verbose);
167
168 return 0;
169 }
170
171 static int
172 statfs_f(
173 int argc,
174 char **argv)
175 {
176 struct xfs_fsop_counts fscounts;
177 struct xfs_fsop_geom fsgeo;
178 struct statfs st;
179
180 printf(_("fd.path = \"%s\"\n"), file->name);
181 if (platform_fstatfs(file->fd, &st) < 0) {
182 perror("fstatfs");
183 } else {
184 printf(_("statfs.f_bsize = %lld\n"), (long long) st.f_bsize);
185 printf(_("statfs.f_blocks = %lld\n"), (long long) st.f_blocks);
186 printf(_("statfs.f_bavail = %lld\n"), (long long) st.f_bavail);
187 printf(_("statfs.f_files = %lld\n"), (long long) st.f_files);
188 printf(_("statfs.f_ffree = %lld\n"), (long long) st.f_ffree);
189 #ifdef HAVE_STATFS_FLAGS
190 printf(_("statfs.f_flags = 0x%llx\n"), (long long) st.f_flags);
191 #endif
192 }
193 if (file->flags & IO_FOREIGN)
194 return 0;
195 if ((xfsctl(file->name, file->fd, XFS_IOC_FSGEOMETRY_V1, &fsgeo)) < 0) {
196 perror("XFS_IOC_FSGEOMETRY_V1");
197 } else {
198 printf(_("geom.bsize = %u\n"), fsgeo.blocksize);
199 printf(_("geom.agcount = %u\n"), fsgeo.agcount);
200 printf(_("geom.agblocks = %u\n"), fsgeo.agblocks);
201 printf(_("geom.datablocks = %llu\n"),
202 (unsigned long long) fsgeo.datablocks);
203 printf(_("geom.rtblocks = %llu\n"),
204 (unsigned long long) fsgeo.rtblocks);
205 printf(_("geom.rtextents = %llu\n"),
206 (unsigned long long) fsgeo.rtextents);
207 printf(_("geom.rtextsize = %u\n"), fsgeo.rtextsize);
208 printf(_("geom.sunit = %u\n"), fsgeo.sunit);
209 printf(_("geom.swidth = %u\n"), fsgeo.swidth);
210 }
211 if ((xfsctl(file->name, file->fd, XFS_IOC_FSCOUNTS, &fscounts)) < 0) {
212 perror("XFS_IOC_FSCOUNTS");
213 } else {
214 printf(_("counts.freedata = %llu\n"),
215 (unsigned long long) fscounts.freedata);
216 printf(_("counts.freertx = %llu\n"),
217 (unsigned long long) fscounts.freertx);
218 printf(_("counts.freeino = %llu\n"),
219 (unsigned long long) fscounts.freeino);
220 printf(_("counts.allocino = %llu\n"),
221 (unsigned long long) fscounts.allocino);
222 }
223 return 0;
224 }
225
226 static ssize_t
227 _statx(
228 int dfd,
229 const char *filename,
230 unsigned int flags,
231 unsigned int mask,
232 struct statx *buffer)
233 {
234 #ifdef __NR_statx
235 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
236 #else
237 errno = ENOSYS;
238 return -1;
239 #endif
240 }
241
242 static void
243 statx_help(void)
244 {
245 printf(_(
246 "\n"
247 " Display extended file status.\n"
248 "\n"
249 " Options:\n"
250 " -v -- More verbose output\n"
251 " -r -- Print raw statx structure fields\n"
252 " -m mask -- Specify the field mask for the statx call\n"
253 " (can also be 'basic' or 'all'; default STATX_ALL)\n"
254 " -D -- Don't sync attributes with the server\n"
255 " -F -- Force the attributes to be sync'd with the server\n"
256 "\n"));
257 }
258
259 /* statx helper */
260 static int
261 dump_raw_statx(struct statx *stx)
262 {
263 printf("stat.mask = 0x%x\n", stx->stx_mask);
264 printf("stat.blksize = %u\n", stx->stx_blksize);
265 printf("stat.attributes = 0x%llx\n", (unsigned long long)stx->stx_attributes);
266 printf("stat.nlink = %u\n", stx->stx_nlink);
267 printf("stat.uid = %u\n", stx->stx_uid);
268 printf("stat.gid = %u\n", stx->stx_gid);
269 printf("stat.mode: 0%o\n", stx->stx_mode);
270 printf("stat.ino = %llu\n", (unsigned long long)stx->stx_ino);
271 printf("stat.size = %llu\n", (unsigned long long)stx->stx_size);
272 printf("stat.blocks = %llu\n", (unsigned long long)stx->stx_blocks);
273 printf("stat.atime.tv_sec = %lld\n", (long long)stx->stx_atime.tv_sec);
274 printf("stat.atime.tv_nsec = %d\n", stx->stx_atime.tv_nsec);
275 printf("stat.btime.tv_sec = %lld\n", (long long)stx->stx_btime.tv_sec);
276 printf("stat.btime.tv_nsec = %d\n", stx->stx_btime.tv_nsec);
277 printf("stat.ctime.tv_sec = %lld\n", (long long)stx->stx_ctime.tv_sec);
278 printf("stat.ctime.tv_nsec = %d\n", stx->stx_ctime.tv_nsec);
279 printf("stat.mtime.tv_sec = %lld\n", (long long)stx->stx_mtime.tv_sec);
280 printf("stat.mtime.tv_nsec = %d\n", stx->stx_mtime.tv_nsec);
281 printf("stat.rdev_major = %u\n", stx->stx_rdev_major);
282 printf("stat.rdev_minor = %u\n", stx->stx_rdev_minor);
283 printf("stat.dev_major = %u\n", stx->stx_dev_major);
284 printf("stat.dev_minor = %u\n", stx->stx_dev_minor);
285 return 0;
286 }
287
288 /*
289 * options:
290 * - input flags - query type
291 * - output style for flags (and all else?) (chars vs. hex?)
292 * - output - mask out incidental flag or not?
293 */
294 int
295 statx_f(
296 int argc,
297 char **argv)
298 {
299 int c, verbose = 0, raw = 0;
300 char *p;
301 struct statx stx;
302 int atflag = 0;
303 unsigned int mask = STATX_ALL;
304
305 while ((c = getopt(argc, argv, "m:rvFD")) != EOF) {
306 switch (c) {
307 case 'm':
308 if (strcmp(optarg, "basic") == 0)
309 mask = STATX_BASIC_STATS;
310 else if (strcmp(optarg, "all") == 0)
311 mask = STATX_ALL;
312 else {
313 mask = strtoul(optarg, &p, 0);
314 if (!p || p == optarg) {
315 printf(
316 _("non-numeric mask -- %s\n"), optarg);
317 return 0;
318 }
319 }
320 break;
321 case 'r':
322 raw = 1;
323 break;
324 case 'v':
325 verbose = 1;
326 break;
327 case 'F':
328 atflag &= ~AT_STATX_SYNC_TYPE;
329 atflag |= AT_STATX_FORCE_SYNC;
330 break;
331 case 'D':
332 atflag &= ~AT_STATX_SYNC_TYPE;
333 atflag |= AT_STATX_DONT_SYNC;
334 break;
335 default:
336 return command_usage(&statx_cmd);
337 }
338 }
339
340 if (raw && verbose)
341 return command_usage(&statx_cmd);
342
343 memset(&stx, 0xbf, sizeof(stx));
344 if (_statx(file->fd, "", atflag | AT_EMPTY_PATH, mask, &stx) < 0) {
345 perror("statx");
346 return 0;
347 }
348
349 if (raw)
350 return dump_raw_statx(&stx);
351
352 print_file_info();
353
354 printf(_("stat.ino = %lld\n"), (long long)stx.stx_ino);
355 printf(_("stat.type = %s\n"), filetype(stx.stx_mode));
356 printf(_("stat.size = %lld\n"), (long long)stx.stx_size);
357 printf(_("stat.blocks = %lld\n"), (long long)stx.stx_blocks);
358 if (verbose) {
359 printf(_("stat.atime = %s"), ctime((time_t *)&stx.stx_atime.tv_sec));
360 printf(_("stat.mtime = %s"), ctime((time_t *)&stx.stx_mtime.tv_sec));
361 printf(_("stat.ctime = %s"), ctime((time_t *)&stx.stx_ctime.tv_sec));
362 if (stx.stx_mask & STATX_BTIME)
363 printf(_("stat.btime = %s"),
364 ctime((time_t *)&stx.stx_btime.tv_sec));
365 }
366
367 if (file->flags & IO_FOREIGN)
368 return 0;
369
370 print_xfs_info(verbose);
371
372 return 0;
373 }
374
375 void
376 stat_init(void)
377 {
378 stat_cmd.name = "stat";
379 stat_cmd.cfunc = stat_f;
380 stat_cmd.argmin = 0;
381 stat_cmd.argmax = 1;
382 stat_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
383 stat_cmd.args = _("[-v|-r]");
384 stat_cmd.oneline = _("statistics on the currently open file");
385
386 statx_cmd.name = "statx";
387 statx_cmd.cfunc = statx_f;
388 statx_cmd.argmin = 0;
389 statx_cmd.argmax = -1;
390 statx_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
391 statx_cmd.args = _("[-v|-r][-m basic | -m all | -m <mask>][-FD]");
392 statx_cmd.oneline = _("extended statistics on the currently open file");
393 statx_cmd.help = statx_help;
394
395 statfs_cmd.name = "statfs";
396 statfs_cmd.cfunc = statfs_f;
397 statfs_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
398 statfs_cmd.oneline =
399 _("statistics on the filesystem of the currently open file");
400
401 add_command(&stat_cmd);
402 add_command(&statx_cmd);
403 add_command(&statfs_cmd);
404 }