FreeBSD/sys/fs/msdosfs/msdosfs_denode.c
/*
 * If deget() succeeds it returns with the gotten denode locked().
 *
 * pmp	     - address of msdosfsmount structure of the filesystem containing
 *	       the denode of interest.  The address of
 *	       the msdosfsmount structure are used.
 * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
 *	       diroffset is relative to the beginning of the root directory,
 *	       otherwise it is cluster relative.
 * diroffset - offset past begin of cluster of denode we want
 * depp	     - returns the address of the gotten denode.
 */
If deget() succeeds it returns with the gotten denode locked().
pmp       - address of msdosfsmount structure of the filesystem containing
the denode of interest.  The address of
the msdosfsmount structure are used.
dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
diroffset is relative to the beginning of the root directory,
otherwise it is cluster relative.
diroffset - offset past begin of cluster of denode we want
depp      - returns the address of the gotten denode.
If deget () succeeds it returns with the gotten denode locked () . pmp - address of msdosfsmount structure of the filesystem containing the denode of interest . The address of the msdosfsmount structure are used . dirclust - which cluster bp contains if dirclust is 0 ( root directory ) diroffset is relative to the beginning of the root directory otherwise it is cluster relative . diroffset - offset past begin of cluster of denode we want depp - returns the address of the gotten denode .
int
deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
    struct denode **depp)
{
	int error;
	uint64_t inode;
	struct mount *mntp = pmp->pm_mountp;
	struct direntry *direntptr;
	struct denode *ldep;
	struct vnode *nvp, *xvp;
	struct buf *bp;
#ifdef MSDOSFS_DEBUG
	printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
	    pmp, dirclust, diroffset, depp);
