This repository was archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdevice.c
More file actions
1573 lines (1340 loc) · 36.8 KB
/
device.c
File metadata and controls
1573 lines (1340 loc) · 36.8 KB
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "ggaoed.h"
#include "util.h"
#include <sys/types.h>
#include <netinet/ether.h>
#include <linux/hdreg.h>
#include <linux/fs.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <libaio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
/**********************************************************************
* Definitions
*/
/* Number of I/O events to submit/receive in one system call */
#define EVENT_BATCH 32
/**********************************************************************
* Forward declarations
*/
static void dev_io(uint32_t events, void *data);
static void dev_timer(uint32_t events, void *data);
static void run_queue(struct device *dev);
static void do_ata_cmd(struct device *dev, struct queue_item *q);
static void do_cfg_cmd(struct device *dev, struct queue_item *q);
static void do_macmask_cmd(struct device *dev, struct queue_item *q);
static void do_reserve_cmd(struct device *dev, struct queue_item *q);
static void trace_ata(const struct device *dev, const struct queue_item *q);
static void trace_cfg(const struct device *dev, const struct queue_item *q);
static void trace_macmask(const struct device *dev, const struct queue_item *q);
static void trace_reserve(const struct device *dev, const struct queue_item *q);
static void activate_dev(struct device *dev);
/**********************************************************************
* Global variables
*/
/* List of all configured devices */
GPtrArray *devices;
#define ATACMD(x) [WIN_ ## x] = #x
static const char *const ata_cmds[256] =
{
ATACMD(READ),
ATACMD(READ_EXT),
ATACMD(WRITE),
ATACMD(WRITE_EXT),
ATACMD(PACKETCMD),
ATACMD(SMART),
ATACMD(FLUSH_CACHE),
ATACMD(FLUSH_CACHE_EXT),
ATACMD(IDENTIFY)
};
#define CFGCMD(x) [AOE_CFG_ ## x] = #x
static const char *const cfg_cmds[16] =
{
CFGCMD(READ),
CFGCMD(TEST),
CFGCMD(TEST_PREFIX),
CFGCMD(SET),
CFGCMD(FORCE_SET)
};
#define MACMASKCMD(x) [AOE_MCMD_ ## x] = #x
static const char *const macmask_cmds[256] =
{
MACMASKCMD(READ),
MACMASKCMD(EDIT)
};
#define RESERVECMD(x) [AOE_RESERVE_ ## x] = #x
static const char *const reserve_cmds[256] =
{
RESERVECMD(READ),
RESERVECMD(SET),
RESERVECMD(FORCESET)
};
struct cmd_info
{
unsigned header_length;
void (*process)(struct device *dev, struct queue_item *q);
void (*trace)(const struct device *dev, const struct queue_item *q);
};
static const struct cmd_info aoe_cmds[] =
{
[AOE_CMD_ATA] =
{
sizeof(struct aoe_ata_hdr),
do_ata_cmd,
trace_ata
},
[AOE_CMD_CFG] =
{
sizeof(struct aoe_cfg_hdr),
do_cfg_cmd,
trace_cfg
},
[AOE_CMD_MASK] =
{
sizeof(struct aoe_macmask_hdr),
do_macmask_cmd,
trace_macmask
},
[AOE_CMD_RESERVE] =
{
sizeof(struct aoe_reserve_hdr),
do_reserve_cmd,
trace_reserve
},
};
GQueue active_devs;
/**********************************************************************
* Misc. helpers
*/
static struct queue_item *new_request(struct device *dev, struct netif *iface,
void *buf, unsigned length, const struct timespec *tv)
{
struct queue_item *q;
q = g_slice_new0(struct queue_item);
q->dev = dev;
q->iface = iface;
q->buf = buf;
q->bufsize = iface->mtu;
q->length = length;
if (tv)
q->start = *tv;
else
clock_gettime(CLOCK_REALTIME, &q->start);
++dev->queue_length;
return q;
}
static struct queue_item *queue_get(struct device *dev, struct netif *iface,
void *buf, unsigned length, const struct timespec *tv)
{
if (dev->queue_length >= dev->cfg.queue_length)
++dev->stats.queue_over;
dev->stats.queue_length += dev->queue_length;
return new_request(dev, iface, buf, length, tv);
}
/* Invalidate the buffer of the request */
static inline void drop_buffer(struct queue_item *q)
{
if (q->dynalloc)
{
free_packet(q->buf, q->bufsize);
q->dynalloc = FALSE;
}
q->length = 0;
}
/* Drop a request without sending a reply */
void drop_request(struct queue_item *q)
{
struct timespec now, len, *dst;
drop_buffer(q);
if (q->dev)
{
struct device *const dev = q->dev;
--dev->queue_length;
/* Update queue statistics */
if (q->start.tv_sec)
{
clock_gettime(CLOCK_REALTIME, &now);
timespec_sub(&now, &q->start, &len);
if (!q->is_ata)
dst = &dev->stats.other_time;
else if (q->is_write)
dst = &dev->stats.write_time;
else
dst = &dev->stats.read_time;
timespec_add(dst, &len, dst);
}
}
g_slice_free(struct queue_item, q);
}
/* Copy the contents of the original request to a dynamically allocated buffer */
static int clone_pkt(struct queue_item *q)
{
void *pkt;
pkt = alloc_packet(q->bufsize);
if (!pkt)
return -1;
memcpy(pkt, q->buf + q->hdrlen, q->length - q->hdrlen);
q->length -= q->hdrlen;
q->buf = pkt;
q->dynalloc = TRUE;
return 0;
}
static inline unsigned max_sect_nr(const struct netif *iface)
{
return (iface->mtu - sizeof(struct aoe_ata_hdr)) >> 9;
}
/**********************************************************************
* Allocate/deallocate devices
*/
static void free_dev(struct device *dev)
{
g_free(dev->name);
if (dev->fd != -1)
close(dev->fd);
if (dev->event_fd != -1)
{
del_fd(dev->event_fd);
close(dev->event_fd);
}
if (dev->timer_fd != -1)
{
del_fd(dev->timer_fd);
close(dev->timer_fd);
}
if (dev->aoe_conf && dev->aoe_conf != MAP_FAILED)
munmap(dev->aoe_conf, sizeof(*dev->aoe_conf));
if (dev->mac_mask && dev->mac_mask != MAP_FAILED)
munmap(dev->mac_mask, sizeof(*dev->mac_mask));
if (dev->reserve && dev->reserve != MAP_FAILED)
munmap(dev->reserve, sizeof(*dev->reserve));
g_ptr_array_free(dev->ifaces, TRUE);
g_ptr_array_free(dev->deferred, TRUE);
destroy_device_config(&dev->cfg);
g_slice_free(struct device, dev);
}
static void *open_and_map(struct device *dev, const char *suffix, size_t length)
{
char *filename;
void *addr;
int fd;
filename = g_strdup_printf("%s/%s.%s", defaults.statedir, dev->name, suffix);
fd = open(filename, O_RDWR | O_CREAT, 0600);
if (fd == -1)
{
deverr(dev, "Failed to open/create %s", filename);
g_free(filename);
return MAP_FAILED;
}
if (ftruncate(fd, length))
{
deverr(dev, "Failed to extend %s", filename);
close(fd);
g_free(filename);
return MAP_FAILED;
}
addr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)
deverr(dev, "Failed to map %s to memory", filename);
close(fd);
g_free(filename);
return addr;
}
static void check_config_map(const struct device *dev, struct config_map *map)
{
if (map->magic == CONFIG_MAP_MAGIC && map->length <= 1024)
return;
devlog(dev, LOG_INFO, "Resetting AoE configuration space");
memset(map, 0, sizeof(*map));
map->magic = CONFIG_MAP_MAGIC;
msync(map, sizeof(*map), MS_ASYNC);
}
static void check_acl_map(const struct device *dev, struct acl_map *map, const char *msg)
{
if (map->magic == ACL_MAP_MAGIC && map->length <= 255)
return;
devlog(dev, LOG_INFO, "Resetting AoE %s list", msg);
memset(map, 0, sizeof(*map));
map->magic = ACL_MAP_MAGIC;
msync(map, sizeof(*map), MS_ASYNC);
}
static int open_dev(struct device *dev, struct device_config *cfg)
{
unsigned long long hsize;
const char *unit;
struct stat st;
int flags;
if (stat(cfg->path, &st))
{
deverr(dev, "stat('%s') failed", cfg->path);
return -1;
}
if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))
{
devlog(dev, LOG_ERR, "Not a device or regular file");
return -1;
}
flags = cfg->read_only ? O_RDONLY : O_RDWR;
if (cfg->direct_io)
flags |= O_DIRECT;
/* Open block devices in exclusive mode */
if (S_ISBLK(st.st_mode))
flags |= O_EXCL;
dev->fd = open(cfg->path, flags);
if (dev->fd == -1)
{
deverr(dev, "Failed to open '%s'", cfg->path);
return -1;
}
if (S_ISBLK(st.st_mode))
{
if (ioctl(dev->fd, BLKGETSIZE64, &dev->size))
{
deverr(dev, "ioctl(BLKGETSIZE64) failed");
return -1;
}
}
else
dev->size = st.st_size;
hsize = human_format(dev->size, &unit);
devlog(dev, LOG_INFO, "Shelf %d, slot %d, path '%s' (size %lld %s, sectors %lld) opened%s%s",
ntohs(cfg->shelf), cfg->slot, cfg->path, hsize, unit,
(long long)dev->size >> 9,
cfg->read_only ? " R/O" : "",
cfg->direct_io ? ", using direct I/O" : "");
return 0;
}
static int validate_dev_fd(struct device *dev, struct device_config *cfg)
{
unsigned long long size, hsize;
const char *unit;
struct stat st;
int ret;
if (dev->cfg.direct_io != cfg->direct_io)
{
long flags = fcntl(dev->fd, F_GETFL);
if (cfg->direct_io)
flags |= O_DIRECT;
else
flags &= ~O_DIRECT;
if (fcntl(dev->fd, F_SETFL, flags))
{
deverr(dev, "Failed to change direct I/O settings");
cfg->direct_io = dev->cfg.direct_io;
}
else
devlog(dev, LOG_INFO, "%s direct I/O from now on",
cfg->direct_io ? "Using" : "Not using");
}
ret = fstat(dev->fd, &st);
if (ret)
{
deverr(dev, "fstat() failed");
return -1;
}
if (S_ISBLK(st.st_mode))
{
if (ioctl(dev->fd, BLKGETSIZE64, &size))
{
deverr(dev, "ioctl(BLKGETSIZE64) failed");
return -1;
}
}
else
size = st.st_size;
if (size != dev->size)
{
hsize = human_format(size, &unit);
devlog(dev, LOG_INFO, "New size %lld (%lld sectors)",
hsize, size >> 9);
dev->size = size;
}
return 0;
}
/* Allocate the device. Do everything that does not need changing if the
* configuration is updated */
static struct device *alloc_dev(const char *name)
{
struct device *dev;
unsigned i;
int ret;
dev = g_slice_new0(struct device);
dev->name = g_strdup(name);
dev->fd = -1;
dev->event_fd = -1;
dev->timer_fd = -1;
dev->ifaces = g_ptr_array_new();
dev->event_ctx.callback = dev_io;
dev->event_ctx.data = dev;
dev->timer_ctx.callback = dev_timer;
dev->timer_ctx.data = dev;
dev->chain.data = dev;
if (!get_device_config(name, &dev->cfg))
{
free_dev(dev);
return NULL;
}
dev->deferred = g_ptr_array_sized_new(dev->cfg.queue_length);
for (i = 0; i < devices->len; i++)
{
struct device *dev2 = g_ptr_array_index(devices, i);
if (dev2 == dev)
continue;
if (dev2->cfg.shelf != dev->cfg.shelf || dev2->cfg.slot != dev->cfg.slot)
continue;
devlog(dev, LOG_ERR, "Duplicate shelf/slot (matches %s)", dev2->name);
free_dev(dev);
return NULL;
}
ret = io_setup(2 * EVENT_BATCH, &dev->aio_ctx);
if (ret)
{
if (ret == -EAGAIN)
{
devlog(dev, LOG_ERR, "Failed to allocate the AIO context.");
devlog(dev, LOG_ERR, "Consider increasing /proc/sys/fs/aio-max-nr");
}
else
devlog(dev, LOG_ERR, "io_setup() failed: %s", strerror(-ret));
free_dev(dev);
return NULL;
}
dev->event_fd = eventfd(0, EFD_NONBLOCK);
if (dev->event_fd == -1)
{
deverr(dev, "Failed to create eventfd");
free_dev(dev);
return NULL;
}
add_fd(dev->event_fd, &dev->event_ctx);
dev->aoe_conf = open_and_map(dev, "config", sizeof(*dev->aoe_conf));
dev->mac_mask = open_and_map(dev, "mac_mask", sizeof(*dev->mac_mask));
dev->reserve = open_and_map(dev, "reserve", sizeof(*dev->reserve));
if (dev->aoe_conf == MAP_FAILED || dev->mac_mask == MAP_FAILED ||
dev->reserve == MAP_FAILED)
{
free_dev(dev);
return NULL;
}
check_config_map(dev, dev->aoe_conf);
check_acl_map(dev, dev->mac_mask, "MAC Mask");
check_acl_map(dev, dev->reserve, "Reserve");
return dev;
}
/* (Re-)configure a device */
static int setup_dev(struct device *dev)
{
struct device_config newcfg;
int ret;
if (!get_device_config(dev->name, &newcfg))
return -1;
/* If the path changes, we have to close and re-open the device.
* The read-only status also can not be changed without re-opening. */
if ((dev->cfg.path && strcmp(dev->cfg.path, newcfg.path)) ||
dev->cfg.read_only != newcfg.read_only)
{
close(dev->fd);
dev->fd = -1;
}
if (dev->fd == -1)
ret = open_dev(dev, &newcfg);
else
ret = validate_dev_fd(dev, &newcfg);
if (ret)
return ret;
if (newcfg.merge_delay && dev->timer_fd == -1)
{
dev->timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (dev->timer_fd == -1)
deverr(dev, "Failed to create timerfd, merge-delay disabled");
else
add_fd(dev->timer_fd, &dev->timer_ctx);
}
if (!newcfg.merge_delay && dev->timer_fd != -1)
{
del_fd(dev->timer_fd);
close(dev->timer_fd);
dev->timer_fd = -1;
dev->timer_armed = FALSE;
/* Make sure to push out any requests that may be pending */
activate_dev(dev);
}
destroy_device_config(&dev->cfg);
dev->cfg = newcfg;
return 0;
}
/**********************************************************************
* I/O handling
*/
/* Called when a request has been finished and a reply should be sent */
static void finish_request(struct queue_item *q, int error)
{
struct device *const dev = q->dev;
q->aoe_hdr.error = error;
if (error)
{
q->hdrlen = sizeof(struct aoe_hdr);
q->aoe_hdr.is_error = TRUE;
++dev->stats.proto_err;
}
/* This can happen if the interface went down while the I/O was still
* in progress */
if (!q->iface)
return drop_request(q);
if (G_UNLIKELY(dev->cfg.trace_io))
devlog(dev, LOG_DEBUG, "%s/%08x: Completed, status %d",
ether_ntoa((struct ether_addr *)&q->aoe_hdr.addr.ether_shost),
(uint32_t)ntohl(q->aoe_hdr.tag), error);
/* Swap the source/destination addresses */
memcpy(&q->aoe_hdr.addr.ether_dhost, &q->aoe_hdr.addr.ether_shost, ETH_ALEN);
memcpy(&q->aoe_hdr.addr.ether_shost, &q->iface->mac, ETH_ALEN);
/* Always supply our own shelf/slot address in case the request was a broadcast */
q->aoe_hdr.shelf = dev->cfg.shelf;
q->aoe_hdr.slot = dev->cfg.slot;
/* Mark the packet as a response */
q->aoe_hdr.is_response = TRUE;
send_response(q);
}
/* Finish an ATA command */
static void finish_ata(struct queue_item *q, int error, int status)
{
q->ata_hdr.err_feature = error;
q->ata_hdr.cmdstat = status;
if (status & ATA_ERR)
{
drop_buffer(q);
++q->dev->stats.ata_err;
}
finish_request(q, 0);
}
/* Called when an I/O event completes */
static void complete_io(struct submit_slot *s, long res)
{
int error, status;
unsigned i;
g_queue_unlink(&s->dev->active, &s->chain);
if (G_UNLIKELY(res < 0))
{
devlog(s->dev, LOG_ERR, "%s request failed: %s",
s->iocb.aio_lio_opcode == IO_CMD_PREADV ? "Read" : "Write",
strerror(-res));
error = res == -EIO ? ATA_UNC : ATA_ABORTED;
status = ATA_ERR | ATA_DRDY;
}
else
{
error = 0;
status = ATA_DRDY;
}
for (i = 0; i < s->num_iov; i++)
{
struct queue_item *q = s->items[i];
/* Check if we got less data than we wanted */
if (G_UNLIKELY(!res))
{
devlog(q->dev, LOG_ERR, "Short %s request",
s->iocb.aio_lio_opcode == IO_CMD_PREADV ? "read" : "write");
error = ATA_ABORTED;
status |= ATA_ERR;
}
res -= q->length;
/* Do not send back the data to the client in case of a write
* request */
if (s->iocb.aio_lio_opcode == IO_CMD_PWRITEV)
q->length = 0;
finish_ata(q, error, status);
}
g_slice_free(struct submit_slot, s);
}
static void activate_dev(struct device *dev)
{
struct itimerspec new, old;
if (dev->is_active || dev->timer_armed)
return;
if (dev->timer_fd != -1 && dev->cfg.merge_delay)
{
memset(&new, 0, sizeof(new));
new.it_value.tv_nsec = dev->cfg.merge_delay;
if (timerfd_settime(dev->timer_fd, 0, &new, &old))
deverr(dev, "Failed to arm timer");
else
{
dev->timer_armed = TRUE;
return;
}
}
g_queue_push_tail_link(&active_devs, &dev->chain);
dev->is_active = TRUE;
}
static void deactivate_dev(struct device *dev)
{
if (!dev->is_active)
return;
g_queue_unlink(&active_devs, &dev->chain);
dev->is_active = FALSE;
}
/* eventfd event handler callback */
static void dev_io(uint32_t events, void *data)
{
struct device *const dev = data;
struct io_event ev[EVENT_BATCH];
eventfd_t dummy;
int ret, i;
/* Reset the event counter */
if (events & EPOLLIN)
{
ret = read(dev->event_fd, &dummy, sizeof(dummy));
if (ret != sizeof(dummy))
devlog(dev, LOG_WARNING, "Short read on the eventfd");
}
while (dev->active.length)
{
ret = io_getevents(dev->aio_ctx, 0, EVENT_BATCH, ev, NULL);
if (ret < 0)
{
devlog(dev, LOG_ERR, "io_getevents() failed: %s",
strerror(-ret));
break;
}
for (i = 0; i < ret; i++)
complete_io(ev[i].data, ev[i].res);
if (ret < EVENT_BATCH)
break;
}
deactivate_dev(dev);
run_queue(dev);
}
/* timerfd callback */
void dev_timer(uint32_t events, void *data)
{
struct device *const dev = data;
uint64_t expires;
int ret;
if (G_UNLIKELY(dev->cfg.trace_io))
devlog(dev, LOG_DEBUG, "Timer expired");
ret = read(dev->timer_fd, &expires, sizeof(expires));
if (ret == -1)
{
if (errno != EAGAIN)
deverr(dev, "Timer read");
return;
}
dev->timer_armed = FALSE;
run_queue(dev);
}
#define CMP(a, b) ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
static int queue_compare(const void *a, const void *b)
{
const struct queue_item *aa = *(void **)a, *bb = *(void **)b;
long t;
if (aa->start.tv_sec != bb->start.tv_sec)
return CMP(aa->start.tv_sec, bb->start.tv_sec);
t = aa->start.tv_nsec - bb->start.tv_nsec;
if (abs(t) > aa->dev->cfg.max_delay)
return t;
return CMP(aa->offset, bb->offset);
}
/* Set up the iocb for submission */
static inline void prepare_io(struct submit_slot *s)
{
if (s->is_write)
io_prep_pwritev(&s->iocb, s->dev->fd, s->iov, s->num_iov, s->offset);
else
io_prep_preadv(&s->iocb, s->dev->fd, s->iov, s->num_iov, s->offset);
s->iocb.data = s;
io_set_eventfd(&s->iocb, s->dev->event_fd);
}
static void submit(struct device *dev)
{
struct iocb *iocbs[EVENT_BATCH];
unsigned i, num_iocbs, req_prep;
unsigned long long next_offset;
struct submit_slot *s;
struct queue_item *q;
int ret;
/* Sort the deferred queue so we can merge more (we hope) */
g_ptr_array_sort(dev->deferred, queue_compare);
s = NULL;
num_iocbs = 0;
next_offset = 0ull;
req_prep = 0;
while (1)
{
/* Add a guard element to force flushing the last slot */
if (req_prep < dev->deferred->len)
q = g_ptr_array_index(dev->deferred, req_prep);
else
q = NULL;
/* - If there is already an open slot, and
* - either nothing is left in the queue, or
* - the next item cannot be merged into the current slot,
* then flush the current slot and open a new one. */
if (s && (!q ||
q->is_write != s->is_write ||
q->offset != next_offset ||
s->num_iov >= G_N_ELEMENTS(s->iov)))
{
prepare_io(s);
iocbs[num_iocbs++] = &s->iocb;
s = NULL;
/* This is the real exit from the loop */
if (!q || num_iocbs >= G_N_ELEMENTS(iocbs))
break;
}
if (!s)
{
s = g_slice_new0(struct submit_slot);
s->chain.data = s;
s->dev = dev;
s->is_write = q->is_write;
next_offset = s->offset = q->offset;
}
s->iov[s->num_iov].iov_base = q->buf;
s->iov[s->num_iov].iov_len = q->length;
s->items[s->num_iov++] = q;
next_offset += q->length;
++req_prep;
}
ret = io_submit(dev->aio_ctx, num_iocbs, iocbs);
if (ret == -EAGAIN)
{
for (i = 0; i < num_iocbs; i++)
g_slice_free(struct submit_slot, iocbs[i]->data);
dev->io_stall = TRUE;
++dev->stats.queue_stall;
return;
}
else if (ret < 0)
{
devlog(dev, LOG_ERR, "Failed to submit I/O: %s", strerror(-ret));
for (i = 0; i < num_iocbs; i++)
g_slice_free(struct submit_slot, iocbs[i]->data);
for (i = 0; i < req_prep; i++)
{
q = g_ptr_array_index(dev->deferred, i);
finish_ata(q, ATA_ABORTED, ATA_DRDY | ATA_ERR);
}
g_ptr_array_remove_range(dev->deferred, 0, req_prep);
return;
}
dev->stats.io_slots += ret;
++dev->stats.io_runs;
/* Add the submitted requests to the active queue */
for (i = 0; i < (unsigned)ret; i++)
{
s = iocbs[i]->data;
g_queue_push_tail_link(&dev->active, &s->chain);
}
/* If not all the requests were submitted, just leave the unsubmitted
* ones in the queue */
while (i < num_iocbs)
{
s = iocbs[i++]->data;
req_prep -= s->num_iov;
g_slice_free(struct submit_slot, s);
}
g_ptr_array_remove_range(dev->deferred, 0, req_prep);
}
static void run_queue(struct device *dev)
{
/* Submit any prepared I/Os */
dev->io_stall = FALSE;
while (dev->deferred->len && !dev->io_stall)
submit(dev);
}
void run_devices(void)
{
GList *l;
while ((l = g_queue_pop_head_link(&active_devs)))
{
struct device *dev = l->data;
dev->is_active = FALSE;
run_queue(dev);
}
}
static void ata_rw(struct queue_item *q)
{
struct device *const dev = q->dev;
/* Check for reservations */
if (dev->reserve->length && !match_acl(dev->reserve,
&q->aoe_hdr.addr.ether_shost))
return finish_request(q, AOE_ERR_RESERVED);
if (G_UNLIKELY(q->ata_hdr.nsect > max_sect_nr(q->iface)))
{
devlog(dev, LOG_ERR, "Request too large (%d)", q->ata_hdr.nsect);
return finish_ata(q, ATA_ABORTED, ATA_DRDY | ATA_ERR);
}
if (G_UNLIKELY(q->is_write && q->length < (unsigned)q->ata_hdr.nsect << 9))
{
devlog(dev, LOG_ERR, "Short write request (have %u, requested %u)",
q->length, (unsigned)q->ata_hdr.nsect << 9);
return finish_ata(q, ATA_ABORTED, ATA_DRDY | ATA_ERR);
}
q->length = (unsigned)q->ata_hdr.nsect << 9;
q->is_ata = TRUE;
if (G_UNLIKELY(q->offset + q->length > dev->size))
{
devlog(dev, LOG_NOTICE, "Attempt to access beyond end-of-device");
return finish_ata(q, ATA_IDNF, ATA_DRDY | ATA_ERR);
}
if (q->is_write)
{
dev->stats.write_bytes += q->length;
++dev->stats.write_cnt;
}
else
{
dev->stats.read_bytes += q->length;
++dev->stats.read_cnt;
}
/* If there are any deferred requests, then mark the device as active
* to ensure run_queue() will get called */
g_ptr_array_add(dev->deferred, q);
activate_dev(dev);
}
static void set_string(char *dst, const char *src, unsigned dstlen)
{
unsigned len;
char tmp;
len = strlen(src);
if (len > dstlen)
len = dstlen;
memcpy(dst, src, len);
memset(dst + len, ' ', dstlen - len);
/* Swapping is required for ATA strings */
for (len = 0; len < dstlen; len += 2)
{
tmp = dst[len];
dst[len] = dst[len + 1];
dst[len + 1] = tmp;
}
}
static void do_identify(struct queue_item *q)
{
struct hd_driveid *ident;
if (q->ata_hdr.nsect != 1)
{
devlog(q->dev, LOG_ERR, "Unexpected sector number (%hhd) for IDENTIFY", q->ata_hdr.nsect);
return finish_request(q, AOE_ERR_BADARG);
}
++q->dev->stats.other_cnt;
ident = q->buf;
memset(ident, 0, sizeof(*ident));
q->length = 512;
set_string((char *)ident->serial_no, q->dev->name, sizeof(ident->serial_no));
set_string((char *)ident->fw_rev, PACKAGE_VERSION, sizeof(ident->fw_rev));
set_string((char *)ident->model, PACKAGE_NAME, sizeof(ident->model));
ident->dword_io = GUINT16_TO_LE(1);
/* LBA */
ident->capability = GUINT16_TO_LE(2);
/* Legacy CHS if anyone still wants it */
ident->cur_cyls = (q->dev->size >> 9) / (255 * 16);
if (ident->cur_cyls > 16383)
ident->cur_cyls = 16383;
ident->cur_cyls = GUINT16_TO_LE(ident->cur_cyls);
ident->cur_heads = GUINT16_TO_LE(16);
ident->cur_sectors = GUINT16_TO_LE(255);
if (q->dev->size >> 9 > MAX_LBA28)
ident->lba_capacity = GUINT32_TO_LE(MAX_LBA28);
else
ident->lba_capacity = GUINT32_TO_LE(q->dev->size >> 9);
/* Bit 14: must be 1, bit 13: FLUSH_CACHE_EXT, bit 12: FLUSH_CACHE, bit 10: LBA48 */
ident->command_set_2 = GUINT16_TO_LE((1 << 14) | (1 << 13) | (1 << 12) | (1 << 10));
/* Bit 14: must be 1 */