File: bs_aio.c

package info (click to toggle)
tgt 1%3A1.0.85-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,784 kB
  • sloc: ansic: 28,398; perl: 3,825; xml: 1,867; sh: 1,271; makefile: 301
file content (472 lines) | stat: -rw-r--r-- 11,510 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * AIO backing store
 *
 * Copyright (C) 2006-2007 FUJITA Tomonori <tomof@acm.org>
 * Copyright (C) 2006-2007 Mike Christie <michaelc@cs.wisc.edu>
 * Copyright (C) 2011 Alexander Nezhinsky <alexandern@mellanox.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/fs.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/eventfd.h>
#include <libaio.h>

#include "list.h"
#include "util.h"
#include "tgtd.h"
#include "target.h"
#include "scsi.h"

#ifndef O_DIRECT
#define O_DIRECT 040000
#endif

#define AIO_MAX_IODEPTH    128

struct bs_aio_info {
	struct list_head dev_list_entry;
	io_context_t ctx;

	struct list_head cmd_wait_list;
	unsigned int nwaiting;
	unsigned int npending;
	unsigned int iodepth;

	int resubmit;

	struct scsi_lu *lu;
	int evt_fd;

	struct iocb iocb_arr[AIO_MAX_IODEPTH];
	struct iocb *piocb_arr[AIO_MAX_IODEPTH];
	struct io_event io_evts[AIO_MAX_IODEPTH];
};

static struct list_head bs_aio_dev_list = LIST_HEAD_INIT(bs_aio_dev_list);

static inline struct bs_aio_info *BS_AIO_I(struct scsi_lu *lu)
{
	return (struct bs_aio_info *) ((char *)lu + sizeof(*lu));
}

static void bs_aio_iocb_prep(struct bs_aio_info *info, int idx,
			     struct scsi_cmd *cmd)
{
	struct iocb *iocb = &info->iocb_arr[idx];
	unsigned int scsi_op = (unsigned int)cmd->scb[0];

	iocb->data = cmd;
	iocb->key = 0;
	iocb->aio_reqprio = 0;
	iocb->aio_fildes = info->lu->fd;

	switch (scsi_op) {
	case WRITE_6:
	case WRITE_10:
	case WRITE_12:
	case WRITE_16:
		iocb->aio_lio_opcode = IO_CMD_PWRITE;
		iocb->u.c.buf = scsi_get_out_buffer(cmd);
		iocb->u.c.nbytes = scsi_get_out_length(cmd);

		dprintf("prep WR cmd:%p op:%x buf:0x%p sz:%lx\n",
			cmd, scsi_op, iocb->u.c.buf, iocb->u.c.nbytes);
		break;

	case READ_6:
	case READ_10:
	case READ_12:
	case READ_16:
		iocb->aio_lio_opcode = IO_CMD_PREAD;
		iocb->u.c.buf = scsi_get_in_buffer(cmd);
		iocb->u.c.nbytes = scsi_get_in_length(cmd);

		dprintf("prep RD cmd:%p op:%x buf:0x%p sz:%lx\n",
			cmd, scsi_op, iocb->u.c.buf, iocb->u.c.nbytes);
		break;

	default:
		return;
	}

	iocb->u.c.offset = cmd->offset;
	iocb->u.c.flags |= (1 << 0); /* IOCB_FLAG_RESFD - use eventfd file desc. */
	iocb->u.c.resfd = info->evt_fd;
}

