~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/block/bsg.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /block/bsg.c (Version linux-6.12-rc7) and /block/bsg.c (Version linux-5.15.171)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * bsg.c - block layer implementation of the s      3  * bsg.c - block layer implementation of the sg v4 interface
  4  */                                                 4  */
  5 #include <linux/module.h>                           5 #include <linux/module.h>
  6 #include <linux/init.h>                             6 #include <linux/init.h>
  7 #include <linux/file.h>                             7 #include <linux/file.h>
  8 #include <linux/blkdev.h>                           8 #include <linux/blkdev.h>
  9 #include <linux/cdev.h>                             9 #include <linux/cdev.h>
 10 #include <linux/jiffies.h>                         10 #include <linux/jiffies.h>
 11 #include <linux/percpu.h>                          11 #include <linux/percpu.h>
 12 #include <linux/idr.h>                             12 #include <linux/idr.h>
 13 #include <linux/bsg.h>                             13 #include <linux/bsg.h>
 14 #include <linux/slab.h>                            14 #include <linux/slab.h>
 15                                                    15 
 16 #include <scsi/scsi.h>                             16 #include <scsi/scsi.h>
 17 #include <scsi/scsi_ioctl.h>                       17 #include <scsi/scsi_ioctl.h>
 18 #include <scsi/sg.h>                               18 #include <scsi/sg.h>
 19                                                    19 
 20 #define BSG_DESCRIPTION "Block layer SCSI gene     20 #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
 21 #define BSG_VERSION     "0.4"                      21 #define BSG_VERSION     "0.4"
 22                                                    22 
 23 struct bsg_device {                                23 struct bsg_device {
 24         struct request_queue *queue;               24         struct request_queue *queue;
 25         struct device device;                      25         struct device device;
 26         struct cdev cdev;                          26         struct cdev cdev;
 27         int max_queue;                             27         int max_queue;
 28         unsigned int timeout;                      28         unsigned int timeout;
 29         unsigned int reserved_size;                29         unsigned int reserved_size;
 30         bsg_sg_io_fn *sg_io_fn;                    30         bsg_sg_io_fn *sg_io_fn;
 31 };                                                 31 };
 32                                                    32 
 33 static inline struct bsg_device *to_bsg_device     33 static inline struct bsg_device *to_bsg_device(struct inode *inode)
 34 {                                                  34 {
 35         return container_of(inode->i_cdev, str     35         return container_of(inode->i_cdev, struct bsg_device, cdev);
 36 }                                                  36 }
 37                                                    37 
 38 #define BSG_DEFAULT_CMDS        64                 38 #define BSG_DEFAULT_CMDS        64
 39 #define BSG_MAX_DEVS            (1 << MINORBIT !!  39 #define BSG_MAX_DEVS            32768
 40                                                    40 
 41 static DEFINE_IDA(bsg_minor_ida);                  41 static DEFINE_IDA(bsg_minor_ida);
 42 static const struct class bsg_class;           !!  42 static struct class *bsg_class;
 43 static int bsg_major;                              43 static int bsg_major;
 44                                                    44 
 45 static unsigned int bsg_timeout(struct bsg_dev     45 static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
 46 {                                                  46 {
 47         unsigned int timeout = BLK_DEFAULT_SG_     47         unsigned int timeout = BLK_DEFAULT_SG_TIMEOUT;
 48                                                    48 
 49         if (hdr->timeout)                          49         if (hdr->timeout)
 50                 timeout = msecs_to_jiffies(hdr     50                 timeout = msecs_to_jiffies(hdr->timeout);
 51         else if (bd->timeout)                      51         else if (bd->timeout)
 52                 timeout = bd->timeout;             52                 timeout = bd->timeout;
 53                                                    53 
 54         return max_t(unsigned int, timeout, BL     54         return max_t(unsigned int, timeout, BLK_MIN_SG_TIMEOUT);
 55 }                                                  55 }
 56                                                    56 
 57 static int bsg_sg_io(struct bsg_device *bd, bo !!  57 static int bsg_sg_io(struct bsg_device *bd, fmode_t mode, void __user *uarg)
 58                      void __user *uarg)        << 
 59 {                                                  58 {
 60         struct sg_io_v4 hdr;                       59         struct sg_io_v4 hdr;
 61         int ret;                                   60         int ret;
 62                                                    61 
 63         if (copy_from_user(&hdr, uarg, sizeof(     62         if (copy_from_user(&hdr, uarg, sizeof(hdr)))
 64                 return -EFAULT;                    63                 return -EFAULT;
 65         if (hdr.guard != 'Q')                      64         if (hdr.guard != 'Q')
 66                 return -EINVAL;                    65                 return -EINVAL;
 67         ret = bd->sg_io_fn(bd->queue, &hdr, op !!  66         ret = bd->sg_io_fn(bd->queue, &hdr, mode, bsg_timeout(bd, &hdr));
 68                            bsg_timeout(bd, &hd << 
 69         if (!ret && copy_to_user(uarg, &hdr, s     67         if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
 70                 return -EFAULT;                    68                 return -EFAULT;
 71         return ret;                                69         return ret;
 72 }                                                  70 }
 73                                                    71 
 74 static int bsg_open(struct inode *inode, struc     72 static int bsg_open(struct inode *inode, struct file *file)
 75 {                                                  73 {
 76         if (!blk_get_queue(to_bsg_device(inode     74         if (!blk_get_queue(to_bsg_device(inode)->queue))
 77                 return -ENXIO;                     75                 return -ENXIO;
 78         return 0;                                  76         return 0;
 79 }                                                  77 }
 80                                                    78 
 81 static int bsg_release(struct inode *inode, st     79 static int bsg_release(struct inode *inode, struct file *file)
 82 {                                                  80 {
 83         blk_put_queue(to_bsg_device(inode)->qu     81         blk_put_queue(to_bsg_device(inode)->queue);
 84         return 0;                                  82         return 0;
 85 }                                                  83 }
 86                                                    84 
 87 static int bsg_get_command_q(struct bsg_device     85 static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
 88 {                                                  86 {
 89         return put_user(READ_ONCE(bd->max_queu     87         return put_user(READ_ONCE(bd->max_queue), uarg);
 90 }                                                  88 }
 91                                                    89 
 92 static int bsg_set_command_q(struct bsg_device     90 static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
 93 {                                                  91 {
 94         int max_queue;                             92         int max_queue;
 95                                                    93 
 96         if (get_user(max_queue, uarg))             94         if (get_user(max_queue, uarg))
 97                 return -EFAULT;                    95                 return -EFAULT;
 98         if (max_queue < 1)                         96         if (max_queue < 1)
 99                 return -EINVAL;                    97                 return -EINVAL;
100         WRITE_ONCE(bd->max_queue, max_queue);      98         WRITE_ONCE(bd->max_queue, max_queue);
101         return 0;                                  99         return 0;
102 }                                                 100 }
103                                                   101 
104 static long bsg_ioctl(struct file *file, unsig    102 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
105 {                                                 103 {
106         struct bsg_device *bd = to_bsg_device(    104         struct bsg_device *bd = to_bsg_device(file_inode(file));
107         struct request_queue *q = bd->queue;      105         struct request_queue *q = bd->queue;
108         void __user *uarg = (void __user *) ar    106         void __user *uarg = (void __user *) arg;
109         int __user *intp = uarg;                  107         int __user *intp = uarg;
110         int val;                                  108         int val;
111                                                   109 
112         switch (cmd) {                            110         switch (cmd) {
113         /*                                        111         /*
114          * Our own ioctls                         112          * Our own ioctls
115          */                                       113          */
116         case SG_GET_COMMAND_Q:                    114         case SG_GET_COMMAND_Q:
117                 return bsg_get_command_q(bd, u    115                 return bsg_get_command_q(bd, uarg);
118         case SG_SET_COMMAND_Q:                    116         case SG_SET_COMMAND_Q:
119                 return bsg_set_command_q(bd, u    117                 return bsg_set_command_q(bd, uarg);
120                                                   118 
121         /*                                        119         /*
122          * SCSI/sg ioctls                         120          * SCSI/sg ioctls
123          */                                       121          */
124         case SG_GET_VERSION_NUM:                  122         case SG_GET_VERSION_NUM:
125                 return put_user(30527, intp);     123                 return put_user(30527, intp);
126         case SCSI_IOCTL_GET_IDLUN:                124         case SCSI_IOCTL_GET_IDLUN:
127                 return put_user(0, intp);         125                 return put_user(0, intp);
128         case SCSI_IOCTL_GET_BUS_NUMBER:           126         case SCSI_IOCTL_GET_BUS_NUMBER:
129                 return put_user(0, intp);         127                 return put_user(0, intp);
130         case SG_SET_TIMEOUT:                      128         case SG_SET_TIMEOUT:
131                 if (get_user(val, intp))          129                 if (get_user(val, intp))
132                         return -EFAULT;           130                         return -EFAULT;
133                 bd->timeout = clock_t_to_jiffi    131                 bd->timeout = clock_t_to_jiffies(val);
134                 return 0;                         132                 return 0;
135         case SG_GET_TIMEOUT:                      133         case SG_GET_TIMEOUT:
136                 return jiffies_to_clock_t(bd->    134                 return jiffies_to_clock_t(bd->timeout);
137         case SG_GET_RESERVED_SIZE:                135         case SG_GET_RESERVED_SIZE:
138                 return put_user(min(bd->reserv    136                 return put_user(min(bd->reserved_size, queue_max_bytes(q)),
139                                 intp);            137                                 intp);
140         case SG_SET_RESERVED_SIZE:                138         case SG_SET_RESERVED_SIZE:
141                 if (get_user(val, intp))          139                 if (get_user(val, intp))
142                         return -EFAULT;           140                         return -EFAULT;
143                 if (val < 0)                      141                 if (val < 0)
144                         return -EINVAL;           142                         return -EINVAL;
145                 bd->reserved_size =               143                 bd->reserved_size =
146                         min_t(unsigned int, va    144                         min_t(unsigned int, val, queue_max_bytes(q));
147                 return 0;                         145                 return 0;
148         case SG_EMULATED_HOST:                    146         case SG_EMULATED_HOST:
149                 return put_user(1, intp);         147                 return put_user(1, intp);
150         case SG_IO:                               148         case SG_IO:
151                 return bsg_sg_io(bd, file->f_m !! 149                 return bsg_sg_io(bd, file->f_mode, uarg);
152         case SCSI_IOCTL_SEND_COMMAND:             150         case SCSI_IOCTL_SEND_COMMAND:
153                 pr_warn_ratelimited("%s: calli    151                 pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
154                                 current->comm)    152                                 current->comm);
155                 return -EINVAL;                   153                 return -EINVAL;
156         default:                                  154         default:
157                 return -ENOTTY;                   155                 return -ENOTTY;
158         }                                         156         }
159 }                                                 157 }
160                                                   158 
161 static const struct file_operations bsg_fops =    159 static const struct file_operations bsg_fops = {
162         .open           =       bsg_open,         160         .open           =       bsg_open,
163         .release        =       bsg_release,      161         .release        =       bsg_release,
164         .unlocked_ioctl =       bsg_ioctl,        162         .unlocked_ioctl =       bsg_ioctl,
165         .compat_ioctl   =       compat_ptr_ioc    163         .compat_ioctl   =       compat_ptr_ioctl,
166         .owner          =       THIS_MODULE,      164         .owner          =       THIS_MODULE,
167         .llseek         =       default_llseek    165         .llseek         =       default_llseek,
168 };                                                166 };
169                                                   167 
170 static void bsg_device_release(struct device *    168 static void bsg_device_release(struct device *dev)
171 {                                                 169 {
172         struct bsg_device *bd = container_of(d    170         struct bsg_device *bd = container_of(dev, struct bsg_device, device);
173                                                   171 
174         ida_free(&bsg_minor_ida, MINOR(bd->dev !! 172         ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt));
175         kfree(bd);                                173         kfree(bd);
176 }                                                 174 }
177                                                   175 
178 void bsg_unregister_queue(struct bsg_device *b    176 void bsg_unregister_queue(struct bsg_device *bd)
179 {                                                 177 {
180         struct gendisk *disk = bd->queue->disk !! 178         if (bd->queue->kobj.sd)
181                                                !! 179                 sysfs_remove_link(&bd->queue->kobj, "bsg");
182         if (disk && disk->queue_kobj.sd)       << 
183                 sysfs_remove_link(&disk->queue << 
184         cdev_device_del(&bd->cdev, &bd->device    180         cdev_device_del(&bd->cdev, &bd->device);
185         put_device(&bd->device);                  181         put_device(&bd->device);
186 }                                                 182 }
187 EXPORT_SYMBOL_GPL(bsg_unregister_queue);          183 EXPORT_SYMBOL_GPL(bsg_unregister_queue);
188                                                   184 
189 struct bsg_device *bsg_register_queue(struct r    185 struct bsg_device *bsg_register_queue(struct request_queue *q,
190                 struct device *parent, const c    186                 struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn)
191 {                                                 187 {
192         struct bsg_device *bd;                    188         struct bsg_device *bd;
193         int ret;                                  189         int ret;
194                                                   190 
195         bd = kzalloc(sizeof(*bd), GFP_KERNEL);    191         bd = kzalloc(sizeof(*bd), GFP_KERNEL);
196         if (!bd)                                  192         if (!bd)
197                 return ERR_PTR(-ENOMEM);          193                 return ERR_PTR(-ENOMEM);
198         bd->max_queue = BSG_DEFAULT_CMDS;         194         bd->max_queue = BSG_DEFAULT_CMDS;
199         bd->reserved_size = INT_MAX;              195         bd->reserved_size = INT_MAX;
200         bd->queue = q;                            196         bd->queue = q;
201         bd->sg_io_fn = sg_io_fn;                  197         bd->sg_io_fn = sg_io_fn;
202                                                   198 
203         ret = ida_alloc_max(&bsg_minor_ida, BS !! 199         ret = ida_simple_get(&bsg_minor_ida, 0, BSG_MAX_DEVS, GFP_KERNEL);
204         if (ret < 0) {                            200         if (ret < 0) {
205                 if (ret == -ENOSPC)               201                 if (ret == -ENOSPC)
206                         dev_err(parent, "bsg:     202                         dev_err(parent, "bsg: too many bsg devices\n");
207                 kfree(bd);                        203                 kfree(bd);
208                 return ERR_PTR(ret);              204                 return ERR_PTR(ret);
209         }                                         205         }
210         bd->device.devt = MKDEV(bsg_major, ret    206         bd->device.devt = MKDEV(bsg_major, ret);
211         bd->device.class = &bsg_class;         !! 207         bd->device.class = bsg_class;
212         bd->device.parent = parent;               208         bd->device.parent = parent;
213         bd->device.release = bsg_device_releas    209         bd->device.release = bsg_device_release;
214         dev_set_name(&bd->device, "%s", name);    210         dev_set_name(&bd->device, "%s", name);
215         device_initialize(&bd->device);           211         device_initialize(&bd->device);
216                                                   212 
217         cdev_init(&bd->cdev, &bsg_fops);          213         cdev_init(&bd->cdev, &bsg_fops);
218         bd->cdev.owner = THIS_MODULE;             214         bd->cdev.owner = THIS_MODULE;
219         ret = cdev_device_add(&bd->cdev, &bd->    215         ret = cdev_device_add(&bd->cdev, &bd->device);
220         if (ret)                                  216         if (ret)
221                 goto out_put_device;              217                 goto out_put_device;
222                                                   218 
223         if (q->disk && q->disk->queue_kobj.sd) !! 219         if (q->kobj.sd) {
224                 ret = sysfs_create_link(&q->di !! 220                 ret = sysfs_create_link(&q->kobj, &bd->device.kobj, "bsg");
225                                         "bsg") << 
226                 if (ret)                          221                 if (ret)
227                         goto out_device_del;      222                         goto out_device_del;
228         }                                         223         }
229                                                   224 
230         return bd;                                225         return bd;
231                                                   226 
232 out_device_del:                                   227 out_device_del:
233         cdev_device_del(&bd->cdev, &bd->device    228         cdev_device_del(&bd->cdev, &bd->device);
234 out_put_device:                                   229 out_put_device:
235         put_device(&bd->device);                  230         put_device(&bd->device);
236         return ERR_PTR(ret);                      231         return ERR_PTR(ret);
237 }                                                 232 }
238 EXPORT_SYMBOL_GPL(bsg_register_queue);            233 EXPORT_SYMBOL_GPL(bsg_register_queue);
239                                                   234 
240 static char *bsg_devnode(const struct device * !! 235 static char *bsg_devnode(struct device *dev, umode_t *mode)
241 {                                                 236 {
242         return kasprintf(GFP_KERNEL, "bsg/%s",    237         return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
243 }                                                 238 }
244                                                   239 
245 static const struct class bsg_class = {        << 
246         .name           = "bsg",               << 
247         .devnode        = bsg_devnode,         << 
248 };                                             << 
249                                                << 
250 static int __init bsg_init(void)                  240 static int __init bsg_init(void)
251 {                                                 241 {
252         dev_t devid;                              242         dev_t devid;
253         int ret;                                  243         int ret;
254                                                   244 
255         ret = class_register(&bsg_class);      !! 245         bsg_class = class_create(THIS_MODULE, "bsg");
256         if (ret)                               !! 246         if (IS_ERR(bsg_class))
257                 return ret;                    !! 247                 return PTR_ERR(bsg_class);
                                                   >> 248         bsg_class->devnode = bsg_devnode;
258                                                   249 
259         ret = alloc_chrdev_region(&devid, 0, B    250         ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
260         if (ret)                                  251         if (ret)
261                 goto destroy_bsg_class;           252                 goto destroy_bsg_class;
262         bsg_major = MAJOR(devid);                 253         bsg_major = MAJOR(devid);
263                                                   254 
264         printk(KERN_INFO BSG_DESCRIPTION " ver    255         printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
265                " loaded (major %d)\n", bsg_maj    256                " loaded (major %d)\n", bsg_major);
266         return 0;                                 257         return 0;
267                                                   258 
268 destroy_bsg_class:                                259 destroy_bsg_class:
269         class_unregister(&bsg_class);          !! 260         class_destroy(bsg_class);
270         return ret;                               261         return ret;
271 }                                                 262 }
272                                                   263 
273 MODULE_AUTHOR("Jens Axboe");                      264 MODULE_AUTHOR("Jens Axboe");
274 MODULE_DESCRIPTION(BSG_DESCRIPTION);              265 MODULE_DESCRIPTION(BSG_DESCRIPTION);
275 MODULE_LICENSE("GPL");                            266 MODULE_LICENSE("GPL");
276                                                   267 
277 device_initcall(bsg_init);                        268 device_initcall(bsg_init);
278                                                   269 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php