]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/nullfs/null_vfsops.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / miscfs / nullfs / null_vfsops.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * This code is derived from software donated to Berkeley by
31 * Jan-Simon Pendry.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)null_vfsops.c 8.7 (Berkeley) 5/14/95
62 *
63 * @(#)lofs_vfsops.c 1.2 (Berkeley) 6/18/92
64 */
65
66 /*
67 * Null Layer
68 * (See null_vnops.c for a description of what this does.)
69 */
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/proc.h>
74 #include <sys/time.h>
75 #include <sys/types.h>
76 #include <sys/vnode.h>
77 #include <sys/mount.h>
78 #include <sys/namei.h>
79 #include <sys/malloc.h>
80 #include <miscfs/nullfs/null.h>
81
82 /*
83 * Mount null layer
84 */
85 int
86 nullfs_mount(mp, path, data, ndp, p)
87 struct mount *mp;
88 char *path;
89 caddr_t data;
90 struct nameidata *ndp;
91 struct proc *p;
92 {
93 int error = 0;
94 struct null_args args;
95 struct vnode *lowerrootvp, *vp;
96 struct vnode *nullm_rootvp;
97 struct null_mount *xmp;
98 u_int size;
99
100 #ifdef NULLFS_DIAGNOSTIC
101 printf("nullfs_mount(mp = %x)\n", mp);
102 #endif
103
104 /*
105 * Update is a no-op
106 */
107 if (mp->mnt_flag & MNT_UPDATE) {
108 return (EOPNOTSUPP);
109 /* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
110 }
111
112 /*
113 * Get argument
114 */
115 if (error = copyin(data, (caddr_t)&args, sizeof(struct null_args)))
116 return (error);
117
118 /*
119 * Find lower node
120 */
121 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
122 UIO_USERSPACE, args.target, p);
123 if (error = namei(ndp))
124 return (error);
125
126 /*
127 * Sanity check on lower vnode
128 */
129 lowerrootvp = ndp->ni_vp;
130
131 vrele(ndp->ni_dvp);
132 ndp->ni_dvp = NULL;
133
134 xmp = (struct null_mount *) _MALLOC(sizeof(struct null_mount),
135 M_UFSMNT, M_WAITOK); /* XXX */
136
137 /*
138 * Save reference to underlying FS
139 */
140 xmp->nullm_vfs = lowerrootvp->v_mount;
141
142 /*
143 * Save reference. Each mount also holds
144 * a reference on the root vnode.
145 */
146 error = null_node_create(mp, lowerrootvp, &vp);
147 /*
148 * Unlock the node (either the lower or the alias)
149 */
150 VOP_UNLOCK(vp, 0, p);
151 /*
152 * Make sure the node alias worked
153 */
154 if (error) {
155 vrele(lowerrootvp);
156 FREE(xmp, M_UFSMNT); /* XXX */
157 return (error);
158 }
159
160 /*
161 * Keep a held reference to the root vnode.
162 * It is vrele'd in nullfs_unmount.
163 */
164 nullm_rootvp = vp;
165 nullm_rootvp->v_flag |= VROOT;
166 xmp->nullm_rootvp = nullm_rootvp;
167 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
168 mp->mnt_flag |= MNT_LOCAL;
169 mp->mnt_data = (qaddr_t) xmp;
170 vfs_getnewfsid(mp);
171
172 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
173 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
174 (void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
175 &size);
176 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
177 #ifdef NULLFS_DIAGNOSTIC
178 printf("nullfs_mount: lower %s, alias at %s\n",
179 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
180 #endif
181 return (0);
182 }
183
184 /*
185 * VFS start. Nothing needed here - the start routine
186 * on the underlying filesystem will have been called
187 * when that filesystem was mounted.
188 */
189 int
190 nullfs_start(mp, flags, p)
191 struct mount *mp;
192 int flags;
193 struct proc *p;
194 {
195 return (0);
196 /* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
197 }
198
199 /*
200 * Free reference to null layer
201 */
202 int
203 nullfs_unmount(mp, mntflags, p)
204 struct mount *mp;
205 int mntflags;
206 struct proc *p;
207 {
208 struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
209 int error;
210 int flags = 0;
211 int force = 0;
212
213 #ifdef NULLFS_DIAGNOSTIC
214 printf("nullfs_unmount(mp = %x)\n", mp);
215 #endif
216
217 if (mntflags & MNT_FORCE) {
218 flags |= FORCECLOSE;
219 force = 1;
220 }
221
222 if ( (nullm_rootvp->v_usecount > 1) && !force )
223 return (EBUSY);
224 if ( (error = vflush(mp, nullm_rootvp, flags)) && !force )
225 return (error);
226
227 #ifdef NULLFS_DIAGNOSTIC
228 vprint("alias root of lower", nullm_rootvp);
229 #endif
230 /*
231 * Release reference on underlying root vnode
232 */
233 vrele(nullm_rootvp);
234 /*
235 * And blow it away for future re-use
236 */
237 vgone(nullm_rootvp);
238 /*
239 * Finally, throw away the null_mount structure
240 */
241 FREE(mp->mnt_data, M_UFSMNT); /* XXX */
242 mp->mnt_data = 0;
243 return 0;
244 }
245
246 int
247 nullfs_root(mp, vpp)
248 struct mount *mp;
249 struct vnode **vpp;
250 {
251 struct proc *p = curproc; /* XXX */
252 struct vnode *vp;
253
254 #ifdef NULLFS_DIAGNOSTIC
255 printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
256 MOUNTTONULLMOUNT(mp)->nullm_rootvp,
257 NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
258 );
259 #endif
260
261 /*
262 * Return locked reference to root.
263 */
264 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
265 VREF(vp);
266 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
267 *vpp = vp;
268 return 0;
269 }
270
271 int
272 nullfs_quotactl(mp, cmd, uid, arg, p)
273 struct mount *mp;
274 int cmd;
275 uid_t uid;
276 caddr_t arg;
277 struct proc *p;
278 {
279 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
280 }
281
282 int
283 nullfs_statfs(mp, sbp, p)
284 struct mount *mp;
285 struct statfs *sbp;
286 struct proc *p;
287 {
288 int error;
289 struct statfs mstat;
290
291 #ifdef NULLFS_DIAGNOSTIC
292 printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
293 MOUNTTONULLMOUNT(mp)->nullm_rootvp,
294 NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
295 );
296 #endif
297
298 bzero(&mstat, sizeof(mstat));
299
300 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
301 if (error)
302 return (error);
303
304 /* now copy across the "interesting" information and fake the rest */
305 sbp->f_type = mstat.f_type;
306 sbp->f_flags = mstat.f_flags;
307 sbp->f_bsize = mstat.f_bsize;
308 sbp->f_iosize = mstat.f_iosize;
309 sbp->f_blocks = mstat.f_blocks;
310 sbp->f_bfree = mstat.f_bfree;
311 sbp->f_bavail = mstat.f_bavail;
312 sbp->f_files = mstat.f_files;
313 sbp->f_ffree = mstat.f_ffree;
314 if (sbp != &mp->mnt_stat) {
315 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
316 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
317 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
318 }
319 return (0);
320 }
321
322 int
323 nullfs_sync(mp, waitfor, cred, p)
324 struct mount *mp;
325 int waitfor;
326 struct ucred *cred;
327 struct proc *p;
328 {
329 /*
330 * XXX - Assumes no data cached at null layer.
331 */
332 return (0);
333 }
334
335 int
336 nullfs_vget(mp, ino, vpp)
337 struct mount *mp;
338 ino_t ino;
339 struct vnode **vpp;
340 {
341
342 return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
343 }
344
345 int
346 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
347 struct mount *mp;
348 struct fid *fidp;
349 struct mbuf *nam;
350 struct vnode **vpp;
351 int *exflagsp;
352 struct ucred**credanonp;
353 {
354
355 return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
356 }
357
358 int
359 nullfs_vptofh(vp, fhp)
360 struct vnode *vp;
361 struct fid *fhp;
362 {
363 return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
364 }
365
366 int nullfs_init __P((struct vfsconf *));
367
368 #define nullfs_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
369 size_t, struct proc *)))eopnotsupp)
370
371 struct vfsops null_vfsops = {
372 nullfs_mount,
373 nullfs_start,
374 nullfs_unmount,
375 nullfs_root,
376 nullfs_quotactl,
377 nullfs_statfs,
378 nullfs_sync,
379 nullfs_vget,
380 nullfs_fhtovp,
381 nullfs_vptofh,
382 nullfs_init,
383 nullfs_sysctl,
384 };