Skip to content

Commit df5189a

Browse files
committed
Abstract out some common code, and place it in the utilities source.
Get rid of some #ifdef __KERNEL__ abstractions - they're not needed.
1 parent 96755c6 commit df5189a

File tree

4 files changed

+64
-191
lines changed

4 files changed

+64
-191
lines changed

dist/iscsi/include/util.h

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,7 @@ typedef int socklen_t;
196196
* Sleeping
197197
*/
198198

199-
#ifdef __KERNEL__
200-
#define ISCSI_SLEEP(N) {uint32_t future = jiffies+N*HZ; while (jiffies<future) ISCSI_SPIN;}
201-
#else
202199
#define ISCSI_SLEEP(N) sleep(N)
203-
#endif
204200

205201
/*
206202
* Memory
@@ -223,13 +219,7 @@ void iscsi_free_atomic(void *);
223219

224220
/* Spin locks */
225221

226-
#ifdef __KERNEL__
227-
typedef spinlock_t
228-
iscsi_spin_t;
229-
#else
230-
typedef pthread_mutex_t
231-
iscsi_spin_t;
232-
#endif
222+
typedef pthread_mutex_t iscsi_spin_t;
233223

234224
int iscsi_spin_init(iscsi_spin_t * );
235225
int iscsi_spin_lock(iscsi_spin_t * );
@@ -303,11 +293,7 @@ int iscsi_queue_full(iscsi_queue_t * );
303293
* Socket Abstraction
304294
*/
305295

306-
#ifdef __KERNEL__
307-
typedef struct socket *iscsi_socket_t;
308-
#else
309296
typedef int iscsi_socket_t;
310-
#endif
311297

312298
/* Turning off Nagle's Algorithm doesn't always seem to work, */
313299
/* so we combine two messages into one when the second's size */
@@ -335,15 +321,15 @@ int iscsi_sock_getsockname(iscsi_socket_t , struct sockaddr * , unsi
335321
int iscsi_sock_getpeername(iscsi_socket_t , struct sockaddr * , unsigned *);
336322
int modify_iov(struct iovec ** , int *, uint32_t , uint32_t );
337323

324+
325+
void cdb2lba(uint32_t *, uint16_t *, uint8_t *);
326+
void lba2cdb(uint8_t *, uint32_t *, uint16_t *);
327+
338328
/*
339329
* Mutexes
340330
*/
341331

342-
#ifdef __KERNEL__
343-
typedef struct semaphore iscsi_mutex_t;
344-
#else
345332
typedef pthread_mutex_t iscsi_mutex_t;
346-
#endif
347333

348334
int iscsi_mutex_init(iscsi_mutex_t * );
349335
int iscsi_mutex_lock(iscsi_mutex_t * );
@@ -352,28 +338,28 @@ int iscsi_mutex_destroy(iscsi_mutex_t * );
352338

353339
#define ISCSI_LOCK(M, ELSE) do { \
354340
if (iscsi_mutex_lock(M) != 0) { \
355-
iscsi_trace_error("iscsi_mutex_lock() failed\n"); \
341+
iscsi_trace_error("iscsi_mutex_lock() failed\n"); \
356342
ELSE; \
357343
} \
358344
} while (/* CONSTCOND */ 0)
359345

360346
#define ISCSI_UNLOCK(M, ELSE) do { \
361347
if (iscsi_mutex_unlock(M) != 0) { \
362-
iscsi_trace_error("iscsi_mutex_unlock() failed\n"); \
348+
iscsi_trace_error("iscsi_mutex_unlock() failed\n"); \
363349
ELSE; \
364350
} \
365351
} while (/* CONSTCOND */ 0)
366352

367353
#define ISCSI_MUTEX_INIT(M, ELSE) do { \
368354
if (iscsi_mutex_init(M) != 0) { \
369-
iscsi_trace_error("iscsi_mutex_init() failed\n"); \
355+
iscsi_trace_error("iscsi_mutex_init() failed\n"); \
370356
ELSE; \
371357
} \
372358
} while (/* CONSTCOND */ 0)
373359

