forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstadir.c
More file actions
137 lines (110 loc) · 4.19 KB
/
Copy pathstadir.c
File metadata and controls
137 lines (110 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "fs.h"
#include <string.h>
#include <assert.h>
#include <sys/stat.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;
mode_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_statvfs *
*===========================================================================*/
int fs_statvfs()
{
struct statvfs st;
struct super_block *sp;
int r, scale;
u64_t used;
sp = get_super(fs_dev);
scale = sp->s_log_zone_size;
memset(&st, 0, sizeof(st));
fs_blockstats(&st.f_blocks, &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_iosize = st.f_frsize;
st.f_files = sp->s_ninodes;
st.f_ffree = count_free_bits(sp, IMAP);
st.f_favail = st.f_ffree;
st.f_namemax = MFS_DIRSIZ;
/* Copy the struct to user space. */
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.m_vfs_fs_statvfs.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, fs_m_in.m_vfs_fs_stat.inode)) == NULL)
return(EINVAL);
r = stat_inode(rip, fs_m_in.m_source, fs_m_in.m_vfs_fs_stat.grant);
put_inode(rip); /* release the inode */
return(r);
}