kernel - kernel - Incidental MPLOCK removal (usched, affinity)
[dragonfly.git] / sys / kern / kern_acl.c
1 /*-
2  * Copyright (c) 1999, 2000 Robert N. M. Watson
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/kern/kern_acl.c,v 1.2.2.1 2000/07/28 18:48:16 rwatson Exp $
27  * $DragonFly: src/sys/kern/kern_acl.c,v 1.17 2007/02/19 00:51:54 swildner Exp $
28  */
29
30 /*
31  * Generic routines to support file system ACLs, at a syntactic level
32  * Semantics are the responsibility of the underlying file system
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysproto.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/vnode.h>
41 #include <sys/lock.h>
42 #include <sys/proc.h>
43 #include <sys/nlookup.h>
44 #include <sys/file.h>
45 #include <sys/sysent.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/acl.h>
49
50 static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
51 static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp);
52 static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp);
53
54 /*
55  * These calls wrap the real vnode operations, and are called by the 
56  * syscall code once the syscall has converted the path or file
57  * descriptor to a vnode (unlocked).  The aclp pointer is assumed
58  * still to point to userland, so this should not be consumed within
59  * the kernel except by syscall code.  Other code should directly
60  * invoke VOP_{SET,GET}ACL.
61  */
62
63 /*
64  * Given a vnode, set its ACL.
65  */
66 static int
67 vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
68 {
69         struct thread *td = curthread;
70         struct acl inkernacl;
71         struct ucred *ucred;
72         int error;
73
74         error = copyin(aclp, &inkernacl, sizeof(struct acl));
75         if (error)
76                 return(error);
77         ucred = td->td_ucred;
78
79         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
80         error = VOP_SETACL(vp, type, &inkernacl, ucred);
81         vn_unlock(vp);
82         return(error);
83 }
84
85 /*
86  * Given a vnode, get its ACL.
87  */
88 static int
89 vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp)
90 {
91         struct thread *td = curthread;
92         struct acl inkernelacl;
93         struct ucred *ucred;
94         int error;
95
96         ucred = td->td_ucred;
97         error = VOP_GETACL(vp, type, &inkernelacl, ucred);
98         if (error == 0)
99                 error = copyout(&inkernelacl, aclp, sizeof(struct acl));
100         return (error);
101 }
102
103 /*
104  * Given a vnode, delete its ACL.
105  */
106 static int
107 vacl_delete(struct vnode *vp, acl_type_t type)
108 {
109         struct thread *td = curthread;
110         struct ucred *ucred;
111         int error;
112
113         ucred = td->td_ucred;
114         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
115         error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred);
116         vn_unlock(vp);
117         return (error);
118 }
119
120 /*
121  * Given a vnode, check whether an ACL is appropriate for it
122  */
123 static int
124 vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp)
125 {
126         struct thread *td = curthread;
127         struct ucred *ucred;
128         struct acl inkernelacl;
129         int error;
130
131         ucred = td->td_ucred;
132         error = copyin(aclp, &inkernelacl, sizeof(struct acl));
133         if (error)
134                 return(error);
135         error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred);
136         return (error);
137 }
138
139 /*
140  * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.
141  * Don't need to lock, as the vacl_ code will get/release any locks
142  * required.
143  */
144
145 /*
146  * Given a file path, get an ACL for it
147  */
148 int
149 sys___acl_get_file(struct __acl_get_file_args *uap)
150 {
151         struct nlookupdata nd;
152         struct vnode *vp;
153         int error;
154
155         vp = NULL;
156         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
157         if (error == 0)
158                 error = nlookup(&nd);
159         if (error == 0)
160                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
161         nlookup_done(&nd);
162         if (error == 0) {
163                 error = vacl_get_acl(vp, uap->type, uap->aclp);
164                 vrele(vp);
165         }
166         return (error);
167 }
168
169 /*
170  * Given a file path, set an ACL for it
171  */
172 int
173 sys___acl_set_file(struct __acl_set_file_args *uap)
174 {
175         struct nlookupdata nd;
176         struct vnode *vp;
177         int error;
178
179         vp = NULL;
180         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
181         if (error == 0)
182                 error = nlookup(&nd);
183         if (error == 0)
184                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
185         nlookup_done(&nd);
186         if (error == 0) {
187                 error = vacl_set_acl(vp, uap->type, uap->aclp);
188                 vrele(vp);
189         }
190         return (error);
191 }
192
193 /*
194  * Given a file descriptor, get an ACL for it
195  */
196 int
197 sys___acl_get_fd(struct __acl_get_fd_args *uap)
198 {
199         struct thread *td = curthread;
200         struct file *fp;
201         int error;
202
203         KKASSERT(td->td_proc);
204         if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
205                 return(error);
206         error = vacl_get_acl((struct vnode *)fp->f_data, uap->type, uap->aclp);
207         fdrop(fp);
208
209         return (error);
210 }
211
212 /*
213  * Given a file descriptor, set an ACL for it
214  */
215 int
216 sys___acl_set_fd(struct __acl_set_fd_args *uap)
217 {
218         struct thread *td = curthread;
219         struct file *fp;
220         int error;
221
222         KKASSERT(td->td_proc);
223         if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
224                 return(error);
225         error = vacl_set_acl((struct vnode *)fp->f_data, uap->type, uap->aclp);
226         fdrop(fp);
227         return (error);
228 }
229
230 /*
231  * Given a file path, delete an ACL from it.
232  */
233 int
234 sys___acl_delete_file(struct __acl_delete_file_args *uap)
235 {
236         struct nlookupdata nd;
237         struct vnode *vp;
238         int error;
239
240         vp = NULL;
241         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
242         if (error == 0)
243                 error = nlookup(&nd);
244         if (error == 0)
245                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
246         nlookup_done(&nd);
247
248         if (error == 0) {
249                 error = vacl_delete(vp, uap->type);
250                 vrele(vp);
251         }
252         return (error);
253 }
254
255 /*
256  * Given a file path, delete an ACL from it.
257  */
258 int
259 sys___acl_delete_fd(struct __acl_delete_fd_args *uap)
260 {
261         struct thread *td = curthread;
262         struct file *fp;
263         int error;
264
265         KKASSERT(td->td_proc);
266         if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
267                 return(error);
268         error = vacl_delete((struct vnode *)fp->f_data, uap->type);
269         fdrop(fp);
270         return (error);
271 }
272
273 /*
274  * Given a file path, check an ACL for it
275  */
276 int
277 sys___acl_aclcheck_file(struct __acl_aclcheck_file_args *uap)
278 {
279         struct nlookupdata nd;
280         struct vnode *vp;
281         int error;
282
283         vp = NULL;
284         error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
285         if (error == 0)
286                 error = nlookup(&nd);
287         if (error == 0)
288                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
289         nlookup_done(&nd);
290
291         if (error == 0) {
292                 error = vacl_aclcheck(vp, uap->type, uap->aclp);
293                 vrele(vp);
294         }
295         return (error);
296 }
297
298 /*
299  * Given a file descriptor, check an ACL for it
300  */
301 int
302 sys___acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap)
303 {
304         struct thread *td = curthread;
305         struct file *fp;
306         int error;
307
308         KKASSERT(td->td_proc);
309         if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0)
310                 return(error);
311         error = vacl_aclcheck((struct vnode *)fp->f_data, uap->type, uap->aclp);
312         fdrop(fp);
313         return (error);
314 }
315