1 /* $NetBSD: msdosfs_vnops.c,v 1.19 2017/04/13 17:10:12 christos Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
56 #include <sys/clock.h>
57 #include <sys/errno.h>
69 #include <fs/msdosfs/bpb.h>
70 #include "msdos/direntry.h"
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
73 #include <fs/msdosfs/msdosfsmount.h>
81 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
82 * read/written using the vnode for the filesystem. Blocks that represent
83 * the contents of a file are read/written using the vnode for the file
84 * (including directories when they are read/written as files). This
85 * presents problems for the dos filesystem because data that should be in
86 * an inode (if dos had them) resides in the directory itself. Since we
87 * must update directory entries without the benefit of having the vnode
88 * for the directory we must use the vnode for the filesystem. This means
89 * that when a directory is actually read/written (via read, write, or
90 * readdir, or seek) we must use the vnode for the filesystem instead of
91 * the vnode for the directory as would happen in ufs. This is to insure we
92 * retrieve the correct block from the buffer cache since the hash value is
93 * based upon the vnode address and the desired block number.
96 static int msdosfs_wfile(const char *, struct denode *, fsnode *);
97 static void unix2fattime(const struct timespec *tsp, uint16_t *ddp,
101 msdosfs_times(struct denode *dep, const struct stat *st)
106 #ifdef HAVE_STRUCT_STAT_BIRTHTIME
107 unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime);
109 unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime);
111 unix2fattime(&st->st_atim, &dep->de_ADate, NULL);
112 unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime);
116 unix2fattime(const struct timespec *tsp, uint16_t *ddp, uint16_t *dtp)
122 localtime_r(&t1, <);
124 unsigned long fat_time = ((lt.tm_year - 80) << 25) |
125 ((lt.tm_mon + 1) << 21) |
132 *ddp = (uint16_t)(fat_time >> 16);
134 *dtp = (uint16_t)fat_time;
138 * When we search a directory the blocks containing directory entries are
139 * read and examined. The directory entries contain information that would
140 * normally be in the inode of a unix filesystem. This means that some of
141 * a directory's contents may also be in memory resident denodes (sort of
142 * an inode). This can cause problems if we are searching while some other
143 * process is modifying a directory. To prevent one process from accessing
144 * incompletely modified directory information we depend upon being the
145 * sole owner of a directory block. bread/brelse provide this service.
146 * This being the case, when a process modifies a directory it must first
147 * acquire the disk block that contains the directory entry to be modified.
148 * Then update the disk block and the denode, and then write the disk block
149 * out to disk. This way disk blocks containing directory entries and in
150 * memory denode's will be in synch.
153 msdosfs_findslot(struct denode *dp, struct componentname *cnp)
164 struct msdosfsmount *pmp;
166 struct direntry *dep;
167 u_char dosfilename[12];
169 int chksum = -1, chksum_ok;
174 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
175 cnp->cn_namelen, 0)) {
181 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
182 cnp->cn_namelen) + 1;
186 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
187 cnp->cn_namelen) + 1;
191 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
195 * Suppress search for slots unless creating
196 * file and at end of pathname, in which case
197 * we watch for a place to put the new file in
198 * case it doesn't already exist.
201 MSDOSFS_DPRINTF(("%s(): dos filename: %s\n", __func__, dosfilename));
203 * Search the directory pointed at by vdp for the name pointed at
204 * by cnp->cn_nameptr.
207 * The outer loop ranges over the clusters that make up the
208 * directory. Note that the root directory is different from all
209 * other directories. It has a fixed number of blocks that are not
210 * part of the pool of allocatable clusters. So, we treat it a
211 * little differently. The root directory starts at "cluster" 0.
214 for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
215 if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
220 error = bread(pmp->pm_devvp, bn, blsize, 0, &bp);
224 for (blkoff = 0; blkoff < blsize;
225 blkoff += sizeof(struct direntry),
226 diroff += sizeof(struct direntry)) {
227 dep = (struct direntry *)(bp->b_data + blkoff);
229 * If the slot is empty and we are still looking
230 * for an empty then remember this one. If the
231 * slot is not empty then check to see if it
232 * matches what we are looking for. If the slot
233 * has never been filled with anything, then the
234 * remainder of the directory has never been used,
235 * so there is no point in searching it.
237 if (dep->deName[0] == SLOT_EMPTY ||
238 dep->deName[0] == SLOT_DELETED) {
240 * Drop memory of previous long matches
244 if (slotcount < wincnt) {
248 if (dep->deName[0] == SLOT_EMPTY) {
254 * If there wasn't enough space for our
255 * winentries, forget about the empty space
257 if (slotcount < wincnt)
261 * Check for Win95 long filename entry
263 if (dep->deAttributes == ATTR_WIN95) {
264 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
268 (const u_char *)cnp->cn_nameptr,
270 (struct winentry *)dep, chksum);
275 * Ignore volume labels (anywhere, not just
276 * the root directory).
278 if (dep->deAttributes & ATTR_VOLUME) {
284 * Check for a checksum or name match
286 chksum_ok = (chksum == winChksum(dep->deName));
288 && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
292 MSDOSFS_DPRINTF(("%s(): match blkoff %d, diroff %u\n",
293 __func__, blkoff, diroff));
295 * Remember where this directory
296 * entry came from for whoever did
299 dp->de_fndoffset = diroff;
304 } /* for (blkoff = 0; .... */
306 * Release the buffer holding the directory cluster just
310 } /* for (frcn = 0; ; frcn++) */
314 * We hold no disk buffers at this point.
318 * If we get here we didn't find the entry we were looking for. But
319 * that's ok if we are creating or renaming and are at the end of
320 * the pathname and the directory hasn't been removed.
322 MSDOSFS_DPRINTF(("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
323 __func__, dp->de_refcnt, slotcount, slotoffset));
325 * Fixup the slot description to point to the place where
326 * we might put the new DOS direntry (putting the Win95
327 * long name entries before that)
333 if (wincnt > slotcount) {
334 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
338 * Return an indication of where the new directory
339 * entry should be put.
341 dp->de_fndoffset = slotoffset;
342 dp->de_fndcnt = wincnt - 1;
345 * We return with the directory locked, so that
346 * the parameters we set up above will still be
347 * valid if we actually decide to do a direnter().
348 * We return ni_vp == NULL to indicate that the entry
349 * does not currently exist; we leave a pointer to
350 * the (locked) directory inode in ndp->ni_dvp.
352 * NB - if the directory is unlocked, then this
353 * information cannot be used.
359 * Create a regular file. On entry the directory to contain the file being
360 * created is locked. We must release before we return.
363 msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
365 struct componentname cn;
366 struct denode ndirent;
369 struct stat *st = &node->inode->st;
371 cn.cn_nameptr = node->name;
372 cn.cn_namelen = strlen(node->name);
374 MSDOSFS_DPRINTF(("%s(name %s, mode 0%o size %zu)\n",
375 __func__, node->name, st->st_mode, (size_t)st->st_size));
378 * If this is the root directory and there is no space left we
379 * can't do anything. This is because the root directory can not
382 if (pdep->de_StartCluster == MSDOSFSROOT
383 && pdep->de_fndoffset >= pdep->de_FileSize) {
389 * Create a directory entry for the file, then call createde() to
390 * have it installed. NOTE: DOS files are always executable. We
391 * use the absence of the owner write bit to make the file
394 memset(&ndirent, 0, sizeof(ndirent));
395 if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
398 ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
399 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
400 ndirent.de_StartCluster = 0;
401 ndirent.de_FileSize = 0;
402 ndirent.de_pmp = pdep->de_pmp;
403 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
404 msdosfs_times(&ndirent, &node->inode->st);
406 if ((error = msdosfs_findslot(pdep, &cn)) != 0)
408 if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
410 if ((error = msdosfs_wfile(path, dep, node)) != 0)
419 msdosfs_updatede(struct denode *dep)
422 struct direntry *dirp;
425 dep->de_flag &= ~DE_MODIFIED;
426 error = readde(dep, &bp, &dirp);
429 DE_EXTERNALIZE(dirp, dep);
435 * Write data to a file or directory.
438 msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
441 size_t osize = dep->de_FileSize;
442 struct stat *st = &node->inode->st;
444 struct msdosfsmount *pmp = dep->de_pmp;
449 error = 0; /* XXX: gcc/vax */
450 MSDOSFS_DPRINTF(("%s(diroff %lu, dirclust %lu, startcluster %lu)\n",
451 __func__, dep->de_diroffset, dep->de_dirclust,
452 dep->de_StartCluster));
453 if (st->st_size == 0)
456 /* Don't bother to try to write files larger than the fs limit */
457 if (st->st_size > MSDOSFS_FILESIZE_MAX)
461 MSDOSFS_DPRINTF(("%s(nsize=%zu, osize=%zu)\n", __func__, nsize, osize));
463 if ((error = deextend(dep, nsize, NULL)) != 0)
465 if ((error = msdosfs_updatede(dep)) != 0)
469 if ((fd = open(path, O_RDONLY)) == -1) {
471 MSDOSFS_DPRINTF(("open %s: %s", path, strerror(error)));
475 if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
478 MSDOSFS_DPRINTF(("%s: mmap %s: %s", __func__, node->name,
485 for (offs = 0; offs < nsize;) {
488 u_long on = offs & pmp->pm_crbomask;
490 if ((error = pcbmap(dep, cn++, &bn, NULL, &blsize)) != 0) {
491 MSDOSFS_DPRINTF(("%s: pcbmap %lu",
492 __func__, (unsigned long)bn));
496 MSDOSFS_DPRINTF(("%s(cn=%lu, bn=%llu, blsize=%d)\n",
497 __func__, cn, (unsigned long long)bn, blsize));
498 if ((error = bread(pmp->pm_devvp, bn, blsize, 0, &bp)) != 0) {
499 MSDOSFS_DPRINTF(("bread %d\n", error));
502 cpsize = MIN((nsize - offs), blsize - on);
503 memcpy(bp->b_data + on, dat + offs, cpsize);
515 static const struct {
517 struct direntry dotdot;
519 { ". ", /* the . entry */
520 ATTR_DIRECTORY, /* file attribute */
522 0, { 0, 0 }, { 0, 0 }, /* create time & date */
523 { 0, 0 }, /* access date */
524 { 0, 0 }, /* high bits of start cluster */
525 { 210, 4 }, { 210, 4 }, /* modify time & date */
526 { 0, 0 }, /* startcluster */
527 { 0, 0, 0, 0 } /* filesize */
529 { ".. ", /* the .. entry */
530 ATTR_DIRECTORY, /* file attribute */
532 0, { 0, 0 }, { 0, 0 }, /* create time & date */
533 { 0, 0 }, /* access date */
534 { 0, 0 }, /* high bits of start cluster */
535 { 210, 4 }, { 210, 4 }, /* modify time & date */
536 { 0, 0 }, /* startcluster */
537 { 0, 0, 0, 0 } /* filesize */
542 msdosfs_mkdire(const char *path, struct denode *pdep, fsnode *node) {
543 struct denode ndirent;
545 struct componentname cn;
546 struct msdosfsmount *pmp = pdep->de_pmp;
548 u_long newcluster, pcl, bn;
549 struct direntry *denp;
552 cn.cn_nameptr = node->name;
553 cn.cn_namelen = strlen(node->name);
555 * If this is the root directory and there is no space left we
556 * can't do anything. This is because the root directory can not
559 if (pdep->de_StartCluster == MSDOSFSROOT
560 && pdep->de_fndoffset >= pdep->de_FileSize) {
566 * Allocate a cluster to hold the about to be created directory.
568 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
572 memset(&ndirent, 0, sizeof(ndirent));
573 ndirent.de_pmp = pmp;
574 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
575 msdosfs_times(&ndirent, &node->inode->st);
578 * Now fill the cluster with the "." and ".." entries. And write
579 * the cluster to disk. This way it is there for the parent
580 * directory to be pointing at if there were a crash.
582 bn = cntobn(pmp, newcluster);
583 MSDOSFS_DPRINTF(("%s(newcluster %lu, bn=%lu)\n",
584 __func__, newcluster, bn));
585 /* always succeeds */
586 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
587 memset(bp->b_data, 0, pmp->pm_bpcluster);
588 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
589 denp = (struct direntry *)bp->b_data;
590 putushort(denp[0].deStartCluster, newcluster);
591 putushort(denp[0].deCDate, ndirent.de_CDate);
592 putushort(denp[0].deCTime, ndirent.de_CTime);
593 denp[0].deCHundredth = ndirent.de_CHun;
594 putushort(denp[0].deADate, ndirent.de_ADate);
595 putushort(denp[0].deMDate, ndirent.de_MDate);
596 putushort(denp[0].deMTime, ndirent.de_MTime);
597 pcl = pdep->de_StartCluster;
598 MSDOSFS_DPRINTF(("%s(pcl %lu, rootdirblk=%lu)\n", __func__, pcl,
599 pmp->pm_rootdirblk));
600 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
602 putushort(denp[1].deStartCluster, pcl);
603 putushort(denp[1].deCDate, ndirent.de_CDate);
604 putushort(denp[1].deCTime, ndirent.de_CTime);
605 denp[1].deCHundredth = ndirent.de_CHun;
606 putushort(denp[1].deADate, ndirent.de_ADate);
607 putushort(denp[1].deMDate, ndirent.de_MDate);
608 putushort(denp[1].deMTime, ndirent.de_MTime);
610 putushort(denp[0].deHighClust, newcluster >> 16);
611 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
613 putushort(denp[0].deHighClust, 0);
614 putushort(denp[1].deHighClust, 0);
617 if ((error = bwrite(bp)) != 0)
621 * Now build up a directory entry pointing to the newly allocated
622 * cluster. This will be written to an empty slot in the parent
625 if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
628 ndirent.de_Attributes = ATTR_DIRECTORY;
629 ndirent.de_StartCluster = newcluster;
630 ndirent.de_FileSize = 0;
631 ndirent.de_pmp = pdep->de_pmp;
632 if ((error = msdosfs_findslot(pdep, &cn)) != 0)
634 if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
636 if ((error = msdosfs_updatede(dep)) != 0)
641 clusterfree(pmp, newcluster, NULL);