+/*
+ * This function is called when link_counted is zero. So this may not
+ * be an xattr inode at all. Return immediately if EA_INODE flag is not
+ * set.
+ */
+static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i, ext2_ino_t *last_ino,
+ struct ext2_inode_large *inode, __u16 *link_counted)
+{
+ __u64 actual_refs = 0;
+ __u64 ref_count;
+
+ if (*last_ino != i) {
+ e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
+ EXT2_INODE_SIZE(ctx->fs->super),
+ "pass4: check_ea_inode");
+ *last_ino = i;
+ }
+ if (!(inode->i_flags & EXT4_EA_INODE_FL))
+ return;
+
+ if (ctx->ea_inode_refs)
+ ea_refcount_fetch(ctx->ea_inode_refs, i, &actual_refs);
+ if (!actual_refs)
+ return;
+
+ /*
+ * There are some attribute references, link_counted is now considered
+ * to be 1.
+ */
+ *link_counted = 1;
+
+ ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(inode));
+
+ /* Old Lustre-style xattr inodes do not have a stored refcount.
+ * However, their i_ctime and i_atime should be the same.
+ */
+ if (ref_count != actual_refs && inode->i_ctime != inode->i_atime) {
+ struct problem_context pctx;
+
+ clear_problem_context(&pctx);
+ pctx.ino = i;
+ pctx.num = ref_count;
+ pctx.num2 = actual_refs;
+ if (fix_problem(ctx, PR_4_EA_INODE_REF_COUNT, &pctx)) {
+ ext2fs_set_ea_inode_ref(EXT2_INODE(inode), actual_refs);
+ e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "pass4");
+ }
+ }
+}
+
+static errcode_t check_link_ea(e2fsck_t ctx, ext2_ino_t ino,
+ ext2_ino_t *last_ino,
+ struct ext2_inode_large *inode,
+ __u16 *link_counted)
+{
+ struct ext2_xattr_handle *handle;
+ struct link_ea_header *leh;
+ void *buf;
+ size_t ea_len;
+ errcode_t retval;
+
+ if (*last_ino != ino) {
+ e2fsck_read_inode_full(ctx, ino, EXT2_INODE(inode),
+ EXT2_INODE_SIZE(ctx->fs->super),
+ "pass4: get link ea count");
+ *last_ino = ino;
+ }
+
+ retval = ext2fs_xattrs_open(ctx->fs, ino, &handle);
+ if (retval)
+ return retval;
+
+ retval = ext2fs_xattrs_read_inode(handle, inode);
+ if (retval)
+ goto err;
+
+ retval = ext2fs_xattr_get(handle, EXT2_ATTR_INDEX_TRUSTED_PREFIX
+ LUSTRE_XATTR_MDT_LINK, &buf, &ea_len);
+ if (retval)
+ goto err;
+
+ leh = (struct link_ea_header *)buf;
+ if (leh->leh_magic == ext2fs_swab32(LINK_EA_MAGIC)) {
+ leh->leh_magic = LINK_EA_MAGIC;
+ leh->leh_reccount = ext2fs_swab32(leh->leh_reccount);
+ leh->leh_len = ext2fs_swab64(leh->leh_len);
+ }
+ if (leh->leh_magic != LINK_EA_MAGIC) {
+ retval = EINVAL;
+ goto err_free;
+ }
+ if (leh->leh_reccount == 0 && !leh->leh_overflow_time) {
+ retval = ENODATA;
+ goto err_free;
+ }
+ if (leh->leh_len > ea_len) {
+ retval = EINVAL;
+ goto err_free;
+ }
+
+ /* if linkEA overflowed and does not hold all links, assume *some*
+ * links exist until LFSCK is next run and resets leh_overflow_time */
+ if (leh->leh_overflow_time) {
+ if (inode->i_links_count > *link_counted)
+ *link_counted = inode->i_links_count;
+ else if (*link_counted == 0)
+ *link_counted = 1111;
+ }
+ if (leh->leh_reccount > *link_counted)
+ *link_counted = leh->leh_reccount;
+err_free:
+ ext2fs_free_mem(&buf);
+err:
+ ext2fs_xattrs_close(&handle);
+ return retval;
+}