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