xref: /freebsd-14.2/sys/compat/linux/linux_stats.c (revision 92a3e552)
1 /*-
2  * Copyright (c) 1994-1995 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/conf.h>
33 #include <sys/dirent.h>
34 #include <sys/file.h>
35 #include <sys/filedesc.h>
36 #include <sys/socketvar.h>
37 #include <sys/pipe.h>
38 #include <sys/proc.h>
39 #include <sys/mount.h>
40 #include <sys/namei.h>
41 #include <sys/stat.h>
42 #include <sys/systm.h>
43 #include <sys/vnode.h>
44 
45 #include <i386/linux/linux.h>
46 #include <i386/linux/linux_proto.h>
47 #include <i386/linux/linux_util.h>
48 
49 struct linux_newstat {
50 	u_short	stat_dev;
51 	u_short	__pad1;
52 	u_long	stat_ino;
53 	u_short	stat_mode;
54 	u_short	stat_nlink;
55 	u_short	stat_uid;
56 	u_short	stat_gid;
57 	u_short	stat_rdev;
58 	u_short	__pad2;
59 	u_long	stat_size;
60 	u_long	stat_blksize;
61 	u_long	stat_blocks;
62 	u_long	stat_atime;
63 	u_long	__unused1;
64 	u_long	stat_mtime;
65 	u_long	__unused2;
66 	u_long	stat_ctime;
67 	u_long	__unused3;
68 	u_long	__unused4;
69 	u_long	__unused5;
70 };
71 
72 struct linux_ustat
73 {
74 	int	f_tfree;
75 	u_long	f_tinode;
76 	char	f_fname[6];
77 	char	f_fpack[6];
78 };
79 
80 static int
81 newstat_copyout(struct stat *buf, void *ubuf)
82 {
83 	struct linux_newstat tbuf;
84 
85 	tbuf.stat_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
86 	tbuf.stat_ino = buf->st_ino;
87 	tbuf.stat_mode = buf->st_mode;
88 	tbuf.stat_nlink = buf->st_nlink;
89 	tbuf.stat_uid = buf->st_uid;
90 	tbuf.stat_gid = buf->st_gid;
91 	tbuf.stat_rdev = buf->st_rdev;
92 	tbuf.stat_size = buf->st_size;
93 	tbuf.stat_atime = buf->st_atime;
94 	tbuf.stat_mtime = buf->st_mtime;
95 	tbuf.stat_ctime = buf->st_ctime;
96 	tbuf.stat_blksize = buf->st_blksize;
97 	tbuf.stat_blocks = buf->st_blocks;
98 
99 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
100 }
101 
102 int
103 linux_newstat(struct proc *p, struct linux_newstat_args *args)
104 {
105 	struct stat buf;
106 	struct nameidata nd;
107 	int error;
108 	caddr_t sg;
109 
110 	sg = stackgap_init();
111 	CHECKALTEXIST(p, &sg, args->path);
112 
113 #ifdef DEBUG
114 	printf("Linux-emul(%ld): newstat(%s, *)\n", (long)p->p_pid,
115 	       args->path);
116 #endif
117 
118 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
119 	       args->path, p);
120 	error = namei(&nd);
121 	if (error)
122 		return (error);
123 
124 	error = vn_stat(nd.ni_vp, &buf, p);
125 	vput(nd.ni_vp);
126 	if (error)
127 		return (error);
128 
129 	return (newstat_copyout(&buf, args->buf));
130 }
131 
132 /*
133  * Get file status; this version does not follow links.
134  */
135 int
136 linux_newlstat(p, uap)
137 	struct proc *p;
138 	struct linux_newlstat_args *uap;
139 {
140 	int error;
141 	struct vnode *vp;
142 	struct stat sb;
143 	struct nameidata nd;
144 	caddr_t sg;
145 
146 	sg = stackgap_init();
147 	CHECKALTEXIST(p, &sg, uap->path);
148 
149 #ifdef DEBUG
150 	printf("Linux-emul(%ld): newlstat(%s, *)\n", (long)p->p_pid,
151 	       uap->path);
152 #endif
153 
154 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
155 	       uap->path, p);
156 	error = namei(&nd);
157 	if (error)
158 		return (error);
159 
160 	vp = nd.ni_vp;
161 	error = vn_stat(vp, &sb, p);
162 	vput(vp);
163 	if (error)
164 		return (error);
165 
166 	return (newstat_copyout(&sb, uap->buf));
167 }
168 
169 int
170 linux_newfstat(struct proc *p, struct linux_newfstat_args *args)
171 {
172 	struct filedesc *fdp;
173 	struct file *fp;
174 	struct stat buf;
175 	int error;
176 
177 	fdp = p->p_fd;
178 
179 #ifdef DEBUG
180 	printf("Linux-emul(%ld): newfstat(%d, *)\n", (long)p->p_pid, args->fd);
181 #endif
182 
183 	if ((unsigned)args->fd >= fdp->fd_nfiles ||
184 	    (fp = fdp->fd_ofiles[args->fd]) == NULL)
185 		return (EBADF);
186 
187 	error = fo_stat(fp, &buf, p);
188 	if (!error)
189 		error = newstat_copyout(&buf, args->buf);
190 
191 	return (error);
192 }
193 
194 struct linux_statfs_buf {
195 	long ftype;
196 	long fbsize;
197 	long fblocks;
198 	long fbfree;
199 	long fbavail;
200 	long ffiles;
201 	long fffree;
202 	linux_fsid_t ffsid;
203 	long fnamelen;
204 	long fspare[6];
205 };
206 
207 int
208 linux_statfs(struct proc *p, struct linux_statfs_args *args)
209 {
210 	struct mount *mp;
211 	struct nameidata *ndp;
212 	struct statfs *bsd_statfs;
213 	struct nameidata nd;
214 	struct linux_statfs_buf linux_statfs_buf;
215 	int error;
216 	caddr_t sg;
217 
218 	sg = stackgap_init();
219 	CHECKALTEXIST(p, &sg, args->path);
220 
221 #ifdef DEBUG
222 	printf("Linux-emul(%d): statfs(%s, *)\n", p->p_pid, args->path);
223 #endif
224 	ndp = &nd;
225 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, curproc);
226 	error = namei(ndp);
227 	if (error)
228 		return error;
229 	mp = ndp->ni_vp->v_mount;
230 	bsd_statfs = &mp->mnt_stat;
231 	vrele(ndp->ni_vp);
232 	error = VFS_STATFS(mp, bsd_statfs, p);
233 	if (error)
234 		return error;
235 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
236 	linux_statfs_buf.ftype = bsd_statfs->f_type;
237 	linux_statfs_buf.fbsize = bsd_statfs->f_bsize;
238 	linux_statfs_buf.fblocks = bsd_statfs->f_blocks;
239 	linux_statfs_buf.fbfree = bsd_statfs->f_bfree;
240 	linux_statfs_buf.fbavail = bsd_statfs->f_bavail;
241   	linux_statfs_buf.fffree = bsd_statfs->f_ffree;
242 	linux_statfs_buf.ffiles = bsd_statfs->f_files;
243 	linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0];
244 	linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1];
245 	linux_statfs_buf.fnamelen = MAXNAMLEN;
246 	return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf,
247 		       sizeof(struct linux_statfs_buf));
248 }
249 
250 int
251 linux_fstatfs(struct proc *p, struct linux_fstatfs_args *args)
252 {
253 	struct file *fp;
254 	struct mount *mp;
255 	struct statfs *bsd_statfs;
256 	struct linux_statfs_buf linux_statfs_buf;
257 	int error;
258 
259 #ifdef DEBUG
260 	printf("Linux-emul(%d): fstatfs(%d, *)\n", p->p_pid, args->fd);
261 #endif
262 	error = getvnode(p->p_fd, args->fd, &fp);
263 	if (error)
264 		return error;
265 	mp = ((struct vnode *)fp->f_data)->v_mount;
266 	bsd_statfs = &mp->mnt_stat;
267 	error = VFS_STATFS(mp, bsd_statfs, p);
268 	if (error)
269 		return error;
270 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
271 	linux_statfs_buf.ftype = bsd_statfs->f_type;
272 	linux_statfs_buf.fbsize = bsd_statfs->f_bsize;
273 	linux_statfs_buf.fblocks = bsd_statfs->f_blocks;
274 	linux_statfs_buf.fbfree = bsd_statfs->f_bfree;
275 	linux_statfs_buf.fbavail = bsd_statfs->f_bavail;
276   	linux_statfs_buf.fffree = bsd_statfs->f_ffree;
277 	linux_statfs_buf.ffiles = bsd_statfs->f_files;
278 	linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0];
279 	linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1];
280 	linux_statfs_buf.fnamelen = MAXNAMLEN;
281 	return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf,
282 		       sizeof(struct linux_statfs_buf));
283 }
284 
285 int
286 linux_ustat(p, uap)
287 	struct proc *p;
288 	struct linux_ustat_args *uap;
289 {
290 	struct linux_ustat lu;
291 	dev_t dev;
292 	struct vnode *vp;
293 	struct statfs *stat;
294 	int error;
295 
296 #ifdef DEBUG
297 	printf("Linux-emul(%ld): ustat(%d, *)\n", (long)p->p_pid, uap->dev);
298 #endif
299 
300 	/*
301 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
302 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
303 	 */
304 	bzero(&lu, sizeof(lu));
305 
306 	/*
307 	 * XXX - Don't return an error if we can't find a vnode for the
308 	 * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
309 	 * dev_t. The dev_t that is used now may as well be a truncated
310 	 * dev_t returned from previous syscalls. Just return a bzeroed
311 	 * ustat in that case.
312 	 */
313 	dev = makebdev(uap->dev >> 8, uap->dev & 0xFF);
314 	if (vfinddev(dev, VBLK, &vp)) {
315 		if (vp->v_mount == NULL)
316 			return (EINVAL);
317 		stat = &(vp->v_mount->mnt_stat);
318 		error = VFS_STATFS(vp->v_mount, stat, p);
319 		if (error)
320 			return (error);
321 
322 		lu.f_tfree = stat->f_bfree;
323 		lu.f_tinode = stat->f_ffree;
324 	}
325 
326 	return (copyout(&lu, uap->ubuf, sizeof(lu)));
327 }
328