2 * Copyright (c) 2003-2004 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Compress 64-bit file numbers into temporary, unique 32-bit file numbers.
28 * This is needed because the algorithm we use to calculate these numbers
29 * generates 64-bit quantities, but struct dirent's d_fileno member and
30 * struct vnodeattr's va_fileid member only have space for 32 bits.
32 * 32-bit file numbers are generated sequentially, and stored in a
33 * red-black tree, indexed on 64-bit file number. The mappings do not
34 * persist across reboots (or unmounts); anything that relies on this
35 * (e.g. NFS) will not work correctly. This scheme consumes 32 bytes
36 * of kernel memory per file (on i386), and it may be possible for a user
37 * to cause a panic by creating millions of tiny files.
39 * As an optimization, we split the file number space between statically
40 * allocated and dynamically allocated. File numbers less than
41 * FILENO_FIRST_DYN are left unchanged and do not have any tree nodes
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/malloc.h>
55 #include <sys/mutex.h>
57 #include <fs/msdosfs/bpb.h>
58 #include <fs/msdosfs/bootsect.h>
59 #include <fs/msdosfs/msdosfsmount.h>
60 #include <fs/msdosfs/direntry.h>
62 static MALLOC_DEFINE(M_MSDOSFSFILENO, "MSDOSFS fileno", "MSDOSFS fileno mapping node");
64 static struct mtx fileno_mtx;
65 MTX_SYSINIT(fileno, &fileno_mtx, "MSDOSFS fileno", MTX_DEF);
67 RB_PROTOTYPE(msdosfs_filenotree, msdosfs_fileno, mf_tree,
68 msdosfs_fileno_compare)
70 static int msdosfs_fileno_compare(struct msdosfs_fileno *,
71 struct msdosfs_fileno *);
73 #define FILENO_FIRST_DYN 0xf0000000
75 /* Initialize file number mapping structures. */
77 msdosfs_fileno_init(mp)
80 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
82 RB_INIT(&pmp->pm_filenos);
83 pmp->pm_nfileno = FILENO_FIRST_DYN;
84 if (pmp->pm_HugeSectors > 0xffffffff /
85 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1)
86 pmp->pm_flags |= MSDOSFS_LARGEFS;
89 /* Free 32-bit file number generation structures. */
91 msdosfs_fileno_free(mp)
94 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
95 struct msdosfs_fileno *mf, *next;
97 for (mf = RB_MIN(msdosfs_filenotree, &pmp->pm_filenos); mf != NULL;
99 next = RB_NEXT(msdosfs_filenotree, &pmp->pm_filenos, mf);
100 RB_REMOVE(msdosfs_filenotree, &pmp->pm_filenos, mf);
101 free(mf, M_MSDOSFSFILENO);
105 /* Map a 64-bit file number into a 32-bit one. */
107 msdosfs_fileno_map(mp, fileno)
111 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
112 struct msdosfs_fileno key, *mf, *tmf;
115 if ((pmp->pm_flags & MSDOSFS_LARGEFS) == 0) {
116 KASSERT((uint32_t)fileno == fileno,
117 ("fileno >32 bits but not a large fs?"));
118 return ((uint32_t)fileno);
120 if (fileno < FILENO_FIRST_DYN)
121 return ((uint32_t)fileno);
122 mtx_lock(&fileno_mtx);
123 key.mf_fileno64 = fileno;
124 mf = RB_FIND(msdosfs_filenotree, &pmp->pm_filenos, &key);
126 mapped = mf->mf_fileno32;
127 mtx_unlock(&fileno_mtx);
130 if (pmp->pm_nfileno < FILENO_FIRST_DYN)
131 panic("msdosfs_fileno_map: wraparound");
132 mtx_unlock(&fileno_mtx);
133 mf = malloc(sizeof(*mf), M_MSDOSFSFILENO, M_WAITOK);
134 mtx_lock(&fileno_mtx);
135 tmf = RB_FIND(msdosfs_filenotree, &pmp->pm_filenos, &key);
137 mapped = tmf->mf_fileno32;
138 mtx_unlock(&fileno_mtx);
139 free(mf, M_MSDOSFSFILENO);
142 mf->mf_fileno64 = fileno;
143 mapped = mf->mf_fileno32 = pmp->pm_nfileno++;
144 RB_INSERT(msdosfs_filenotree, &pmp->pm_filenos, mf);
145 mtx_unlock(&fileno_mtx);
149 /* Compare by 64-bit file number. */
151 msdosfs_fileno_compare(fa, fb)
152 struct msdosfs_fileno *fa, *fb;
155 if (fa->mf_fileno64 > fb->mf_fileno64)
157 else if (fa->mf_fileno64 < fb->mf_fileno64)
162 RB_GENERATE(msdosfs_filenotree, msdosfs_fileno, mf_tree,
163 msdosfs_fileno_compare)