#endif
	if (FAT32(pmp) && dirclust == MSDOSFSROOT)
		dirclust = pmp->pm_rootdirblk;
	inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
	error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp,
	    de_vncmpf, &inode);
	if (error)
		return (error);
	if (nvp != NULL) {
		*depp = VTODE(nvp);
		KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust"));
		KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset"));
		return (0);
	}
	ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
	error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
	if (error) {
		*depp = NULL;
		free(ldep, M_MSDOSFSNODE);
		return error;
	}
	nvp->v_data = ldep;
	ldep->de_vnode = nvp;
	ldep->de_flag = 0;
	ldep->de_dirclust = dirclust;
	ldep->de_diroffset = diroffset;
	ldep->de_inode = inode;
	lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL);
	fc_purge(ldep, 0);	
	error = insmntque(nvp, mntp);
	if (error != 0) {
		free(ldep, M_MSDOSFSNODE);
		*depp = NULL;
		return (error);
	}
	error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp,
	    de_vncmpf, &inode);
	if (error) {
		*depp = NULL;
		return (error);
	}
	if (xvp != NULL) {
		*depp = xvp->v_data;
		return (0);
	}
	ldep->de_pmp = pmp;
	ldep->de_refcnt = 1;
	if ((dirclust == MSDOSFSROOT
	     || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
	    && diroffset == MSDOSFSROOT_OFS) {
		nvp->v_vflag |= VV_ROOT; 
		ldep->de_Attributes = ATTR_DIRECTORY;
		ldep->de_LowerCase = 0;
		if (FAT32(pmp))
			ldep->de_StartCluster = pmp->pm_rootdirblk;
		else {
			ldep->de_StartCluster = MSDOSFSROOT;
			ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
		}
		ldep->de_CHun = 0;
		ldep->de_CTime = 0x0000;	
		ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
		    | (1 << DD_DAY_SHIFT);
		ldep->de_ADate = ldep->de_CDate;
		ldep->de_MTime = ldep->de_CTime;
		ldep->de_MDate = ldep->de_CDate;
	} else {
		error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
		if (error) {
			ldep->de_Name[0] = SLOT_DELETED;
			vput(nvp);
			*depp = NULL;
			return (error);
		}
		(void)DE_INTERNALIZE(ldep, direntptr);
		brelse(bp);
	}
	if (ldep->de_Attributes & ATTR_DIRECTORY) {
		u_long size;
		if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
#ifdef MSDOSFS_DEBUG
			printf("deget(): \".\" entry at clust %lu != %lu\n",
			    dirclust, ldep->de_StartCluster);
#endif
			ldep->de_StartCluster = dirclust;
		}
		nvp->v_type = VDIR;
		if (ldep->de_StartCluster != MSDOSFSROOT) {
			error = pcbmap(ldep, 0xffff, 0, &size, 0);
			if (error == E2BIG) {
				ldep->de_FileSize = de_cn2off(pmp, size);
				error = 0;
			} else {
#ifdef MSDOSFS_DEBUG
				printf("deget(): pcbmap returned %d\n", error);
#endif
			}
		}
	} else
		nvp->v_type = VREG;
	ldep->de_modrev = init_va_filerev();
	*depp = ldep;
	return (0);
}
int deget ( struct msdosfsmount * pmp , u_long dirclust , u_long diroffset , struct denode * * depp ) { int error ; uint64_t inode ; struct mount * mntp = pmp -> pm_mountp ; struct direntry * direntptr ; struct denode * ldep ; struct vnode * nvp , * xvp ; struct buf * bp ; #ifdef MSDOSFS_DEBUG printf ( " \n " , pmp , dirclust , diroffset , depp ) ; #endif if ( FAT32 ( pmp ) && dirclust == MSDOSFSROOT ) dirclust = pmp -> pm_rootdirblk ; inode = ( uint64_t ) pmp -> pm_bpcluster * dirclust + diroffset ; error = vfs_hash_get ( mntp , inode , LK_EXCLUSIVE , curthread , & nvp , de_vncmpf , & inode ) ; if ( error ) return ( error ) ; if ( nvp != NULL ) { * depp = VTODE ( nvp ) ; KASSERT ( ( * depp ) -> de_dirclust == dirclust , ( " " ) ) ; KASSERT ( ( * depp ) -> de_diroffset == diroffset , ( " " ) ) ; return ( 0 ) ; } ldep = malloc ( sizeof ( struct denode ) , M_MSDOSFSNODE , M_WAITOK | M_ZERO ) ; error = getnewvnode ( " " , mntp , & msdosfs_vnodeops , & nvp ) ; if ( error ) { * depp = NULL ; free ( ldep , M_MSDOSFSNODE ) ; return error ; } nvp -> v_data = ldep ; ldep -> de_vnode = nvp ; ldep -> de_flag = 0 ; ldep -> de_dirclust = dirclust ; ldep -> de_diroffset = diroffset ; ldep -> de_inode = inode ; lockmgr ( nvp -> v_vnlock , LK_EXCLUSIVE , NULL ) ; fc_purge ( ldep , 0 ) ; error = insmntque ( nvp , mntp ) ; if ( error != 0 ) { free ( ldep , M_MSDOSFSNODE ) ; * depp = NULL ; return ( error ) ; } error = vfs_hash_insert ( nvp , inode , LK_EXCLUSIVE , curthread , & xvp , de_vncmpf , & inode ) ; if ( error ) { * depp = NULL ; return ( error ) ; } if ( xvp != NULL ) { * depp = xvp -> v_data ; return ( 0 ) ; } ldep -> de_pmp = pmp ; ldep -> de_refcnt = 1 ; if ( ( dirclust == MSDOSFSROOT || ( FAT32 ( pmp ) && dirclust == pmp -> pm_rootdirblk ) ) && diroffset == MSDOSFSROOT_OFS ) { nvp -> v_vflag |= VV_ROOT ; ldep -> de_Attributes = ATTR_DIRECTORY ; ldep -> de_LowerCase = 0 ; if ( FAT32 ( pmp ) ) ldep -> de_StartCluster = pmp -> pm_rootdirblk ; else { ldep -> de_StartCluster = MSDOSFSROOT ; ldep -> de_FileSize = pmp -> pm_rootdirsize * DEV_BSIZE ; } ldep -> de_CHun = 0 ; ldep -> de_CTime = 0x0000 ; ldep -> de_CDate = ( 0 << DD_YEAR_SHIFT ) | ( 1 << DD_MONTH_SHIFT ) | ( 1 << DD_DAY_SHIFT ) ; ldep -> de_ADate = ldep -> de_CDate ; ldep -> de_MTime = ldep -> de_CTime ; ldep -> de_MDate = ldep -> de_CDate ; } else { error = readep ( pmp , dirclust , diroffset , & bp , & direntptr ) ; if ( error ) { ldep -> de_Name [ 0 ] = SLOT_DELETED ; vput ( nvp ) ; * depp = NULL ; return ( error ) ; } ( void ) DE_INTERNALIZE ( ldep , direntptr ) ; brelse ( bp ) ; } if ( ldep -> de_Attributes & ATTR_DIRECTORY ) { u_long size ; if ( diroffset == 0 && ldep -> de_StartCluster != dirclust ) { #ifdef MSDOSFS_DEBUG printf ( " \" \" \n " , dirclust , ldep -> de_StartCluster ) ; #endif ldep -> de_StartCluster = dirclust ; } nvp -> v_type = VDIR ; if ( ldep -> de_StartCluster != MSDOSFSROOT ) { error = pcbmap ( ldep , 0xffff , 0 , & size , 0 ) ; if ( error == E2BIG ) { ldep -> de_FileSize = de_cn2off ( pmp , size ) ; error = 0 ; } else { #ifdef MSDOSFS_DEBUG printf ( " \n " , error ) ; #endif } } } else nvp -> v_type = VREG ; ldep -> de_modrev = init_va_filerev ( ) ; * depp = ldep ; return ( 0 ) ; }
If deget() succeeds it returns with the gotten denode locked().
If deget () succeeds it returns with the gotten denode locked () .
/*
	 * On FAT32 filesystems, root is a (more or less) normal
	 * directory
	 */ /*
	 * See if the denode is in the denode cache. Use the location of
	 * the directory entry to compute the hash value. For subdir use
	 * address of "." entry. For root dir (if not FAT32) use cluster
	 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
	 *
	 * 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.
	 */ /*
	 * 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 */ /* init the fat cache for this denode */ /*
	 * Copy the directory entry into the denode area of the vnode.
	 */ /*
		 * 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.
		 */ /* should be further down XXX */ /* de_FileSize will be filled in further down */ /*
		 * fill in time and date so that fattime2timespec() doesn't
		 * spit up when called from msdosfs_getattr() with root
		 * denode
		 */ /* 00:00:00	 */ /* Jan 1, 1980	 */ /* leave the other fields as garbage */ /*
			 * 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.
			 */ /*
	 * Fill in a few fields of the vnode and finish filling in the
	 * denode.  Then return the address of the found denode.
	 */ /*
		 * 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.
		 */ /*
		 * XXX it sometimes happens that the "." entry has cluster
		 * number 0 when it shouldn't.  Use the actual cluster number
		 * instead of what is written in directory entry.
		 */