1999-08-28 01:08:13 +00:00
|
|
|
/* $FreeBSD$ */
|
1998-02-18 09:28:47 +00:00
|
|
|
/* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*-
|
1998-02-18 09:28:47 +00:00
|
|
|
* Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1994, 1995, 1997 TooLs GmbH.
|
1994-09-19 15:41:57 +00:00
|
|
|
* All rights reserved.
|
|
|
|
* Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2005-01-06 18:10:42 +00:00
|
|
|
/*-
|
1994-09-19 15:41:57 +00:00
|
|
|
* Written by Paul Popelka (paulp@uts.amdahl.com)
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1994-09-19 15:41:57 +00:00
|
|
|
* You can do anything you want with this software, just don't say you wrote
|
|
|
|
* it, and don't remove this notice.
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1994-09-19 15:41:57 +00:00
|
|
|
* This software is provided "as is".
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1994-09-19 15:41:57 +00:00
|
|
|
* The author supplies this software to be publicly redistributed on the
|
|
|
|
* understanding that the author is not responsible for the correct
|
|
|
|
* functioning of this software in any circumstances and is not liable for
|
|
|
|
* any damages caused by this software.
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1994-09-19 15:41:57 +00:00
|
|
|
* October 1992
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
2006-10-24 11:14:05 +00:00
|
|
|
#include <sys/clock.h>
|
2007-08-07 02:08:06 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mount.h>
|
1994-09-19 15:41:57 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
1994-09-27 20:42:59 +00:00
|
|
|
#include <vm/vm.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1994-09-27 20:42:59 +00:00
|
|
|
|
2001-05-25 08:14:14 +00:00
|
|
|
#include <fs/msdosfs/bpb.h>
|
|
|
|
#include <fs/msdosfs/direntry.h>
|
|
|
|
#include <fs/msdosfs/denode.h>
|
|
|
|
#include <fs/msdosfs/fat.h>
|
2007-08-07 02:08:06 +00:00
|
|
|
#include <fs/msdosfs/msdosfsmount.h>
|
1994-09-19 15:41:57 +00:00
|
|
|
|
2005-10-31 15:41:29 +00:00
|
|
|
static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part");
|
1997-10-11 18:31:40 +00:00
|
|
|
|
2005-04-07 07:55:37 +00:00
|
|
|
static int
|
|
|
|
de_vncmpf(struct vnode *vp, void *arg)
|
|
|
|
{
|
|
|
|
struct denode *de;
|
|
|
|
uint64_t *a;
|
|
|
|
|
|
|
|
a = arg;
|
|
|
|
de = VTODE(vp);
|
|
|
|
return (de->de_inode != *a);
|
|
|
|
}
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
1995-05-30 08:16:23 +00:00
|
|
|
* If deget() succeeds it returns with the gotten denode locked().
|
1994-09-19 15:41:57 +00:00
|
|
|
*
|
|
|
|
* pmp - address of msdosfsmount structure of the filesystem containing
|
2005-03-15 20:57:25 +00:00
|
|
|
* the denode of interest. The address of
|
1995-05-30 08:16:23 +00:00
|
|
|
* the msdosfsmount structure are used.
|
1994-09-19 15:41:57 +00:00
|
|
|
* dirclust - which cluster bp contains, if dirclust is 0 (root directory)
|
|
|
|
* diroffset is relative to the beginning of the root directory,
|
1995-05-30 08:16:23 +00:00
|
|
|
* otherwise it is cluster relative.
|
|
|
|
* diroffset - offset past begin of cluster of denode we want
|
1994-09-19 15:41:57 +00:00
|
|
|
* depp - returns the address of the gotten denode.
|
|
|
|
*/
|
|
|
|
int
|
1998-02-18 09:28:47 +00:00
|
|
|
deget(pmp, dirclust, diroffset, depp)
|
1994-09-19 15:41:57 +00:00
|
|
|
struct msdosfsmount *pmp; /* so we know the maj/min number */
|
|
|
|
u_long dirclust; /* cluster this dir entry came from */
|
|
|
|
u_long diroffset; /* index of entry within the cluster */
|
|
|
|
struct denode **depp; /* returns the addr of the gotten denode */
|
|
|
|
{
|
|
|
|
int error;
|
2005-04-07 07:55:37 +00:00
|
|
|
uint64_t inode;
|
1994-09-19 15:41:57 +00:00
|
|
|
struct mount *mntp = pmp->pm_mountp;
|
1998-02-18 09:28:47 +00:00
|
|
|
struct direntry *direntptr;
|
1994-09-19 15:41:57 +00:00
|
|
|
struct denode *ldep;
|
2005-03-14 12:24:35 +00:00
|
|
|
struct vnode *nvp, *xvp;
|
1994-09-19 15:41:57 +00:00
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
1998-02-18 09:28:47 +00:00
|
|
|
printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
|
|
|
|
pmp, dirclust, diroffset, depp);
|
1994-09-19 15:41:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1998-02-18 09:28:47 +00:00
|
|
|
* On FAT32 filesystems, root is a (more or less) normal
|
|
|
|
* directory
|
1994-09-19 15:41:57 +00:00
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
if (FAT32(pmp) && dirclust == MSDOSFSROOT)
|
|
|
|
dirclust = pmp->pm_rootdirblk;
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See if the denode is in the denode cache. Use the location of
|
|
|
|
* the directory entry to compute the hash value. For subdir use
|
1998-02-18 09:28:47 +00:00
|
|
|
* address of "." entry. For root dir (if not FAT32) use cluster
|
|
|
|
* MSDOSFSROOT, offset MSDOSFSROOT_OFS
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1994-09-19 15:41:57 +00:00
|
|
|
* NOTE: The check for de_refcnt > 0 below insures the denode being
|
|
|
|
* examined does not represent an unlinked but still open file.
|
|
|
|
* These files are not to be accessible even when the directory
|
|
|
|
* entry that represented the file happens to be reused while the
|
|
|
|
* deleted file is still open.
|
|
|
|
*/
|
2005-09-07 10:32:58 +00:00
|
|
|
inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
|
2005-04-07 07:55:37 +00:00
|
|
|
|
|
|
|
error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp,
|
|
|
|
de_vncmpf, &inode);
|
2005-03-14 12:24:35 +00:00
|
|
|
if (error)
|
2007-08-07 03:59:49 +00:00
|
|
|
return (error);
|
2005-03-14 12:24:35 +00:00
|
|
|
if (nvp != NULL) {
|
|
|
|
*depp = VTODE(nvp);
|
2005-04-07 07:55:37 +00:00
|
|
|
KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust"));
|
|
|
|
KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset"));
|
1998-02-18 09:28:47 +00:00
|
|
|
return (0);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
2010-02-13 12:11:03 +00:00
|
|
|
ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Directory entry was not in cache, have to create a vnode and
|
|
|
|
* copy it from the passed disk buffer.
|
|
|
|
*/
|
|
|
|
/* getnewvnode() does a VREF() on the vnode */
|
2004-12-01 23:16:38 +00:00
|
|
|
error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
|
1994-09-27 20:42:59 +00:00
|
|
|
if (error) {
|
1996-06-12 03:37:57 +00:00
|
|
|
*depp = NULL;
|
2008-10-23 15:53:51 +00:00
|
|
|
free(ldep, M_MSDOSFSNODE);
|
1994-09-19 15:41:57 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
nvp->v_data = ldep;
|
|
|
|
ldep->de_vnode = nvp;
|
|
|
|
ldep->de_flag = 0;
|
|
|
|
ldep->de_dirclust = dirclust;
|
|
|
|
ldep->de_diroffset = diroffset;
|
2005-04-07 07:55:37 +00:00
|
|
|
ldep->de_inode = inode;
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL);
|
2010-02-28 17:07:49 +00:00
|
|
|
fc_purge(ldep, 0); /* init the fat cache for this denode */
|
2007-03-13 01:50:27 +00:00
|
|
|
error = insmntque(nvp, mntp);
|
|
|
|
if (error != 0) {
|
2008-10-23 15:53:51 +00:00
|
|
|
free(ldep, M_MSDOSFSNODE);
|
2007-03-13 01:50:27 +00:00
|
|
|
*depp = NULL;
|
|
|
|
return (error);
|
|
|
|
}
|
2008-01-24 12:34:30 +00:00
|
|
|
error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp,
|
2005-04-07 07:55:37 +00:00
|
|
|
de_vncmpf, &inode);
|
2005-03-14 12:24:35 +00:00
|
|
|
if (error) {
|
|
|
|
*depp = NULL;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (xvp != NULL) {
|
2010-02-28 17:11:31 +00:00
|
|
|
*depp = xvp->v_data;
|
|
|
|
return (0);
|
2005-03-14 12:24:35 +00:00
|
|
|
}
|
1994-09-19 15:41:57 +00:00
|
|
|
|
1998-02-18 09:28:47 +00:00
|
|
|
ldep->de_pmp = pmp;
|
|
|
|
ldep->de_refcnt = 1;
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
|
|
|
* Copy the directory entry into the denode area of the vnode.
|
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
if ((dirclust == MSDOSFSROOT
|
|
|
|
|| (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
|
|
|
|
&& diroffset == MSDOSFSROOT_OFS) {
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
|
|
|
* Directory entry for the root directory. There isn't one,
|
|
|
|
* so we manufacture one. We should probably rummage
|
|
|
|
* through the root directory and find a label entry (if it
|
|
|
|
* exists), and then use the time and date from that entry
|
|
|
|
* as the time and date for the root denode.
|
|
|
|
*/
|
2002-08-04 10:29:36 +00:00
|
|
|
nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
|
1998-02-18 09:28:47 +00:00
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
ldep->de_Attributes = ATTR_DIRECTORY;
|
1998-11-21 00:20:24 +00:00
|
|
|
ldep->de_LowerCase = 0;
|
1998-02-18 09:28:47 +00:00
|
|
|
if (FAT32(pmp))
|
|
|
|
ldep->de_StartCluster = pmp->pm_rootdirblk;
|
|
|
|
/* de_FileSize will be filled in further down */
|
|
|
|
else {
|
|
|
|
ldep->de_StartCluster = MSDOSFSROOT;
|
2000-01-27 14:43:07 +00:00
|
|
|
ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
|
1998-02-18 09:28:47 +00:00
|
|
|
}
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
2006-10-24 11:14:05 +00:00
|
|
|
* fill in time and date so that fattime2timespec() doesn't
|
1994-09-19 15:41:57 +00:00
|
|
|
* spit up when called from msdosfs_getattr() with root
|
|
|
|
* denode
|
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
ldep->de_CHun = 0;
|
|
|
|
ldep->de_CTime = 0x0000; /* 00:00:00 */
|
|
|
|
ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
|
1994-09-19 15:41:57 +00:00
|
|
|
| (1 << DD_DAY_SHIFT);
|
|
|
|
/* Jan 1, 1980 */
|
1998-02-18 09:28:47 +00:00
|
|
|
ldep->de_ADate = ldep->de_CDate;
|
|
|
|
ldep->de_MTime = ldep->de_CTime;
|
|
|
|
ldep->de_MDate = ldep->de_CDate;
|
1994-09-19 15:41:57 +00:00
|
|
|
/* leave the other fields as garbage */
|
|
|
|
} else {
|
1998-02-18 09:28:47 +00:00
|
|
|
error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
|
1998-05-17 18:09:28 +00:00
|
|
|
if (error) {
|
|
|
|
/*
|
|
|
|
* The denode does not contain anything useful, so
|
|
|
|
* it would be wrong to leave it on its hash chain.
|
|
|
|
* Arrange for vput() to just forget about it.
|
|
|
|
*/
|
|
|
|
ldep->de_Name[0] = SLOT_DELETED;
|
|
|
|
|
|
|
|
vput(nvp);
|
|
|
|
*depp = NULL;
|
1998-02-18 09:28:47 +00:00
|
|
|
return (error);
|
1998-05-17 18:09:28 +00:00
|
|
|
}
|
2010-10-13 11:37:39 +00:00
|
|
|
(void)DE_INTERNALIZE(ldep, direntptr);
|
1998-02-18 09:28:47 +00:00
|
|
|
brelse(bp);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in a few fields of the vnode and finish filling in the
|
|
|
|
* denode. Then return the address of the found denode.
|
|
|
|
*/
|
|
|
|
if (ldep->de_Attributes & ATTR_DIRECTORY) {
|
|
|
|
/*
|
|
|
|
* Since DOS directory entries that describe directories
|
|
|
|
* have 0 in the filesize field, we take this opportunity
|
|
|
|
* to find out the length of the directory and plug it into
|
|
|
|
* the denode structure.
|
|
|
|
*/
|
|
|
|
u_long size;
|
|
|
|
|
2002-08-16 05:13:42 +00:00
|
|
|
/*
|
2007-08-07 03:59:49 +00:00
|
|
|
* XXX it sometimes happens that the "." entry has cluster
|
|
|
|
* number 0 when it shouldn't. Use the actual cluster number
|
2002-08-16 05:13:42 +00:00
|
|
|
* instead of what is written in directory entry.
|
|
|
|
*/
|
2007-08-07 03:59:49 +00:00
|
|
|
if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
|
2011-11-22 13:30:36 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
2007-08-07 03:59:49 +00:00
|
|
|
printf("deget(): \".\" entry at clust %lu != %lu\n",
|
|
|
|
dirclust, ldep->de_StartCluster);
|
2011-11-22 13:30:36 +00:00
|
|
|
#endif
|
2002-08-16 05:13:42 +00:00
|
|
|
ldep->de_StartCluster = dirclust;
|
|
|
|
}
|
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
nvp->v_type = VDIR;
|
1998-02-18 09:28:47 +00:00
|
|
|
if (ldep->de_StartCluster != MSDOSFSROOT) {
|
|
|
|
error = pcbmap(ldep, 0xffff, 0, &size, 0);
|
1994-09-19 15:41:57 +00:00
|
|
|
if (error == E2BIG) {
|
1998-02-18 09:28:47 +00:00
|
|
|
ldep->de_FileSize = de_cn2off(pmp, size);
|
1994-09-19 15:41:57 +00:00
|
|
|
error = 0;
|
2011-11-22 13:30:36 +00:00
|
|
|
} else {
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
1994-09-19 15:41:57 +00:00
|
|
|
printf("deget(): pcbmap returned %d\n", error);
|
2011-11-22 13:30:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
nvp->v_type = VREG;
|
2004-09-07 09:17:05 +00:00
|
|
|
ldep->de_modrev = init_va_filerev();
|
1994-09-19 15:41:57 +00:00
|
|
|
*depp = ldep;
|
1998-02-18 09:28:47 +00:00
|
|
|
return (0);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-02-18 09:28:47 +00:00
|
|
|
deupdat(dep, waitfor)
|
1994-09-19 15:41:57 +00:00
|
|
|
struct denode *dep;
|
|
|
|
int waitfor;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct buf *bp;
|
|
|
|
struct direntry *dirp;
|
1998-02-18 09:28:47 +00:00
|
|
|
struct timespec ts;
|
1994-09-19 15:41:57 +00:00
|
|
|
|
1998-02-18 09:28:47 +00:00
|
|
|
if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
|
|
|
|
return (0);
|
1998-03-26 20:54:05 +00:00
|
|
|
getnanotime(&ts);
|
1998-02-18 09:28:47 +00:00
|
|
|
DETIMES(dep, &ts, &ts, &ts);
|
|
|
|
if ((dep->de_flag & DE_MODIFIED) == 0)
|
|
|
|
return (0);
|
|
|
|
dep->de_flag &= ~DE_MODIFIED;
|
|
|
|
if (dep->de_Attributes & ATTR_DIRECTORY)
|
|
|
|
return (0);
|
|
|
|
if (dep->de_refcnt <= 0)
|
|
|
|
return (0);
|
1994-09-27 20:42:59 +00:00
|
|
|
error = readde(dep, &bp, &dirp);
|
1995-05-30 08:16:23 +00:00
|
|
|
if (error)
|
1998-02-18 09:28:47 +00:00
|
|
|
return (error);
|
1994-09-19 15:41:57 +00:00
|
|
|
DE_EXTERNALIZE(dirp, dep);
|
|
|
|
if (waitfor)
|
1998-02-18 09:28:47 +00:00
|
|
|
return (bwrite(bp));
|
|
|
|
else {
|
1994-09-19 15:41:57 +00:00
|
|
|
bdwrite(bp);
|
1998-02-18 09:28:47 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Truncate the file described by dep to the length specified by length.
|
|
|
|
*/
|
|
|
|
int
|
2012-04-23 13:21:28 +00:00
|
|
|
detrunc(dep, length, flags, cred)
|
1994-09-19 15:41:57 +00:00
|
|
|
struct denode *dep;
|
|
|
|
u_long length;
|
|
|
|
int flags;
|
|
|
|
struct ucred *cred;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int allerror;
|
|
|
|
u_long eofentry;
|
|
|
|
u_long chaintofree;
|
|
|
|
daddr_t bn;
|
|
|
|
int boff;
|
|
|
|
int isadir = dep->de_Attributes & ATTR_DIRECTORY;
|
|
|
|
struct buf *bp;
|
|
|
|
struct msdosfsmount *pmp = dep->de_pmp;
|
|
|
|
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
1998-02-18 09:28:47 +00:00
|
|
|
printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
|
1994-09-19 15:41:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disallow attempts to truncate the root directory since it is of
|
|
|
|
* fixed size. That's just the way dos filesystems are. We use
|
|
|
|
* the VROOT bit in the vnode because checking for the directory
|
|
|
|
* bit and a startcluster of 0 in the denode is not adequate to
|
|
|
|
* recognize the root directory at this point in a file or
|
|
|
|
* directory's life.
|
|
|
|
*/
|
2002-08-04 10:29:36 +00:00
|
|
|
if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
|
2011-11-22 13:30:36 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
1998-02-18 09:28:47 +00:00
|
|
|
printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
|
1994-09-19 15:41:57 +00:00
|
|
|
dep->de_dirclust, dep->de_diroffset);
|
2011-11-22 13:30:36 +00:00
|
|
|
#endif
|
1998-02-18 09:28:47 +00:00
|
|
|
return (EINVAL);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
1996-01-19 04:00:31 +00:00
|
|
|
if (dep->de_FileSize < length) {
|
|
|
|
vnode_pager_setsize(DETOV(dep), length);
|
1994-09-19 15:41:57 +00:00
|
|
|
return deextend(dep, length, cred);
|
1996-01-19 04:00:31 +00:00
|
|
|
}
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the desired length is 0 then remember the starting cluster of
|
|
|
|
* the file and set the StartCluster field in the directory entry
|
|
|
|
* to 0. If the desired length is not zero, then get the number of
|
|
|
|
* the last cluster in the shortened file. Then get the number of
|
|
|
|
* the first cluster in the part of the file that is to be freed.
|
|
|
|
* Then set the next cluster pointer in the last cluster of the
|
|
|
|
* file to CLUST_EOFE.
|
|
|
|
*/
|
|
|
|
if (length == 0) {
|
|
|
|
chaintofree = dep->de_StartCluster;
|
|
|
|
dep->de_StartCluster = 0;
|
|
|
|
eofentry = ~0;
|
|
|
|
} else {
|
1998-02-18 09:28:47 +00:00
|
|
|
error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
|
|
|
|
&eofentry, 0);
|
1994-09-27 20:42:59 +00:00
|
|
|
if (error) {
|
1994-09-19 15:41:57 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
|
|
|
printf("detrunc(): pcbmap fails %d\n", error);
|
|
|
|
#endif
|
1998-02-18 09:28:47 +00:00
|
|
|
return (error);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-18 09:28:47 +00:00
|
|
|
fc_purge(dep, de_clcount(pmp, length));
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the new length is not a multiple of the cluster size then we
|
|
|
|
* must zero the tail end of the new last cluster in case it
|
|
|
|
* becomes part of the file again because of a seek.
|
|
|
|
*/
|
|
|
|
if ((boff = length & pmp->pm_crbomask) != 0) {
|
|
|
|
if (isadir) {
|
|
|
|
bn = cntobn(pmp, eofentry);
|
|
|
|
error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
|
|
|
|
NOCRED, &bp);
|
2003-06-27 15:46:38 +00:00
|
|
|
if (error) {
|
|
|
|
brelse(bp);
|
1994-09-19 15:41:57 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
2003-06-27 15:46:38 +00:00
|
|
|
printf("detrunc(): bread fails %d\n", error);
|
1994-09-19 15:41:57 +00:00
|
|
|
#endif
|
2003-06-27 15:46:38 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
|
|
|
|
if (flags & IO_SYNC)
|
|
|
|
bwrite(bp);
|
|
|
|
else
|
|
|
|
bdwrite(bp);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the updated directory entry. Even if the update fails
|
|
|
|
* we free the trailing clusters.
|
|
|
|
*/
|
|
|
|
dep->de_FileSize = length;
|
1998-02-18 09:28:47 +00:00
|
|
|
if (!isadir)
|
2007-08-07 03:59:49 +00:00
|
|
|
dep->de_flag |= DE_UPDATE | DE_MODIFIED;
|
2012-04-23 13:21:28 +00:00
|
|
|
allerror = vtruncbuf(DETOV(dep), cred, length, pmp->pm_bpcluster);
|
1998-10-29 09:29:17 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
|
|
|
if (allerror)
|
|
|
|
printf("detrunc(): vtruncbuf error %d\n", allerror);
|
|
|
|
#endif
|
2012-02-22 13:01:17 +00:00
|
|
|
error = deupdat(dep, !DOINGASYNC((DETOV(dep))));
|
2007-08-07 03:59:49 +00:00
|
|
|
if (error != 0 && allerror == 0)
|
1998-10-29 09:29:17 +00:00
|
|
|
allerror = error;
|
1994-09-19 15:41:57 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
1998-02-18 09:28:47 +00:00
|
|
|
printf("detrunc(): allerror %d, eofentry %lu\n",
|
1994-09-19 15:41:57 +00:00
|
|
|
allerror, eofentry);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we need to break the cluster chain for the file then do it
|
|
|
|
* now.
|
|
|
|
*/
|
|
|
|
if (eofentry != ~0) {
|
|
|
|
error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
|
|
|
|
&chaintofree, CLUST_EOFE);
|
|
|
|
if (error) {
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
|
|
|
printf("detrunc(): fatentry errors %d\n", error);
|
|
|
|
#endif
|
1998-02-18 09:28:47 +00:00
|
|
|
return (error);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
1998-02-18 09:28:47 +00:00
|
|
|
fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
|
1994-09-19 15:41:57 +00:00
|
|
|
eofentry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now free the clusters removed from the file because of the
|
|
|
|
* truncation.
|
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
|
1994-09-19 15:41:57 +00:00
|
|
|
freeclusterchain(pmp, chaintofree);
|
|
|
|
|
1998-02-18 09:28:47 +00:00
|
|
|
return (allerror);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extend the file described by dep to length specified by length.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
deextend(dep, length, cred)
|
|
|
|
struct denode *dep;
|
1998-02-18 09:28:47 +00:00
|
|
|
u_long length;
|
1994-09-19 15:41:57 +00:00
|
|
|
struct ucred *cred;
|
|
|
|
{
|
|
|
|
struct msdosfsmount *pmp = dep->de_pmp;
|
|
|
|
u_long count;
|
|
|
|
int error;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
|
|
|
* The root of a DOS filesystem cannot be extended.
|
|
|
|
*/
|
2002-08-04 10:29:36 +00:00
|
|
|
if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
|
1998-02-18 09:28:47 +00:00
|
|
|
return (EINVAL);
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
1998-02-18 09:28:47 +00:00
|
|
|
* Directories cannot be extended.
|
1994-09-19 15:41:57 +00:00
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
if (dep->de_Attributes & ATTR_DIRECTORY)
|
|
|
|
return (EISDIR);
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
if (length <= dep->de_FileSize)
|
|
|
|
panic("deextend: file too large");
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
|
|
|
* Compute the number of clusters to allocate.
|
|
|
|
*/
|
|
|
|
count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > pmp->pm_freeclustercount)
|
1998-02-18 09:28:47 +00:00
|
|
|
return (ENOSPC);
|
1994-09-27 20:42:59 +00:00
|
|
|
error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
|
|
|
|
if (error) {
|
1994-09-19 15:41:57 +00:00
|
|
|
/* truncate the added clusters away again */
|
2012-04-23 13:21:28 +00:00
|
|
|
(void) detrunc(dep, dep->de_FileSize, 0, cred);
|
1998-02-18 09:28:47 +00:00
|
|
|
return (error);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dep->de_FileSize = length;
|
2007-08-07 03:59:49 +00:00
|
|
|
dep->de_flag |= DE_UPDATE | DE_MODIFIED;
|
2012-02-22 13:01:17 +00:00
|
|
|
return (deupdat(dep, !DOINGASYNC(DETOV(dep))));
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move a denode to its correct hash queue after the file it represents has
|
|
|
|
* been moved to a new directory.
|
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
void
|
|
|
|
reinsert(dep)
|
1994-09-19 15:41:57 +00:00
|
|
|
struct denode *dep;
|
|
|
|
{
|
2005-03-14 12:24:35 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
|
|
|
* Fix up the denode cache. If the denode is for a directory,
|
|
|
|
* there is nothing to do since the hash is based on the starting
|
|
|
|
* cluster of the directory file and that hasn't changed. If for a
|
|
|
|
* file the hash is based on the location of the directory entry,
|
|
|
|
* so we must remove it from the cache and re-enter it with the
|
|
|
|
* hash based on the new location of the directory entry.
|
|
|
|
*/
|
2005-04-07 07:55:37 +00:00
|
|
|
#if 0
|
1998-02-18 09:28:47 +00:00
|
|
|
if (dep->de_Attributes & ATTR_DIRECTORY)
|
|
|
|
return;
|
2005-04-07 07:55:37 +00:00
|
|
|
#endif
|
2005-03-14 12:24:35 +00:00
|
|
|
vp = DETOV(dep);
|
2005-09-07 10:32:58 +00:00
|
|
|
dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust +
|
2007-08-07 03:59:49 +00:00
|
|
|
dep->de_diroffset;
|
2005-04-07 07:55:37 +00:00
|
|
|
vfs_hash_rehash(vp, dep->de_inode);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
msdosfs_reclaim(ap)
|
|
|
|
struct vop_reclaim_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
struct denode *dep = VTODE(vp);
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
1994-10-10 07:57:33 +00:00
|
|
|
printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
|
1994-09-19 15:41:57 +00:00
|
|
|
dep, dep->de_Name, dep->de_refcnt);
|
|
|
|
#endif
|
|
|
|
|
2006-01-17 17:29:03 +00:00
|
|
|
/*
|
|
|
|
* Destroy the vm object and flush associated pages.
|
|
|
|
*/
|
|
|
|
vnode_destroy_vobject(vp);
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
1998-02-18 09:28:47 +00:00
|
|
|
* Remove the denode from its hash chain.
|
1994-09-19 15:41:57 +00:00
|
|
|
*/
|
2005-03-14 12:24:35 +00:00
|
|
|
vfs_hash_remove(vp);
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
1998-02-18 09:28:47 +00:00
|
|
|
* Purge old data structures associated with the denode.
|
1994-09-19 15:41:57 +00:00
|
|
|
*/
|
1998-02-18 09:28:47 +00:00
|
|
|
#if 0 /* XXX */
|
1994-09-19 15:41:57 +00:00
|
|
|
dep->de_flag = 0;
|
1998-02-18 09:28:47 +00:00
|
|
|
#endif
|
2008-10-23 15:53:51 +00:00
|
|
|
free(dep, M_MSDOSFSNODE);
|
1994-09-19 15:41:57 +00:00
|
|
|
vp->v_data = NULL;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1998-02-18 09:28:47 +00:00
|
|
|
return (0);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
msdosfs_inactive(ap)
|
|
|
|
struct vop_inactive_args /* {
|
|
|
|
struct vnode *a_vp;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *a_td;
|
1994-09-19 15:41:57 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
struct denode *dep = VTODE(vp);
|
|
|
|
int error = 0;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-09-19 15:41:57 +00:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
1994-10-10 07:57:33 +00:00
|
|
|
printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
|
1994-09-19 15:41:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1998-02-18 09:28:47 +00:00
|
|
|
* Ignore denodes related to stale file handles.
|
1994-09-19 15:41:57 +00:00
|
|
|
*/
|
2010-02-28 17:10:41 +00:00
|
|
|
if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
|
1997-02-26 14:23:16 +00:00
|
|
|
goto out;
|
1994-09-19 15:41:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the file has been deleted and it is on a read/write
|
|
|
|
* filesystem, then truncate the file, and mark the directory slot
|
|
|
|
* as empty. (This may not be necessary for the dos filesystem.)
|
|
|
|
*/
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
1994-10-10 07:57:33 +00:00
|
|
|
printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
|
1994-09-19 15:41:57 +00:00
|
|
|
dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
|
|
|
|
#endif
|
|
|
|
if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
|
2012-04-23 13:21:28 +00:00
|
|
|
error = detrunc(dep, (u_long) 0, 0, NOCRED);
|
1994-09-19 15:41:57 +00:00
|
|
|
dep->de_flag |= DE_UPDATE;
|
|
|
|
dep->de_Name[0] = SLOT_DELETED;
|
|
|
|
}
|
1998-02-18 09:28:47 +00:00
|
|
|
deupdat(dep, 0);
|
|
|
|
|
1997-02-26 14:23:16 +00:00
|
|
|
out:
|
1994-09-19 15:41:57 +00:00
|
|
|
/*
|
1998-02-18 09:28:47 +00:00
|
|
|
* If we are done with the denode, reclaim it
|
|
|
|
* so that it can be reused immediately.
|
1994-09-19 15:41:57 +00:00
|
|
|
*/
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
2002-09-25 02:32:42 +00:00
|
|
|
printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
|
|
|
|
vrefcnt(vp), dep->de_Name[0]);
|
1994-09-19 15:41:57 +00:00
|
|
|
#endif
|
2010-02-28 17:10:41 +00:00
|
|
|
if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
|
2012-04-23 14:10:34 +00:00
|
|
|
vrecycle(vp);
|
1998-02-18 09:28:47 +00:00
|
|
|
return (error);
|
1994-09-19 15:41:57 +00:00
|
|
|
}
|