Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / sys / vfs / ufs / ufs_vfsops.c
1 /*
2  * Copyright (c) 1991, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)ufs_vfsops.c        8.8 (Berkeley) 5/20/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_vfsops.c,v 1.17.2.3 2001/10/14 19:08:16 iedowse Exp $
40  * $DragonFly: src/sys/vfs/ufs/ufs_vfsops.c,v 1.17 2008/09/17 21:44:25 dillon Exp $
41  */
42
43 #include "opt_quota.h"
44
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/mount.h>
48 #include <sys/proc.h>
49 #include <sys/priv.h>
50 #include <sys/malloc.h>
51 #include <sys/vnode.h>
52
53 #include "quota.h"
54 #include "inode.h"
55 #include "ufsmount.h"
56 #include "ufs_extern.h"
57
58 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
59 /*
60  * Return the root of a filesystem.
61  */
62 int
63 ufs_root(struct mount *mp, struct vnode **vpp)
64 {
65         struct vnode *nvp;
66         int error;
67
68         error = VFS_VGET(mp, (ino_t)ROOTINO, &nvp);
69         if (error)
70                 return (error);
71         *vpp = nvp;
72         return (0);
73 }
74
75 /*
76  * Do operations associated with quotas
77  */
78 int
79 ufs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
80              struct ucred *cred)
81 {
82 #ifndef QUOTA
83         return (EOPNOTSUPP);
84 #else
85         int cmd, type, error;
86
87         type = cmds & SUBCMDMASK;
88         cmd = cmds >> SUBCMDSHIFT;
89
90         if (uid == -1) {
91                 switch(type) {
92                         case USRQUOTA:
93                                 uid = cred->cr_ruid;
94                                 break;
95                         case GRPQUOTA:
96                                 uid = cred->cr_rgid;
97                                 break;
98                         default:
99                                 return (EINVAL);
100                 }
101         }
102                                         
103         switch (cmd) {
104         case Q_SYNC:
105                 break;
106         case Q_GETQUOTA:
107                 if (uid == cred->cr_ruid)
108                         break;
109                 /* fall through */
110         default:
111                 if ((error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT)) != 0)
112                         return (error);
113         }
114
115         type = cmds & SUBCMDMASK;
116         if ((uint)type >= MAXQUOTAS)
117                 return (EINVAL);
118         if (vfs_busy(mp, LK_NOWAIT))
119                 return (0);
120
121         switch (cmd) {
122
123         case Q_QUOTAON:
124                 error = ufs_quotaon(cred, mp, type, arg);
125                 break;
126
127         case Q_QUOTAOFF:
128                 error = ufs_quotaoff(mp, type);
129                 break;
130
131         case Q_SETQUOTA:
132                 error = ufs_setquota(mp, uid, type, arg);
133                 break;
134
135         case Q_SETUSE:
136                 error = ufs_setuse(mp, uid, type, arg);
137                 break;
138
139         case Q_GETQUOTA:
140                 error = ufs_getquota(mp, uid, type, arg);
141                 break;
142
143         case Q_SYNC:
144                 error = ufs_qsync(mp);
145                 break;
146
147         default:
148                 error = EINVAL;
149                 break;
150         }
151         vfs_unbusy(mp);
152         return (error);
153 #endif
154 }
155
156 /*
157  * Initial UFS filesystems, done only once.
158  */
159 int
160 ufs_init(struct vfsconf *vfsp)
161 {
162         static int done;
163
164         if (done)
165                 return (0);
166         done = 1;
167         ufs_ihashinit();
168 #ifdef QUOTA
169         ufs_dqinit();
170 #endif
171         return (0);
172 }
173
174 /*
175  * This is the generic part of fhtovp called after the underlying
176  * filesystem has validated the file handle.
177  *
178  * Call the VFS_CHECKEXP beforehand to verify access.
179  */
180 int
181 ufs_fhtovp(struct mount *mp, struct vnode *rootpv,
182            struct ufid *ufhp, struct vnode **vpp)
183 {
184         struct inode *ip;
185         struct vnode *nvp;
186         int error;
187
188         error = VFS_VGET(mp, ufhp->ufid_ino, &nvp);
189         if (error) {
190                 *vpp = NULLVP;
191                 return (error);
192         }
193         ip = VTOI(nvp);
194         if (ip->i_mode == 0 ||
195             ip->i_gen != ufhp->ufid_gen ||
196             (VFSTOUFS(mp)->um_i_effnlink_valid ? ip->i_effnlink :
197             ip->i_nlink) <= 0) {
198                 vput(nvp);
199                 *vpp = NULLVP;
200                 return (ESTALE);
201         }
202         *vpp = nvp;
203         return (0);
204 }
205
206
207 /*
208  * This is the generic part of fhtovp called after the underlying
209  * filesystem has validated the file handle.
210  *
211  * Verify that a host should have access to a filesystem.
212  */
213 int
214 ufs_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
215                  struct ucred **credanonp)
216 {
217         struct netcred *np;
218         struct ufsmount *ump;
219
220         ump = VFSTOUFS(mp);
221         /*
222          * Get the export permission structure for this <mp, client> tuple.
223          */
224         np = vfs_export_lookup(mp, &ump->um_export, nam);
225         if (np == NULL)
226                 return (EACCES);
227
228         *exflagsp = np->netc_exflags;
229         *credanonp = &np->netc_anon;
230         return (0);
231 }