forked from gregkh/ndas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudev.c
More file actions
5903 lines (4968 loc) · 180 KB
/
udev.c
File metadata and controls
5903 lines (4968 loc) · 180 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
/*
to do: Change udev as unit device not single type logical unit. Need to create single type logical unit device types.
*/
#include "linux_ver.h"
#include "inc/xplatcfg.h"
#include "inc/sal/types.h"
#include "inc/xlib/dpc.h"
#include "inc/ndasuser/ndasuser.h"
#include "inc/netdisk/netdiskid.h"
#include "inc/netdisk/ndasdib.h"
#include "inc/netdisk/sdev.h"
#include "inc/raid/bitmap.h"
#include "udev.h"
#include "ndas_scsi_cmd_fmt.h"
#include "inc/netdisk/conn.h"
#include "inc/xlib/gtypes.h"
#include "registrar.h"
#include "inc/netdisk/scrc32.h"
#include "lockmgmt.h"
#ifndef NDAS_NO_LANSCSI
#define NDAS_SET_ENC_TIMEOUT (15*1000)
//#ifdef DEBUG
//#define debug_ndiod(l, x...) do {\
// if(l <= DEBUG_LEVEL_NDIOD) { \
// sal_debug_print("NDIOD|%d|%s|",l,__FUNCTION__); \
// sal_debug_println(x); \
// } \
//} while(0)
//#define debug_udev(l, x...) do {\
// if(l <= DEBUG_LEVEL_UDEV) { \
// sal_debug_print("UD|%d|%s|",l,__FUNCTION__); \
// sal_debug_println(x); \
// } \
//} while(0)
//#define debug_tir(l, x...) do {\
// if(l <= DEBUG_LEVEL_TIR) { \
// sal_debug_print("TR|%d|%s|",l,__FUNCTION__); \
// sal_debug_println(x); \
// } \
//} while(0)
//#else
#define debug_ndiod(l, x...) do {} while(0)
#define debug_udev(l, x...) do {} while(0)
#define debug_tir(l, x...) do {} while(0)
//#endif
#define MEMORY_FREE(ptr) if ( ptr ) { sal_free(ptr); ptr = NULL; }
/* max buffer size. todo remove this buffer */
int udev_max_xfer_unit = ND_MAX_BLOCK_SIZE;
LOCAL
NDAS_CALL void v_uop_enabled(int slot, ndas_error_t err, logunit_t* log_udev);
LOCAL
ndas_error_t uop_init(logunit_t *log_udev);
LOCAL
void uop_cleanup(logunit_t *log_udev);
LOCAL
void uop_enable(logunit_t * log_udev, int flag);
LOCAL
void uop_disable(logunit_t * log_udev, ndas_error_t err);
#ifdef XPLAT_ASYNC_IO
LOCAL
void uop_io(logunit_t *log_udev, struct udev_request_block *tir, xbool urgent);
LOCAL void
udev_queue_request(udev_t *udev, urb_ptr tir, xbool urgent);
#endif
#if 0
LOCAL
ndas_error_t uop_writable(logunit_t *log_udev);
#endif
LOCAL
int uop_status(logunit_t *log_udev) {
return SDEV2UDEV(log_udev)->conn.status;
}
#if 0
LOCAL
ndas_error_t uop_query(logunit_t *log_udev, ndas_metadata_t *nmd);
#endif
LOCAL
ndas_error_t uop_create(logunit_t *log_udev, sdev_create_param_t* param);
LOCAL void uop_deinit(logunit_t *log_udev);
struct ops_s udev_ops = {
.create_disks = (create_disks_func) uop_create,
.destroy_disks = (destroy_disks_func) uop_cleanup,
.init = (init_func) uop_init,
.deinit = (deinit_func) uop_deinit,
.enable = (enable_func) uop_enable,
.disable = (disable_func) uop_disable,
// .writable = (writable_func) uop_writable,
#ifdef XPLAT_ASYNC_IO
.io = (io_func) uop_io,
#else
.io = NULL, /* Not used */
#endif
#if 0
.query = (query_func) uop_query,
#endif
.status = (status_func) uop_status,
.changed = NULL,
};
struct ndas_lock_io_blocking_done_arg {
sal_semaphore done_sema;
ndas_error_t err;
};
struct ndas_lock_oprand {
NDAS_LOCK_OP op;
int lockid;
void* lockdata;
void* opt;
};
/*
To do: integrate into lockmgmt.
*/
ndas_error_t
ndas_lock_aop_operation(udev_t *udev, urb_ptr tir)
{
struct ndas_lock_oprand * oprand = NULL;
if(tir == NULL) {
return NDAS_ERROR_INVALID_PARAMETER ;
}
if(tir->req->done_arg == NULL) {
return NDAS_ERROR_INVALID_PARAMETER ;
}
oprand = (struct ndas_lock_oprand *)tir->arg;
if( udev == NULL) {
return NDAS_ERROR_NO_DEVICE;
}
if (oprand->op == NDAS_LOCK_TAKE) {
return OpAcquireDevLock(&udev->conn,
oprand->lockid, NULL, 3 * 1000, TRUE);
} else if (oprand->op == NDAS_LOCK_GIVE) {
return OpReleaseDevLock(&udev->conn, oprand->lockid, NULL, 3 * 1000);
} else {
return conn_lock_operation(&(udev->conn), oprand->op, oprand->lockid, oprand->lockdata, oprand->opt);
}
}
void ndas_lock_aop_done(int slot, ndas_error_t err, void* arg)
{
struct ndas_lock_io_blocking_done_arg* darg = (struct ndas_lock_io_blocking_done_arg*)arg;
darg->err = err;
debug_udev(0,"CALL ndas_lock_aop_done!");
sal_semaphore_give((sal_semaphore)darg->done_sema);
}
ndas_error_t
udev_io_idle_operation(udev_t *udev)
{
#ifdef XPLAT_ASYNC_IO
urb_ptr tir = NULL;
/* Send IO idle message */
debug_udev(4,"udev_io_idle_operation");
tir = tir_alloc(ND_CMD_IO_IDLE, NULL);
tir->func = NULL;
tir->arg = NULL;
udev_queue_request(udev, tir, FALSE);
return NDAS_OK;
#else
return NDAS_OK;
#endif //#ifdef XPLAT_ASYNC_IO
}
/*
Currently this is only used for xixfs lock.
to do:
do not use tir->func.
Integrate into lockmgmt.
*/
ndas_error_t
udev_lock_operation(udev_t *udev, NDAS_LOCK_OP op, int lockid, void* lockdata, void* opt)
{
#ifdef XPLAT_ASYNC_IO
urb_ptr tir = NULL;
struct ndas_lock_io_blocking_done_arg darg ;
struct ndas_lock_oprand oprand;
int err;
/* Currently udev_lock_operation is used only by xixfs */
sal_assert(lockid == 2);
oprand.lockid = lockid;
oprand.op = op;
oprand.lockdata = lockdata;
oprand.opt = opt;
debug_udev(4,"ed");
tir = tir_alloc(ND_CMD_LOCKOP, NULL);
tir->func = ndas_lock_aop_operation;
tir->arg = &oprand;
tir->req->done = ndas_lock_aop_done;
tir->req->done_arg = &darg;
darg.done_sema = sal_semaphore_create("udev_lock_io_done", 1, 0);
udev_queue_request(udev, tir, FALSE);
do {
err = sal_semaphore_take(darg.done_sema, SAL_SYNC_FOREVER);
} while (err == SAL_SYNC_INTERRUPTED);
sal_semaphore_give(darg.done_sema);
sal_semaphore_destroy(darg.done_sema);
return darg.err;
#else
return conn_lock_operation(&(udev->conn), op, lockid, lockdata, opt);
#endif //#ifdef XPLAT_ASYNC_IO
}
/*
To do:
Change this function to operation on unit device.
*/
NDASUSER_API ndas_error_t
ndas_lock_operation(int slot, NDAS_LOCK_OP op, int lockid, void* lockdata, void* opt)
{
logunit_t * log_udev = sdev_lookup_byslot(slot);
if (log_udev==NULL) {
return NDAS_ERROR_INVALID_SLOT_NUMBER;
}
return udev_lock_operation(SDEV2UDEV(log_udev), op, lockid, lockdata, opt);
}
void udev_set_max_xfer_unit(int io_buffer_size) {
udev_max_xfer_unit = (io_buffer_size > 0) ? io_buffer_size : ND_MAX_BLOCK_SIZE;
debug_udev(1, "io_buffer_size=%d", io_buffer_size);
}
LOCAL
ndas_error_t uop_create(logunit_t *log_udev, sdev_create_param_t* param)
{
udev_t *udev;
ndas_error_t err = NDAS_ERROR_OUT_OF_MEMORY;
debug_udev(1, "ing node=%p unit=%d", param->u.single.ndev->network_id, param->u.single.unit);
udev = sal_malloc(sizeof(udev_t));
if ( !udev ) return NDAS_ERROR_OUT_OF_MEMORY;
sal_memset(udev, 0, sizeof(udev_t));
#ifdef DEBUG
udev->magic = UDEV_MAGIC;
#endif
log_udev->info2 = sal_malloc(sizeof(ndas_unit_info_t));
if ( !log_udev->info2 ) {
sal_free(udev);
return NDAS_ERROR_OUT_OF_MEMORY;
}
sal_memset(log_udev->info2, 0, sizeof(ndas_unit_info_t));
SDEV_UNIT_INFO(log_udev)->raid_slot = NDAS_FIRST_SLOT_NR - 1;
udev->ndev = param->u.single.ndev;
udev->unit = param->u.single.unit;
udev->info = &log_udev->info;
udev->uinfo = log_udev->info2;
debug_udev(3, "udev=%p", udev);
debug_udev(3, "udev->info=%p", udev->info);
debug_udev(3, "log_udev->info2=%p", log_udev->info2);
udev->info->unit = param->u.single.unit;
#ifdef NDAS_MSHARE
if(udev->Disk_info == NULL)
{
struct nd_diskmgr_struct * diskmgr;
diskmgr = sal_malloc(sizeof(struct nd_diskmgr_struct));
udev->Disk_info = diskmgr;
if(diskmgr) {
int i = 0;
sal_memset(diskmgr, 0, sizeof(struct nd_diskmgr_struct));
for(i = 0; i < MAX_PART ; i++)
{
diskmgr->disc_part_map[i] = -1;
}
diskmgr->MetaKey = sal_malloc(sizeof(struct media_key));
if(!diskmgr->MetaKey) {
debug_udev(1, "Can't alloc meida_key\n");
sal_debug_println("Can't alloc meida_key\n");
sal_free(diskmgr);
sal_free(log_udev->info2);
udev->Disk_info = NULL;
goto out1;
}
sal_memset(diskmgr->MetaKey,0,sizeof(struct media_key));
// Make Meta Key
if(!MakeMetaKey(diskmgr))
{
debug_udev(1, "Can't Make MetaKey\n");
sal_debug_println("Can't Make MetaKey\n");
sal_free(diskmgr->MetaKey);
sal_free(diskmgr);
sal_free(log_udev->info2);
udev->Disk_info = NULL;
goto out1;
}
diskmgr->mediainfo = sal_malloc(sizeof(struct media_disc_map));
if(!diskmgr->mediainfo)
{
debug_udev(1, "Can't alloc media_disc_map\n");
sal_debug_println("Can't alloc media_disc_map\n");
sal_free(diskmgr->MetaKey);
sal_free(diskmgr);
sal_free(log_udev->info2);
udev->Disk_info = NULL;
goto out1;
}
sal_memset(diskmgr->mediainfo, 0, sizeof(struct media_disc_map));
}else{
sal_debug_println("Can't alloc diskmgr\n");
goto out1;
}
}
#endif
#ifdef XPLAT_ASYNC_IO
INIT_LIST_HEAD(&udev->request_head);
if (!sal_spinlock_create("uq", &udev->lock))
goto out1;
#else
udev->io_lock = sal_semaphore_create("udev-iolock", 1, 1);
if(!udev->io_lock) {
goto out1;
}
#endif
#ifdef XPLAT_NDASHIX
udev->hix_sema = sal_semaphore_create("hix", 1, 0);
if ( udev->hix_sema == SAL_INVALID_SEMAPHORE )
goto out2;
#endif
// Give the reference of single NDAS device to the logical device.
log_udev->disks = (logunit_t **) udev;
return NDAS_OK;
#ifdef XPLAT_NDASHIX
out2:
sal_semaphore_destroy(udev->hix_sema);
#endif
#ifdef XPLAT_ASYNC_IO
out1:
sal_spinlock_destroy(udev->lock);
#else
out1:
sal_semaphore_destroy(udev->io_lock);
#endif
sal_free(udev);
return err;
}
LOCAL
void uop_cleanup(logunit_t *log_udev)
{
udev_t *udev = SDEV2UDEV(log_udev);
if ( !log_udev || !udev ) return;
#ifdef XPLAT_NDASHIX
if ( udev->hix_sema != SAL_INVALID_SEMAPHORE)
sal_semaphore_destroy(udev->hix_sema);
#endif
#ifdef XPLAT_ASYNC_IO
sal_spinlock_destroy(udev->lock);
#else
sal_semaphore_destroy(udev->io_lock);
#endif
#ifdef NDAS_MSHARE
{
int i;
struct nd_diskmgr_struct * diskmgr;
diskmgr = udev->Disk_info;
if(diskmgr->MetaKey) sal_free(diskmgr->MetaKey);
if(diskmgr->mediainfo) sal_free(diskmgr->mediainfo);
for(i = 0; i<8; i++){
if(diskmgr->Disc_info[i] != NULL)
{
if(diskmgr->Disc_info[i]->disc_key)
sal_free(diskmgr->Disc_info[i]->disc_key);
if(diskmgr->Disc_info[i]->disc_info)
sal_free(diskmgr->Disc_info[i]->disc_info);
if(diskmgr->Disc_info[i])
sal_free(diskmgr->Disc_info[i]);
diskmgr->Disc_info[i] = NULL;
}
}
sal_free(udev->Disk_info);
}
#endif
sal_free(udev);
log_udev->disks = NULL;
}
#if 0
/* Called by ndas_query_slot and raid query functions
*/
LOCAL
ndas_error_t uop_query(logunit_t *log_udev, ndas_metadata_t *nmd)
{
ndas_error_t err;
udev_t *udev = SDEV2UDEV(log_udev);
debug_udev(3, "log_udev=%p", log_udev);
debug_udev(5, "slot=%d conn=%p", log_udev->info.slot, &udev->conn);
debug_udev(5, "slot=%d status=%x", log_udev->info.slot, udev->conn.status);
err = udev_query_unit(udev, nmd);
if ( !NDAS_SUCCESS(err) ) {
debug_udev(1, "err=%d", err);
return err;
}
return NDAS_OK;
}
#endif
LOCAL void uop_deinit(logunit_t *log_udev)
{
debug_udev(1, "slot=%d", log_udev->info.slot);
#ifdef XPLAT_RAID
log_udev->private = NULL;
#endif
}
/*
* called by sdev_create
*/
LOCAL
ndas_error_t uop_init(logunit_t *log_udev)
{
ndev_t* ndev = SDEV2UDEV(log_udev)->ndev;
debug_udev(1, "udev=%p", SDEV2UDEV(log_udev));
#ifdef XPLAT_SERIAL
log_udev->has_key = TRUE;
#else
log_udev->has_key = ndev->info.ndas_key[0] ? TRUE: FALSE;
#endif
sal_strncpy(log_udev->info.ndas_serial, ndev->info.ndas_serial, NDAS_SERIAL_LENGTH+1);
ndev->info.slot[log_udev->info.unit] = log_udev->info.slot;
log_udev->info.io_splits = 1;
log_udev->info.mode = NDAS_DISK_MODE_SINGLE;
log_udev->info.mode_role = 0;
// log_udev->info.type = NDAS_UNIT_TYPE_HARD_DISK;
log_udev->info.sector_size = 1 << 9;
return NDAS_OK;
}
#if 0
LOCAL
ndas_error_t uop_writable(logunit_t *log_udev)
{
udev_t *udev = SDEV2UDEV(log_udev);
return udev->info->writable ==TRUE;
}
#endif
#ifdef XPLAT_ASYNC_IO
#ifdef XPLAT_RAID
struct udev_request_block *
tir_clone(urb_ptr tir)
{
urb_ptr ret = sal_alloc_from_pool(Urb_pool, struct udev_request_block));
if ( !ret )
return NULL;
sal_memcpy(ret, tir, sizeof(struct udev_request_block));
// If the URB uses external NDAS request, copy it to the new one.
ret->req = &ret->int_req;
if(tir->req != &tir->int_req) {
sal_memcpy(ret->req, tir->req, sizeof(ret->int_req));
}
debug_tir(3, "clone ret=%p", ret);
return ret;
}
#endif
/*
To do:
use pool mem
Don't use ndas_io_request_ptr. It is user interface structure.
*/
struct udev_request_block *
tir_alloc(xint8 cmd, ndas_io_request_ptr req)
{
urb_ptr tir = sal_alloc_from_pool(Urb_pool, sizeof(struct udev_request_block));
if ( !tir ) return NULL;
#ifdef DEBUG
tir->magic = TIR_MAGIC;
#endif
INIT_LIST_HEAD(&tir->queue);
tir->func = NULL;
tir->cmd = cmd;
// If the caller does not specify NDAS request, use the internal one.
if(req) {
tir->req = req;
} else {
sal_memset(&tir->int_req, 0, sizeof(ndas_io_request));
tir->req = &tir->int_req;
}
tir->arg = NULL;
debug_tir(3, "alloc tir=%p", tir);
return tir;
}
void tir_free(urb_ptr tir) {
debug_tir(3, "free tir=%p", tir);
sal_free_from_pool(Urb_pool, tir);
}
LOCAL
ndas_error_t udev_shuting_down(udev_t *udev, urb_ptr tir)
{
debug_udev(2, "ing slot=%d", udev->info->slot);
return NDAS_OK;
}
/**
* Shut down the unit device
* Called by
* uop_disable(ndas_disable_slot) : user thread
**/
LOCAL void udev_request_shutdown(udev_t *udev)
{
urb_ptr tir;
debug_udev(2, "ing slot=%d", udev->info->slot);
// udev->thread.exit_requested = TRUE;
tir = tir_alloc(ND_CMD_DISCONNECT, NULL);
if ( !tir ) {
sal_error_print("ndas: out of memory to shut down slot %d\n", udev->info->slot);
return;
}
tir->func = udev_shuting_down;
tir->arg = NULL;
udev_queue_request(udev, tir, FALSE);
}
LOCAL
struct udev_request_block* ndiod_get_next_task(udev_t *udev)
{
struct udev_request_block* treq;
debug_ndiod(4, "ing slot=%d", udev->info->slot );
sal_spinlock_take_softirq(udev->lock);
if( list_empty(&udev->request_head))
{
debug_ndiod(3, "ed slot=%d udev->request_head is no_pending io", udev->info->slot);
goto out;
}
treq = list_entry(udev->request_head.next, struct udev_request_block, queue);
list_del(&treq->queue);
sal_spinlock_give_softirq(udev->lock);
debug_ndiod(4, "ed slot=%d treq=%p", udev->info->slot, treq);
return treq;
out:
sal_spinlock_give_softirq(udev->lock);
return NULL;
}
static
int
ndiod_dispatch(udev_t *udev, struct udev_request_block *tir)
{
uconn_t *conn = &udev->conn;
ndas_error_t res;
xbool locklost;
xbool do_exit = FALSE;
debug_ndiod(4,"waiting slot=%d", udev->info->slot);
if ( conn->status != CONN_STATUS_CONNECTED )
{
if ( tir->cmd == ND_CMD_READ || tir->cmd == ND_CMD_WRITE )
{
debug_ndiod(1, "status=%d", conn->status);
if ( tir->req->done )
tir->req->done(udev->info->slot, NDAS_ERROR_SHUTDOWN_IN_PROGRESS, tir->req->done_arg);
do_exit = TRUE;
goto out;
}
}
#ifdef DEBUG
if ( tir->cmd == ND_CMD_CONNECT ) {
debug_ndiod(2, "slot=%d connecting", udev->info->slot);
}
#endif
if ( tir->func ) {
res = tir->func(udev, tir);
} else {
switch(tir->cmd) {
case ND_CMD_READ:
case ND_CMD_WRITE:
case ND_CMD_WV:
{
if(udev->BuffLockCtl.BufferLockConrol) {
// Check to see if this IO is in the range of a lost device lock.
locklost = LockCacheCheckLostLockIORange(
&udev->lock_info,
tir->req->start_sec,
tir->req->start_sec + tir->req->num_sec -1
);
if (locklost) {
debug_ndiod(1, "Lock lost. Failing operation");
res = NDAS_ERROR_LOCK_LOST;
break;
}
StopIoIdleTimer(&udev->BuffLockCtl);
}
if(tir->cmd == ND_CMD_WV) {
res = conn_do_ata_write_verify(conn, tir->req);
} else {
res = conn_do_ata_rw(conn, tir->cmd, tir->req);
}
if(udev->BuffLockCtl.BufferLockConrol) {
StartIoIdleTimer(&udev->BuffLockCtl, udev);
}
}
break;
case ND_CMD_FLUSH:
{
res = conn_do_ata_flush(conn, tir->req);
}
break;
case ND_CMD_VERIFY:
{
res = conn_do_ata_verify(conn, tir->req);
}
break;
case ND_CMD_PACKET:
{
res = conn_do_atapi_cmd(conn, tir->req);
}
break;
case ND_CMD_PASSTHROUGH:
{
res = conn_do_ata_passthrough(conn, tir->req);
}
break;
case ND_CMD_SET_FEATURE:
{
res = conn_do_ata_set_feature(conn, tir->req);
}
break;
case ND_CMD_HANDSHAKE:
{
res = conn_handshake(conn);
}
break;
case ND_CMD_IO_IDLE:
res = EnterBufferLockIoIdle( &udev->BuffLockCtl, conn, &udev->lock_info);
if (!NDAS_SUCCESS(res)) {
debug_ndiod(1, "EnterBufferLockIoIdle failed:%d\n", res);
res = conn_reconnect(&udev->conn);
LockCacheAllLocksLost(&udev->lock_info);
}
break;
case ND_CMD_DISCONNECT:
res = NDAS_OK;
conn_set_error(conn, NDAS_ERROR_SHUTDOWN_IN_PROGRESS);
break;
default:
res = NDAS_ERROR_INTERNAL;
sal_assert(FALSE);
break;
}
/* Mark this device alive while it response. We may miss PNP packet while IO */
#ifdef XPLAT_PNP
udev->ndev->last_tick = sal_get_tick();
#endif
}
#ifdef DEBUG
if ( tir->cmd == ND_CMD_CONNECT ) {
debug_ndiod(2, "slot=%d connect err=%d", udev->info->slot, res);
}
#endif
if(tir->cmd == ND_CMD_PACKET) {
switch(res){
case NDAS_ERROR_IDE_REMOTE_INITIATOR_BAD_COMMAND :
case NDAS_ERROR_IDE_REMOTE_COMMAND_FAILED :
case NDAS_ERROR_UNSUPPORTED_HARDWARE_VERSION:
case NDAS_ERROR_IDE_REMOTE_AUTH_FAILED :
case NDAS_ERROR_HARDWARE_DEFECT:
case NDAS_ERROR_IDE_TARGET_BROKEN_DATA :
case NDAS_ERROR_BAD_SECTOR:
case NDAS_ERROR_IDE_VENDOR_SPECIFIC :
conn_do_atapi_req_sense(conn, tir->req);
goto process;
break;
default:
break;
}
}
if(tir->cmd == ND_CMD_PASSTHROUGH){
switch(res) {
case NDAS_ERROR_IDE_REMOTE_INITIATOR_BAD_COMMAND :
case NDAS_ERROR_IDE_REMOTE_COMMAND_FAILED :
case NDAS_ERROR_UNSUPPORTED_HARDWARE_VERSION:
case NDAS_ERROR_IDE_REMOTE_AUTH_FAILED :
case NDAS_ERROR_HARDWARE_DEFECT:
case NDAS_ERROR_IDE_TARGET_BROKEN_DATA :
case NDAS_ERROR_BAD_SECTOR:
case NDAS_ERROR_IDE_VENDOR_SPECIFIC :
res = NDAS_OK;
break;
default:
break;
}
}
if ( !NDAS_SUCCESS(res) )
{
debug_ndiod(1, "cmd=%d, err=%d", tir->cmd, res);
conn_set_status(conn, CONN_STATUS_SHUTING_DOWN);
conn_set_error(conn, res);
do_exit = TRUE;
}
process:
if ( tir->req->done ) {
tir->req->done(udev->info->slot, res, tir->req->done_arg);
debug_ndiod(3, "tir done finished");
}
out:
tir_free(tir);
return do_exit;
}
/* nd i/o deamon - ndas io handling thread */
void* ndiod_thread(udev_t *udev)
{
uconn_t *conn = &udev->conn;
char name[32];
xbool do_exit;
struct udev_request_block *urb;
#ifdef NDASIOD_YIELD
sal_tick pre_time;
sal_tick running_time = 0;
#endif
sal_thread_block_signals();
sal_snprintf(name, sizeof(name), "ndiod/%d", udev->info->slot);
sal_thread_daemonize(name); // change the name of process
sal_thread_nice(-20);
debug_ndiod(1,"Thread starting %s", name);
do_exit = FALSE;
while(!do_exit)
{
sal_event_wait(udev->thread.idle, SAL_SYNC_FOREVER);
sal_event_reset(udev->thread.idle);
if ( conn->status == CONN_STATUS_SHUTING_DOWN || !NDAS_SUCCESS(conn->err) ) {
debug_ndiod(2, "status=%d err=%d", conn->status,conn->err);
do_exit = TRUE;
break;
}
#ifdef NDASIOD_YIELD
running_time = 0;
pre_time = sal_get_tick();
#endif
while ((urb = ndiod_get_next_task(udev)) != NULL){
do_exit = ndiod_dispatch(udev, urb);
if(do_exit) {
break;
}
#ifdef NDASIOD_YIELD
running_time = sal_tick_add(running_time, sal_tick_sub(sal_get_tick(),pre_time));
pre_time = sal_get_tick();
if(running_time > 3 * SAL_TICKS_PER_SEC) {
running_time = 0;
sal_thread_yield();
}
#endif
}
}
debug_ndiod(1, "Terminating ndiod thread slot=%d", udev->info->slot);
debug_ndiod(2, "Terminating ndiod thread conn->sock=%d",conn->sock);
udev_shutdown(udev);
sal_thread_exit(0);
return 0;
}
LOCAL ndas_error_t ndiod_create(udev_t *udev)
{
ndas_error_t err = NDAS_OK;
udev->thread.idle = sal_event_create("ndiod_idle");
if ( udev->thread.idle == SAL_INVALID_EVENT )
goto out;
/* to do: register event handler(data_ready, state_change, write_space) to socket */
err = sal_thread_create(&udev->thread.tid, "ndiod_thread", -1, 0,
(void*(*)(void*)) ndiod_thread, udev);
if(err < 0) {
udev->thread.tid = SAL_INVALID_THREAD_ID;
debug_ndiod(1, "failed to create ndiod thread");
} else {
debug_ndiod(1, "ndiod thread created for slot %d", udev->info->slot);
}
return err;
out:
sal_event_destroy(udev->thread.idle);
udev->thread.idle = SAL_INVALID_EVENT;
return NDAS_ERROR_OUT_OF_MEMORY;
}
LOCAL void
udev_queue_request(udev_t *udev, struct udev_request_block *tir, xbool urgent)
{
sal_assert(udev);
sal_assert(udev->info);
debug_ndiod(5, "ing slot=%d", udev->info->slot);
#ifdef DEBUG
if ( tir->req->nr_uio == 1 ) {
debug_ndiod(6, "slot=%d blocks=%p", udev->info->slot, tir->req->uio);
} else {
debug_ndiod(6, "slot=%d nr_uio=%d, blocks=%p", udev->info->slot, tir->req->nr_uio, tir->req->uio);
}
#endif
sal_spinlock_take_softirq(udev->lock);
sal_assert( udev->thread.idle != SAL_INVALID_EVENT );
if ( (udev->conn.status & CONN_STATUS_SHUTING_DOWN ))
{
sal_spinlock_give_softirq(udev->lock);
debug_ndiod(1, "status=%x", udev->conn.status);
if ( tir->req->done )
tir->req->done(udev->info->slot, NDAS_ERROR_SHUTDOWN_IN_PROGRESS, tir->req->done_arg);
return;
}
if ( urgent )
list_add(&tir->queue, &udev->request_head);
else
list_add_tail(&tir->queue, &udev->request_head);
sal_spinlock_give_softirq(udev->lock);
sal_event_set(udev->thread.idle);
/* To do: need to handle the case that new task is queued continuously.. */
debug_ndiod(5, "ed");
return;
}
LOCAL void
uop_io(logunit_t *log_udev, struct udev_request_block *tir, xbool urgent)
{
if ( !log_udev->accept_io ) {
if ( tir->cmd != ND_CMD_CONNECT) {
debug_ndiod(1, "log_udev is not connected status");
if ( tir->req->done )
tir->req->done(log_udev->info.slot, NDAS_ERROR_SHUTDOWN_IN_PROGRESS, tir->req->done_arg);
return;
}
}
udev_queue_request(SDEV2UDEV(log_udev), tir, urgent);
}
/**
* old: udev_invalidte, nd_invalidate_driver, nd_invalidate_queue
*/
LOCAL
void udev_invalidate_requests(udev_t *udev)
{
struct udev_request_block *tir;
debug_udev(2, "ing slot=%d", udev->info->slot);
do {
tir = NULL;
sal_spinlock_take_softirq(udev->lock);
if(!list_empty(&udev->request_head)) {
tir = list_entry(udev->request_head.next, struct udev_request_block, queue);
TIR_ASSERT(tir);
list_del(&tir->queue);
sal_spinlock_give_softirq(udev->lock);
sal_assert(!NDAS_SUCCESS(udev->conn.err));
if ( tir->cmd == ND_CMD_DISCONNECT )
tir->func(udev,tir);
if ( tir->req->done)
tir->req->done(udev->info->slot, udev->conn.err, tir->req->done_arg);// TODO
tir_free(tir);
continue;
}
sal_spinlock_give_softirq(udev->lock);
} while(tir);
debug_udev(2, "ed");
}
LOCAL
void ndiod_cleanup(struct ndiod *thread)
{
debug_udev(2, "ing");
sal_event_destroy(thread->idle);
thread->idle = SAL_INVALID_EVENT;
thread->tid = 0;
}
#endif // end of #ifdef XPLAT_ASYNC_IO
#ifdef XPLAT_RAID
#define BITMAP_IO_UNIT (32*1024)
ndas_error_t
conn_read_bitmap(uconn_t *conn,
xuint64 sector_count,
bitmap_t *bitmap)
{
int i, j;
ndas_error_t err = NDAS_OK;
xuint64 start_sec;
xuint32 num_sec;
int nr_uio;
int bitmap_size;
int nr_io;
struct sal_mem_block *uio;
uio = sal_malloc(sizeof(struct sal_mem_block) * (BITMAP_IO_UNIT / bitmap->chunk_size));
if ( !uio )
return NDAS_ERROR_OUT_OF_MEMORY;