HAMMER / VFS_VGET - Add optional dvp argument to VFS_VGET(). Fix readdirplus
[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, NULL, (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         /*
104          * Check permissions.
105          */
106         switch (cmd) {
107
108         case Q_QUOTAON:
109                 error = priv_check_cred(cred, PRIV_UFS_QUOTAON, 0);
110                 break;
111
112         case Q_QUOTAOFF:
113                 error = priv_check_cred(cred, PRIV_UFS_QUOTAOFF, 0);
114                 break;
115
116         case Q_SETQUOTA:
117                 error = priv_check_cred(cred, PRIV_VFS_SETQUOTA, 0);
118                 break;
119
120         case Q_SETUSE:
121                 error = priv_check_cred(cred, PRIV_UFS_SETUSE, 0);
122                 break;
123
124         case Q_GETQUOTA:
125                 if (uid == cred->cr_ruid)
126                         error = 0;
127                 else
128                         error = priv_check_cred(cred, PRIV_VFS_GETQUOTA, 0);
129                 break;
130
131         case Q_SYNC:
132                 error = 0;
133                 break;
134
135         default:
136                 error = EINVAL;
137                 break;
138         }
139
140         if (error)
141                 return (error);
142
143
144         if ((uint)type >= MAXQUOTAS)
145                 return (EINVAL);
146         if (vfs_busy(mp, LK_NOWAIT))
147                 return (0);
148
149         switch (cmd) {
150
151         case Q_QUOTAON:
152                 error = ufs_quotaon(cred, mp, type, arg);
153                 break;
154
155         case Q_QUOTAOFF:
156                 error = ufs_quotaoff(mp, type);
157                 break;
158
159         case Q_SETQUOTA:
160                 error = ufs_setquota(mp, uid, type, arg);
161                 break;
162
163         case Q_SETUSE:
164                 error = ufs_setuse(mp, uid, type, arg);
165                 break;
166
167         case Q_GETQUOTA:
168                 error = ufs_getquota(mp, uid, type, arg);
169                 break;
170
171         case Q_SYNC:
172                 error = ufs_qsync(mp);
173                 break;
174
175         default:
176                 error = EINVAL;
177                 break;
178         }
179         vfs_unbusy(mp);
180         return (error);
181 #endif
182 }
183
184 /*
185  * Initial UFS filesystems, done only once.
186  */
187 int
188 ufs_init(struct vfsconf *vfsp)
189 {
190         static int done;
191
192         if (done)
193                 return (0);
194         done = 1;
195         ufs_ihashinit();
196 #ifdef QUOTA
197         ufs_dqinit();
198 #endif
199         return (0);
200 }
201
202 /*
203  * This is the generic part of fhtovp called after the underlying
204  * filesystem has validated the file handle.
205  *
206  * Call the VFS_CHECKEXP beforehand to verify access.
207  */
208 int
209 ufs_fhtovp(struct mount *mp, struct vnode *rootpv,
210            struct ufid *ufhp, struct vnode **vpp)
211 {
212         struct inode *ip;
213         struct vnode *nvp;
214         int error;
215
216         error = VFS_VGET(mp, NULL, ufhp->ufid_ino, &nvp);
217         if (error) {
218                 *vpp = NULLVP;
219                 return (error);
220         }
221         ip = VTOI(nvp);
222         if (ip->i_mode == 0 ||
223             ip->i_gen != ufhp->ufid_gen ||
224             (VFSTOUFS(mp)->um_i_effnlink_valid ? ip->i_effnlink :
225             ip->i_nlink) <= 0) {
226                 vput(nvp);
227                 *vpp = NULLVP;
228                 return (ESTALE);
229         }
230         *vpp = nvp;
231         return (0);
232 }
233
234
235 /*
236  * This is the generic part of fhtovp called after the underlying
237  * filesystem has validated the file handle.
238  *
239  * Verify that a host should have access to a filesystem.
240  */
241 int
242 ufs_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
243                  struct ucred **credanonp)
244 {
245         struct netcred *np;
246         struct ufsmount *ump;
247
248         ump = VFSTOUFS(mp);
249         /*
250          * Get the export permission structure for this <mp, client> tuple.
251          */
252         np = vfs_export_lookup(mp, &ump->um_export, nam);
253         if (np == NULL)
254                 return (EACCES);
255
256         *exflagsp = np->netc_exflags;
257         *credanonp = &np->netc_anon;
258         return (0);
259 }