From patchwork Tue Mar 21 14:51:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 9636733 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 DCBEE602CC for ; Tue, 21 Mar 2017 14:52:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CFB692833B for ; Tue, 21 Mar 2017 14:52:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C32E42840E; Tue, 21 Mar 2017 14:52:50 +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.4 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 482B82833B for ; Tue, 21 Mar 2017 14:52:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932907AbdCUOwt (ORCPT ); Tue, 21 Mar 2017 10:52:49 -0400 Received: from mail-qk0-f174.google.com ([209.85.220.174]:36403 "EHLO mail-qk0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933066AbdCUOwq (ORCPT ); Tue, 21 Mar 2017 10:52:46 -0400 Received: by mail-qk0-f174.google.com with SMTP id 1so136583397qkl.3 for ; Tue, 21 Mar 2017 07:52:45 -0700 (PDT) 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=XuWzSsc11nafLWgVO0vXybXC7L3cxfIKjrIsvpFw1B0=; b=VzAENd4I2JUq8SBL2WuW26JFGI4Wy5AOWi37YwTxROEY4k4WLVu5a8G3JR+uRH+Qwn o2CI4KFPaY/xpux5KjRrssrq279R0JTrHh8Qy586A0MJ0vC9q+7z1YPocYBcs/3k2GoQ FgCoB68H2KwqVKxAzcbhNM5QqVHv1f5DU59By+FoNG0d4im3zsNOws/LwXWIMFPLd7f3 X4mWkWikwcR+SBeFnA3zG9acw6XlCQKJiC9AETTdizphz2dqAFhw4s+yaaqOKq+C5CRf UU+KQO8spwVLNDG7+2M9DiSwu2XE7pSIfzn+lAs+DT4COx98PwoYuwTEq+0JOmFDQ0T0 T4KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=XuWzSsc11nafLWgVO0vXybXC7L3cxfIKjrIsvpFw1B0=; b=OS6zhTOVDyOhHTIREBMxJqzdb91S7him+9T6SEbe+tMBhykHVFCTvTYuBtdj197U2a ETzxkwG1mbscL5xURmpDaEA+XO47/fUHnW4nwW/ZuzvbZt2K/HjYqGowWpHXM/kFEYKH lMwI+H6szATGR3hZzwrFoWaIFqX+YDWjIlHJDUV+WZ4JWhxgMLvkbHApjo3oJKGKTGO2 kmSheiTK9VQVI5Kzp0PTeS9JNW49g802HJlxlUx9UFihAmTqoFNiI8YUgJ6ypkgcL1H2 QRKcAY/Fjw/BNrmnWDaAxf8VW5uR/mBQ1ZxTTzCRqZh7x04rQFguFtfqxmipq5azxvQc imVw== X-Gm-Message-State: AFeK/H2nUHpTmjvCCyL6POxgP98T3MrE+glLketN2tXLjlEW0Ewl1YHJTI9ghXZeP+Hy+O+i X-Received: by 10.55.135.66 with SMTP id j63mr26459555qkd.310.1490107964469; Tue, 21 Mar 2017 07:52:44 -0700 (PDT) Received: from vader.event.rightround.com ([69.46.234.238]) by smtp.gmail.com with ESMTPSA id q11sm14907189qkh.16.2017.03.21.07.52.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Mar 2017 07:52:44 -0700 (PDT) From: Omar Sandoval To: Al Viro , linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , linux-api@vger.kernel.org, kernel-team@fb.com, Xi Wang , Omar Sandoval Subject: [RFC PATCH v2 2/2] Btrfs: add support for linkat() AT_REPLACE Date: Tue, 21 Mar 2017 07:51:48 -0700 Message-Id: X-Mailer: git-send-email 2.12.0 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 | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c40060cc481f..188e8e5fca50 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -6468,16 +6468,21 @@ 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); struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 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; @@ -6485,16 +6490,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(BTRFS_I(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; @@ -6506,6 +6542,22 @@ 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, BTRFS_I(dir), + BTRFS_I(new_inode), + dentry->d_name.name, + dentry->d_name.len); + if (!err && new_inode->i_nlink == 0) + err = btrfs_orphan_add(trans, BTRFS_I(new_inode)); + if (err) { + btrfs_abort_transaction(trans, err); + goto fail; + } + } + ihold(inode); set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); @@ -6528,7 +6580,12 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, if (err) goto fail; } - d_instantiate(dentry, inode); + if (new_inode) { + d_drop(dentry); + iput(inode); + } else { + d_instantiate(dentry, inode); + } btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent); } @@ -10519,7 +10576,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,