summaryrefslogtreecommitdiffstats
path: root/sys/fs/msdosfs/msdosfs_fileno.c
blob: b56fb43874b54878fcb2ff813e031208517f5927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-
 * Copyright (c) 2003-2004 Tim J. Robbins.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * Compress 64-bit file numbers into temporary, unique 32-bit file numbers.
 * This is needed because the algorithm we use to calculate these numbers
 * generates 64-bit quantities, but struct dirent's d_fileno member and
 * struct vnodeattr's va_fileid member only have space for 32 bits.
 *
 * 32-bit file numbers are generated sequentially, and stored in a
 * red-black tree, indexed on 64-bit file number. The mappings do not
 * persist across reboots (or unmounts); anything that relies on this
 * (e.g. NFS) will not work correctly. This scheme consumes 32 bytes
 * of kernel memory per file (on i386), and it may be possible for a user
 * to cause a panic by creating millions of tiny files.
 *
 * As an optimization, we split the file number space between statically
 * allocated and dynamically allocated. File numbers less than
 * FILENO_FIRST_DYN are left unchanged and do not have any tree nodes
 * allocated to them.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mount.h>

#include <fs/msdosfs/bpb.h>
#include <fs/msdosfs/direntry.h>
#include <fs/msdosfs/msdosfsmount.h>

static MALLOC_DEFINE(M_MSDOSFSFILENO, "msdosfs_fileno", "MSDOSFS fileno mapping node");

RB_PROTOTYPE(msdosfs_filenotree, msdosfs_fileno, mf_tree,
    msdosfs_fileno_compare)

static int msdosfs_fileno_compare(struct msdosfs_fileno *,
    struct msdosfs_fileno *);

#define	FILENO_FIRST_DYN	0xf0000000

/* Initialize file number mapping structures. */
void
msdosfs_fileno_init(mp)
	struct mount *mp;
{
	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);

	RB_INIT(&pmp->pm_filenos);
	pmp->pm_nfileno = FILENO_FIRST_DYN;
        if (pmp->pm_HugeSectors > 0xffffffff /
	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1)
		pmp->pm_flags |= MSDOSFS_LARGEFS;
}

/* Free 32-bit file number generation structures. */
void
msdosfs_fileno_free(mp)
	struct mount *mp;
{
	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
	struct msdosfs_fileno *mf, *next;

	for (mf = RB_MIN(msdosfs_filenotree, &pmp->pm_filenos); mf != NULL;
	    mf = next) {
		next = RB_NEXT(msdosfs_filenotree, &pmp->pm_filenos, mf);
		RB_REMOVE(msdosfs_filenotree, &pmp->pm_filenos, mf);
		free(mf, M_MSDOSFSFILENO);
	}
}

/* Map a 64-bit file number into a 32-bit one. */
uint32_t
msdosfs_fileno_map(mp, fileno)
	struct mount *mp;
	uint64_t fileno;
{
	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
	struct msdosfs_fileno key, *mf, *tmf;
	uint32_t mapped;

	if ((pmp->pm_flags & MSDOSFS_LARGEFS) == 0) {
		KASSERT((uint32_t)fileno == fileno,
		    ("fileno >32 bits but not a large fs?"));
		return ((uint32_t)fileno);
	}
	if (fileno < FILENO_FIRST_DYN)
		return ((uint32_t)fileno);
	MSDOSFS_LOCK_MP(pmp);
	key.mf_fileno64 = fileno;
	mf = RB_FIND(msdosfs_filenotree, &pmp->pm_filenos, &key);
	if (mf != NULL) {
		mapped = mf->mf_fileno32;
		MSDOSFS_UNLOCK_MP(pmp);
		return (mapped);
	}
	if (pmp->pm_nfileno < FILENO_FIRST_DYN)
		panic("msdosfs_fileno_map: wraparound");
	MSDOSFS_UNLOCK_MP(pmp);
	mf = malloc(sizeof(*mf), M_MSDOSFSFILENO, M_WAITOK);
	MSDOSFS_LOCK_MP(pmp);
	tmf = RB_FIND(msdosfs_filenotree, &pmp->pm_filenos, &key);
	if (tmf != NULL) {
		mapped = tmf->mf_fileno32;
		MSDOSFS_UNLOCK_MP(pmp);
		free(mf, M_MSDOSFSFILENO);
		return (mapped);
	}
	mf->mf_fileno64 = fileno;
	mapped = mf->mf_fileno32 = pmp->pm_nfileno++;
	RB_INSERT(msdosfs_filenotree, &pmp->pm_filenos, mf);
	MSDOSFS_UNLOCK_MP(pmp);
	return (mapped);
}

/* Compare by 64-bit file number. */
static int
msdosfs_fileno_compare(fa, fb)
	struct msdosfs_fileno *fa, *fb;
{

	if (fa->mf_fileno64 > fb->mf_fileno64)
		return (1);
	else if (fa->mf_fileno64 < fb->mf_fileno64)
		return (-1);
	return (0);
}

RB_GENERATE(msdosfs_filenotree, msdosfs_fileno, mf_tree,
    msdosfs_fileno_compare)
OpenPOWER on IntegriCloud