]>
Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /******************************************************************************* |
2 | * Filename: target_core_iblock.c | |
3 | * | |
4 | * This file contains the Storage Engine <-> Linux BlockIO transport | |
5 | * specific functions. | |
6 | * | |
4c76251e | 7 | * (c) Copyright 2003-2013 Datera, Inc. |
c66ac9db NB |
8 | * |
9 | * Nicholas A. Bellinger <nab@kernel.org> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
24 | * | |
25 | ******************************************************************************/ | |
26 | ||
c66ac9db NB |
27 | #include <linux/string.h> |
28 | #include <linux/parser.h> | |
29 | #include <linux/timer.h> | |
30 | #include <linux/fs.h> | |
31 | #include <linux/blkdev.h> | |
32 | #include <linux/slab.h> | |
33 | #include <linux/spinlock.h> | |
c66ac9db NB |
34 | #include <linux/bio.h> |
35 | #include <linux/genhd.h> | |
36 | #include <linux/file.h> | |
827509e3 | 37 | #include <linux/module.h> |
ba929992 | 38 | #include <scsi/scsi_proto.h> |
14150a6b | 39 | #include <asm/unaligned.h> |
c66ac9db NB |
40 | |
41 | #include <target/target_core_base.h> | |
c4795fb2 | 42 | #include <target/target_core_backend.h> |
c66ac9db NB |
43 | |
44 | #include "target_core_iblock.h" | |
45 | ||
d5b4a21b CH |
46 | #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */ |
47 | #define IBLOCK_BIO_POOL_SIZE 128 | |
48 | ||
0fd97ccf CH |
49 | static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev) |
50 | { | |
51 | return container_of(dev, struct iblock_dev, dev); | |
52 | } | |
53 | ||
54 | ||
c66ac9db NB |
55 | static int iblock_attach_hba(struct se_hba *hba, u32 host_id) |
56 | { | |
6708bb27 | 57 | pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on" |
c66ac9db | 58 | " Generic Target Core Stack %s\n", hba->hba_id, |
ce8dd25d | 59 | IBLOCK_VERSION, TARGET_CORE_VERSION); |
c66ac9db NB |
60 | return 0; |
61 | } | |
62 | ||
63 | static void iblock_detach_hba(struct se_hba *hba) | |
64 | { | |
c66ac9db NB |
65 | } |
66 | ||
0fd97ccf | 67 | static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name) |
c66ac9db NB |
68 | { |
69 | struct iblock_dev *ib_dev = NULL; | |
c66ac9db NB |
70 | |
71 | ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL); | |
6708bb27 AG |
72 | if (!ib_dev) { |
73 | pr_err("Unable to allocate struct iblock_dev\n"); | |
c66ac9db NB |
74 | return NULL; |
75 | } | |
c66ac9db | 76 | |
6708bb27 | 77 | pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name); |
c66ac9db | 78 | |
0fd97ccf | 79 | return &ib_dev->dev; |
c66ac9db NB |
80 | } |
81 | ||
0fd97ccf | 82 | static int iblock_configure_device(struct se_device *dev) |
c66ac9db | 83 | { |
0fd97ccf | 84 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); |
c66ac9db | 85 | struct request_queue *q; |
0fd97ccf | 86 | struct block_device *bd = NULL; |
ecebbf6c | 87 | struct blk_integrity *bi; |
44bfd018 | 88 | fmode_t mode; |
2237498f | 89 | unsigned int max_write_zeroes_sectors; |
0fd97ccf | 90 | int ret = -ENOMEM; |
c66ac9db | 91 | |
0fd97ccf CH |
92 | if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) { |
93 | pr_err("Missing udev_path= parameters for IBLOCK\n"); | |
94 | return -EINVAL; | |
c66ac9db | 95 | } |
d5b4a21b CH |
96 | |
97 | ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0); | |
6708bb27 | 98 | if (!ib_dev->ibd_bio_set) { |
0fd97ccf CH |
99 | pr_err("IBLOCK: Unable to create bioset\n"); |
100 | goto out; | |
c66ac9db | 101 | } |
0fd97ccf | 102 | |
6708bb27 | 103 | pr_debug( "IBLOCK: Claiming struct block_device: %s\n", |
c66ac9db NB |
104 | ib_dev->ibd_udev_path); |
105 | ||
44bfd018 AG |
106 | mode = FMODE_READ|FMODE_EXCL; |
107 | if (!ib_dev->ibd_readonly) | |
108 | mode |= FMODE_WRITE; | |
eeeb9522 NB |
109 | else |
110 | dev->dev_flags |= DF_READ_ONLY; | |
44bfd018 AG |
111 | |
112 | bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev); | |
613640e4 NB |
113 | if (IS_ERR(bd)) { |
114 | ret = PTR_ERR(bd); | |
0fd97ccf | 115 | goto out_free_bioset; |
613640e4 | 116 | } |
c66ac9db NB |
117 | ib_dev->ibd_bd = bd; |
118 | ||
0fd97ccf CH |
119 | q = bdev_get_queue(bd); |
120 | ||
121 | dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd); | |
046ba642 | 122 | dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q); |
0fd97ccf | 123 | dev->dev_attrib.hw_queue_depth = q->nr_requests; |
c66ac9db | 124 | |
ea263c7f | 125 | if (target_configure_unmap_from_queue(&dev->dev_attrib, q)) |
6708bb27 | 126 | pr_debug("IBLOCK: BLOCK Discard support available," |
8a9ebe71 MC |
127 | " disabled by default\n"); |
128 | ||
f6970ad3 NB |
129 | /* |
130 | * Enable write same emulation for IBLOCK and use 0xFFFF as | |
131 | * the smaller WRITE_SAME(10) only has a two-byte block count. | |
132 | */ | |
2237498f NB |
133 | max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd); |
134 | if (max_write_zeroes_sectors) | |
135 | dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors; | |
136 | else | |
137 | dev->dev_attrib.max_write_same_len = 0xFFFF; | |
c66ac9db | 138 | |
e22a7f07 | 139 | if (blk_queue_nonrot(q)) |
0fd97ccf | 140 | dev->dev_attrib.is_nonrot = 1; |
d0c8b259 | 141 | |
ecebbf6c NB |
142 | bi = bdev_get_integrity(bd); |
143 | if (bi) { | |
144 | struct bio_set *bs = ib_dev->ibd_bio_set; | |
145 | ||
0f8087ec MP |
146 | if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") || |
147 | !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) { | |
ecebbf6c | 148 | pr_err("IBLOCK export of blk_integrity: %s not" |
0f8087ec | 149 | " supported\n", bi->profile->name); |
ecebbf6c NB |
150 | ret = -ENOSYS; |
151 | goto out_blkdev_put; | |
152 | } | |
153 | ||
0f8087ec | 154 | if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) { |
ecebbf6c | 155 | dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT; |
0f8087ec | 156 | } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) { |
ecebbf6c NB |
157 | dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT; |
158 | } | |
159 | ||
160 | if (dev->dev_attrib.pi_prot_type) { | |
161 | if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) { | |
162 | pr_err("Unable to allocate bioset for PI\n"); | |
163 | ret = -ENOMEM; | |
164 | goto out_blkdev_put; | |
165 | } | |
166 | pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n", | |
167 | bs->bio_integrity_pool); | |
168 | } | |
169 | dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type; | |
170 | } | |
171 | ||
0fd97ccf | 172 | return 0; |
c66ac9db | 173 | |
ecebbf6c NB |
174 | out_blkdev_put: |
175 | blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); | |
0fd97ccf CH |
176 | out_free_bioset: |
177 | bioset_free(ib_dev->ibd_bio_set); | |
178 | ib_dev->ibd_bio_set = NULL; | |
179 | out: | |
180 | return ret; | |
c66ac9db NB |
181 | } |
182 | ||
4cc987ea NB |
183 | static void iblock_dev_call_rcu(struct rcu_head *p) |
184 | { | |
185 | struct se_device *dev = container_of(p, struct se_device, rcu_head); | |
186 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
187 | ||
188 | kfree(ib_dev); | |
189 | } | |
190 | ||
0fd97ccf | 191 | static void iblock_free_device(struct se_device *dev) |
c66ac9db | 192 | { |
0fd97ccf | 193 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); |
c66ac9db | 194 | |
bc665524 NB |
195 | if (ib_dev->ibd_bd != NULL) |
196 | blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); | |
d84287bc | 197 | if (ib_dev->ibd_bio_set != NULL) |
bc665524 | 198 | bioset_free(ib_dev->ibd_bio_set); |
d84287bc | 199 | |
4cc987ea | 200 | call_rcu(&dev->rcu_head, iblock_dev_call_rcu); |
c66ac9db NB |
201 | } |
202 | ||
c66ac9db NB |
203 | static unsigned long long iblock_emulate_read_cap_with_block_size( |
204 | struct se_device *dev, | |
205 | struct block_device *bd, | |
206 | struct request_queue *q) | |
207 | { | |
208 | unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode), | |
209 | bdev_logical_block_size(bd)) - 1); | |
210 | u32 block_size = bdev_logical_block_size(bd); | |
211 | ||
0fd97ccf | 212 | if (block_size == dev->dev_attrib.block_size) |
c66ac9db NB |
213 | return blocks_long; |
214 | ||
215 | switch (block_size) { | |
216 | case 4096: | |
0fd97ccf | 217 | switch (dev->dev_attrib.block_size) { |
c66ac9db NB |
218 | case 2048: |
219 | blocks_long <<= 1; | |
220 | break; | |
221 | case 1024: | |
222 | blocks_long <<= 2; | |
223 | break; | |
224 | case 512: | |
225 | blocks_long <<= 3; | |
226 | default: | |
227 | break; | |
228 | } | |
229 | break; | |
230 | case 2048: | |
0fd97ccf | 231 | switch (dev->dev_attrib.block_size) { |
c66ac9db NB |
232 | case 4096: |
233 | blocks_long >>= 1; | |
234 | break; | |
235 | case 1024: | |
236 | blocks_long <<= 1; | |
237 | break; | |
238 | case 512: | |
239 | blocks_long <<= 2; | |
240 | break; | |
241 | default: | |
242 | break; | |
243 | } | |
244 | break; | |
245 | case 1024: | |
0fd97ccf | 246 | switch (dev->dev_attrib.block_size) { |
c66ac9db NB |
247 | case 4096: |
248 | blocks_long >>= 2; | |
249 | break; | |
250 | case 2048: | |
251 | blocks_long >>= 1; | |
252 | break; | |
253 | case 512: | |
254 | blocks_long <<= 1; | |
255 | break; | |
256 | default: | |
257 | break; | |
258 | } | |
259 | break; | |
260 | case 512: | |
0fd97ccf | 261 | switch (dev->dev_attrib.block_size) { |
c66ac9db NB |
262 | case 4096: |
263 | blocks_long >>= 3; | |
264 | break; | |
265 | case 2048: | |
266 | blocks_long >>= 2; | |
267 | break; | |
268 | case 1024: | |
269 | blocks_long >>= 1; | |
270 | break; | |
271 | default: | |
272 | break; | |
273 | } | |
274 | break; | |
275 | default: | |
276 | break; | |
277 | } | |
278 | ||
279 | return blocks_long; | |
280 | } | |
281 | ||
3a41d85f NB |
282 | static void iblock_complete_cmd(struct se_cmd *cmd) |
283 | { | |
284 | struct iblock_req *ibr = cmd->priv; | |
285 | u8 status; | |
286 | ||
5981c245 | 287 | if (!refcount_dec_and_test(&ibr->pending)) |
3a41d85f NB |
288 | return; |
289 | ||
290 | if (atomic_read(&ibr->ib_bio_err_cnt)) | |
291 | status = SAM_STAT_CHECK_CONDITION; | |
292 | else | |
293 | status = SAM_STAT_GOOD; | |
294 | ||
295 | target_complete_cmd(cmd, status); | |
296 | kfree(ibr); | |
297 | } | |
298 | ||
4246a0b6 | 299 | static void iblock_bio_done(struct bio *bio) |
3a41d85f NB |
300 | { |
301 | struct se_cmd *cmd = bio->bi_private; | |
302 | struct iblock_req *ibr = cmd->priv; | |
303 | ||
4246a0b6 CH |
304 | if (bio->bi_error) { |
305 | pr_err("bio error: %p, err: %d\n", bio, bio->bi_error); | |
3a41d85f NB |
306 | /* |
307 | * Bump the ib_bio_err_cnt and release bio. | |
308 | */ | |
309 | atomic_inc(&ibr->ib_bio_err_cnt); | |
4e857c58 | 310 | smp_mb__after_atomic(); |
3a41d85f NB |
311 | } |
312 | ||
313 | bio_put(bio); | |
314 | ||
315 | iblock_complete_cmd(cmd); | |
316 | } | |
317 | ||
318 | static struct bio * | |
e742fc32 MC |
319 | iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, int op, |
320 | int op_flags) | |
3a41d85f NB |
321 | { |
322 | struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev); | |
323 | struct bio *bio; | |
324 | ||
325 | /* | |
326 | * Only allocate as many vector entries as the bio code allows us to, | |
327 | * we'll loop later on until we have handled the whole request. | |
328 | */ | |
329 | if (sg_num > BIO_MAX_PAGES) | |
330 | sg_num = BIO_MAX_PAGES; | |
331 | ||
332 | bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set); | |
333 | if (!bio) { | |
334 | pr_err("Unable to allocate memory for bio\n"); | |
335 | return NULL; | |
336 | } | |
337 | ||
338 | bio->bi_bdev = ib_dev->ibd_bd; | |
339 | bio->bi_private = cmd; | |
340 | bio->bi_end_io = &iblock_bio_done; | |
4f024f37 | 341 | bio->bi_iter.bi_sector = lba; |
e742fc32 | 342 | bio_set_op_attrs(bio, op, op_flags); |
3a41d85f NB |
343 | |
344 | return bio; | |
345 | } | |
346 | ||
4e49ea4a | 347 | static void iblock_submit_bios(struct bio_list *list) |
3a41d85f NB |
348 | { |
349 | struct blk_plug plug; | |
350 | struct bio *bio; | |
351 | ||
352 | blk_start_plug(&plug); | |
353 | while ((bio = bio_list_pop(list))) | |
4e49ea4a | 354 | submit_bio(bio); |
3a41d85f NB |
355 | blk_finish_plug(&plug); |
356 | } | |
357 | ||
4246a0b6 | 358 | static void iblock_end_io_flush(struct bio *bio) |
df5fa691 CH |
359 | { |
360 | struct se_cmd *cmd = bio->bi_private; | |
361 | ||
4246a0b6 CH |
362 | if (bio->bi_error) |
363 | pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_error); | |
df5fa691 | 364 | |
5787cacd | 365 | if (cmd) { |
4246a0b6 | 366 | if (bio->bi_error) |
5787cacd | 367 | target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); |
de103c93 | 368 | else |
5787cacd | 369 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
5787cacd CH |
370 | } |
371 | ||
df5fa691 CH |
372 | bio_put(bio); |
373 | } | |
374 | ||
c66ac9db | 375 | /* |
df5fa691 CH |
376 | * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must |
377 | * always flush the whole cache. | |
c66ac9db | 378 | */ |
de103c93 CH |
379 | static sense_reason_t |
380 | iblock_execute_sync_cache(struct se_cmd *cmd) | |
c66ac9db | 381 | { |
0fd97ccf | 382 | struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev); |
a1d8b49a | 383 | int immed = (cmd->t_task_cdb[1] & 0x2); |
df5fa691 | 384 | struct bio *bio; |
c66ac9db NB |
385 | |
386 | /* | |
387 | * If the Immediate bit is set, queue up the GOOD response | |
df5fa691 | 388 | * for this SYNCHRONIZE_CACHE op. |
c66ac9db NB |
389 | */ |
390 | if (immed) | |
5787cacd | 391 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
c66ac9db | 392 | |
df5fa691 CH |
393 | bio = bio_alloc(GFP_KERNEL, 0); |
394 | bio->bi_end_io = iblock_end_io_flush; | |
395 | bio->bi_bdev = ib_dev->ibd_bd; | |
70fd7614 | 396 | bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
c66ac9db | 397 | if (!immed) |
df5fa691 | 398 | bio->bi_private = cmd; |
4e49ea4a | 399 | submit_bio(bio); |
ad67f0d9 | 400 | return 0; |
c66ac9db NB |
401 | } |
402 | ||
dbc21c5a | 403 | static sense_reason_t |
62e46942 | 404 | iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) |
dbc21c5a | 405 | { |
62e46942 | 406 | struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd; |
8a9ebe71 | 407 | struct se_device *dev = cmd->se_dev; |
dbc21c5a AH |
408 | int ret; |
409 | ||
8a9ebe71 MC |
410 | ret = blkdev_issue_discard(bdev, |
411 | target_to_linux_sector(dev, lba), | |
412 | target_to_linux_sector(dev, nolb), | |
413 | GFP_KERNEL, 0); | |
dbc21c5a AH |
414 | if (ret < 0) { |
415 | pr_err("blkdev_issue_discard() failed: %d\n", ret); | |
416 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
417 | } | |
418 | ||
419 | return 0; | |
420 | } | |
421 | ||
07b63196 | 422 | static sense_reason_t |
2237498f | 423 | iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd) |
07b63196 MC |
424 | { |
425 | struct se_device *dev = cmd->se_dev; | |
426 | struct scatterlist *sg = &cmd->t_data_sg[0]; | |
2237498f NB |
427 | unsigned char *buf, zero = 0x00, *p = &zero; |
428 | int rc, ret; | |
07b63196 | 429 | |
2237498f NB |
430 | buf = kmap(sg_page(sg)) + sg->offset; |
431 | if (!buf) | |
432 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
433 | /* | |
434 | * Fall back to block_execute_write_same() slow-path if | |
435 | * incoming WRITE_SAME payload does not contain zeros. | |
436 | */ | |
437 | rc = memcmp(buf, p, cmd->data_length); | |
438 | kunmap(sg_page(sg)); | |
439 | ||
440 | if (rc) | |
441 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
07b63196 | 442 | |
2237498f | 443 | ret = blkdev_issue_zeroout(bdev, |
07b63196 MC |
444 | target_to_linux_sector(dev, cmd->t_task_lba), |
445 | target_to_linux_sector(dev, | |
446 | sbc_get_write_same_sectors(cmd)), | |
2237498f | 447 | GFP_KERNEL, false); |
07b63196 MC |
448 | if (ret) |
449 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
450 | ||
451 | target_complete_cmd(cmd, GOOD); | |
452 | return 0; | |
453 | } | |
454 | ||
f6970ad3 NB |
455 | static sense_reason_t |
456 | iblock_execute_write_same(struct se_cmd *cmd) | |
457 | { | |
07b63196 | 458 | struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd; |
f6970ad3 NB |
459 | struct iblock_req *ibr; |
460 | struct scatterlist *sg; | |
461 | struct bio *bio; | |
462 | struct bio_list list; | |
8a9ebe71 MC |
463 | struct se_device *dev = cmd->se_dev; |
464 | sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); | |
465 | sector_t sectors = target_to_linux_sector(dev, | |
466 | sbc_get_write_same_sectors(cmd)); | |
f6970ad3 | 467 | |
afd73f1b NB |
468 | if (cmd->prot_op) { |
469 | pr_err("WRITE_SAME: Protection information with IBLOCK" | |
470 | " backends not supported\n"); | |
471 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
472 | } | |
f6970ad3 NB |
473 | sg = &cmd->t_data_sg[0]; |
474 | ||
475 | if (cmd->t_data_nents > 1 || | |
476 | sg->length != cmd->se_dev->dev_attrib.block_size) { | |
477 | pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" | |
478 | " block_size: %u\n", cmd->t_data_nents, sg->length, | |
479 | cmd->se_dev->dev_attrib.block_size); | |
480 | return TCM_INVALID_CDB_FIELD; | |
481 | } | |
482 | ||
2237498f NB |
483 | if (bdev_write_zeroes_sectors(bdev)) { |
484 | if (!iblock_execute_zero_out(bdev, cmd)) | |
485 | return 0; | |
486 | } | |
07b63196 | 487 | |
f6970ad3 NB |
488 | ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); |
489 | if (!ibr) | |
490 | goto fail; | |
491 | cmd->priv = ibr; | |
492 | ||
e742fc32 | 493 | bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE, 0); |
f6970ad3 NB |
494 | if (!bio) |
495 | goto fail_free_ibr; | |
496 | ||
497 | bio_list_init(&list); | |
498 | bio_list_add(&list, bio); | |
499 | ||
5981c245 | 500 | refcount_set(&ibr->pending, 1); |
f6970ad3 NB |
501 | |
502 | while (sectors) { | |
503 | while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) | |
504 | != sg->length) { | |
505 | ||
e742fc32 MC |
506 | bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE, |
507 | 0); | |
f6970ad3 NB |
508 | if (!bio) |
509 | goto fail_put_bios; | |
510 | ||
5981c245 | 511 | refcount_inc(&ibr->pending); |
f6970ad3 NB |
512 | bio_list_add(&list, bio); |
513 | } | |
514 | ||
515 | /* Always in 512 byte units for Linux/Block */ | |
516 | block_lba += sg->length >> IBLOCK_LBA_SHIFT; | |
517 | sectors -= 1; | |
518 | } | |
519 | ||
4e49ea4a | 520 | iblock_submit_bios(&list); |
f6970ad3 NB |
521 | return 0; |
522 | ||
523 | fail_put_bios: | |
524 | while ((bio = bio_list_pop(&list))) | |
525 | bio_put(bio); | |
526 | fail_free_ibr: | |
527 | kfree(ibr); | |
528 | fail: | |
529 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
530 | } | |
531 | ||
c66ac9db | 532 | enum { |
44bfd018 | 533 | Opt_udev_path, Opt_readonly, Opt_force, Opt_err |
c66ac9db NB |
534 | }; |
535 | ||
536 | static match_table_t tokens = { | |
537 | {Opt_udev_path, "udev_path=%s"}, | |
44bfd018 | 538 | {Opt_readonly, "readonly=%d"}, |
c66ac9db NB |
539 | {Opt_force, "force=%d"}, |
540 | {Opt_err, NULL} | |
541 | }; | |
542 | ||
0fd97ccf CH |
543 | static ssize_t iblock_set_configfs_dev_params(struct se_device *dev, |
544 | const char *page, ssize_t count) | |
c66ac9db | 545 | { |
0fd97ccf | 546 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); |
6d180253 | 547 | char *orig, *ptr, *arg_p, *opts; |
c66ac9db | 548 | substring_t args[MAX_OPT_ARGS]; |
21bca31c | 549 | int ret = 0, token; |
44bfd018 | 550 | unsigned long tmp_readonly; |
c66ac9db NB |
551 | |
552 | opts = kstrdup(page, GFP_KERNEL); | |
553 | if (!opts) | |
554 | return -ENOMEM; | |
555 | ||
556 | orig = opts; | |
557 | ||
90c161b6 | 558 | while ((ptr = strsep(&opts, ",\n")) != NULL) { |
c66ac9db NB |
559 | if (!*ptr) |
560 | continue; | |
561 | ||
562 | token = match_token(ptr, tokens, args); | |
563 | switch (token) { | |
564 | case Opt_udev_path: | |
565 | if (ib_dev->ibd_bd) { | |
6708bb27 | 566 | pr_err("Unable to set udev_path= while" |
c66ac9db NB |
567 | " ib_dev->ibd_bd exists\n"); |
568 | ret = -EEXIST; | |
569 | goto out; | |
570 | } | |
852b6ed1 NB |
571 | if (match_strlcpy(ib_dev->ibd_udev_path, &args[0], |
572 | SE_UDEV_PATH_LEN) == 0) { | |
573 | ret = -EINVAL; | |
6d180253 JJ |
574 | break; |
575 | } | |
6708bb27 | 576 | pr_debug("IBLOCK: Referencing UDEV path: %s\n", |
c66ac9db NB |
577 | ib_dev->ibd_udev_path); |
578 | ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH; | |
579 | break; | |
44bfd018 AG |
580 | case Opt_readonly: |
581 | arg_p = match_strdup(&args[0]); | |
582 | if (!arg_p) { | |
583 | ret = -ENOMEM; | |
584 | break; | |
585 | } | |
57103d7f | 586 | ret = kstrtoul(arg_p, 0, &tmp_readonly); |
44bfd018 AG |
587 | kfree(arg_p); |
588 | if (ret < 0) { | |
57103d7f | 589 | pr_err("kstrtoul() failed for" |
44bfd018 AG |
590 | " readonly=\n"); |
591 | goto out; | |
592 | } | |
593 | ib_dev->ibd_readonly = tmp_readonly; | |
594 | pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly); | |
595 | break; | |
c66ac9db | 596 | case Opt_force: |
c66ac9db NB |
597 | break; |
598 | default: | |
599 | break; | |
600 | } | |
601 | } | |
602 | ||
603 | out: | |
604 | kfree(orig); | |
605 | return (!ret) ? count : ret; | |
606 | } | |
607 | ||
0fd97ccf | 608 | static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b) |
c66ac9db | 609 | { |
0fd97ccf CH |
610 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); |
611 | struct block_device *bd = ib_dev->ibd_bd; | |
c66ac9db NB |
612 | char buf[BDEVNAME_SIZE]; |
613 | ssize_t bl = 0; | |
614 | ||
615 | if (bd) | |
616 | bl += sprintf(b + bl, "iBlock device: %s", | |
617 | bdevname(bd, buf)); | |
0fd97ccf | 618 | if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH) |
44bfd018 | 619 | bl += sprintf(b + bl, " UDEV PATH: %s", |
0fd97ccf CH |
620 | ib_dev->ibd_udev_path); |
621 | bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly); | |
c66ac9db NB |
622 | |
623 | bl += sprintf(b + bl, " "); | |
624 | if (bd) { | |
625 | bl += sprintf(b + bl, "Major: %d Minor: %d %s\n", | |
21bca31c | 626 | MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ? |
0fd97ccf | 627 | "" : (bd->bd_holder == ib_dev) ? |
c66ac9db NB |
628 | "CLAIMED: IBLOCK" : "CLAIMED: OS"); |
629 | } else { | |
21bca31c | 630 | bl += sprintf(b + bl, "Major: 0 Minor: 0\n"); |
c66ac9db NB |
631 | } |
632 | ||
633 | return bl; | |
634 | } | |
635 | ||
ecebbf6c NB |
636 | static int |
637 | iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio) | |
638 | { | |
639 | struct se_device *dev = cmd->se_dev; | |
640 | struct blk_integrity *bi; | |
641 | struct bio_integrity_payload *bip; | |
642 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
643 | struct scatterlist *sg; | |
644 | int i, rc; | |
645 | ||
646 | bi = bdev_get_integrity(ib_dev->ibd_bd); | |
647 | if (!bi) { | |
648 | pr_err("Unable to locate bio_integrity\n"); | |
649 | return -ENODEV; | |
650 | } | |
651 | ||
652 | bip = bio_integrity_alloc(bio, GFP_NOIO, cmd->t_prot_nents); | |
06c1e390 | 653 | if (IS_ERR(bip)) { |
ecebbf6c | 654 | pr_err("Unable to allocate bio_integrity_payload\n"); |
06c1e390 | 655 | return PTR_ERR(bip); |
ecebbf6c NB |
656 | } |
657 | ||
4e13c5d0 | 658 | bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) * |
ecebbf6c | 659 | dev->prot_length; |
4e13c5d0 | 660 | bip->bip_iter.bi_sector = bio->bi_iter.bi_sector; |
ecebbf6c | 661 | |
4e13c5d0 LT |
662 | pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size, |
663 | (unsigned long long)bip->bip_iter.bi_sector); | |
ecebbf6c NB |
664 | |
665 | for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) { | |
666 | ||
667 | rc = bio_integrity_add_page(bio, sg_page(sg), sg->length, | |
668 | sg->offset); | |
669 | if (rc != sg->length) { | |
670 | pr_err("bio_integrity_add_page() failed; %d\n", rc); | |
671 | return -ENOMEM; | |
672 | } | |
673 | ||
674 | pr_debug("Added bio integrity page: %p length: %d offset; %d\n", | |
675 | sg_page(sg), sg->length, sg->offset); | |
676 | } | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
de103c93 | 681 | static sense_reason_t |
a82a9538 NB |
682 | iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, |
683 | enum dma_data_direction data_direction) | |
c66ac9db | 684 | { |
5951146d | 685 | struct se_device *dev = cmd->se_dev; |
8a9ebe71 | 686 | sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); |
5787cacd | 687 | struct iblock_req *ibr; |
ecebbf6c | 688 | struct bio *bio, *bio_start; |
dbbf3e94 | 689 | struct bio_list list; |
c66ac9db | 690 | struct scatterlist *sg; |
5787cacd | 691 | u32 sg_num = sgl_nents; |
d5b4a21b | 692 | unsigned bio_cnt; |
e742fc32 | 693 | int i, op, op_flags = 0; |
dbbf3e94 | 694 | |
5787cacd | 695 | if (data_direction == DMA_TO_DEVICE) { |
d0c8b259 NB |
696 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); |
697 | struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd); | |
dbbf3e94 | 698 | /* |
70fd7614 | 699 | * Force writethrough using REQ_FUA if a volatile write cache |
d0c8b259 | 700 | * is not enabled, or if initiator set the Force Unit Access bit. |
dbbf3e94 | 701 | */ |
e742fc32 | 702 | op = REQ_OP_WRITE; |
c888a8f9 | 703 | if (test_bit(QUEUE_FLAG_FUA, &q->queue_flags)) { |
d0c8b259 | 704 | if (cmd->se_cmd_flags & SCF_FUA) |
70fd7614 | 705 | op_flags = REQ_FUA; |
c888a8f9 | 706 | else if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) |
70fd7614 | 707 | op_flags = REQ_FUA; |
d0c8b259 | 708 | } |
dbbf3e94 | 709 | } else { |
e742fc32 | 710 | op = REQ_OP_READ; |
dbbf3e94 CH |
711 | } |
712 | ||
5787cacd CH |
713 | ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); |
714 | if (!ibr) | |
715 | goto fail; | |
716 | cmd->priv = ibr; | |
717 | ||
e0de4457 | 718 | if (!sgl_nents) { |
5981c245 | 719 | refcount_set(&ibr->pending, 1); |
e0de4457 PB |
720 | iblock_complete_cmd(cmd); |
721 | return 0; | |
722 | } | |
723 | ||
e742fc32 | 724 | bio = iblock_get_bio(cmd, block_lba, sgl_nents, op, op_flags); |
5787cacd CH |
725 | if (!bio) |
726 | goto fail_free_ibr; | |
dbbf3e94 | 727 | |
ecebbf6c | 728 | bio_start = bio; |
dbbf3e94 CH |
729 | bio_list_init(&list); |
730 | bio_list_add(&list, bio); | |
5787cacd | 731 | |
5981c245 | 732 | refcount_set(&ibr->pending, 2); |
d5b4a21b | 733 | bio_cnt = 1; |
c66ac9db | 734 | |
5787cacd | 735 | for_each_sg(sgl, sg, sgl_nents, i) { |
dbbf3e94 CH |
736 | /* |
737 | * XXX: if the length the device accepts is shorter than the | |
738 | * length of the S/G list entry this will cause and | |
739 | * endless loop. Better hope no driver uses huge pages. | |
740 | */ | |
741 | while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) | |
742 | != sg->length) { | |
d5b4a21b | 743 | if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) { |
4e49ea4a | 744 | iblock_submit_bios(&list); |
d5b4a21b CH |
745 | bio_cnt = 0; |
746 | } | |
747 | ||
e742fc32 MC |
748 | bio = iblock_get_bio(cmd, block_lba, sg_num, op, |
749 | op_flags); | |
6708bb27 | 750 | if (!bio) |
5787cacd CH |
751 | goto fail_put_bios; |
752 | ||
5981c245 | 753 | refcount_inc(&ibr->pending); |
dbbf3e94 | 754 | bio_list_add(&list, bio); |
d5b4a21b | 755 | bio_cnt++; |
c66ac9db | 756 | } |
dbbf3e94 | 757 | |
c66ac9db NB |
758 | /* Always in 512 byte units for Linux/Block */ |
759 | block_lba += sg->length >> IBLOCK_LBA_SHIFT; | |
760 | sg_num--; | |
c66ac9db NB |
761 | } |
762 | ||
6f16ec43 | 763 | if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { |
ecebbf6c NB |
764 | int rc = iblock_alloc_bip(cmd, bio_start); |
765 | if (rc) | |
766 | goto fail_put_bios; | |
767 | } | |
768 | ||
4e49ea4a | 769 | iblock_submit_bios(&list); |
5787cacd | 770 | iblock_complete_cmd(cmd); |
03e98c9e | 771 | return 0; |
dbbf3e94 | 772 | |
5787cacd | 773 | fail_put_bios: |
dbbf3e94 | 774 | while ((bio = bio_list_pop(&list))) |
c66ac9db | 775 | bio_put(bio); |
5787cacd CH |
776 | fail_free_ibr: |
777 | kfree(ibr); | |
5787cacd | 778 | fail: |
de103c93 | 779 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
c66ac9db NB |
780 | } |
781 | ||
c66ac9db NB |
782 | static sector_t iblock_get_blocks(struct se_device *dev) |
783 | { | |
0fd97ccf CH |
784 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); |
785 | struct block_device *bd = ib_dev->ibd_bd; | |
c66ac9db NB |
786 | struct request_queue *q = bdev_get_queue(bd); |
787 | ||
788 | return iblock_emulate_read_cap_with_block_size(dev, bd, q); | |
789 | } | |
790 | ||
7f7caf6a AG |
791 | static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev) |
792 | { | |
793 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
794 | struct block_device *bd = ib_dev->ibd_bd; | |
795 | int ret; | |
796 | ||
797 | ret = bdev_alignment_offset(bd); | |
798 | if (ret == -1) | |
799 | return 0; | |
800 | ||
801 | /* convert offset-bytes to offset-lbas */ | |
802 | return ret / bdev_logical_block_size(bd); | |
803 | } | |
804 | ||
805 | static unsigned int iblock_get_lbppbe(struct se_device *dev) | |
806 | { | |
807 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
808 | struct block_device *bd = ib_dev->ibd_bd; | |
809 | int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd); | |
810 | ||
811 | return ilog2(logs_per_phys); | |
812 | } | |
813 | ||
814 | static unsigned int iblock_get_io_min(struct se_device *dev) | |
815 | { | |
816 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
817 | struct block_device *bd = ib_dev->ibd_bd; | |
818 | ||
819 | return bdev_io_min(bd); | |
820 | } | |
821 | ||
822 | static unsigned int iblock_get_io_opt(struct se_device *dev) | |
823 | { | |
824 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
825 | struct block_device *bd = ib_dev->ibd_bd; | |
826 | ||
827 | return bdev_io_opt(bd); | |
828 | } | |
829 | ||
9e999a6c | 830 | static struct sbc_ops iblock_sbc_ops = { |
0c2ad7d1 | 831 | .execute_rw = iblock_execute_rw, |
ad67f0d9 | 832 | .execute_sync_cache = iblock_execute_sync_cache, |
6f974e8c | 833 | .execute_write_same = iblock_execute_write_same, |
14150a6b | 834 | .execute_unmap = iblock_execute_unmap, |
0c2ad7d1 CH |
835 | }; |
836 | ||
de103c93 CH |
837 | static sense_reason_t |
838 | iblock_parse_cdb(struct se_cmd *cmd) | |
0c2ad7d1 | 839 | { |
9e999a6c | 840 | return sbc_parse_cdb(cmd, &iblock_sbc_ops); |
0c2ad7d1 CH |
841 | } |
842 | ||
452e2010 | 843 | static bool iblock_get_write_cache(struct se_device *dev) |
d0c8b259 NB |
844 | { |
845 | struct iblock_dev *ib_dev = IBLOCK_DEV(dev); | |
846 | struct block_device *bd = ib_dev->ibd_bd; | |
847 | struct request_queue *q = bdev_get_queue(bd); | |
848 | ||
c888a8f9 | 849 | return test_bit(QUEUE_FLAG_WC, &q->queue_flags); |
d0c8b259 NB |
850 | } |
851 | ||
0a06d430 | 852 | static const struct target_backend_ops iblock_ops = { |
c66ac9db | 853 | .name = "iblock", |
0fd97ccf CH |
854 | .inquiry_prod = "IBLOCK", |
855 | .inquiry_rev = IBLOCK_VERSION, | |
c66ac9db | 856 | .owner = THIS_MODULE, |
c66ac9db NB |
857 | .attach_hba = iblock_attach_hba, |
858 | .detach_hba = iblock_detach_hba, | |
0fd97ccf CH |
859 | .alloc_device = iblock_alloc_device, |
860 | .configure_device = iblock_configure_device, | |
c66ac9db | 861 | .free_device = iblock_free_device, |
0c2ad7d1 | 862 | .parse_cdb = iblock_parse_cdb, |
c66ac9db NB |
863 | .set_configfs_dev_params = iblock_set_configfs_dev_params, |
864 | .show_configfs_dev_params = iblock_show_configfs_dev_params, | |
6f23ac8a | 865 | .get_device_type = sbc_get_device_type, |
c66ac9db | 866 | .get_blocks = iblock_get_blocks, |
7f7caf6a AG |
867 | .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas, |
868 | .get_lbppbe = iblock_get_lbppbe, | |
869 | .get_io_min = iblock_get_io_min, | |
870 | .get_io_opt = iblock_get_io_opt, | |
d0c8b259 | 871 | .get_write_cache = iblock_get_write_cache, |
5873c4d1 | 872 | .tb_dev_attrib_attrs = sbc_attrib_attrs, |
c66ac9db NB |
873 | }; |
874 | ||
875 | static int __init iblock_module_init(void) | |
876 | { | |
0a06d430 | 877 | return transport_backend_register(&iblock_ops); |
c66ac9db NB |
878 | } |
879 | ||
63b91d5a | 880 | static void __exit iblock_module_exit(void) |
c66ac9db | 881 | { |
0a06d430 | 882 | target_backend_unregister(&iblock_ops); |
c66ac9db NB |
883 | } |
884 | ||
885 | MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); | |
886 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); | |
887 | MODULE_LICENSE("GPL"); | |
888 | ||
889 | module_init(iblock_module_init); | |
890 | module_exit(iblock_module_exit); |