-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcdfs-cdromutils.c
1633 lines (958 loc) · 37 KB
/
cdfs-cdromutils.c
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
/*
2010, 2011 Stef Bon <[email protected]>
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; either version 2
of the License, or (at your option) any later version.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "global-defines.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include <inttypes.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <pthread.h>
#include <fuse/fuse_lowlevel.h>
#include <sqlite3.h>
#include "logging.h"
#include "cdfs.h"
#include "entry-management.h"
#include "cdfs-cache.h"
#include "cdfs-cdromutils.h"
extern struct cdfs_options_struct cdfs_options;
extern struct cdfs_device_struct cdfs_device;
struct read_command_struct *head_queue_read_commands=NULL;
struct read_command_struct *tail_queue_read_commands=NULL;
struct read_command_struct *unused_read_commands=NULL;
struct read_call_struct *unused_read_calls=NULL;
unsigned char queue_lock=0;
pthread_mutex_t queue_lockmutex;
pthread_cond_t queue_lockcond;
// lock vars to lock the the queue
static char wavheader[SIZE_RIFFHEADER] = {
'R', 'I', 'F', 'F',
0, 0, 0, 0, //file size
'W', 'A', 'V', 'E',
'f', 'm', 't', ' ',
16, 0, 0, 0, //size of tag
1, 0, //Format
2, 0, //Channels
0x44, 0xac, 0, 0, // Samplerate 44100
0x10, 0xb1, 0x2, 0, // avg byte/sec 44100*2*2
4, 0, //Block align
16, 0, //bits/sample
'd', 'a', 't', 'a',
0, 0, 0, 0 //size of tag
};
// utilities
//
// tool for determin the discid
//
static int cddb_sum(int n)
{
int result=0;
while ( n>0 ) {
result+=n%10;
n /= 10;
}
return result;
}
//
// create the table with track info
//
// when there is no device with audio there is an error
//
//
int create_track_info()
{
int i, j, nreturn=0;
struct track_info_struct *track_info;
logoutput("get trackinfo from %s", cdfs_options.device);
cdfs_device.p_cdio=cdio_open(cdfs_options.device, DRIVER_DEVICE);
if ( ! cdfs_device.p_cdio ) {
nreturn=-ENOENT;
goto out;
}
// a valid cd device, now look at the number of tracks....
cdfs_device.nrtracks=cdio_get_num_tracks(cdfs_device.p_cdio);
logoutput("number tracks: %i", cdfs_device.nrtracks);
if ( cdfs_device.nrtracks<=0 || cdfs_device.nrtracks==CDIO_INVALID_TRACK ) {
nreturn=-EIO;
cdio_destroy(cdfs_device.p_cdio);
cdfs_device.p_cdio=NULL;
goto out;
}
i=cdio_get_first_track_num(cdfs_device.p_cdio);
// allocate the space for the tracks info (array)
cdfs_device.track_info = malloc(cdfs_device.nrtracks * sizeof(struct track_info_struct));
if ( ! cdfs_device.track_info ) {
nreturn=-ENOMEM;
cdio_destroy(cdfs_device.p_cdio);
cdfs_device.p_cdio=NULL;
goto out;
}
for ( j=0; j<cdfs_device.nrtracks; j++) {
track_info = (struct track_info_struct *) (cdfs_device.track_info + j * sizeof(struct track_info_struct));
track_info->firstsector_lsn=cdio_get_track_lsn(cdfs_device.p_cdio, i);
track_info->firstsector_lba=cdio_get_track_lba(cdfs_device.p_cdio, i);
track_info->lastsector=cdio_get_track_last_lsn(cdfs_device.p_cdio, i);
logoutput("track %i: %i - %i", i, track_info->firstsector_lsn, track_info->lastsector);
i++;
}
out:
return nreturn;
}
char *create_unique_hash(const char *path)
{
char *completeprogram=NULL;
int nlen=0;
char outputline[256];
char *hash;
// if defined use the hash program defined on the commandline
// maybe do this by using a library like mhash in stead of running an external command
if ( cdfs_options.hashprogram ) {
nlen=strlen(cdfs_options.hashprogram) + 1 + strlen(path) + 1;
} else {
nlen=strlen("md5sum") + 1 + strlen(path) + 1;
}
completeprogram=malloc(nlen);
if ( completeprogram ) {
char *tmppos=NULL;
FILE *pipe;
memset(completeprogram, '\0', nlen);
if ( cdfs_options.hashprogram ) {
snprintf(completeprogram, nlen, "%s %s", cdfs_options.hashprogram, path);
} else {
// if no hashprogram defined use the default: md5sum
snprintf(completeprogram, nlen, "md5sum %s", path);
}
logoutput2("create_cache_hash: running %s", completeprogram);
pipe=popen(completeprogram, "r");
if ( pipe ) {
while ( ! feof(pipe) ) {
memset(outputline, '\0', 256);
if ( ! fgets(outputline, 256, pipe) ) continue;
if ( strlen(outputline)>0 ) {
logoutput2("create_cache_hash: got output %s", outputline);
tmppos=strchr(outputline, ' '); /* look for the first space */
if ( tmppos ) break;
}
}
pclose(pipe);
if ( tmppos ) {
// copy the first part till the first space
nlen=tmppos-outputline;
hash=malloc(nlen+1);
if ( hash ) {
memset(hash, '\0', nlen+1);
strncpy(hash, outputline, nlen);
}
}
}
free(completeprogram);
}
return hash;
}
int create_discid()
{
char path[PATH_MAX];
int i, j, nreturn;
struct track_info_struct *track_info;
unsigned nsum=0;
for ( j=0; j<cdfs_device.nrtracks; j++) {
track_info = (struct track_info_struct *) (cdfs_device.track_info + j * sizeof(struct track_info_struct));
nsum+= cddb_sum(track_info->firstsector_lba / CDIO_CD_FRAMES_PER_SEC);
}
{
unsigned start_sec = cdio_get_track_lba(cdfs_device.p_cdio, 1) / CDIO_CD_FRAMES_PER_SEC;
unsigned leadout_sec = cdio_get_track_lba(cdfs_device.p_cdio, CDIO_CDROM_LEADOUT_TRACK) / CDIO_CD_FRAMES_PER_SEC;
unsigned total = leadout_sec - start_sec;
unsigned discid=((nsum % 0xff) << 24 | total << 8 | cdfs_device.nrtracks);
snprintf(cdfs_device.discidfile, PATH_MAX, "%s/discid.%i", cdfs_options.cache_directory, (int) getpid());
FILE *pipe=fopen(cdfs_device.discidfile, "wb");
if ( pipe ) {
logoutput2("file open for writing");
fprintf(pipe, "%08X %d", discid, cdfs_device.nrtracks);
j=0;
for ( i=1; i<= cdfs_device.nrtracks; i++) {
track_info = (struct track_info_struct *) (cdfs_device.track_info + j * sizeof(struct track_info_struct));
fprintf(pipe, " %ld", (long) track_info->firstsector_lba);
j++;
}
fprintf(pipe, " %ld\n", (long) leadout_sec);
fclose(pipe);
} else {
logoutput2("unable to open file for writing");
}
}
out:
return nreturn;
}
//
// function to do various calls in background
//
void *do_init_in_background()
{
char *hash=NULL;
char path[PATH_MAX];
int nreturn=0;
// first create the discid
// store the file with the discid somewhere
nreturn=create_discid();
if ( nreturn<0 ) goto out;
if ( cdfs_options.caching > 0 ) {
// create an unique hash to use in the cache path
hash=create_unique_hash(cdfs_device.discidfile);
}
out:
if ( hash ) {
logoutput2("do_init_in_background: got hash %s", hash);
cdfs_options.cachehash=hash;
snprintf(path, PATH_MAX, "%s/%s", cdfs_options.cache_directory, hash);
nreturn=mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if ( nreturn==0 || ( nreturn==-1 && errno==EEXIST ) ) {
// move the discid file to the new created directory
snprintf(path, PATH_MAX, "%s/%s/discid", cdfs_options.cache_directory, hash);
nreturn=rename(cdfs_device.discidfile, path);
strcpy(cdfs_device.discidfile, path);
// make the cache set and available
pthread_mutex_lock(&cdfs_device.initmutex);
cdfs_device.initready=1;
pthread_cond_broadcast(&(cdfs_device.initcond));
pthread_mutex_unlock(&cdfs_device.initmutex);
}
}
if ( cdfs_device.initready==1 && cdfs_options.cachebackend==CDFS_CACHE_ADMIN_BACKEND_SQLITE ) {
// create the sqlite db
nreturn=create_sqlite_db(cdfs_device.nrtracks);
if ( nreturn<0 ) {
fprintf(stderr, "Error, cannot create the sqlite db (error: %i).\n", abs(nreturn));
}
}
return;
}
int start_do_init_in_background_thread(pthread_t *pthreadid)
{
int nreturn=0;
//
// create a thread to read the cd
//
nreturn=pthread_create(pthreadid, NULL, do_init_in_background, NULL);
if ( nreturn==-1 ) {
// some error creating the thread
nreturn=-errno;
logoutput("Error creating a new thread (error: %i).", abs(nreturn));
}
return nreturn;
}
//
// function to open the cdrom using cdda functions
// this is required to open
//
static int open_cdrom_cdda()
{
int nreturn=0;
// open the device using "high level" calls from cddap library
// identify the cdrom using the p_cdio which has been set before
cdfs_device.cddevice=cdio_cddap_identify_cdio(cdfs_device.p_cdio, 0, NULL);
if ( ! cdfs_device.cddevice ) {
logoutput("Error, cannot identify device %s.", cdfs_options.device);
nreturn=-EIO;
goto out;
} else {
logoutput("Found cdrom model %s.", cdfs_device.cddevice->drive_model);
}
if ( cdio_cddap_open(cdfs_device.cddevice) != 0 ) {
logoutput("Cannot open device %s with cdda.", cdfs_options.device);
cdio_cddap_close(cdfs_device.cddevice);
nreturn=-EIO;
goto out;
}
// get additional info from cd like:
// a. number of tracks
// b. first sector
// c. last sector
// d. totalblocks
logoutput("Number of tracks: %li.", cdio_cddap_tracks(cdfs_device.cddevice));
logoutput("First audio sector: %li.", cdfs_device.cddevice->audio_first_sector);
logoutput("Last audio sector: %li.", cdfs_device.cddevice->audio_last_sector);
cdfs_device.totalblocks=get_totalblocks();
logoutput("Total blocks: %li.", cdfs_device.totalblocks);
out:
return nreturn;
}
//
// determine the tracknummer given the name, which is of the form:
//
// track-%nr%.wav
//
// return the tracknr
//
// if it cannot resolve the number, return 0
//
int get_tracknr(const char *name)
{
int i;
if (strlen(name) == 12 && strncmp(name, "track-", 6)==0 && strcmp(name + 8, ".wav")==0 ) {
if ( sscanf(name + 6, "%d", &i) == 1 && i > 0 && i <= cdfs_device.nrtracks) return i;
}
return 0;
}
//
// get size of track
//
size_t get_size_track(int tracknr)
{
size_t size=0;
struct track_info_struct *track_info;
// get the right trackinfo from the array
track_info = (struct track_info_struct *) (cdfs_device.track_info + (tracknr - 1) * sizeof(struct track_info_struct));
size = SIZE_RIFFHEADER + (track_info->lastsector - track_info->firstsector_lsn + 1) * CDIO_CD_FRAMESIZE_RAW;
logoutput3("get_size_track: size %zi for track %i", size, tracknr);
return size;
}
//
// get total nr of blocks used by tracks on the cd
// note the calculation of the total size first and then by dividing this by the block size (=sector size)
// this is because the file size presented by this fs is bigger than on the cd
// the difference is the header ( which is not part of the track on the cd!!! here this fs has to add this header)
//
unsigned long get_totalblocks()
{
unsigned long totalsectors=0;
size_t totalsize=0;
int i=1;
while ( i<= cdfs_device.nrtracks) {
totalsize+=get_size_track(i);
i++;
}
totalsectors=(unsigned long) totalsize / CDIO_CD_FRAMESIZE_RAW;
return totalsectors;
}
//
// give stat of track
// just let if be a simple readonly file
//
int stat_track(struct cdfs_entry_struct *entry, struct stat *st)
{
int nreturn=0;
int tracknr;
// entries are or:
//
// root entry, is directory, has root ino
// OR
// files with names track-%nr%.wav
if ( entry->type==ENTRY_TYPE_ROOT ) {
st->st_mode=S_IFDIR | 0555;
st->st_nlink=2;
st->st_blksize=CDIO_CD_FRAMESIZE_RAW;
st->st_blocks=st->st_size / 512 + 1 ;
st->st_uid=0; /* what here ? */
st->st_gid=0; /* what here ? */
st->st_size=4096; /* default size for directories */
} else {
tracknr=get_tracknr(entry->name);
if ( tracknr>0 ) {
st->st_mode=S_IFREG | 0444; /* dealing with a read only fs */
st->st_nlink=1;
st->st_size=get_size_track(tracknr);
st->st_blksize=CDIO_CD_FRAMESIZE_RAW;
st->st_blocks=st->st_size / 512 + 1 ;
st->st_uid=0; /* what here ? */
st->st_gid=0; /* what here ? */
} else {
nreturn=-ENOENT;
}
}
return nreturn;
}
void write_wavheader(char *header, size_t filesize)
{
size_t size1, size2;
logoutput2("creating wav header: filesize : %zi", filesize);
if ( header ) {
memcpy(header, wavheader, SIZE_RIFFHEADER);
size1=filesize-8;
size2=filesize-SIZE_RIFFHEADER;
*(header + 4) = (u_int8_t) (size1);
*(header + 5) = (u_int8_t) (size1 >> 8);
*(header + 6) = (u_int8_t) (size1 >> 16);
*(header + 7) = (u_int8_t) (size1 >> 24);
*(header + 40) = (u_int8_t) (size2);
*(header + 41) = (u_int8_t) (size2 >> 8);
*(header + 42) = (u_int8_t) (size2 >> 16);
*(header + 43) = (u_int8_t) (size2 >> 24);
}
}
//
// translate the file position (in bytes) in a track to a sector
//
int get_sector_from_position(int tracknr, off_t pos)
{
struct track_info_struct *track_info;
// get the right trackinfo from the array
track_info = (struct track_info_struct *) (cdfs_device.track_info + (tracknr - 1) * sizeof(struct track_info_struct));
return pos / CDIO_CD_FRAMESIZE_RAW + track_info->firstsector_lsn;
}
//
// manage the read_commands
//
struct read_call_struct *get_read_call()
{
struct read_call_struct *read_call;
unsigned char created=0;
if ( ! unused_read_calls ) {
// no unused ones... : create a new one
read_call=malloc(sizeof(struct read_call_struct));
created=1;
} else {
// take from list
read_call=unused_read_calls;
unused_read_calls=read_call->next;
if (unused_read_calls) unused_read_calls->prev=NULL;
}
if ( read_call ) {
read_call->fuse_cdfs_thread_id=0;
read_call->tracknr=0;
read_call->startsector=0;
read_call->endsector=0;
read_call->nrsectorsread=0;
read_call->nrsectorstoread=0;
read_call->complete=0;
read_call->next=NULL;
read_call->prev=NULL;
if ( created>0 ) {
// only once
pthread_mutex_init(&read_call->lockmutex, NULL);
pthread_cond_init(&read_call->lockcond, NULL);
}
read_call->lock=0;
}
return read_call;
}
// move a read_command to the unused list...
void move_read_call_to_unused_list(struct read_call_struct *read_call)
{
// remove first from the active list
if ( read_call->next ) read_call->next->prev=read_call->prev;
if ( read_call->prev ) read_call->prev->next=read_call->next;
// insert in unused list at beginning
if ( unused_read_calls ) {
read_call->next=unused_read_calls;
if ( read_call->next ) read_call->next->prev=read_call;
read_call->prev=NULL;
}
unused_read_calls=read_call;
}
//
// manage the read_commands
//
struct read_command_struct *get_read_command()
{
struct read_command_struct *read_command;
if ( ! unused_read_commands ) {
// no unused ones... : create a new one
read_command=malloc(sizeof(struct read_command_struct));
} else {
// take from list
read_command=unused_read_commands;
unused_read_commands=read_command->next;
if (unused_read_commands) unused_read_commands->prev=NULL;
}
if ( read_command ) {
read_command->startsector=0;
read_command->endsector=0;
read_command->read_call=NULL;
read_command->next=NULL;
read_command->prev=NULL;
}
return read_command;
}
// move a read_command to the unused list...
void move_read_command_to_unused_list(struct read_command_struct *read_command)
{
// remove first from the active list
if ( read_command->next ) read_command->next->prev=read_command->prev;
if ( read_command->prev ) read_command->prev->next=read_command->next;
// insert in unused list at beginning
if ( unused_read_commands ) {
read_command->next=unused_read_commands;
if ( read_command->next ) read_command->next->prev=read_command;
read_command->prev=NULL;
}
unused_read_commands=read_command;
}
//
// add a read command to the queue
//
// check the interval is already in the queue: if completly than ignore
// if partly then merge
//
// todo: rewrite to add the sectors, and only create a new read_command when not merged
// like the result queue
//
void add_read_command_to_queue(struct read_command_struct *read_command)
{
struct read_command_struct *read_command_tmp;
struct read_command_struct *read_command_prev;
unsigned char merged=0;
logoutput2("add read command to queue: to read %zi to %zi", read_command->startsector, read_command->endsector);
// lock the queue: set lock to 1
pthread_mutex_lock(&(queue_lockmutex));
while (queue_lock==1) {
pthread_cond_wait(&(queue_lockcond), &(queue_lockmutex));
}
queue_lock=1;
pthread_mutex_unlock(&(queue_lockmutex));
if ( tail_queue_read_commands ) {
// walk back to head of list, start at tail
read_command_tmp=tail_queue_read_commands;
while (read_command_tmp) {
// remember the prev
read_command_prev=read_command_tmp->prev;
//
// look at some overlap
// first look at cases there is no overlap at all
// if those are not the case then there must be some overlap
// excluding first the non overlap cases is far much easier then look at every
// possible overlap
if ( read_command->read_call != read_command_tmp->read_call ) {
// read command is for other track... skip
read_command_tmp=read_command_prev;
continue;
} else if ( read_command->startsector > read_command_tmp->endsector + 1 ) {
// no overlap: too left
read_command_tmp=read_command_prev;
continue;
} else if ( read_command->endsector < read_command_tmp->startsector - 1 ) {
// no overlap: too right
read_command_tmp=read_command_prev;
continue;
} else {
// some overlap: merge
// add the request to the already existing readcommand
logoutput2("add to queue: merging with block from %i to %i", read_command_tmp->startsector, read_command_tmp->endsector);
if ( read_command->startsector < read_command_tmp->startsector ) read_command_tmp->startsector=read_command->startsector;
if ( read_command->endsector > read_command_tmp->endsector ) read_command_tmp->endsector=read_command->endsector;
move_read_command_to_unused_list(read_command);
read_command=read_command_tmp;
merged=1;
}
read_command_tmp=read_command_prev;
continue;
}
if ( merged==0 ) {
// not merged somewhere..add it to the queue
logoutput2("add to queue: not merging... adding at tail");
tail_queue_read_commands->next=read_command;
read_command->prev=tail_queue_read_commands;
tail_queue_read_commands=read_command;
if ( ! head_queue_read_commands ) head_queue_read_commands=read_command;
}
} else {
// queue was empty
tail_queue_read_commands=read_command;
head_queue_read_commands=read_command;
}
logoutput2("add to queue: ready");
pthread_mutex_lock(&(queue_lockmutex));
queue_lock=0;
pthread_cond_broadcast(&(queue_lockcond));
pthread_mutex_unlock(&(queue_lockmutex));
}
int send_read_command(struct read_call_struct *read_call, struct caching_data_struct *caching_data, unsigned int startsector, unsigned int endsector, unsigned char readaheadpolicy, unsigned char readaheadlevel)
{
int nreturn=0;
struct read_command_struct *read_command;
read_command=get_read_command();
if ( ! read_command) {
nreturn=-ENOMEM;
goto out;
}
read_command->read_call=read_call;
read_command->caching_data=caching_data;
read_command->startsector=startsector;
read_command->endsector=endsector;
read_command->readaheadpolicy=readaheadpolicy;
if ( readaheadpolicy==READAHEAD_POLICY_PIECE ) read_command->readaheadlevel=readaheadlevel;
add_read_command_to_queue(read_command);
out:
return nreturn;
}