120f6ddd0STomohiro Kusumi /* $NetBSD: msdosfs_denode.c,v 1.7 2015/03/29 05:52:59 agc Exp $ */
220f6ddd0STomohiro Kusumi
320f6ddd0STomohiro Kusumi /*-
420f6ddd0STomohiro Kusumi * SPDX-License-Identifier: BSD-4-Clause
520f6ddd0STomohiro Kusumi *
620f6ddd0STomohiro Kusumi * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
720f6ddd0STomohiro Kusumi * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
820f6ddd0STomohiro Kusumi * All rights reserved.
920f6ddd0STomohiro Kusumi * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
1020f6ddd0STomohiro Kusumi *
1120f6ddd0STomohiro Kusumi * Redistribution and use in source and binary forms, with or without
1220f6ddd0STomohiro Kusumi * modification, are permitted provided that the following conditions
1320f6ddd0STomohiro Kusumi * are met:
1420f6ddd0STomohiro Kusumi * 1. Redistributions of source code must retain the above copyright
1520f6ddd0STomohiro Kusumi * notice, this list of conditions and the following disclaimer.
1620f6ddd0STomohiro Kusumi * 2. Redistributions in binary form must reproduce the above copyright
1720f6ddd0STomohiro Kusumi * notice, this list of conditions and the following disclaimer in the
1820f6ddd0STomohiro Kusumi * documentation and/or other materials provided with the distribution.
1920f6ddd0STomohiro Kusumi * 3. All advertising materials mentioning features or use of this software
2020f6ddd0STomohiro Kusumi * must display the following acknowledgement:
2120f6ddd0STomohiro Kusumi * This product includes software developed by TooLs GmbH.
2220f6ddd0STomohiro Kusumi * 4. The name of TooLs GmbH may not be used to endorse or promote products
2320f6ddd0STomohiro Kusumi * derived from this software without specific prior written permission.
2420f6ddd0STomohiro Kusumi *
2520f6ddd0STomohiro Kusumi * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2620f6ddd0STomohiro Kusumi * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2720f6ddd0STomohiro Kusumi * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2820f6ddd0STomohiro Kusumi * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2920f6ddd0STomohiro Kusumi * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3020f6ddd0STomohiro Kusumi * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3120f6ddd0STomohiro Kusumi * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3220f6ddd0STomohiro Kusumi * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3320f6ddd0STomohiro Kusumi * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3420f6ddd0STomohiro Kusumi * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3520f6ddd0STomohiro Kusumi */
3620f6ddd0STomohiro Kusumi /*-
3720f6ddd0STomohiro Kusumi * Written by Paul Popelka (paulp@uts.amdahl.com)
3820f6ddd0STomohiro Kusumi *
3920f6ddd0STomohiro Kusumi * You can do anything you want with this software, just don't say you wrote
4020f6ddd0STomohiro Kusumi * it, and don't remove this notice.
4120f6ddd0STomohiro Kusumi *
4220f6ddd0STomohiro Kusumi * This software is provided "as is".
4320f6ddd0STomohiro Kusumi *
4420f6ddd0STomohiro Kusumi * The author supplies this software to be publicly redistributed on the
4520f6ddd0STomohiro Kusumi * understanding that the author is not responsible for the correct
4620f6ddd0STomohiro Kusumi * functioning of this software in any circumstances and is not liable for
4720f6ddd0STomohiro Kusumi * any damages caused by this software.
4820f6ddd0STomohiro Kusumi *
4920f6ddd0STomohiro Kusumi * October 1992
5020f6ddd0STomohiro Kusumi */
5120f6ddd0STomohiro Kusumi
5220f6ddd0STomohiro Kusumi #include <sys/cdefs.h>
5320f6ddd0STomohiro Kusumi __FBSDID("$FreeBSD$");
5420f6ddd0STomohiro Kusumi
5520f6ddd0STomohiro Kusumi #include <sys/param.h>
5620f6ddd0STomohiro Kusumi #include <sys/errno.h>
5720f6ddd0STomohiro Kusumi
5820f6ddd0STomohiro Kusumi #include <stdbool.h>
5920f6ddd0STomohiro Kusumi #include <stdio.h>
6020f6ddd0STomohiro Kusumi #include <string.h>
6120f6ddd0STomohiro Kusumi #include <stdlib.h>
6220f6ddd0STomohiro Kusumi #include <util.h>
6320f6ddd0STomohiro Kusumi
6420f6ddd0STomohiro Kusumi #include <vfs/msdosfs/bpb.h>
6520f6ddd0STomohiro Kusumi #include "msdos/denode.h"
6620f6ddd0STomohiro Kusumi #include <vfs/msdosfs/fat.h>
6720f6ddd0STomohiro Kusumi #include <vfs/msdosfs/msdosfsmount.h>
6820f6ddd0STomohiro Kusumi
6920f6ddd0STomohiro Kusumi #include "makefs.h"
7020f6ddd0STomohiro Kusumi #include "msdos.h"
7120f6ddd0STomohiro Kusumi
7220f6ddd0STomohiro Kusumi
7320f6ddd0STomohiro Kusumi /*
7420f6ddd0STomohiro Kusumi * If deget() succeeds it returns with the gotten denode locked().
7520f6ddd0STomohiro Kusumi *
7620f6ddd0STomohiro Kusumi * pmp - address of msdosfsmount structure of the filesystem containing
7720f6ddd0STomohiro Kusumi * the denode of interest. The pm_dev field and the address of
7820f6ddd0STomohiro Kusumi * the msdosfsmount structure are used.
7920f6ddd0STomohiro Kusumi * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
8020f6ddd0STomohiro Kusumi * diroffset is relative to the beginning of the root directory,
8120f6ddd0STomohiro Kusumi * otherwise it is cluster relative.
8220f6ddd0STomohiro Kusumi * diroffset - offset past begin of cluster of denode we want
8320f6ddd0STomohiro Kusumi * depp - returns the address of the gotten denode.
8420f6ddd0STomohiro Kusumi */
8520f6ddd0STomohiro Kusumi int
deget(struct msdosfsmount * pmp,u_long dirclust,u_long diroffset,struct denode ** depp)8620f6ddd0STomohiro Kusumi deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
8720f6ddd0STomohiro Kusumi struct denode **depp)
8820f6ddd0STomohiro Kusumi {
8920f6ddd0STomohiro Kusumi int error;
9020f6ddd0STomohiro Kusumi uint64_t inode;
9120f6ddd0STomohiro Kusumi struct direntry *direntptr;
9220f6ddd0STomohiro Kusumi struct denode *ldep;
9320f6ddd0STomohiro Kusumi struct m_buf *bp;
9420f6ddd0STomohiro Kusumi
9520f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
9620f6ddd0STomohiro Kusumi pmp, dirclust, diroffset, depp));
9720f6ddd0STomohiro Kusumi
9820f6ddd0STomohiro Kusumi /*
9920f6ddd0STomohiro Kusumi * On FAT32 filesystems, root is a (more or less) normal
10020f6ddd0STomohiro Kusumi * directory
10120f6ddd0STomohiro Kusumi */
10220f6ddd0STomohiro Kusumi if (FAT32(pmp) && dirclust == MSDOSFSROOT)
10320f6ddd0STomohiro Kusumi dirclust = pmp->pm_rootdirblk;
10420f6ddd0STomohiro Kusumi
10520f6ddd0STomohiro Kusumi inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
10620f6ddd0STomohiro Kusumi
10720f6ddd0STomohiro Kusumi ldep = ecalloc(1, sizeof(*ldep));
10820f6ddd0STomohiro Kusumi ldep->de_vnode = NULL;
10920f6ddd0STomohiro Kusumi ldep->de_flag = 0;
11020f6ddd0STomohiro Kusumi ldep->de_dirclust = dirclust;
11120f6ddd0STomohiro Kusumi ldep->de_diroffset = diroffset;
11220f6ddd0STomohiro Kusumi /* XXX unused + non existent in DragonFly */
11320f6ddd0STomohiro Kusumi //ldep->de_inode = inode;
11420f6ddd0STomohiro Kusumi ldep->de_pmp = pmp;
11520f6ddd0STomohiro Kusumi ldep->de_refcnt = 1;
11620f6ddd0STomohiro Kusumi fc_purge(ldep, 0); /* init the FAT cache for this denode */
11720f6ddd0STomohiro Kusumi /*
11820f6ddd0STomohiro Kusumi * Copy the directory entry into the denode area of the vnode.
11920f6ddd0STomohiro Kusumi */
12020f6ddd0STomohiro Kusumi if ((dirclust == MSDOSFSROOT
12120f6ddd0STomohiro Kusumi || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
12220f6ddd0STomohiro Kusumi && diroffset == MSDOSFSROOT_OFS) {
12320f6ddd0STomohiro Kusumi /*
12420f6ddd0STomohiro Kusumi * Directory entry for the root directory. There isn't one,
12520f6ddd0STomohiro Kusumi * so we manufacture one. We should probably rummage
12620f6ddd0STomohiro Kusumi * through the root directory and find a label entry (if it
12720f6ddd0STomohiro Kusumi * exists), and then use the time and date from that entry
12820f6ddd0STomohiro Kusumi * as the time and date for the root denode.
12920f6ddd0STomohiro Kusumi */
13020f6ddd0STomohiro Kusumi ldep->de_vnode = (struct vnode *)-1;
13120f6ddd0STomohiro Kusumi
13220f6ddd0STomohiro Kusumi ldep->de_Attributes = ATTR_DIRECTORY;
13320f6ddd0STomohiro Kusumi ldep->de_LowerCase = 0;
13420f6ddd0STomohiro Kusumi if (FAT32(pmp))
13520f6ddd0STomohiro Kusumi ldep->de_StartCluster = pmp->pm_rootdirblk;
13620f6ddd0STomohiro Kusumi /* de_FileSize will be filled in further down */
13720f6ddd0STomohiro Kusumi else {
13820f6ddd0STomohiro Kusumi ldep->de_StartCluster = MSDOSFSROOT;
13920f6ddd0STomohiro Kusumi ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
14020f6ddd0STomohiro Kusumi }
14120f6ddd0STomohiro Kusumi /*
14220f6ddd0STomohiro Kusumi * fill in time and date so that dos2unixtime() doesn't
14320f6ddd0STomohiro Kusumi * spit up when called from msdosfs_getattr() with root
14420f6ddd0STomohiro Kusumi * denode
14520f6ddd0STomohiro Kusumi */
14620f6ddd0STomohiro Kusumi ldep->de_CHun = 0;
14720f6ddd0STomohiro Kusumi ldep->de_CTime = 0x0000; /* 00:00:00 */
14820f6ddd0STomohiro Kusumi ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
14920f6ddd0STomohiro Kusumi | (1 << DD_DAY_SHIFT);
15020f6ddd0STomohiro Kusumi /* Jan 1, 1980 */
15120f6ddd0STomohiro Kusumi ldep->de_ADate = ldep->de_CDate;
15220f6ddd0STomohiro Kusumi ldep->de_MTime = ldep->de_CTime;
15320f6ddd0STomohiro Kusumi ldep->de_MDate = ldep->de_CDate;
15420f6ddd0STomohiro Kusumi /* leave the other fields as garbage */
15520f6ddd0STomohiro Kusumi } else {
15620f6ddd0STomohiro Kusumi error = m_readep(pmp, dirclust, diroffset, &bp, &direntptr);
15720f6ddd0STomohiro Kusumi if (error) {
15820f6ddd0STomohiro Kusumi ldep->de_Name[0] = SLOT_DELETED;
15920f6ddd0STomohiro Kusumi
16020f6ddd0STomohiro Kusumi *depp = NULL;
16120f6ddd0STomohiro Kusumi return (error);
16220f6ddd0STomohiro Kusumi }
16320f6ddd0STomohiro Kusumi (void)DE_INTERNALIZE(ldep, direntptr);
16420f6ddd0STomohiro Kusumi brelse(bp);
16520f6ddd0STomohiro Kusumi }
16620f6ddd0STomohiro Kusumi
16720f6ddd0STomohiro Kusumi /*
16820f6ddd0STomohiro Kusumi * Fill in a few fields of the vnode and finish filling in the
16920f6ddd0STomohiro Kusumi * denode. Then return the address of the found denode.
17020f6ddd0STomohiro Kusumi */
17120f6ddd0STomohiro Kusumi if (ldep->de_Attributes & ATTR_DIRECTORY) {
17220f6ddd0STomohiro Kusumi /*
17320f6ddd0STomohiro Kusumi * Since DOS directory entries that describe directories
17420f6ddd0STomohiro Kusumi * have 0 in the filesize field, we take this opportunity
17520f6ddd0STomohiro Kusumi * to find out the length of the directory and plug it into
17620f6ddd0STomohiro Kusumi * the denode structure.
17720f6ddd0STomohiro Kusumi */
17820f6ddd0STomohiro Kusumi u_long size;
17920f6ddd0STomohiro Kusumi
18020f6ddd0STomohiro Kusumi /*
18120f6ddd0STomohiro Kusumi * XXX it sometimes happens that the "." entry has cluster
18220f6ddd0STomohiro Kusumi * number 0 when it shouldn't. Use the actual cluster number
18320f6ddd0STomohiro Kusumi * instead of what is written in directory entry.
18420f6ddd0STomohiro Kusumi */
18520f6ddd0STomohiro Kusumi if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
18620f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n",
18720f6ddd0STomohiro Kusumi dirclust, ldep->de_StartCluster));
18820f6ddd0STomohiro Kusumi
18920f6ddd0STomohiro Kusumi ldep->de_StartCluster = dirclust;
19020f6ddd0STomohiro Kusumi }
19120f6ddd0STomohiro Kusumi
19220f6ddd0STomohiro Kusumi if (ldep->de_StartCluster != MSDOSFSROOT) {
19320f6ddd0STomohiro Kusumi error = pcbmap(ldep, 0xffff, 0, &size, 0);
19420f6ddd0STomohiro Kusumi if (error == E2BIG) {
19520f6ddd0STomohiro Kusumi ldep->de_FileSize = de_cn2off(pmp, size);
19620f6ddd0STomohiro Kusumi error = 0;
19720f6ddd0STomohiro Kusumi } else {
19820f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n",
19920f6ddd0STomohiro Kusumi error));
20020f6ddd0STomohiro Kusumi }
20120f6ddd0STomohiro Kusumi }
20220f6ddd0STomohiro Kusumi }
20320f6ddd0STomohiro Kusumi *depp = ldep;
20420f6ddd0STomohiro Kusumi return (0);
20520f6ddd0STomohiro Kusumi }
20620f6ddd0STomohiro Kusumi
20720f6ddd0STomohiro Kusumi /*
20820f6ddd0STomohiro Kusumi * Truncate the file described by dep to the length specified by length.
20920f6ddd0STomohiro Kusumi */
21020f6ddd0STomohiro Kusumi int
detrunc(struct denode * dep,u_long length,int flags)21120f6ddd0STomohiro Kusumi detrunc(struct denode *dep, u_long length, int flags)
21220f6ddd0STomohiro Kusumi {
21320f6ddd0STomohiro Kusumi int error;
21420f6ddd0STomohiro Kusumi u_long eofentry;
21520f6ddd0STomohiro Kusumi u_long chaintofree;
21620f6ddd0STomohiro Kusumi daddr_t bn;
21720f6ddd0STomohiro Kusumi int boff;
21820f6ddd0STomohiro Kusumi int isadir = dep->de_Attributes & ATTR_DIRECTORY;
21920f6ddd0STomohiro Kusumi struct m_buf *bp;
22020f6ddd0STomohiro Kusumi struct msdosfsmount *pmp = dep->de_pmp;
22120f6ddd0STomohiro Kusumi
22220f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n",
22320f6ddd0STomohiro Kusumi dep->de_Name, length, flags));
22420f6ddd0STomohiro Kusumi
22520f6ddd0STomohiro Kusumi /*
22620f6ddd0STomohiro Kusumi * Disallow attempts to truncate the root directory since it is of
22720f6ddd0STomohiro Kusumi * fixed size. That's just the way dos filesystems are. We use
22820f6ddd0STomohiro Kusumi * the VROOT bit in the vnode because checking for the directory
22920f6ddd0STomohiro Kusumi * bit and a startcluster of 0 in the denode is not adequate to
23020f6ddd0STomohiro Kusumi * recognize the root directory at this point in a file or
23120f6ddd0STomohiro Kusumi * directory's life.
23220f6ddd0STomohiro Kusumi */
23320f6ddd0STomohiro Kusumi if (dep->de_vnode != NULL && !FAT32(pmp)) {
23420f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, "
23520f6ddd0STomohiro Kusumi "clust %ld, offset %ld\n",
23620f6ddd0STomohiro Kusumi dep->de_dirclust, dep->de_diroffset));
23720f6ddd0STomohiro Kusumi
23820f6ddd0STomohiro Kusumi return (EINVAL);
23920f6ddd0STomohiro Kusumi }
24020f6ddd0STomohiro Kusumi
24120f6ddd0STomohiro Kusumi if (dep->de_FileSize < length)
24220f6ddd0STomohiro Kusumi return deextend(dep, length);
24320f6ddd0STomohiro Kusumi
24420f6ddd0STomohiro Kusumi /*
24520f6ddd0STomohiro Kusumi * If the desired length is 0 then remember the starting cluster of
24620f6ddd0STomohiro Kusumi * the file and set the StartCluster field in the directory entry
24720f6ddd0STomohiro Kusumi * to 0. If the desired length is not zero, then get the number of
24820f6ddd0STomohiro Kusumi * the last cluster in the shortened file. Then get the number of
24920f6ddd0STomohiro Kusumi * the first cluster in the part of the file that is to be freed.
25020f6ddd0STomohiro Kusumi * Then set the next cluster pointer in the last cluster of the
25120f6ddd0STomohiro Kusumi * file to CLUST_EOFE.
25220f6ddd0STomohiro Kusumi */
25320f6ddd0STomohiro Kusumi if (length == 0) {
25420f6ddd0STomohiro Kusumi chaintofree = dep->de_StartCluster;
25520f6ddd0STomohiro Kusumi dep->de_StartCluster = 0;
256*2294e82fSTomohiro Kusumi eofentry = ~0ul;
25720f6ddd0STomohiro Kusumi } else {
25820f6ddd0STomohiro Kusumi error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
25920f6ddd0STomohiro Kusumi &eofentry, 0);
26020f6ddd0STomohiro Kusumi if (error) {
26120f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n",
26220f6ddd0STomohiro Kusumi error));
26320f6ddd0STomohiro Kusumi return (error);
26420f6ddd0STomohiro Kusumi }
26520f6ddd0STomohiro Kusumi }
26620f6ddd0STomohiro Kusumi
26720f6ddd0STomohiro Kusumi fc_purge(dep, de_clcount(pmp, length));
26820f6ddd0STomohiro Kusumi
26920f6ddd0STomohiro Kusumi /*
27020f6ddd0STomohiro Kusumi * If the new length is not a multiple of the cluster size then we
27120f6ddd0STomohiro Kusumi * must zero the tail end of the new last cluster in case it
27220f6ddd0STomohiro Kusumi * becomes part of the file again because of a seek.
27320f6ddd0STomohiro Kusumi */
27420f6ddd0STomohiro Kusumi if ((boff = length & pmp->pm_crbomask) != 0) {
27520f6ddd0STomohiro Kusumi if (isadir) {
27620f6ddd0STomohiro Kusumi bn = cntobn(pmp, eofentry);
27720f6ddd0STomohiro Kusumi error = bread((void *)pmp->pm_devvp, bn,
27820f6ddd0STomohiro Kusumi pmp->pm_bpcluster, 0, &bp);
27920f6ddd0STomohiro Kusumi if (error) {
28020f6ddd0STomohiro Kusumi brelse(bp);
28120f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n",
28220f6ddd0STomohiro Kusumi error));
28320f6ddd0STomohiro Kusumi
28420f6ddd0STomohiro Kusumi return (error);
28520f6ddd0STomohiro Kusumi }
28620f6ddd0STomohiro Kusumi memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
28720f6ddd0STomohiro Kusumi bwrite(bp);
28820f6ddd0STomohiro Kusumi }
28920f6ddd0STomohiro Kusumi }
29020f6ddd0STomohiro Kusumi
29120f6ddd0STomohiro Kusumi /*
29220f6ddd0STomohiro Kusumi * Write out the updated directory entry. Even if the update fails
29320f6ddd0STomohiro Kusumi * we free the trailing clusters.
29420f6ddd0STomohiro Kusumi */
29520f6ddd0STomohiro Kusumi dep->de_FileSize = length;
29620f6ddd0STomohiro Kusumi if (!isadir)
29720f6ddd0STomohiro Kusumi dep->de_flag |= DE_UPDATE|DE_MODIFIED;
298*2294e82fSTomohiro Kusumi MSDOSFS_DPRINTF(("detrunc(): eofentry %lu\n", eofentry));
29920f6ddd0STomohiro Kusumi
30020f6ddd0STomohiro Kusumi /*
30120f6ddd0STomohiro Kusumi * If we need to break the cluster chain for the file then do it
30220f6ddd0STomohiro Kusumi * now.
30320f6ddd0STomohiro Kusumi */
304*2294e82fSTomohiro Kusumi if (eofentry != ~0ul) {
30520f6ddd0STomohiro Kusumi error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
30620f6ddd0STomohiro Kusumi &chaintofree, CLUST_EOFE);
30720f6ddd0STomohiro Kusumi if (error) {
30820f6ddd0STomohiro Kusumi MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n",
30920f6ddd0STomohiro Kusumi error));
31020f6ddd0STomohiro Kusumi return (error);
31120f6ddd0STomohiro Kusumi }
31220f6ddd0STomohiro Kusumi fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
31320f6ddd0STomohiro Kusumi eofentry);
31420f6ddd0STomohiro Kusumi }
31520f6ddd0STomohiro Kusumi
31620f6ddd0STomohiro Kusumi /*
31720f6ddd0STomohiro Kusumi * Now free the clusters removed from the file because of the
31820f6ddd0STomohiro Kusumi * truncation.
31920f6ddd0STomohiro Kusumi */
32020f6ddd0STomohiro Kusumi if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
32120f6ddd0STomohiro Kusumi freeclusterchain(pmp, chaintofree);
32220f6ddd0STomohiro Kusumi
323*2294e82fSTomohiro Kusumi return (0);
32420f6ddd0STomohiro Kusumi }
32520f6ddd0STomohiro Kusumi
32620f6ddd0STomohiro Kusumi /*
32720f6ddd0STomohiro Kusumi * Extend the file described by dep to length specified by length.
32820f6ddd0STomohiro Kusumi */
32920f6ddd0STomohiro Kusumi int
deextend(struct denode * dep,u_long length)33020f6ddd0STomohiro Kusumi deextend(struct denode *dep, u_long length)
33120f6ddd0STomohiro Kusumi {
33220f6ddd0STomohiro Kusumi struct msdosfsmount *pmp = dep->de_pmp;
33320f6ddd0STomohiro Kusumi u_long count;
33420f6ddd0STomohiro Kusumi int error;
33520f6ddd0STomohiro Kusumi
33620f6ddd0STomohiro Kusumi /*
33720f6ddd0STomohiro Kusumi * The root of a DOS filesystem cannot be extended.
33820f6ddd0STomohiro Kusumi */
33920f6ddd0STomohiro Kusumi if (dep->de_vnode != NULL && !FAT32(pmp))
34020f6ddd0STomohiro Kusumi return (EINVAL);
34120f6ddd0STomohiro Kusumi
34220f6ddd0STomohiro Kusumi /*
34320f6ddd0STomohiro Kusumi * Directories cannot be extended.
34420f6ddd0STomohiro Kusumi */
34520f6ddd0STomohiro Kusumi if (dep->de_Attributes & ATTR_DIRECTORY)
34620f6ddd0STomohiro Kusumi return (EISDIR);
34720f6ddd0STomohiro Kusumi
34820f6ddd0STomohiro Kusumi if (length <= dep->de_FileSize)
34920f6ddd0STomohiro Kusumi return (E2BIG);
35020f6ddd0STomohiro Kusumi
35120f6ddd0STomohiro Kusumi /*
35220f6ddd0STomohiro Kusumi * Compute the number of clusters to allocate.
35320f6ddd0STomohiro Kusumi */
35420f6ddd0STomohiro Kusumi count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
35520f6ddd0STomohiro Kusumi if (count > 0) {
35620f6ddd0STomohiro Kusumi if (count > pmp->pm_freeclustercount)
35720f6ddd0STomohiro Kusumi return (ENOSPC);
35820f6ddd0STomohiro Kusumi error = m_extendfile(dep, count, NULL, NULL, DE_CLEAR);
35920f6ddd0STomohiro Kusumi if (error) {
36020f6ddd0STomohiro Kusumi /* truncate the added clusters away again */
36120f6ddd0STomohiro Kusumi (void) detrunc(dep, dep->de_FileSize, 0);
36220f6ddd0STomohiro Kusumi return (error);
36320f6ddd0STomohiro Kusumi }
36420f6ddd0STomohiro Kusumi }
36520f6ddd0STomohiro Kusumi
36620f6ddd0STomohiro Kusumi /*
36720f6ddd0STomohiro Kusumi * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
36820f6ddd0STomohiro Kusumi * memset(); we set the write size so ubc won't read in file data that
36920f6ddd0STomohiro Kusumi * is zero'd later.
37020f6ddd0STomohiro Kusumi */
37120f6ddd0STomohiro Kusumi dep->de_FileSize = length;
37220f6ddd0STomohiro Kusumi dep->de_flag |= DE_UPDATE | DE_MODIFIED;
37320f6ddd0STomohiro Kusumi return 0;
37420f6ddd0STomohiro Kusumi }
375