Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/statx.c
48529 views
/*1* SPDX-License-Identifier: MIT2*3* Copyright (c) 2025, Rob Norris <[email protected]>4*5* Permission is hereby granted, free of charge, to any person obtaining a copy6* of this software and associated documentation files (the "Software"), to7* deal in the Software without restriction, including without limitation the8* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or9* sell copies of the Software, and to permit persons to whom the Software is10* furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in13* all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/2324#include <stdint.h>25#include <stdio.h>26#include <string.h>27#include <errno.h>28#include <fcntl.h>29#include <sys/syscall.h>30#include <unistd.h>3132/*33* statx() may be available in the kernel, but not in the libc, so we build34* our own wrapper if we can't link one.35*/3637#ifndef __NR_statx38#if defined(__x86_64__)39#define __NR_statx (332)40#elif defined(__i386__)41#define __NR_statx (383)42#elif defined(__s390__)43#define __NR_statx (379)44#elif defined(__arm__)45#define __NR_statx (397)46#elif defined(__aarch64__)47#define __NR_statx (291)48#elif defined(__powerpc__)49#define __NR_statx (383)50#else51#error "no definition of __NR_statx for this platform"52#endif53#endif /* __NR_statx */545556int57statx(int, const char *, int, unsigned int, void *)58__attribute__((weak));5960static inline int61_statx(int fd, const char *path, int flags, unsigned int mask, void *stx)62{63if (statx)64return (statx(fd, path, flags, mask, stx));65else66return (syscall(__NR_statx, fd, path, flags, mask, stx));67}6869#ifndef STATX_TYPE70#define STATX_TYPE (1<<0)71#endif72#ifndef STATX_MODE73#define STATX_MODE (1<<1)74#endif75#ifndef STATX_NLINK76#define STATX_NLINK (1<<2)77#endif78#ifndef STATX_UID79#define STATX_UID (1<<3)80#endif81#ifndef STATX_GID82#define STATX_GID (1<<4)83#endif84#ifndef STATX_ATIME85#define STATX_ATIME (1<<5)86#endif87#ifndef STATX_MTIME88#define STATX_MTIME (1<<6)89#endif90#ifndef STATX_CTIME91#define STATX_CTIME (1<<7)92#endif93#ifndef STATX_INO94#define STATX_INO (1<<8)95#endif96#ifndef STATX_SIZE97#define STATX_SIZE (1<<9)98#endif99#ifndef STATX_BLOCKS100#define STATX_BLOCKS (1<<10)101#endif102#ifndef STATX_BTIME103#define STATX_BTIME (1<<11)104#endif105#ifndef STATX_MNT_ID106#define STATX_MNT_ID (1<<12)107#endif108#ifndef STATX_DIOALIGN109#define STATX_DIOALIGN (1<<13)110#endif111#ifndef S_IFMT112#define S_IFMT 0170000113#endif114115typedef struct {116int64_t tv_sec;117uint32_t tv_nsec;118int32_t _pad;119} stx_timestamp_t;120_Static_assert(sizeof (stx_timestamp_t) == 0x10,121"stx_timestamp_t not 16 bytes");122123typedef struct {124uint32_t stx_mask;125uint32_t stx_blksize;126uint64_t stx_attributes;127uint32_t stx_nlink;128uint32_t stx_uid;129uint32_t stx_gid;130uint16_t stx_mode;131uint16_t _pad1;132uint64_t stx_ino;133uint64_t stx_size;134uint64_t stx_blocks;135uint64_t stx_attributes_mask;136stx_timestamp_t stx_atime;137stx_timestamp_t stx_btime;138stx_timestamp_t stx_ctime;139stx_timestamp_t stx_mtime;140uint32_t stx_rdev_major;141uint32_t stx_rdev_minor;142uint32_t stx_dev_major;143uint32_t stx_dev_minor;144uint64_t stx_mnt_id;145uint32_t stx_dio_mem_align;146uint32_t stx_dio_offset_align;147uint64_t _pad2[12];148} stx_t;149_Static_assert(sizeof (stx_t) == 0x100, "stx_t not 256 bytes");150151typedef struct {152const char *name;153unsigned int mask;154} stx_field_t;155156stx_field_t fields[] = {157{ "type", STATX_TYPE },158{ "mode", STATX_MODE },159{ "nlink", STATX_NLINK },160{ "uid", STATX_UID },161{ "gid", STATX_GID },162{ "atime", STATX_ATIME },163{ "mtime", STATX_MTIME },164{ "ctime", STATX_CTIME },165{ "ino", STATX_INO },166{ "size", STATX_SIZE },167{ "blocks", STATX_BLOCKS },168{ "btime", STATX_BTIME },169{ "mnt_id", STATX_MNT_ID },170{ "dioalign", STATX_DIOALIGN },171{ NULL },172};173174static int175usage(void)176{177printf(178"usage: statx <field[,field,field]> <file>\n"179"available fields:\n");180181int w = 0;182for (stx_field_t *f = fields; f->name != NULL; f++) {183if (w > 0 && (w + strlen(f->name) + 1) > 60) {184fputc('\n', stdout);185w = 0;186}187if (w == 0)188fputc(' ', stdout);189w += printf(" %s", f->name);190}191if (w > 0)192fputc('\n', stdout);193return (1);194}195196int197main(int argc, char **argv)198{199if (argc < 3)200return (usage());201202unsigned int mask = 0;203204char *name;205while ((name = strsep(&argv[1], ",")) != NULL) {206stx_field_t *f;207for (f = fields; f->name != NULL; f++) {208if (strcmp(name, f->name) == 0) {209mask |= f->mask;210break;211}212}213if (f->name == NULL) {214fprintf(stderr, "unknown field name: %s\n", name);215return (usage());216}217}218219int fd = open(argv[2], O_PATH);220if (fd < 0) {221fprintf(stderr, "open: %s: %s\n", argv[2], strerror(errno));222return (1);223}224225stx_t stx = {};226227if (_statx(fd, "",228AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, mask, &stx) < 0) {229fprintf(stderr, "statx: %s: %s\n", argv[2], strerror(errno));230close(fd);231return (1);232}233234int rc = 0;235236for (stx_field_t *f = fields; f->name != NULL; f++) {237if (!(mask & f->mask))238continue;239if (!(stx.stx_mask & f->mask)) {240printf("statx: kernel did not return field: %s\n",241f->name);242rc = 2;243continue;244}245}246247if (rc > 0)248return (rc);249250for (stx_field_t *f = fields; f->name != NULL; f++) {251if (!(mask & f->mask))252continue;253254switch (f->mask) {255case STATX_TYPE:256printf("type: %u\n", stx.stx_mode & S_IFMT);257break;258case STATX_MODE:259printf("mode: %u\n", stx.stx_mode & ~S_IFMT);260break;261case STATX_NLINK:262printf("nlink: %u\n", stx.stx_nlink);263break;264case STATX_UID:265printf("uid: %u\n", stx.stx_uid);266break;267case STATX_GID:268printf("gid: %u\n", stx.stx_gid);269break;270case STATX_ATIME:271printf("atime: %ld.%u\n",272stx.stx_atime.tv_sec, stx.stx_atime.tv_nsec);273break;274case STATX_MTIME:275printf("mtime: %ld.%u\n",276stx.stx_mtime.tv_sec, stx.stx_mtime.tv_nsec);277break;278case STATX_CTIME:279printf("ctime: %ld.%u\n",280stx.stx_ctime.tv_sec, stx.stx_ctime.tv_nsec);281break;282case STATX_INO:283printf("ino: %lu\n", stx.stx_ino);284break;285case STATX_SIZE:286printf("size: %lu\n", stx.stx_size);287break;288case STATX_BLOCKS:289printf("blocks: %lu\n", stx.stx_blocks);290break;291case STATX_BTIME:292printf("btime: %ld.%u\n",293stx.stx_btime.tv_sec, stx.stx_btime.tv_nsec);294break;295case STATX_MNT_ID:296printf("mnt_id: %lu\n", stx.stx_mnt_id);297break;298case STATX_DIOALIGN:299printf("dioalign: %u %u\n",300stx.stx_dio_mem_align, stx.stx_dio_offset_align);301break;302}303}304305return (rc);306}307308309