1.调试模块debug.h
#ifndef _DEBUG_H
#define _DEBUG_H

#include 
<linux/kernel.h>
#include 
<linux/types.h>

#if defined(__KERNEL__)
#define PRINT printk
#else
#include 
<stdio.h>
#define PRINT printf
#endif

#if !defined(CONFIG_ISCSI_DEBUG)

#define TRACE_SET(mask) do { } while(0)
#define TRACE_GET(mask) do { } while(0)
#define TRACE_TEST(mask) (0)
#define TRACE(mask, args) do { } while(0)
#define TRACE_BUFFER(mask, buffer, len, args) do { } while(0)

#else

#define TRACE_ENDING            0x0000
#define TRACE_DEBUG             0x0001
#define TRACE_ISCSI_FULL        0x0002
#define TRACE_ISCSI             0x0004
#define TRACE_NET               0x0008
#define TRACE_BUF               0x0010
#define TRACE_SEM               0x0020
#define TRACE_ENTER_LEAVE       0x0040
#define TRACE_MY_MEMORY         0x0080
#define TRACE_TIMERS            0x0100
#define TRACE_ERROR_RECOVERY    0x0200
#define TRACE_VERBOSE           0x0400
#define TRACE_ISER              0x0800
#define TRACE_CHAP              0x1000
#define TRACE_SRP               0x2000
#define TRACE_TASK_MGT          0x4000
#define TRACE_VFS               0x8000
#define TRACE_ALL               0xffff

#ifdef CONFIG_ISCSI_DEBUG

static __u32 iscsi_trace_mask =
        TRACE_DEBUG 
| TRACE_ISCSI_FULL | TRACE_ISCSI | TRACE_NET |

        
/*** TRACE_BUF | ***/

        
/*** TRACE_SEM | ***/
        
/*** TRACE_VFS | ***/
        TRACE_ENTER_LEAVE 
|

        
/*** TRACE_MY_MEMORY | ***/

        
/*** TRACE_TIMERS | ***/

        
/*** TRACE_ERROR_RECOVERY | ***/

        
/*** TRACE_ISER | ***/
        TRACE_ENDING;

#endif

#define TRACE_SET(mask)         iscsi_trace_mask = mask

#define TRACE_GET(mask)         mask = iscsi_trace_mask

#define TRACE_TEST(mask)        (iscsi_trace_mask & (mask))

#define TRACE(mask, args)                                               \
        
do {                                                               \
                
if (iscsi_trace_mask & (mask)) {                           \
                        
if (iscsi_trace_mask & TRACE_VERBOSE) {            \
                                PRINT(
"%s:%d:", __FUNCTION__, __LINE__);   \
                        }                                                  \
                        PRINT(args);                                       \
                }                                                          \
        } 
while(0)

#define TRACE_BUFFER(mask, buffer, len, args)                           \
        
do {                                                               \
                
if (iscsi_trace_mask & (mask)) {                           \
                        
int ndx;                                           \
                        PRINT(args);                                       \
                        
for (ndx = 0; ndx < len; ndx++) {                  \
                                
if ((ndx & 0xf== 0) {                    \
                                        PRINT(
"%3d:", ndx);                \
                                }                                          \
                                PRINT(
" %02x",                             \
                                        
*((__u8 *)(buffer)+ndx)); \
                                
if ((ndx & 0xf== 0xf) {                  \
                                        PRINT(
"\n");                       \
                                }                                          \
                        }                                                  \
                        
if ((ndx & 0xf!= 0) {                            \
                                PRINT(
"\n");                               \
                        }                                                  \
                }                                                          \
        } 
while(0)

#endif

#define TRACE_ERROR(args)                                               \
        
do  {                                                              \
                PRINT(
"%s:%d:", __FUNCTION__, __LINE__);                   \
                PRINT(__FILE__ 
": ***ERROR*** " args);                     \
        } 
while(0)

#define TRACE_WARNING(args)                                             \
        
do  {                                                              \
                PRINT(
"***Warning*** " args);                              \
        } 
while(0)

#endif


2.信号量
#include <asm/semaphore.h>

struct semaphore my_mutex_t;

init_MUTEX_LOCKED(
&my_mutex_t);
init_MUTEX(
&my_mutex_t);
sema_init(
&my_mutex_t, count); 

down(
&my_mutex_t);
down_interruptible(
&my_mutex_t);
down_trylock(
&my_mutex_t);

up(
&my_mutex_t);

3.链表
#include <linux/list.h>

struct
 list_head cmd_queue;

INIT_LIST_HEAD (
&cmd_queue);

if(list_empty (&cmd_queue))
{
     
//。。。
}

list_for_each_safe (lptr, next, 
&cmd_queue)
{
    
//。。。
}

list_del (
&cmd_queue->link);

list_add_tail (
&cmd->link, &commands);
//struct list_head commands;