pseudofs_fileno.c revision 168720
175295Sdes/*-
275295Sdes * 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: head/sys/fs/pseudofs/pseudofs_fileno.c 168720 2007-04-14 14:08:30Z des $");
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_assert(&Giant, MA_OWNED);
56168720Sdes	mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF);
57168720Sdes	pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex);
5875295Sdes}
5975295Sdes
6075295Sdes/*
6175295Sdes * Tear down fileno bitmap
6275295Sdes */
6375295Sdesvoid
6475295Sdespfs_fileno_uninit(struct pfs_info *pi)
6575295Sdes{
6697940Sdes
67168720Sdes	mtx_assert(&Giant, MA_OWNED);
68168720Sdes	delete_unrhdr(pi->pi_unrhdr);
69143841Sphk	pi->pi_unrhdr = NULL;
70168720Sdes	mtx_destroy(&pi->pi_mutex);
7175295Sdes}
7275295Sdes
7375295Sdes/*
7475295Sdes * Allocate a file number
7575295Sdes */
7675295Sdesvoid
77168720Sdespfs_fileno_alloc(struct pfs_node *pn)
7875295Sdes{
79168637Sdes
8075295Sdes	/* make sure our parent has a file number */
8175295Sdes	if (pn->pn_parent && !pn->pn_parent->pn_fileno)
82168720Sdes		pfs_fileno_alloc(pn->pn_parent);
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,
9775295Sdes		    ("pfstype_this node has no parent"));
9875295Sdes		pn->pn_fileno = pn->pn_parent->pn_fileno;
9975295Sdes		break;
10075295Sdes	case pfstype_parent:
10175295Sdes		KASSERT(pn->pn_parent != NULL,
10275295Sdes		    ("pfstype_parent node has no parent"));
103168720Sdes		if (pn->pn_parent == pn->pn_info->pi_root) {
10475295Sdes			pn->pn_fileno = pn->pn_parent->pn_fileno;
10575295Sdes			break;
10675295Sdes		}
10775295Sdes		KASSERT(pn->pn_parent->pn_parent != NULL,
10875295Sdes		    ("pfstype_parent node has no grandparent"));
10975295Sdes		pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
11075295Sdes		break;
11175295Sdes	case pfstype_none:
11277998Sdes		KASSERT(0,
11375295Sdes		    ("pfs_fileno_alloc() called for pfstype_none node"));
11475295Sdes		break;
11597940Sdes	}
11677998Sdes
11777998Sdes#if 0
118168720Sdes	printf("pfs_fileno_alloc(): %s: ", 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
13675295Sdes	switch (pn->pn_type) {
13775295Sdes	case pfstype_root:
138168637Sdes		/* not allocated from unrhdr */
139168637Sdes		return;
14075295Sdes	case pfstype_dir:
14175295Sdes	case pfstype_file:
14275295Sdes	case pfstype_symlink:
14377998Sdes	case pfstype_procdir:
144168720Sdes		free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno);
14575295Sdes		break;
14675295Sdes	case pfstype_this:
14775295Sdes	case pfstype_parent:
14875295Sdes		/* ignore these, as they don't "own" their file number */
14975295Sdes		break;
15075295Sdes	case pfstype_none:
15177998Sdes		KASSERT(0,
15275295Sdes		    ("pfs_fileno_free() called for pfstype_none node"));
15375295Sdes		break;
15475295Sdes	}
15575295Sdes}
156