2008年8月29日 星期五

random number in driver

get_random_bytes():

ex:
unsigned char uuid[16];
get_random_bytes(uuid, sizeof (uuid));

2008年8月28日 星期四

type definition

struct block_device:
linux/fs.h

dev_t:
linux/types.h

struct page:
linux/mm_types.h

struct gendisk:
linux/genhd.h

struct request_queue:
linux/blkdev.h

wait_queue_head_t:
linux/wait.h

spinlock_t:
linux/spinlock_types.h

struct semaphore:
linux/semaphore.h

2008年8月21日 星期四

dmesg

/var/log/messages

2008年8月19日 星期二

dhclient

get new ip

dm

 //The request function that just remaps the bio built up by dm_merge_bvec
static int dm_request(struct request_queue *q, struct bio *bio)
       call  __split_bio

// Split the bio into several clones.
static int __split_bio(struct mapped_device *md, struct bio *bio)
       call  __clone_and_map with parameter ci
       ( ci.sector_count= bio_sectors(bio)  )

static int __clone_and_map(struct clone_info *ci)
call  split_bvec

2008年8月18日 星期一

sector_t

typedef unsigned long sector_t;

2008年8月17日 星期日

mkfs

create filesystem
ex:
mkfs -v  /dev/hda3

struct bio

struct bio:
struct block_device  *bi_bdev;

struct bio_vec          *bi_io_vec;     /* the actual vec list */

unsigned short          bi_vcnt;        /* how many bio_vec's */

unsigned int            bi_size;        /* residual I/O count */

struct bio_vec:
  struct page     *bv_page;
  unsigned int    bv_len;
  unsigned int    bv_offset;

do I/O without request queue

three steps:
1. define a make_request function
ex:
static int sbull_make_request(request_queue_t *q, struct bio *bio) 

     in the make_request function, transfer bio & call bio_endio(struct bio *bio, unsigned int bytes, int error) to signal completion 

2. use blk_alloc_queue(GFP_KERNEL) to allocate a queue 

3. call blk_queue_make_request(request_queue_t *queue, make_request_fn *func)

when make_request function return non-zero:
the bio is submitted again. Hence, we can modify bi_bdev & starting sector to redirect bio to another device

split bio into multiple chunks:
bio_split

kthread

show kthread list
ps  -ef

2008年8月16日 星期六

interrupt

prevent interrupt entering critical section 
method 1:
 local_irq_disable();  /* Disable Interrupts in local CPU */
  /* ... Critical Section ...  */
  local_irq_enable();   /* Enable Interrupts in local CPU */

method 2:
unsigned long flags;

Point A:
  local_irq_save(flags);     /* Disable Interrupts */
  /* ... Critical Section ... */
  local_irq_restore(flags);  /* Restore state to what it was at point A */

spinlock & mutex

spinlock ensures only one thread can enter critical section
spinlock example:

#include
spinlock_t mylock = SPIN_LOCK_UNLOCKED; /* Initialize */

/* Acquire the spinlock. This is inexpensive if there
 * is no one inside the critical section. In the face of
 * contention, spinlock() has to busy-wait.
 */
spin_lock(&mylock);

/* ... Critical Section code ... */

spin_unlock(&mylock); /* Release the lock */

spinlock will busy-wait,  mutex will sleep
mutex example:
#include

/* Statically declare a mutex. To dynamically
   create a mutex, use mutex_init() */
static DEFINE_MUTEX(mymutex);

/* Acquire the mutex. This is inexpensive if there
 * is no one inside the critical section. In the face of
 * contention, mutex_lock() puts the calling thread to sleep.
 */
mutex_lock(&mymutex);

/* ... Critical Section code ... */

mutex_unlock(&mymutex);      /* Release the mutex */

linux開機設定

GRUB:
/boot/grub/grub.conf  or /boot/grub/menu.lst
grub.conf example:
default 0  #Boot the 2.6.23 kernel by default
timeout 5  #5 second to alter boot order or parameters

title kernel 2.6.23     #Boot Option 1
  #The boot image resides in the first partition of the first disk
  #under the /boot/ directory and is named vmlinuz-2.6.23. 'ro'
  #indicates that the root partition should be mounted read-only.
  kernel (hd0,0)/boot/vmlinuz-2.6.23 ro root=/dev/hda1

  #Look under section "Freeing initrd memory:387k freed"
  initrd (hd0,0)/boot/initrd

#...

LILO:
/etc/lilo.conf

compile driver

method 1:
Makefile:
      obj-m+=test.ko

make -C /usr/src/kernels/x.y.z/  M=`pwd`  modules

kernel source module

compile module:
1. cd  /usr/src/linux-x.y.z/
2. make modules

install module
1.  make modules_install

the generated module( ko)  is stored in /lib/modules/x.y.z/kernel/ 

2008年8月15日 星期五

patch

A patch is a text file containing source code differences between a development tree and the original snapshot from which the developer started work

apply patch:
ex:  if x.y.z-mm2.bz2 is a patch, go to the directory of linux source
      bzip2  -dc  ../x.y.z-mm2.bz2 |  patch -p1

create patch:
ex:
diff  -Nur   /path/originalKernal/    /path/newKernel  >  change changes.patch

global code browser

install:
http://www.gnu.org/software/global/global.html

run:
1. go to directory of source code
2. gtags
3. htags  ./codeTrace/
the generated html is in ./codeTrace/

2008年8月13日 星期三

find command

find  .  -name  "test.txt"
find text.txt under current directory

linux module location

/lib/modules/2.6.18-92.el5/kernel/drivers

2008年8月12日 星期二

HZ & jiffies

HZ是用來定義每一秒有幾次timer interrupts(time tick)

jiffies:
the number of time ticks since kernel start

ex:
unsigned long timeout=jiffies+ 3*HZ;
//  the time after 3 second
time_after(jiffies, timeout)

convert from jiffies to seconds
divide by HZ

time related methods:
time_after:  parameter is time tick 
time_before: parameter is time tick
schedule_timeout: parameter is time tick  
only schedule_timeout will give up processor

wait_event_timeout:
msleep: parameter is milliseconds

timer example( do something in the future):
#include

struct timer_list my_timer;

init_timer(&my_timer);            /* Also see setup_timer() */
my_timer.expire = jiffies + n*HZ; /* n is the timeout in number
                                     of seconds */
my_timer.function = timer_func;   /* Function to execute
                                     after n seconds */
my_timer.data = func_parameter;   /* Parameter to be passed
                                     to timer_func */
add_timer(&my_timer);             /* Start the timer */

2008年8月10日 星期日

smp

smp(多cpu)

2008年8月8日 星期五

rpm

remove rpm:
rpm -e test

query info about specific rpm
ex: 
rpm -q  test

2008年8月7日 星期四

rpm for red hat and cent os

Because red hat & cent os are related , we can get rpm for ret hat from rpm for cent os 

init network

開機網路設定:
/etc/sysconfig/network/
ifcfg-lo
ifcfg-eth0

2008年8月6日 星期三

blk_queue_make_request

 void blk_queue_make_request (request_queue_t * q, make_request_fn * mfn);

define an alternate make_request function for a device

ARGUMENTS

q

the request queue for the device to be affected
mfn
the alternate make_request function