msdosfs_fat.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: msdosfs_fat.c,v 1.1.2.2 2002/12/29 19:56:02 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 1.1.2.2 thorpej * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 1.1.2.2 thorpej * All rights reserved.
7 1.1.2.2 thorpej * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 1.1.2.2 thorpej *
9 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
11 1.1.2.2 thorpej * are met:
12 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
17 1.1.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1.2.2 thorpej * must display the following acknowledgement:
19 1.1.2.2 thorpej * This product includes software developed by TooLs GmbH.
20 1.1.2.2 thorpej * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.1.2.2 thorpej * derived from this software without specific prior written permission.
22 1.1.2.2 thorpej *
23 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1.2.2 thorpej * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.1.2.2 thorpej * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1.2.2 thorpej * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.1.2.2 thorpej * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1.2.2 thorpej * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.1.2.2 thorpej * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.1.2.2 thorpej * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1.2.2 thorpej */
34 1.1.2.2 thorpej /*
35 1.1.2.2 thorpej * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 1.1.2.2 thorpej *
37 1.1.2.2 thorpej * You can do anything you want with this software, just don't say you wrote
38 1.1.2.2 thorpej * it, and don't remove this notice.
39 1.1.2.2 thorpej *
40 1.1.2.2 thorpej * This software is provided "as is".
41 1.1.2.2 thorpej *
42 1.1.2.2 thorpej * The author supplies this software to be publicly redistributed on the
43 1.1.2.2 thorpej * understanding that the author is not responsible for the correct
44 1.1.2.2 thorpej * functioning of this software in any circumstances and is not liable for
45 1.1.2.2 thorpej * any damages caused by this software.
46 1.1.2.2 thorpej *
47 1.1.2.2 thorpej * October 1992
48 1.1.2.2 thorpej */
49 1.1.2.2 thorpej
50 1.1.2.2 thorpej #include <sys/cdefs.h>
51 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: msdosfs_fat.c,v 1.1.2.2 2002/12/29 19:56:02 thorpej Exp $");
52 1.1.2.2 thorpej
53 1.1.2.2 thorpej /*
54 1.1.2.2 thorpej * kernel include files.
55 1.1.2.2 thorpej */
56 1.1.2.2 thorpej #include <sys/param.h>
57 1.1.2.2 thorpej #include <sys/systm.h>
58 1.1.2.2 thorpej #include <sys/buf.h>
59 1.1.2.2 thorpej #include <sys/file.h>
60 1.1.2.2 thorpej #include <sys/namei.h>
61 1.1.2.2 thorpej #include <sys/mount.h> /* to define statfs structure */
62 1.1.2.2 thorpej #include <sys/vnode.h> /* to define vattr structure */
63 1.1.2.2 thorpej #include <sys/errno.h>
64 1.1.2.2 thorpej #include <sys/dirent.h>
65 1.1.2.2 thorpej
66 1.1.2.2 thorpej /*
67 1.1.2.2 thorpej * msdosfs include files.
68 1.1.2.2 thorpej */
69 1.1.2.2 thorpej #include <fs/msdosfs/bpb.h>
70 1.1.2.2 thorpej #include <fs/msdosfs/msdosfsmount.h>
71 1.1.2.2 thorpej #include <fs/msdosfs/direntry.h>
72 1.1.2.2 thorpej #include <fs/msdosfs/denode.h>
73 1.1.2.2 thorpej #include <fs/msdosfs/fat.h>
74 1.1.2.2 thorpej
75 1.1.2.2 thorpej /*
76 1.1.2.2 thorpej * Fat cache stats.
77 1.1.2.2 thorpej */
78 1.1.2.2 thorpej int fc_fileextends; /* # of file extends */
79 1.1.2.2 thorpej int fc_lfcempty; /* # of time last file cluster cache entry
80 1.1.2.2 thorpej * was empty */
81 1.1.2.2 thorpej int fc_bmapcalls; /* # of times pcbmap was called */
82 1.1.2.2 thorpej
83 1.1.2.2 thorpej #define LMMAX 20
84 1.1.2.2 thorpej int fc_lmdistance[LMMAX]; /* counters for how far off the last
85 1.1.2.2 thorpej * cluster mapped entry was. */
86 1.1.2.2 thorpej int fc_largedistance; /* off by more than LMMAX */
87 1.1.2.2 thorpej
88 1.1.2.2 thorpej static void fatblock __P((struct msdosfsmount *, u_long, u_long *, u_long *,
89 1.1.2.2 thorpej u_long *));
90 1.1.2.2 thorpej void updatefats __P((struct msdosfsmount *, struct buf *, u_long));
91 1.1.2.2 thorpej static __inline void usemap_free __P((struct msdosfsmount *, u_long));
92 1.1.2.2 thorpej static __inline void usemap_alloc __P((struct msdosfsmount *, u_long));
93 1.1.2.2 thorpej static int fatchain __P((struct msdosfsmount *, u_long, u_long, u_long));
94 1.1.2.2 thorpej int chainlength __P((struct msdosfsmount *, u_long, u_long));
95 1.1.2.2 thorpej int chainalloc __P((struct msdosfsmount *, u_long, u_long, u_long, u_long *,
96 1.1.2.2 thorpej u_long *));
97 1.1.2.2 thorpej
98 1.1.2.2 thorpej static void
99 1.1.2.2 thorpej fatblock(pmp, ofs, bnp, sizep, bop)
100 1.1.2.2 thorpej struct msdosfsmount *pmp;
101 1.1.2.2 thorpej u_long ofs;
102 1.1.2.2 thorpej u_long *bnp;
103 1.1.2.2 thorpej u_long *sizep;
104 1.1.2.2 thorpej u_long *bop;
105 1.1.2.2 thorpej {
106 1.1.2.2 thorpej u_long bn, size;
107 1.1.2.2 thorpej
108 1.1.2.2 thorpej bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
109 1.1.2.2 thorpej size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
110 1.1.2.2 thorpej * pmp->pm_BytesPerSec;
111 1.1.2.2 thorpej bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
112 1.1.2.2 thorpej
113 1.1.2.2 thorpej if (bnp)
114 1.1.2.2 thorpej *bnp = bn;
115 1.1.2.2 thorpej if (sizep)
116 1.1.2.2 thorpej *sizep = size;
117 1.1.2.2 thorpej if (bop)
118 1.1.2.2 thorpej *bop = ofs % pmp->pm_fatblocksize;
119 1.1.2.2 thorpej }
120 1.1.2.2 thorpej
121 1.1.2.2 thorpej /*
122 1.1.2.2 thorpej * Map the logical cluster number of a file into a physical disk sector
123 1.1.2.2 thorpej * that is filesystem relative.
124 1.1.2.2 thorpej *
125 1.1.2.2 thorpej * dep - address of denode representing the file of interest
126 1.1.2.2 thorpej * findcn - file relative cluster whose filesystem relative cluster number
127 1.1.2.2 thorpej * and/or block number are/is to be found
128 1.1.2.2 thorpej * bnp - address of where to place the file system relative block number.
129 1.1.2.2 thorpej * If this pointer is null then don't return this quantity.
130 1.1.2.2 thorpej * cnp - address of where to place the file system relative cluster number.
131 1.1.2.2 thorpej * If this pointer is null then don't return this quantity.
132 1.1.2.2 thorpej *
133 1.1.2.2 thorpej * NOTE: Either bnp or cnp must be non-null.
134 1.1.2.2 thorpej * This function has one side effect. If the requested file relative cluster
135 1.1.2.2 thorpej * is beyond the end of file, then the actual number of clusters in the file
136 1.1.2.2 thorpej * is returned in *cnp. This is useful for determining how long a directory is.
137 1.1.2.2 thorpej * If cnp is null, nothing is returned.
138 1.1.2.2 thorpej */
139 1.1.2.2 thorpej int
140 1.1.2.2 thorpej pcbmap(dep, findcn, bnp, cnp, sp)
141 1.1.2.2 thorpej struct denode *dep;
142 1.1.2.2 thorpej u_long findcn; /* file relative cluster to get */
143 1.1.2.2 thorpej daddr_t *bnp; /* returned filesys relative blk number */
144 1.1.2.2 thorpej u_long *cnp; /* returned cluster number */
145 1.1.2.2 thorpej int *sp; /* returned block size */
146 1.1.2.2 thorpej {
147 1.1.2.2 thorpej int error;
148 1.1.2.2 thorpej u_long i;
149 1.1.2.2 thorpej u_long cn;
150 1.1.2.2 thorpej u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
151 1.1.2.2 thorpej u_long byteoffset;
152 1.1.2.2 thorpej u_long bn;
153 1.1.2.2 thorpej u_long bo;
154 1.1.2.2 thorpej struct buf *bp = NULL;
155 1.1.2.2 thorpej u_long bp_bn = -1;
156 1.1.2.2 thorpej struct msdosfsmount *pmp = dep->de_pmp;
157 1.1.2.2 thorpej u_long bsize;
158 1.1.2.2 thorpej
159 1.1.2.2 thorpej fc_bmapcalls++;
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej /*
162 1.1.2.2 thorpej * If they don't give us someplace to return a value then don't
163 1.1.2.2 thorpej * bother doing anything.
164 1.1.2.2 thorpej */
165 1.1.2.2 thorpej if (bnp == NULL && cnp == NULL && sp == NULL)
166 1.1.2.2 thorpej return (0);
167 1.1.2.2 thorpej
168 1.1.2.2 thorpej cn = dep->de_StartCluster;
169 1.1.2.2 thorpej /*
170 1.1.2.2 thorpej * The "file" that makes up the root directory is contiguous,
171 1.1.2.2 thorpej * permanently allocated, of fixed size, and is not made up of
172 1.1.2.2 thorpej * clusters. If the cluster number is beyond the end of the root
173 1.1.2.2 thorpej * directory, then return the number of clusters in the file.
174 1.1.2.2 thorpej */
175 1.1.2.2 thorpej if (cn == MSDOSFSROOT) {
176 1.1.2.2 thorpej if (dep->de_Attributes & ATTR_DIRECTORY) {
177 1.1.2.2 thorpej if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
178 1.1.2.2 thorpej if (cnp)
179 1.1.2.2 thorpej *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
180 1.1.2.2 thorpej return (E2BIG);
181 1.1.2.2 thorpej }
182 1.1.2.2 thorpej if (bnp)
183 1.1.2.2 thorpej *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
184 1.1.2.2 thorpej if (cnp)
185 1.1.2.2 thorpej *cnp = MSDOSFSROOT;
186 1.1.2.2 thorpej if (sp)
187 1.1.2.2 thorpej *sp = min(pmp->pm_bpcluster,
188 1.1.2.2 thorpej dep->de_FileSize - de_cn2off(pmp, findcn));
189 1.1.2.2 thorpej return (0);
190 1.1.2.2 thorpej } else { /* just an empty file */
191 1.1.2.2 thorpej if (cnp)
192 1.1.2.2 thorpej *cnp = 0;
193 1.1.2.2 thorpej return (E2BIG);
194 1.1.2.2 thorpej }
195 1.1.2.2 thorpej }
196 1.1.2.2 thorpej
197 1.1.2.2 thorpej /*
198 1.1.2.2 thorpej * All other files do I/O in cluster sized blocks
199 1.1.2.2 thorpej */
200 1.1.2.2 thorpej if (sp)
201 1.1.2.2 thorpej *sp = pmp->pm_bpcluster;
202 1.1.2.2 thorpej
203 1.1.2.2 thorpej /*
204 1.1.2.2 thorpej * Rummage around in the fat cache, maybe we can avoid tromping
205 1.1.2.2 thorpej * thru every fat entry for the file. And, keep track of how far
206 1.1.2.2 thorpej * off the cache was from where we wanted to be.
207 1.1.2.2 thorpej */
208 1.1.2.2 thorpej i = 0;
209 1.1.2.2 thorpej fc_lookup(dep, findcn, &i, &cn);
210 1.1.2.2 thorpej if ((bn = findcn - i) >= LMMAX)
211 1.1.2.2 thorpej fc_largedistance++;
212 1.1.2.2 thorpej else
213 1.1.2.2 thorpej fc_lmdistance[bn]++;
214 1.1.2.2 thorpej
215 1.1.2.2 thorpej /*
216 1.1.2.2 thorpej * Handle all other files or directories the normal way.
217 1.1.2.2 thorpej */
218 1.1.2.2 thorpej for (; i < findcn; i++) {
219 1.1.2.2 thorpej /*
220 1.1.2.2 thorpej * Stop with all reserved clusters, not just with EOF.
221 1.1.2.2 thorpej */
222 1.1.2.2 thorpej if (cn >= (CLUST_RSRVD & pmp->pm_fatmask))
223 1.1.2.2 thorpej goto hiteof;
224 1.1.2.2 thorpej byteoffset = FATOFS(pmp, cn);
225 1.1.2.2 thorpej fatblock(pmp, byteoffset, &bn, &bsize, &bo);
226 1.1.2.2 thorpej if (bn != bp_bn) {
227 1.1.2.2 thorpej if (bp)
228 1.1.2.2 thorpej brelse(bp);
229 1.1.2.2 thorpej error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
230 1.1.2.2 thorpej if (error) {
231 1.1.2.2 thorpej brelse(bp);
232 1.1.2.2 thorpej return (error);
233 1.1.2.2 thorpej }
234 1.1.2.2 thorpej bp_bn = bn;
235 1.1.2.2 thorpej }
236 1.1.2.2 thorpej prevcn = cn;
237 1.1.2.2 thorpej if (FAT32(pmp))
238 1.1.2.2 thorpej cn = getulong(&bp->b_data[bo]);
239 1.1.2.2 thorpej else
240 1.1.2.2 thorpej cn = getushort(&bp->b_data[bo]);
241 1.1.2.2 thorpej if (FAT12(pmp) && (prevcn & 1))
242 1.1.2.2 thorpej cn >>= 4;
243 1.1.2.2 thorpej cn &= pmp->pm_fatmask;
244 1.1.2.2 thorpej }
245 1.1.2.2 thorpej
246 1.1.2.2 thorpej if (!MSDOSFSEOF(cn, pmp->pm_fatmask)) {
247 1.1.2.2 thorpej if (bp)
248 1.1.2.2 thorpej brelse(bp);
249 1.1.2.2 thorpej if (bnp)
250 1.1.2.2 thorpej *bnp = cntobn(pmp, cn);
251 1.1.2.2 thorpej if (cnp)
252 1.1.2.2 thorpej *cnp = cn;
253 1.1.2.2 thorpej fc_setcache(dep, FC_LASTMAP, i, cn);
254 1.1.2.2 thorpej return (0);
255 1.1.2.2 thorpej }
256 1.1.2.2 thorpej
257 1.1.2.2 thorpej hiteof:;
258 1.1.2.2 thorpej if (cnp)
259 1.1.2.2 thorpej *cnp = i;
260 1.1.2.2 thorpej if (bp)
261 1.1.2.2 thorpej brelse(bp);
262 1.1.2.2 thorpej /* update last file cluster entry in the fat cache */
263 1.1.2.2 thorpej fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
264 1.1.2.2 thorpej return (E2BIG);
265 1.1.2.2 thorpej }
266 1.1.2.2 thorpej
267 1.1.2.2 thorpej /*
268 1.1.2.2 thorpej * Find the closest entry in the fat cache to the cluster we are looking
269 1.1.2.2 thorpej * for.
270 1.1.2.2 thorpej */
271 1.1.2.2 thorpej void
272 1.1.2.2 thorpej fc_lookup(dep, findcn, frcnp, fsrcnp)
273 1.1.2.2 thorpej struct denode *dep;
274 1.1.2.2 thorpej u_long findcn;
275 1.1.2.2 thorpej u_long *frcnp;
276 1.1.2.2 thorpej u_long *fsrcnp;
277 1.1.2.2 thorpej {
278 1.1.2.2 thorpej int i;
279 1.1.2.2 thorpej u_long cn;
280 1.1.2.2 thorpej struct fatcache *closest = 0;
281 1.1.2.2 thorpej
282 1.1.2.2 thorpej for (i = 0; i < FC_SIZE; i++) {
283 1.1.2.2 thorpej cn = dep->de_fc[i].fc_frcn;
284 1.1.2.2 thorpej if (cn != FCE_EMPTY && cn <= findcn) {
285 1.1.2.2 thorpej if (closest == 0 || cn > closest->fc_frcn)
286 1.1.2.2 thorpej closest = &dep->de_fc[i];
287 1.1.2.2 thorpej }
288 1.1.2.2 thorpej }
289 1.1.2.2 thorpej if (closest) {
290 1.1.2.2 thorpej *frcnp = closest->fc_frcn;
291 1.1.2.2 thorpej *fsrcnp = closest->fc_fsrcn;
292 1.1.2.2 thorpej }
293 1.1.2.2 thorpej }
294 1.1.2.2 thorpej
295 1.1.2.2 thorpej /*
296 1.1.2.2 thorpej * Purge the fat cache in denode dep of all entries relating to file
297 1.1.2.2 thorpej * relative cluster frcn and beyond.
298 1.1.2.2 thorpej */
299 1.1.2.2 thorpej void
300 1.1.2.2 thorpej fc_purge(dep, frcn)
301 1.1.2.2 thorpej struct denode *dep;
302 1.1.2.2 thorpej u_int frcn;
303 1.1.2.2 thorpej {
304 1.1.2.2 thorpej int i;
305 1.1.2.2 thorpej struct fatcache *fcp;
306 1.1.2.2 thorpej
307 1.1.2.2 thorpej fcp = dep->de_fc;
308 1.1.2.2 thorpej for (i = 0; i < FC_SIZE; i++, fcp++) {
309 1.1.2.2 thorpej if (fcp->fc_frcn >= frcn)
310 1.1.2.2 thorpej fcp->fc_frcn = FCE_EMPTY;
311 1.1.2.2 thorpej }
312 1.1.2.2 thorpej }
313 1.1.2.2 thorpej
314 1.1.2.2 thorpej /*
315 1.1.2.2 thorpej * Update the fat.
316 1.1.2.2 thorpej * If mirroring the fat, update all copies, with the first copy as last.
317 1.1.2.2 thorpej * Else update only the current fat (ignoring the others).
318 1.1.2.2 thorpej *
319 1.1.2.2 thorpej * pmp - msdosfsmount structure for filesystem to update
320 1.1.2.2 thorpej * bp - addr of modified fat block
321 1.1.2.2 thorpej * fatbn - block number relative to begin of filesystem of the modified fat block.
322 1.1.2.2 thorpej */
323 1.1.2.2 thorpej void
324 1.1.2.2 thorpej updatefats(pmp, bp, fatbn)
325 1.1.2.2 thorpej struct msdosfsmount *pmp;
326 1.1.2.2 thorpej struct buf *bp;
327 1.1.2.2 thorpej u_long fatbn;
328 1.1.2.2 thorpej {
329 1.1.2.2 thorpej int i;
330 1.1.2.2 thorpej struct buf *bpn;
331 1.1.2.2 thorpej
332 1.1.2.2 thorpej #ifdef MSDOSFS_DEBUG
333 1.1.2.2 thorpej printf("updatefats(pmp %p, bp %p, fatbn %lu)\n",
334 1.1.2.2 thorpej pmp, bp, fatbn);
335 1.1.2.2 thorpej #endif
336 1.1.2.2 thorpej
337 1.1.2.2 thorpej /*
338 1.1.2.2 thorpej * If we have an FSInfo block, update it.
339 1.1.2.2 thorpej */
340 1.1.2.2 thorpej if (pmp->pm_fsinfo) {
341 1.1.2.2 thorpej u_long cn = pmp->pm_nxtfree;
342 1.1.2.2 thorpej
343 1.1.2.2 thorpej if (pmp->pm_freeclustercount
344 1.1.2.2 thorpej && (pmp->pm_inusemap[cn / N_INUSEBITS]
345 1.1.2.2 thorpej & (1 << (cn % N_INUSEBITS)))) {
346 1.1.2.2 thorpej /*
347 1.1.2.2 thorpej * The cluster indicated in FSInfo isn't free
348 1.1.2.2 thorpej * any longer. Got get a new free one.
349 1.1.2.2 thorpej */
350 1.1.2.2 thorpej for (cn = 0; cn < pmp->pm_maxcluster; cn++)
351 1.1.2.2 thorpej if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
352 1.1.2.2 thorpej break;
353 1.1.2.2 thorpej pmp->pm_nxtfree = cn
354 1.1.2.2 thorpej + ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
355 1.1.2.2 thorpej ^ (u_int)-1) - 1;
356 1.1.2.2 thorpej }
357 1.1.2.2 thorpej if (bread(pmp->pm_devvp, pmp->pm_fsinfo, 1024, NOCRED, &bpn) != 0) {
358 1.1.2.2 thorpej /*
359 1.1.2.2 thorpej * Ignore the error, but turn off FSInfo update for the future.
360 1.1.2.2 thorpej */
361 1.1.2.2 thorpej pmp->pm_fsinfo = 0;
362 1.1.2.2 thorpej brelse(bpn);
363 1.1.2.2 thorpej } else {
364 1.1.2.2 thorpej struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
365 1.1.2.2 thorpej
366 1.1.2.2 thorpej putulong(fp->fsinfree, pmp->pm_freeclustercount);
367 1.1.2.2 thorpej putulong(fp->fsinxtfree, pmp->pm_nxtfree);
368 1.1.2.2 thorpej if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
369 1.1.2.2 thorpej bwrite(bpn);
370 1.1.2.2 thorpej else
371 1.1.2.2 thorpej bdwrite(bpn);
372 1.1.2.2 thorpej }
373 1.1.2.2 thorpej }
374 1.1.2.2 thorpej
375 1.1.2.2 thorpej if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
376 1.1.2.2 thorpej /*
377 1.1.2.2 thorpej * Now copy the block(s) of the modified fat to the other copies of
378 1.1.2.2 thorpej * the fat and write them out. This is faster than reading in the
379 1.1.2.2 thorpej * other fats and then writing them back out. This could tie up
380 1.1.2.2 thorpej * the fat for quite a while. Preventing others from accessing it.
381 1.1.2.2 thorpej * To prevent us from going after the fat quite so much we use
382 1.1.2.2 thorpej * delayed writes, unless they specfied "synchronous" when the
383 1.1.2.2 thorpej * filesystem was mounted. If synch is asked for then use
384 1.1.2.2 thorpej * bwrite()'s and really slow things down.
385 1.1.2.2 thorpej */
386 1.1.2.2 thorpej for (i = 1; i < pmp->pm_FATs; i++) {
387 1.1.2.2 thorpej fatbn += pmp->pm_FATsecs;
388 1.1.2.2 thorpej /* getblk() never fails */
389 1.1.2.2 thorpej bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 0, 0);
390 1.1.2.2 thorpej memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
391 1.1.2.2 thorpej if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
392 1.1.2.2 thorpej bwrite(bpn);
393 1.1.2.2 thorpej else
394 1.1.2.2 thorpej bdwrite(bpn);
395 1.1.2.2 thorpej }
396 1.1.2.2 thorpej }
397 1.1.2.2 thorpej
398 1.1.2.2 thorpej /*
399 1.1.2.2 thorpej * Write out the first (or current) fat last.
400 1.1.2.2 thorpej */
401 1.1.2.2 thorpej if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
402 1.1.2.2 thorpej bwrite(bp);
403 1.1.2.2 thorpej else
404 1.1.2.2 thorpej bdwrite(bp);
405 1.1.2.2 thorpej /*
406 1.1.2.2 thorpej * Maybe update fsinfo sector here?
407 1.1.2.2 thorpej */
408 1.1.2.2 thorpej }
409 1.1.2.2 thorpej
410 1.1.2.2 thorpej /*
411 1.1.2.2 thorpej * Updating entries in 12 bit fats is a pain in the butt.
412 1.1.2.2 thorpej *
413 1.1.2.2 thorpej * The following picture shows where nibbles go when moving from a 12 bit
414 1.1.2.2 thorpej * cluster number into the appropriate bytes in the FAT.
415 1.1.2.2 thorpej *
416 1.1.2.2 thorpej * byte m byte m+1 byte m+2
417 1.1.2.2 thorpej * +----+----+ +----+----+ +----+----+
418 1.1.2.2 thorpej * | 0 1 | | 2 3 | | 4 5 | FAT bytes
419 1.1.2.2 thorpej * +----+----+ +----+----+ +----+----+
420 1.1.2.2 thorpej *
421 1.1.2.2 thorpej * +----+----+----+ +----+----+----+
422 1.1.2.2 thorpej * | 3 0 1 | | 4 5 2 |
423 1.1.2.2 thorpej * +----+----+----+ +----+----+----+
424 1.1.2.2 thorpej * cluster n cluster n+1
425 1.1.2.2 thorpej *
426 1.1.2.2 thorpej * Where n is even. m = n + (n >> 2)
427 1.1.2.2 thorpej *
428 1.1.2.2 thorpej */
429 1.1.2.2 thorpej static __inline void
430 1.1.2.2 thorpej usemap_alloc(pmp, cn)
431 1.1.2.2 thorpej struct msdosfsmount *pmp;
432 1.1.2.2 thorpej u_long cn;
433 1.1.2.2 thorpej {
434 1.1.2.2 thorpej
435 1.1.2.2 thorpej pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
436 1.1.2.2 thorpej pmp->pm_freeclustercount--;
437 1.1.2.2 thorpej }
438 1.1.2.2 thorpej
439 1.1.2.2 thorpej static __inline void
440 1.1.2.2 thorpej usemap_free(pmp, cn)
441 1.1.2.2 thorpej struct msdosfsmount *pmp;
442 1.1.2.2 thorpej u_long cn;
443 1.1.2.2 thorpej {
444 1.1.2.2 thorpej
445 1.1.2.2 thorpej pmp->pm_freeclustercount++;
446 1.1.2.2 thorpej pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
447 1.1.2.2 thorpej }
448 1.1.2.2 thorpej
449 1.1.2.2 thorpej int
450 1.1.2.2 thorpej clusterfree(pmp, cluster, oldcnp)
451 1.1.2.2 thorpej struct msdosfsmount *pmp;
452 1.1.2.2 thorpej u_long cluster;
453 1.1.2.2 thorpej u_long *oldcnp;
454 1.1.2.2 thorpej {
455 1.1.2.2 thorpej int error;
456 1.1.2.2 thorpej u_long oldcn;
457 1.1.2.2 thorpej
458 1.1.2.2 thorpej usemap_free(pmp, cluster);
459 1.1.2.2 thorpej error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
460 1.1.2.2 thorpej if (error) {
461 1.1.2.2 thorpej usemap_alloc(pmp, cluster);
462 1.1.2.2 thorpej return (error);
463 1.1.2.2 thorpej }
464 1.1.2.2 thorpej /*
465 1.1.2.2 thorpej * If the cluster was successfully marked free, then update
466 1.1.2.2 thorpej * the count of free clusters, and turn off the "allocated"
467 1.1.2.2 thorpej * bit in the "in use" cluster bit map.
468 1.1.2.2 thorpej */
469 1.1.2.2 thorpej if (oldcnp)
470 1.1.2.2 thorpej *oldcnp = oldcn;
471 1.1.2.2 thorpej return (0);
472 1.1.2.2 thorpej }
473 1.1.2.2 thorpej
474 1.1.2.2 thorpej /*
475 1.1.2.2 thorpej * Get or Set or 'Get and Set' the cluster'th entry in the fat.
476 1.1.2.2 thorpej *
477 1.1.2.2 thorpej * function - whether to get or set a fat entry
478 1.1.2.2 thorpej * pmp - address of the msdosfsmount structure for the filesystem
479 1.1.2.2 thorpej * whose fat is to be manipulated.
480 1.1.2.2 thorpej * cn - which cluster is of interest
481 1.1.2.2 thorpej * oldcontents - address of a word that is to receive the contents of the
482 1.1.2.2 thorpej * cluster'th entry if this is a get function
483 1.1.2.2 thorpej * newcontents - the new value to be written into the cluster'th element of
484 1.1.2.2 thorpej * the fat if this is a set function.
485 1.1.2.2 thorpej *
486 1.1.2.2 thorpej * This function can also be used to free a cluster by setting the fat entry
487 1.1.2.2 thorpej * for a cluster to 0.
488 1.1.2.2 thorpej *
489 1.1.2.2 thorpej * All copies of the fat are updated if this is a set function. NOTE: If
490 1.1.2.2 thorpej * fatentry() marks a cluster as free it does not update the inusemap in
491 1.1.2.2 thorpej * the msdosfsmount structure. This is left to the caller.
492 1.1.2.2 thorpej */
493 1.1.2.2 thorpej int
494 1.1.2.2 thorpej fatentry(function, pmp, cn, oldcontents, newcontents)
495 1.1.2.2 thorpej int function;
496 1.1.2.2 thorpej struct msdosfsmount *pmp;
497 1.1.2.2 thorpej u_long cn;
498 1.1.2.2 thorpej u_long *oldcontents;
499 1.1.2.2 thorpej u_long newcontents;
500 1.1.2.2 thorpej {
501 1.1.2.2 thorpej int error;
502 1.1.2.2 thorpej u_long readcn;
503 1.1.2.2 thorpej u_long bn, bo, bsize, byteoffset;
504 1.1.2.2 thorpej struct buf *bp;
505 1.1.2.2 thorpej
506 1.1.2.2 thorpej #ifdef MSDOSFS_DEBUG
507 1.1.2.2 thorpej printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
508 1.1.2.2 thorpej function, pmp, cn, oldcontents, newcontents);
509 1.1.2.2 thorpej #endif
510 1.1.2.2 thorpej
511 1.1.2.2 thorpej #ifdef DIAGNOSTIC
512 1.1.2.2 thorpej /*
513 1.1.2.2 thorpej * Be sure they asked us to do something.
514 1.1.2.2 thorpej */
515 1.1.2.2 thorpej if ((function & (FAT_SET | FAT_GET)) == 0) {
516 1.1.2.2 thorpej printf("fatentry(): function code doesn't specify get or set\n");
517 1.1.2.2 thorpej return (EINVAL);
518 1.1.2.2 thorpej }
519 1.1.2.2 thorpej
520 1.1.2.2 thorpej /*
521 1.1.2.2 thorpej * If they asked us to return a cluster number but didn't tell us
522 1.1.2.2 thorpej * where to put it, give them an error.
523 1.1.2.2 thorpej */
524 1.1.2.2 thorpej if ((function & FAT_GET) && oldcontents == NULL) {
525 1.1.2.2 thorpej printf("fatentry(): get function with no place to put result\n");
526 1.1.2.2 thorpej return (EINVAL);
527 1.1.2.2 thorpej }
528 1.1.2.2 thorpej #endif
529 1.1.2.2 thorpej
530 1.1.2.2 thorpej /*
531 1.1.2.2 thorpej * Be sure the requested cluster is in the filesystem.
532 1.1.2.2 thorpej */
533 1.1.2.2 thorpej if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
534 1.1.2.2 thorpej return (EINVAL);
535 1.1.2.2 thorpej
536 1.1.2.2 thorpej byteoffset = FATOFS(pmp, cn);
537 1.1.2.2 thorpej fatblock(pmp, byteoffset, &bn, &bsize, &bo);
538 1.1.2.2 thorpej if ((error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp)) != 0) {
539 1.1.2.2 thorpej brelse(bp);
540 1.1.2.2 thorpej return (error);
541 1.1.2.2 thorpej }
542 1.1.2.2 thorpej
543 1.1.2.2 thorpej if (function & FAT_GET) {
544 1.1.2.2 thorpej if (FAT32(pmp))
545 1.1.2.2 thorpej readcn = getulong(&bp->b_data[bo]);
546 1.1.2.2 thorpej else
547 1.1.2.2 thorpej readcn = getushort(&bp->b_data[bo]);
548 1.1.2.2 thorpej if (FAT12(pmp) & (cn & 1))
549 1.1.2.2 thorpej readcn >>= 4;
550 1.1.2.2 thorpej readcn &= pmp->pm_fatmask;
551 1.1.2.2 thorpej *oldcontents = readcn;
552 1.1.2.2 thorpej }
553 1.1.2.2 thorpej if (function & FAT_SET) {
554 1.1.2.2 thorpej switch (pmp->pm_fatmask) {
555 1.1.2.2 thorpej case FAT12_MASK:
556 1.1.2.2 thorpej readcn = getushort(&bp->b_data[bo]);
557 1.1.2.2 thorpej if (cn & 1) {
558 1.1.2.2 thorpej readcn &= 0x000f;
559 1.1.2.2 thorpej readcn |= newcontents << 4;
560 1.1.2.2 thorpej } else {
561 1.1.2.2 thorpej readcn &= 0xf000;
562 1.1.2.2 thorpej readcn |= newcontents & 0xfff;
563 1.1.2.2 thorpej }
564 1.1.2.2 thorpej putushort(&bp->b_data[bo], readcn);
565 1.1.2.2 thorpej break;
566 1.1.2.2 thorpej case FAT16_MASK:
567 1.1.2.2 thorpej putushort(&bp->b_data[bo], newcontents);
568 1.1.2.2 thorpej break;
569 1.1.2.2 thorpej case FAT32_MASK:
570 1.1.2.2 thorpej /*
571 1.1.2.2 thorpej * According to spec we have to retain the
572 1.1.2.2 thorpej * high order bits of the fat entry.
573 1.1.2.2 thorpej */
574 1.1.2.2 thorpej readcn = getulong(&bp->b_data[bo]);
575 1.1.2.2 thorpej readcn &= ~FAT32_MASK;
576 1.1.2.2 thorpej readcn |= newcontents & FAT32_MASK;
577 1.1.2.2 thorpej putulong(&bp->b_data[bo], readcn);
578 1.1.2.2 thorpej break;
579 1.1.2.2 thorpej }
580 1.1.2.2 thorpej updatefats(pmp, bp, bn);
581 1.1.2.2 thorpej bp = NULL;
582 1.1.2.2 thorpej pmp->pm_fmod = 1;
583 1.1.2.2 thorpej }
584 1.1.2.2 thorpej if (bp)
585 1.1.2.2 thorpej brelse(bp);
586 1.1.2.2 thorpej return (0);
587 1.1.2.2 thorpej }
588 1.1.2.2 thorpej
589 1.1.2.2 thorpej /*
590 1.1.2.2 thorpej * Update a contiguous cluster chain
591 1.1.2.2 thorpej *
592 1.1.2.2 thorpej * pmp - mount point
593 1.1.2.2 thorpej * start - first cluster of chain
594 1.1.2.2 thorpej * count - number of clusters in chain
595 1.1.2.2 thorpej * fillwith - what to write into fat entry of last cluster
596 1.1.2.2 thorpej */
597 1.1.2.2 thorpej static int
598 1.1.2.2 thorpej fatchain(pmp, start, count, fillwith)
599 1.1.2.2 thorpej struct msdosfsmount *pmp;
600 1.1.2.2 thorpej u_long start;
601 1.1.2.2 thorpej u_long count;
602 1.1.2.2 thorpej u_long fillwith;
603 1.1.2.2 thorpej {
604 1.1.2.2 thorpej int error;
605 1.1.2.2 thorpej u_long bn, bo, bsize, byteoffset, readcn, newc;
606 1.1.2.2 thorpej struct buf *bp;
607 1.1.2.2 thorpej
608 1.1.2.2 thorpej #ifdef MSDOSFS_DEBUG
609 1.1.2.2 thorpej printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
610 1.1.2.2 thorpej pmp, start, count, fillwith);
611 1.1.2.2 thorpej #endif
612 1.1.2.2 thorpej /*
613 1.1.2.2 thorpej * Be sure the clusters are in the filesystem.
614 1.1.2.2 thorpej */
615 1.1.2.2 thorpej if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
616 1.1.2.2 thorpej return (EINVAL);
617 1.1.2.2 thorpej
618 1.1.2.2 thorpej while (count > 0) {
619 1.1.2.2 thorpej byteoffset = FATOFS(pmp, start);
620 1.1.2.2 thorpej fatblock(pmp, byteoffset, &bn, &bsize, &bo);
621 1.1.2.2 thorpej error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
622 1.1.2.2 thorpej if (error) {
623 1.1.2.2 thorpej brelse(bp);
624 1.1.2.2 thorpej return (error);
625 1.1.2.2 thorpej }
626 1.1.2.2 thorpej while (count > 0) {
627 1.1.2.2 thorpej start++;
628 1.1.2.2 thorpej newc = --count > 0 ? start : fillwith;
629 1.1.2.2 thorpej switch (pmp->pm_fatmask) {
630 1.1.2.2 thorpej case FAT12_MASK:
631 1.1.2.2 thorpej readcn = getushort(&bp->b_data[bo]);
632 1.1.2.2 thorpej if (start & 1) {
633 1.1.2.2 thorpej readcn &= 0xf000;
634 1.1.2.2 thorpej readcn |= newc & 0xfff;
635 1.1.2.2 thorpej } else {
636 1.1.2.2 thorpej readcn &= 0x000f;
637 1.1.2.2 thorpej readcn |= newc << 4;
638 1.1.2.2 thorpej }
639 1.1.2.2 thorpej putushort(&bp->b_data[bo], readcn);
640 1.1.2.2 thorpej bo++;
641 1.1.2.2 thorpej if (!(start & 1))
642 1.1.2.2 thorpej bo++;
643 1.1.2.2 thorpej break;
644 1.1.2.2 thorpej case FAT16_MASK:
645 1.1.2.2 thorpej putushort(&bp->b_data[bo], newc);
646 1.1.2.2 thorpej bo += 2;
647 1.1.2.2 thorpej break;
648 1.1.2.2 thorpej case FAT32_MASK:
649 1.1.2.2 thorpej readcn = getulong(&bp->b_data[bo]);
650 1.1.2.2 thorpej readcn &= ~pmp->pm_fatmask;
651 1.1.2.2 thorpej readcn |= newc & pmp->pm_fatmask;
652 1.1.2.2 thorpej putulong(&bp->b_data[bo], readcn);
653 1.1.2.2 thorpej bo += 4;
654 1.1.2.2 thorpej break;
655 1.1.2.2 thorpej }
656 1.1.2.2 thorpej if (bo >= bsize)
657 1.1.2.2 thorpej break;
658 1.1.2.2 thorpej }
659 1.1.2.2 thorpej updatefats(pmp, bp, bn);
660 1.1.2.2 thorpej }
661 1.1.2.2 thorpej pmp->pm_fmod = 1;
662 1.1.2.2 thorpej return (0);
663 1.1.2.2 thorpej }
664 1.1.2.2 thorpej
665 1.1.2.2 thorpej /*
666 1.1.2.2 thorpej * Check the length of a free cluster chain starting at start.
667 1.1.2.2 thorpej *
668 1.1.2.2 thorpej * pmp - mount point
669 1.1.2.2 thorpej * start - start of chain
670 1.1.2.2 thorpej * count - maximum interesting length
671 1.1.2.2 thorpej */
672 1.1.2.2 thorpej int
673 1.1.2.2 thorpej chainlength(pmp, start, count)
674 1.1.2.2 thorpej struct msdosfsmount *pmp;
675 1.1.2.2 thorpej u_long start;
676 1.1.2.2 thorpej u_long count;
677 1.1.2.2 thorpej {
678 1.1.2.2 thorpej u_long idx, max_idx;
679 1.1.2.2 thorpej u_int map;
680 1.1.2.2 thorpej u_long len;
681 1.1.2.2 thorpej
682 1.1.2.2 thorpej max_idx = pmp->pm_maxcluster / N_INUSEBITS;
683 1.1.2.2 thorpej idx = start / N_INUSEBITS;
684 1.1.2.2 thorpej start %= N_INUSEBITS;
685 1.1.2.2 thorpej map = pmp->pm_inusemap[idx];
686 1.1.2.2 thorpej map &= ~((1 << start) - 1);
687 1.1.2.2 thorpej if (map) {
688 1.1.2.2 thorpej len = ffs(map) - 1 - start;
689 1.1.2.2 thorpej return (len > count ? count : len);
690 1.1.2.2 thorpej }
691 1.1.2.2 thorpej len = N_INUSEBITS - start;
692 1.1.2.2 thorpej if (len >= count)
693 1.1.2.2 thorpej return (count);
694 1.1.2.2 thorpej while (++idx <= max_idx) {
695 1.1.2.2 thorpej if (len >= count)
696 1.1.2.2 thorpej break;
697 1.1.2.2 thorpej if ((map = pmp->pm_inusemap[idx]) != 0) {
698 1.1.2.2 thorpej len += ffs(map) - 1;
699 1.1.2.2 thorpej break;
700 1.1.2.2 thorpej }
701 1.1.2.2 thorpej len += N_INUSEBITS;
702 1.1.2.2 thorpej }
703 1.1.2.2 thorpej return (len > count ? count : len);
704 1.1.2.2 thorpej }
705 1.1.2.2 thorpej
706 1.1.2.2 thorpej /*
707 1.1.2.2 thorpej * Allocate contigous free clusters.
708 1.1.2.2 thorpej *
709 1.1.2.2 thorpej * pmp - mount point.
710 1.1.2.2 thorpej * start - start of cluster chain.
711 1.1.2.2 thorpej * count - number of clusters to allocate.
712 1.1.2.2 thorpej * fillwith - put this value into the fat entry for the
713 1.1.2.2 thorpej * last allocated cluster.
714 1.1.2.2 thorpej * retcluster - put the first allocated cluster's number here.
715 1.1.2.2 thorpej * got - how many clusters were actually allocated.
716 1.1.2.2 thorpej */
717 1.1.2.2 thorpej int
718 1.1.2.2 thorpej chainalloc(pmp, start, count, fillwith, retcluster, got)
719 1.1.2.2 thorpej struct msdosfsmount *pmp;
720 1.1.2.2 thorpej u_long start;
721 1.1.2.2 thorpej u_long count;
722 1.1.2.2 thorpej u_long fillwith;
723 1.1.2.2 thorpej u_long *retcluster;
724 1.1.2.2 thorpej u_long *got;
725 1.1.2.2 thorpej {
726 1.1.2.2 thorpej int error;
727 1.1.2.2 thorpej u_long cl, n;
728 1.1.2.2 thorpej
729 1.1.2.2 thorpej for (cl = start, n = count; n-- > 0;)
730 1.1.2.2 thorpej usemap_alloc(pmp, cl++);
731 1.1.2.2 thorpej if ((error = fatchain(pmp, start, count, fillwith)) != 0)
732 1.1.2.2 thorpej return (error);
733 1.1.2.2 thorpej #ifdef MSDOSFS_DEBUG
734 1.1.2.2 thorpej printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
735 1.1.2.2 thorpej start, count);
736 1.1.2.2 thorpej #endif
737 1.1.2.2 thorpej if (retcluster)
738 1.1.2.2 thorpej *retcluster = start;
739 1.1.2.2 thorpej if (got)
740 1.1.2.2 thorpej *got = count;
741 1.1.2.2 thorpej return (0);
742 1.1.2.2 thorpej }
743 1.1.2.2 thorpej
744 1.1.2.2 thorpej /*
745 1.1.2.2 thorpej * Allocate contiguous free clusters.
746 1.1.2.2 thorpej *
747 1.1.2.2 thorpej * pmp - mount point.
748 1.1.2.2 thorpej * start - preferred start of cluster chain.
749 1.1.2.2 thorpej * count - number of clusters requested.
750 1.1.2.2 thorpej * fillwith - put this value into the fat entry for the
751 1.1.2.2 thorpej * last allocated cluster.
752 1.1.2.2 thorpej * retcluster - put the first allocated cluster's number here.
753 1.1.2.2 thorpej * got - how many clusters were actually allocated.
754 1.1.2.2 thorpej */
755 1.1.2.2 thorpej int
756 1.1.2.2 thorpej clusteralloc(pmp, start, count, retcluster, got)
757 1.1.2.2 thorpej struct msdosfsmount *pmp;
758 1.1.2.2 thorpej u_long start;
759 1.1.2.2 thorpej u_long count;
760 1.1.2.2 thorpej u_long *retcluster;
761 1.1.2.2 thorpej u_long *got;
762 1.1.2.2 thorpej {
763 1.1.2.2 thorpej u_long idx;
764 1.1.2.2 thorpej u_long len, newst, foundl, cn, l;
765 1.1.2.2 thorpej u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
766 1.1.2.2 thorpej u_long fillwith = CLUST_EOFE;
767 1.1.2.2 thorpej u_int map;
768 1.1.2.2 thorpej
769 1.1.2.2 thorpej #ifdef MSDOSFS_DEBUG
770 1.1.2.2 thorpej printf("clusteralloc(): find %lu clusters\n",count);
771 1.1.2.2 thorpej #endif
772 1.1.2.2 thorpej if (start) {
773 1.1.2.2 thorpej if ((len = chainlength(pmp, start, count)) >= count)
774 1.1.2.2 thorpej return (chainalloc(pmp, start, count, fillwith, retcluster, got));
775 1.1.2.2 thorpej } else {
776 1.1.2.2 thorpej /*
777 1.1.2.2 thorpej * This is a new file, initialize start
778 1.1.2.2 thorpej */
779 1.1.2.2 thorpej struct timeval tv;
780 1.1.2.2 thorpej
781 1.1.2.2 thorpej microtime(&tv);
782 1.1.2.2 thorpej start = (tv.tv_usec >> 10) | tv.tv_usec;
783 1.1.2.2 thorpej len = 0;
784 1.1.2.2 thorpej }
785 1.1.2.2 thorpej
786 1.1.2.2 thorpej /*
787 1.1.2.2 thorpej * Start at a (pseudo) random place to maximize cluster runs
788 1.1.2.2 thorpej * under multiple writers.
789 1.1.2.2 thorpej */
790 1.1.2.2 thorpej newst = (start * 1103515245 + 12345) % (pmp->pm_maxcluster + 1);
791 1.1.2.2 thorpej foundl = 0;
792 1.1.2.2 thorpej
793 1.1.2.2 thorpej for (cn = newst; cn <= pmp->pm_maxcluster;) {
794 1.1.2.2 thorpej idx = cn / N_INUSEBITS;
795 1.1.2.2 thorpej map = pmp->pm_inusemap[idx];
796 1.1.2.2 thorpej map |= (1 << (cn % N_INUSEBITS)) - 1;
797 1.1.2.2 thorpej if (map != (u_int)-1) {
798 1.1.2.2 thorpej cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
799 1.1.2.2 thorpej if ((l = chainlength(pmp, cn, count)) >= count)
800 1.1.2.2 thorpej return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
801 1.1.2.2 thorpej if (l > foundl) {
802 1.1.2.2 thorpej foundcn = cn;
803 1.1.2.2 thorpej foundl = l;
804 1.1.2.2 thorpej }
805 1.1.2.2 thorpej cn += l + 1;
806 1.1.2.2 thorpej continue;
807 1.1.2.2 thorpej }
808 1.1.2.2 thorpej cn += N_INUSEBITS - cn % N_INUSEBITS;
809 1.1.2.2 thorpej }
810 1.1.2.2 thorpej for (cn = 0; cn < newst;) {
811 1.1.2.2 thorpej idx = cn / N_INUSEBITS;
812 1.1.2.2 thorpej map = pmp->pm_inusemap[idx];
813 1.1.2.2 thorpej map |= (1 << (cn % N_INUSEBITS)) - 1;
814 1.1.2.2 thorpej if (map != (u_int)-1) {
815 1.1.2.2 thorpej cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
816 1.1.2.2 thorpej if ((l = chainlength(pmp, cn, count)) >= count)
817 1.1.2.2 thorpej return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
818 1.1.2.2 thorpej if (l > foundl) {
819 1.1.2.2 thorpej foundcn = cn;
820 1.1.2.2 thorpej foundl = l;
821 1.1.2.2 thorpej }
822 1.1.2.2 thorpej cn += l + 1;
823 1.1.2.2 thorpej continue;
824 1.1.2.2 thorpej }
825 1.1.2.2 thorpej cn += N_INUSEBITS - cn % N_INUSEBITS;
826 1.1.2.2 thorpej }
827 1.1.2.2 thorpej
828 1.1.2.2 thorpej if (!foundl)
829 1.1.2.2 thorpej return (ENOSPC);
830 1.1.2.2 thorpej
831 1.1.2.2 thorpej if (len)
832 1.1.2.2 thorpej return (chainalloc(pmp, start, len, fillwith, retcluster, got));
833 1.1.2.2 thorpej else
834 1.1.2.2 thorpej return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
835 1.1.2.2 thorpej }
836 1.1.2.2 thorpej
837 1.1.2.2 thorpej
838 1.1.2.2 thorpej /*
839 1.1.2.2 thorpej * Free a chain of clusters.
840 1.1.2.2 thorpej *
841 1.1.2.2 thorpej * pmp - address of the msdosfs mount structure for the filesystem
842 1.1.2.2 thorpej * containing the cluster chain to be freed.
843 1.1.2.2 thorpej * startcluster - number of the 1st cluster in the chain of clusters to be
844 1.1.2.2 thorpej * freed.
845 1.1.2.2 thorpej */
846 1.1.2.2 thorpej int
847 1.1.2.2 thorpej freeclusterchain(pmp, cluster)
848 1.1.2.2 thorpej struct msdosfsmount *pmp;
849 1.1.2.2 thorpej u_long cluster;
850 1.1.2.2 thorpej {
851 1.1.2.2 thorpej int error;
852 1.1.2.2 thorpej struct buf *bp = NULL;
853 1.1.2.2 thorpej u_long bn, bo, bsize, byteoffset;
854 1.1.2.2 thorpej u_long readcn, lbn = -1;
855 1.1.2.2 thorpej
856 1.1.2.2 thorpej while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
857 1.1.2.2 thorpej byteoffset = FATOFS(pmp, cluster);
858 1.1.2.2 thorpej fatblock(pmp, byteoffset, &bn, &bsize, &bo);
859 1.1.2.2 thorpej if (lbn != bn) {
860 1.1.2.2 thorpej if (bp)
861 1.1.2.2 thorpej updatefats(pmp, bp, lbn);
862 1.1.2.2 thorpej error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
863 1.1.2.2 thorpej if (error) {
864 1.1.2.2 thorpej brelse(bp);
865 1.1.2.2 thorpej return (error);
866 1.1.2.2 thorpej }
867 1.1.2.2 thorpej lbn = bn;
868 1.1.2.2 thorpej }
869 1.1.2.2 thorpej usemap_free(pmp, cluster);
870 1.1.2.2 thorpej switch (pmp->pm_fatmask) {
871 1.1.2.2 thorpej case FAT12_MASK:
872 1.1.2.2 thorpej readcn = getushort(&bp->b_data[bo]);
873 1.1.2.2 thorpej if (cluster & 1) {
874 1.1.2.2 thorpej cluster = readcn >> 4;
875 1.1.2.2 thorpej readcn &= 0x000f;
876 1.1.2.2 thorpej readcn |= MSDOSFSFREE << 4;
877 1.1.2.2 thorpej } else {
878 1.1.2.2 thorpej cluster = readcn;
879 1.1.2.2 thorpej readcn &= 0xf000;
880 1.1.2.2 thorpej readcn |= MSDOSFSFREE & 0xfff;
881 1.1.2.2 thorpej }
882 1.1.2.2 thorpej putushort(&bp->b_data[bo], readcn);
883 1.1.2.2 thorpej break;
884 1.1.2.2 thorpej case FAT16_MASK:
885 1.1.2.2 thorpej cluster = getushort(&bp->b_data[bo]);
886 1.1.2.2 thorpej putushort(&bp->b_data[bo], MSDOSFSFREE);
887 1.1.2.2 thorpej break;
888 1.1.2.2 thorpej case FAT32_MASK:
889 1.1.2.2 thorpej cluster = getulong(&bp->b_data[bo]);
890 1.1.2.2 thorpej putulong(&bp->b_data[bo],
891 1.1.2.2 thorpej (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
892 1.1.2.2 thorpej break;
893 1.1.2.2 thorpej }
894 1.1.2.2 thorpej cluster &= pmp->pm_fatmask;
895 1.1.2.2 thorpej }
896 1.1.2.2 thorpej if (bp)
897 1.1.2.2 thorpej updatefats(pmp, bp, bn);
898 1.1.2.2 thorpej return (0);
899 1.1.2.2 thorpej }
900 1.1.2.2 thorpej
901 1.1.2.2 thorpej /*
902 1.1.2.2 thorpej * Read in fat blocks looking for free clusters. For every free cluster
903 1.1.2.2 thorpej * found turn off its corresponding bit in the pm_inusemap.
904 1.1.2.2 thorpej */
905 1.1.2.2 thorpej int
906 1.1.2.2 thorpej fillinusemap(pmp)
907 1.1.2.2 thorpej struct msdosfsmount *pmp;
908 1.1.2.2 thorpej {
909 1.1.2.2 thorpej struct buf *bp = NULL;
910 1.1.2.2 thorpej u_long cn, readcn;
911 1.1.2.2 thorpej int error;
912 1.1.2.2 thorpej u_long bn, bo, bsize, byteoffset;
913 1.1.2.2 thorpej
914 1.1.2.2 thorpej /*
915 1.1.2.2 thorpej * Mark all clusters in use, we mark the free ones in the fat scan
916 1.1.2.2 thorpej * loop further down.
917 1.1.2.2 thorpej */
918 1.1.2.2 thorpej for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
919 1.1.2.2 thorpej pmp->pm_inusemap[cn] = (u_int)-1;
920 1.1.2.2 thorpej
921 1.1.2.2 thorpej /*
922 1.1.2.2 thorpej * Figure how many free clusters are in the filesystem by ripping
923 1.1.2.2 thorpej * through the fat counting the number of entries whose content is
924 1.1.2.2 thorpej * zero. These represent free clusters.
925 1.1.2.2 thorpej */
926 1.1.2.2 thorpej pmp->pm_freeclustercount = 0;
927 1.1.2.2 thorpej for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
928 1.1.2.2 thorpej byteoffset = FATOFS(pmp, cn);
929 1.1.2.2 thorpej bo = byteoffset % pmp->pm_fatblocksize;
930 1.1.2.2 thorpej if (!bo || !bp) {
931 1.1.2.2 thorpej /* Read new FAT block */
932 1.1.2.2 thorpej if (bp)
933 1.1.2.2 thorpej brelse(bp);
934 1.1.2.2 thorpej fatblock(pmp, byteoffset, &bn, &bsize, NULL);
935 1.1.2.2 thorpej error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
936 1.1.2.2 thorpej if (error) {
937 1.1.2.2 thorpej brelse(bp);
938 1.1.2.2 thorpej return (error);
939 1.1.2.2 thorpej }
940 1.1.2.2 thorpej }
941 1.1.2.2 thorpej if (FAT32(pmp))
942 1.1.2.2 thorpej readcn = getulong(&bp->b_data[bo]);
943 1.1.2.2 thorpej else
944 1.1.2.2 thorpej readcn = getushort(&bp->b_data[bo]);
945 1.1.2.2 thorpej if (FAT12(pmp) && (cn & 1))
946 1.1.2.2 thorpej readcn >>= 4;
947 1.1.2.2 thorpej readcn &= pmp->pm_fatmask;
948 1.1.2.2 thorpej
949 1.1.2.2 thorpej if (readcn == 0)
950 1.1.2.2 thorpej usemap_free(pmp, cn);
951 1.1.2.2 thorpej }
952 1.1.2.2 thorpej brelse(bp);
953 1.1.2.2 thorpej return (0);
954 1.1.2.2 thorpej }
955 1.1.2.2 thorpej
956 1.1.2.2 thorpej /*
957 1.1.2.2 thorpej * Allocate a new cluster and chain it onto the end of the file.
958 1.1.2.2 thorpej *
959 1.1.2.2 thorpej * dep - the file to extend
960 1.1.2.2 thorpej * count - number of clusters to allocate
961 1.1.2.2 thorpej * bpp - where to return the address of the buf header for the first new
962 1.1.2.2 thorpej * file block
963 1.1.2.2 thorpej * ncp - where to put cluster number of the first newly allocated cluster
964 1.1.2.2 thorpej * If this pointer is 0, do not return the cluster number.
965 1.1.2.2 thorpej * flags - see fat.h
966 1.1.2.2 thorpej *
967 1.1.2.2 thorpej * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
968 1.1.2.2 thorpej * the de_flag field of the denode and it does not change the de_FileSize
969 1.1.2.2 thorpej * field. This is left for the caller to do.
970 1.1.2.2 thorpej */
971 1.1.2.2 thorpej
972 1.1.2.2 thorpej int
973 1.1.2.2 thorpej extendfile(dep, count, bpp, ncp, flags)
974 1.1.2.2 thorpej struct denode *dep;
975 1.1.2.2 thorpej u_long count;
976 1.1.2.2 thorpej struct buf **bpp;
977 1.1.2.2 thorpej u_long *ncp;
978 1.1.2.2 thorpej int flags;
979 1.1.2.2 thorpej {
980 1.1.2.2 thorpej int error;
981 1.1.2.2 thorpej u_long frcn = 0, cn, got;
982 1.1.2.2 thorpej struct msdosfsmount *pmp = dep->de_pmp;
983 1.1.2.2 thorpej struct buf *bp;
984 1.1.2.2 thorpej
985 1.1.2.2 thorpej /*
986 1.1.2.2 thorpej * Don't try to extend the root directory
987 1.1.2.2 thorpej */
988 1.1.2.2 thorpej if (dep->de_StartCluster == MSDOSFSROOT
989 1.1.2.2 thorpej && (dep->de_Attributes & ATTR_DIRECTORY)) {
990 1.1.2.2 thorpej printf("extendfile(): attempt to extend root directory\n");
991 1.1.2.2 thorpej return (ENOSPC);
992 1.1.2.2 thorpej }
993 1.1.2.2 thorpej
994 1.1.2.2 thorpej /*
995 1.1.2.2 thorpej * If the "file's last cluster" cache entry is empty, and the file
996 1.1.2.2 thorpej * is not empty, then fill the cache entry by calling pcbmap().
997 1.1.2.2 thorpej */
998 1.1.2.2 thorpej fc_fileextends++;
999 1.1.2.2 thorpej if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
1000 1.1.2.2 thorpej dep->de_StartCluster != 0) {
1001 1.1.2.2 thorpej fc_lfcempty++;
1002 1.1.2.2 thorpej error = pcbmap(dep, CLUST_END, 0, &cn, 0);
1003 1.1.2.2 thorpej /* we expect it to return E2BIG */
1004 1.1.2.2 thorpej if (error != E2BIG)
1005 1.1.2.2 thorpej return (error);
1006 1.1.2.2 thorpej }
1007 1.1.2.2 thorpej
1008 1.1.2.2 thorpej while (count > 0) {
1009 1.1.2.2 thorpej
1010 1.1.2.2 thorpej /*
1011 1.1.2.2 thorpej * Allocate a new cluster chain and cat onto the end of the
1012 1.1.2.2 thorpej * file. If the file is empty we make de_StartCluster point
1013 1.1.2.2 thorpej * to the new block. Note that de_StartCluster being 0 is
1014 1.1.2.2 thorpej * sufficient to be sure the file is empty since we exclude
1015 1.1.2.2 thorpej * attempts to extend the root directory above, and the root
1016 1.1.2.2 thorpej * dir is the only file with a startcluster of 0 that has
1017 1.1.2.2 thorpej * blocks allocated (sort of).
1018 1.1.2.2 thorpej */
1019 1.1.2.2 thorpej
1020 1.1.2.2 thorpej if (dep->de_StartCluster == 0)
1021 1.1.2.2 thorpej cn = 0;
1022 1.1.2.2 thorpej else
1023 1.1.2.2 thorpej cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1024 1.1.2.2 thorpej error = clusteralloc(pmp, cn, count, &cn, &got);
1025 1.1.2.2 thorpej if (error)
1026 1.1.2.2 thorpej return (error);
1027 1.1.2.2 thorpej
1028 1.1.2.2 thorpej count -= got;
1029 1.1.2.2 thorpej
1030 1.1.2.2 thorpej /*
1031 1.1.2.2 thorpej * Give them the filesystem relative cluster number if they want
1032 1.1.2.2 thorpej * it.
1033 1.1.2.2 thorpej */
1034 1.1.2.2 thorpej if (ncp) {
1035 1.1.2.2 thorpej *ncp = cn;
1036 1.1.2.2 thorpej ncp = NULL;
1037 1.1.2.2 thorpej }
1038 1.1.2.2 thorpej
1039 1.1.2.2 thorpej if (dep->de_StartCluster == 0) {
1040 1.1.2.2 thorpej dep->de_StartCluster = cn;
1041 1.1.2.2 thorpej frcn = 0;
1042 1.1.2.2 thorpej } else {
1043 1.1.2.2 thorpej error = fatentry(FAT_SET, pmp,
1044 1.1.2.2 thorpej dep->de_fc[FC_LASTFC].fc_fsrcn,
1045 1.1.2.2 thorpej 0, cn);
1046 1.1.2.2 thorpej if (error) {
1047 1.1.2.2 thorpej clusterfree(pmp, cn, NULL);
1048 1.1.2.2 thorpej return (error);
1049 1.1.2.2 thorpej }
1050 1.1.2.2 thorpej frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1051 1.1.2.2 thorpej }
1052 1.1.2.2 thorpej
1053 1.1.2.2 thorpej /*
1054 1.1.2.2 thorpej * Update the "last cluster of the file" entry in the
1055 1.1.2.2 thorpej * denode's fat cache.
1056 1.1.2.2 thorpej */
1057 1.1.2.2 thorpej
1058 1.1.2.2 thorpej fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1059 1.1.2.2 thorpej if ((flags & DE_CLEAR) &&
1060 1.1.2.2 thorpej (dep->de_Attributes & ATTR_DIRECTORY)) {
1061 1.1.2.2 thorpej while (got-- > 0) {
1062 1.1.2.2 thorpej bp = getblk(pmp->pm_devvp, cntobn(pmp, cn++),
1063 1.1.2.2 thorpej pmp->pm_bpcluster, 0, 0);
1064 1.1.2.2 thorpej clrbuf(bp);
1065 1.1.2.2 thorpej if (bpp) {
1066 1.1.2.2 thorpej *bpp = bp;
1067 1.1.2.2 thorpej bpp = NULL;
1068 1.1.2.2 thorpej } else {
1069 1.1.2.2 thorpej bdwrite(bp);
1070 1.1.2.2 thorpej }
1071 1.1.2.2 thorpej }
1072 1.1.2.2 thorpej }
1073 1.1.2.2 thorpej }
1074 1.1.2.2 thorpej
1075 1.1.2.2 thorpej return (0);
1076 1.1.2.2 thorpej }
1077