From patchwork Tue Nov 22 08:25:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 9440609 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2DD0960237 for ; Tue, 22 Nov 2016 08:25:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2335E28480 for ; Tue, 22 Nov 2016 08:25:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 17B4628482; Tue, 22 Nov 2016 08:25:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB86328480 for ; Tue, 22 Nov 2016 08:25:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755546AbcKVIZ3 (ORCPT ); Tue, 22 Nov 2016 03:25:29 -0500 Received: from mail-pg0-f53.google.com ([74.125.83.53]:35581 "EHLO mail-pg0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755547AbcKVIZY (ORCPT ); Tue, 22 Nov 2016 03:25:24 -0500 Received: by mail-pg0-f53.google.com with SMTP id p66so5352096pga.2 for ; Tue, 22 Nov 2016 00:25:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=mUlTwH900Rwo0HnkmylPDHXyGXyRVOU+RPjLWUBS/44=; b=VkLs1V0bnggK9pT4Of0sI1qELCV7BqzsPajrfRY3fSmg2byjiuPnPNnrCUPoqSlguZ 37hLQuHDG2bYYqTTTS2FsS7/WUDFYuEtCDwJGN3XMSEMxW81rTmMhPT6VpO9MQ900w/D JsWK7i74hrJglVFtctnGl++0LXMJ6vNNhwLNwSgPfkjpL907w8sPhVgmEtQlXglMRBhS zyfz42yIj9UXdClXXUWjS9PiJYDV/J4CL68QMkpv9mY/5i/UAcOxuH6shjSaaEdprpPQ 9bNQPnJ0lIIh++khfmg5UMo81MvCSxvycUS3S9Vuvr+NglZjDgTA/Q7dBkX0lA5u56IW 7bmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=mUlTwH900Rwo0HnkmylPDHXyGXyRVOU+RPjLWUBS/44=; b=YSfsdf76Ad6X0iQ3fMK8tYwiryCRrTx4QHq3eegFIH7E3n01FdNiDxMQMdeL41T/5U xmlHyC/IocNc/q8rcTT/MjCn7NAPoKlBWexspP1h6GfeBiyOc/zd4YscgiNMucZiMUBh DC7M/NNAa1ZbRJtnMi6TnL/pZzJQhmoWEACW522NbCSuZI4q1dadtsAMqWgvW+17oxzY 2rwwgovta3hwx2dIFp0LMw/t+gwFnpQdv4gXuT5Rv9mLcIj4+QyMgnuDh/7Z6cefiX2E Mm/xv/nMODRyqH6rmlHdbLCsnKkXT/mEkcCM4L/iFF4y0UM7E4zdF8Yl2waK9aLBf1r7 eV6g== X-Gm-Message-State: AKaTC00jB2nVcVqOqeGrSY209wqnta/V5X/7iijs5OIqdwdss6ZEbMKvbL6RmAu4UYE2kwR5 X-Received: by 10.98.80.140 with SMTP id g12mr24014562pfj.54.1479803123435; Tue, 22 Nov 2016 00:25:23 -0800 (PST) Received: from localhost.localdomain ([2601:602:8801:8110:1a5e:fff:fea7:e0ef]) by smtp.gmail.com with ESMTPSA id y2sm42948466pff.82.2016.11.22.00.25.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Nov 2016 00:25:23 -0800 (PST) From: Omar Sandoval To: linux-fsdevel@vger.kernel.org Cc: linux-api@vger.kernel.org, kernel-team@fb.com Subject: [RFC PATCH 3/3] Btrfs: add support for linkat() AT_REPLACE Date: Tue, 22 Nov 2016 00:25:03 -0800 Message-Id: <6695444541aecd7a9f2360d9c22293522e7df41d.1479802448.git.osandov@fb.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: References: In-Reply-To: References: Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval The implementation is fairly straightforward and looks a lot like btrfs_rename(). Signed-off-by: Omar Sandoval --- fs/btrfs/inode.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 8e3a5a2..c903f6d 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -6566,15 +6566,20 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry, } static int btrfs_link(struct dentry *old_dentry, struct inode *dir, - struct dentry *dentry) + struct dentry *dentry, unsigned int flags) { struct btrfs_trans_handle *trans = NULL; + unsigned int trans_num_items; struct btrfs_root *root = BTRFS_I(dir)->root; struct inode *inode = d_inode(old_dentry); + struct inode *new_inode = d_inode(dentry); u64 index; int err; int drop_inode = 0; + if (flags & ~AT_REPLACE) + return -EINVAL; + /* do not allow sys_link's with other subvols of the same device */ if (root->objectid != BTRFS_I(inode)->root->objectid) return -EXDEV; @@ -6582,16 +6587,47 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, if (inode->i_nlink >= BTRFS_LINK_MAX) return -EMLINK; + /* check for collisions, even if the name isn't there */ + err = btrfs_check_dir_item_collision(root, dir->i_ino, + dentry->d_name.name, + dentry->d_name.len); + if (err) { + if (err == -EEXIST) { + if (WARN_ON(!new_inode)) + return err; + } else { + return err; + } + } + + /* + * we're using link to replace one file with another. Start IO on it now + * so we don't add too much work to the end of the transaction + */ + if (new_inode && S_ISREG(inode->i_mode) && new_inode->i_size) + filemap_flush(inode->i_mapping); + err = btrfs_set_inode_index(dir, &index); if (err) goto fail; /* + * For the source: * 2 items for inode and inode ref * 2 items for dir items * 1 item for parent inode + * + * For the target: + * 1 for the possible orphan item + * 1 for the dir item + * 1 for the dir index + * 1 for the inode ref + * 1 for the inode */ - trans = btrfs_start_transaction(root, 5); + trans_num_items = 5; + if (new_inode) + trans_num_items += 5; + trans = btrfs_start_transaction(root, trans_num_items); if (IS_ERR(trans)) { err = PTR_ERR(trans); trans = NULL; @@ -6603,6 +6639,21 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, inc_nlink(inode); inode_inc_iversion(inode); inode->i_ctime = current_time(inode); + + if (new_inode) { + inode_inc_iversion(new_inode); + new_inode->i_ctime = current_time(new_inode); + err = btrfs_unlink_inode(trans, root, dir, new_inode, + dentry->d_name.name, + dentry->d_name.len); + if (!err && new_inode->i_nlink == 0) + err = btrfs_orphan_add(trans, new_inode); + if (err) { + btrfs_abort_transaction(trans, err); + goto fail; + } + } + ihold(inode); set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); @@ -6624,7 +6675,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, if (err) goto fail; } - d_instantiate(dentry, inode); + d_replace(dentry, inode); btrfs_log_new_name(trans, inode, NULL, parent); } @@ -10570,7 +10621,7 @@ static const struct inode_operations btrfs_dir_inode_operations = { .lookup = btrfs_lookup, .create = btrfs_create, .unlink = btrfs_unlink, - .link = btrfs_link, + .link2 = btrfs_link, .mkdir = btrfs_mkdir, .rmdir = btrfs_rmdir, .rename = btrfs_rename2,