Skip to contentSkip to conversation

xfs_repair: add support for validating dirent ftype field

PATCH Superseded Dave Chinner 2 participants 2 messages Jan 22, 2014

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs_repair: add support for validating dirent ftype field

From: Dave Chinner <[email protected]>

Add code to track the filetype of an inode from phase 3 when all the inodes are scanned throught to phase 6 when the directory structure is validated and corrected.

Add code to phase 6 shortform and long form directory entry validation to read the ftype from the dirent, lookup the inode record and check they are the same. If they aren't and we are in no-modify mode, issue a warning such as:

Phase 6 - check inode connectivity...
- traversing filesystem ...
would fix ftype mismatch (5/1) in directory/child inode 64/68
- traversal finished ...
- moving disconnected inodes to lost+found ...

If we are fixing the problem:

Phase 6 - check inode connectivity...
- resetting contents of realtime bitmap and summary inodes
- traversing filesystem ...
fixing ftype mismatch (5/1) in directory/child inode 64/68
- traversal finished ...
- moving disconnected inodes to lost+found ...

Note that this is from a leaf form directory entry that was intentionally corrupted with xfs_db like so:

xfs_db> inode 64
xfs_db> a u3.bmx[0].startblock
xfs_db> p
....
du[3].inumber = 68
du[3].namelen = 11
du[3].name = "syscalltest"
du[3].filetype = 1
du[3].tag = 0x70
....
xfs_db> write du[3].filetype 5
du[3].filetype = 5
xfs_db> quit

Shortform directory entry repair was tested in a similar fashion.

Further, track the ftype in the directory hash table that is build, so if the directory is rebuild from scratch it has the necessary ftype information to rebuild the directory correctly. Further, if we detect a ftype mismatch, update the entry in the hash so that later directory errors that lead to it being rebuilt use the corrected ftype field, not the bad one.

Note that this code pulls in some kernel side code that is currently in kernel private locations (xfs_mode_to_ftype table), so there'll be some kernel/userspace sync work needed to bring these back into line.

Version 2:
- factored out junking of entry in shortform directory code
- fixed leak of ftypes array memory

---
 include/xfs_dir2.h   |   3 +
 libxfs/xfs_dir2.c    |  16 ++++
 repair/dino_chunks.c |  11 +++
 repair/incore.h      |  27 +++++-
 repair/incore_ino.c  |  30 ++++++-
 repair/phase6.c      | 235 ++++++++++++++++++++++++++++++++++++---------------
 repair/scan.c        |   4 +-
 7 files changed, 251 insertions(+), 75 deletions(-)
include/xfs_dir2.h +3 -0
index 9910401..3900130 100644
@@ -57,6 +57,9 @@ extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
 extern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp,
 				struct xfs_name *name, uint resblks);
 
+#define S_SHIFT 12
+extern const unsigned char xfs_mode_to_ftype[];
+
 /*
  * Direct call from the bmap code, bypassing the generic directory layer.
  */
libxfs/xfs_dir2.c +16 -0
index 96a3c1d..4c8c836 100644
@@ -20,6 +20,22 @@
 
 struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
 
