1*8086f46eSthorpej /* $NetBSD: msdosfs_denode.c,v 1.8 2021/10/23 16:58:17 thorpej Exp $ */
27e2d9be6Schristos
37e2d9be6Schristos /*-
47e2d9be6Schristos * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
57e2d9be6Schristos * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
67e2d9be6Schristos * All rights reserved.
77e2d9be6Schristos * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
87e2d9be6Schristos *
97e2d9be6Schristos * Redistribution and use in source and binary forms, with or without
107e2d9be6Schristos * modification, are permitted provided that the following conditions
117e2d9be6Schristos * are met:
127e2d9be6Schristos * 1. Redistributions of source code must retain the above copyright
137e2d9be6Schristos * notice, this list of conditions and the following disclaimer.
147e2d9be6Schristos * 2. Redistributions in binary form must reproduce the above copyright
157e2d9be6Schristos * notice, this list of conditions and the following disclaimer in the
167e2d9be6Schristos * documentation and/or other materials provided with the distribution.
177e2d9be6Schristos * 3. All advertising materials mentioning features or use of this software
187e2d9be6Schristos * must display the following acknowledgement:
197e2d9be6Schristos * This product includes software developed by TooLs GmbH.
207e2d9be6Schristos * 4. The name of TooLs GmbH may not be used to endorse or promote products
217e2d9be6Schristos * derived from this software without specific prior written permission.
227e2d9be6Schristos *
237e2d9be6Schristos * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
247e2d9be6Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
257e2d9be6Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
267e2d9be6Schristos * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
277e2d9be6Schristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
287e2d9be6Schristos * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
297e2d9be6Schristos * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
307e2d9be6Schristos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
317e2d9be6Schristos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
327e2d9be6Schristos * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
337e2d9be6Schristos */
347e2d9be6Schristos /*
357e2d9be6Schristos * Written by Paul Popelka (paulp@uts.amdahl.com)
367e2d9be6Schristos *
377e2d9be6Schristos * You can do anything you want with this software, just don't say you wrote
387e2d9be6Schristos * it, and don't remove this notice.
397e2d9be6Schristos *
407e2d9be6Schristos * This software is provided "as is".
417e2d9be6Schristos *
427e2d9be6Schristos * The author supplies this software to be publicly redistributed on the
437e2d9be6Schristos * understanding that the author is not responsible for the correct
447e2d9be6Schristos * functioning of this software in any circumstances and is not liable for
457e2d9be6Schristos * any damages caused by this software.
467e2d9be6Schristos *
477e2d9be6Schristos * October 1992
487e2d9be6Schristos */
497e2d9be6Schristos
507e2d9be6Schristos #if HAVE_NBTOOL_CONFIG_H
517e2d9be6Schristos #include "nbtool_config.h"
527e2d9be6Schristos #endif
537e2d9be6Schristos
547e2d9be6Schristos #include <sys/cdefs.h>
55*8086f46eSthorpej __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.8 2021/10/23 16:58:17 thorpej Exp $");
567e2d9be6Schristos
577e2d9be6Schristos #include <sys/param.h>
587e2d9be6Schristos
597e2d9be6Schristos #include <ffs/buf.h>
607e2d9be6Schristos
617e2d9be6Schristos #include <fs/msdosfs/bpb.h>
627e2d9be6Schristos #include <fs/msdosfs/msdosfsmount.h>
637e2d9be6Schristos #include <fs/msdosfs/direntry.h>
647e2d9be6Schristos #include <fs/msdosfs/denode.h>
657e2d9be6Schristos #include <fs/msdosfs/fat.h>
667e2d9be6Schristos
67e4989541Schristos #include <util.h>
68e4989541Schristos
697e2d9be6Schristos /*
707e2d9be6Schristos * If deget() succeeds it returns with the gotten denode locked().
717e2d9be6Schristos *
727e2d9be6Schristos * pmp - address of msdosfsmount structure of the filesystem containing
737e2d9be6Schristos * the denode of interest. The pm_dev field and the address of
747e2d9be6Schristos * the msdosfsmount structure are used.
757e2d9be6Schristos * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
767e2d9be6Schristos * diroffset is relative to the beginning of the root directory,
777e2d9be6Schristos * otherwise it is cluster relative.
787e2d9be6Schristos * diroffset - offset past begin of cluster of denode we want
797e2d9be6Schristos * depp - returns the address of the gotten denode.
807e2d9be6Schristos */
817e2d9be6Schristos int
msdosfs_deget(struct msdosfsmount * pmp,u_long dirclust,u_long diroffset,struct denode ** depp)82*8086f46eSthorpej msdosfs_deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
83e4989541Schristos struct denode **depp)
847e2d9be6Schristos /* pmp: so we know the maj/min number */
857e2d9be6Schristos /* dirclust: cluster this dir entry came from */
867e2d9be6Schristos /* diroffset: index of entry within the cluster */
877e2d9be6Schristos /* depp: returns the addr of the gotten denode */
887e2d9be6Schristos {
897e2d9be6Schristos int error;
907e2d9be6Schristos struct direntry *direntptr;
917e2d9be6Schristos struct denode *ldep;
927e2d9be6Schristos struct buf *bp;
937e2d9be6Schristos
947e2d9be6Schristos #ifdef MSDOSFS_DEBUG
957e2d9be6Schristos printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
967e2d9be6Schristos pmp, dirclust, diroffset, depp);
977e2d9be6Schristos #endif
987e2d9be6Schristos
997e2d9be6Schristos /*
1007e2d9be6Schristos * On FAT32 filesystems, root is a (more or less) normal
1017e2d9be6Schristos * directory
1027e2d9be6Schristos */
1037e2d9be6Schristos if (FAT32(pmp) && dirclust == MSDOSFSROOT)
1047e2d9be6Schristos dirclust = pmp->pm_rootdirblk;
1057e2d9be6Schristos
106e4989541Schristos ldep = ecalloc(1, sizeof(*ldep));
1077e2d9be6Schristos ldep->de_vnode = NULL;
1087e2d9be6Schristos ldep->de_flag = 0;
1097e2d9be6Schristos ldep->de_devvp = 0;
1107e2d9be6Schristos ldep->de_lockf = 0;
1117e2d9be6Schristos ldep->de_dev = pmp->pm_dev;
1127e2d9be6Schristos ldep->de_dirclust = dirclust;
1137e2d9be6Schristos ldep->de_diroffset = diroffset;
1147e2d9be6Schristos ldep->de_pmp = pmp;
1157e2d9be6Schristos ldep->de_devvp = pmp->pm_devvp;
1167e2d9be6Schristos ldep->de_refcnt = 1;
117*8086f46eSthorpej msdosfs_fc_purge(ldep, 0);
1187e2d9be6Schristos /*
1197e2d9be6Schristos * Copy the directory entry into the denode area of the vnode.
1207e2d9be6Schristos */
1217e2d9be6Schristos if ((dirclust == MSDOSFSROOT
1227e2d9be6Schristos || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
1237e2d9be6Schristos && diroffset == MSDOSFSROOT_OFS) {
1247e2d9be6Schristos /*
1257e2d9be6Schristos * Directory entry for the root directory. There isn't one,
1267e2d9be6Schristos * so we manufacture one. We should probably rummage
1277e2d9be6Schristos * through the root directory and find a label entry (if it
1287e2d9be6Schristos * exists), and then use the time and date from that entry
1297e2d9be6Schristos * as the time and date for the root denode.
1307e2d9be6Schristos */
1317e2d9be6Schristos ldep->de_vnode = (struct vnode *)-1;
1327e2d9be6Schristos
1337e2d9be6Schristos ldep->de_Attributes = ATTR_DIRECTORY;
1347e2d9be6Schristos if (FAT32(pmp))
1357e2d9be6Schristos ldep->de_StartCluster = pmp->pm_rootdirblk;
1367e2d9be6Schristos /* de_FileSize will be filled in further down */
1377e2d9be6Schristos else {
1387e2d9be6Schristos ldep->de_StartCluster = MSDOSFSROOT;
1397e2d9be6Schristos ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
1407e2d9be6Schristos }
1417e2d9be6Schristos /*
1427e2d9be6Schristos * fill in time and date so that dos2unixtime() doesn't
1437e2d9be6Schristos * spit up when called from msdosfs_getattr() with root
1447e2d9be6Schristos * denode
1457e2d9be6Schristos */
1467e2d9be6Schristos ldep->de_CHun = 0;
1477e2d9be6Schristos ldep->de_CTime = 0x0000; /* 00:00:00 */
1487e2d9be6Schristos ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
1497e2d9be6Schristos | (1 << DD_DAY_SHIFT);
1507e2d9be6Schristos /* Jan 1, 1980 */
1517e2d9be6Schristos ldep->de_ADate = ldep->de_CDate;
1527e2d9be6Schristos ldep->de_MTime = ldep->de_CTime;
1537e2d9be6Schristos ldep->de_MDate = ldep->de_CDate;
1547e2d9be6Schristos /* leave the other fields as garbage */
1557e2d9be6Schristos } else {
156*8086f46eSthorpej error = msdosfs_readep(pmp, dirclust, diroffset, &bp,
157*8086f46eSthorpej &direntptr);
1587e2d9be6Schristos if (error) {
1597e2d9be6Schristos ldep->de_devvp = NULL;
1607e2d9be6Schristos ldep->de_Name[0] = SLOT_DELETED;
1617e2d9be6Schristos return (error);
1627e2d9be6Schristos }
1637e2d9be6Schristos DE_INTERNALIZE(ldep, direntptr);
1647e2d9be6Schristos brelse(bp, 0);
1657e2d9be6Schristos }
1667e2d9be6Schristos
1677e2d9be6Schristos /*
1687e2d9be6Schristos * Fill in a few fields of the vnode and finish filling in the
1697e2d9be6Schristos * denode. Then return the address of the found denode.
1707e2d9be6Schristos */
1717e2d9be6Schristos if (ldep->de_Attributes & ATTR_DIRECTORY) {
1727e2d9be6Schristos /*
1737e2d9be6Schristos * Since DOS directory entries that describe directories
1747e2d9be6Schristos * have 0 in the filesize field, we take this opportunity
1757e2d9be6Schristos * to find out the length of the directory and plug it into
1767e2d9be6Schristos * the denode structure.
1777e2d9be6Schristos */
1787e2d9be6Schristos u_long size;
1797e2d9be6Schristos
1807e2d9be6Schristos if (ldep->de_StartCluster != MSDOSFSROOT) {
181*8086f46eSthorpej error = msdosfs_pcbmap(ldep, CLUST_END, 0, &size, 0);
1827e2d9be6Schristos if (error == E2BIG) {
1837e2d9be6Schristos ldep->de_FileSize = de_cn2off(pmp, size);
1847e2d9be6Schristos error = 0;
1857e2d9be6Schristos } else
1867e2d9be6Schristos printf("deget(): pcbmap returned %d\n", error);
1877e2d9be6Schristos }
1887e2d9be6Schristos }
1897e2d9be6Schristos *depp = ldep;
1907e2d9be6Schristos return (0);
1917e2d9be6Schristos }
1927e2d9be6Schristos
1937e2d9be6Schristos /*
1947e2d9be6Schristos * Truncate the file described by dep to the length specified by length.
1957e2d9be6Schristos */
1967e2d9be6Schristos int
msdosfs_detrunc(struct denode * dep,u_long length,int flags,struct kauth_cred * cred)197*8086f46eSthorpej msdosfs_detrunc(struct denode *dep, u_long length, int flags,
198*8086f46eSthorpej struct kauth_cred *cred)
1997e2d9be6Schristos {
2007e2d9be6Schristos int error;
2017e2d9be6Schristos int allerror = 0;
2027e2d9be6Schristos u_long eofentry;
2037e2d9be6Schristos u_long chaintofree = 0;
2047e2d9be6Schristos daddr_t bn, lastblock;
2057e2d9be6Schristos int boff;
2067e2d9be6Schristos int isadir = dep->de_Attributes & ATTR_DIRECTORY;
2077e2d9be6Schristos struct buf *bp;
2087e2d9be6Schristos struct msdosfsmount *pmp = dep->de_pmp;
2097e2d9be6Schristos
2107e2d9be6Schristos #ifdef MSDOSFS_DEBUG
2117e2d9be6Schristos printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
2127e2d9be6Schristos #endif
2137e2d9be6Schristos
2147e2d9be6Schristos /*
2157e2d9be6Schristos * Disallow attempts to truncate the root directory since it is of
2167e2d9be6Schristos * fixed size. That's just the way dos filesystems are. We use
2177e2d9be6Schristos * the VROOT bit in the vnode because checking for the directory
2187e2d9be6Schristos * bit and a startcluster of 0 in the denode is not adequate to
2197e2d9be6Schristos * recognize the root directory at this point in a file or
2207e2d9be6Schristos * directory's life.
2217e2d9be6Schristos */
222330e688fSchristos if (dep->de_vnode != NULL && !FAT32(pmp)) {
2237e2d9be6Schristos printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
2247e2d9be6Schristos dep->de_dirclust, dep->de_diroffset);
2257e2d9be6Schristos return (EINVAL);
2267e2d9be6Schristos }
2277e2d9be6Schristos
2287e2d9be6Schristos if (dep->de_FileSize < length)
229*8086f46eSthorpej return (msdosfs_deextend(dep, length, cred));
2307e2d9be6Schristos lastblock = de_clcount(pmp, length) - 1;
2317e2d9be6Schristos
2327e2d9be6Schristos /*
2337e2d9be6Schristos * If the desired length is 0 then remember the starting cluster of
2347e2d9be6Schristos * the file and set the StartCluster field in the directory entry
2357e2d9be6Schristos * to 0. If the desired length is not zero, then get the number of
2367e2d9be6Schristos * the last cluster in the shortened file. Then get the number of
2377e2d9be6Schristos * the first cluster in the part of the file that is to be freed.
2387e2d9be6Schristos * Then set the next cluster pointer in the last cluster of the
2397e2d9be6Schristos * file to CLUST_EOFE.
2407e2d9be6Schristos */
2417e2d9be6Schristos if (length == 0) {
2427e2d9be6Schristos chaintofree = dep->de_StartCluster;
2437e2d9be6Schristos dep->de_StartCluster = 0;
2447e2d9be6Schristos eofentry = ~0;
2457e2d9be6Schristos } else {
246*8086f46eSthorpej error = msdosfs_pcbmap(dep, lastblock, 0, &eofentry, 0);
2477e2d9be6Schristos if (error) {
2487e2d9be6Schristos #ifdef MSDOSFS_DEBUG
2497e2d9be6Schristos printf("detrunc(): pcbmap fails %d\n", error);
2507e2d9be6Schristos #endif
2517e2d9be6Schristos return (error);
2527e2d9be6Schristos }
2537e2d9be6Schristos }
2547e2d9be6Schristos
2557e2d9be6Schristos /*
2567e2d9be6Schristos * If the new length is not a multiple of the cluster size then we
2577e2d9be6Schristos * must zero the tail end of the new last cluster in case it
2587e2d9be6Schristos * becomes part of the file again because of a seek.
2597e2d9be6Schristos */
2607e2d9be6Schristos if ((boff = length & pmp->pm_crbomask) != 0) {
2617e2d9be6Schristos if (isadir) {
2627e2d9be6Schristos bn = cntobn(pmp, eofentry);
2637e2d9be6Schristos error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
264aa7e0e4bSagc pmp->pm_bpcluster, B_MODIFY, &bp);
2657e2d9be6Schristos if (error) {
2667e2d9be6Schristos #ifdef MSDOSFS_DEBUG
2677e2d9be6Schristos printf("detrunc(): bread fails %d\n", error);
2687e2d9be6Schristos #endif
2697e2d9be6Schristos return (error);
2707e2d9be6Schristos }
2717e2d9be6Schristos memset((char *)bp->b_data + boff, 0,
2727e2d9be6Schristos pmp->pm_bpcluster - boff);
2737e2d9be6Schristos if (flags & IO_SYNC)
2747e2d9be6Schristos bwrite(bp);
2757e2d9be6Schristos else
2767e2d9be6Schristos bdwrite(bp);
2777e2d9be6Schristos }
2787e2d9be6Schristos }
2797e2d9be6Schristos
2807e2d9be6Schristos /*
2817e2d9be6Schristos * Write out the updated directory entry. Even if the update fails
2827e2d9be6Schristos * we free the trailing clusters.
2837e2d9be6Schristos */
2847e2d9be6Schristos dep->de_FileSize = length;
2857e2d9be6Schristos if (!isadir)
2867e2d9be6Schristos dep->de_flag |= DE_UPDATE|DE_MODIFIED;
2877e2d9be6Schristos #ifdef MSDOSFS_DEBUG
2887e2d9be6Schristos printf("detrunc(): allerror %d, eofentry %lu\n",
2897e2d9be6Schristos allerror, eofentry);
2907e2d9be6Schristos #endif
2917e2d9be6Schristos
2927e2d9be6Schristos /*
2937e2d9be6Schristos * If we need to break the cluster chain for the file then do it
2947e2d9be6Schristos * now.
2957e2d9be6Schristos */
2967e2d9be6Schristos if (eofentry != (u_long)~0) {
297*8086f46eSthorpej error = msdosfs_fatentry(FAT_GET_AND_SET, pmp, eofentry,
2987e2d9be6Schristos &chaintofree, CLUST_EOFE);
2997e2d9be6Schristos if (error) {
3007e2d9be6Schristos #ifdef MSDOSFS_DEBUG
3017e2d9be6Schristos printf("detrunc(): fatentry errors %d\n", error);
3027e2d9be6Schristos #endif
3037e2d9be6Schristos return (error);
3047e2d9be6Schristos }
3057e2d9be6Schristos }
3067e2d9be6Schristos
3077e2d9be6Schristos /*
3087e2d9be6Schristos * Now free the clusters removed from the file because of the
3097e2d9be6Schristos * truncation.
3107e2d9be6Schristos */
3117e2d9be6Schristos if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
312*8086f46eSthorpej msdosfs_freeclusterchain(pmp, chaintofree);
3137e2d9be6Schristos
3147e2d9be6Schristos return (allerror);
3157e2d9be6Schristos }
3167e2d9be6Schristos
3177e2d9be6Schristos /*
3187e2d9be6Schristos * Extend the file described by dep to length specified by length.
3197e2d9be6Schristos */
3207e2d9be6Schristos int
msdosfs_deextend(struct denode * dep,u_long length,struct kauth_cred * cred)321*8086f46eSthorpej msdosfs_deextend(struct denode *dep, u_long length, struct kauth_cred *cred)
3227e2d9be6Schristos {
3237e2d9be6Schristos struct msdosfsmount *pmp = dep->de_pmp;
3240a77b69aSchristos u_long count;
3257e2d9be6Schristos int error;
3267e2d9be6Schristos
3277e2d9be6Schristos /*
3287e2d9be6Schristos * The root of a DOS filesystem cannot be extended.
3297e2d9be6Schristos */
330330e688fSchristos if (dep->de_vnode != NULL && !FAT32(pmp))
331ed8edcd6Schristos return EINVAL;
3327e2d9be6Schristos
3337e2d9be6Schristos /*
3347e2d9be6Schristos * Directories cannot be extended.
3357e2d9be6Schristos */
3367e2d9be6Schristos if (dep->de_Attributes & ATTR_DIRECTORY)
337ed8edcd6Schristos return EISDIR;
3387e2d9be6Schristos
3397e2d9be6Schristos if (length <= dep->de_FileSize)
340ed8edcd6Schristos return E2BIG;
3417e2d9be6Schristos
3427e2d9be6Schristos /*
3437e2d9be6Schristos * Compute the number of clusters to allocate.
3447e2d9be6Schristos */
3457e2d9be6Schristos count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
3467e2d9be6Schristos if (count > 0) {
3477e2d9be6Schristos if (count > pmp->pm_freeclustercount)
3487e2d9be6Schristos return (ENOSPC);
349*8086f46eSthorpej error = msdosfs_extendfile(dep, count, NULL, NULL, DE_CLEAR);
3507e2d9be6Schristos if (error) {
3517e2d9be6Schristos /* truncate the added clusters away again */
352*8086f46eSthorpej (void) msdosfs_detrunc(dep, dep->de_FileSize, 0, cred);
3537e2d9be6Schristos return (error);
3547e2d9be6Schristos }
3557e2d9be6Schristos }
3567e2d9be6Schristos
3577e2d9be6Schristos /*
3587e2d9be6Schristos * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
3597e2d9be6Schristos * memset(); we set the write size so ubc won't read in file data that
3607e2d9be6Schristos * is zero'd later.
3617e2d9be6Schristos */
3627e2d9be6Schristos dep->de_FileSize = length;
3637e2d9be6Schristos dep->de_flag |= DE_UPDATE|DE_MODIFIED;
3647e2d9be6Schristos return 0;
3657e2d9be6Schristos }
366