From patchwork Thu Mar 15 08:11:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Misono Tomohiro X-Patchwork-Id: 10284017 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 EBD19602C2 for ; Thu, 15 Mar 2018 08:11:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB6DC28901 for ; Thu, 15 Mar 2018 08:11:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AEDB728910; Thu, 15 Mar 2018 08:11:16 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 A1536288F6 for ; Thu, 15 Mar 2018 08:11:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751541AbeCOILN (ORCPT ); Thu, 15 Mar 2018 04:11:13 -0400 Received: from mgwkm03.jp.fujitsu.com ([202.219.69.170]:64491 "EHLO mgwkm03.jp.fujitsu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750722AbeCOILM (ORCPT ); Thu, 15 Mar 2018 04:11:12 -0400 Received: from kw-mxq.gw.nic.fujitsu.com (unknown [192.168.231.130]) by mgwkm03.jp.fujitsu.com with smtp id 1746_2405_17e90ac6_e5da_4f68_a5de_2501a52f8ceb; Thu, 15 Mar 2018 17:11:04 +0900 Received: from g01jpfmpwyt03.exch.g01.fujitsu.local (g01jpfmpwyt03.exch.g01.fujitsu.local [10.128.193.57]) by kw-mxq.gw.nic.fujitsu.com (Postfix) with ESMTP id 94632AC01BD for ; Thu, 15 Mar 2018 17:11:04 +0900 (JST) Received: from G01JPEXCHYT17.g01.fujitsu.local (G01JPEXCHYT17.g01.fujitsu.local [10.128.194.56]) by g01jpfmpwyt03.exch.g01.fujitsu.local (Postfix) with ESMTP id 8D76946E82E for ; Thu, 15 Mar 2018 17:11:03 +0900 (JST) X-SecurityPolicyCheck: OK by SHieldMailChecker v2.5.2 X-SHieldMailCheckerPolicyVersion: FJ-ISEC-20170217-enc X-SHieldMailCheckerMailID: 2b117130f961420c899f2c9baa378de3 Subject: [PATCH v2 2/3] btrfs: Add unprivileged ioctl which returns subvolume's ROOT_REF From: "Misono, Tomohiro" To: linux-btrfs References: Message-ID: Date: Thu, 15 Mar 2018 17:11:01 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-SecurityPolicyCheck-GC: OK by FENCE-Mail X-TM-AS-MML: disable Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add unprivileged ioctl BTRFS_IOC_GET_SUBVOL_ROOTREF which returns ROOT_REF information of the subvolume containing this inode. The min id of root ref's subvolume to be searched is specified by min_id in struct btrfs_ioctl_get_subvol_rootref_args. If there are more root refs than BTRFS_MAX_ROOTREF_BUFFER_NUM, this ioctl sets min_id to the last searched root ref's subvolid + 1 and return -EOVERFLOW. Therefore the caller can just call this ioctl again without changing the argument to continue search. Signed-off-by: Tomohiro Misono --- fs/btrfs/ioctl.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/btrfs.h | 16 ++++++++ 2 files changed, 107 insertions(+) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 852421ac81cd..816a3eb60020 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -2425,6 +2425,95 @@ static noinline int btrfs_ioctl_get_subvol_info(struct file *file, return ret; } +/* Returns ROOT_REF information of the subvolume contining this inode. */ +static noinline int btrfs_ioctl_get_subvol_rootref(struct file *file, + void __user *argp) +{ + struct btrfs_ioctl_get_subvol_rootref_args __user *rootrefs; + struct btrfs_root_ref *rref; + struct btrfs_root *root; + struct btrfs_path *path; + struct btrfs_key key; + + struct extent_buffer *l; + int slot; + + struct inode *inode; + int i, nritems; + int ret; + u64 objectid; + u8 found; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + rootrefs = memdup_user(argp, sizeof(*rootrefs)); + if (!rootrefs) { + btrfs_free_path(path); + return -ENOMEM; + } + + inode = file_inode(file); + root = BTRFS_I(inode)->root->fs_info->tree_root; + objectid = BTRFS_I(inode)->root->root_key.objectid; + + key.objectid = objectid; + key.type = BTRFS_ROOT_REF_KEY; + key.offset = rootrefs->min_id; + found = 0; + while (1) { + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + if (ret < 0) + goto out; + + l = path->nodes[0]; + slot = path->slots[0]; + nritems = btrfs_header_nritems(l); + if (nritems - slot == 0) { + ret = 0; + goto out; + } + + for (i = slot; i < nritems; i++) { + btrfs_item_key_to_cpu(l, &key, i); + if (key.objectid != objectid || + key.type != BTRFS_ROOT_REF_KEY) { + ret = 0; + goto out; + } + + if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { + /* update min_id for next search */ + rootrefs->min_id = key.offset; + ret = -EOVERFLOW; + goto out; + } + + rref = btrfs_item_ptr(l, i, struct btrfs_root_ref); + rootrefs->rootref[found].subvolid = key.offset; + rootrefs->rootref[found].dirid = + btrfs_root_ref_dirid(l, rref); + found++; + } + + btrfs_release_path(path); + key.offset++; + } + +out: + if (!ret || ret == -EOVERFLOW) { + rootrefs->num_items = found; + if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) + ret = -EFAULT; + } + + btrfs_free_path(path); + kfree(rootrefs); + + return ret; +} + static noinline int btrfs_ioctl_snap_destroy(struct file *file, void __user *arg) { @@ -5781,6 +5870,8 @@ long btrfs_ioctl(struct file *file, unsigned int return btrfs_ioctl_set_features(file, argp); case BTRFS_IOC_GET_SUBVOL_INFO: return btrfs_ioctl_get_subvol_info(file, argp); + case BTRFS_IOC_GET_SUBVOL_ROOTREF: + return btrfs_ioctl_get_subvol_rootref(file, argp); } return -ENOTTY; diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h index ed053852c71f..82c88d52d6e6 100644 --- a/include/uapi/linux/btrfs.h +++ b/include/uapi/linux/btrfs.h @@ -774,6 +774,20 @@ struct btrfs_ioctl_get_subvol_info_args { __u64 reserved[8]; }; +#define BTRFS_MAX_ROOTREF_BUFFER_NUM 255 +struct btrfs_ioctl_get_subvol_rootref_args { + /* in/out, min id of rootref's subvolid to be searched */ + __u64 min_id; + /* out */ + struct { + __u64 subvolid; + __u64 dirid; + } rootref[BTRFS_MAX_ROOTREF_BUFFER_NUM]; + /* out, number of found items */ + __u8 num_items; + __u8 align[7]; +}; + /* Error codes as returned by the kernel */ enum btrfs_err_code { BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1, @@ -894,5 +908,7 @@ enum btrfs_err_code { struct btrfs_ioctl_logical_ino_args) #define BTRFS_IOC_GET_SUBVOL_INFO _IOR(BTRFS_IOCTL_MAGIC, 60, \ struct btrfs_ioctl_get_subvol_info_args) +#define BTRFS_IOC_GET_SUBVOL_ROOTREF _IOWR(BTRFS_IOCTL_MAGIC, 61, \ + struct btrfs_ioctl_get_subvol_rootref_args) #endif /* _UAPI_LINUX_BTRFS_H */