msdosfs_vnops.c revision 1.8 1 1.8 christos /* $NetBSD: msdosfs_vnops.c,v 1.8 2013/01/27 16:03:15 christos 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.8 christos __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.8 2013/01/27 16:03:15 christos 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 <sys/mount.h>
59 1.1 christos #include <fcntl.h>
60 1.1 christos #include <unistd.h>
61 1.1 christos
62 1.1 christos #include <ffs/buf.h>
63 1.1 christos
64 1.1 christos #include <fs/msdosfs/bpb.h>
65 1.1 christos #include <fs/msdosfs/direntry.h>
66 1.1 christos #include <fs/msdosfs/denode.h>
67 1.1 christos #include <fs/msdosfs/msdosfsmount.h>
68 1.1 christos #include <fs/msdosfs/fat.h>
69 1.1 christos
70 1.1 christos #include "makefs.h"
71 1.1 christos #include "msdos.h"
72 1.1 christos
73 1.1 christos /*
74 1.1 christos * Some general notes:
75 1.1 christos *
76 1.1 christos * In the ufs filesystem the inodes, superblocks, and indirect blocks are
77 1.1 christos * read/written using the vnode for the filesystem. Blocks that represent
78 1.1 christos * the contents of a file are read/written using the vnode for the file
79 1.1 christos * (including directories when they are read/written as files). This
80 1.1 christos * presents problems for the dos filesystem because data that should be in
81 1.7 christos * an inode (if dos had them) resides in the directory itself. Since we
82 1.1 christos * must update directory entries without the benefit of having the vnode
83 1.7 christos * for the directory we must use the vnode for the filesystem. This means
84 1.1 christos * that when a directory is actually read/written (via read, write, or
85 1.1 christos * readdir, or seek) we must use the vnode for the filesystem instead of
86 1.1 christos * the vnode for the directory as would happen in ufs. This is to insure we
87 1.1 christos * retrieve the correct block from the buffer cache since the hash value is
88 1.1 christos * based upon the vnode address and the desired block number.
89 1.1 christos */
90 1.1 christos
91 1.1 christos static int msdosfs_wfile(const char *, struct denode *, fsnode *);
92 1.1 christos
93 1.1 christos static void
94 1.1 christos msdosfs_times(struct msdosfsmount *pmp, struct denode *dep,
95 1.1 christos const struct stat *st)
96 1.1 christos {
97 1.4 christos #ifndef HAVE_NBTOOL_CONFIG_H
98 1.3 christos struct timespec at = st->st_atimespec;
99 1.3 christos struct timespec mt = st->st_mtimespec;
100 1.3 christos #else
101 1.3 christos struct timespec at = { st->st_atime, 0 };
102 1.3 christos struct timespec mt = { st->st_mtime, 0 };
103 1.3 christos #endif
104 1.3 christos unix2dostime(&at, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
105 1.3 christos unix2dostime(&mt, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
106 1.1 christos }
107 1.1 christos
108 1.1 christos /*
109 1.7 christos * When we search a directory the blocks containing directory entries are
110 1.7 christos * read and examined. The directory entries contain information that would
111 1.7 christos * normally be in the inode of a unix filesystem. This means that some of
112 1.7 christos * a directory's contents may also be in memory resident denodes (sort of
113 1.7 christos * an inode). This can cause problems if we are searching while some other
114 1.7 christos * process is modifying a directory. To prevent one process from accessing
115 1.7 christos * incompletely modified directory information we depend upon being the
116 1.7 christos * sole owner of a directory block. bread/brelse provide this service.
117 1.7 christos * This being the case, when a process modifies a directory it must first
118 1.7 christos * acquire the disk block that contains the directory entry to be modified.
119 1.7 christos * Then update the disk block and the denode, and then write the disk block
120 1.7 christos * out to disk. This way disk blocks containing directory entries and in
121 1.7 christos * memory denode's will be in synch.
122 1.7 christos */
123 1.7 christos static int
124 1.7 christos msdosfs_findslot(struct denode *dp, struct componentname *cnp)
125 1.7 christos {
126 1.7 christos daddr_t bn;
127 1.7 christos int error;
128 1.7 christos int slotcount;
129 1.7 christos int slotoffset = 0;
130 1.7 christos int frcn;
131 1.7 christos u_long cluster;
132 1.7 christos int blkoff;
133 1.7 christos u_int diroff;
134 1.7 christos int blsize;
135 1.7 christos struct denode *tdp;
136 1.7 christos struct msdosfsmount *pmp;
137 1.7 christos struct buf *bp = 0;
138 1.7 christos struct direntry *dep;
139 1.7 christos u_char dosfilename[12];
140 1.7 christos int wincnt = 1;
141 1.7 christos int chksum = -1, chksum_ok;
142 1.7 christos int olddos = 1;
143 1.7 christos
144 1.7 christos pmp = dp->de_pmp;
145 1.7 christos switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
146 1.7 christos cnp->cn_namelen, 0)) {
147 1.7 christos case 0:
148 1.7 christos return (EINVAL);
149 1.7 christos case 1:
150 1.7 christos break;
151 1.7 christos case 2:
152 1.7 christos wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
153 1.7 christos cnp->cn_namelen) + 1;
154 1.7 christos break;
155 1.7 christos case 3:
156 1.7 christos olddos = 0;
157 1.7 christos wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
158 1.7 christos cnp->cn_namelen) + 1;
159 1.7 christos break;
160 1.7 christos }
161 1.7 christos
162 1.7 christos if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
163 1.7 christos wincnt = 1;
164 1.7 christos
165 1.7 christos /*
166 1.7 christos * Suppress search for slots unless creating
167 1.7 christos * file and at end of pathname, in which case
168 1.7 christos * we watch for a place to put the new file in
169 1.7 christos * case it doesn't already exist.
170 1.7 christos */
171 1.7 christos slotcount = 0;
172 1.7 christos #ifdef MSDOSFS_DEBUG
173 1.7 christos printf("%s(): dos filename: %s\n", __func__, dosfilename);
174 1.7 christos #endif
175 1.7 christos /*
176 1.7 christos * Search the directory pointed at by vdp for the name pointed at
177 1.7 christos * by cnp->cn_nameptr.
178 1.7 christos */
179 1.7 christos tdp = NULL;
180 1.7 christos /*
181 1.7 christos * The outer loop ranges over the clusters that make up the
182 1.7 christos * directory. Note that the root directory is different from all
183 1.7 christos * other directories. It has a fixed number of blocks that are not
184 1.7 christos * part of the pool of allocatable clusters. So, we treat it a
185 1.7 christos * little differently. The root directory starts at "cluster" 0.
186 1.7 christos */
187 1.7 christos diroff = 0;
188 1.7 christos for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
189 1.7 christos if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
190 1.7 christos if (error == E2BIG)
191 1.7 christos break;
192 1.7 christos return (error);
193 1.7 christos }
194 1.7 christos error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
195 1.7 christos 0, &bp);
196 1.7 christos if (error) {
197 1.7 christos return (error);
198 1.7 christos }
199 1.7 christos for (blkoff = 0; blkoff < blsize;
200 1.7 christos blkoff += sizeof(struct direntry),
201 1.7 christos diroff += sizeof(struct direntry)) {
202 1.7 christos dep = (struct direntry *)((char *)bp->b_data + blkoff);
203 1.7 christos /*
204 1.7 christos * If the slot is empty and we are still looking
205 1.7 christos * for an empty then remember this one. If the
206 1.7 christos * slot is not empty then check to see if it
207 1.7 christos * matches what we are looking for. If the slot
208 1.7 christos * has never been filled with anything, then the
209 1.7 christos * remainder of the directory has never been used,
210 1.7 christos * so there is no point in searching it.
211 1.7 christos */
212 1.7 christos if (dep->deName[0] == SLOT_EMPTY ||
213 1.7 christos dep->deName[0] == SLOT_DELETED) {
214 1.7 christos /*
215 1.7 christos * Drop memory of previous long matches
216 1.7 christos */
217 1.7 christos chksum = -1;
218 1.7 christos
219 1.7 christos if (slotcount < wincnt) {
220 1.7 christos slotcount++;
221 1.7 christos slotoffset = diroff;
222 1.7 christos }
223 1.7 christos if (dep->deName[0] == SLOT_EMPTY) {
224 1.7 christos brelse(bp, 0);
225 1.7 christos goto notfound;
226 1.7 christos }
227 1.7 christos } else {
228 1.7 christos /*
229 1.7 christos * If there wasn't enough space for our
230 1.7 christos * winentries, forget about the empty space
231 1.7 christos */
232 1.7 christos if (slotcount < wincnt)
233 1.7 christos slotcount = 0;
234 1.7 christos
235 1.7 christos /*
236 1.7 christos * Check for Win95 long filename entry
237 1.7 christos */
238 1.7 christos if (dep->deAttributes == ATTR_WIN95) {
239 1.7 christos if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
240 1.7 christos continue;
241 1.7 christos
242 1.7 christos chksum = winChkName((const u_char *)cnp->cn_nameptr,
243 1.7 christos cnp->cn_namelen,
244 1.7 christos (struct winentry *)dep,
245 1.7 christos chksum);
246 1.7 christos continue;
247 1.7 christos }
248 1.7 christos
249 1.7 christos /*
250 1.7 christos * Ignore volume labels (anywhere, not just
251 1.7 christos * the root directory).
252 1.7 christos */
253 1.7 christos if (dep->deAttributes & ATTR_VOLUME) {
254 1.7 christos chksum = -1;
255 1.7 christos continue;
256 1.7 christos }
257 1.7 christos
258 1.7 christos /*
259 1.7 christos * Check for a checksum or name match
260 1.7 christos */
261 1.7 christos chksum_ok = (chksum == winChksum(dep->deName));
262 1.7 christos if (!chksum_ok
263 1.7 christos && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
264 1.7 christos chksum = -1;
265 1.7 christos continue;
266 1.7 christos }
267 1.7 christos #ifdef MSDOSFS_DEBUG
268 1.7 christos printf("%s(): match blkoff %d, diroff %d\n",
269 1.7 christos __func__, blkoff, diroff);
270 1.7 christos #endif
271 1.7 christos /*
272 1.7 christos * Remember where this directory
273 1.7 christos * entry came from for whoever did
274 1.7 christos * this lookup.
275 1.7 christos */
276 1.7 christos dp->de_fndoffset = diroff;
277 1.7 christos dp->de_fndcnt = 0;
278 1.7 christos
279 1.7 christos return EEXIST;
280 1.7 christos }
281 1.7 christos } /* for (blkoff = 0; .... */
282 1.7 christos /*
283 1.7 christos * Release the buffer holding the directory cluster just
284 1.7 christos * searched.
285 1.7 christos */
286 1.7 christos brelse(bp, 0);
287 1.7 christos } /* for (frcn = 0; ; frcn++) */
288 1.7 christos
289 1.7 christos notfound:
290 1.7 christos /*
291 1.7 christos * We hold no disk buffers at this point.
292 1.7 christos */
293 1.7 christos
294 1.7 christos /*
295 1.7 christos * If we get here we didn't find the entry we were looking for. But
296 1.7 christos * that's ok if we are creating or renaming and are at the end of
297 1.7 christos * the pathname and the directory hasn't been removed.
298 1.7 christos */
299 1.7 christos #ifdef MSDOSFS_DEBUG
300 1.7 christos printf("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
301 1.7 christos __func__, dp->de_refcnt, slotcount, slotoffset);
302 1.7 christos #endif
303 1.7 christos /*
304 1.7 christos * Fixup the slot description to point to the place where
305 1.7 christos * we might put the new DOS direntry (putting the Win95
306 1.7 christos * long name entries before that)
307 1.7 christos */
308 1.7 christos if (!slotcount) {
309 1.7 christos slotcount = 1;
310 1.7 christos slotoffset = diroff;
311 1.7 christos }
312 1.7 christos if (wincnt > slotcount) {
313 1.7 christos slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
314 1.7 christos }
315 1.7 christos
316 1.7 christos /*
317 1.7 christos * Return an indication of where the new directory
318 1.7 christos * entry should be put.
319 1.7 christos */
320 1.7 christos dp->de_fndoffset = slotoffset;
321 1.7 christos dp->de_fndcnt = wincnt - 1;
322 1.7 christos
323 1.7 christos /*
324 1.7 christos * We return with the directory locked, so that
325 1.7 christos * the parameters we set up above will still be
326 1.7 christos * valid if we actually decide to do a direnter().
327 1.7 christos * We return ni_vp == NULL to indicate that the entry
328 1.7 christos * does not currently exist; we leave a pointer to
329 1.7 christos * the (locked) directory inode in ndp->ni_dvp.
330 1.7 christos *
331 1.7 christos * NB - if the directory is unlocked, then this
332 1.7 christos * information cannot be used.
333 1.7 christos */
334 1.7 christos return 0;
335 1.7 christos }
336 1.7 christos
337 1.7 christos /*
338 1.1 christos * Create a regular file. On entry the directory to contain the file being
339 1.1 christos * created is locked. We must release before we return.
340 1.1 christos */
341 1.1 christos struct denode *
342 1.1 christos msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
343 1.1 christos {
344 1.1 christos struct componentname cn;
345 1.1 christos struct denode ndirent;
346 1.1 christos struct denode *dep;
347 1.1 christos int error;
348 1.1 christos struct stat *st = &node->inode->st;
349 1.1 christos struct msdosfsmount *pmp = pdep->de_pmp;
350 1.1 christos
351 1.1 christos cn.cn_nameptr = node->name;
352 1.1 christos cn.cn_namelen = strlen(node->name);
353 1.1 christos
354 1.1 christos #ifdef MSDOSFS_DEBUG
355 1.2 christos printf("msdosfs_create(name %s, mode 0%o size %zu\n", node->name,
356 1.2 christos st->st_mode, (size_t)st->st_size);
357 1.1 christos #endif
358 1.1 christos
359 1.1 christos /*
360 1.1 christos * If this is the root directory and there is no space left we
361 1.1 christos * can't do anything. This is because the root directory can not
362 1.1 christos * change size.
363 1.1 christos */
364 1.1 christos if (pdep->de_StartCluster == MSDOSFSROOT
365 1.1 christos && pdep->de_fndoffset >= pdep->de_FileSize) {
366 1.1 christos error = ENOSPC;
367 1.1 christos goto bad;
368 1.1 christos }
369 1.1 christos
370 1.1 christos /*
371 1.1 christos * Create a directory entry for the file, then call createde() to
372 1.1 christos * have it installed. NOTE: DOS files are always executable. We
373 1.1 christos * use the absence of the owner write bit to make the file
374 1.1 christos * readonly.
375 1.1 christos */
376 1.1 christos memset(&ndirent, 0, sizeof(ndirent));
377 1.1 christos if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
378 1.1 christos goto bad;
379 1.1 christos
380 1.1 christos ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
381 1.1 christos ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
382 1.1 christos ndirent.de_StartCluster = 0;
383 1.1 christos ndirent.de_FileSize = 0;
384 1.1 christos ndirent.de_dev = pdep->de_dev;
385 1.1 christos ndirent.de_devvp = pdep->de_devvp;
386 1.1 christos ndirent.de_pmp = pdep->de_pmp;
387 1.1 christos ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
388 1.1 christos msdosfs_times(pmp, &ndirent, st);
389 1.7 christos if ((error = msdosfs_findslot(pdep, &cn)) != 0)
390 1.7 christos goto bad;
391 1.1 christos if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
392 1.1 christos goto bad;
393 1.7 christos if ((error = msdosfs_wfile(path, dep, node)) != 0)
394 1.1 christos goto bad;
395 1.1 christos return dep;
396 1.1 christos
397 1.1 christos bad:
398 1.1 christos errno = error;
399 1.1 christos return NULL;
400 1.1 christos }
401 1.1 christos
402 1.1 christos /*
403 1.1 christos * Write data to a file or directory.
404 1.1 christos */
405 1.1 christos static int
406 1.1 christos msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
407 1.1 christos {
408 1.1 christos int error, fd;
409 1.1 christos size_t osize = dep->de_FileSize;
410 1.1 christos struct stat *st = &node->inode->st;
411 1.6 martin size_t nsize;
412 1.1 christos size_t offs = 0;
413 1.1 christos struct msdosfsmount *pmp = dep->de_pmp;
414 1.1 christos struct buf *bp;
415 1.1 christos char *dat;
416 1.3 christos u_long cn;
417 1.1 christos
418 1.1 christos #ifdef MSDOSFS_DEBUG
419 1.7 christos printf("%s(diroff %lu, dirclust %lu, startcluster %lu)\n", __func__,
420 1.1 christos dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
421 1.1 christos #endif
422 1.6 martin if (st->st_size == 0)
423 1.1 christos return 0;
424 1.1 christos
425 1.1 christos /* Don't bother to try to write files larger than the fs limit */
426 1.8 christos if (st->st_size > MSDOSFS_FILESIZE_MAX) {
427 1.1 christos errno = EFBIG;
428 1.1 christos return -1;
429 1.1 christos }
430 1.1 christos
431 1.6 martin nsize = st->st_size;
432 1.1 christos if (nsize > osize) {
433 1.1 christos if ((error = deextend(dep, nsize, NULL)) != 0) {
434 1.1 christos errno = error;
435 1.1 christos return -1;
436 1.1 christos }
437 1.1 christos }
438 1.1 christos
439 1.1 christos if ((fd = open(path, O_RDONLY)) == -1)
440 1.1 christos err(1, "open %s", path);
441 1.1 christos
442 1.7 christos if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
443 1.7 christos == MAP_FAILED) {
444 1.7 christos fprintf(stderr, "mmap %s", node->name);
445 1.7 christos goto out;
446 1.7 christos }
447 1.1 christos close(fd);
448 1.1 christos
449 1.7 christos for (cn = 0;; cn++) {
450 1.1 christos int blsize, cpsize;
451 1.1 christos daddr_t bn;
452 1.7 christos if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
453 1.1 christos fprintf(stderr, "pcbmap %ld\n", cn);
454 1.1 christos goto out;
455 1.1 christos }
456 1.7 christos #ifdef MSDOSFS_DEBUG
457 1.7 christos printf("%s(cn=%lu, bn=%llu/%llu, blsize=%d)\n", __func__, cn,
458 1.7 christos (unsigned long long)bn,
459 1.7 christos (unsigned long long)de_bn2kb(pmp, bn), blsize);
460 1.7 christos #endif
461 1.7 christos if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
462 1.7 christos NULL, 0, &bp)) != 0) {
463 1.7 christos fprintf(stderr, "bread %d\n", error);
464 1.7 christos goto out;
465 1.7 christos }
466 1.1 christos cpsize = MIN((int)(nsize - offs), blsize);
467 1.1 christos memcpy(bp->b_data, dat + offs, cpsize);
468 1.1 christos bwrite(bp);
469 1.1 christos brelse(bp, 0);
470 1.1 christos offs += blsize;
471 1.1 christos if (offs >= nsize)
472 1.1 christos break;
473 1.1 christos }
474 1.1 christos
475 1.1 christos munmap(dat, nsize);
476 1.1 christos return 0;
477 1.1 christos out:
478 1.1 christos munmap(dat, nsize);
479 1.7 christos return error;
480 1.1 christos }
481 1.1 christos
482 1.1 christos
483 1.1 christos static const struct {
484 1.1 christos struct direntry dot;
485 1.1 christos struct direntry dotdot;
486 1.1 christos } dosdirtemplate = {
487 1.7 christos { ". ", " ", /* the . entry */
488 1.1 christos ATTR_DIRECTORY, /* file attribute */
489 1.7 christos 0, /* reserved */
490 1.1 christos 0, { 0, 0 }, { 0, 0 }, /* create time & date */
491 1.1 christos { 0, 0 }, /* access date */
492 1.1 christos { 0, 0 }, /* high bits of start cluster */
493 1.1 christos { 210, 4 }, { 210, 4 }, /* modify time & date */
494 1.1 christos { 0, 0 }, /* startcluster */
495 1.7 christos { 0, 0, 0, 0 } /* filesize */
496 1.1 christos },
497 1.7 christos { ".. ", " ", /* the .. entry */
498 1.1 christos ATTR_DIRECTORY, /* file attribute */
499 1.7 christos 0, /* reserved */
500 1.1 christos 0, { 0, 0 }, { 0, 0 }, /* create time & date */
501 1.1 christos { 0, 0 }, /* access date */
502 1.1 christos { 0, 0 }, /* high bits of start cluster */
503 1.1 christos { 210, 4 }, { 210, 4 }, /* modify time & date */
504 1.1 christos { 0, 0 }, /* startcluster */
505 1.1 christos { 0, 0, 0, 0 } /* filesize */
506 1.1 christos }
507 1.1 christos };
508 1.1 christos
509 1.1 christos struct denode *
510 1.1 christos msdosfs_mkdire(const char *path, struct denode *pdep, fsnode *node) {
511 1.1 christos struct denode ndirent;
512 1.1 christos struct denode *dep;
513 1.1 christos struct componentname cn;
514 1.1 christos struct stat *st = &node->inode->st;
515 1.1 christos struct msdosfsmount *pmp = pdep->de_pmp;
516 1.1 christos int error;
517 1.1 christos u_long newcluster, pcl, bn;
518 1.1 christos daddr_t lbn;
519 1.1 christos struct direntry *denp;
520 1.1 christos struct buf *bp;
521 1.1 christos
522 1.1 christos cn.cn_nameptr = node->name;
523 1.1 christos cn.cn_namelen = strlen(node->name);
524 1.1 christos /*
525 1.1 christos * If this is the root directory and there is no space left we
526 1.1 christos * can't do anything. This is because the root directory can not
527 1.1 christos * change size.
528 1.1 christos */
529 1.1 christos if (pdep->de_StartCluster == MSDOSFSROOT
530 1.1 christos && pdep->de_fndoffset >= pdep->de_FileSize) {
531 1.1 christos error = ENOSPC;
532 1.1 christos goto bad2;
533 1.1 christos }
534 1.1 christos
535 1.1 christos /*
536 1.1 christos * Allocate a cluster to hold the about to be created directory.
537 1.1 christos */
538 1.1 christos error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
539 1.1 christos if (error)
540 1.1 christos goto bad2;
541 1.1 christos
542 1.1 christos memset(&ndirent, 0, sizeof(ndirent));
543 1.1 christos ndirent.de_pmp = pmp;
544 1.1 christos ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
545 1.1 christos msdosfs_times(pmp, &ndirent, st);
546 1.1 christos
547 1.1 christos /*
548 1.1 christos * Now fill the cluster with the "." and ".." entries. And write
549 1.7 christos * the cluster to disk. This way it is there for the parent
550 1.1 christos * directory to be pointing at if there were a crash.
551 1.1 christos */
552 1.1 christos bn = cntobn(pmp, newcluster);
553 1.1 christos lbn = de_bn2kb(pmp, bn);
554 1.1 christos /* always succeeds */
555 1.1 christos bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
556 1.1 christos memset(bp->b_data, 0, pmp->pm_bpcluster);
557 1.1 christos memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
558 1.1 christos denp = (struct direntry *)bp->b_data;
559 1.1 christos putushort(denp[0].deStartCluster, newcluster);
560 1.1 christos putushort(denp[0].deCDate, ndirent.de_CDate);
561 1.1 christos putushort(denp[0].deCTime, ndirent.de_CTime);
562 1.1 christos denp[0].deCHundredth = ndirent.de_CHun;
563 1.1 christos putushort(denp[0].deADate, ndirent.de_ADate);
564 1.1 christos putushort(denp[0].deMDate, ndirent.de_MDate);
565 1.1 christos putushort(denp[0].deMTime, ndirent.de_MTime);
566 1.1 christos pcl = pdep->de_StartCluster;
567 1.1 christos if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
568 1.1 christos pcl = 0;
569 1.1 christos putushort(denp[1].deStartCluster, pcl);
570 1.1 christos putushort(denp[1].deCDate, ndirent.de_CDate);
571 1.1 christos putushort(denp[1].deCTime, ndirent.de_CTime);
572 1.1 christos denp[1].deCHundredth = ndirent.de_CHun;
573 1.1 christos putushort(denp[1].deADate, ndirent.de_ADate);
574 1.1 christos putushort(denp[1].deMDate, ndirent.de_MDate);
575 1.1 christos putushort(denp[1].deMTime, ndirent.de_MTime);
576 1.1 christos if (FAT32(pmp)) {
577 1.1 christos putushort(denp[0].deHighClust, newcluster >> 16);
578 1.1 christos putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
579 1.1 christos } else {
580 1.1 christos putushort(denp[0].deHighClust, 0);
581 1.1 christos putushort(denp[1].deHighClust, 0);
582 1.1 christos }
583 1.1 christos
584 1.1 christos if ((error = bwrite(bp)) != 0)
585 1.1 christos goto bad;
586 1.1 christos
587 1.1 christos /*
588 1.1 christos * Now build up a directory entry pointing to the newly allocated
589 1.1 christos * cluster. This will be written to an empty slot in the parent
590 1.1 christos * directory.
591 1.1 christos */
592 1.1 christos if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
593 1.1 christos goto bad;
594 1.1 christos
595 1.1 christos ndirent.de_Attributes = ATTR_DIRECTORY;
596 1.1 christos ndirent.de_StartCluster = newcluster;
597 1.1 christos ndirent.de_FileSize = 0;
598 1.1 christos ndirent.de_dev = pdep->de_dev;
599 1.1 christos ndirent.de_devvp = pdep->de_devvp;
600 1.7 christos if ((error = msdosfs_findslot(pdep, &cn)) != 0)
601 1.7 christos goto bad;
602 1.1 christos if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
603 1.1 christos goto bad;
604 1.1 christos return dep;
605 1.1 christos
606 1.1 christos bad:
607 1.1 christos clusterfree(pmp, newcluster, NULL);
608 1.1 christos bad2:
609 1.1 christos errno = error;
610 1.1 christos return NULL;
611 1.1 christos }
612