⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xfs_dir2_sf.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	/*	 * Special case for ..	 */	if (args->namelen == 2 &&	    args->name[0] == '.' && args->name[1] == '.') {		args->inumber = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);		return XFS_ERROR(EEXIST);	}	/*	 * Loop over all the entries trying to match ours.	 */	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);	     i < sfp->hdr.count;	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {		if (sfep->namelen == args->namelen &&		    sfep->name[0] == args->name[0] &&		    memcmp(args->name, sfep->name, args->namelen) == 0) {			args->inumber =				xfs_dir2_sf_get_inumber(sfp,					xfs_dir2_sf_inumberp(sfep));			return XFS_ERROR(EEXIST);		}	}	/*	 * Didn't find it.	 */	ASSERT(args->oknoent);	return XFS_ERROR(ENOENT);}/* * Remove an entry from a shortform directory. */int						/* error */xfs_dir2_sf_removename(	xfs_da_args_t		*args){	int			byteoff;	/* offset of removed entry */	xfs_inode_t		*dp;		/* incore directory inode */	int			entsize;	/* this entry's size */	int			i;		/* shortform entry index */	int			newsize;	/* new inode size */	int			oldsize;	/* old inode size */	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */	xfs_dir2_sf_t		*sfp;		/* shortform structure */	xfs_dir2_trace_args("sf_removename", args);	dp = args->dp;	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);	oldsize = (int)dp->i_d.di_size;	/*	 * Bail out if the directory is way too short.	 */	if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {		ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));		return XFS_ERROR(EIO);	}	ASSERT(dp->i_df.if_bytes == oldsize);	ASSERT(dp->i_df.if_u1.if_data != NULL);	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));	/*	 * Loop over the old directory entries.	 * Find the one we're deleting.	 */	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);	     i < sfp->hdr.count;	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {		if (sfep->namelen == args->namelen &&		    sfep->name[0] == args->name[0] &&		    memcmp(sfep->name, args->name, args->namelen) == 0) {			ASSERT(xfs_dir2_sf_get_inumber(sfp,					xfs_dir2_sf_inumberp(sfep)) ==				args->inumber);			break;		}	}	/*	 * Didn't find it.	 */	if (i == sfp->hdr.count) {		return XFS_ERROR(ENOENT);	}	/*	 * Calculate sizes.	 */	byteoff = (int)((char *)sfep - (char *)sfp);	entsize = xfs_dir2_sf_entsize_byname(sfp, args->namelen);	newsize = oldsize - entsize;	/*	 * Copy the part if any after the removed entry, sliding it down.	 */	if (byteoff + entsize < oldsize)		memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,			oldsize - (byteoff + entsize));	/*	 * Fix up the header and file size.	 */	sfp->hdr.count--;	dp->i_d.di_size = newsize;	/*	 * Reallocate, making it smaller.	 */	xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;#if XFS_BIG_INUMS	/*	 * Are we changing inode number size?	 */	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {		if (sfp->hdr.i8count == 1)			xfs_dir2_sf_toino4(args);		else			sfp->hdr.i8count--;	}#endif	xfs_dir2_sf_check(args);	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);	return 0;}/* * Replace the inode number of an entry in a shortform directory. */int						/* error */xfs_dir2_sf_replace(	xfs_da_args_t		*args)		/* operation arguments */{	xfs_inode_t		*dp;		/* incore directory inode */	int			i;		/* entry index */#if XFS_BIG_INUMS || defined(DEBUG)	xfs_ino_t		ino=0;		/* entry old inode number */#endif#if XFS_BIG_INUMS	int			i8elevated;	/* sf_toino8 set i8count=1 */#endif	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */	xfs_dir2_sf_t		*sfp;		/* shortform structure */	xfs_dir2_trace_args("sf_replace", args);	dp = args->dp;	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);	/*	 * Bail out if the shortform directory is way too small.	 */	if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {		ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));		return XFS_ERROR(EIO);	}	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);	ASSERT(dp->i_df.if_u1.if_data != NULL);	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));#if XFS_BIG_INUMS	/*	 * New inode number is large, and need to convert to 8-byte inodes.	 */	if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->hdr.i8count == 0) {		int	error;			/* error return value */		int	newsize;		/* new inode size */		newsize =			dp->i_df.if_bytes +			(sfp->hdr.count + 1) *			((uint)sizeof(xfs_dir2_ino8_t) -			 (uint)sizeof(xfs_dir2_ino4_t));		/*		 * Won't fit as shortform, convert to block then do replace.		 */		if (newsize > XFS_IFORK_DSIZE(dp)) {			error = xfs_dir2_sf_to_block(args);			if (error) {				return error;			}			return xfs_dir2_block_replace(args);		}		/*		 * Still fits, convert to 8-byte now.		 */		xfs_dir2_sf_toino8(args);		i8elevated = 1;		sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	} else		i8elevated = 0;#endif	ASSERT(args->namelen != 1 || args->name[0] != '.');	/*	 * Replace ..'s entry.	 */	if (args->namelen == 2 &&	    args->name[0] == '.' && args->name[1] == '.') {#if XFS_BIG_INUMS || defined(DEBUG)		ino = xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent);		ASSERT(args->inumber != ino);#endif		xfs_dir2_sf_put_inumber(sfp, &args->inumber, &sfp->hdr.parent);	}	/*	 * Normal entry, look for the name.	 */	else {		for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);		     i < sfp->hdr.count;		     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep)) {			if (sfep->namelen == args->namelen &&			    sfep->name[0] == args->name[0] &&			    memcmp(args->name, sfep->name, args->namelen) == 0) {#if XFS_BIG_INUMS || defined(DEBUG)				ino = xfs_dir2_sf_get_inumber(sfp,					xfs_dir2_sf_inumberp(sfep));				ASSERT(args->inumber != ino);#endif				xfs_dir2_sf_put_inumber(sfp, &args->inumber,					xfs_dir2_sf_inumberp(sfep));				break;			}		}		/*		 * Didn't find it.		 */		if (i == sfp->hdr.count) {			ASSERT(args->oknoent);#if XFS_BIG_INUMS			if (i8elevated)				xfs_dir2_sf_toino4(args);#endif			return XFS_ERROR(ENOENT);		}	}#if XFS_BIG_INUMS	/*	 * See if the old number was large, the new number is small.	 */	if (ino > XFS_DIR2_MAX_SHORT_INUM &&	    args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {		/*		 * And the old count was one, so need to convert to small.		 */		if (sfp->hdr.i8count == 1)			xfs_dir2_sf_toino4(args);		else			sfp->hdr.i8count--;	}	/*	 * See if the old number was small, the new number is large.	 */	if (ino <= XFS_DIR2_MAX_SHORT_INUM &&	    args->inumber > XFS_DIR2_MAX_SHORT_INUM) {		/*		 * add to the i8count unless we just converted to 8-byte		 * inodes (which does an implied i8count = 1)		 */		ASSERT(sfp->hdr.i8count != 0);		if (!i8elevated)			sfp->hdr.i8count++;	}#endif	xfs_dir2_sf_check(args);	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);	return 0;}#if XFS_BIG_INUMS/* * Convert from 8-byte inode numbers to 4-byte inode numbers. * The last 8-byte inode number is gone, but the count is still 1. */static voidxfs_dir2_sf_toino4(	xfs_da_args_t		*args)		/* operation arguments */{	char			*buf;		/* old dir's buffer */	xfs_inode_t		*dp;		/* incore directory inode */	int			i;		/* entry index */	xfs_ino_t		ino;		/* entry inode number */	int			newsize;	/* new inode size */	xfs_dir2_sf_entry_t	*oldsfep;	/* old sf entry */	xfs_dir2_sf_t		*oldsfp;	/* old sf directory */	int			oldsize;	/* old inode size */	xfs_dir2_sf_entry_t	*sfep;		/* new sf entry */	xfs_dir2_sf_t		*sfp;		/* new sf directory */	xfs_dir2_trace_args("sf_toino4", args);	dp = args->dp;	/*	 * Copy the old directory to the buffer.	 * Then nuke it from the inode, and add the new buffer to the inode.	 * Don't want xfs_idata_realloc copying the data here.	 */	oldsize = dp->i_df.if_bytes;	buf = kmem_alloc(oldsize, KM_SLEEP);	oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	ASSERT(oldsfp->hdr.i8count == 1);	memcpy(buf, oldsfp, oldsize);	/*	 * Compute the new inode size.	 */	newsize =		oldsize -		(oldsfp->hdr.count + 1) *		((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));	xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);	xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);	/*	 * Reset our pointers, the data has moved.	 */	oldsfp = (xfs_dir2_sf_t *)buf;	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	/*	 * Fill in the new header.	 */	sfp->hdr.count = oldsfp->hdr.count;	sfp->hdr.i8count = 0;	ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);	xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);	/*	 * Copy the entries field by field.	 */	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),		    oldsfep = xfs_dir2_sf_firstentry(oldsfp);	     i < sfp->hdr.count;	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),		  oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {		sfep->namelen = oldsfep->namelen;		sfep->offset = oldsfep->offset;		memcpy(sfep->name, oldsfep->name, sfep->namelen);		ino = xfs_dir2_sf_get_inumber(oldsfp,			xfs_dir2_sf_inumberp(oldsfep));		xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));	}	/*	 * Clean up the inode.	 */	kmem_free(buf, oldsize);	dp->i_d.di_size = newsize;	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);}/* * Convert from 4-byte inode numbers to 8-byte inode numbers. * The new 8-byte inode number is not there yet, we leave with the * count 1 but no corresponding entry. */static voidxfs_dir2_sf_toino8(	xfs_da_args_t		*args)		/* operation arguments */{	char			*buf;		/* old dir's buffer */	xfs_inode_t		*dp;		/* incore directory inode */	int			i;		/* entry index */	xfs_ino_t		ino;		/* entry inode number */	int			newsize;	/* new inode size */	xfs_dir2_sf_entry_t	*oldsfep;	/* old sf entry */	xfs_dir2_sf_t		*oldsfp;	/* old sf directory */	int			oldsize;	/* old inode size */	xfs_dir2_sf_entry_t	*sfep;		/* new sf entry */	xfs_dir2_sf_t		*sfp;		/* new sf directory */	xfs_dir2_trace_args("sf_toino8", args);	dp = args->dp;	/*	 * Copy the old directory to the buffer.	 * Then nuke it from the inode, and add the new buffer to the inode.	 * Don't want xfs_idata_realloc copying the data here.	 */	oldsize = dp->i_df.if_bytes;	buf = kmem_alloc(oldsize, KM_SLEEP);	oldsfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	ASSERT(oldsfp->hdr.i8count == 0);	memcpy(buf, oldsfp, oldsize);	/*	 * Compute the new inode size.	 */	newsize =		oldsize +		(oldsfp->hdr.count + 1) *		((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));	xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);	xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);	/*	 * Reset our pointers, the data has moved.	 */	oldsfp = (xfs_dir2_sf_t *)buf;	sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;	/*	 * Fill in the new header.	 */	sfp->hdr.count = oldsfp->hdr.count;	sfp->hdr.i8count = 1;	ino = xfs_dir2_sf_get_inumber(oldsfp, &oldsfp->hdr.parent);	xfs_dir2_sf_put_inumber(sfp, &ino, &sfp->hdr.parent);	/*	 * Copy the entries field by field.	 */	for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),		    oldsfep = xfs_dir2_sf_firstentry(oldsfp);	     i < sfp->hdr.count;	     i++, sfep = xfs_dir2_sf_nextentry(sfp, sfep),		  oldsfep = xfs_dir2_sf_nextentry(oldsfp, oldsfep)) {		sfep->namelen = oldsfep->namelen;		sfep->offset = oldsfep->offset;		memcpy(sfep->name, oldsfep->name, sfep->namelen);		ino = xfs_dir2_sf_get_inumber(oldsfp,			xfs_dir2_sf_inumberp(oldsfep));		xfs_dir2_sf_put_inumber(sfp, &ino, xfs_dir2_sf_inumberp(sfep));	}	/*	 * Clean up the inode.	 */	kmem_free(buf, oldsize);	dp->i_d.di_size = newsize;	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);}#endif	/* XFS_BIG_INUMS */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -