xref: /NextBSD/sys/fs/msdosfs/msdosfs_fileno.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*-
2  * Copyright (c) 2003-2004 Tim J. Robbins.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 /*
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.
31  *
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.
38  *
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
42  * allocated to them.
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 
55 #include <fs/msdosfs/bpb.h>
56 #include <fs/msdosfs/direntry.h>
57 #include <fs/msdosfs/msdosfsmount.h>
58 
59 static MALLOC_DEFINE(M_MSDOSFSFILENO, "msdosfs_fileno", "MSDOSFS fileno mapping node");
60 
61 RB_PROTOTYPE(msdosfs_filenotree, msdosfs_fileno, mf_tree,
62     msdosfs_fileno_compare)
63 
64 static int msdosfs_fileno_compare(struct msdosfs_fileno *,
65     struct msdosfs_fileno *);
66 
67 #define	FILENO_FIRST_DYN	0xf0000000
68 
69 /* Initialize file number mapping structures. */
70 void
msdosfs_fileno_init(struct mount * mp)71 msdosfs_fileno_init(struct mount *mp)
72 {
73 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
74 
75 	RB_INIT(&pmp->pm_filenos);
76 	pmp->pm_nfileno = FILENO_FIRST_DYN;
77 	if (pmp->pm_HugeSectors > 0xffffffff /
78 	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1)
79 		pmp->pm_flags |= MSDOSFS_LARGEFS;
80 }
81 
82 /* Free 32-bit file number generation structures. */
83 void
msdosfs_fileno_free(struct mount * mp)84 msdosfs_fileno_free(struct mount *mp)
85 {
86 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
87 	struct msdosfs_fileno *mf, *next;
88 
89 	for (mf = RB_MIN(msdosfs_filenotree, &pmp->pm_filenos); mf != NULL;
90 	    mf = next) {
91 		next = RB_NEXT(msdosfs_filenotree, &pmp->pm_filenos, mf);
92 		RB_REMOVE(msdosfs_filenotree, &pmp->pm_filenos, mf);
93 		free(mf, M_MSDOSFSFILENO);
94 	}
95 }
96 
97 /* Map a 64-bit file number into a 32-bit one. */
98 uint32_t
msdosfs_fileno_map(struct mount * mp,uint64_t fileno)99 msdosfs_fileno_map(struct mount *mp, uint64_t fileno)
100 {
101 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
102 	struct msdosfs_fileno key, *mf, *tmf;
103 	uint32_t mapped;
104 
105 	if ((pmp->pm_flags & MSDOSFS_LARGEFS) == 0) {
106 		KASSERT((uint32_t)fileno == fileno,
107 		    ("fileno >32 bits but not a large fs?"));
108 		return ((uint32_t)fileno);
109 	}
110 	if (fileno < FILENO_FIRST_DYN)
111 		return ((uint32_t)fileno);
112 	MSDOSFS_LOCK_MP(pmp);
113 	key.mf_fileno64 = fileno;
114 	mf = RB_FIND(msdosfs_filenotree, &pmp->pm_filenos, &key);
115 	if (mf != NULL) {
116 		mapped = mf->mf_fileno32;
117 		MSDOSFS_UNLOCK_MP(pmp);
118 		return (mapped);
119 	}
120 	if (pmp->pm_nfileno < FILENO_FIRST_DYN)
121 		panic("msdosfs_fileno_map: wraparound");
122 	MSDOSFS_UNLOCK_MP(pmp);
123 	mf = malloc(sizeof(*mf), M_MSDOSFSFILENO, M_WAITOK);
124 	MSDOSFS_LOCK_MP(pmp);
125 	tmf = RB_FIND(msdosfs_filenotree, &pmp->pm_filenos, &key);
126 	if (tmf != NULL) {
127 		mapped = tmf->mf_fileno32;
128 		MSDOSFS_UNLOCK_MP(pmp);
129 		free(mf, M_MSDOSFSFILENO);
130 		return (mapped);
131 	}
132 	mf->mf_fileno64 = fileno;
133 	mapped = mf->mf_fileno32 = pmp->pm_nfileno++;
134 	RB_INSERT(msdosfs_filenotree, &pmp->pm_filenos, mf);
135 	MSDOSFS_UNLOCK_MP(pmp);
136 	return (mapped);
137 }
138 
139 /* Compare by 64-bit file number. */
140 static int
msdosfs_fileno_compare(struct msdosfs_fileno * fa,struct msdosfs_fileno * fb)141 msdosfs_fileno_compare(struct msdosfs_fileno *fa, struct msdosfs_fileno *fb)
142 {
143 
144 	if (fa->mf_fileno64 > fb->mf_fileno64)
145 		return (1);
146 	else if (fa->mf_fileno64 < fb->mf_fileno64)
147 		return (-1);
148 	return (0);
149 }
150 
151 RB_GENERATE(msdosfs_filenotree, msdosfs_fileno, mf_tree,
152     msdosfs_fileno_compare)
153