^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/blkpg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/hdreg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/blktrace_api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "blk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int blkpg_do_ioctl(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct blkpg_partition __user *upart, int op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct blkpg_partition p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) long long start, length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (copy_from_user(&p, upart, sizeof(struct blkpg_partition)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (bdev_is_partition(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (p.pno <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (op == BLKPG_DEL_PARTITION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return bdev_del_partition(bdev, p.pno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) start = p.start >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) length = p.length >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* check for fit in a hd_struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (sizeof(sector_t) < sizeof(long long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) long pstart = start, plength = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (pstart != start || plength != length || pstart < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) plength < 0 || p.pno > 65535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) switch (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) case BLKPG_ADD_PARTITION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* check if partition is aligned to blocksize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (p.start & (bdev_logical_block_size(bdev) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return bdev_add_partition(bdev, p.pno, start, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) case BLKPG_RESIZE_PARTITION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return bdev_resize_partition(bdev, p.pno, start, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int blkpg_ioctl(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct blkpg_ioctl_arg __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct blkpg_partition __user *udata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (get_user(op, &arg->op) || get_user(udata, &arg->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return blkpg_do_ioctl(bdev, udata, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct compat_blkpg_ioctl_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) compat_int_t op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) compat_int_t flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) compat_int_t datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) compat_caddr_t data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int compat_blkpg_ioctl(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct compat_blkpg_ioctl_arg __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) compat_caddr_t udata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (get_user(op, &arg->op) || get_user(udata, &arg->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return blkpg_do_ioctl(bdev, compat_ptr(udata), op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct block_device *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (bdev->bd_part_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Reopen the device to revalidate the driver state and force a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * partition rescan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mode &= ~FMODE_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (IS_ERR(tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return PTR_ERR(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) blkdev_put(tmp, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned long arg, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) uint64_t range[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) uint64_t start, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct request_queue *q = bdev_get_queue(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!(mode & FMODE_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!blk_queue_discard(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (copy_from_user(range, (void __user *)arg, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) start = range[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) len = range[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (start & 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (len & 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (start + len > i_size_read(bdev->bd_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err = truncate_bdev_range(bdev, mode, start, start + len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return blkdev_issue_discard(bdev, start >> 9, len >> 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) GFP_KERNEL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) uint64_t range[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) uint64_t start, end, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!(mode & FMODE_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (copy_from_user(range, (void __user *)arg, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) start = range[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) len = range[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (start & 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (len & 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (end >= (uint64_t)i_size_read(bdev->bd_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (end < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Invalidate the page cache, including dirty pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = truncate_bdev_range(bdev, mode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) BLKDEV_ZERO_NOUNMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int put_ushort(unsigned short __user *argp, unsigned short val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int put_int(int __user *argp, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int put_uint(unsigned int __user *argp, unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int put_long(long __user *argp, long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int put_ulong(unsigned long __user *argp, unsigned long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int put_u64(u64 __user *argp, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int compat_put_long(compat_long_t __user *argp, long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int compat_put_ulong(compat_ulong_t __user *argp, compat_ulong_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return put_user(val, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) unsigned cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (disk->fops->ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return disk->fops->ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * For the record: _GPL here is only because somebody decided to slap it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * on the previous export. Sheer idiocy, since it wasn't copyrightable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * at all and could be open-coded without any exports by anybody who cares.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * This is the equivalent of compat_ptr_ioctl(), to be used by block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * drivers that implement only commands that are completely compatible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * between 32-bit and 64-bit user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int blkdev_compat_ptr_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (disk->fops->ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return disk->fops->ioctl(bdev, mode, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) EXPORT_SYMBOL(blkdev_compat_ptr_ioctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int blkdev_pr_register(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct pr_registration __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct pr_registration reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!ops || !ops->pr_register)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (copy_from_user(®, arg, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (reg.flags & ~PR_FL_IGNORE_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return ops->pr_register(bdev, reg.old_key, reg.new_key, reg.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int blkdev_pr_reserve(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct pr_reservation __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct pr_reservation rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!ops || !ops->pr_reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (copy_from_user(&rsv, arg, sizeof(rsv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (rsv.flags & ~PR_FL_IGNORE_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return ops->pr_reserve(bdev, rsv.key, rsv.type, rsv.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int blkdev_pr_release(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct pr_reservation __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct pr_reservation rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!ops || !ops->pr_release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (copy_from_user(&rsv, arg, sizeof(rsv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (rsv.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return ops->pr_release(bdev, rsv.key, rsv.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int blkdev_pr_preempt(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) struct pr_preempt __user *arg, bool abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct pr_preempt p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (!ops || !ops->pr_preempt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (copy_from_user(&p, arg, sizeof(p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (p.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return ops->pr_preempt(bdev, p.old_key, p.new_key, p.type, abort);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int blkdev_pr_clear(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct pr_clear __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct pr_clear c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!ops || !ops->pr_clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (copy_from_user(&c, arg, sizeof(c)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (c.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return ops->pr_clear(bdev, c.key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Is it an unrecognized ioctl? The correct returns are either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * code before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * Confused drivers sometimes return EINVAL, which is wrong. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * means "I understood the ioctl command, but the parameters to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * it were wrong".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * We should aim to just fix the broken drivers, the EINVAL case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * should go away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static inline int is_unrecognized_ioctl(int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return ret == -EINVAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ret == -ENOTTY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret == -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int blkdev_flushbuf(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) unsigned cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!is_unrecognized_ioctl(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) fsync_bdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) invalidate_bdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static int blkdev_roset(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) unsigned cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int ret, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ret = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (!is_unrecognized_ioctl(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (get_user(n, (int __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) set_device_ro(bdev, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int blkdev_getgeo(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct hd_geometry __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct hd_geometry geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (!disk->fops->getgeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * We need to set the startsect first, the driver may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * want to override it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) memset(&geo, 0, sizeof(geo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) geo.start = get_start_sect(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) ret = disk->fops->getgeo(bdev, &geo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (copy_to_user(argp, &geo, sizeof(geo)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct compat_hd_geometry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) unsigned char heads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) unsigned char sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) unsigned short cylinders;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) u32 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int compat_hdio_getgeo(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct compat_hd_geometry __user *ugeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct hd_geometry geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (!ugeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (!disk->fops->getgeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) memset(&geo, 0, sizeof(geo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * We need to set the startsect first, the driver may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * want to override it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) geo.start = get_start_sect(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ret = disk->fops->getgeo(bdev, &geo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ret = copy_to_user(ugeo, &geo, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret |= put_user(geo.start, &ugeo->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* set the logical block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int blkdev_bszset(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int ret, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (get_user(n, argp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (mode & FMODE_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return set_blocksize(bdev, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (IS_ERR(blkdev_get_by_dev(bdev->bd_dev, mode | FMODE_EXCL, &bdev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) ret = set_blocksize(bdev, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) blkdev_put(bdev, mode | FMODE_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * Common commands that are handled the same way on native and compat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * user space. Note the separate arg/argp parameters that are needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * to deal with the compat_ptr() conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) unsigned cmd, unsigned long arg, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) unsigned int max_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) case BLKFLSBUF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return blkdev_flushbuf(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) case BLKROSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return blkdev_roset(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) case BLKDISCARD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return blk_ioctl_discard(bdev, mode, arg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) case BLKSECDISCARD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return blk_ioctl_discard(bdev, mode, arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) BLKDEV_DISCARD_SECURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) case BLKZEROOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return blk_ioctl_zeroout(bdev, mode, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) case BLKREPORTZONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return blkdev_report_zones_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) case BLKRESETZONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) case BLKOPENZONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) case BLKCLOSEZONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) case BLKFINISHZONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return blkdev_zone_mgmt_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) case BLKGETZONESZ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return put_uint(argp, bdev_zone_sectors(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) case BLKGETNRZONES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return put_uint(argp, blkdev_nr_zones(bdev->bd_disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) case BLKROGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return put_int(argp, bdev_read_only(bdev) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) case BLKSSZGET: /* get block device logical block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return put_int(argp, bdev_logical_block_size(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) case BLKPBSZGET: /* get block device physical block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return put_uint(argp, bdev_physical_block_size(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) case BLKIOMIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return put_uint(argp, bdev_io_min(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) case BLKIOOPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return put_uint(argp, bdev_io_opt(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) case BLKALIGNOFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) return put_int(argp, bdev_alignment_offset(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) case BLKDISCARDZEROES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return put_uint(argp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) case BLKSECTGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) max_sectors = min_t(unsigned int, USHRT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) queue_max_sectors(bdev_get_queue(bdev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return put_ushort(argp, max_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) case BLKROTATIONAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return put_ushort(argp, !blk_queue_nonrot(bdev_get_queue(bdev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) case BLKRASET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) case BLKFRASET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if(!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) case BLKRRPART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return blkdev_reread_part(bdev, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) case BLKTRACESTART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) case BLKTRACESTOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) case BLKTRACETEARDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return blk_trace_ioctl(bdev, cmd, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) case IOC_PR_REGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return blkdev_pr_register(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) case IOC_PR_RESERVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return blkdev_pr_reserve(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) case IOC_PR_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return blkdev_pr_release(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) case IOC_PR_PREEMPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return blkdev_pr_preempt(bdev, argp, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) case IOC_PR_PREEMPT_ABORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return blkdev_pr_preempt(bdev, argp, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) case IOC_PR_CLEAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return blkdev_pr_clear(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * Always keep this in sync with compat_blkdev_ioctl()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * to handle all incompatible commands in both functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * New commands must be compatible and go into blkdev_common_ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /* These need separate implementations for the data structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) case HDIO_GETGEO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return blkdev_getgeo(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) case BLKPG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) return blkpg_ioctl(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /* Compat mode returns 32-bit data instead of 'long' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) case BLKRAGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) case BLKFRAGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (!argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return put_long(argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE) / 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) case BLKGETSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) size = i_size_read(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if ((size >> 9) > ~0UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return put_ulong(argp, size >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /* The data is compatible, but the command number is different */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return put_int(argp, block_size(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) case BLKBSZSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return blkdev_bszset(bdev, mode, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) case BLKGETSIZE64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return put_u64(argp, i_size_read(bdev->bd_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /* Incompatible alignment on i386 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) case BLKTRACESETUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return blk_trace_ioctl(bdev, cmd, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (ret == -ENOIOCTLCMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) EXPORT_SYMBOL_GPL(blkdev_ioctl); /* for /dev/raw */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) #define BLKBSZGET_32 _IOR(0x12, 112, int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) #define BLKBSZSET_32 _IOW(0x12, 113, int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /* Most of the generic ioctls are handled in the normal fallback path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) This assumes the blkdev's low level compat_ioctl always returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) ENOIOCTLCMD for unknown ioctls. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) void __user *argp = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct block_device *bdev = inode->i_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) fmode_t mode = file->f_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * to updated it before every ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (file->f_flags & O_NDELAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) mode |= FMODE_NDELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) mode &= ~FMODE_NDELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /* These need separate implementations for the data structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) case HDIO_GETGEO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return compat_hdio_getgeo(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) case BLKPG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return compat_blkpg_ioctl(bdev, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /* Compat mode returns 32-bit data instead of 'long' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) case BLKRAGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) case BLKFRAGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (!argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return compat_put_long(argp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) case BLKGETSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) size = i_size_read(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if ((size >> 9) > ~0UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return compat_put_ulong(argp, size >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /* The data is compatible, but the command number is different */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return put_int(argp, bdev_logical_block_size(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) case BLKBSZSET_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return blkdev_bszset(bdev, mode, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) case BLKGETSIZE64_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) return put_u64(argp, i_size_read(bdev->bd_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /* Incompatible alignment on i386 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) case BLKTRACESETUP32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return blk_trace_ioctl(bdev, cmd, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) ret = blkdev_common_ioctl(bdev, mode, cmd, arg, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) #endif