374360
#define ISCSI_MUTEX_DESTROY(M, ELSE) do { \
375361
if (iscsi_mutex_destroy(M) != 0) { \
376-
iscsi_trace_error("iscsi_mutex_destroy() failed\n"); \
362+
iscsi_trace_error("iscsi_mutex_destroy() failed\n"); \
377363
ELSE; \
378364
} \
379365
} while (/* CONSTCOND */ 0)
@@ -382,11 +368,7 @@ int iscsi_mutex_destroy(iscsi_mutex_t * );
382368
* Condition Variable
383369
*/
384370

385-
#ifdef __KERNEL__
386-
typedef struct semaphore iscsi_cond_t;
387-
#else
388371
typedef pthread_cond_t iscsi_cond_t;
389-
#endif
390372

391373
int iscsi_cond_init(iscsi_cond_t * );
392374
int iscsi_cond_wait(iscsi_cond_t * , iscsi_mutex_t * );
@@ -422,36 +404,14 @@ int iscsi_cond_destroy(iscsi_cond_t * );
422404
*/
423405

424406
typedef struct iscsi_thread_t {
425-
#ifdef __KERNEL__
426-
struct task_struct *pthread;
427-
#else
428407
pthread_t pthread;
429-
#endif
430408
} iscsi_thread_t;
431409

432410
int iscsi_thread_create(iscsi_thread_t * , void *(*proc) (void *), void *);
433411

434-
#ifdef __KERNEL__
435-
#define ISCSI_SET_THREAD(ME) me->thread.pthread = current;
436-
437-
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
438-
#define REPARENT_TO_INIT() reparent_to_init()
439-
#define ISCSI_THREAD_START(NAME) \
440-
lock_kernel(); \
441-
daemonize(); \
442-
REPARENT_TO_INIT(); \
443-
unlock_kernel(); \
444-
sprintf(current->comm, "%s", NAME);
445-
#else
446-
#define REPARENT_TO_INIT
447-
#define ISCSI_THREAD_START(NAME) \
448-
sprintf(current->comm, "%s", NAME);
449-
#endif
450-
#else
451412
#define ISCSI_SET_THREAD(ME) /* for user pthread id set by pthread_create
452413
* in iscsi_thread_create */
453414
#define ISCSI_THREAD_START(NAME)
454-
#endif
455415

456416
/*
457417
* Worker Thread
@@ -481,12 +441,7 @@ typedef struct {
481441
/*
482442
* Spin Lock
483443
*/
484-
485-
#ifdef __KERNEL__
486-
#define ISCSI_SPIN schedule()
487-
#else
488444
#define ISCSI_SPIN
489-
#endif
490445

491446
/*
492447
* Pre/Post condition checking

dist/iscsi/src/disk.c

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: disk.c,v 1.7 2006/03/20 20:45:07 agc Exp $ */
1+
/* $NetBSD: disk.c,v 1.8 2006/03/21 21:03:14 agc Exp $ */
22

33
/*
44
* Copyright © 2006 Alistair Crooks. All rights reserved.
@@ -934,25 +934,7 @@ device_command(target_session_t * sess, target_cmd_t * cmd)
934934

935935
case WRITE_10:
936936

937-
/* Some platforms (like strongarm) aligns on */
938-
/* word boundaries. So HTONL and NTOHL won't */
939-
/* work here. */
940-
941-
#if (BYTE_ORDER == BIG_ENDIAN)
942-
((uint8_t *) (void *) &lba)[0] = cdb[2];
943-
((uint8_t *) (void *) &lba)[1] = cdb[3];
944-
((uint8_t *) (void *) &lba)[2] = cdb[4];
945-
((uint8_t *) (void *) &lba)[3] = cdb[5];
946-
((uint8_t *) (void *) &len)[0] = cdb[7];
947-
((uint8_t *) (void *) &len)[1] = cdb[8];
948-
#else
949-
((uint8_t *) (void *) &lba)[0] = cdb[5];
950-
((uint8_t *) (void *) &lba)[1] = cdb[4];
951-
((uint8_t *) (void *) &lba)[2] = cdb[3];
952-
((uint8_t *) (void *) &lba)[3] = cdb[2];
953-
((uint8_t *) (void *) &len)[0] = cdb[8];
954-
((uint8_t *) (void *) &len)[1] = cdb[7];
955-
#endif
937+
cdb2lba(&lba, &len, cdb);
956938

