1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RDMA Network Block Driver
5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
12 #include "rnbd-srv-dev.h"
15 struct rnbd_dev
*rnbd_dev_open(const char *path
, fmode_t flags
,
21 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
23 return ERR_PTR(-ENOMEM
);
25 dev
->blk_open_flags
= flags
;
26 dev
->bdev
= blkdev_get_by_path(path
, flags
, THIS_MODULE
);
27 ret
= PTR_ERR_OR_ZERO(dev
->bdev
);
31 dev
->blk_open_flags
= flags
;
32 bdevname(dev
->bdev
, dev
->name
);
33 dev
->ibd_bio_set
= bs
;
42 void rnbd_dev_close(struct rnbd_dev
*dev
)
44 blkdev_put(dev
->bdev
, dev
->blk_open_flags
);
48 void rnbd_dev_bi_end_io(struct bio
*bio
)
50 struct rnbd_dev_blk_io
*io
= bio
->bi_private
;
52 rnbd_endio(io
->priv
, blk_status_to_errno(bio
->bi_status
));
57 * rnbd_bio_map_kern - map kernel address into bio
58 * @data: pointer to buffer to map
59 * @bs: bio_set to use.
60 * @len: length in bytes
61 * @gfp_mask: allocation flags for bio allocation
63 * Map the kernel address into a bio suitable for io to a block
64 * device. Returns an error pointer in case of error.
66 struct bio
*rnbd_bio_map_kern(void *data
, struct bio_set
*bs
,
67 unsigned int len
, gfp_t gfp_mask
)
69 unsigned long kaddr
= (unsigned long)data
;
70 unsigned long end
= (kaddr
+ len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
71 unsigned long start
= kaddr
>> PAGE_SHIFT
;
72 const int nr_pages
= end
- start
;
76 bio
= bio_alloc_bioset(gfp_mask
, nr_pages
, bs
);
78 return ERR_PTR(-ENOMEM
);
80 offset
= offset_in_page(kaddr
);
81 for (i
= 0; i
< nr_pages
; i
++) {
82 unsigned int bytes
= PAGE_SIZE
- offset
;
90 if (bio_add_page(bio
, virt_to_page(data
), bytes
,
92 /* we don't support partial mappings */
94 return ERR_PTR(-EINVAL
);