Skip to content

Commit

Permalink
nfsv4: add parsing of rdev
Browse files Browse the repository at this point in the history
also cleanup nfsc3 handling of rdev and remove the dependency in makedev()

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Dec 14, 2024
1 parent c026f8e commit 46cf506
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 97 deletions.
11 changes: 4 additions & 7 deletions lib/nfs_v3.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@
#include "libnfs-raw-mount.h"
#include "libnfs-private.h"

static dev_t
specdata3_to_rdev(struct specdata3 *rdev)
static uint64_t
specdata3_to_rdev(struct specdata3 *specdata)
{
#ifdef makedev
return makedev(rdev->specdata1, rdev->specdata2);
#else
return 0;
#endif
uint64_t rdev = specdata->specdata1;
return (rdev << 32) | specdata->specdata2;
}

struct mount_attr_cb {
Expand Down
13 changes: 13 additions & 0 deletions lib/nfs_v4.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ static uint32_t standard_attributes[2] = {
1 << (FATTR4_NUMLINKS - 32) |
1 << (FATTR4_OWNER - 32) |
1 << (FATTR4_OWNER_GROUP - 32) |
1 << (FATTR4_RAWDEV - 32) |
1 << (FATTR4_SPACE_USED - 32) |
1 << (FATTR4_TIME_ACCESS - 32) |
1 << (FATTR4_TIME_METADATA - 32) |
Expand Down Expand Up @@ -463,6 +464,13 @@ nfs_get_ugid(struct nfs_context *nfs, const char *buf, int slen, int is_user)
return -1; \
}

static uint64_t
specdata4_to_rdev(uint32_t *specdata)
{
uint64_t rdev = ntohl(specdata[0]);
return (rdev << 32) | ntohl(specdata[1]);
}

static int
nfs_parse_attributes(struct nfs_context *nfs, struct nfs4_cb_data *data,
struct nfs_stat_64 *st, const char *buf, int len)
Expand Down Expand Up @@ -543,6 +551,11 @@ nfs_parse_attributes(struct nfs_context *nfs, struct nfs4_cb_data *data,
CHECK_GETATTR_BUF_SPACE(len, pad);
buf += pad;
len -= pad;
/* raw device */
CHECK_GETATTR_BUF_SPACE(len, 8);
st->nfs_rdev = specdata4_to_rdev((uint32_t *)buf);
buf += 8;
len -= 8;
/* Space Used */
CHECK_GETATTR_BUF_SPACE(len, 8);
st->nfs_used = nfs_pntoh64((uint32_t *)(void *)buf);
Expand Down
121 changes: 31 additions & 90 deletions utils/nfs-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,95 +55,13 @@ WSADATA wsaData;
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"

struct file_context {
int fd;
struct nfs_context *nfs;
struct nfsfh *nfsfh;
struct nfs_url *url;
};

void usage(void)
{
fprintf(stderr, "Usage: nfs-stat <file>\n");
fprintf(stderr, "<file> stat an nfs file.\n");
exit(0);
}

void
free_file_context(struct file_context *file_context)
{
if (file_context->fd != -1) {
close(file_context->fd);
}
if (file_context->nfsfh != NULL) {
nfs_close(file_context->nfs, file_context->nfsfh);
}
if (file_context->nfs != NULL) {
nfs_destroy_context(file_context->nfs);
}
nfs_destroy_url(file_context->url);
free(file_context);
}

struct file_context *
open_file(const char *url, int flags)
{
struct file_context *file_context;

file_context = malloc(sizeof(struct file_context));
if (file_context == NULL) {
fprintf(stderr, "Failed to malloc file_context\n");
return NULL;
}
file_context->fd = -1;
file_context->nfs = NULL;
file_context->nfsfh = NULL;
file_context->url = NULL;

file_context->nfs = nfs_init_context();
if (file_context->nfs == NULL) {
fprintf(stderr, "failed to init context\n");
free_file_context(file_context);
return NULL;
}

file_context->url = nfs_parse_url_full(file_context->nfs, url);
if (file_context->url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}

if (nfs_mount(file_context->nfs, file_context->url->server,
file_context->url->path) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n",
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}

if (flags == O_RDONLY) {
if (nfs_open(file_context->nfs, file_context->url->file, flags,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to open file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
} else {
if (nfs_creat(file_context->nfs, file_context->url->file, 0660,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to creat file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
}
return file_context;
}

char *get_file_type(int mode)
{
switch (mode & S_IFMT) {
Expand Down Expand Up @@ -240,7 +158,8 @@ char *get_access_bits(int mode)

int main(int argc, char *argv[])
{
struct file_context *nf;
struct nfs_url *url;
struct nfs_context *nfs;
struct nfs_stat_64 st;

#ifdef WIN32
Expand All @@ -258,22 +177,43 @@ int main(int argc, char *argv[])
usage();
}

nf = open_file(argv[1], O_RDONLY);
if (nf == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[1]);
nfs = nfs_init_context();
if (nfs == NULL) {
fprintf(stderr, "failed to init context\n");
exit(10);
}
if (nfs_fstat64(nf->nfs, nf->nfsfh, &st) < 0) {
fprintf(stderr, "Failed to stat %s\n", argv[1]);

url = nfs_parse_url_full(nfs, argv[1]);
if (url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(nfs));
exit(10);
}

if (nfs_mount(nfs, url->server, url->path) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n",
nfs_get_error(nfs));
exit(10);
}

if (nfs_stat64(nfs, url->file, &st) < 0) {
fprintf(stderr, "Failed to stat %s\n", url->file);
exit(10);
}

printf(" File:%s\n", argv[1]);
printf(" Size: %-16" PRIu64 "Blocks: %-11" PRIu64 " IO Block: %" PRIu64 " %s\n",
st.nfs_size, st.nfs_blocks, st.nfs_blksize,
get_file_type(st.nfs_mode));
printf("Inode:%-12" PRIu64 "Links %" PRIu64 "\n",
printf("Inode:%-12" PRIu64 "Links %" PRIu64,
st.nfs_ino, st.nfs_nlink);
switch (st.nfs_mode & S_IFMT) {
case S_IFCHR:
case S_IFBLK:
printf(" Device type: %d, %d", (int)(st.nfs_rdev >> 32), (int)(st.nfs_rdev & 0xffffffff));
break;
default:
}
printf("\n");
printf("Access: (%04" PRIo64 "/%s) Uid: ( %" PRIu64 "/%s) Gid: ( %" PRIu64 "/%s)\n",
st.nfs_mode & 07777, get_access_bits(st.nfs_mode),
st.nfs_uid, uid_to_name(st.nfs_uid),
Expand All @@ -282,7 +222,8 @@ int main(int argc, char *argv[])
printf("Access: %s", ctime( (const time_t *) &st.nfs_atime));
printf("Modify: %s", ctime( (const time_t *) &st.nfs_mtime));
printf("Change: %s", ctime( (const time_t *) &st.nfs_ctime));
free_file_context(nf);

nfs_destroy_context(nfs);
nfs_destroy_url(url);
return 0;
}

0 comments on commit 46cf506

Please sign in to comment.