]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/volfs/volfs_vfsops.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / miscfs / volfs / volfs_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) 1998 Apple Computer, Inc. All Rights Reserved */
26 /*
27 * Change History:
28 *
29 * 29-May-1998 Pat Dirks Changed to cache pointer to root vnode until unmount.
30 *
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/namei.h>
36 #include <sys/proc.h>
37 #include <sys/kernel.h>
38 #include <mach/machine/vm_types.h>
39 #include <sys/vnode.h>
40 #include <sys/socket.h>
41 #include <sys/mount.h>
42 #include <sys/buf.h>
43 #include <sys/mbuf.h>
44 #include <sys/file.h>
45 #include <dev/disk.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <dev/ldd.h>
50
51 #include <miscfs/specfs/specdev.h>
52 #include "volfs.h"
53
54 struct vfsops volfs_vfsops = {
55 volfs_mount,
56 volfs_start,
57 volfs_unmount,
58 volfs_root,
59 volfs_quotactl,
60 volfs_statfs,
61 volfs_sync,
62 volfs_vget,
63 volfs_fhtovp,
64 volfs_vptofh,
65 volfs_init,
66 volfs_sysctl
67 };
68
69 static char volfs_fs_name[MFSNAMELEN] = "volfs";
70 extern struct vnodeopv_desc volfs_vnodeop_opv_desc;
71
72 /* The following refer to kernel global variables used in the loading/initialization: */
73 extern int maxvfsslots; /* Total number of slots in the system's vfsconf table */
74 extern int maxvfsconf; /* The highest fs type number [old-style ID] in use [dispite its name] */
75 extern int vfs_opv_numops; /* The total number of defined vnode operations */
76 extern int kdp_flag;
77
78 void
79 volfs_load(int loadArgument) {
80 struct vfsconf *vfsconflistentry;
81 int entriesRemaining;
82 struct vfsconf *newvfsconf = NULL;
83 struct vfsconf *lastentry = NULL;
84 int j;
85 int (***opv_desc_vector_p)();
86 int (**opv_desc_vector)();
87 struct vnodeopv_entry_desc *opve_descp;
88
89 #pragma unused(loadArgument)
90
91 /*
92 * This routine is responsible for all the initialization that would
93 * ordinarily be done as part of the system startup; it calls volfs_init
94 * to do the initialization that is strictly volfs-specific.
95 */
96
97 /*
98 prevvfsconf is supposed to be the entry preceding the new entry.
99 To make sure we can always get hooked in SOMEWHERE in the list,
100 start it out at the first entry of the list. This assumes the
101 first entry in the list will be non-empty and not volfs.
102
103 This becomes irrelevant when volfs is compiled into the list.
104 */
105 DBG_VOP(("load_volfs: Scanning vfsconf list...\n"));
106 vfsconflistentry = vfsconf;
107 for (entriesRemaining = maxvfsslots; entriesRemaining > 0; --entriesRemaining) {
108 if (vfsconflistentry->vfc_vfsops != NULL) {
109 /*
110 * Check to see if we're reloading a new version of volfs during debugging
111 * and overwrite the previously assigned entry if we find one:
112 */
113 if (strcmp(vfsconflistentry->vfc_name, volfs_fs_name) == 0) {
114 newvfsconf = vfsconflistentry;
115 break;
116 } else {
117 lastentry = vfsconflistentry;
118 };
119 } else {
120 /*
121 * This is at least a POSSIBLE place to insert the new entry...
122 */
123 newvfsconf = vfsconflistentry;
124 };
125 ++vfsconflistentry;
126 };
127
128 if (newvfsconf) {
129 DBG_VOP(("load_volfs: filling in vfsconf entry at 0x%08lX; lastentry = 0x%08lX.\n", (long)newvfsconf, (long)lastentry));
130 newvfsconf->vfc_vfsops = &volfs_vfsops;
131 strncpy(&newvfsconf->vfc_name[0], "volfs", MFSNAMELEN);
132 newvfsconf->vfc_typenum = maxvfsconf++;
133 newvfsconf->vfc_refcount = 0;
134 newvfsconf->vfc_flags = 0;
135 newvfsconf->vfc_mountroot = NULL; /* Can't mount root of file system [yet] */
136
137 /* Hook into the list: */
138 newvfsconf->vfc_next = NULL;
139 if (lastentry) {
140 newvfsconf->vfc_next = lastentry->vfc_next;
141 lastentry->vfc_next = newvfsconf;
142 };
143
144 /* Based on vfs_op_init and ... */
145 opv_desc_vector_p = volfs_vnodeop_opv_desc.opv_desc_vector_p;
146
147 DBG_VOP(("load_volfs: Allocating and initializing VNode ops vector...\n"));
148
149 /*
150 * Allocate and init the vector.
151 * Also handle backwards compatibility.
152 */
153 MALLOC(*opv_desc_vector_p, PFI *, vfs_opv_numops*sizeof(PFI), M_TEMP, M_WAITOK);
154
155 bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
156
157 opv_desc_vector = *opv_desc_vector_p;
158 for (j=0; volfs_vnodeop_opv_desc.opv_desc_ops[j].opve_op; j++) {
159 opve_descp = &(volfs_vnodeop_opv_desc.opv_desc_ops[j]);
160
161 /*
162 * Sanity check: is this operation listed
163 * in the list of operations? We check this
164 * by seeing if its offest is zero. Since
165 * the default routine should always be listed
166 * first, it should be the only one with a zero
167 * offset. Any other operation with a zero
168 * offset is probably not listed in
169 * vfs_op_descs, and so is probably an error.
170 *
171 * A panic here means the layer programmer
172 * has committed the all-too common bug
173 * of adding a new operation to the layer's
174 * list of vnode operations but
175 * not adding the operation to the system-wide
176 * list of supported operations.
177 */
178 if (opve_descp->opve_op->vdesc_offset == 0 &&
179 opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
180 DBG_VOP(("load_volfs: operation %s not listed in %s.\n",
181 opve_descp->opve_op->vdesc_name,
182 "vfs_op_descs"));
183 panic ("load_volfs: bad operation");
184 }
185 /*
186 * Fill in this entry.
187 */
188 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
189 opve_descp->opve_impl;
190 }
191
192 /*
193 * Finally, go back and replace unfilled routines
194 * with their default. (Sigh, an O(n^3) algorithm. I
195 * could make it better, but that'd be work, and n is small.)
196 */
197 opv_desc_vector_p = volfs_vnodeop_opv_desc.opv_desc_vector_p;
198
199 /*
200 * Force every operations vector to have a default routine.
201 */
202 opv_desc_vector = *opv_desc_vector_p;
203 if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
204 panic("load_vp;fs: operation vector without default routine.");
205 }
206 for (j = 0;j<vfs_opv_numops; j++)
207 if (opv_desc_vector[j] == NULL)
208 opv_desc_vector[j] =
209 opv_desc_vector[VOFFSET(vop_default)];
210
211 DBG_VOP(("load_volfs: calling volfs_init()...\n"));
212 volfs_init(newvfsconf);
213 };
214 }
215
216 /*
217 * VFS Operations.
218 *
219 * mount system call
220 */
221 int
222 volfs_mount(mp, path, data, ndp, p)
223 register struct mount *mp;
224 char *path;
225 caddr_t data;
226 struct nameidata *ndp;
227 struct proc *p;
228 {
229 struct volfs_mntdata *priv_mnt_data;
230 struct vnode *root_vp;
231 struct volfs_vndata *priv_vn_data;
232 int error;
233 size_t size;
234
235 DBG_VOP(("volfs_mount called\n"));
236 MALLOC(priv_mnt_data, struct volfs_mntdata *, sizeof(struct volfs_mntdata),
237 M_VOLFSMNT, M_WAITOK);
238 DBG_VOP(("MALLOC succeeded\n"));
239 LIST_INIT(&priv_mnt_data->volfs_fsvnodes);
240 DBG_VOP(("LIST_INIT succeeded\n"));
241
242 mp->mnt_data = (void *)priv_mnt_data;
243 strcpy(mp->mnt_stat.f_fstypename, "volfs");
244 (void) copyinstr(path, mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname) - 1, &size);
245 strcpy(mp->mnt_stat.f_mntfromname, "<volfs>");
246
247 /* Set up the root vnode for fast reference in the future.
248 Note that the root is maintained unlocked but with a pos. ref count until unmount. */
249
250 MALLOC(priv_vn_data, struct volfs_vndata *, sizeof(struct volfs_vndata), M_VOLFSNODE, M_WAITOK);
251 error = getnewvnode(VT_VOLFS, mp, volfs_vnodeop_p, &root_vp);
252 if (error != 0)
253 {
254 FREE(priv_mnt_data, M_VOLFSMNT);
255 FREE(priv_vn_data, M_VOLFSNODE);
256 DBG_VOP(("getnewvnode failed with error code %d\n", error));
257 return(error);
258 }
259 root_vp->v_type = VDIR;
260 root_vp->v_flag |= VROOT;
261 lockinit(&priv_vn_data->lock, PINOD, "volfsnode", 0, 0);
262 priv_vn_data->vnode_type = VOLFS_ROOT;
263 priv_vn_data->nodeID = 0;
264 priv_vn_data->fs_mount = mp;
265 root_vp->v_data = priv_vn_data;
266
267 priv_mnt_data->volfs_rootvp = root_vp;
268
269 return (0);
270 }
271
272 int
273 volfs_start(mp, flags, p)
274 struct mount * mp;
275 int flags;
276 struct proc * p;
277 {
278 DBG_VOP(("volfs_start called\n"));
279 return (0);
280 }
281
282 /*
283 * Return the root of a filesystem. For volfs the root vnode is a directory
284 * containing the list of all filesystems volfs can work with.
285 */
286 int
287 volfs_root(mp, vpp)
288 struct mount *mp;
289 struct vnode **vpp;
290 {
291 struct volfs_mntdata *priv_data;
292 // struct volfs_vndata *priv_vn_data;
293 // int error;
294
295 DBG_VOP(("volfs_root called\n"));
296 priv_data = (struct volfs_mntdata *)mp->mnt_data;
297
298 if (priv_data->volfs_rootvp) {
299 vref(priv_data->volfs_rootvp);
300 VOP_LOCK(priv_data->volfs_rootvp, LK_EXCLUSIVE, current_proc());
301 *vpp = priv_data->volfs_rootvp;
302 } else {
303 panic("volfs: root vnode missing!");
304 };
305
306 DBG_VOP(("volfs_root returned with "));
307 DBG_VOP_PRINT_VNODE_INFO(*vpp);DBG_VOP(("\n"));
308
309 return(0);
310 }
311
312 int
313 volfs_quotactl(mp, cmds, uid, arg, p)
314 struct mount *mp;
315 int cmds;
316 uid_t uid;
317 caddr_t arg;
318 struct proc * p;
319 {
320 DBG_VOP(("volfs_quotactl called\n"));
321 return (0);
322 }
323
324 /*
325 * unmount system call
326 */
327 int
328 volfs_unmount(mp, mntflags, p)
329 struct mount *mp;
330 int mntflags;
331 struct proc *p;
332 {
333 struct volfs_mntdata *priv_data;
334 struct vnode *root_vp;
335 int retval;
336
337 DBG_VOP(("volfs_unmount called\n"));
338 priv_data = (struct volfs_mntdata *)mp->mnt_data;
339
340 root_vp = priv_data->volfs_rootvp;
341 retval = vflush(mp, root_vp, 0);
342 if (retval) goto Err_Exit;
343
344 /* Free the root vnode.
345 Note that there's no need to vget() or vref() it before locking it here:
346 the ref. count has been maintained at +1 ever since mount time. */
347 if (root_vp) {
348 retval = vn_lock(root_vp, LK_EXCLUSIVE, p);
349 if (retval) goto Err_Exit;
350 if (root_vp->v_usecount > 1) {
351 DBG_VOP(("VOLFS ERROR: root vnode = %x, usecount = %d\n", (int)root_vp, priv_data->volfs_rootvp->v_usecount));
352 VOP_UNLOCK(root_vp, 0, p);
353 retval = EBUSY;
354 goto Err_Exit;
355 };
356
357 priv_data->volfs_rootvp = NULL;
358 vput(root_vp); /* This drops volfs's own refcount */
359 vgone(root_vp);
360 };
361
362 /* All vnodes should be gone, and no errors, clean up the last */
363 /* XXX DBG_ASSERT(mp->mnt_vnodelist.lh_first == NULL); */
364 /* XXX DBG_ASSERT(retval == 0); */
365
366 mp->mnt_data = NULL;
367 FREE(priv_data, M_VOLFSMNT);
368
369 Err_Exit:
370
371 return(retval);
372 }
373
374 /*
375 * Get file system statistics.
376 */
377 int
378 volfs_statfs(mp, sbp, p)
379 struct mount *mp;
380 register struct statfs *sbp;
381 struct proc *p;
382 {
383 DBG_VOP(("volfs_statfs called\n"));
384 sbp->f_bsize = 512;
385 sbp->f_iosize = 512;
386 sbp->f_blocks = 1024; // lies, darn lies and virtual file systems
387 sbp->f_bfree = 0; // Nope, can't write here!
388 sbp->f_bavail = 0;
389 sbp->f_files = 0; // Hmmm...maybe later
390 sbp->f_ffree = 0;
391 return (0);
392 }
393
394 /*
395 * volfs doesn't have any data and you can't write into any of the volfs
396 * structures, so don't do anything
397 */
398 int
399 volfs_sync(mp, waitfor, cred, p)
400 struct mount *mp;
401 int waitfor;
402 struct ucred *cred;
403 struct proc *p;
404 {
405 // DBG_VOP(("volfs_sync called\n"));
406 return 0;
407 }
408 /*
409 * Look up a FFS dinode number to find its incore vnode, otherwise read it
410 * in from disk. If it is in core, wait for the lock bit to clear, then
411 * return the inode locked. Detection and handling of mount points must be
412 * done by the calling routine.
413 */
414 int
415 volfs_vget(mp, ino, vpp)
416 struct mount *mp;
417 void *ino;
418 struct vnode **vpp;
419 {
420 // DBG_VOP(("volfs_vget called\n"));
421 return(0);
422 }
423 /*
424 * File handle to vnode
425 *
426 * Have to be really careful about stale file handles:
427 * - check that the inode number is valid
428 * - call ffs_vget() to get the locked inode
429 * - check for an unallocated inode (i_mode == 0)
430 * - check that the given client host has export rights and return
431 * those rights via. exflagsp and credanonp
432 */
433 int
434 volfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
435 register struct mount *mp;
436 struct fid *fhp;
437 struct mbuf *nam;
438 struct vnode **vpp;
439 int *exflagsp;
440 struct ucred **credanonp;
441 {
442 DBG_VOP(("volfs_fhtovp called\n"));
443 return(0);
444 }
445 /*
446 * Vnode pointer to File handle
447 */
448 /* ARGSUSED */
449 int
450 volfs_vptofh(vp, fhp)
451 struct vnode *vp;
452 struct fid *fhp;
453 {
454 DBG_VOP(("volfs_vptofh called\n"));
455 return(0);
456 }
457 /*
458 * Initialize the filesystem
459 */
460 int
461 volfs_init(vfsp)
462 struct vfsconf *vfsp;
463 {
464 DBG_VOP(("volfs_init called\n"));
465 return (0);
466 }
467
468 /*
469 * fast filesystem related variables.
470 */
471 int
472 volfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
473 int *name;
474 u_int namelen;
475 void *oldp;
476 size_t *oldlenp;
477 void *newp;
478 size_t newlen;
479 struct proc *p;
480 {
481 DBG_VOP(("volfs_sysctl called\n"));
482 return (EOPNOTSUPP);
483 }
484