From patchwork Tue Sep 30 21:02:26 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 5007051 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 53349BEEA6 for ; Tue, 30 Sep 2014 21:04:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 79B40201EF for ; Tue, 30 Sep 2014 21:04:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 99502201C7 for ; Tue, 30 Sep 2014 21:04:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751807AbaI3VEd (ORCPT ); Tue, 30 Sep 2014 17:04:33 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:55551 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750846AbaI3VEb (ORCPT ); Tue, 30 Sep 2014 17:04:31 -0400 Received: from pps.filterd (m0004077 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id s8UL4RY0020091 for ; Tue, 30 Sep 2014 14:04:30 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : subject : date : message-id : mime-version : content-type; s=facebook; bh=+IxaJjQFPQIglxKCfGe0xyrAHsup48pmKajss9HLGn0=; b=Y8Fxa4uBlLynF2V3CseqeHxOFvJ+1aQCr982wuC4e07aYWBXFmPS4bZ+1W4yqnsYeWBr B+3yehhY8dQjtY2VxjI28/kEN5I6kyL7C0kS59of734Ba2o+9YmrWiBGEdCkSPu7E2a0 GvjHgP6kC8yLsK42AcIphRdo2CWpOx1aXQU= Received: from mail.thefacebook.com (mailwest.thefacebook.com [173.252.71.148]) by mx0b-00082601.pphosted.com with ESMTP id 1pqrywjg51-1 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=OK) for ; Tue, 30 Sep 2014 14:04:29 -0700 Received: from localhost (192.168.57.29) by mail.TheFacebook.com (192.168.16.18) with Microsoft SMTP Server (TLS) id 14.3.195.1; Tue, 30 Sep 2014 14:02:28 -0700 From: Josef Bacik To: Subject: [PATCH] Btrfs-progs: add shift_items to btrfs-corrupt-block Date: Tue, 30 Sep 2014 17:02:26 -0400 Message-ID: <1412110946-8598-1-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [192.168.57.29] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.12.52, 1.0.28, 0.0.0000 definitions=2014-09-30_08:2014-09-30, 2014-09-30, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 spamscore=0 suspectscore=3 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1402240000 definitions=main-1409300212 X-FB-Internal: deliver Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP A user had a corrupted fs where his items where shifted oddly. This adds the functionality I needed to btrfs-corrupt-block in order to reproduce this corruption in order to make fsck fix this sort of problem. Thanks, Signed-off-by: Josef Bacik --- btrfs-corrupt-block.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 474d48f..22390b5 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -303,6 +303,7 @@ enum btrfs_file_extent_field { enum btrfs_metadata_block_field { BTRFS_METADATA_BLOCK_GENERATION, + BTRFS_METADATA_BLOCK_SHIFT_ITEMS, BTRFS_METADATA_BLOCK_BAD, }; @@ -332,6 +333,8 @@ convert_metadata_block_field(char *field) { if (!strncmp(field, "generation", FIELD_BUF_LEN)) return BTRFS_METADATA_BLOCK_GENERATION; + if (!strncmp(field, "shift_items", FIELD_BUF_LEN)) + return BTRFS_METADATA_BLOCK_SHIFT_ITEMS; return BTRFS_METADATA_BLOCK_BAD; } @@ -538,6 +541,26 @@ out: return ret; } +static void shift_items(struct btrfs_root *root, struct extent_buffer *eb) +{ + int nritems = btrfs_header_nritems(eb); + int shift_space = btrfs_leaf_free_space(root, eb) / 2; + int slot = nritems / 2; + int i = 0; + unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1); + + /* Shift the item data up to and including slot back by shift space */ + memmove_extent_buffer(eb, data_end - shift_space, data_end, + btrfs_item_offset_nr(eb, slot - 1) - data_end); + + /* Now update the item pointers. */ + for (i = nritems - 1; i >= slot; i--) { + u32 offset = btrfs_item_offset_nr(eb, i); + offset -= shift_space; + btrfs_set_item_offset(eb, btrfs_item_nr(i), offset); + } +} + static int corrupt_metadata_block(struct btrfs_root *root, u64 block, char *field) { @@ -608,6 +631,9 @@ static int corrupt_metadata_block(struct btrfs_root *root, u64 block, bogus = generate_u64(orig); btrfs_set_header_generation(eb, bogus); break; + case BTRFS_METADATA_BLOCK_SHIFT_ITEMS: + shift_items(root, path->nodes[level]); + break; default: ret = -EINVAL; break;