16ae75c730f53001209eb63400579c6b38ade4f5
[dragonfly.git] / sys / emulation / dragonfly12 / dfbsd12_stat.c
1 /*-
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Joerg Sonnenberger <joerg@bec.de>.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/emulation/dragonfly12/dfbsd12_stat.c,v 1.4 2008/09/17 21:44:16 dillon Exp $
35  */
36
37 #include "opt_compatdf12.h"
38
39 #include <sys/param.h>
40 #include <sys/kern_syscall.h>
41 #include <sys/mount.h>
42 #include <sys/nlookup.h>
43 #include <sys/proc.h>
44 #include <sys/priv.h>
45 #include <sys/stat.h>
46 #include <sys/sysproto.h>
47 #include <sys/systm.h>
48 #include <sys/thread.h>
49 #include <sys/vnode.h>
50 #include <emulation/dragonfly12/stat.h>
51
52 #include <sys/mplock2.h>
53
54 static void
55 cvtstat(struct dfbsd12_stat *oldstat, struct stat *newstat)
56 {
57         bzero(oldstat, sizeof(oldstat));
58
59         oldstat->st_dev = newstat->st_dev;
60         oldstat->st_ino = newstat->st_ino;      /* truncation */
61         oldstat->st_mode = newstat->st_mode;
62         oldstat->st_nlink = newstat->st_nlink;  /* truncation */
63         oldstat->st_uid = newstat->st_uid;
64         oldstat->st_gid = newstat->st_gid;
65         oldstat->st_rdev = newstat->st_rdev;
66         oldstat->st_atimespec = newstat->st_atimespec;
67         oldstat->st_mtimespec = newstat->st_mtimespec;
68         oldstat->st_ctimespec = newstat->st_ctimespec;
69         oldstat->st_size = newstat->st_size;
70         oldstat->st_blocks = newstat->st_blocks;
71         oldstat->st_blksize = newstat->st_blksize;
72         oldstat->st_flags = newstat->st_flags;
73         oldstat->st_gen = newstat->st_gen;
74 }
75
76 /*
77  * stat_args(char *path, struct dfbsd12_stat *ub)
78  *
79  * Get file status; this version follows links.
80  *
81  * MPALMOSTSAFE
82  */
83 int
84 sys_dfbsd12_stat(struct dfbsd12_stat_args *uap)
85 {
86         struct nlookupdata nd;
87         struct dfbsd12_stat ost;
88         struct stat st;
89         int error;
90
91         get_mplock();
92         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
93         if (error == 0) {
94                 error = kern_stat(&nd, &st);
95                 if (error == 0) {
96                         cvtstat(&ost, &st);
97                         error = copyout(&ost, uap->ub, sizeof(ost));
98                 }
99         }
100         nlookup_done(&nd);
101         rel_mplock();
102         return (error);
103 }
104
105 /*
106  * lstat_args(char *path, struct dfbsd12_stat *ub)
107  *
108  * Get file status; this version does not follow links.
109  *
110  * MPALMOSTSAFE
111  */
112 int
113 sys_dfbsd12_lstat(struct dfbsd12_lstat_args *uap)
114 {
115         struct nlookupdata nd;
116         struct dfbsd12_stat ost;
117         struct stat st;
118         int error;
119
120         get_mplock();
121         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
122         if (error == 0) {
123                 error = kern_stat(&nd, &st);
124                 if (error == 0) {
125                         cvtstat(&ost, &st);
126                         error = copyout(&ost, uap->ub, sizeof(ost));
127                 }
128         }
129         nlookup_done(&nd);
130         rel_mplock();
131         return (error);
132 }
133
134 /*
135  * fhstat_args(struct fhandle *u_fhp, struct dfbsd12_stat *sb)
136  *
137  * MPALMOSTSAFE
138  */
139 int
140 sys_dfbsd12_fhstat(struct dfbsd12_fhstat_args *uap)
141 {
142         struct thread *td = curthread;
143         struct dfbsd12_stat osb;
144         struct stat sb;
145         fhandle_t fh;
146         struct mount *mp;
147         struct vnode *vp;
148         int error;
149
150         /*
151          * Must be super user
152          */
153         error = priv_check(td, PRIV_ROOT);
154         if (error)
155                 return (error);
156         
157         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
158         if (error)
159                 return (error);
160
161         get_mplock();
162         if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
163                 error = ESTALE;
164                 goto done;
165         }
166         if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
167                 goto done;
168         error = vn_stat(vp, &sb, td->td_ucred);
169         vput(vp);
170         if (error)
171                 goto done;
172         cvtstat(&osb, &sb);
173         error = copyout(&osb, uap->sb, sizeof(osb));
174 done:
175         rel_mplock();
176         return (error);
177 }
178
179 /*
180  * dfbsd12_fstat_args(int fd, struct dfbsd12_stat *sb)
181  *
182  * MPALMOSTSAFE
183  */
184 int
185 sys_dfbsd12_fstat(struct dfbsd12_fstat_args *uap)
186 {
187         struct dfbsd12_stat ost;
188         struct stat st;
189         int error;
190
191         get_mplock();
192         error = kern_fstat(uap->fd, &st);
193         rel_mplock();
194
195         if (error == 0) {
196                 cvtstat(&ost, &st);
197                 error = copyout(&ost, uap->sb, sizeof(ost));
198         }
199         return (error);
200 }