+/*
+ * @mode, if set, indicates that the type field needs to be set up.
+ * This uses the transformation from file mode to DT_* as defined in linux/fs.h
+ * for file type specification. This will be propagated into the directory
+ * structure if appropriate for the given operation and filesystem config.
+ */
+const unsigned char xfs_mode_to_ftype[S_IFMT >> S_SHIFT] = {
+	[0]			= XFS_DIR3_FT_UNKNOWN,
+	[S_IFREG >> S_SHIFT]	= XFS_DIR3_FT_REG_FILE,
+	[S_IFDIR >> S_SHIFT]	= XFS_DIR3_FT_DIR,
+	[S_IFCHR >> S_SHIFT]	= XFS_DIR3_FT_CHRDEV,
+	[S_IFBLK >> S_SHIFT]	= XFS_DIR3_FT_BLKDEV,
+	[S_IFIFO >> S_SHIFT]	= XFS_DIR3_FT_FIFO,
+	[S_IFSOCK >> S_SHIFT]	= XFS_DIR3_FT_SOCK,
+	[S_IFLNK >> S_SHIFT]	= XFS_DIR3_FT_SYMLINK,
+};
 
 /*
  * ASCII case-insensitive (ie. A-Z) support for directories that was
repair/dino_chunks.c +11 -0
index d3c2236..65281e4 100644
@@ -788,6 +788,8 @@ process_inode_chunk(
 		 * we do now, this is where to start.
 		 */
 		if (is_used)  {
+			__uint16_t	di_mode;
+
 			if (is_inode_free(ino_rec, irec_offset))  {
 				if (verbose || no_modify)  {
 					do_warn(
@@ -803,6 +805,15 @@ process_inode_chunk(
 			set_inode_used(ino_rec, irec_offset);
 
 			/*
+			 * store the on-disk file type for comparing in
+			 * phase 6.
+			 */
+			di_mode = be16_to_cpu(dino->di_mode);
+			di_mode = (di_mode & S_IFMT) >> S_SHIFT;
+			set_inode_ftype(ino_rec, irec_offset,
+					xfs_mode_to_ftype[di_mode]);
+
+			/*
 			 * store on-disk nlink count for comparing in phase 7
 			 */
 			set_inode_disk_nlinks(ino_rec, irec_offset,
repair/incore.h +26 -1
index 38caa6d..5419884 100644
@@ -293,6 +293,7 @@ typedef struct ino_tree_node  {
 		ino_ex_data_t	*ex_data;	/* phases 6,7 */
 		parent_list_t	*plist;		/* phases 2-5 */
 	} ino_un;
+	__uint8_t		*ftypes;	/* phases 3,6 */
 } ino_tree_node_t;
 
 #define INOS_PER_IREC	(sizeof(__uint64_t) * NBBY)
@@ -359,7 +360,8 @@ ino_tree_node_t		*find_uncertain_inode_rec(xfs_agnumber_t agno,
 						xfs_agino_t ino);
 void			add_inode_uncertain(xfs_mount_t *mp,
 						xfs_ino_t ino, int free);
-void			add_aginode_uncertain(xfs_agnumber_t agno,
+void			add_aginode_uncertain(struct xfs_mount *mp,
+						xfs_agnumber_t agno,
 						xfs_agino_t agino, int free);
 void			get_uncertain_inode_rec(struct xfs_mount *mp,
 						xfs_agnumber_t agno,
@@ -476,6 +478,29 @@ static inline void add_inode_reached(struct ino_tree_node *irec, int offset)
 }
 
 /*
+ * get/set inode filetype. Only used if the superblock feature bit is set
+ * which allocates irec->ftypes.
+ */
+static inline void
+set_inode_ftype(struct ino_tree_node *irec,
+	int		ino_offset,
+	__uint8_t	ftype)
+{
+	if (irec->ftypes)
+		irec->ftypes[ino_offset] = ftype;
+}
+
+static inline __uint8_t
+get_inode_ftype(
+	struct ino_tree_node *irec,
+	int		ino_offset)
+{
+	if (!irec->ftypes)
+		return XFS_DIR3_FT_UNKNOWN;
+	return irec->ftypes[ino_offset];
+}
+
+/*
  * set/get inode number of parent -- works for directory inodes only
  */
 void		set_inode_parent(ino_tree_node_t *irec, int ino_offset,
repair/incore_ino.c +26 -4
index 735737a..9502648 100644
@@ -211,6 +211,21 @@ __uint32_t get_inode_disk_nlinks(struct ino_tree_node *irec, int ino_offset)
 	return 0;
 }
 
+static __uint8_t *
+alloc_ftypes_array(
+	struct xfs_mount *mp)
+{
+	__uint8_t	*ptr;
+
+	if (!xfs_sb_version_hasftype(&mp->m_sb))
+		return NULL;
+
+	ptr = calloc(XFS_INODES_PER_CHUNK, sizeof(*ptr));
+	if (!ptr)
+		do_error(_("could not allocate ftypes array\n"));
+	return ptr;
+}
+
 /*
  * Next is the uncertain inode list -- a sorted (in ascending order)
  * list of inode records sorted on the starting inode number.  There
@@ -226,6 +241,7 @@ __uint32_t get_inode_disk_nlinks(struct ino_tree_node *irec, int ino_offset)
  */
 static struct ino_tree_node *
 alloc_ino_node(
+	struct xfs_mount	*mp,
 	xfs_agino_t		starting_ino)
 {
 	struct ino_tree_node 	*irec;
@@ -245,6 +261,7 @@ alloc_ino_node(
 	irec->ino_un.ex_data = NULL;
 	irec->nlink_size = sizeof(__uint8_t);
 	irec->disk_nlinks.un8 = alloc_nlink_array(irec->nlink_size);
+	irec->ftypes = alloc_ftypes_array(mp);
 	return irec;
 }
 
@@ -285,6 +302,7 @@ free_ino_tree_node(
 
 	}
 
+	free(irec->ftypes);
 	free(irec);
 }
 
@@ -303,7 +321,11 @@ static ino_tree_node_t **last_rec;
  * free is set to 1 if the inode is thought to be free, 0 if used
  */
 void
-add_aginode_uncertain(xfs_agnumber_t agno, xfs_agino_t ino, int free)
+add_aginode_uncertain(
+	struct xfs_mount	*mp,
+	xfs_agnumber_t		agno,
+	xfs_agino_t		ino,
+	int			free)
 {
 	ino_tree_node_t		*ino_rec;
 	xfs_agino_t		s_ino;
@@ -334,7 +356,7 @@ add_aginode_uncertain(xfs_agnumber_t agno, xfs_agino_t ino, int free)
 	ino_rec = (ino_tree_node_t *)
 		avl_findrange(inode_uncertain_tree_ptrs[agno], s_ino);
 	if (!ino_rec) {
-		ino_rec = alloc_ino_node(s_ino);
+		ino_rec = alloc_ino_node(mp, s_ino);
 
 		if (!avl_insert(inode_uncertain_tree_ptrs[agno],
 				&ino_rec->avl_node))
@@ -360,7 +382,7 @@ add_aginode_uncertain(xfs_agnumber_t agno, xfs_agino_t ino, int free)
 void
 add_inode_uncertain(xfs_mount_t *mp, xfs_ino_t ino, int free)
 {
-	add_aginode_uncertain(XFS_INO_TO_AGNO(mp, ino),
+	add_aginode_uncertain(mp, XFS_INO_TO_AGNO(mp, ino),
 				XFS_INO_TO_AGINO(mp, ino), free);
 }
 
@@ -432,7 +454,7 @@ add_inode(
 {
 	struct ino_tree_node	*irec;
 
-	irec = alloc_ino_node(agino);
+	irec = alloc_ino_node(mp, agino);
 	if (!avl_insert(inode_tree_ptrs[agno],	&irec->avl_node))
 		do_warn(_("add_inode - duplicate inode range\n"));
 	return irec;
repair/phase6.c +167 -68

366 lines not on this page · raw message

repair/scan.c +2 -2
index 49ed194..73b4581 100644
@@ -866,9 +866,9 @@ _("inode rec for ino %" PRIu64 " (%d/%d) overlaps existing rec (start %d/%d)\n")
 		for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {
 			if (XFS_INOBT_IS_FREE_DISK(rp, j)) {
 				nfree++;
-				add_aginode_uncertain(agno, ino + j, 1);
+				add_aginode_uncertain(mp, agno, ino + j, 1);
 			} else  {
-				add_aginode_uncertain(agno, ino + j, 0);
+				add_aginode_uncertain(mp, agno, ino + j, 0);
 			}
 		}
 	}
Signature · 6 lines
1.8.4.rc3

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 72 lines · Dave Chinner

From: Dave Chinner <[email protected]>

Add code to track the filetype of an inode from phase 3 when all the inodes are scanned throught to phase 6 when the directory structure is validated and corrected.

Add code to phase 6 shortform and long form directory entry validation to read the ftype from the dirent, lookup the inode record and check they are the same. If they aren't and we are in no-modify mode, issue a warning such as:

Phase 6 - check inode connectivity...
- traversing filesystem ...
would fix ftype mismatch (5/1) in directory/child inode 64/68
- traversal finished ...
- moving disconnected inodes to lost+found ...

If we are fixing the problem:

Phase 6 - check inode connectivity...
- resetting contents of realtime bitmap and summary inodes
- traversing filesystem ...
fixing ftype mismatch (5/1) in directory/child inode 64/68
- traversal finished ...
- moving disconnected inodes to lost+found ...

Note that this is from a leaf form directory entry that was intentionally corrupted with xfs_db like so:

xfs_db> inode 64
xfs_db> a u3.bmx[0].startblock
xfs_db> p
....
du[3].inumber = 68
du[3].namelen = 11
du[3].name = "syscalltest"
du[3].filetype = 1
du[3].tag = 0x70
....
xfs_db> write du[3].filetype 5
du[3].filetype = 5
xfs_db> quit

Shortform directory entry repair was tested in a similar fashion.

Further, track the ftype in the directory hash table that is build, so if the directory is rebuild from scratch it has the necessary ftype information to rebuild the directory correctly. Further, if we detect a ftype mismatch, update the entry in the hash so that later directory errors that lead to it being rebuilt use the corrected ftype field, not the bad one.

Note that this code pulls in some kernel side code that is currently in kernel private locations (xfs_mode_to_ftype table), so there'll be some kernel/userspace sync work needed to bring these back into line.

Version 2:
- factored out junking of entry in shortform directory code
- fixed leak of ftypes array memory

---
 include/xfs_dir2.h   |   3 +
 libxfs/xfs_dir2.c    |  16 ++++
 repair/dino_chunks.c |  11 +++
 repair/incore.h      |  27 +++++-
 repair/incore_ino.c  |  30 ++++++-
 repair/phase6.c      | 235 ++++++++++++++++++++++++++++++++++++---------------
 repair/scan.c        |   4 +-
 7 files changed, 251 insertions(+), 75 deletions(-)

...

Quoted 4 lines
repair/phase6.c +0 -0
 		/*
 		 * check easy case first, regular inode, just bump
 		 * the link count and continue

repair/phase6.c out_fix: · line 2247

Quoted 10 lines
repair/phase6.c +7 -0
@@ -2189,6 +2238,59 @@ out_fix:
  * shortform directory v2 processing routines -- entry verification and
  * bad entry deletion (pruning).
  */
+static struct xfs_dir2_sf_entry *
+shortform_dir2_junk(
+	struct xfs_mount	*mp,
+	struct xfs_dir2_sf_hdr	*sfp,
+	struct xfs_dir2_sf_entry *sfep,
+	xfs_ino_t		lino,
+	int			max_size,

We should probably be passing max_size as a pointer. Otherwise, nice cleanup.

...

repair/phase6.c _("entry \"%s\" (ino %" PRIu64 ") in dir %" PRIu64 " is a duplicate name"), · line 2585

Quoted 11 lines
repair/phase6.c +4 -4
-		} else if (lino > XFS_DIR2_MAX_SHORT_INUM)
+		}
+
+		if (lino > XFS_DIR2_MAX_SHORT_INUM)
 			i8++;
 
 		/*
-		 * go onto next entry unless we've just junked an
-		 * entry in which the current entry pointer points
-		 * to an unprocessed entry.  have to take into entries
+		 * go onto next entry - we have to take into entries

Nit... extra "into" on the above line (reads weird with the line below). The rest of it looks good to me, though I'm pretty new to the directory format bits.

Brian

Quoted 44 lines
repair/phase6.c +7 -9
 		 * with bad namelen into account in no modify mode since we
 		 * calculate size based on next_sfep.
 		 */
 		ASSERT(no_modify || bad_sfnamelen == 0);
-
-		next_sfep = (tmp_sfep == NULL)
-			? (xfs_dir2_sf_entry_t *) ((__psint_t) sfep
-							+ ((!bad_sfnamelen)
-				? xfs_dir3_sf_entsize(mp, sfp, sfep->namelen)
-				: xfs_dir3_sf_entsize(mp, sfp, namelen)))
-			: tmp_sfep;
+		next_sfep = (struct xfs_dir2_sf_entry *)((__psint_t)sfep +
+			      (bad_sfnamelen
+				? xfs_dir3_sf_entsize(mp, sfp, namelen)
+				: xfs_dir3_sf_entsize(mp, sfp, sfep->namelen)));
 	}
 
 	if (sfp->i8count != i8) {
@@ -2501,6 +2599,8 @@ do_junkit:
 				ino);
 		} else {
 			if (i8 == 0) {
+				struct xfs_dir2_sf_entry *tmp_sfep;
+
 				tmp_sfep = next_sfep;
 				process_sf_dir2_fixi8(mp, sfp, &tmp_sfep);
 				bytes_deleted +=
@@ -2518,8 +2618,7 @@ do_junkit:
 	/*
 	 * sync up sizes if required
 	 */
-	if (*ino_dirty)  {
-		ASSERT(bytes_deleted > 0);
+	if (*ino_dirty && bytes_deleted > 0)  {
 		ASSERT(!no_modify);
 		libxfs_idata_realloc(ip, -bytes_deleted, XFS_DATA_FORK);
 		ip->i_d.di_size -= bytes_deleted;
repair/scan.c +0 -0
index 49ed194..73b4581 100644
@@ -866,9 +866,9 @@ _("inode rec for ino %" PRIu64 " (%d/%d) overlaps existing rec (start %d/%d)\n")
 		for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {

repair/scan.c _("inode rec for ino %" PRIu64 " (%d/%d) overlaps existing rec (start %d/%d)\n") · line 874

Quoted 10 lines
repair/scan.c +2 -2
 			if (XFS_INOBT_IS_FREE_DISK(rp, j)) {
 				nfree++;
-				add_aginode_uncertain(agno, ino + j, 1);
+				add_aginode_uncertain(mp, agno, ino + j, 1);
 			} else  {
-				add_aginode_uncertain(agno, ino + j, 0);
+				add_aginode_uncertain(mp, agno, ino + j, 0);
 			}
 		}
 	}

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs