[kdave-btrfs-devel:for-next-20220401 96/102] fs/btrfs/sysfs.c:830 btrfs_force_chunk_alloc_store() warn: passing zero to 'PTR_ERR'

From: Dan Carpenter
Date: Mon Apr 04 2022 - 09:40:53 EST


tree: https://github.com/kdave/btrfs-devel.git for-next-20220401
head: 0c593f04d2f520a720c5c618ba9fe0355d08bba8
commit: 6e115adef2b6a9b9a7cc25414e1b8f4414e02044 [96/102] btrfs: add force_chunk_alloc sysfs entry to force allocation
config: nios2-randconfig-m031-20220403 (https://download.01.org/0day-ci/archive/20220403/202204030841.qWK5rMDU-lkp@xxxxxxxxx/config)
compiler: nios2-linux-gcc (GCC) 11.2.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>

New smatch warnings:
fs/btrfs/sysfs.c:830 btrfs_force_chunk_alloc_store() warn: passing zero to 'PTR_ERR'

vim +/PTR_ERR +830 fs/btrfs/sysfs.c

6e115adef2b6a9 Stefan Roesch 2021-12-03 797 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
6e115adef2b6a9 Stefan Roesch 2021-12-03 798 struct kobj_attribute *a,
6e115adef2b6a9 Stefan Roesch 2021-12-03 799 const char *buf, size_t len)
6e115adef2b6a9 Stefan Roesch 2021-12-03 800 {
6e115adef2b6a9 Stefan Roesch 2021-12-03 801 struct btrfs_space_info *space_info = to_space_info(kobj);
6e115adef2b6a9 Stefan Roesch 2021-12-03 802 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
6e115adef2b6a9 Stefan Roesch 2021-12-03 803 struct btrfs_trans_handle *trans;
6e115adef2b6a9 Stefan Roesch 2021-12-03 804 unsigned long val;
6e115adef2b6a9 Stefan Roesch 2021-12-03 805 int ret;
6e115adef2b6a9 Stefan Roesch 2021-12-03 806
6e115adef2b6a9 Stefan Roesch 2021-12-03 807 if (!fs_info) {
6e115adef2b6a9 Stefan Roesch 2021-12-03 808 pr_err("couldn't get fs_info\n");
6e115adef2b6a9 Stefan Roesch 2021-12-03 809 return -EPERM;
6e115adef2b6a9 Stefan Roesch 2021-12-03 810 }
6e115adef2b6a9 Stefan Roesch 2021-12-03 811
6e115adef2b6a9 Stefan Roesch 2021-12-03 812 if (!capable(CAP_SYS_ADMIN))
6e115adef2b6a9 Stefan Roesch 2021-12-03 813 return -EPERM;
6e115adef2b6a9 Stefan Roesch 2021-12-03 814
6e115adef2b6a9 Stefan Roesch 2021-12-03 815 if (sb_rdonly(fs_info->sb))
6e115adef2b6a9 Stefan Roesch 2021-12-03 816 return -EROFS;
6e115adef2b6a9 Stefan Roesch 2021-12-03 817
6e115adef2b6a9 Stefan Roesch 2021-12-03 818 ret = kstrtoul(buf, 10, &val);
6e115adef2b6a9 Stefan Roesch 2021-12-03 819 if (ret)
6e115adef2b6a9 Stefan Roesch 2021-12-03 820 return ret;
6e115adef2b6a9 Stefan Roesch 2021-12-03 821
6e115adef2b6a9 Stefan Roesch 2021-12-03 822 if (val == 0)
6e115adef2b6a9 Stefan Roesch 2021-12-03 823 return -EINVAL;
6e115adef2b6a9 Stefan Roesch 2021-12-03 824
6e115adef2b6a9 Stefan Roesch 2021-12-03 825 /*
6e115adef2b6a9 Stefan Roesch 2021-12-03 826 * Allocate new chunk.
6e115adef2b6a9 Stefan Roesch 2021-12-03 827 */
6e115adef2b6a9 Stefan Roesch 2021-12-03 828 trans = btrfs_start_transaction(fs_info->tree_root, 0);
6e115adef2b6a9 Stefan Roesch 2021-12-03 829 if (!trans)
6e115adef2b6a9 Stefan Roesch 2021-12-03 @830 return PTR_ERR(trans);

PTR(NULL) is success.

6e115adef2b6a9 Stefan Roesch 2021-12-03 831 ret = btrfs_force_chunk_alloc(trans, space_info->flags);
6e115adef2b6a9 Stefan Roesch 2021-12-03 832 btrfs_end_transaction(trans);
6e115adef2b6a9 Stefan Roesch 2021-12-03 833
6e115adef2b6a9 Stefan Roesch 2021-12-03 834 if (ret == 1)
6e115adef2b6a9 Stefan Roesch 2021-12-03 835 return len;
6e115adef2b6a9 Stefan Roesch 2021-12-03 836
6e115adef2b6a9 Stefan Roesch 2021-12-03 837 return -ENOSPC;
6e115adef2b6a9 Stefan Roesch 2021-12-03 838 }

--
0-DAY CI Kernel Test Service
https://01.org/lkp