xref: /freebsd/sys/fs/pseudofs/pseudofs_fileno.c (revision e738085b94631f90e21a49852538ac95974baf44)
19733a808SDag-Erling Smørgrav /*-
2d63027b6SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3d63027b6SPedro F. Giffuni  *
4*e738085bSDag-Erling Smørgrav  * Copyright (c) 2001 Dag-Erling Smørgrav
59733a808SDag-Erling Smørgrav  * All rights reserved.
69733a808SDag-Erling Smørgrav  *
79733a808SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
89733a808SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
99733a808SDag-Erling Smørgrav  * are met:
109733a808SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
119733a808SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer
129733a808SDag-Erling Smørgrav  *    in this position and unchanged.
139733a808SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
149733a808SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
159733a808SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
169733a808SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote products
179733a808SDag-Erling Smørgrav  *    derived from this software without specific prior written permission.
189733a808SDag-Erling Smørgrav  *
199733a808SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
209733a808SDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
219733a808SDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
229733a808SDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
239733a808SDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
249733a808SDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
259733a808SDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
269733a808SDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279733a808SDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
289733a808SDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299733a808SDag-Erling Smørgrav  */
309733a808SDag-Erling Smørgrav 
31de52d21aSDag-Erling Smørgrav #include <sys/cdefs.h>
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>
41f61bc4eaSDag-Erling Smørgrav #include <sys/proc.h>
429733a808SDag-Erling Smørgrav #include <sys/sysctl.h>
4315bad11fSDag-Erling Smørgrav #include <sys/systm.h>
449733a808SDag-Erling Smørgrav 
459733a808SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
469733a808SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs_internal.h>
479733a808SDag-Erling Smørgrav 
489733a808SDag-Erling Smørgrav /*
499733a808SDag-Erling Smørgrav  * Initialize fileno bitmap
509733a808SDag-Erling Smørgrav  */
519733a808SDag-Erling Smørgrav void
pfs_fileno_init(struct pfs_info * pi)529733a808SDag-Erling Smørgrav pfs_fileno_init(struct pfs_info *pi)
539733a808SDag-Erling Smørgrav {
549733a808SDag-Erling Smørgrav 
55f61bc4eaSDag-Erling Smørgrav 	mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF);
56f61bc4eaSDag-Erling Smørgrav 	pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex);
579733a808SDag-Erling Smørgrav }
589733a808SDag-Erling Smørgrav 
599733a808SDag-Erling Smørgrav /*
609733a808SDag-Erling Smørgrav  * Tear down fileno bitmap
619733a808SDag-Erling Smørgrav  */
629733a808SDag-Erling Smørgrav void
pfs_fileno_uninit(struct pfs_info * pi)639733a808SDag-Erling Smørgrav pfs_fileno_uninit(struct pfs_info *pi)
649733a808SDag-Erling Smørgrav {
659733a808SDag-Erling Smørgrav 
66f61bc4eaSDag-Erling Smørgrav 	delete_unrhdr(pi->pi_unrhdr);
677f661c6bSPoul-Henning Kamp 	pi->pi_unrhdr = NULL;
68f61bc4eaSDag-Erling Smørgrav 	mtx_destroy(&pi->pi_mutex);
699733a808SDag-Erling Smørgrav }
709733a808SDag-Erling Smørgrav 
719733a808SDag-Erling Smørgrav /*
729733a808SDag-Erling Smørgrav  * Allocate a file number
739733a808SDag-Erling Smørgrav  */
749733a808SDag-Erling Smørgrav void
pfs_fileno_alloc(struct pfs_node * pn)75f61bc4eaSDag-Erling Smørgrav pfs_fileno_alloc(struct pfs_node *pn)
769733a808SDag-Erling Smørgrav {
7715bad11fSDag-Erling Smørgrav 
78388596dfSDag-Erling Smørgrav 	if (pn->pn_parent)
79388596dfSDag-Erling Smørgrav 		PFS_TRACE(("%s/%s", pn->pn_parent->pn_name, pn->pn_name));
80388596dfSDag-Erling Smørgrav 	else
81388596dfSDag-Erling Smørgrav 		PFS_TRACE(("%s", pn->pn_name));
82388596dfSDag-Erling Smørgrav 	pfs_assert_not_owned(pn);
839733a808SDag-Erling Smørgrav 
849733a808SDag-Erling Smørgrav 	switch (pn->pn_type) {
859733a808SDag-Erling Smørgrav 	case pfstype_root:
8615bad11fSDag-Erling Smørgrav 		/* root must always be 2 */
8715bad11fSDag-Erling Smørgrav 		pn->pn_fileno = 2;
8815bad11fSDag-Erling Smørgrav 		break;
899733a808SDag-Erling Smørgrav 	case pfstype_dir:
909733a808SDag-Erling Smørgrav 	case pfstype_file:
919733a808SDag-Erling Smørgrav 	case pfstype_symlink:
92649ad985SDag-Erling Smørgrav 	case pfstype_procdir:
93f61bc4eaSDag-Erling Smørgrav 		pn->pn_fileno = alloc_unr(pn->pn_info->pi_unrhdr);
949733a808SDag-Erling Smørgrav 		break;
959733a808SDag-Erling Smørgrav 	case pfstype_this:
969733a808SDag-Erling Smørgrav 		KASSERT(pn->pn_parent != NULL,
97388596dfSDag-Erling Smørgrav 		    ("%s(): pfstype_this node has no parent", __func__));
989733a808SDag-Erling Smørgrav 		pn->pn_fileno = pn->pn_parent->pn_fileno;
999733a808SDag-Erling Smørgrav 		break;
1009733a808SDag-Erling Smørgrav 	case pfstype_parent:
1019733a808SDag-Erling Smørgrav 		KASSERT(pn->pn_parent != NULL,
102388596dfSDag-Erling Smørgrav 		    ("%s(): pfstype_parent node has no parent", __func__));
103388596dfSDag-Erling Smørgrav 		if (pn->pn_parent->pn_type == pfstype_root) {
1049733a808SDag-Erling Smørgrav 			pn->pn_fileno = pn->pn_parent->pn_fileno;
1059733a808SDag-Erling Smørgrav 			break;
1069733a808SDag-Erling Smørgrav 		}
1079733a808SDag-Erling Smørgrav 		KASSERT(pn->pn_parent->pn_parent != NULL,
108388596dfSDag-Erling Smørgrav 		    ("%s(): pfstype_parent node has no grandparent", __func__));
1099733a808SDag-Erling Smørgrav 		pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
1109733a808SDag-Erling Smørgrav 		break;
1119733a808SDag-Erling Smørgrav 	case pfstype_none:
112649ad985SDag-Erling Smørgrav 		KASSERT(0,
113388596dfSDag-Erling Smørgrav 		    ("%s(): pfstype_none node", __func__));
1149733a808SDag-Erling Smørgrav 		break;
1159733a808SDag-Erling Smørgrav 	}
1169733a808SDag-Erling Smørgrav 
117649ad985SDag-Erling Smørgrav #if 0
118388596dfSDag-Erling Smørgrav 	printf("%s(): %s: ", __func__, pn->pn_info->pi_name);
1199733a808SDag-Erling Smørgrav 	if (pn->pn_parent) {
1209733a808SDag-Erling Smørgrav 		if (pn->pn_parent->pn_parent) {
1219733a808SDag-Erling Smørgrav 			printf("%s/", pn->pn_parent->pn_parent->pn_name);
1229733a808SDag-Erling Smørgrav 		}
1239733a808SDag-Erling Smørgrav 		printf("%s/", pn->pn_parent->pn_name);
1249733a808SDag-Erling Smørgrav 	}
1259733a808SDag-Erling Smørgrav 	printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);
126649ad985SDag-Erling Smørgrav #endif
1279733a808SDag-Erling Smørgrav }
1289733a808SDag-Erling Smørgrav 
1299733a808SDag-Erling Smørgrav /*
1309733a808SDag-Erling Smørgrav  * Release a file number
1319733a808SDag-Erling Smørgrav  */
1329733a808SDag-Erling Smørgrav void
pfs_fileno_free(struct pfs_node * pn)133f61bc4eaSDag-Erling Smørgrav pfs_fileno_free(struct pfs_node *pn)
1349733a808SDag-Erling Smørgrav {
13515bad11fSDag-Erling Smørgrav 
136388596dfSDag-Erling Smørgrav 	pfs_assert_not_owned(pn);
137388596dfSDag-Erling Smørgrav 
1389733a808SDag-Erling Smørgrav 	switch (pn->pn_type) {
1399733a808SDag-Erling Smørgrav 	case pfstype_root:
14015bad11fSDag-Erling Smørgrav 		/* not allocated from unrhdr */
14115bad11fSDag-Erling Smørgrav 		return;
1429733a808SDag-Erling Smørgrav 	case pfstype_dir:
1439733a808SDag-Erling Smørgrav 	case pfstype_file:
1449733a808SDag-Erling Smørgrav 	case pfstype_symlink:
145649ad985SDag-Erling Smørgrav 	case pfstype_procdir:
146f61bc4eaSDag-Erling Smørgrav 		free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno);
1479733a808SDag-Erling Smørgrav 		break;
1489733a808SDag-Erling Smørgrav 	case pfstype_this:
1499733a808SDag-Erling Smørgrav 	case pfstype_parent:
1509733a808SDag-Erling Smørgrav 		/* ignore these, as they don't "own" their file number */
1519733a808SDag-Erling Smørgrav 		break;
1529733a808SDag-Erling Smørgrav 	case pfstype_none:
153649ad985SDag-Erling Smørgrav 		KASSERT(0,
1549733a808SDag-Erling Smørgrav 		    ("pfs_fileno_free() called for pfstype_none node"));
1559733a808SDag-Erling Smørgrav 		break;
1569733a808SDag-Erling Smørgrav 	}
1579733a808SDag-Erling Smørgrav }
158