Date: 24 Aug 2001 13:26:59 +0200 From: Thomas Gellekum <tg@melaten.rwth-aachen.de> To: freebsd-audit@FreeBSD.org Subject: RFC: statvfs(3) Message-ID: <kqitfdwvek.fsf@cip12.melaten.rwth-aachen.de>
next in thread | raw e-mail | index | archive | help
--=-=-= Moin, here's a simple implementation of statvfs(3), which is mandated by SUSV2. I'd welcome suggestions on how to deal with the f_namemax member of struct statvfs. Currently the functions return PATH_MAX for it, but this is probably wrong for some filesystem types. We could convert one of the f_spare* members in struct statfs (I'd prefer 'long f_spare[2]' -> 'long f_namemax; long f_spare') and define a <FS>_PATH_MAX (in <sys/param.h>?) to initialize that for every filesystem type. Comments? tg --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=statvfs.diff Index: lib/libc/gen/Makefile.inc =================================================================== RCS file: /usr/cvs/src/lib/libc/gen/Makefile.inc,v retrieving revision 1.80 diff -u -r1.80 Makefile.inc --- lib/libc/gen/Makefile.inc 2001/08/17 22:09:15 1.80 +++ lib/libc/gen/Makefile.inc 2001/08/22 19:03:57 @@ -25,7 +25,7 @@ setdomainname.c sethostname.c setjmperr.c setmode.c setprogname.c \ setproctitle.c \ shmat.c shmctl.c shmdt.c shmget.c siginterrupt.c siglist.c signal.c \ - sigsetops.c sleep.c srand48.c stringlist.c strtofflags.c \ + sigsetops.c sleep.c srand48.c statvfs.c stringlist.c strtofflags.c \ sysconf.c sysctl.c sysctlbyname.c sysctlnametomib.c \ syslog.c telldir.c termios.c time.c times.c timezone.c ttyname.c \ ttyslot.c ualarm.c uname.c unvis.c usleep.c utime.c valloc.c vis.c \ @@ -53,7 +53,7 @@ nice.3 nlist.3 pause.3 popen.3 psignal.3 pwcache.3 \ raise.3 rand48.3 rfork_thread.3 \ scandir.3 setjmp.3 setmode.3 setproctitle.3 shm_open.3 \ - siginterrupt.3 signal.3 sigsetops.3 sleep.3 stringlist.3 \ + siginterrupt.3 signal.3 sigsetops.3 sleep.3 statvfs.3 stringlist.3 \ strtofflags.3 sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 \ tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 time.3 times.3 timezone.3 \ ttyname.3 tzset.3 ualarm.3 uname.3 unvis.3 usleep.3 utime.3 \ @@ -115,6 +115,7 @@ MLINKS+=sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \ sigsetops.3 sigemptyset.3 sigsetops.3 sigfillset.3 \ sigsetops.3 sigismember.3 +MLINKS+=statvfs.3 fstatvfs.3 MLINKS+=stringlist.3 sl_add.3 stringlist.3 sl_find.3 \ stringlist.3 sl_free.3 stringlist.3 sl_init.3 MLINKS+=sysctl.3 sysctlbyname.3 sysctl.3 sysctlnametomib.3 --- /dev/null Fri Aug 24 07:51:38 2001 +++ lib/libc/gen/statvfs.3 Mon Aug 20 09:00:06 2001 @@ -0,0 +1,152 @@ +.\" Copyright (c) 2001 +.\" The FreeBSD Project, Inc. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY The FreeBSD Project, Inc. AND +.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL The FreeBSD Project, Inc. OR CONTRIBUTORS BE LIABLE FOR +.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd August 19, 2001 +.Dt STATVFS 3 +.Os +.Sh NAME +.Nm statvfs +.Nd get file system statistics +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.Fd #include <sys/statvfs.h> +.Ft int +.Fn statvfs "const char *path" "struct statvfs *buf" +.Ft int +.Fn fstatvfs "int fd" "struct statvfs *buf" +.Sh DESCRIPTION +.Fn Statvfs +returns information about a mounted file system. +.Fa Path +is the path name of any file within the mounted filesystem. +.Fa Buf +is a pointer to a +.Fn statvfs +structure defined as follows: +.Bd -literal +typedef u_int32_t fsblkcnt_t; /* file system block count */ +typedef u_int32_t fsfilcnt_t; /* file system file count */ + +struct statvfs { +unsigned long f_bsize; /* file system block size */ +unsigned long f_frsize; /* fundamental file system + block size */ +fsblkcnt_t f_blocks; /* total number of blocks on file + system in units of f_frsize */ +fsblkcnt_t f_bfree; /* total number of free blocks */ +fsblkcnt_t f_bavail; /* number of free blocks available to + non-privileged process */ +fsfilcnt_t f_files; /* total number of file + serial numbers */ +fsfilcnt_t f_ffree; /* total number of free file + serial numbers */ +fsfilcnt_t f_favail; /* number of file serial numbers + available to non-privileged + process */ +unsigned long f_fsid; /* file system id */ +unsigned long f_flag; /* bit mask of f_flag values */ +unsigned long f_namemax; /* maximum filename length */ +}; +.Ed +The flags that may be returned include: +.Bl -tag -width ST_RDONLY +.It Dv ST_RDONLY +The filesystem is mounted read-only; +Even the super-user may not write on it. +.It Dv ST_NOSUID +Setuid and setgid bits on files are not honored when they are executed. +.El +.Pp +Fields that are undefined for a particular file system are set to -1. +.Fn Fstatvfs +returns the same information about an open file referenced by descriptor +.Fa fd . +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, -1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn Statvfs +fails if one or more of the following are true: +.Bl -tag -width Er +.It Bq Er ENOTDIR +A component of the path prefix of +.Fa Path +is not a directory. +.It Bq Er ENAMETOOLONG +The length of a component of +.Fa path +exceeds 255 characters, +or the length of +.Fa path +exceeds 1023 characters. +.It Bq Er ENOENT +The file referred to by +.Fa path +does not exist. +.It Bq Er EACCES +Search permission is denied for a component of the path prefix of +.Fa path . +.It Bq Er ELOOP +Too many symbolic links were encountered in translating +.Fa path . +.It Bq Er EFAULT +.Fa Buf +or +.Fa path +points to an invalid address. +.It Bq Er EIO +An +.Tn I/O +error occurred while reading from or writing to the file system. +.El +.Pp +.Fn Fstatvfs +fails if one or more of the following are true: +.Bl -tag -width Er +.It Bq Er EBADF +.Fa fd +is not a valid open file descriptor. +.It Bq Er EFAULT +.Fa Buf +points to an invalid address. +.It Bq Er EIO +An +.Tn I/O +error occurred while reading from or writing to the file system. +.El +.Sh SEE ALSO +.Xr fstatfs 2 , +.Xr statfs 2 +.Sh BUGS +The +.Fa f_namemax +member will always return +.Dv PATH_MAX +for now. --- /dev/null Fri Aug 24 07:51:38 2001 +++ lib/libc/gen/statvfs.c Mon Aug 20 10:57:05 2001 @@ -0,0 +1,105 @@ +/*- + * Copyright (c) 2001 The FreeBSD Project, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY The FreeBSD Project, Inc. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL The FreeBSD Project, Inc. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/statvfs.h> +#include <errno.h> + +/* + * fstatvfs -- + * get information about the file system containing the file referenced + * by 'filedes'. + * + * Currently, there does not seem to be a way to find out about the + * maximum file length for a particular file system type, so we simply + * return 'PATH_MAX'. + */ +int +fstatvfs(int filedes, struct statvfs *buf) +{ + struct statfs st; + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + if (fstatfs(filedes, &st) == -1) + return -1; + + buf->f_bsize = (unsigned long)st.f_iosize; + buf->f_frsize = (unsigned long)st.f_bsize; + buf->f_blocks = (fsblkcnt_t)st.f_blocks; + buf->f_bfree = (fsblkcnt_t)st.f_bfree; + buf->f_bavail = (fsblkcnt_t)st.f_bavail; + buf->f_files = (fsfilcnt_t)st.f_files; + buf->f_ffree = (fsfilcnt_t)st.f_ffree; + buf->f_favail = (fsfilcnt_t)st.f_ffree; + buf->f_fsid = (unsigned long)st.f_fsid.val[0]; + buf->f_flag = (unsigned long)st.f_flags; + buf->f_namemax = PATH_MAX; /* XXX */ + + return 0; +} + +/* + * statvfs -- + * get information about the file system containing the file named by + * 'path'. + * + * Currently, there does not seem to be a way to find out about the + * maximum file length for a particular file system type, so we simply + * return 'PATH_MAX'. + */ +int +statvfs(const char *path, struct statvfs *buf) +{ + struct statfs st; + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + if (statfs(path, &st) == -1) + return -1; + + buf->f_bsize = (unsigned long)st.f_iosize; + buf->f_frsize = (unsigned long)st.f_bsize; + buf->f_blocks = (fsblkcnt_t)st.f_blocks; + buf->f_bfree = (fsblkcnt_t)st.f_bfree; + buf->f_bavail = (fsblkcnt_t)st.f_bavail; + buf->f_files = (fsfilcnt_t)st.f_files; + buf->f_ffree = (fsfilcnt_t)st.f_ffree; + buf->f_favail = (fsfilcnt_t)st.f_ffree; + buf->f_fsid = (unsigned long)st.f_fsid.val[0]; + buf->f_flag = (unsigned long)st.f_flags; + buf->f_namemax = PATH_MAX; /* XXX */ + + return 0; +} Index: sys/sys/types.h =================================================================== RCS file: /usr/cvs/src/sys/sys/types.h,v retrieving revision 1.44 diff -u -r1.44 types.h --- sys/sys/types.h 2001/07/16 00:55:26 1.44 +++ sys/sys/types.h 2001/08/19 15:25:23 @@ -72,6 +72,8 @@ typedef int32_t daddr_t; /* disk address */ typedef u_int32_t u_daddr_t; /* unsigned disk address */ typedef u_int32_t fixpt_t; /* fixed point number */ +typedef u_int32_t fsblkcnt_t; /* file system block count */ +typedef u_int32_t fsfilcnt_t; /* file system file count */ typedef u_int32_t gid_t; /* group id */ typedef u_int32_t in_addr_t; /* base type for internet address */ typedef u_int16_t in_port_t; --=-=-=-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?kqitfdwvek.fsf>