175295Sdes/*-
2230132Suqs * Copyright (c) 2001 Dag-Erling Co��dan Sm��rgrav
375295Sdes * All rights reserved.
475295Sdes *
575295Sdes * Redistribution and use in source and binary forms, with or without
675295Sdes * modification, are permitted provided that the following conditions
775295Sdes * are met:
875295Sdes * 1. Redistributions of source code must retain the above copyright
975295Sdes *    notice, this list of conditions and the following disclaimer
1075295Sdes *    in this position and unchanged.
1175295Sdes * 2. Redistributions in binary form must reproduce the above copyright
1275295Sdes *    notice, this list of conditions and the following disclaimer in the
1375295Sdes *    documentation and/or other materials provided with the distribution.
1475295Sdes * 3. The name of the author may not be used to endorse or promote products
1575295Sdes *    derived from this software without specific prior written permission.
1675295Sdes *
1775295Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1875295Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1975295Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2075295Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2175295Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2275295Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2375295Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2475295Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2575295Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2675295Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2775295Sdes */
2875295Sdes
29143592Sdes#include <sys/cdefs.h>
30143592Sdes__FBSDID("$FreeBSD$");
31143592Sdes
32143592Sdes#include "opt_pseudofs.h"
33143592Sdes
3475295Sdes#include <sys/param.h>
3575295Sdes#include <sys/kernel.h>
3675295Sdes#include <sys/systm.h>
37114216Skan#include <sys/limits.h>
3878073Sdes#include <sys/lock.h>
3975295Sdes#include <sys/malloc.h>
4077965Sdes#include <sys/mutex.h>
41168720Sdes#include <sys/proc.h>
4275295Sdes#include <sys/sysctl.h>
43168637Sdes#include <sys/systm.h>
4475295Sdes
4575295Sdes#include <fs/pseudofs/pseudofs.h>
4675295Sdes#include <fs/pseudofs/pseudofs_internal.h>
4775295Sdes
4875295Sdes/*
4975295Sdes * Initialize fileno bitmap
5075295Sdes */
5175295Sdesvoid
5275295Sdespfs_fileno_init(struct pfs_info *pi)
5375295Sdes{
5497940Sdes
55168720Sdes	mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF);
56168720Sdes	pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex);
5775295Sdes}
5875295Sdes
5975295Sdes/*
6075295Sdes * Tear down fileno bitmap
6175295Sdes */
6275295Sdesvoid
6375295Sdespfs_fileno_uninit(struct pfs_info *pi)
6475295Sdes{
6597940Sdes
66168720Sdes	delete_unrhdr(pi->pi_unrhdr);
67143841Sphk	pi->pi_unrhdr = NULL;
68168720Sdes	mtx_destroy(&pi->pi_mutex);
6975295Sdes}
7075295Sdes
7175295Sdes/*
7275295Sdes * Allocate a file number
7375295Sdes */
7475295Sdesvoid
75168720Sdespfs_fileno_alloc(struct pfs_node *pn)
7675295Sdes{
77168637Sdes
78168764Sdes	if (pn->pn_parent)
79168764Sdes		PFS_TRACE(("%s/%s", pn->pn_parent->pn_name, pn->pn_name));
80168764Sdes	else
81168764Sdes		PFS_TRACE(("%s", pn->pn_name));
82168764Sdes	pfs_assert_not_owned(pn);
8397940Sdes
8475295Sdes	switch (pn->pn_type) {
8575295Sdes	case pfstype_root:
86168637Sdes		/* root must always be 2 */
87168637Sdes		pn->pn_fileno = 2;
88168637Sdes		break;
8975295Sdes	case pfstype_dir:
9075295Sdes	case pfstype_file:
9175295Sdes	case pfstype_symlink:
9277998Sdes	case pfstype_procdir:
93168720Sdes		pn->pn_fileno = alloc_unr(pn->pn_info->pi_unrhdr);
9475295Sdes		break;
9575295Sdes	case pfstype_this:
9675295Sdes		KASSERT(pn->pn_parent != NULL,
97168764Sdes		    ("%s(): pfstype_this node has no parent", __func__));
9875295Sdes		pn->pn_fileno = pn->pn_parent->pn_fileno;
9975295Sdes		break;
10075295Sdes	case pfstype_parent:
10175295Sdes		KASSERT(pn->pn_parent != NULL,
102168764Sdes		    ("%s(): pfstype_parent node has no parent", __func__));
103168764Sdes		if (pn->pn_parent->pn_type == pfstype_root) {
10475295Sdes			pn->pn_fileno = pn->pn_parent->pn_fileno;
10575295Sdes			break;
10675295Sdes		}
10775295Sdes		KASSERT(pn->pn_parent->pn_parent != NULL,
108168764Sdes		    ("%s(): pfstype_parent node has no grandparent", __func__));
10975295Sdes		pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
11075295Sdes		break;
11175295Sdes	case pfstype_none:
11277998Sdes		KASSERT(0,
113168764Sdes		    ("%s(): pfstype_none node", __func__));
11475295Sdes		break;
11597940Sdes	}
11677998Sdes
11777998Sdes#if 0
118168764Sdes	printf("%s(): %s: ", __func__, pn->pn_info->pi_name);
11975295Sdes	if (pn->pn_parent) {
12075295Sdes		if (pn->pn_parent->pn_parent) {
12175295Sdes			printf("%s/", pn->pn_parent->pn_parent->pn_name);
12275295Sdes		}
12375295Sdes		printf("%s/", pn->pn_parent->pn_name);
12475295Sdes	}
12575295Sdes	printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);
12677998Sdes#endif
12775295Sdes}
12875295Sdes
12975295Sdes/*
13075295Sdes * Release a file number
13175295Sdes */
13275295Sdesvoid
133168720Sdespfs_fileno_free(struct pfs_node *pn)
13475295Sdes{
135168637Sdes
136168764Sdes	pfs_assert_not_owned(pn);
137168764Sdes
13875295Sdes	switch (pn->pn_type) {
13975295Sdes	case pfstype_root:
140168637Sdes		/* not allocated from unrhdr */
141168637Sdes		return;
14275295Sdes	case pfstype_dir:
14375295Sdes	case pfstype_file:
14475295Sdes	case pfstype_symlink:
14577998Sdes	case pfstype_procdir:
146168720Sdes		free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno);
14775295Sdes		break;
14875295Sdes	case pfstype_this:
14975295Sdes	case pfstype_parent:
15075295Sdes		/* ignore these, as they don't "own" their file number */
15175295Sdes		break;
15275295Sdes	case pfstype_none:
15377998Sdes		KASSERT(0,
15475295Sdes		    ("pfs_fileno_free() called for pfstype_none node"));
15575295Sdes		break;
15675295Sdes	}
15775295Sdes}
158