1 /* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */
4 * block device routines
7 #include <linux/kernel.h>
8 #include <linux/hdreg.h>
9 #include <linux/blk-mq.h>
10 #include <linux/backing-dev.h>
12 #include <linux/ioctl.h>
13 #include <linux/slab.h>
14 #include <linux/ratelimit.h>
15 #include <linux/netdevice.h>
16 #include <linux/mutex.h>
17 #include <linux/export.h>
18 #include <linux/moduleparam.h>
19 #include <linux/debugfs.h>
23 static DEFINE_MUTEX(aoeblk_mutex
);
24 static struct kmem_cache
*buf_pool_cache
;
25 static struct dentry
*aoe_debugfs_dir
;
27 /* random default picked from the historic block max_sectors cap */
28 static int aoe_maxsectors
= 2560;
29 module_param(aoe_maxsectors
, int, 0644);
30 MODULE_PARM_DESC(aoe_maxsectors
,
31 "When nonzero, set the maximum number of sectors per I/O request");
33 static ssize_t
aoedisk_show_state(struct device
*dev
,
34 struct device_attribute
*attr
, char *page
)
36 struct gendisk
*disk
= dev_to_disk(dev
);
37 struct aoedev
*d
= disk
->private_data
;
39 return sysfs_emit(page
, "%s%s\n",
40 (d
->flags
& DEVFL_UP
) ? "up" : "down",
41 (d
->flags
& DEVFL_KICKME
) ? ",kickme" :
42 (d
->nopen
&& !(d
->flags
& DEVFL_UP
)) ? ",closewait" : "");
43 /* I'd rather see nopen exported so we can ditch closewait */
45 static ssize_t
aoedisk_show_mac(struct device
*dev
,
46 struct device_attribute
*attr
, char *page
)
48 struct gendisk
*disk
= dev_to_disk(dev
);
49 struct aoedev
*d
= disk
->private_data
;
50 struct aoetgt
*t
= d
->targets
[0];
53 return sysfs_emit(page
, "none\n");
54 return sysfs_emit(page
, "%pm\n", t
->addr
);
56 static ssize_t
aoedisk_show_netif(struct device
*dev
,
57 struct device_attribute
*attr
, char *page
)
59 struct gendisk
*disk
= dev_to_disk(dev
);
60 struct aoedev
*d
= disk
->private_data
;
61 struct net_device
*nds
[8], **nd
, **nnd
, **ne
;
62 struct aoetgt
**t
, **te
;
63 struct aoeif
*ifp
, *e
;
66 memset(nds
, 0, sizeof nds
);
68 ne
= nd
+ ARRAY_SIZE(nds
);
71 for (; t
< te
&& *t
; t
++) {
74 for (; ifp
< e
&& ifp
->nd
; ifp
++) {
75 for (nnd
= nds
; nnd
< nd
; nnd
++)
78 if (nnd
== nd
&& nd
!= ne
)
86 return sysfs_emit(page
, "none\n");
87 for (p
= page
; nd
< ne
; nd
++)
88 p
+= scnprintf(p
, PAGE_SIZE
- (p
-page
), "%s%s",
89 p
== page
? "" : ",", (*nd
)->name
);
90 p
+= scnprintf(p
, PAGE_SIZE
- (p
-page
), "\n");
93 /* firmware version */
94 static ssize_t
aoedisk_show_fwver(struct device
*dev
,
95 struct device_attribute
*attr
, char *page
)
97 struct gendisk
*disk
= dev_to_disk(dev
);
98 struct aoedev
*d
= disk
->private_data
;
100 return sysfs_emit(page
, "0x%04x\n", (unsigned int) d
->fw_ver
);
102 static ssize_t
aoedisk_show_payload(struct device
*dev
,
103 struct device_attribute
*attr
, char *page
)
105 struct gendisk
*disk
= dev_to_disk(dev
);
106 struct aoedev
*d
= disk
->private_data
;
108 return sysfs_emit(page
, "%lu\n", d
->maxbcnt
);
111 static int aoe_debugfs_show(struct seq_file
*s
, void *ignored
)
114 struct aoetgt
**t
, **te
;
115 struct aoeif
*ifp
, *ife
;
120 seq_printf(s
, "rttavg: %d rttdev: %d\n",
121 d
->rttavg
>> RTTSCALE
,
122 d
->rttdev
>> RTTDSCALE
);
123 seq_printf(s
, "nskbpool: %d\n", skb_queue_len(&d
->skbpool
));
124 seq_printf(s
, "kicked: %ld\n", d
->kicked
);
125 seq_printf(s
, "maxbcnt: %ld\n", d
->maxbcnt
);
126 seq_printf(s
, "ref: %ld\n", d
->ref
);
128 spin_lock_irqsave(&d
->lock
, flags
);
130 te
= t
+ d
->ntargets
;
131 for (; t
< te
&& *t
; t
++) {
133 seq_printf(s
, "falloc: %ld\n", (*t
)->falloc
);
134 seq_printf(s
, "ffree: %p\n",
135 list_empty(&(*t
)->ffree
) ? NULL
: (*t
)->ffree
.next
);
136 seq_printf(s
, "%pm:%d:%d:%d\n", (*t
)->addr
, (*t
)->nout
,
137 (*t
)->maxout
, (*t
)->nframes
);
138 seq_printf(s
, "\tssthresh:%d\n", (*t
)->ssthresh
);
139 seq_printf(s
, "\ttaint:%d\n", (*t
)->taint
);
140 seq_printf(s
, "\tr:%d\n", (*t
)->rpkts
);
141 seq_printf(s
, "\tw:%d\n", (*t
)->wpkts
);
143 ife
= ifp
+ ARRAY_SIZE((*t
)->ifs
);
144 for (; ifp
->nd
&& ifp
< ife
; ifp
++) {
145 seq_printf(s
, "%c%s", c
, ifp
->nd
->name
);
150 spin_unlock_irqrestore(&d
->lock
, flags
);
154 DEFINE_SHOW_ATTRIBUTE(aoe_debugfs
);
156 static DEVICE_ATTR(state
, 0444, aoedisk_show_state
, NULL
);
157 static DEVICE_ATTR(mac
, 0444, aoedisk_show_mac
, NULL
);
158 static DEVICE_ATTR(netif
, 0444, aoedisk_show_netif
, NULL
);
159 static struct device_attribute dev_attr_firmware_version
= {
160 .attr
= { .name
= "firmware-version", .mode
= 0444 },
161 .show
= aoedisk_show_fwver
,
163 static DEVICE_ATTR(payload
, 0444, aoedisk_show_payload
, NULL
);
165 static struct attribute
*aoe_attrs
[] = {
166 &dev_attr_state
.attr
,
168 &dev_attr_netif
.attr
,
169 &dev_attr_firmware_version
.attr
,
170 &dev_attr_payload
.attr
,
174 static const struct attribute_group aoe_attr_group
= {
178 static const struct attribute_group
*aoe_attr_groups
[] = {
184 aoedisk_add_debugfs(struct aoedev
*d
)
188 if (aoe_debugfs_dir
== NULL
)
190 p
= strchr(d
->gd
->disk_name
, '/');
192 p
= d
->gd
->disk_name
;
196 d
->debugfs
= debugfs_create_file(p
, 0444, aoe_debugfs_dir
, d
,
200 aoedisk_rm_debugfs(struct aoedev
*d
)
202 debugfs_remove(d
->debugfs
);
207 aoeblk_open(struct gendisk
*disk
, blk_mode_t mode
)
209 struct aoedev
*d
= disk
->private_data
;
212 if (!virt_addr_valid(d
)) {
213 pr_crit("aoe: invalid device pointer in %s\n",
218 if (!(d
->flags
& DEVFL_UP
) || d
->flags
& DEVFL_TKILL
)
221 mutex_lock(&aoeblk_mutex
);
222 spin_lock_irqsave(&d
->lock
, flags
);
223 if (d
->flags
& DEVFL_UP
&& !(d
->flags
& DEVFL_TKILL
)) {
225 spin_unlock_irqrestore(&d
->lock
, flags
);
226 mutex_unlock(&aoeblk_mutex
);
229 spin_unlock_irqrestore(&d
->lock
, flags
);
230 mutex_unlock(&aoeblk_mutex
);
235 aoeblk_release(struct gendisk
*disk
)
237 struct aoedev
*d
= disk
->private_data
;
240 spin_lock_irqsave(&d
->lock
, flags
);
242 if (--d
->nopen
== 0) {
243 spin_unlock_irqrestore(&d
->lock
, flags
);
244 aoecmd_cfg(d
->aoemajor
, d
->aoeminor
);
247 spin_unlock_irqrestore(&d
->lock
, flags
);
250 static blk_status_t
aoeblk_queue_rq(struct blk_mq_hw_ctx
*hctx
,
251 const struct blk_mq_queue_data
*bd
)
253 struct aoedev
*d
= hctx
->queue
->queuedata
;
255 spin_lock_irq(&d
->lock
);
257 if ((d
->flags
& DEVFL_UP
) == 0) {
258 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
259 d
->aoemajor
, d
->aoeminor
);
260 spin_unlock_irq(&d
->lock
);
261 blk_mq_start_request(bd
->rq
);
262 return BLK_STS_IOERR
;
265 list_add_tail(&bd
->rq
->queuelist
, &d
->rq_list
);
267 spin_unlock_irq(&d
->lock
);
272 aoeblk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
274 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
276 if ((d
->flags
& DEVFL_UP
) == 0) {
277 printk(KERN_ERR
"aoe: disk not up\n");
281 geo
->cylinders
= d
->geo
.cylinders
;
282 geo
->heads
= d
->geo
.heads
;
283 geo
->sectors
= d
->geo
.sectors
;
288 aoeblk_ioctl(struct block_device
*bdev
, blk_mode_t mode
, uint cmd
, ulong arg
)
295 d
= bdev
->bd_disk
->private_data
;
296 if ((d
->flags
& DEVFL_UP
) == 0) {
297 pr_err("aoe: disk not up\n");
301 if (cmd
== HDIO_GET_IDENTITY
) {
302 if (!copy_to_user((void __user
*) arg
, &d
->ident
,
308 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
310 pr_info("aoe: unknown ioctl 0x%x\n", cmd
);
315 static const struct block_device_operations aoe_bdops
= {
317 .release
= aoeblk_release
,
318 .ioctl
= aoeblk_ioctl
,
319 .compat_ioctl
= blkdev_compat_ptr_ioctl
,
320 .getgeo
= aoeblk_getgeo
,
321 .owner
= THIS_MODULE
,
324 static const struct blk_mq_ops aoeblk_mq_ops
= {
325 .queue_rq
= aoeblk_queue_rq
,
328 /* blk_mq_alloc_disk and add_disk can sleep */
330 aoeblk_gdalloc(void *vp
)
332 struct aoedev
*d
= vp
;
335 struct blk_mq_tag_set
*set
;
337 struct queue_limits lim
= {
338 .max_hw_sectors
= aoe_maxsectors
,
340 .features
= BLK_FEAT_ROTATIONAL
,
346 spin_lock_irqsave(&d
->lock
, flags
);
347 if (d
->flags
& DEVFL_GDALLOC
348 && !(d
->flags
& DEVFL_TKILL
)
349 && !(d
->flags
& DEVFL_GD_NOW
))
350 d
->flags
|= DEVFL_GD_NOW
;
353 spin_unlock_irqrestore(&d
->lock
, flags
);
357 mp
= mempool_create(MIN_BUFS
, mempool_alloc_slab
, mempool_free_slab
,
360 printk(KERN_ERR
"aoe: cannot allocate bufpool for %ld.%d\n",
361 d
->aoemajor
, d
->aoeminor
);
366 set
->ops
= &aoeblk_mq_ops
;
367 set
->cmd_size
= sizeof(struct aoe_req
);
368 set
->nr_hw_queues
= 1;
369 set
->queue_depth
= 128;
370 set
->numa_node
= NUMA_NO_NODE
;
371 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
372 err
= blk_mq_alloc_tag_set(set
);
374 pr_err("aoe: cannot allocate tag set for %ld.%d\n",
375 d
->aoemajor
, d
->aoeminor
);
379 gd
= blk_mq_alloc_disk(set
, &lim
, d
);
381 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
382 d
->aoemajor
, d
->aoeminor
);
386 spin_lock_irqsave(&d
->lock
, flags
);
387 WARN_ON(!(d
->flags
& DEVFL_GD_NOW
));
388 WARN_ON(!(d
->flags
& DEVFL_GDALLOC
));
389 WARN_ON(d
->flags
& DEVFL_TKILL
);
391 WARN_ON(d
->flags
& DEVFL_UP
);
395 gd
->major
= AOE_MAJOR
;
396 gd
->first_minor
= d
->sysminor
;
397 gd
->minors
= AOE_PARTITIONS
;
398 gd
->fops
= &aoe_bdops
;
399 gd
->private_data
= d
;
401 snprintf(gd
->disk_name
, sizeof gd
->disk_name
, "etherd/e%ld.%d",
402 d
->aoemajor
, d
->aoeminor
);
404 d
->flags
&= ~DEVFL_GDALLOC
;
405 d
->flags
|= DEVFL_UP
;
407 spin_unlock_irqrestore(&d
->lock
, flags
);
409 set_capacity(gd
, ssize
);
411 err
= device_add_disk(NULL
, gd
, aoe_attr_groups
);
413 goto out_disk_cleanup
;
414 aoedisk_add_debugfs(d
);
416 spin_lock_irqsave(&d
->lock
, flags
);
417 WARN_ON(!(d
->flags
& DEVFL_GD_NOW
));
418 d
->flags
&= ~DEVFL_GD_NOW
;
419 spin_unlock_irqrestore(&d
->lock
, flags
);
425 blk_mq_free_tag_set(set
);
429 spin_lock_irqsave(&d
->lock
, flags
);
430 d
->flags
&= ~DEVFL_GD_NOW
;
431 queue_work(aoe_wq
, &d
->work
);
432 spin_unlock_irqrestore(&d
->lock
, flags
);
438 debugfs_remove_recursive(aoe_debugfs_dir
);
439 aoe_debugfs_dir
= NULL
;
440 kmem_cache_destroy(buf_pool_cache
);
446 buf_pool_cache
= kmem_cache_create("aoe_bufs",
449 if (buf_pool_cache
== NULL
)
451 aoe_debugfs_dir
= debugfs_create_dir("aoe", NULL
);