From patchwork Fri Oct 10 20:57:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 5067291 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 425DF9F295 for ; Fri, 10 Oct 2014 20:57:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E7D3C201FE for ; Fri, 10 Oct 2014 20:57:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 963282018E for ; Fri, 10 Oct 2014 20:57:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754584AbaJJU53 (ORCPT ); Fri, 10 Oct 2014 16:57:29 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:37223 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751498AbaJJU50 (ORCPT ); Fri, 10 Oct 2014 16:57:26 -0400 Received: from pps.filterd (m0004060 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id s9AKu4rV028553 for ; Fri, 10 Oct 2014 13:57:25 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=tKghCQEv29mqRiJro8kB/aPhUYvrHf0bjOdHhCq0DHY=; b=ma1LIfOwBVNh64kYqZp+fsacseE7Gw54LvbZUMI0JzKg4k8DPzL4vTTecSE627TM0gLY YITcfIh79mHZJ5zbBh5f/lWdcIpHXjMxU5+cnBFILcMHpImoFQtoEThIcGtCtufBOL6r bS8yNlNSuA+/QnNR1W6VoMIlmN0lwCSqVvA= Received: from mail.thefacebook.com (mailwest.thefacebook.com [173.252.71.148]) by mx0b-00082601.pphosted.com with ESMTP id 1pxprq81mf-1 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=OK) for ; Fri, 10 Oct 2014 13:57:25 -0700 Received: from localhost (192.168.57.29) by mail.thefacebook.com (192.168.16.15) with Microsoft SMTP Server (TLS) id 14.3.195.1; Fri, 10 Oct 2014 13:57:23 -0700 From: Josef Bacik To: Subject: [PATCH 01/12] Btrfs-progs: repair missing dir index Date: Fri, 10 Oct 2014 16:57:06 -0400 Message-ID: <1412974637-31334-2-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1412974637-31334-1-git-send-email-jbacik@fb.com> References: <1412974637-31334-1-git-send-email-jbacik@fb.com> 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-10-10_08:2014-10-10, 2014-10-10, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 kscore.is_bulkscore=3.2261198479322e-07 kscore.compositescore=0 circleOfTrustscore=502.112 compositescore=0.999776574422466 urlsuspect_oldscore=0.999776574422466 suspectscore=3 recipient_domain_to_sender_totalscore=0 phishscore=0 bulkscore=0 kscore.is_spamscore=0 recipient_to_sender_totalscore=0 recipient_domain_to_sender_domain_totalscore=62764 rbsscore=0.999776574422466 spamscore=0 recipient_to_sender_domain_totalscore=0 urlsuspectscore=0.9 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1402240000 definitions=main-1410100175 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.8 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 If we have an inode backref entry then we know enough to add back a missing dir index. When messing with the inode backrefs we need to do all of that first before we process the inode recs themselves as we may clear errors on the inode recs as we fix the directory indexes. This adds the framework for fixing backref errors and fixes missing dir index issues. Thanks, Signed-off-by: Josef Bacik --- cmds-check.c | 121 +++++++++++++++++++++++++++++++++- tests/fsck-tests/004-no-dir-index.img | Bin 0 -> 4096 bytes 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/fsck-tests/004-no-dir-index.img diff --git a/tests/fsck-tests/004-no-dir-index.img b/tests/fsck-tests/004-no-dir-index.img new file mode 100644 index 0000000000000000000000000000000000000000..6f2483e6e4285e6703096b67c5153b3da3309b7b GIT binary patch literal 4096 zcmeH|c{CJUAIEJ`%Gf3$Yh{TN(O65bwZcpzM#hrFj3rNlG!&ySmKlmlLp5Y4%QQq8 z3`QlwWEuM!qM{Lxb%uGF^PcmZ^PJ~-|9;PTpL_24-p~Erd+t5I@4e?am@t^x&j!i`)W=1!q;d5+8PxO{tuc^bIt#*J|CANj|D ze;oMVaUgVXXo8{^!z;RWI&1#nFXUzYkZ5>2AIq9!8@J!EA8 zXY=Hw$+BtVRMY{<_{I-+mBhd5?7sWjOecAB_c`)7%H`0>IPoUFIMuiJv=a}oFFH5s zobIMmgCB5y6zEbxp0^{s_;2ame#h%0aVJVFkmr%Dk49Q@_er4cAvLJn9zYA<<*&XC ze)lAVGsUm-O5%2jZO%$5O^JG>BBgibcmNZlpxVM#yha8nF<1ZcY({zf*Kf*t>->d_ zfZ0VM=yd1P4~bKh?g{O~LEW-VL?J*{OHPqB8p;3Eq;fcn$5YF=W5Fa!71BTi_C69=6bP8uXC9ubv5p4fPgX4+M2OD^{cGS@fCmLJI)EeA|hE& z*IS9FgrgCgN}I>B8FQ{-z9C&F>O*4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Y|1F`&$Hp3N4YI5h_>D?87$Y9>UE9_^OV9zdF+V{}GBAk(!bd?E2yB_d(xo_MaA0*UD`BJwf(_fFI;+|$8xk3e$&cGB`fh+J&CUXc>l|esBi0q) zoPxx>ZN3CuR`?_Dzao#;vjlv6o|Vkx57nWN9xYcr-jHXuElQ9aO5q4lMjx6_kW(2! z5~>0^UzoZIBG?q!Ca{*z3W}Nv&W~)631JPKw(etWISDn}L4akxT|qVNHfT{QdV<9o zEqfLi$9mnhUlDn$jvMK-(yGm)=u3u zHJrCxt@V1jxHjU$7U4xzZSH)XB(ND&nr9(cobO50<#>43gbsmG_esm-s#$&%lej*=ln<7#rq`+{kBJ&G zN0&OTutw^66^|oDw{!iJB|^K%W7)}J5TvUbl9O?Q(TAl?_*w+_7ivTycJXB&<$IvI z?=Ii_eF6!}xr`Xzj+E8u(@S7e_QGfhDY`F|OdNd~)IWsnG{oGw{?(wqO`)0P<(ye= zP98*GV%hx3WS+G?p|L4R+V;+EWD%OS*shB%KnI!FT@3?m=k%^OXz79D%P&#R^DP4ZOqlmv6F8U_SA~XW1^Nn(tj422mTkKpAAgiQ9!0pQ=Y@>vNsZ Vz`wTS|0Ig(1o-l2+5Wd|{|(AQMP&d0 literal 0 HcmV?d00001 diff --git a/cmds-check.c b/cmds-check.c index 76df5ae..a7e0840 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -1516,15 +1516,111 @@ static int repair_inode_orphan_item(struct btrfs_trans_handle *trans, return ret; } +static int add_missing_dir_index(struct btrfs_root *root, + struct cache_tree *inode_cache, + struct inode_record *rec, + struct inode_backref *backref) +{ + struct btrfs_path *path; + struct btrfs_trans_handle *trans; + struct btrfs_dir_item *dir_item; + struct extent_buffer *leaf; + struct btrfs_key key; + struct btrfs_disk_key disk_key; + struct inode_record *dir_rec; + unsigned long name_ptr; + u32 data_size = sizeof(*dir_item) + backref->namelen; + int ret; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + trans = btrfs_start_transaction(root, 1); + if (IS_ERR(trans)) { + btrfs_free_path(path); + return PTR_ERR(trans); + } + + fprintf(stderr, "repairing missing dir index item for inode %llu\n", + (unsigned long long)rec->ino); + key.objectid = backref->dir; + key.type = BTRFS_DIR_INDEX_KEY; + key.offset = backref->index; + + ret = btrfs_insert_empty_item(trans, root, path, &key, data_size); + BUG_ON(ret); + + leaf = path->nodes[0]; + dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); + + disk_key.objectid = cpu_to_le64(rec->ino); + disk_key.type = BTRFS_INODE_ITEM_KEY; + disk_key.offset = 0; + + btrfs_set_dir_item_key(leaf, dir_item, &disk_key); + btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode)); + btrfs_set_dir_data_len(leaf, dir_item, 0); + btrfs_set_dir_name_len(leaf, dir_item, backref->namelen); + name_ptr = (unsigned long)(dir_item + 1); + write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen); + btrfs_mark_buffer_dirty(leaf); + btrfs_free_path(path); + btrfs_commit_transaction(trans, root); + + backref->found_dir_index = 1; + dir_rec = get_inode_rec(inode_cache, backref->dir, 0); + if (!dir_rec) + return 0; + dir_rec->found_size += backref->namelen; + if (dir_rec->found_size == dir_rec->isize && + (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG)) + dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG; + if (dir_rec->found_size != dir_rec->isize) + dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG; + + return 0; +} + +static int repair_inode_backrefs(struct btrfs_root *root, + struct inode_record *rec, + struct cache_tree *inode_cache) +{ + struct inode_backref *tmp, *backref; + u64 root_dirid = btrfs_root_dirid(&root->root_item); + int ret = 0; + + list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) { + /* Index 0 for root dir's are special, don't mess with it */ + if (rec->ino == root_dirid && backref->index == 0) + continue; + + if (!backref->found_dir_index && backref->found_inode_ref) { + ret = add_missing_dir_index(root, inode_cache, rec, + backref); + if (ret) + break; + } + + if (backref->found_dir_item && backref->found_dir_index) { + if (!backref->errors && backref->found_inode_ref) { + list_del(&backref->list); + free(backref); + } + } + } + + return ret; +} + static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec) { struct btrfs_trans_handle *trans; struct btrfs_path *path; int ret = 0; - /* So far we just fix dir isize wrong */ if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM))) - return 1; + return rec->errors; path = btrfs_alloc_path(); if (!path) @@ -1562,6 +1658,27 @@ static int check_inode_recs(struct btrfs_root *root, return 0; } + /* + * We need to repair backrefs first because we could change some of the + * errors in the inode recs. + * + * For example, if we were missing a dir index then the directories + * isize would be wrong, so if we fixed the isize to what we thought it + * would be and then fixed the backref we'd still have a invalid fs, so + * we need to add back the dir index and then check to see if the isize + * is still wrong. + */ + cache = search_cache_extent(inode_cache, 0); + while (repair && cache) { + node = container_of(cache, struct ptr_node, cache); + rec = node->data; + cache = next_cache_extent(cache); + + if (list_empty(&rec->backrefs)) + continue; + repair_inode_backrefs(root, rec, inode_cache); + } + rec = get_inode_rec(inode_cache, root_dirid, 0); if (rec) { ret = check_root_dir(rec);