Home | History | Annotate | Line # | Download | only in msdos
msdosfs_denode.c revision 1.4
      1 /*	$NetBSD: msdosfs_denode.c,v 1.4 2013/01/28 00:16:24 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 
     50 #if HAVE_NBTOOL_CONFIG_H
     51 #include "nbtool_config.h"
     52 #endif
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.4 2013/01/28 00:16:24 christos Exp $");
     56 
     57 #include <sys/param.h>
     58 
     59 #include <ffs/buf.h>
     60 
     61 #include <fs/msdosfs/bpb.h>
     62 #include <fs/msdosfs/msdosfsmount.h>
     63 #include <fs/msdosfs/direntry.h>
     64 #include <fs/msdosfs/denode.h>
     65 #include <fs/msdosfs/fat.h>
     66 
     67 /*
     68  * If deget() succeeds it returns with the gotten denode locked().
     69  *
     70  * pmp	     - address of msdosfsmount structure of the filesystem containing
     71  *	       the denode of interest.  The pm_dev field and the address of
     72  *	       the msdosfsmount structure are used.
     73  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
     74  *	       diroffset is relative to the beginning of the root directory,
     75  *	       otherwise it is cluster relative.
     76  * diroffset - offset past begin of cluster of denode we want
     77  * depp	     - returns the address of the gotten denode.
     78  */
     79 int
     80 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct denode **depp)
     81 	/* pmp:	 so we know the maj/min number */
     82 	/* dirclust:		 cluster this dir entry came from */
     83 	/* diroffset:		 index of entry within the cluster */
     84 	/* depp:		 returns the addr of the gotten denode */
     85 {
     86 	int error;
     87 	struct direntry *direntptr;
     88 	struct denode *ldep;
     89 	struct buf *bp;
     90 
     91 #ifdef MSDOSFS_DEBUG
     92 	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
     93 	    pmp, dirclust, diroffset, depp);
     94 #endif
     95 
     96 	/*
     97 	 * On FAT32 filesystems, root is a (more or less) normal
     98 	 * directory
     99 	 */
    100 	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
    101 		dirclust = pmp->pm_rootdirblk;
    102 
    103 	ldep = calloc(1, sizeof(*ldep));
    104 	if (ldep == NULL)
    105 		err(1, "calloc");
    106 	ldep->de_vnode = NULL;
    107 	ldep->de_flag = 0;
    108 	ldep->de_devvp = 0;
    109 	ldep->de_lockf = 0;
    110 	ldep->de_dev = pmp->pm_dev;
    111 	ldep->de_dirclust = dirclust;
    112 	ldep->de_diroffset = diroffset;
    113 	ldep->de_pmp = pmp;
    114 	ldep->de_devvp = pmp->pm_devvp;
    115 	ldep->de_refcnt = 1;
    116 	fc_purge(ldep, 0);
    117 	/*
    118 	 * Copy the directory entry into the denode area of the vnode.
    119 	 */
    120 	if ((dirclust == MSDOSFSROOT
    121 	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
    122 	    && diroffset == MSDOSFSROOT_OFS) {
    123 		/*
    124 		 * Directory entry for the root directory. There isn't one,
    125 		 * so we manufacture one. We should probably rummage
    126 		 * through the root directory and find a label entry (if it
    127 		 * exists), and then use the time and date from that entry
    128 		 * as the time and date for the root denode.
    129 		 */
    130 		ldep->de_vnode = (struct vnode *)-1;
    131 
    132 		ldep->de_Attributes = ATTR_DIRECTORY;
    133 		if (FAT32(pmp))
    134 			ldep->de_StartCluster = pmp->pm_rootdirblk;
    135 			/* de_FileSize will be filled in further down */
    136 		else {
    137 			ldep->de_StartCluster = MSDOSFSROOT;
    138 			ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
    139 		}
    140 		/*
    141 		 * fill in time and date so that dos2unixtime() doesn't
    142 		 * spit up when called from msdosfs_getattr() with root
    143 		 * denode
    144 		 */
    145 		ldep->de_CHun = 0;
    146 		ldep->de_CTime = 0x0000;	/* 00:00:00	 */
    147 		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
    148 		    | (1 << DD_DAY_SHIFT);
    149 		/* Jan 1, 1980	 */
    150 		ldep->de_ADate = ldep->de_CDate;
    151 		ldep->de_MTime = ldep->de_CTime;
    152 		ldep->de_MDate = ldep->de_CDate;
    153 		/* leave the other fields as garbage */
    154 	} else {
    155 		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
    156 		if (error) {
    157 			ldep->de_devvp = NULL;
    158 			ldep->de_Name[0] = SLOT_DELETED;
    159 			return (error);
    160 		}
    161 		DE_INTERNALIZE(ldep, direntptr);
    162 		brelse(bp, 0);
    163 	}
    164 
    165 	/*
    166 	 * Fill in a few fields of the vnode and finish filling in the
    167 	 * denode.  Then return the address of the found denode.
    168 	 */
    169 	if (ldep->de_Attributes & ATTR_DIRECTORY) {
    170 		/*
    171 		 * Since DOS directory entries that describe directories
    172 		 * have 0 in the filesize field, we take this opportunity
    173 		 * to find out the length of the directory and plug it into
    174 		 * the denode structure.
    175 		 */
    176 		u_long size;
    177 
    178 		if (ldep->de_StartCluster != MSDOSFSROOT) {
    179 			error = pcbmap(ldep, CLUST_END, 0, &size, 0);
    180 			if (error == E2BIG) {
    181 				ldep->de_FileSize = de_cn2off(pmp, size);
    182 				error = 0;
    183 			} else
    184 				printf("deget(): pcbmap returned %d\n", error);
    185 		}
    186 	}
    187 	*depp = ldep;
    188 	return (0);
    189 }
    190 
    191 /*
    192  * Truncate the file described by dep to the length specified by length.
    193  */
    194 int
    195 detrunc(struct denode *dep, u_long length, int flags, struct kauth_cred *cred)
    196 {
    197 	int error;
    198 	int allerror = 0;
    199 	u_long eofentry;
    200 	u_long chaintofree = 0;
    201 	daddr_t bn, lastblock;
    202 	int boff;
    203 	int isadir = dep->de_Attributes & ATTR_DIRECTORY;
    204 	struct buf *bp;
    205 	struct msdosfsmount *pmp = dep->de_pmp;
    206 
    207 #ifdef MSDOSFS_DEBUG
    208 	printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
    209 #endif
    210 
    211 	/*
    212 	 * Disallow attempts to truncate the root directory since it is of
    213 	 * fixed size.  That's just the way dos filesystems are.  We use
    214 	 * the VROOT bit in the vnode because checking for the directory
    215 	 * bit and a startcluster of 0 in the denode is not adequate to
    216 	 * recognize the root directory at this point in a file or
    217 	 * directory's life.
    218 	 */
    219 	if (dep->de_vnode != NULL && !FAT32(pmp)) {
    220 		printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
    221 		    dep->de_dirclust, dep->de_diroffset);
    222 		return (EINVAL);
    223 	}
    224 
    225 	if (dep->de_FileSize < length)
    226 		return (deextend(dep, length, cred));
    227 	lastblock = de_clcount(pmp, length) - 1;
    228 
    229 	/*
    230 	 * If the desired length is 0 then remember the starting cluster of
    231 	 * the file and set the StartCluster field in the directory entry
    232 	 * to 0.  If the desired length is not zero, then get the number of
    233 	 * the last cluster in the shortened file.  Then get the number of
    234 	 * the first cluster in the part of the file that is to be freed.
    235 	 * Then set the next cluster pointer in the last cluster of the
    236 	 * file to CLUST_EOFE.
    237 	 */
    238 	if (length == 0) {
    239 		chaintofree = dep->de_StartCluster;
    240 		dep->de_StartCluster = 0;
    241 		eofentry = ~0;
    242 	} else {
    243 		error = pcbmap(dep, lastblock, 0, &eofentry, 0);
    244 		if (error) {
    245 #ifdef MSDOSFS_DEBUG
    246 			printf("detrunc(): pcbmap fails %d\n", error);
    247 #endif
    248 			return (error);
    249 		}
    250 	}
    251 
    252 	/*
    253 	 * If the new length is not a multiple of the cluster size then we
    254 	 * must zero the tail end of the new last cluster in case it
    255 	 * becomes part of the file again because of a seek.
    256 	 */
    257 	if ((boff = length & pmp->pm_crbomask) != 0) {
    258 		if (isadir) {
    259 			bn = cntobn(pmp, eofentry);
    260 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
    261 			    pmp->pm_bpcluster, NOCRED, B_MODIFY, &bp);
    262 			if (error) {
    263 #ifdef MSDOSFS_DEBUG
    264 				printf("detrunc(): bread fails %d\n", error);
    265 #endif
    266 				return (error);
    267 			}
    268 			memset((char *)bp->b_data + boff, 0,
    269 			    pmp->pm_bpcluster - boff);
    270 			if (flags & IO_SYNC)
    271 				bwrite(bp);
    272 			else
    273 				bdwrite(bp);
    274 		}
    275 	}
    276 
    277 	/*
    278 	 * Write out the updated directory entry.  Even if the update fails
    279 	 * we free the trailing clusters.
    280 	 */
    281 	dep->de_FileSize = length;
    282 	if (!isadir)
    283 		dep->de_flag |= DE_UPDATE|DE_MODIFIED;
    284 #ifdef MSDOSFS_DEBUG
    285 	printf("detrunc(): allerror %d, eofentry %lu\n",
    286 	       allerror, eofentry);
    287 #endif
    288 
    289 	/*
    290 	 * If we need to break the cluster chain for the file then do it
    291 	 * now.
    292 	 */
    293 	if (eofentry != (u_long)~0) {
    294 		error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
    295 				 &chaintofree, CLUST_EOFE);
    296 		if (error) {
    297 #ifdef MSDOSFS_DEBUG
    298 			printf("detrunc(): fatentry errors %d\n", error);
    299 #endif
    300 			return (error);
    301 		}
    302 	}
    303 
    304 	/*
    305 	 * Now free the clusters removed from the file because of the
    306 	 * truncation.
    307 	 */
    308 	if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
    309 		freeclusterchain(pmp, chaintofree);
    310 
    311 	return (allerror);
    312 }
    313 
    314 /*
    315  * Extend the file described by dep to length specified by length.
    316  */
    317 int
    318 deextend(struct denode *dep, u_long length, struct kauth_cred *cred)
    319 {
    320 	struct msdosfsmount *pmp = dep->de_pmp;
    321 	u_long count, osize;
    322 	int error;
    323 
    324 	/*
    325 	 * The root of a DOS filesystem cannot be extended.
    326 	 */
    327 	if (dep->de_vnode != NULL && !FAT32(pmp))
    328 		return EINVAL;
    329 
    330 	/*
    331 	 * Directories cannot be extended.
    332 	 */
    333 	if (dep->de_Attributes & ATTR_DIRECTORY)
    334 		return EISDIR;
    335 
    336 	if (length <= dep->de_FileSize)
    337 		return E2BIG;
    338 
    339 	/*
    340 	 * Compute the number of clusters to allocate.
    341 	 */
    342 	count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
    343 	if (count > 0) {
    344 		if (count > pmp->pm_freeclustercount)
    345 			return (ENOSPC);
    346 		error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
    347 		if (error) {
    348 			/* truncate the added clusters away again */
    349 			(void) detrunc(dep, dep->de_FileSize, 0, cred);
    350 			return (error);
    351 		}
    352 	}
    353 
    354 	/*
    355 	 * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
    356 	 * memset(); we set the write size so ubc won't read in file data that
    357 	 * is zero'd later.
    358 	 */
    359 	osize = dep->de_FileSize;
    360 	dep->de_FileSize = length;
    361 	dep->de_flag |= DE_UPDATE|DE_MODIFIED;
    362 	return 0;
    363 }
    364