[v2,1/3] btrfs: Add unprivileged ioctl which returns subvolume information
diff mbox

Message ID e786ad44-e2d9-d130-7c89-4173e10a007b@jp.fujitsu.com
State New
Headers show

Commit Message

Misono Tomohiro March 15, 2018, 8:10 a.m. UTC
Add new unprivileged ioctl BTRFS_IOC_GET_SUBVOL_INFO which returns
the information of subvolume containing this inode.
(i.e. returns the information in ROOT_ITEM and ROOT_BACKREF.)

Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com>
---
 fs/btrfs/ioctl.c           | 118 +++++++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/btrfs.h |  51 ++++++++++++++++++++
 2 files changed, 169 insertions(+)

Comments

kernel test robot March 18, 2018, 4:16 a.m. UTC | #1
Hi Tomohiro,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v4.16-rc5 next-20180316]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Misono-Tomohiro/btrfs-Add-three-new-unprivileged-ioctls-to-allow-normal-users-to-call-sub-list-show-etc/20180318-101013
reproduce:
        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

   fs/btrfs/ioctl.c:1561:9: sparse: incompatible types in comparison expression (different address spaces)
>> fs/btrfs/ioctl.c:2336:21: sparse: incorrect type in assignment (different address spaces) @@    expected struct btrfs_ioctl_get_subvol_info_args [noderef] <asn:1>*subvol_info @@    got sn:1>*subvol_info @@
   fs/btrfs/ioctl.c:2336:21:    expected struct btrfs_ioctl_get_subvol_info_args [noderef] <asn:1>*subvol_info
   fs/btrfs/ioctl.c:2336:21:    got void *
>> fs/btrfs/ioctl.c:2373:9: sparse: incorrect type in argument 1 (different address spaces) @@    expected void *to @@    got unsigned char [noderef] <avoid *to @@
   fs/btrfs/ioctl.c:2373:9:    expected void *to
   fs/btrfs/ioctl.c:2373:9:    got unsigned char [noderef] <asn:1>*<noident>
   fs/btrfs/ioctl.c:2374:9: sparse: incorrect type in argument 1 (different address spaces) @@    expected void *to @@    got unsigned char [noderef] <avoid *to @@
   fs/btrfs/ioctl.c:2374:9:    expected void *to
   fs/btrfs/ioctl.c:2374:9:    got unsigned char [noderef] <asn:1>*<noident>
   fs/btrfs/ioctl.c:2376:9: sparse: incorrect type in argument 1 (different address spaces) @@    expected void *to @@    got unsigned char [noderef] <avoid *to @@
   fs/btrfs/ioctl.c:2376:9:    expected void *to
   fs/btrfs/ioctl.c:2376:9:    got unsigned char [noderef] <asn:1>*<noident>
>> fs/btrfs/ioctl.c:2416:39: sparse: incorrect type in argument 2 (different address spaces) @@    expected void *dst @@    got char [noderef] <avoid *dst @@
   fs/btrfs/ioctl.c:2416:39:    expected void *dst
   fs/btrfs/ioctl.c:2416:39:    got char [noderef] <asn:1>*<noident>
>> fs/btrfs/ioctl.c:2419:32: sparse: incorrect type in argument 2 (different address spaces) @@    expected void const *from @@    got struct btrfs_ioctl_get_subvol_info_args [nodervoid const *from @@
   fs/btrfs/ioctl.c:2419:32:    expected void const *from
   fs/btrfs/ioctl.c:2419:32:    got struct btrfs_ioctl_get_subvol_info_args [noderef] <asn:1>*subvol_info
>> fs/btrfs/ioctl.c:2423:16: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const *<noident> @@    got struct btrfs_ioctl_get_subvol_info_args [nodervoid const *<noident> @@
   fs/btrfs/ioctl.c:2423:16:    expected void const *<noident>
   fs/btrfs/ioctl.c:2423:16:    got struct btrfs_ioctl_get_subvol_info_args [noderef] <asn:1>*subvol_info
   fs/btrfs/ioctl.c:2917:24: sparse: incompatible types in comparison expression (different address spaces)
>> fs/btrfs/ioctl.c:2368:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2370:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2371:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2379:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2380:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2381:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2383:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2384:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2385:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2387:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2388:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2389:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2391:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2392:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2393:9: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2405:29: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2407:17: sparse: dereference of noderef expression
   fs/btrfs/ioctl.c:2410:17: sparse: dereference of noderef expression

vim +2336 fs/btrfs/ioctl.c

  2311	
  2312	/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
  2313	static noinline int btrfs_ioctl_get_subvol_info(struct file *file,
  2314						   void __user *argp)
  2315	{
  2316		struct btrfs_ioctl_get_subvol_info_args __user *subvol_info;
  2317		struct btrfs_root *root;
  2318		struct btrfs_path *path;
  2319		struct btrfs_key key;
  2320	
  2321		struct btrfs_root_item root_item;
  2322		struct btrfs_root_ref *rref;
  2323		struct extent_buffer *l;
  2324		int slot;
  2325	
  2326		unsigned long item_off;
  2327		unsigned long item_len;
  2328	
  2329		struct inode *inode;
  2330		int ret;
  2331	
  2332		path = btrfs_alloc_path();
  2333		if (!path)
  2334			return -ENOMEM;
  2335	
> 2336		subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
  2337		if (!subvol_info) {
  2338			btrfs_free_path(path);
  2339			return -ENOMEM;
  2340		}
  2341		inode = file_inode(file);
  2342	
  2343		root = BTRFS_I(inode)->root->fs_info->tree_root;
  2344		key.objectid = BTRFS_I(inode)->root->root_key.objectid;
  2345		key.type = BTRFS_ROOT_ITEM_KEY;
  2346		key.offset = 0;
  2347		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2348		if (ret < 0) {
  2349			goto out;
  2350		} else if (ret > 0) {
  2351			/* If the subvolume is a snapshot, offset is not zero */
  2352			u64 objectid = key.objectid;
  2353	
  2354			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  2355			if (key.objectid != objectid ||
  2356			    key.type != BTRFS_ROOT_ITEM_KEY) {
  2357				ret = -ENOENT;
  2358				goto out;
  2359			}
  2360		}
  2361	
  2362		l = path->nodes[0];
  2363		slot = path->slots[0];
  2364		item_off = btrfs_item_ptr_offset(l, slot);
  2365		item_len = btrfs_item_size_nr(l, slot);
  2366		read_extent_buffer(l, &root_item, item_off, item_len);
  2367	
> 2368		subvol_info->id = key.objectid;
  2369	
  2370		subvol_info->generation = btrfs_root_generation(&root_item);
  2371		subvol_info->flags = btrfs_root_flags(&root_item);
  2372	
> 2373		memcpy(subvol_info->uuid, root_item.uuid, BTRFS_UUID_SIZE);
> 2374		memcpy(subvol_info->parent_uuid, root_item.parent_uuid,
  2375							    BTRFS_UUID_SIZE);
> 2376		memcpy(subvol_info->received_uuid, root_item.received_uuid,
  2377							    BTRFS_UUID_SIZE);
  2378	
  2379		subvol_info->ctransid = btrfs_root_ctransid(&root_item);
  2380		subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item.ctime);
  2381		subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item.ctime);
  2382	
  2383		subvol_info->otransid = btrfs_root_otransid(&root_item);
  2384		subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item.otime);
  2385		subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item.otime);
  2386	
  2387		subvol_info->stransid = btrfs_root_stransid(&root_item);
  2388		subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item.stime);
  2389		subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item.stime);
  2390	
  2391		subvol_info->rtransid = btrfs_root_rtransid(&root_item);
  2392		subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item.rtime);
  2393		subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item.rtime);
  2394	
  2395		btrfs_release_path(path);
  2396		key.type = BTRFS_ROOT_BACKREF_KEY;
  2397		key.offset = 0;
  2398		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2399		if (ret < 0)
  2400			goto out;
  2401	
  2402		l = path->nodes[0];
  2403		slot = path->slots[0];
  2404		btrfs_item_key_to_cpu(l, &key, slot);
  2405		if (key.objectid == subvol_info->id &&
  2406				key.type == BTRFS_ROOT_BACKREF_KEY){
  2407			subvol_info->parent_id = key.offset;
  2408	
  2409			rref = btrfs_item_ptr(l, slot, struct btrfs_root_ref);
  2410			subvol_info->dirid = btrfs_root_ref_dirid(l, rref);
  2411	
  2412			item_off = btrfs_item_ptr_offset(l, slot)
  2413					+ sizeof(struct btrfs_root_ref);
  2414			item_len = btrfs_item_size_nr(l, slot)
  2415					- sizeof(struct btrfs_root_ref);
> 2416			read_extent_buffer(l, subvol_info->name, item_off, item_len);
  2417		}
  2418	