957939
iscsi_trace(TRACE_SCSI_CMD, "WRITE_10(lba %u, len %u blocks)\n", lba, len);
958940
if (disk_write(sess, args, lun, lba, (unsigned) len) != 0) {
@@ -964,26 +946,7 @@ device_command(target_session_t * sess, target_cmd_t * cmd)
964946

965947
case READ_10:
966948

967-
/* Some platforms (like strongarm) aligns on */
968-
/* word boundaries. So HTONL and NTOHL won't */
969-
/* work here. */
970-
971-
#if (BYTE_ORDER == BIG_ENDIAN)
972-
((uint8_t *) (void *) &lba)[0] = cdb[2];
973-
((uint8_t *) (void *) &lba)[1] = cdb[3];
974-
((uint8_t *) (void *) &lba)[2] = cdb[4];
975-
((uint8_t *) (void *) &lba)[3] = cdb[5];
976-
((uint8_t *) (void *) &len)[0] = cdb[7];
977-
((uint8_t *) (void *) &len)[1] = cdb[8];
978-
#else
979-
((uint8_t *) (void *) &lba)[0] = cdb[5];
980-
((uint8_t *) (void *) &lba)[1] = cdb[4];
981-
982-
((uint8_t *) (void *) &lba)[2] = cdb[3];
983-
((uint8_t *) (void *) &lba)[3] = cdb[2];
984-
((uint8_t *) (void *) &len)[0] = cdb[8];
985-
((uint8_t *) (void *) &len)[1] = cdb[7];
986-
#endif
949+
cdb2lba(&lba, &len, cdb);
987950

988951
iscsi_trace(TRACE_SCSI_CMD, "READ_10(lba %u, len %u blocks)\n", lba, len);
989952
if (disk_read(sess, args, lba, len, lun) != 0) {

dist/iscsi/src/tests.c

Lines changed: 5 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -248,26 +248,7 @@ write_read_test(uint64_t target, uint32_t lun, int type)
248248
if (type == 10) {
249249
cdb[0] = WRITE_10;
250250
cdb[1] = lun << 5;
251-
252-
/* Strongarm aligns on word boundaries. */
253-
/* So HTONL and NTOHL won't work here. */
254-
255-
#if (BYTE_ORDER == BIG_ENDIAN)
256-
cdb[2] = ((uint8_t *) &i)[2];
257-
cdb[3] = ((uint8_t *) &i)[3];
258-
cdb[4] = ((uint8_t *) &i)[0];
259-
cdb[5] = ((uint8_t *) &i)[1];
260-
cdb[7] = ((uint8_t *) &len)[0];
261-
cdb[8] = ((uint8_t *) &len)[1];
262-
#else
263-
cdb[2] = ((uint8_t *) &i)[3];
264-
cdb[3] = ((uint8_t *) &i)[2];
265-
cdb[4] = ((uint8_t *) &i)[1];
266-
cdb[5] = ((uint8_t *) &i)[0];
267-
cdb[7] = ((uint8_t *) &len)[1];
268-
cdb[8] = ((uint8_t *) &len)[0];
269-
#endif
270-
251+
lba2cdb(cdb, &i, &len);
271252
} else {
272253
*((uint32_t *) (cdb + 0)) = ISCSI_HTONL(i);
273254
cdb[0] = WRITE_6;
@@ -302,26 +283,7 @@ write_read_test(uint64_t target, uint32_t lun, int type)
302283
if (type == 10) {
303284
cdb[0] = READ_10;
304285
cdb[1] = lun << 5;
305-
306-
/* Strongarm aligns on word boundaries. */
307-
/* So HTONL and NTOHL won't work here. */
308-
309-
#if (BYTE_ORDER == BIG_ENDIAN)
310-
cdb[2] = ((uint8_t *) &i)[2];
311-
cdb[3] = ((uint8_t *) &i)[3];
312-
cdb[4] = ((uint8_t *) &i)[0];
313-
cdb[5] = ((uint8_t *) &i)[1];
314-
cdb[7] = ((uint8_t *) &len)[0];
315-
cdb[8] = ((uint8_t *) &len)[1];
316-
#else
317-
cdb[2] = ((uint8_t *) &i)[3];
318-
cdb[3] = ((uint8_t *) &i)[2];
319-
cdb[4] = ((uint8_t *) &i)[1];
320-
cdb[5] = ((uint8_t *) &i)[0];
321-
cdb[7] = ((uint8_t *) &len)[1];
322-
cdb[8] = ((uint8_t *) &len)[0];
323-
#endif
324-
286+
lba2cdb(cdb, &i, &len);
325287
} else {
326288
*((uint32_t *) (cdb + 0)) = ISCSI_HTONL(i);
327289
cdb[0] = READ_6;
@@ -710,50 +672,14 @@ latency_test(uint64_t target, uint32_t lun, uint8_t op, uint32_t iters)
710672
trans_len = block_len;
711673
cdb[0] = READ_10;
712674
cdb[1] = lun << 5;
713-
714-
/* Strongarm aligns on word boundaries. */
715-
/* So HTONL and NTOHL won't work here. */
716-
717-
#if (BYTE_ORDER == BIG_ENDIAN)
718-
cdb[2] = ((uint8_t *) &lba)[2];
719-
cdb[3] = ((uint8_t *) &lba)[3];
720-
cdb[4] = ((uint8_t *) &lba)[0];
721-
cdb[5] = ((uint8_t *) &lba)[1];
722-
cdb[7] = ((uint8_t *) &len)[0];
723-
cdb[8] = ((uint8_t *) &len)[1];
724-
#else
725-
cdb[2] = ((uint8_t *) &lba)[3];
726-
cdb[3] = ((uint8_t *) &lba)[2];
727-
cdb[4] = ((uint8_t *) &lba)[1];
728-
cdb[5] = ((uint8_t *) &lba)[0];
729-
cdb[7] = ((uint8_t *) &len)[1];
730-
cdb[8] = ((uint8_t *) &len)[0];
731-
#endif
675+
lba2cdb(cdb, &lba, &len);
732676
break;
733677
case WRITE_10:
734678
output = 1;
735679
trans_len = block_len;
736680
cdb[0] = WRITE_10;
737681
cdb[1] = lun << 5;
738-
739-
/* Strongarm aligns on word boundaries. */
740-
/* So HTONL and NTOHL won't work here. */
741-
742-
#if (BYTE_ORDER == BIG_ENDIAN)
743-
cdb[2] = ((uint8_t *) &lba)[2];
744-
cdb[3] = ((uint8_t *) &lba)[3];
745-
cdb[4] = ((uint8_t *) &lba)[0];
746-
cdb[5] = ((uint8_t *) &lba)[1];
747-
cdb[7] = ((uint8_t *) &len)[0];
748-
cdb[8] = ((uint8_t *) &len)[1];
749-
#else
750-
cdb[2] = ((uint8_t *) &lba)[3];
751-
cdb[3] = ((uint8_t *) &lba)[2];
752-
cdb[4] = ((uint8_t *) &lba)[1];
753-
cdb[5] = ((uint8_t *) &lba)[0];
754-
cdb[7] = ((uint8_t *) &len)[1];
755-
cdb[8] = ((uint8_t *) &len)[0];
756-
#endif
682+
lba2cdb(cdb, &lba, &len);
757683
break;
758684
default:
759685
iscsi_trace_error("op 0x%x not implemented\n", op);
@@ -892,25 +818,7 @@ scatter_gather_test(uint64_t target, uint32_t lun, uint8_t op)
892818
sg[i].iov_base = buff[i];
893819
sg[i].iov_len = block_len;
894820
}
895-
896-
/* Strongarm aligns on word boundaries. */
897-
/* So HTONL and NTOHL won't work here. */
898-
899-
#if (BYTE_ORDER == BIG_ENDIAN)
900-
cdb[2] = ((uint8_t *) &lba)[2];
901-
cdb[3] = ((uint8_t *) &lba)[3];
902-
cdb[4] = ((uint8_t *) &lba)[0];
903-
cdb[5] = ((uint8_t *) &lba)[1];
904-
cdb[7] = ((uint8_t *) &len)[0];
905-
cdb[8] = ((uint8_t *) &len)[1];
906-
#else
907-
cdb[2] = ((uint8_t *) &lba)[3];
908-
cdb[3] = ((uint8_t *) &lba)[2];
909-
cdb[4] = ((uint8_t *) &lba)[1];
910-
cdb[5] = ((uint8_t *) &lba)[0];
911-
cdb[7] = ((uint8_t *) &len)[1];
912-
cdb[8] = ((uint8_t *) &len)[0];
913-
#endif
821+
lba2cdb(cdb, &lba, &len);
914822

915823
gettimeofday(&t_start, 0);
916824

0 commit comments

Comments
 (0)