static int bs_aio_submit_dev_batch(struct bs_aio_info *info)
{
	int nsubmit, nsuccess;
	struct scsi_cmd *cmd, *next;
	int i = 0;

	nsubmit = info->iodepth - info->npending; /* max allowed to submit */
	if (nsubmit > info->nwaiting)
		nsubmit = info->nwaiting;

	dprintf("nsubmit:%d waiting:%d pending:%d, tgt:%d lun:%"PRId64 "\n",
		nsubmit, info->nwaiting, info->npending,
		info->lu->tgt->tid, info->lu->lun);

	if (!nsubmit)
		return 0;

	list_for_each_entry_safe(cmd, next, &info->cmd_wait_list, bs_list) {
		bs_aio_iocb_prep(info, i, cmd);
		list_del(&cmd->bs_list);
		if (++i == nsubmit)
			break;
	}

	nsuccess = io_submit(info->ctx, nsubmit, info->piocb_arr);
	if (unlikely(nsuccess < 0)) {
		if (nsuccess == -EAGAIN) {
			eprintf("delayed submit %d cmds to tgt:%d lun:%"PRId64 "\n",
				nsubmit, info->lu->tgt->tid, info->lu->lun);
			nsuccess = 0; /* leave the dev pending with all cmds */
		}
		else {
			eprintf("failed to submit %d cmds to tgt:%d lun:%"PRId64
				", err: %d\n",
				nsubmit, info->lu->tgt->tid,
				info->lu->lun, -nsuccess);
			for (i = nsubmit - 1; i >= 0; i--) {
				cmd = info->iocb_arr[i].data;
				clear_cmd_async(cmd);
				info->nwaiting--;
				if (!info->nwaiting)
					list_del(&info->dev_list_entry);
			}
			return nsuccess;
		}
	}
	if (unlikely(nsuccess < nsubmit)) {
		for (i=nsubmit-1; i >= nsuccess; i--) {
			cmd = info->iocb_arr[i].data;
			list_add(&cmd->bs_list, &info->cmd_wait_list);
		}
	}

	info->npending += nsuccess;
	info->nwaiting -= nsuccess;
	/* if no cmds remain, remove the dev from the pending list */
	if (likely(!info->nwaiting))
			list_del(&info->dev_list_entry);

	dprintf("submitted %d of %d cmds to tgt:%d lun:%"PRId64
		", waiting:%d pending:%d\n",
		nsuccess, nsubmit, info->lu->tgt->tid, info->lu->lun,
		info->nwaiting, info->npending);
	return 0;
}

static int bs_aio_submit_all_devs(void)
{
	struct bs_aio_info *dev_info, *next_dev;
	int err;

	/* pass over all devices having some queued cmds and submit */
	list_for_each_entry_safe(dev_info, next_dev, &bs_aio_dev_list, dev_list_entry) {
		err = bs_aio_submit_dev_batch(dev_info);
		if (unlikely(err))
			return err;
	}
	return 0;
}

static int bs_aio_cmd_submit(struct scsi_cmd *cmd)
{
	struct scsi_lu *lu = cmd->dev;
	struct bs_aio_info *info = BS_AIO_I(lu);
	unsigned int scsi_op = (unsigned int)cmd->scb[0];

	switch (scsi_op) {
	case WRITE_6:
	case WRITE_10:
	case WRITE_12:
	case WRITE_16:
	case READ_6:
	case READ_10:
	case READ_12:
	case READ_16:
		break;

	case WRITE_SAME:
	case WRITE_SAME_16:
		eprintf("WRITE_SAME not yet supported for AIO backend.\n");
		return -1;

	case SYNCHRONIZE_CACHE:
	case SYNCHRONIZE_CACHE_16:
	default:
		dprintf("skipped cmd:%p op:%x\n", cmd, scsi_op);
		return 0;
	}

	list_add_tail(&cmd->bs_list, &info->cmd_wait_list);
	if (!info->nwaiting)
		list_add_tail(&info->dev_list_entry, &bs_aio_dev_list);
	info->nwaiting++;
	set_cmd_async(cmd);

	if (!cmd_not_last(cmd)) /* last cmd in batch */
		return bs_aio_submit_all_devs();

	if (info->nwaiting == info->iodepth - info->npending)
		return bs_aio_submit_dev_batch(info);

	return 0;
}

