Home | History | Annotate | Line # | Download | only in msdosfs
denode.h revision 1.13
      1 /*	$NetBSD: denode.h,v 1.13 2006/05/14 21:31:52 elad 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 #ifndef _MSDOSFS_DENODE_H_
     50 #define _MSDOSFS_DENODE_H_
     51 
     52 #include <miscfs/genfs/genfs_node.h>
     53 
     54 /*
     55  * This is the pc filesystem specific portion of the vnode structure.
     56  *
     57  * To describe a file uniquely the de_dirclust, de_diroffset, and
     58  * de_StartCluster fields are used.
     59  *
     60  * de_dirclust contains the cluster number of the directory cluster
     61  *	containing the entry for a file or directory.
     62  * de_diroffset is the index into the cluster for the entry describing
     63  *	a file or directory.
     64  * de_StartCluster is the number of the first cluster of the file or directory.
     65  *
     66  * Now to describe the quirks of the pc filesystem.
     67  * - Clusters 0 and 1 are reserved.
     68  * - The first allocatable cluster is 2.
     69  * - The root directory is of fixed size and all blocks that make it up
     70  *   are contiguous.
     71  * - Cluster 0 refers to the root directory when it is found in the
     72  *   startcluster field of a directory entry that points to another directory.
     73  * - Cluster 0 implies a 0 length file when found in the start cluster field
     74  *   of a directory entry that points to a file.
     75  * - You can't use the cluster number 0 to derive the address of the root
     76  *   directory.
     77  * - Multiple directory entries can point to a directory. The entry in the
     78  *   parent directory points to a child directory.  Any directories in the
     79  *   child directory contain a ".." entry that points back to the parent.
     80  *   The child directory itself contains a "." entry that points to itself.
     81  * - The root directory does not contain a "." or ".." entry.
     82  * - Directory entries for directories are never changed once they are created
     83  *   (except when removed).  The size stays 0, and the last modification time
     84  *   is never changed.  This is because so many directory entries can point to
     85  *   the physical clusters that make up a directory.  It would lead to an
     86  *   update nightmare.
     87  * - The length field in a directory entry pointing to a directory contains 0
     88  *   (always).  The only way to find the end of a directory is to follow the
     89  *   cluster chain until the "last cluster" marker is found.
     90  *
     91  * My extensions to make this house of cards work.  These apply only to the in
     92  * memory copy of the directory entry.
     93  * - A reference count for each denode will be kept since dos doesn't keep such
     94  *   things.
     95  */
     96 
     97 /*
     98  * Internal pseudo-offset for (nonexistent) directory entry for the root
     99  * dir in the root dir
    100  */
    101 #define	MSDOSFSROOT_OFS	0x1fffffff
    102 
    103 /*
    104  * The fat cache structure. fc_fsrcn is the filesystem relative cluster
    105  * number that corresponds to the file relative cluster number in this
    106  * structure (fc_frcn).
    107  */
    108 struct fatcache {
    109 	u_long fc_frcn;		/* file relative cluster number */
    110 	u_long fc_fsrcn;	/* filesystem relative cluster number */
    111 };
    112 
    113 /*
    114  * The fat entry cache as it stands helps make extending files a "quick"
    115  * operation by avoiding having to scan the fat to discover the last
    116  * cluster of the file. The cache also helps sequential reads by
    117  * remembering the last cluster read from the file.  This also prevents us
    118  * from having to rescan the fat to find the next cluster to read.  This
    119  * cache is probably pretty worthless if a file is opened by multiple
    120  * processes.
    121  */
    122 #define	FC_SIZE		2	/* number of entries in the cache */
    123 #define	FC_LASTMAP	0	/* entry the last call to pcbmap() resolved
    124 				 * to */
    125 #define	FC_LASTFC	1	/* entry for the last cluster in the file */
    126 
    127 #define	FCE_EMPTY	0xffffffff	/* doesn't represent an actual cluster # */
    128 
    129 /*
    130  * Set a slot in the fat cache.
    131  */
    132 #define	fc_setcache(dep, slot, frcn, fsrcn) \
    133 	(dep)->de_fc[slot].fc_frcn = frcn; \
    134 	(dep)->de_fc[slot].fc_fsrcn = fsrcn;
    135 
    136 /*
    137  * This is the in memory variant of a dos directory entry.  It is usually
    138  * contained within a vnode.
    139  */
    140 struct denode {
    141 	struct genfs_node de_gnode;
    142 	LIST_ENTRY(denode) de_hash;
    143 	struct vnode *de_vnode;	/* addr of vnode we are part of */
    144 	struct vnode *de_devvp;	/* vnode of blk dev we live on */
    145 	u_long de_flag;		/* flag bits */
    146 	dev_t de_dev;		/* device where direntry lives */
    147 	u_long de_dirclust;	/* cluster of the directory file containing this entry */
    148 	u_long de_diroffset;	/* offset of this entry in the directory cluster */
    149 	u_long de_fndoffset;	/* offset of found dir entry */
    150 	int de_fndcnt;		/* number of slots before de_fndoffset */
    151 	long de_refcnt;		/* reference count */
    152 	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
    153 	struct lockf *de_lockf;	/* byte level lock list */
    154 	u_char de_Name[12];	/* name, from DOS directory entry */
    155 	u_char de_Attributes;	/* attributes, from directory entry */
    156 	u_char de_CHun;		/* Hundredth of second of CTime*/
    157 	u_short de_CTime;	/* creation time */
    158 	u_short de_CDate;	/* creation date */
    159 	u_short de_ADate;	/* access date */
    160 	u_short de_MTime;	/* modification time */
    161 	u_short de_MDate;	/* modification date */
    162 	u_long de_StartCluster; /* starting cluster of file */
    163 	u_long de_FileSize;	/* size of file in bytes */
    164 	struct fatcache de_fc[FC_SIZE];	/* fat cache */
    165 };
    166 
    167 /*
    168  * Values for the de_flag field of the denode.
    169  */
    170 #define	DE_UPDATE	0x0001	/* Modification time update request. */
    171 #define	DE_CREATE	0x0002	/* Creation time update */
    172 #define	DE_ACCESS	0x0004	/* Access time update */
    173 #define	DE_MODIFIED	0x0008	/* Denode has been modified. */
    174 #define	DE_RENAME	0x0010	/* Denode is in the process of being renamed */
    175 
    176 /*
    177  * Maximum filename length in Win95
    178  * Note: Must be < sizeof(dirent.d_name)
    179  */
    180 #define	WIN_MAXLEN	255
    181 
    182 /* Maximum size of a file on a FAT filesystem */
    183 #define MSDOSFS_FILESIZE_MAX	0xFFFFFFFFLL
    184 
    185 /*
    186  * Transfer directory entries between internal and external form.
    187  * dep is a struct denode * (internal form),
    188  * dp is a struct direntry * (external form).
    189  */
    190 #define DE_INTERNALIZE32(dep, dp)			\
    191 	 ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
    192 #define DE_INTERNALIZE(dep, dp)				\
    193 	(memcpy((dep)->de_Name, (dp)->deName, 11),	\
    194 	 (dep)->de_Attributes = (dp)->deAttributes,	\
    195 	 (dep)->de_CHun = (dp)->deCHundredth,		\
    196 	 (dep)->de_CTime = getushort((dp)->deCTime),	\
    197 	 (dep)->de_CDate = getushort((dp)->deCDate),	\
    198 	 (dep)->de_ADate = getushort((dp)->deADate),	\
    199 	 (dep)->de_MTime = getushort((dp)->deMTime),	\
    200 	 (dep)->de_MDate = getushort((dp)->deMDate),	\
    201 	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
    202 	 (dep)->de_FileSize = getulong((dp)->deFileSize), \
    203 	 (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
    204 
    205 #define DE_EXTERNALIZE32(dp, dep)			\
    206 	 putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16)
    207 #define DE_EXTERNALIZE16(dp, dep)			\
    208 	 putushort((dp)->deHighClust, 0)
    209 #define DE_EXTERNALIZE(dp, dep)				\
    210 	(memcpy((dp)->deName, (dep)->de_Name, 11),	\
    211 	 (dp)->deAttributes = (dep)->de_Attributes,	\
    212 	 (dp)->deCHundredth = (dep)->de_CHun,		\
    213 	 putushort((dp)->deCTime, (dep)->de_CTime),	\
    214 	 putushort((dp)->deCDate, (dep)->de_CDate),	\
    215 	 putushort((dp)->deADate, (dep)->de_ADate),	\
    216 	 putushort((dp)->deMTime, (dep)->de_MTime),	\
    217 	 putushort((dp)->deMDate, (dep)->de_MDate),	\
    218 	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
    219 	 putulong((dp)->deFileSize,			\
    220 	     ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \
    221 	 (FAT32((dep)->de_pmp) ? DE_EXTERNALIZE32((dp), (dep)) : DE_EXTERNALIZE16((dp), (dep))))
    222 
    223 #define	de_forw		de_chain[0]
    224 #define	de_back		de_chain[1]
    225 
    226 #ifdef _KERNEL
    227 
    228 #define	VTODE(vp)	((struct denode *)(vp)->v_data)
    229 #define	DETOV(de)	((de)->de_vnode)
    230 
    231 #define	DETIMES(dep, acc, mod, cre, gmtoff) \
    232 	while ((dep)->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS)) \
    233 		msdosfs_detimes(dep, acc, mod, cre, gmtoff)
    234 
    235 /*
    236  * This overlays the fid structure (see mount.h)
    237  */
    238 struct defid {
    239 	u_int16_t defid_len;	/* length of structure */
    240 	u_int16_t defid_pad;	/* force 4-byte alignment */
    241 
    242 	u_int32_t defid_dirclust; /* cluster this dir entry came from */
    243 	u_int32_t defid_dirofs;	/* offset of entry within the cluster */
    244 #if 0
    245 	u_int32_t defid_gen;	/* generation number */
    246 #endif
    247 };
    248 
    249 /*
    250  * Prototypes for MSDOSFS vnode operations
    251  */
    252 int	msdosfs_lookup		(void *);
    253 int	msdosfs_create		(void *);
    254 int	msdosfs_mknod		(void *);
    255 int	msdosfs_open		(void *);
    256 int	msdosfs_close		(void *);
    257 int	msdosfs_access		(void *);
    258 int	msdosfs_getattr		(void *);
    259 int	msdosfs_setattr		(void *);
    260 int	msdosfs_read		(void *);
    261 int	msdosfs_write		(void *);
    262 #define	msdosfs_lease_check	genfs_lease_check
    263 #define	msdosfs_ioctl		genfs_enoioctl
    264 #define	msdosfs_poll		genfs_poll
    265 #define	msdosfs_revoke		genfs_revoke
    266 #define	msdosfs_mmap		genfs_mmap
    267 int	msdosfs_fsync		(void *);
    268 #define	msdosfs_seek		genfs_seek
    269 int	msdosfs_remove		(void *);
    270 int	msdosfs_link		(void *);
    271 int	msdosfs_rename		(void *);
    272 int	msdosfs_mkdir		(void *);
    273 int	msdosfs_rmdir		(void *);
    274 int	msdosfs_symlink		(void *);
    275 int	msdosfs_readdir		(void *);
    276 int	msdosfs_readlink	(void *);
    277 #define	msdosfs_abortop		genfs_abortop
    278 int	msdosfs_inactive	(void *);
    279 int	msdosfs_reclaim		(void *);
    280 int	msdosfs_bmap		(void *);
    281 int	msdosfs_strategy	(void *);
    282 int	msdosfs_print		(void *);
    283 int	msdosfs_advlock		(void *);
    284 int	msdosfs_pathconf	(void *);
    285 
    286 /*
    287  * Internal service routine prototypes.
    288  */
    289 int msdosfs_update(struct vnode *, const struct timespec *,
    290 	    const struct timespec *, int);
    291 int createde(struct denode *, struct denode *,
    292 		struct denode **, struct componentname *);
    293 int deextend(struct denode *, u_long, kauth_cred_t);
    294 int deget(struct msdosfsmount *, u_long, u_long, struct denode **);
    295 int detrunc(struct denode *, u_long, int, kauth_cred_t, struct lwp *);
    296 int deupdat(struct denode *, int);
    297 int doscheckpath(struct denode *, struct denode *);
    298 int dosdirempty(struct denode *);
    299 int readde(struct denode *, struct buf **, struct direntry **);
    300 int readep(struct msdosfsmount *, u_long, u_long,
    301 		struct buf **, struct direntry **);
    302 void reinsert(struct denode *);
    303 int removede(struct denode *, struct denode *);
    304 int uniqdosname(struct denode *, struct componentname *, u_char *);
    305 int findwin95(struct denode *);
    306 int msdosfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
    307 void msdosfs_gop_markupdate(struct vnode *, int);
    308 void msdosfs_detimes(struct denode *, const struct timespec *,
    309     const struct timespec *, const struct timespec *, int);
    310 #endif	/* _KERNEL */
    311 #endif /* _MSDOSFS_DENODE_H_ */
    312