Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2013 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "xfs_dir2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "xfs_dir2_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "xfs_bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * Directory file type support functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static unsigned char xfs_dir3_filetype_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) unsigned char
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) xfs_dir3_get_dtype(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	uint8_t			filetype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (!xfs_sb_version_hasftype(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return DT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if (filetype >= XFS_DIR3_FT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		return DT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	return xfs_dir3_filetype_table[filetype];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) xfs_dir2_sf_getdents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct xfs_da_args	*args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct dir_context	*ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int			i;		/* shortform entry number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct xfs_inode	*dp = args->dp;	/* incore directory inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct xfs_mount	*mp = dp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	xfs_dir2_dataptr_t	off;		/* current entry's offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	xfs_dir2_sf_entry_t	*sfep;		/* shortform directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	xfs_dir2_sf_hdr_t	*sfp;		/* shortform structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	xfs_dir2_dataptr_t	dot_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	xfs_dir2_dataptr_t	dotdot_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	xfs_ino_t		ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct xfs_da_geometry	*geo = args->geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ASSERT(dp->i_df.if_u1.if_data != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 * If the block number in the offset is out of range, we're done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	 * Precalculate offsets for "." and ".." as we will always need them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	 * This relies on the fact that directories always start with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	 * entries for "." and "..".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			geo->data_entry_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			geo->data_entry_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			xfs_dir2_data_entsize(mp, sizeof(".") - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 * Put . entry unless we're starting past it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (ctx->pos <= dot_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		ctx->pos = dot_offset & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * Put .. entry unless we're starting past it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (ctx->pos <= dotdot_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		ino = xfs_dir2_sf_get_parent_ino(sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		ctx->pos = dotdot_offset & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 * Loop while there are more entries and put'ing works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	sfep = xfs_dir2_sf_firstentry(sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	for (i = 0; i < sfp->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		uint8_t filetype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				xfs_dir2_sf_get_offset(sfep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		if (ctx->pos > off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		filetype = xfs_dir2_sf_get_ftype(mp, sfep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		ctx->pos = off & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		if (XFS_IS_CORRUPT(dp->i_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				   !xfs_dir2_namecheck(sfep->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 						       sfep->namelen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			    xfs_dir3_get_dtype(mp, filetype)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 								0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * Readdir for block directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) xfs_dir2_block_getdents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct xfs_da_args	*args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct dir_context	*ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct xfs_inode	*dp = args->dp;	/* incore directory inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct xfs_buf		*bp;		/* buffer for block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int			error;		/* error return value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int			wantoff;	/* starting block offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	xfs_off_t		cook;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct xfs_da_geometry	*geo = args->geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	int			lock_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	unsigned int		offset, next_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	unsigned int		end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * If the block number in the offset is out of range, we're done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	lock_mode = xfs_ilock_data_map_shared(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	error = xfs_dir3_block_read(args->trans, dp, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	xfs_iunlock(dp, lock_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * Extract the byte offset we start at from the seek pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * We'll skip entries before this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	xfs_dir3_data_check(dp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * Loop over the data portion of the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 * Each object is a real entry (dep) or an unused one (dup).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	end = xfs_dir3_data_end_offset(geo, bp->b_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	for (offset = geo->data_entry_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	     offset < end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	     offset = next_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		struct xfs_dir2_data_unused	*dup = bp->b_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		struct xfs_dir2_data_entry	*dep = bp->b_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		uint8_t filetype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		 * Unused, skip it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			next_offset = offset + be16_to_cpu(dup->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		 * Bump pointer for the next iteration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		next_offset = offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			xfs_dir2_data_entsize(dp->i_mount, dep->namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		 * The entry is before the desired starting point, skip it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (offset < wantoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		ctx->pos = cook & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		 * If it didn't fit, set the final offset to here & return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (XFS_IS_CORRUPT(dp->i_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 				   !xfs_dir2_namecheck(dep->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 						       dep->namelen))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			goto out_rele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			    be64_to_cpu(dep->inumber),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			    xfs_dir3_get_dtype(dp->i_mount, filetype)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			goto out_rele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	 * Reached the end of the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	 * Set the offset to a non-existent block 1 and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 								0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) out_rele:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	xfs_trans_brelse(args->trans, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * Read a directory block and initiate readahead for blocks beyond that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * We maintain a sliding readahead window of the remaining space in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * buffer rounded up to the nearest block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) xfs_dir2_leaf_readbuf(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct xfs_da_args	*args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	size_t			bufsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	xfs_dir2_off_t		*cur_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	xfs_dablk_t		*ra_blk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct xfs_buf		**bpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct xfs_inode	*dp = args->dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct xfs_buf		*bp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct xfs_da_geometry	*geo = args->geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct xfs_bmbt_irec	map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct blk_plug		plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	xfs_dir2_off_t		new_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	xfs_dablk_t		next_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	xfs_dablk_t		map_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	xfs_dablk_t		last_da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	int			ra_want;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * Look for mapped directory blocks at or above the current offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 * Truncate down to the nearest directory block to start the scanning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 * operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (map.br_startoff >= last_da)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	xfs_trim_extent(&map, map_off, last_da - map_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/* Read the directory block of that first mapping. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (new_off > *cur_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		*cur_off = new_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * Start readahead for the next bufsize's worth of dir data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * We may have already issued readahead for some of that range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * ra_blk tracks the last block we tried to read(ahead).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (*ra_blk >= last_da)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	else if (*ra_blk == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		*ra_blk = map.br_startoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	next_ra = map.br_startoff + geo->fsbcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (next_ra >= last_da)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		goto out_no_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (map.br_blockcount < geo->fsbcount &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	    !xfs_iext_next_extent(ifp, &icur, &map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		goto out_no_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (map.br_startoff >= last_da)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		goto out_no_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	xfs_trim_extent(&map, next_ra, last_da - next_ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	/* Start ra for each dir (not fs) block that has a mapping. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	while (ra_want > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		while (ra_want > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		       next_ra < map.br_startoff + map.br_blockcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			if (next_ra >= last_da) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				*ra_blk = last_da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			if (next_ra > *ra_blk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				xfs_dir3_data_readahead(dp, next_ra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 							XFS_DABUF_MAP_HOLE_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				*ra_blk = next_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			ra_want -= geo->fsbcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			next_ra += geo->fsbcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		if (!xfs_iext_next_extent(ifp, &icur, &map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			*ra_blk = last_da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	*bpp = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) out_no_ra:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	*ra_blk = last_da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * Getdents (readdir) for leaf and node directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * This reads the data blocks only, so is the same for both forms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) xfs_dir2_leaf_getdents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct xfs_da_args	*args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct dir_context	*ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	size_t			bufsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct xfs_inode	*dp = args->dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct xfs_mount	*mp = dp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct xfs_buf		*bp = NULL;	/* data block buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	xfs_dir2_data_entry_t	*dep;		/* data entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	xfs_dir2_data_unused_t	*dup;		/* unused entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct xfs_da_geometry	*geo = args->geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	xfs_dablk_t		rablk = 0;	/* current readahead block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	xfs_dir2_off_t		curoff;		/* current overall offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	int			length;		/* temporary length value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	int			byteoff;	/* offset in current block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	int			lock_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	unsigned int		offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int			error = 0;	/* error return value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * If the offset is at or past the largest allowed value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * give up right away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	 * Inside the loop we keep the main offset value as a byte offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 * in the directory file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 * Loop over directory entries until we reach the end offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 * Get more blocks and readahead as necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	while (curoff < XFS_DIR2_LEAF_OFFSET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		uint8_t filetype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		 * If we have no buffer, or we're off the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		 * current buffer, need to get another one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (!bp || offset >= geo->blksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			if (bp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				xfs_trans_brelse(args->trans, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 				bp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			lock_mode = xfs_ilock_data_map_shared(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 					&rablk, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			xfs_iunlock(dp, lock_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			if (error || !bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			xfs_dir3_data_check(dp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			 * Find our position in the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			offset = geo->data_entry_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			byteoff = xfs_dir2_byte_to_off(geo, curoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			 * Skip past the header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			if (byteoff == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 				curoff += geo->data_entry_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			 * Skip past entries until we reach our offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				while (offset < byteoff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 					dup = bp->b_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 					if (be16_to_cpu(dup->freetag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 						  == XFS_DIR2_DATA_FREE_TAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 						length = be16_to_cpu(dup->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 						offset += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 						continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 					dep = bp->b_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 					length = xfs_dir2_data_entsize(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 							dep->namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 					offset += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 				 * Now set our real offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 				curoff =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 					xfs_dir2_db_off_to_byte(geo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 					    xfs_dir2_byte_to_db(geo, curoff),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 					    offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 				if (offset >= geo->blksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		 * We have a pointer to an entry.  Is it a live one?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		dup = bp->b_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		 * No, it's unused, skip over it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			length = be16_to_cpu(dup->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			offset += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			curoff += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		dep = bp->b_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		length = xfs_dir2_data_entsize(mp, dep->namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		filetype = xfs_dir2_data_get_ftype(mp, dep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		if (XFS_IS_CORRUPT(dp->i_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 				   !xfs_dir2_namecheck(dep->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 						       dep->namelen))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			    be64_to_cpu(dep->inumber),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			    xfs_dir3_get_dtype(dp->i_mount, filetype)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		 * Advance to next entry in the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		offset += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		curoff += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		/* bufsize may have just been a guess; don't go negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		bufsize = bufsize > length ? bufsize - length : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	 * All done.  Set output offset value to current offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	if (bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		xfs_trans_brelse(args->trans, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  * Read a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)  * If supplied, the transaction collects locked dir buffers to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  * nested buffer deadlocks.  This function does not dirty the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  * transaction.  The caller should ensure that the inode is locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)  * before calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) xfs_readdir(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct xfs_inode	*dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	struct dir_context	*ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	size_t			bufsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct xfs_da_args	args = { NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	int			rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	int			v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	trace_xfs_readdir(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	args.dp = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	args.geo = dp->i_mount->m_dir_geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	args.trans = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		rval = xfs_dir2_sf_getdents(&args, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	else if ((rval = xfs_dir2_isblock(&args, &v)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	else if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		rval = xfs_dir2_block_getdents(&args, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }