This patch will make mode_t 16-bits->32-bits, off_t 32-bits->64-bits,
ino_t 32-bits->64-bits, and nlink_t 16-bits->32-bits. As uid_t and
gid_t were already 32-bits we can drop the big_{mode,off,ino,uid,
gid,nlink}_t types from struct stat. This has huge consequences:
. lseek now has to accept a 64-bit argument and returns a 64-bit
result. Therefore we need a new lseek and return proper results
based on the used version (i.e., translate).
. some fcntl functions take and return a struct flock which holds 2
off_t fields. Like lseek, we need to distinguish between old and
new versions.
. getdents uses struct dirent which has among other fields an ino_t
and off_t field. Consequently we need a new getdents system call
and have to translate when we encounter an old call.
. truncate and ftruncate also take off_t arguments and therefore
require a new version.
To add insult to injury VFS itself uses getdents. This has as
consequence that we have 2^3 combinations of version for getdents; old
and new version of libc (new system calls), new and old version of VFS
(when booting an old image), old and new version of an FS. We can
distinguish between these cases as follows; libc will use new system
call numbers that have not yet been in use. When using a new number,
VFS knows libc is new, otherwise it's an old libc. Vice versa, libc
first tries the new number and if that fails due to ENOSYS (unknown
system call number) it knows it's dealing with an old VFS. Finally,
since we now have VFS-FS protocol versioning and by bumping the version
to 1, VFS knows an FS is old when it reports version 0 and an FS knows
VFS is old when VFS reports it's using version 0.
0 = old version, 1 = new version
libc | VFS | FS | approach
----------------------------
0 | 0 | 0 | Nothing to do
----------------------------
0 | 0 | 1 | VFS reports protocol version 0, FS falls back
| to using the old getdents function
----------------------------
0 | 1 | 0 | VFS reports protocol version 1, FS version 0.
| libc requires old getdents so there is nothing
| to do. VFS itself needs new getdents and will
| translate.
----------------------------
0 | 1 | 1 | VFS reports protocol version 1, FS version 1.
| VFS intercepts getdents results and translates
| them on the fly before returning results to libc
----------------------------
1 | 0 | 0 | libc first tries new system call number and
| then fall back to the old number. libc first
| translates the results to the new getdents before
| returning.
----------------------------
1 | 0 | 1 | VFS reports protocol version 0, so FS uses fall
| back. libc translates.
----------------------------
1 | 1 | 0 | VFS reports protocol version 1, FS version 0. VFS
| will intercept all results and translate on the
| fly.
----------------------------
1 | 1 | 1 | Nothing to do.
As VFS can distinguish between old and new system calls of (f)truncate,
lseek, and fcntl, it will translate the arguments and results if
necessary. Likewise libc falls back to using the old calls and
translating the results to the new version if necessary.
Change-Id: Id5bc819e6ef90f317dbddb21020e0a65028225ce
160 lines
4.8 KiB
C
160 lines
4.8 KiB
C
#include "fs.h"
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/statfs.h>
|
|
#include <sys/statvfs.h>
|
|
#include "inode.h"
|
|
#include "super.h"
|
|
#include <minix/vfsif.h>
|
|
|
|
/*===========================================================================*
|
|
* estimate_blocks *
|
|
*===========================================================================*/
|
|
static blkcnt_t estimate_blocks(struct inode *rip)
|
|
{
|
|
/* Return the number of 512-byte blocks used by this file. This includes space
|
|
* used by data zones and indirect blocks (actually also zones). Reading in all
|
|
* indirect blocks is too costly for a stat call, so we disregard holes and
|
|
* return a conservative estimation.
|
|
*/
|
|
blkcnt_t zones, sindirs, dindirs, nr_indirs, sq_indirs;
|
|
unsigned int zone_size;
|
|
|
|
/* Compute the number of zones used by the file. */
|
|
zone_size = rip->i_sp->s_block_size << rip->i_sp->s_log_zone_size;
|
|
|
|
zones = (blkcnt_t) ((rip->i_size + zone_size - 1) / zone_size);
|
|
|
|
/* Compute the number of indirect blocks needed for that zone count. */
|
|
nr_indirs = (blkcnt_t) rip->i_nindirs;
|
|
sq_indirs = nr_indirs * nr_indirs;
|
|
|
|
sindirs = (zones - (blkcnt_t) rip->i_ndzones + nr_indirs - 1) / nr_indirs;
|
|
dindirs = (sindirs - 1 + sq_indirs - 1) / sq_indirs;
|
|
|
|
/* Return the number of 512-byte blocks corresponding to the number of data
|
|
* zones and indirect blocks.
|
|
*/
|
|
return (zones + sindirs + dindirs) * (blkcnt_t) (zone_size / 512);
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* stat_inode *
|
|
*===========================================================================*/
|
|
static int stat_inode(
|
|
register struct inode *rip, /* pointer to inode to stat */
|
|
endpoint_t who_e, /* Caller endpoint */
|
|
cp_grant_id_t gid /* grant for the stat buf */
|
|
)
|
|
{
|
|
/* Common code for stat and fstat system calls. */
|
|
|
|
struct stat statbuf;
|
|
pmode_t mo;
|
|
int r, s;
|
|
|
|
/* Update the atime, ctime, and mtime fields in the inode, if need be. */
|
|
if (rip->i_update) update_times(rip);
|
|
|
|
/* Fill in the statbuf struct. */
|
|
mo = rip->i_mode & I_TYPE;
|
|
|
|
/* true iff special */
|
|
s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL);
|
|
|
|
memset(&statbuf, 0, sizeof(struct stat));
|
|
|
|
statbuf.st_dev = rip->i_dev;
|
|
statbuf.st_ino = (ino_t) rip->i_num;
|
|
statbuf.st_mode = (mode_t) rip->i_mode;
|
|
statbuf.st_nlink = (nlink_t) rip->i_nlinks;
|
|
statbuf.st_uid = rip->i_uid;
|
|
statbuf.st_gid = rip->i_gid;
|
|
statbuf.st_rdev = (s ? (dev_t) rip->i_zone[0] : NO_DEV);
|
|
statbuf.st_size = rip->i_size;
|
|
statbuf.st_atime = rip->i_atime;
|
|
statbuf.st_mtime = rip->i_mtime;
|
|
statbuf.st_ctime = rip->i_ctime;
|
|
statbuf.st_blksize = lmfs_fs_block_size();
|
|
statbuf.st_blocks = estimate_blocks(rip);
|
|
|
|
/* Copy the struct to user space. */
|
|
r = sys_safecopyto(who_e, gid, (vir_bytes) 0, (vir_bytes) &statbuf,
|
|
(size_t) sizeof(statbuf));
|
|
|
|
return(r);
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* fs_fstatfs *
|
|
*===========================================================================*/
|
|
int fs_fstatfs()
|
|
{
|
|
struct statfs st;
|
|
struct inode *rip;
|
|
int r;
|
|
|
|
if((rip = find_inode(fs_dev, ROOT_INODE)) == NULL)
|
|
return(EINVAL);
|
|
|
|
st.f_bsize = rip->i_sp->s_block_size;
|
|
|
|
/* Copy the struct to user space. */
|
|
r = sys_safecopyto(fs_m_in.m_source, (cp_grant_id_t) fs_m_in.REQ_GRANT,
|
|
(vir_bytes) 0, (vir_bytes) &st, (size_t) sizeof(st));
|
|
|
|
return(r);
|
|
}
|
|
|
|
|
|
/*===========================================================================*
|
|
* fs_statvfs *
|
|
*===========================================================================*/
|
|
int fs_statvfs()
|
|
{
|
|
struct statvfs st;
|
|
struct super_block *sp;
|
|
int r, scale;
|
|
u32_t used;
|
|
|
|
sp = get_super(fs_dev);
|
|
|
|
scale = sp->s_log_zone_size;
|
|
|
|
fs_blockstats((u32_t *) &st.f_blocks, (u32_t *) &st.f_bfree, &used);
|
|
st.f_bavail = st.f_bfree;
|
|
|
|
st.f_bsize = sp->s_block_size << scale;
|
|
st.f_frsize = sp->s_block_size;
|
|
st.f_files = sp->s_ninodes;
|
|
st.f_ffree = count_free_bits(sp, IMAP);
|
|
st.f_favail = st.f_ffree;
|
|
st.f_fsid = fs_dev;
|
|
st.f_flag = (sp->s_rd_only == 1 ? ST_RDONLY : 0);
|
|
st.f_namemax = MFS_DIRSIZ;
|
|
|
|
/* Copy the struct to user space. */
|
|
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.REQ_GRANT, 0, (vir_bytes) &st,
|
|
(phys_bytes) sizeof(st));
|
|
|
|
return(r);
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* fs_stat *
|
|
*===========================================================================*/
|
|
int fs_stat()
|
|
{
|
|
register int r; /* return value */
|
|
register struct inode *rip; /* target inode */
|
|
|
|
if ((rip = get_inode(fs_dev, (pino_t) fs_m_in.REQ_INODE_NR)) == NULL)
|
|
return(EINVAL);
|
|
|
|
r = stat_inode(rip, fs_m_in.m_source, (cp_grant_id_t) fs_m_in.REQ_GRANT);
|
|
put_inode(rip); /* release the inode */
|
|
return(r);
|
|
}
|
|
|