static void bs_aio_complete_one(struct io_event *ep)
{
	struct scsi_cmd *cmd = (void *)(unsigned long)ep->data;
	uint32_t length;
	int result;

	switch (cmd->scb[0]) {
	case WRITE_6:
	case WRITE_10:
	case WRITE_12:
	case WRITE_16:
		length = scsi_get_out_length(cmd);
		break;
	default:
		length = scsi_get_in_length(cmd);
		break;
	}

	if (likely(ep->res == length))
		result = SAM_STAT_GOOD;
	else {
		sense_data_build(cmd, MEDIUM_ERROR, 0);
		result = SAM_STAT_CHECK_CONDITION;
	}
	dprintf("cmd: %p\n", cmd);
	target_cmd_io_done(cmd, result);
}

static void bs_aio_get_completions(int fd, int events, void *data)
{
	struct bs_aio_info *info = data;
	int i, ret;
	/* read from eventfd returns 8-byte int, fails with the error EINVAL
	   if the size of the supplied buffer is less than 8 bytes */
	uint64_t evts_complete;
	unsigned int ncomplete, nevents;

retry_read:
	ret = read(info->evt_fd, &evts_complete, sizeof(evts_complete));
	if (unlikely(ret < 0)) {
		eprintf("failed to read AIO completions, %m\n");
		if (errno == EAGAIN || errno == EINTR)
			goto retry_read;

		return;
	}
	ncomplete = (unsigned int) evts_complete;

	while (ncomplete) {
		nevents = min_t(unsigned int, ncomplete, ARRAY_SIZE(info->io_evts));
retry_getevts:
		ret = io_getevents(info->ctx, 1, nevents, info->io_evts, NULL);
		if (likely(ret > 0)) {
			nevents = ret;
			info->npending -= nevents;
		} else {
			if (ret == -EINTR)
				goto retry_getevts;
			eprintf("io_getevents failed, err:%d\n", -ret);
			return;
		}
		dprintf("got %d ioevents out of %d, pending %d\n",
			nevents, ncomplete, info->npending);

		for (i = 0; i < nevents; i++)
			bs_aio_complete_one(&info->io_evts[i]);
		ncomplete -= nevents;
	}

	if (info->nwaiting) {
		dprintf("submit waiting cmds to tgt:%d lun:%"PRId64 "\n",
			info->lu->tgt->tid, info->lu->lun);
		bs_aio_submit_dev_batch(info);
	}
}

static int bs_aio_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size)
{
	struct bs_aio_info *info = BS_AIO_I(lu);
	int ret, afd;
	uint32_t blksize = 0;

	info->iodepth = AIO_MAX_IODEPTH;
	eprintf("create aio context for tgt:%d lun:%"PRId64 ", max iodepth:%d\n",
		info->lu->tgt->tid, info->lu->lun, info->iodepth);
	ret = io_setup(info->iodepth, &info->ctx);
	if (ret) {
		eprintf("failed to create aio context, %m\n");
		return -1;
	}

	afd = eventfd(0, O_NONBLOCK);
	if (afd < 0) {
		eprintf("failed to create eventfd for tgt:%d lun:%"PRId64 ", %m\n",
			info->lu->tgt->tid, info->lu->lun);
		ret = afd;
		goto close_ctx;
	}
	dprintf("eventfd:%d for tgt:%d lun:%"PRId64 "\n",
		afd, info->lu->tgt->tid, info->lu->lun);

	ret = tgt_event_add(afd, EPOLLIN, bs_aio_get_completions, info);
	if (ret)
		goto close_eventfd;
	info->evt_fd = afd;

	eprintf("open %s, RW, O_DIRECT for tgt:%d lun:%"PRId64 "\n",
		path, info->lu->tgt->tid, info->lu->lun);
	*fd = backed_file_open(path, O_RDWR|O_LARGEFILE|O_DIRECT, size,
				&blksize);
	/* If we get access denied, try opening the file in readonly mode */
	if (*fd == -1 && (errno == EACCES || errno == EROFS)) {
		eprintf("open %s, READONLY, O_DIRECT for tgt:%d lun:%"PRId64 "\n",
			path, info->lu->tgt->tid, info->lu->lun);
		*fd = backed_file_open(path, O_RDONLY|O_LARGEFILE|O_DIRECT,
				       size, &blksize);
		lu->attrs.readonly = 1;
	}
	if (*fd < 0) {
		eprintf("failed to open %s, for tgt:%d lun:%"PRId64 ", %m\n",
			path, info->lu->tgt->tid, info->lu->lun);
		ret = *fd;
		goto remove_tgt_evt;
	}

	eprintf("%s opened successfully for tgt:%d lun:%"PRId64 "\n",
		path, info->lu->tgt->tid, info->lu->lun);

	if (!lu->attrs.no_auto_lbppbe)
		update_lbppbe(lu, blksize);

	return 0;

remove_tgt_evt:
	tgt_event_del(afd);
close_eventfd:
	close(afd);
close_ctx:
	io_destroy(info->ctx);
	return ret;
}

