msdosfs_vnops.c revision 1.22 1 1.22 riastrad /* $NetBSD: msdosfs_vnops.c,v 1.22 2022/04/09 10:05:35 riastradh Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 1.1 christos * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 1.1 christos * All rights reserved.
7 1.1 christos * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos * 3. All advertising materials mentioning features or use of this software
18 1.1 christos * must display the following acknowledgement:
19 1.1 christos * This product includes software developed by TooLs GmbH.
20 1.1 christos * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.1 christos * derived from this software without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 christos * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1 christos * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.1 christos * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1 christos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.1 christos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.1 christos * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 christos */
34 1.1 christos /*
35 1.1 christos * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 1.1 christos *
37 1.1 christos * You can do anything you want with this software, just don't say you wrote
38 1.1 christos * it, and don't remove this notice.
39 1.1 christos *
40 1.1 christos * This software is provided "as is".
41 1.1 christos *
42 1.1 christos * The author supplies this software to be publicly redistributed on the
43 1.1 christos * understanding that the author is not responsible for the correct
44 1.1 christos * functioning of this software in any circumstances and is not liable for
45 1.1 christos * any damages caused by this software.
46 1.1 christos *
47 1.1 christos * October 1992
48 1.1 christos */
49 1.3 christos #if HAVE_NBTOOL_CONFIG_H
50 1.3 christos #include "nbtool_config.h"
51 1.3 christos #endif
52 1.1 christos
53 1.1 christos #include <sys/cdefs.h>
54 1.22 riastrad __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.22 2022/04/09 10:05:35 riastradh Exp $");
55 1.1 christos
56 1.1 christos #include <sys/param.h>
57 1.1 christos #include <sys/mman.h>
58 1.1 christos #include <fcntl.h>
59 1.1 christos #include <unistd.h>
60 1.1 christos
61 1.1 christos #include <ffs/buf.h>
62 1.1 christos
63 1.1 christos #include <fs/msdosfs/bpb.h>
64 1.1 christos #include <fs/msdosfs/direntry.h>
65 1.1 christos #include <fs/msdosfs/denode.h>
66 1.1 christos #include <fs/msdosfs/msdosfsmount.h>
67 1.1 christos #include <fs/msdosfs/fat.h>
68 1.1 christos
69 1.1 christos #include "makefs.h"
70 1.1 christos #include "msdos.h"
71 1.1 christos
72 1.9 christos #ifdef MSDOSFS_DEBUG
73 1.9 christos #define DPRINTF(a) printf a
74 1.9 christos #else
75 1.9 christos #define DPRINTF(a)
76 1.9 christos #endif
77 1.1 christos /*
78 1.1 christos * Some general notes:
79 1.1 christos *
80 1.1 christos * In the ufs filesystem the inodes, superblocks, and indirect blocks are
81 1.1 christos * read/written using the vnode for the filesystem. Blocks that represent
82 1.1 christos * the contents of a file are read/written using the vnode for the file
83 1.1 christos * (including directories when they are read/written as files). This
84 1.1 christos * presents problems for the dos filesystem because data that should be in
85 1.7 christos * an inode (if dos had them) resides in the directory itself. Since we
86 1.1 christos * must update directory entries without the benefit of having the vnode
87 1.7 christos * for the directory we must use the vnode for the filesystem. This means
88 1.1 christos * that when a directory is actually read/written (via read, write, or
89 1.1 christos * readdir, or seek) we must use the vnode for the filesystem instead of
90 1.1 christos * the vnode for the directory as would happen in ufs. This is to insure we
91 1.1 christos * retrieve the correct block from the buffer cache since the hash value is
92 1.1 christos * based upon the vnode address and the desired block number.
93 1.1 christos */
94 1.1 christos
95 1.1 christos static int msdosfs_wfile(const char *, struct denode *, fsnode *);
96 1.1 christos
97 1.1 christos static void
98 1.1 christos msdosfs_times(struct msdosfsmount *pmp, struct denode *dep,
99 1.1 christos const struct stat *st)
100 1.1 christos {
101 1.18 christos struct timespec at;
102 1.18 christos struct timespec mt;
103 1.18 christos
104 1.22 riastrad if (stampst.st_ino)
105 1.18 christos st = &stampst;
106 1.18 christos
107 1.4 christos #ifndef HAVE_NBTOOL_CONFIG_H
108 1.18 christos at = st->st_atimespec;
109 1.18 christos mt = st->st_mtimespec;
110 1.3 christos #else
111 1.18 christos at.tv_sec = st->st_atime;
112 1.18 christos at.tv_nsec = 0;
113 1.18 christos mt.tv_sec = st->st_mtime;
114 1.18 christos mt.tv_nsec = 0;
115 1.3 christos #endif
116 1.21 thorpej msdosfs_unix2dostime(&at, pmp->pm_gmtoff, &dep->de_ADate,
117 1.21 thorpej NULL, NULL);
118 1.21 thorpej msdosfs_unix2dostime(&mt, pmp->pm_gmtoff, &dep->de_MDate,
119 1.21 thorpej &dep->de_MTime, NULL);
120 1.1 christos }
121 1.1 christos
122 1.1 christos /*
123 1.7 christos * When we search a directory the blocks containing directory entries are
124 1.7 christos * read and examined. The directory entries contain information that would
125 1.7 christos * normally be in the inode of a unix filesystem. This means that some of
126 1.7 christos * a directory's contents may also be in memory resident denodes (sort of
127 1.7 christos * an inode). This can cause problems if we are searching while some other
128 1.7 christos * process is modifying a directory. To prevent one process from accessing
129 1.7 christos * incompletely modified directory information we depend upon being the
130 1.7 christos * sole owner of a directory block. bread/brelse provide this service.
131 1.7 christos * This being the case, when a process modifies a directory it must first
132 1.7 christos * acquire the disk block that contains the directory entry to be modified.
133 1.7 christos * Then update the disk block and the denode, and then write the disk block
134 1.7 christos * out to disk. This way disk blocks containing directory entries and in
135 1.7 christos * memory denode's will be in synch.
136 1.7 christos */
137 1.7 christos static int
138 1.22 riastrad msdosfs_findslot(struct denode *dp, struct componentname *cnp)
139 1.7 christos {
140 1.7 christos daddr_t bn;
141 1.7 christos int error;
142 1.7 christos int slotcount;
143 1.7 christos int slotoffset = 0;
144 1.7 christos int frcn;
145 1.7 christos u_long cluster;
146 1.7 christos int blkoff;
147 1.7 christos u_int diroff;
148 1.7 christos int blsize;
149 1.7 christos struct msdosfsmount *pmp;
150 1.7 christos struct buf *bp = 0;
151 1.7 christos struct direntry *dep;
152 1.7 christos u_char dosfilename[12];
153 1.7 christos int wincnt = 1;
154 1.7 christos int chksum = -1, chksum_ok;
155 1.7 christos int olddos = 1;
156 1.7 christos
157 1.7 christos pmp = dp->de_pmp;
158 1.12 christos
159 1.21 thorpej switch (msdosfs_unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
160 1.7 christos cnp->cn_namelen, 0)) {
161 1.7 christos case 0:
162 1.7 christos return (EINVAL);
163 1.7 christos case 1:
164 1.7 christos break;
165 1.7 christos case 2:
166 1.21 thorpej wincnt = msdosfs_winSlotCnt((const u_char *)cnp->cn_nameptr,
167 1.17 mlelstv cnp->cn_namelen, pmp->pm_flags & MSDOSFSMNT_UTF8) + 1;
168 1.7 christos break;
169 1.7 christos case 3:
170 1.7 christos olddos = 0;
171 1.21 thorpej wincnt = msdosfs_winSlotCnt((const u_char *)cnp->cn_nameptr,
172 1.17 mlelstv cnp->cn_namelen, pmp->pm_flags & MSDOSFSMNT_UTF8) + 1;
173 1.7 christos break;
174 1.7 christos }
175 1.7 christos
176 1.7 christos if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
177 1.7 christos wincnt = 1;
178 1.7 christos
179 1.7 christos /*
180 1.7 christos * Suppress search for slots unless creating
181 1.7 christos * file and at end of pathname, in which case
182 1.7 christos * we watch for a place to put the new file in
183 1.7 christos * case it doesn't already exist.
184 1.7 christos */
185 1.7 christos slotcount = 0;
186 1.9 christos DPRINTF(("%s(): dos filename: %s\n", __func__, dosfilename));
187 1.7 christos /*
188 1.7 christos * Search the directory pointed at by vdp for the name pointed at
189 1.7 christos * by cnp->cn_nameptr.
190 1.7 christos */
191 1.7 christos /*
192 1.7 christos * The outer loop ranges over the clusters that make up the
193 1.7 christos * directory. Note that the root directory is different from all
194 1.7 christos * other directories. It has a fixed number of blocks that are not
195 1.7 christos * part of the pool of allocatable clusters. So, we treat it a
196 1.7 christos * little differently. The root directory starts at "cluster" 0.
197 1.7 christos */
198 1.7 christos diroff = 0;
199 1.7 christos for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
200 1.21 thorpej if ((error = msdosfs_pcbmap(dp, frcn, &bn, &cluster,
201 1.21 thorpej &blsize)) != 0) {
202 1.7 christos if (error == E2BIG)
203 1.7 christos break;
204 1.7 christos return (error);
205 1.7 christos }
206 1.16 agc error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
207 1.7 christos 0, &bp);
208 1.7 christos if (error) {
209 1.7 christos return (error);
210 1.7 christos }
211 1.7 christos for (blkoff = 0; blkoff < blsize;
212 1.7 christos blkoff += sizeof(struct direntry),
213 1.7 christos diroff += sizeof(struct direntry)) {
214 1.7 christos dep = (struct direntry *)((char *)bp->b_data + blkoff);
215 1.7 christos /*
216 1.7 christos * If the slot is empty and we are still looking
217 1.7 christos * for an empty then remember this one. If the
218 1.7 christos * slot is not empty then check to see if it
219 1.7 christos * matches what we are looking for. If the slot
220 1.7 christos * has never been filled with anything, then the
221 1.7 christos * remainder of the directory has never been used,
222 1.7 christos * so there is no point in searching it.
223 1.7 christos */
224 1.7 christos if (dep->deName[0] == SLOT_EMPTY ||
225 1.7 christos dep->deName[0] == SLOT_DELETED) {
226 1.7 christos /*
227 1.7 christos * Drop memory of previous long matches
228 1.7 christos */
229 1.7 christos chksum = -1;
230 1.7 christos
231 1.7 christos if (slotcount < wincnt) {
232 1.7 christos slotcount++;
233 1.7 christos slotoffset = diroff;
234 1.7 christos }
235 1.7 christos if (dep->deName[0] == SLOT_EMPTY) {
236 1.7 christos brelse(bp, 0);
237 1.7 christos goto notfound;
238 1.7 christos }
239 1.7 christos } else {
240 1.7 christos /*
241 1.7 christos * If there wasn't enough space for our
242 1.7 christos * winentries, forget about the empty space
243 1.7 christos */
244 1.7 christos if (slotcount < wincnt)
245 1.7 christos slotcount = 0;
246 1.7 christos
247 1.7 christos /*
248 1.7 christos * Check for Win95 long filename entry
249 1.7 christos */
250 1.7 christos if (dep->deAttributes == ATTR_WIN95) {
251 1.7 christos if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
252 1.7 christos continue;
253 1.7 christos
254 1.21 thorpej chksum = msdosfs_winChkName((const u_char *)cnp->cn_nameptr,
255 1.7 christos cnp->cn_namelen,
256 1.7 christos (struct winentry *)dep,
257 1.17 mlelstv chksum,
258 1.17 mlelstv pmp->pm_flags & MSDOSFSMNT_UTF8);
259 1.7 christos continue;
260 1.7 christos }
261 1.7 christos
262 1.7 christos /*
263 1.7 christos * Ignore volume labels (anywhere, not just
264 1.7 christos * the root directory).
265 1.7 christos */
266 1.7 christos if (dep->deAttributes & ATTR_VOLUME) {
267 1.7 christos chksum = -1;
268 1.7 christos continue;
269 1.7 christos }
270 1.7 christos
271 1.7 christos /*
272 1.7 christos * Check for a checksum or name match
273 1.7 christos */
274 1.21 thorpej chksum_ok =
275 1.21 thorpej (chksum == msdosfs_winChksum(dep->deName));
276 1.7 christos if (!chksum_ok
277 1.7 christos && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
278 1.7 christos chksum = -1;
279 1.7 christos continue;
280 1.7 christos }
281 1.9 christos DPRINTF(("%s(): match blkoff %d, diroff %d\n",
282 1.9 christos __func__, blkoff, diroff));
283 1.7 christos /*
284 1.7 christos * Remember where this directory
285 1.7 christos * entry came from for whoever did
286 1.7 christos * this lookup.
287 1.7 christos */
288 1.20 hannken dp->de_crap.mlr_fndoffset = diroff;
289 1.20 hannken dp->de_crap.mlr_fndcnt = 0;
290 1.7 christos
291 1.7 christos return EEXIST;
292 1.7 christos }
293 1.7 christos } /* for (blkoff = 0; .... */
294 1.7 christos /*
295 1.7 christos * Release the buffer holding the directory cluster just
296 1.7 christos * searched.
297 1.7 christos */
298 1.7 christos brelse(bp, 0);
299 1.7 christos } /* for (frcn = 0; ; frcn++) */
300 1.7 christos
301 1.7 christos notfound:
302 1.7 christos /*
303 1.7 christos * We hold no disk buffers at this point.
304 1.7 christos */
305 1.7 christos
306 1.7 christos /*
307 1.7 christos * If we get here we didn't find the entry we were looking for. But
308 1.7 christos * that's ok if we are creating or renaming and are at the end of
309 1.7 christos * the pathname and the directory hasn't been removed.
310 1.7 christos */
311 1.9 christos DPRINTF(("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
312 1.9 christos __func__, dp->de_refcnt, slotcount, slotoffset));
313 1.7 christos /*
314 1.7 christos * Fixup the slot description to point to the place where
315 1.7 christos * we might put the new DOS direntry (putting the Win95
316 1.7 christos * long name entries before that)
317 1.7 christos */
318 1.7 christos if (!slotcount) {
319 1.7 christos slotcount = 1;
320 1.7 christos slotoffset = diroff;
321 1.7 christos }
322 1.7 christos if (wincnt > slotcount) {
323 1.7 christos slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
324 1.7 christos }
325 1.7 christos
326 1.7 christos /*
327 1.7 christos * Return an indication of where the new directory
328 1.7 christos * entry should be put.
329 1.7 christos */
330 1.20 hannken dp->de_crap.mlr_fndoffset = slotoffset;
331 1.20 hannken dp->de_crap.mlr_fndcnt = wincnt - 1;
332 1.7 christos
333 1.7 christos /*
334 1.7 christos * We return with the directory locked, so that
335 1.7 christos * the parameters we set up above will still be
336 1.7 christos * valid if we actually decide to do a direnter().
337 1.7 christos * We return ni_vp == NULL to indicate that the entry
338 1.7 christos * does not currently exist; we leave a pointer to
339 1.7 christos * the (locked) directory inode in ndp->ni_dvp.
340 1.7 christos *
341 1.7 christos * NB - if the directory is unlocked, then this
342 1.7 christos * information cannot be used.
343 1.7 christos */
344 1.7 christos return 0;
345 1.7 christos }
346 1.7 christos
347 1.7 christos /*
348 1.1 christos * Create a regular file. On entry the directory to contain the file being
349 1.1 christos * created is locked. We must release before we return.
350 1.1 christos */
351 1.1 christos struct denode *
352 1.1 christos msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
353 1.1 christos {
354 1.1 christos struct componentname cn;
355 1.1 christos struct denode ndirent;
356 1.1 christos struct denode *dep;
357 1.1 christos int error;
358 1.1 christos struct stat *st = &node->inode->st;
359 1.1 christos struct msdosfsmount *pmp = pdep->de_pmp;
360 1.1 christos
361 1.1 christos cn.cn_nameptr = node->name;
362 1.1 christos cn.cn_namelen = strlen(node->name);
363 1.1 christos
364 1.9 christos DPRINTF(("%s(name %s, mode 0%o size %zu)\n", __func__, node->name,
365 1.9 christos st->st_mode, (size_t)st->st_size));
366 1.1 christos
367 1.1 christos /*
368 1.1 christos * If this is the root directory and there is no space left we
369 1.1 christos * can't do anything. This is because the root directory can not
370 1.1 christos * change size.
371 1.1 christos */
372 1.1 christos if (pdep->de_StartCluster == MSDOSFSROOT
373 1.20 hannken && pdep->de_crap.mlr_fndoffset >= pdep->de_FileSize) {
374 1.1 christos error = ENOSPC;
375 1.1 christos goto bad;
376 1.1 christos }
377 1.1 christos
378 1.1 christos /*
379 1.1 christos * Create a directory entry for the file, then call createde() to
380 1.1 christos * have it installed. NOTE: DOS files are always executable. We
381 1.1 christos * use the absence of the owner write bit to make the file
382 1.1 christos * readonly.
383 1.1 christos */
384 1.1 christos memset(&ndirent, 0, sizeof(ndirent));
385 1.21 thorpej if ((error = msdosfs_uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
386 1.1 christos goto bad;
387 1.1 christos
388 1.1 christos ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
389 1.1 christos ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
390 1.1 christos ndirent.de_StartCluster = 0;
391 1.1 christos ndirent.de_FileSize = 0;
392 1.1 christos ndirent.de_dev = pdep->de_dev;
393 1.1 christos ndirent.de_devvp = pdep->de_devvp;
394 1.1 christos ndirent.de_pmp = pdep->de_pmp;
395 1.1 christos ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
396 1.1 christos msdosfs_times(pmp, &ndirent, st);
397 1.7 christos if ((error = msdosfs_findslot(pdep, &cn)) != 0)
398 1.7 christos goto bad;
399 1.21 thorpej if ((error = msdosfs_createde(&ndirent, pdep, &pdep->de_crap, &dep,
400 1.21 thorpej &cn)) != 0)
401 1.1 christos goto bad;
402 1.7 christos if ((error = msdosfs_wfile(path, dep, node)) != 0)
403 1.1 christos goto bad;
404 1.1 christos return dep;
405 1.1 christos
406 1.1 christos bad:
407 1.1 christos errno = error;
408 1.1 christos return NULL;
409 1.1 christos }
410 1.9 christos static int
411 1.9 christos msdosfs_updatede(struct denode *dep)
412 1.9 christos {
413 1.9 christos struct buf *bp;
414 1.9 christos struct direntry *dirp;
415 1.9 christos int error;
416 1.9 christos
417 1.9 christos dep->de_flag &= ~DE_MODIFIED;
418 1.21 thorpej error = msdosfs_readde(dep, &bp, &dirp);
419 1.9 christos if (error)
420 1.9 christos return error;
421 1.9 christos DE_EXTERNALIZE(dirp, dep);
422 1.9 christos error = bwrite(bp);
423 1.9 christos return error;
424 1.9 christos }
425 1.1 christos
426 1.1 christos /*
427 1.1 christos * Write data to a file or directory.
428 1.1 christos */
429 1.1 christos static int
430 1.1 christos msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
431 1.1 christos {
432 1.1 christos int error, fd;
433 1.1 christos size_t osize = dep->de_FileSize;
434 1.1 christos struct stat *st = &node->inode->st;
435 1.11 christos size_t nsize, offs;
436 1.1 christos struct msdosfsmount *pmp = dep->de_pmp;
437 1.1 christos struct buf *bp;
438 1.1 christos char *dat;
439 1.13 christos u_long cn = 0;
440 1.1 christos
441 1.14 christos error = 0; /* XXX: gcc/vax */
442 1.9 christos DPRINTF(("%s(diroff %lu, dirclust %lu, startcluster %lu)\n", __func__,
443 1.9 christos dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster));
444 1.6 martin if (st->st_size == 0)
445 1.1 christos return 0;
446 1.1 christos
447 1.1 christos /* Don't bother to try to write files larger than the fs limit */
448 1.19 christos if (st->st_size > MSDOSFS_FILESIZE_MAX)
449 1.19 christos return EFBIG;
450 1.1 christos
451 1.6 martin nsize = st->st_size;
452 1.9 christos DPRINTF(("%s(nsize=%zu, osize=%zu)\n", __func__, nsize, osize));
453 1.1 christos if (nsize > osize) {
454 1.21 thorpej if ((error = msdosfs_deextend(dep, nsize, NULL)) != 0)
455 1.19 christos return error;
456 1.19 christos if ((error = msdosfs_updatede(dep)) != 0)
457 1.19 christos return error;
458 1.1 christos }
459 1.1 christos
460 1.19 christos if ((fd = open(path, O_RDONLY)) == -1) {
461 1.19 christos error = errno;
462 1.19 christos DPRINTF((1, "open %s: %s", path, strerror(error)));
463 1.19 christos return error;
464 1.19 christos }
465 1.1 christos
466 1.7 christos if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
467 1.7 christos == MAP_FAILED) {
468 1.19 christos error = errno;
469 1.19 christos DPRINTF(("%s: mmap %s: %s", __func__, node->name,
470 1.19 christos strerror(error)));
471 1.9 christos close(fd);
472 1.7 christos goto out;
473 1.7 christos }
474 1.1 christos close(fd);
475 1.1 christos
476 1.11 christos for (offs = 0; offs < nsize;) {
477 1.1 christos int blsize, cpsize;
478 1.1 christos daddr_t bn;
479 1.9 christos u_long on = offs & pmp->pm_crbomask;
480 1.13 christos #ifdef HACK
481 1.13 christos cn = dep->de_StartCluster;
482 1.13 christos if (cn == MSDOSFSROOT) {
483 1.13 christos DPRINTF(("%s: bad lbn %lu", __func__, cn));
484 1.19 christos error = EINVAL;
485 1.1 christos goto out;
486 1.1 christos }
487 1.13 christos bn = cntobn(pmp, cn);
488 1.9 christos blsize = pmp->pm_bpcluster;
489 1.13 christos #else
490 1.21 thorpej if ((error = msdosfs_pcbmap(dep, cn++, &bn, NULL,
491 1.21 thorpej &blsize)) != 0) {
492 1.13 christos DPRINTF(("%s: pcbmap %lu", __func__, bn));
493 1.13 christos goto out;
494 1.13 christos }
495 1.13 christos #endif
496 1.13 christos DPRINTF(("%s(cn=%lu, bn=%llu/%llu, blsize=%d)\n", __func__,
497 1.13 christos cn, (unsigned long long)bn,
498 1.9 christos (unsigned long long)de_bn2kb(pmp, bn), blsize));
499 1.7 christos if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
500 1.16 agc 0, &bp)) != 0) {
501 1.9 christos DPRINTF(("bread %d\n", error));
502 1.7 christos goto out;
503 1.22 riastrad }
504 1.9 christos cpsize = MIN((nsize - offs), blsize - on);
505 1.9 christos memcpy((char *)bp->b_data + on, dat + offs, cpsize);
506 1.1 christos bwrite(bp);
507 1.9 christos offs += cpsize;
508 1.1 christos }
509 1.1 christos
510 1.1 christos munmap(dat, nsize);
511 1.1 christos return 0;
512 1.1 christos out:
513 1.1 christos munmap(dat, nsize);
514 1.7 christos return error;
515 1.1 christos }
516 1.1 christos
517 1.1 christos
518 1.1 christos static const struct {
519 1.1 christos struct direntry dot;
520 1.1 christos struct direntry dotdot;
521 1.1 christos } dosdirtemplate = {
522 1.12 christos { ". ", " ", /* the . entry */
523 1.1 christos ATTR_DIRECTORY, /* file attribute */
524 1.7 christos 0, /* reserved */
525 1.1 christos 0, { 0, 0 }, { 0, 0 }, /* create time & date */
526 1.1 christos { 0, 0 }, /* access date */
527 1.1 christos { 0, 0 }, /* high bits of start cluster */
528 1.1 christos { 210, 4 }, { 210, 4 }, /* modify time & date */
529 1.1 christos { 0, 0 }, /* startcluster */
530 1.7 christos { 0, 0, 0, 0 } /* filesize */
531 1.1 christos },
532 1.12 christos { ".. ", " ", /* the .. entry */
533 1.1 christos ATTR_DIRECTORY, /* file attribute */
534 1.7 christos 0, /* reserved */
535 1.1 christos 0, { 0, 0 }, { 0, 0 }, /* create time & date */
536 1.1 christos { 0, 0 }, /* access date */
537 1.1 christos { 0, 0 }, /* high bits of start cluster */
538 1.1 christos { 210, 4 }, { 210, 4 }, /* modify time & date */
539 1.1 christos { 0, 0 }, /* startcluster */
540 1.1 christos { 0, 0, 0, 0 } /* filesize */
541 1.1 christos }
542 1.1 christos };
543 1.1 christos
544 1.1 christos struct denode *
545 1.1 christos msdosfs_mkdire(const char *path, struct denode *pdep, fsnode *node) {
546 1.1 christos struct denode ndirent;
547 1.1 christos struct denode *dep;
548 1.1 christos struct componentname cn;
549 1.1 christos struct stat *st = &node->inode->st;
550 1.1 christos struct msdosfsmount *pmp = pdep->de_pmp;
551 1.1 christos int error;
552 1.1 christos u_long newcluster, pcl, bn;
553 1.1 christos daddr_t lbn;
554 1.1 christos struct direntry *denp;
555 1.1 christos struct buf *bp;
556 1.1 christos
557 1.1 christos cn.cn_nameptr = node->name;
558 1.1 christos cn.cn_namelen = strlen(node->name);
559 1.1 christos /*
560 1.1 christos * If this is the root directory and there is no space left we
561 1.1 christos * can't do anything. This is because the root directory can not
562 1.1 christos * change size.
563 1.1 christos */
564 1.1 christos if (pdep->de_StartCluster == MSDOSFSROOT
565 1.20 hannken && pdep->de_crap.mlr_fndoffset >= pdep->de_FileSize) {
566 1.1 christos error = ENOSPC;
567 1.1 christos goto bad2;
568 1.1 christos }
569 1.1 christos
570 1.1 christos /*
571 1.1 christos * Allocate a cluster to hold the about to be created directory.
572 1.1 christos */
573 1.21 thorpej error = msdosfs_clusteralloc(pmp, 0, 1, &newcluster, NULL);
574 1.1 christos if (error)
575 1.1 christos goto bad2;
576 1.1 christos
577 1.1 christos memset(&ndirent, 0, sizeof(ndirent));
578 1.1 christos ndirent.de_pmp = pmp;
579 1.1 christos ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
580 1.1 christos msdosfs_times(pmp, &ndirent, st);
581 1.1 christos
582 1.1 christos /*
583 1.1 christos * Now fill the cluster with the "." and ".." entries. And write
584 1.7 christos * the cluster to disk. This way it is there for the parent
585 1.1 christos * directory to be pointing at if there were a crash.
586 1.1 christos */
587 1.1 christos bn = cntobn(pmp, newcluster);
588 1.1 christos lbn = de_bn2kb(pmp, bn);
589 1.10 christos DPRINTF(("%s(newcluster %lu, bn=%lu, lbn=%lu)\n", __func__, newcluster,
590 1.10 christos bn, lbn));
591 1.1 christos /* always succeeds */
592 1.1 christos bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
593 1.1 christos memset(bp->b_data, 0, pmp->pm_bpcluster);
594 1.1 christos memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
595 1.1 christos denp = (struct direntry *)bp->b_data;
596 1.1 christos putushort(denp[0].deStartCluster, newcluster);
597 1.1 christos putushort(denp[0].deCDate, ndirent.de_CDate);
598 1.1 christos putushort(denp[0].deCTime, ndirent.de_CTime);
599 1.1 christos denp[0].deCHundredth = ndirent.de_CHun;
600 1.1 christos putushort(denp[0].deADate, ndirent.de_ADate);
601 1.1 christos putushort(denp[0].deMDate, ndirent.de_MDate);
602 1.1 christos putushort(denp[0].deMTime, ndirent.de_MTime);
603 1.1 christos pcl = pdep->de_StartCluster;
604 1.12 christos DPRINTF(("%s(pcl %lu, rootdirblk=%lu)\n", __func__, pcl,
605 1.12 christos pmp->pm_rootdirblk));
606 1.1 christos if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
607 1.1 christos pcl = 0;
608 1.1 christos putushort(denp[1].deStartCluster, pcl);
609 1.1 christos putushort(denp[1].deCDate, ndirent.de_CDate);
610 1.1 christos putushort(denp[1].deCTime, ndirent.de_CTime);
611 1.1 christos denp[1].deCHundredth = ndirent.de_CHun;
612 1.1 christos putushort(denp[1].deADate, ndirent.de_ADate);
613 1.1 christos putushort(denp[1].deMDate, ndirent.de_MDate);
614 1.1 christos putushort(denp[1].deMTime, ndirent.de_MTime);
615 1.1 christos if (FAT32(pmp)) {
616 1.1 christos putushort(denp[0].deHighClust, newcluster >> 16);
617 1.1 christos putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
618 1.1 christos } else {
619 1.1 christos putushort(denp[0].deHighClust, 0);
620 1.1 christos putushort(denp[1].deHighClust, 0);
621 1.1 christos }
622 1.1 christos
623 1.1 christos if ((error = bwrite(bp)) != 0)
624 1.1 christos goto bad;
625 1.1 christos
626 1.1 christos /*
627 1.1 christos * Now build up a directory entry pointing to the newly allocated
628 1.1 christos * cluster. This will be written to an empty slot in the parent
629 1.1 christos * directory.
630 1.1 christos */
631 1.21 thorpej if ((error = msdosfs_uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
632 1.1 christos goto bad;
633 1.1 christos
634 1.1 christos ndirent.de_Attributes = ATTR_DIRECTORY;
635 1.1 christos ndirent.de_StartCluster = newcluster;
636 1.1 christos ndirent.de_FileSize = 0;
637 1.1 christos ndirent.de_dev = pdep->de_dev;
638 1.1 christos ndirent.de_devvp = pdep->de_devvp;
639 1.12 christos ndirent.de_pmp = pdep->de_pmp;
640 1.7 christos if ((error = msdosfs_findslot(pdep, &cn)) != 0)
641 1.7 christos goto bad;
642 1.21 thorpej if ((error = msdosfs_createde(&ndirent, pdep, &pdep->de_crap, &dep,
643 1.21 thorpej &cn)) != 0)
644 1.1 christos goto bad;
645 1.10 christos if ((error = msdosfs_updatede(dep)) != 0)
646 1.10 christos goto bad;
647 1.1 christos return dep;
648 1.1 christos
649 1.1 christos bad:
650 1.21 thorpej msdosfs_clusterfree(pmp, newcluster, NULL);
651 1.1 christos bad2:
652 1.1 christos errno = error;
653 1.1 christos return NULL;
654 1.1 christos }
655