xref: /freebsd/sys/fs/pseudofs/pseudofs_fileno.c (revision 7f661c6ba1d1ecf588ec0406de9344b5f4320655)
19733a808SDag-Erling Smørgrav /*-
29733a808SDag-Erling Smørgrav  * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav
39733a808SDag-Erling Smørgrav  * All rights reserved.
49733a808SDag-Erling Smørgrav  *
59733a808SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
69733a808SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
79733a808SDag-Erling Smørgrav  * are met:
89733a808SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
99733a808SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer
109733a808SDag-Erling Smørgrav  *    in this position and unchanged.
119733a808SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
129733a808SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
139733a808SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
149733a808SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote products
159733a808SDag-Erling Smørgrav  *    derived from this software without specific prior written permission.
169733a808SDag-Erling Smørgrav  *
179733a808SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189733a808SDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199733a808SDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209733a808SDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219733a808SDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229733a808SDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239733a808SDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249733a808SDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259733a808SDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269733a808SDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279733a808SDag-Erling Smørgrav  */
289733a808SDag-Erling Smørgrav 
29de52d21aSDag-Erling Smørgrav #include <sys/cdefs.h>
30de52d21aSDag-Erling Smørgrav __FBSDID("$FreeBSD$");
31de52d21aSDag-Erling Smørgrav 
32de52d21aSDag-Erling Smørgrav #include "opt_pseudofs.h"
33de52d21aSDag-Erling Smørgrav 
349733a808SDag-Erling Smørgrav #include <sys/param.h>
359733a808SDag-Erling Smørgrav #include <sys/kernel.h>
369733a808SDag-Erling Smørgrav #include <sys/systm.h>
37104a9b7eSAlexander Kabaev #include <sys/limits.h>
3821ceb6efSDag-Erling Smørgrav #include <sys/lock.h>
399733a808SDag-Erling Smørgrav #include <sys/malloc.h>
4049fa664fSDag-Erling Smørgrav #include <sys/mutex.h>
419733a808SDag-Erling Smørgrav #include <sys/sysctl.h>
429733a808SDag-Erling Smørgrav 
439733a808SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
449733a808SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs_internal.h>
459733a808SDag-Erling Smørgrav 
469733a808SDag-Erling Smørgrav /*
479733a808SDag-Erling Smørgrav  * Initialize fileno bitmap
489733a808SDag-Erling Smørgrav  */
499733a808SDag-Erling Smørgrav void
509733a808SDag-Erling Smørgrav pfs_fileno_init(struct pfs_info *pi)
519733a808SDag-Erling Smørgrav {
527f661c6bSPoul-Henning Kamp 	struct unrhdr *up;
539733a808SDag-Erling Smørgrav 
547f661c6bSPoul-Henning Kamp 	up = new_unrhdr(3, INT_MAX, &pi->pi_mutex);
559733a808SDag-Erling Smørgrav 	mtx_lock(&pi->pi_mutex);
567f661c6bSPoul-Henning Kamp 	pi->pi_unrhdr = up;
579733a808SDag-Erling Smørgrav 	pi->pi_root->pn_fileno = 2;
589733a808SDag-Erling Smørgrav 	mtx_unlock(&pi->pi_mutex);
599733a808SDag-Erling Smørgrav }
609733a808SDag-Erling Smørgrav 
619733a808SDag-Erling Smørgrav /*
629733a808SDag-Erling Smørgrav  * Tear down fileno bitmap
639733a808SDag-Erling Smørgrav  */
649733a808SDag-Erling Smørgrav void
659733a808SDag-Erling Smørgrav pfs_fileno_uninit(struct pfs_info *pi)
669733a808SDag-Erling Smørgrav {
677f661c6bSPoul-Henning Kamp 	struct unrhdr *up;
689733a808SDag-Erling Smørgrav 
699733a808SDag-Erling Smørgrav 	mtx_lock(&pi->pi_mutex);
709733a808SDag-Erling Smørgrav 
717f661c6bSPoul-Henning Kamp 	up = pi->pi_unrhdr;
727f661c6bSPoul-Henning Kamp 	pi->pi_unrhdr = NULL;
739733a808SDag-Erling Smørgrav 
749733a808SDag-Erling Smørgrav 	mtx_unlock(&pi->pi_mutex);
759733a808SDag-Erling Smørgrav 
767f661c6bSPoul-Henning Kamp 	delete_unrhdr(up);
779733a808SDag-Erling Smørgrav }
789733a808SDag-Erling Smørgrav 
799733a808SDag-Erling Smørgrav /*
809733a808SDag-Erling Smørgrav  * Allocate a file number
819733a808SDag-Erling Smørgrav  */
829733a808SDag-Erling Smørgrav void
839733a808SDag-Erling Smørgrav pfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn)
849733a808SDag-Erling Smørgrav {
859733a808SDag-Erling Smørgrav 	/* make sure our parent has a file number */
869733a808SDag-Erling Smørgrav 	if (pn->pn_parent && !pn->pn_parent->pn_fileno)
879733a808SDag-Erling Smørgrav 		pfs_fileno_alloc(pi, pn->pn_parent);
889733a808SDag-Erling Smørgrav 
899733a808SDag-Erling Smørgrav 	switch (pn->pn_type) {
909733a808SDag-Erling Smørgrav 	case pfstype_root:
919733a808SDag-Erling Smørgrav 	case pfstype_dir:
929733a808SDag-Erling Smørgrav 	case pfstype_file:
939733a808SDag-Erling Smørgrav 	case pfstype_symlink:
94649ad985SDag-Erling Smørgrav 	case pfstype_procdir:
957f661c6bSPoul-Henning Kamp 		pn->pn_fileno = alloc_unr(pi->pi_unrhdr);
969733a808SDag-Erling Smørgrav 		break;
979733a808SDag-Erling Smørgrav 	case pfstype_this:
989733a808SDag-Erling Smørgrav 		KASSERT(pn->pn_parent != NULL,
999733a808SDag-Erling Smørgrav 		    ("pfstype_this node has no parent"));
1009733a808SDag-Erling Smørgrav 		pn->pn_fileno = pn->pn_parent->pn_fileno;
1019733a808SDag-Erling Smørgrav 		break;
1029733a808SDag-Erling Smørgrav 	case pfstype_parent:
1039733a808SDag-Erling Smørgrav 		KASSERT(pn->pn_parent != NULL,
1049733a808SDag-Erling Smørgrav 		    ("pfstype_parent node has no parent"));
1059733a808SDag-Erling Smørgrav 		if (pn->pn_parent == pi->pi_root) {
1069733a808SDag-Erling Smørgrav 			pn->pn_fileno = pn->pn_parent->pn_fileno;
1079733a808SDag-Erling Smørgrav 			break;
1089733a808SDag-Erling Smørgrav 		}
1099733a808SDag-Erling Smørgrav 		KASSERT(pn->pn_parent->pn_parent != NULL,
1109733a808SDag-Erling Smørgrav 		    ("pfstype_parent node has no grandparent"));
1119733a808SDag-Erling Smørgrav 		pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
1129733a808SDag-Erling Smørgrav 		break;
1139733a808SDag-Erling Smørgrav 	case pfstype_none:
114649ad985SDag-Erling Smørgrav 		KASSERT(0,
1159733a808SDag-Erling Smørgrav 		    ("pfs_fileno_alloc() called for pfstype_none node"));
1169733a808SDag-Erling Smørgrav 		break;
1179733a808SDag-Erling Smørgrav 	}
1189733a808SDag-Erling Smørgrav 
119649ad985SDag-Erling Smørgrav #if 0
1209733a808SDag-Erling Smørgrav 	printf("pfs_fileno_alloc(): %s: ", pi->pi_name);
1219733a808SDag-Erling Smørgrav 	if (pn->pn_parent) {
1229733a808SDag-Erling Smørgrav 		if (pn->pn_parent->pn_parent) {
1239733a808SDag-Erling Smørgrav 			printf("%s/", pn->pn_parent->pn_parent->pn_name);
1249733a808SDag-Erling Smørgrav 		}
1259733a808SDag-Erling Smørgrav 		printf("%s/", pn->pn_parent->pn_name);
1269733a808SDag-Erling Smørgrav 	}
1279733a808SDag-Erling Smørgrav 	printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);
128649ad985SDag-Erling Smørgrav #endif
1299733a808SDag-Erling Smørgrav }
1309733a808SDag-Erling Smørgrav 
1319733a808SDag-Erling Smørgrav /*
1329733a808SDag-Erling Smørgrav  * Release a file number
1339733a808SDag-Erling Smørgrav  */
1349733a808SDag-Erling Smørgrav void
1359733a808SDag-Erling Smørgrav pfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn)
1369733a808SDag-Erling Smørgrav {
1379733a808SDag-Erling Smørgrav 	switch (pn->pn_type) {
1389733a808SDag-Erling Smørgrav 	case pfstype_root:
1399733a808SDag-Erling Smørgrav 	case pfstype_dir:
1409733a808SDag-Erling Smørgrav 	case pfstype_file:
1419733a808SDag-Erling Smørgrav 	case pfstype_symlink:
142649ad985SDag-Erling Smørgrav 	case pfstype_procdir:
1437f661c6bSPoul-Henning Kamp 		free_unr(pi->pi_unrhdr, pn->pn_fileno);
1449733a808SDag-Erling Smørgrav 		break;
1459733a808SDag-Erling Smørgrav 	case pfstype_this:
1469733a808SDag-Erling Smørgrav 	case pfstype_parent:
1479733a808SDag-Erling Smørgrav 		/* ignore these, as they don't "own" their file number */
1489733a808SDag-Erling Smørgrav 		break;
1499733a808SDag-Erling Smørgrav 	case pfstype_none:
150649ad985SDag-Erling Smørgrav 		KASSERT(0,
1519733a808SDag-Erling Smørgrav 		    ("pfs_fileno_free() called for pfstype_none node"));
1529733a808SDag-Erling Smørgrav 		break;
1539733a808SDag-Erling Smørgrav 	}
1549733a808SDag-Erling Smørgrav }
155