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