static void bs_aio_close(struct scsi_lu *lu)
{
	close(lu->fd);
}

static tgtadm_err bs_aio_init(struct scsi_lu *lu, char *bsopts)
{
	struct bs_aio_info *info = BS_AIO_I(lu);
	int i;

	memset(info, 0, sizeof(*info));
	INIT_LIST_HEAD(&info->dev_list_entry);
	INIT_LIST_HEAD(&info->cmd_wait_list);
	info->lu = lu;

	for (i=0; i < ARRAY_SIZE(info->iocb_arr); i++)
		info->piocb_arr[i] = &info->iocb_arr[i];

	return TGTADM_SUCCESS;
}

static void bs_aio_exit(struct scsi_lu *lu)
{
	struct bs_aio_info *info = BS_AIO_I(lu);

	tgt_event_del(info->evt_fd);
	close(info->evt_fd);
	io_destroy(info->ctx);
}

static struct backingstore_template aio_bst = {
	.bs_name		= "aio",
	.bs_datasize    	= sizeof(struct bs_aio_info),
	.bs_init		= bs_aio_init,
	.bs_exit		= bs_aio_exit,
	.bs_open		= bs_aio_open,
	.bs_close       	= bs_aio_close,
	.bs_cmd_submit  	= bs_aio_cmd_submit,
};

__attribute__((constructor)) static void register_bs_module(void)
{
	unsigned char opcodes[] = {
		ALLOW_MEDIUM_REMOVAL,
		COMPARE_AND_WRITE,
		FORMAT_UNIT,
		INQUIRY,
		MAINT_PROTOCOL_IN,
		MODE_SELECT,
		MODE_SELECT_10,
		MODE_SENSE,
		MODE_SENSE_10,
		ORWRITE_16,
		PERSISTENT_RESERVE_IN,
		PERSISTENT_RESERVE_OUT,
		PRE_FETCH_10,
		PRE_FETCH_16,
		READ_10,
		READ_12,
		READ_16,
		READ_6,
		READ_CAPACITY,
		RELEASE,
		REPORT_LUNS,
		REQUEST_SENSE,
		RESERVE,
		SEND_DIAGNOSTIC,
		SERVICE_ACTION_IN,
		START_STOP,
		SYNCHRONIZE_CACHE,
		SYNCHRONIZE_CACHE_16,
		TEST_UNIT_READY,
		UNMAP,
		VERIFY_10,
		VERIFY_12,
		VERIFY_16,
		WRITE_10,
		WRITE_12,
		WRITE_16,
		WRITE_6,
		WRITE_VERIFY,
		WRITE_VERIFY_12,
		WRITE_VERIFY_16
	};
	bs_create_opcode_map(&aio_bst, opcodes, ARRAY_SIZE(opcodes));
	register_backingstore_template(&aio_bst);
}