xref: /openbmc/qemu/bsd-user/freebsd/os-stat.c (revision f14eced5)
1 /*
2  *  FreeBSD stat related conversion routines
3  *
4  *  Copyright (c) 2013 Stacey D. Son
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "qemu.h"
22 
23 /*
24  * stat conversion
25  */
26 abi_long h2t_freebsd11_stat(abi_ulong target_addr,
27         struct freebsd11_stat *host_st)
28 {
29     struct target_freebsd11_stat *target_st;
30 
31     if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) {
32         return -TARGET_EFAULT;
33     }
34     memset(target_st, 0, sizeof(*target_st));
35     __put_user(host_st->st_dev, &target_st->st_dev);
36     __put_user(host_st->st_ino, &target_st->st_ino);
37     __put_user(host_st->st_mode, &target_st->st_mode);
38     __put_user(host_st->st_nlink, &target_st->st_nlink);
39     __put_user(host_st->st_uid, &target_st->st_uid);
40     __put_user(host_st->st_gid, &target_st->st_gid);
41     __put_user(host_st->st_rdev, &target_st->st_rdev);
42     __put_user(host_st->st_atim.tv_sec, &target_st->st_atim.tv_sec);
43     __put_user(host_st->st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
44     __put_user(host_st->st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
45     __put_user(host_st->st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
46     __put_user(host_st->st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
47     __put_user(host_st->st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
48     __put_user(host_st->st_size, &target_st->st_size);
49     __put_user(host_st->st_blocks, &target_st->st_blocks);
50     __put_user(host_st->st_blksize, &target_st->st_blksize);
51     __put_user(host_st->st_flags, &target_st->st_flags);
52     __put_user(host_st->st_gen, &target_st->st_gen);
53     /* st_lspare not used */
54     __put_user(host_st->st_birthtim.tv_sec, &target_st->st_birthtim.tv_sec);
55     __put_user(host_st->st_birthtim.tv_nsec, &target_st->st_birthtim.tv_nsec);
56     unlock_user_struct(target_st, target_addr, 1);
57 
58     return 0;
59 }
60 
61 abi_long h2t_freebsd_stat(abi_ulong target_addr,
62         struct stat *host_st)
63 {
64     struct target_stat *target_st;
65 
66     if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) {
67         return -TARGET_EFAULT;
68     }
69     memset(target_st, 0, sizeof(*target_st));
70     __put_user(host_st->st_dev, &target_st->st_dev);
71     __put_user(host_st->st_ino, &target_st->st_ino);
72     __put_user(host_st->st_nlink, &target_st->st_nlink);
73     __put_user(host_st->st_mode, &target_st->st_mode);
74     __put_user(host_st->st_uid, &target_st->st_uid);
75     __put_user(host_st->st_gid, &target_st->st_gid);
76     __put_user(host_st->st_rdev, &target_st->st_rdev);
77     __put_user(host_st->st_atim.tv_sec, &target_st->st_atim.tv_sec);
78     __put_user(host_st->st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
79 #ifdef TARGET_HAS_STAT_TIME_T_EXT
80 /*    __put_user(host_st->st_mtim_ext, &target_st->st_mtim_ext); XXX */
81 #endif
82     __put_user(host_st->st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
83     __put_user(host_st->st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
84 #ifdef TARGET_HAS_STAT_TIME_T_EXT
85 /*    __put_user(host_st->st_ctim_ext, &target_st->st_ctim_ext); XXX */
86 #endif
87     __put_user(host_st->st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
88     __put_user(host_st->st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
89 #ifdef TARGET_HAS_STAT_TIME_T_EXT
90 /*    __put_user(host_st->st_birthtim_ext, &target_st->st_birthtim_ext); XXX */
91 #endif
92     __put_user(host_st->st_birthtim.tv_sec, &target_st->st_birthtim.tv_sec);
93     __put_user(host_st->st_birthtim.tv_nsec, &target_st->st_birthtim.tv_nsec);
94 
95     __put_user(host_st->st_size, &target_st->st_size);
96     __put_user(host_st->st_blocks, &target_st->st_blocks);
97     __put_user(host_st->st_blksize, &target_st->st_blksize);
98     __put_user(host_st->st_flags, &target_st->st_flags);
99     __put_user(host_st->st_gen, &target_st->st_gen);
100     unlock_user_struct(target_st, target_addr, 1);
101 
102     return 0;
103 }
104 
105 abi_long h2t_freebsd11_nstat(abi_ulong target_addr,
106         struct freebsd11_stat *host_st)
107 {
108     struct target_freebsd11_nstat *target_st;
109 
110     if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) {
111         return -TARGET_EFAULT;
112     }
113     memset(target_st, 0, sizeof(*target_st));
114     __put_user(host_st->st_dev, &target_st->st_dev);
115     __put_user(host_st->st_ino, &target_st->st_ino);
116     __put_user(host_st->st_mode, &target_st->st_mode);
117     __put_user(host_st->st_nlink, &target_st->st_nlink);
118     __put_user(host_st->st_uid, &target_st->st_uid);
119     __put_user(host_st->st_gid, &target_st->st_gid);
120     __put_user(host_st->st_rdev, &target_st->st_rdev);
121     __put_user(host_st->st_atim.tv_sec, &target_st->st_atim.tv_sec);
122     __put_user(host_st->st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
123     __put_user(host_st->st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
124     __put_user(host_st->st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
125     __put_user(host_st->st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
126     __put_user(host_st->st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
127     __put_user(host_st->st_size, &target_st->st_size);
128     __put_user(host_st->st_blocks, &target_st->st_blocks);
129     __put_user(host_st->st_blksize, &target_st->st_blksize);
130     __put_user(host_st->st_flags, &target_st->st_flags);
131     __put_user(host_st->st_gen, &target_st->st_gen);
132     __put_user(host_st->st_birthtim.tv_sec, &target_st->st_birthtim.tv_sec);
133     __put_user(host_st->st_birthtim.tv_nsec, &target_st->st_birthtim.tv_nsec);
134     unlock_user_struct(target_st, target_addr, 1);
135 
136     return 0;
137 }
138 
139 /*
140  * file handle conversion
141  */
142 abi_long t2h_freebsd_fhandle(fhandle_t *host_fh, abi_ulong target_addr)
143 {
144     target_freebsd_fhandle_t *target_fh;
145 
146     if (!lock_user_struct(VERIFY_READ, target_fh, target_addr, 1)) {
147         return -TARGET_EFAULT;
148     }
149     __get_user(host_fh->fh_fsid.val[0], &target_fh->fh_fsid.val[0]);
150     __get_user(host_fh->fh_fsid.val[1], &target_fh->fh_fsid.val[0]);
151     __get_user(host_fh->fh_fid.fid_len, &target_fh->fh_fid.fid_len);
152     /* u_short         fid_data0; */
153     memcpy(host_fh->fh_fid.fid_data, target_fh->fh_fid.fid_data,
154         TARGET_MAXFIDSZ);
155     unlock_user_struct(target_fh, target_addr, 0);
156     return 0;
157 }
158 
159 abi_long h2t_freebsd_fhandle(abi_ulong target_addr, fhandle_t *host_fh)
160 {
161     target_freebsd_fhandle_t *target_fh;
162 
163     if (!lock_user_struct(VERIFY_WRITE, target_fh, target_addr, 0)) {
164         return -TARGET_EFAULT;
165     }
166     __put_user(host_fh->fh_fsid.val[0], &target_fh->fh_fsid.val[0]);
167     __put_user(host_fh->fh_fsid.val[1], &target_fh->fh_fsid.val[0]);
168     __put_user(host_fh->fh_fid.fid_len, &target_fh->fh_fid.fid_len);
169     /* u_short         fid_data0; */
170     memcpy(target_fh->fh_fid.fid_data, host_fh->fh_fid.fid_data,
171             TARGET_MAXFIDSZ);
172     unlock_user_struct(target_fh, target_addr, 1);
173     return 0;
174 }
175 
176 /*
177  *  file system stat
178  */
179 abi_long h2t_freebsd11_statfs(abi_ulong target_addr,
180         struct freebsd11_statfs *host_statfs)
181 {
182     struct target_freebsd11_statfs *target_statfs;
183 
184     if (!lock_user_struct(VERIFY_WRITE, target_statfs, target_addr, 0)) {
185         return -TARGET_EFAULT;
186     }
187     __put_user(host_statfs->f_version, &target_statfs->f_version);
188     __put_user(host_statfs->f_type, &target_statfs->f_type);
189     __put_user(host_statfs->f_flags, &target_statfs->f_flags);
190     __put_user(host_statfs->f_bsize, &target_statfs->f_bsize);
191     __put_user(host_statfs->f_iosize, &target_statfs->f_iosize);
192     __put_user(host_statfs->f_blocks, &target_statfs->f_blocks);
193     __put_user(host_statfs->f_bfree, &target_statfs->f_bfree);
194     __put_user(host_statfs->f_bavail, &target_statfs->f_bavail);
195     __put_user(host_statfs->f_files, &target_statfs->f_files);
196     __put_user(host_statfs->f_ffree, &target_statfs->f_ffree);
197     __put_user(host_statfs->f_syncwrites, &target_statfs->f_syncwrites);
198     __put_user(host_statfs->f_asyncwrites, &target_statfs->f_asyncwrites);
199     __put_user(host_statfs->f_syncreads, &target_statfs->f_syncreads);
200     __put_user(host_statfs->f_asyncreads, &target_statfs->f_asyncreads);
201     /* uint64_t f_spare[10]; */
202     __put_user(host_statfs->f_namemax, &target_statfs->f_namemax);
203     __put_user(host_statfs->f_owner, &target_statfs->f_owner);
204     __put_user(host_statfs->f_fsid.val[0], &target_statfs->f_fsid.val[0]);
205     __put_user(host_statfs->f_fsid.val[1], &target_statfs->f_fsid.val[1]);
206     /* char f_charspace[80]; */
207     strncpy(target_statfs->f_fstypename, host_statfs->f_fstypename,
208         sizeof(target_statfs->f_fstypename));
209     strncpy(target_statfs->f_mntfromname, host_statfs->f_mntfromname,
210         sizeof(target_statfs->f_mntfromname));
211     strncpy(target_statfs->f_mntonname, host_statfs->f_mntonname,
212         sizeof(target_statfs->f_mntonname));
213     unlock_user_struct(target_statfs, target_addr, 1);
214     return 0;
215 }
216 
217 abi_long h2t_freebsd_statfs(abi_ulong target_addr,
218         struct statfs *host_statfs)
219 {
220     struct target_statfs *target_statfs;
221 
222     if (!lock_user_struct(VERIFY_WRITE, target_statfs, target_addr, 0)) {
223         return -TARGET_EFAULT;
224     }
225     __put_user(host_statfs->f_version, &target_statfs->f_version);
226     __put_user(host_statfs->f_type, &target_statfs->f_type);
227     __put_user(host_statfs->f_flags, &target_statfs->f_flags);
228     __put_user(host_statfs->f_bsize, &target_statfs->f_bsize);
229     __put_user(host_statfs->f_iosize, &target_statfs->f_iosize);
230     __put_user(host_statfs->f_blocks, &target_statfs->f_blocks);
231     __put_user(host_statfs->f_bfree, &target_statfs->f_bfree);
232     __put_user(host_statfs->f_bavail, &target_statfs->f_bavail);
233     __put_user(host_statfs->f_files, &target_statfs->f_files);
234     __put_user(host_statfs->f_ffree, &target_statfs->f_ffree);
235     __put_user(host_statfs->f_syncwrites, &target_statfs->f_syncwrites);
236     __put_user(host_statfs->f_asyncwrites, &target_statfs->f_asyncwrites);
237     __put_user(host_statfs->f_syncreads, &target_statfs->f_syncreads);
238     __put_user(host_statfs->f_asyncreads, &target_statfs->f_asyncreads);
239     /* uint64_t f_spare[10]; */
240     __put_user(host_statfs->f_namemax, &target_statfs->f_namemax);
241     __put_user(host_statfs->f_owner, &target_statfs->f_owner);
242     __put_user(host_statfs->f_fsid.val[0], &target_statfs->f_fsid.val[0]);
243     __put_user(host_statfs->f_fsid.val[1], &target_statfs->f_fsid.val[1]);
244     /* char f_charspace[80]; */
245     strncpy(target_statfs->f_fstypename, host_statfs->f_fstypename,
246         sizeof(target_statfs->f_fstypename));
247     strncpy(target_statfs->f_mntfromname, host_statfs->f_mntfromname,
248         sizeof(target_statfs->f_mntfromname));
249     strncpy(target_statfs->f_mntonname, host_statfs->f_mntonname,
250         sizeof(target_statfs->f_mntonname));
251     unlock_user_struct(target_statfs, target_addr, 1);
252     return 0;
253 }
254 
255 /*
256  * fcntl cmd conversion
257  */
258 abi_long target_to_host_fcntl_cmd(int cmd)
259 {
260     return cmd;
261 }
262 
263