> 2419		if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
  2420			ret = -EFAULT;
  2421	
  2422	out:
> 2423		kzfree(subvol_info);
  2424		btrfs_free_path(path);
  2425		return ret;
  2426	}
  2427	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch
diff mbox

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 111ee282b777..852421ac81cd 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2309,6 +2309,122 @@  static noinline int btrfs_ioctl_ino_lookup(struct file *file,
 	return ret;
 }
 
+/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
+static noinline int btrfs_ioctl_get_subvol_info(struct file *file,
+					   void __user *argp)
+{
+	struct btrfs_ioctl_get_subvol_info_args __user *subvol_info;
+	struct btrfs_root *root;
+	struct btrfs_path *path;
+	struct btrfs_key key;
+
+	struct btrfs_root_item root_item;
+	struct btrfs_root_ref *rref;
+	struct extent_buffer *l;
+	int slot;
+
+	unsigned long item_off;
+	unsigned long item_len;
+
+	struct inode *inode;
+	int ret;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
+	if (!subvol_info) {
+		btrfs_free_path(path);
+		return -ENOMEM;
+	}
+	inode = file_inode(file);
+
+	root = BTRFS_I(inode)->root->fs_info->tree_root;
+	key.objectid = BTRFS_I(inode)->root->root_key.objectid;
+	key.type = BTRFS_ROOT_ITEM_KEY;
+	key.offset = 0;
+	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	if (ret < 0) {
+		goto out;
+	} else if (ret > 0) {
+		/* If the subvolume is a snapshot, offset is not zero */
+		u64 objectid = key.objectid;
+
+		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+		if (key.objectid != objectid ||
+		    key.type != BTRFS_ROOT_ITEM_KEY) {
+			ret = -ENOENT;
+			goto out;
+		}
+	}
+
+	l = path->nodes[0];
+	slot = path->slots[0];
+	item_off = btrfs_item_ptr_offset(l, slot);
+	item_len = btrfs_item_size_nr(l, slot);
+	read_extent_buffer(l, &root_item, item_off, item_len);
+
+	subvol_info->id = key.objectid;
+
+	subvol_info->generation = btrfs_root_generation(&root_item);
+	subvol_info->flags = btrfs_root_flags(&root_item);
+
+	memcpy(subvol_info->uuid, root_item.uuid, BTRFS_UUID_SIZE);
+	memcpy(subvol_info->parent_uuid, root_item.parent_uuid,
+						    BTRFS_UUID_SIZE);
+	memcpy(subvol_info->received_uuid, root_item.received_uuid,
+						    BTRFS_UUID_SIZE);
+
+	subvol_info->ctransid = btrfs_root_ctransid(&root_item);
+	subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item.ctime);
+	subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item.ctime);
+
+	subvol_info->otransid = btrfs_root_otransid(&root_item);
+	subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item.otime);
+	subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item.otime);
+
+	subvol_info->stransid = btrfs_root_stransid(&root_item);
+	subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item.stime);
+	subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item.stime);
+
+	subvol_info->rtransid = btrfs_root_rtransid(&root_item);
+	subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item.rtime);
+	subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item.rtime);
+
+	btrfs_release_path(path);
+	key.type = BTRFS_ROOT_BACKREF_KEY;
+	key.offset = 0;
+	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	if (ret < 0)
+		goto out;
+
+	l = path->nodes[0];
+	slot = path->slots[0];
+	btrfs_item_key_to_cpu(l, &key, slot);
+	if (key.objectid == subvol_info->id &&
+			key.type == BTRFS_ROOT_BACKREF_KEY){
+		subvol_info->parent_id = key.offset;
+
+		rref = btrfs_item_ptr(l, slot, struct btrfs_root_ref);
+		subvol_info->dirid = btrfs_root_ref_dirid(l, rref);
+
+		item_off = btrfs_item_ptr_offset(l, slot)
+				+ sizeof(struct btrfs_root_ref);
+		item_len = btrfs_item_size_nr(l, slot)
+				- sizeof(struct btrfs_root_ref);
+		read_extent_buffer(l, subvol_info->name, item_off, item_len);
+	}
+
+	if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
+		ret = -EFAULT;
+
+out:
+	kzfree(subvol_info);
+	btrfs_free_path(path);
+	return ret;
+}
+
 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
 					     void __user *arg)
 {
@@ -5663,6 +5779,8 @@  long btrfs_ioctl(struct file *file, unsigned int
 		return btrfs_ioctl_get_features(file, argp);
 	case BTRFS_IOC_SET_FEATURES:
 		return btrfs_ioctl_set_features(file, argp);
+	case BTRFS_IOC_GET_SUBVOL_INFO:
+		return btrfs_ioctl_get_subvol_info(file, argp);
 	}
 
 	return -ENOTTY;
diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h
index c8d99b9ca550..ed053852c71f 100644
--- a/include/uapi/linux/btrfs.h
+++ b/include/uapi/linux/btrfs.h
@@ -725,6 +725,55 @@  struct btrfs_ioctl_send_args {
 	__u64 reserved[4];		/* in */
 };
 
+struct btrfs_ioctl_get_subvol_info_args {
+	/* All filed is out */
+	/* Id of this subvolume */
+	__u64 id;
+	/* Name of this subvolume, used to get the real name at mount point */
+	char name[BTRFS_VOL_NAME_MAX + 1];
+	/*
+	 * Id of the subvolume which contains this subvolume.
+	 * Zero for top-level subvolume or deleted subvolume
+	 */
+	__u64 parent_id;
+	/*
+	 * Inode number of the directory which contains this subvolume.
+	 * Zero for top-level subvolume or deleted subvolume
+	 */
+	__u64 dirid;
+
+	/* Latest transaction id of this subvolume */
+	__u64 generation;
+	/* Flags of this subvolume */
+	__u64 flags;
+
+	/* uuid of this subvolume */
+	__u8 uuid[BTRFS_UUID_SIZE];
+	/*
+	 * uuid of the subvolume of which this subvolume is a snapshot.
+	 * All zero for non-snapshot subvolume
+	 */
+	__u8 parent_uuid[BTRFS_UUID_SIZE];
+	/*
+	 * uuid of the subvolume from which this subvolume is received.
+	 * All zero for non-received subvolume
+	 */
+	__u8 received_uuid[BTRFS_UUID_SIZE];
+
+	/* Transaction id indicates when change/create/send/receive happens */
+	__u64 ctransid;
+	__u64 otransid;
+	__u64 stransid;
+	__u64 rtransid;
+	/* Time corresponds to c/o/s/rtransid */
+	struct btrfs_ioctl_timespec ctime;
+	struct btrfs_ioctl_timespec otime;
+	struct btrfs_ioctl_timespec stime;
+	struct btrfs_ioctl_timespec rtime;
+
+	__u64 reserved[8];
+};
+
 /* Error codes as returned by the kernel */
 enum btrfs_err_code {
 	BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET = 1,
@@ -843,5 +892,7 @@  enum btrfs_err_code {
 				   struct btrfs_ioctl_vol_args_v2)
 #define BTRFS_IOC_LOGICAL_INO_V2 _IOWR(BTRFS_IOCTL_MAGIC, 59, \
 					struct btrfs_ioctl_logical_ino_args)
+#define BTRFS_IOC_GET_SUBVOL_INFO _IOR(BTRFS_IOCTL_MAGIC, 60, \
+				struct btrfs_ioctl_get_subvol_info_args)
 
 #endif /* _UAPI_LINUX_BTRFS_H */