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