Monthly Archives: March 2015

LIO manually create store

#!/bin/bash

#run tcm_dump --s

mount -t configfs configfs /sys/kernel/config/

mkdir /sys/kernel/config/target/core/fileio_0
tcm_node --establishdev fileio_0/mystore0 fd_dev_name=mystore0.img,fd_dev_size=10737418240
tcm_node --setunitserialwithmd fileio_0/mystore0 0d33cd79-5170-4f18-b9c1-1aee80db226b
#### ALUA Target Port Groups
tcm_node --addaluatpgwithmd fileio_0/mystore0 default_tg_pt_gp 0
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/alua/default_tg_pt_gp/alua_access_type
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/alua/default_tg_pt_gp/preferred
echo 100 > /sys/kernel/config/target/core/fileio_0/mystore0/alua/default_tg_pt_gp/nonop_delay_msecs
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/alua/default_tg_pt_gp/trans_delay_msecs
#### Attributes for /sys/kernel/config/target/core/fileio_0/mystore0
echo 4096 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/max_write_same_len
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/unmap_granularity_alignment
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/unmap_granularity
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/max_unmap_block_desc_count
echo 8192 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/max_unmap_lba_count
echo 128 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/queue_depth
echo 8192 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/optimal_sectors
#echo 8192 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/fabric_max_sectors
echo 512 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/block_size
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_rest_reord
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/is_nonrot
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/enforce_pr_isids
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_3pc
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_caw
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_tpws
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_tpu
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_tas
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_ua_intlck_ctrl
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_write_cache
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_fua_read
echo 1 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_fua_write
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_dpo
echo 0 > /sys/kernel/config/target/core/fileio_0/mystore0/attrib/emulate_model_alias

#loopback
mkdir /sys/kernel/config/target/loopback
mkdir /sys/kernel/config/target/loopback/naa.60014055f195952b 
mkdir /sys/kernel/config/target/loopback/naa.60014055f195952b/tpgt_1
mkdir /sys/kernel/config/target/loopback/naa.60014055f195952b/tpgt_1/lun/lun_0
echo naa.60014058861c1305 > /sys/kernel/config/target/loopback/naa.60014055f195952b/tpgt_1/nexus
cd /sys/kernel/config/target/loopback/naa.60014055f195952b/tpgt_1/lun/lun_0
ln -sf ../../../../../../target/core/fileio_0/mystore0/ e9ef58132b

 

closure example

/* (C) IT Sky Consulting GmbH 2014
 * http://www.it-sky-consulting.com/
 * Author: Karl Brodowsky
 * Date: 2014-02-27
 * License: GPL v2 (See https://de.wikipedia.org/wiki/GNU_General_Public_License )
 *
 * This file is inspired by http://cs.baylor.edu/~donahoo/practical/CSockets/code/HandleTCPClient.c
 */

#include <errno.h>
#include <stdio.h>      /* for printf() and fprintf() and ... */
#include <stdlib.h>     /* for atoi() and exit() and ... */
#include <string.h>     /* for memset() and ... */
#include <sys/types.h>
#include <unistd.h>     /* for close() */

void usage(const char *argv0, const char *msg) {
  if (msg != NULL && strlen(msg) > 0) {
    printf("%s\n\n", msg);
  }
  printf("Usage\n\n");
  printf("%s x y\n", argv0);
  printf("creates closure adder which adds x to given param\n");
  printf("prints i+x for i=0..y-1\n");
  exit(1);
}

struct closure;

typedef int (*fun_type)(const struct closure *context, const int param);

struct closure {
  int x;
  fun_type fun;
};

int f(const struct closure *context, const int param) {
  return (context->x) + param;
}

struct closure *adder(int x) {
  struct closure *result = malloc(sizeof(struct closure));
  result->x = x;
  result->fun = f;
  return  result;
}

int main(int argc, char *argv[]) {

  int retcode;

  if (argc < 2) {
    usage(argv[0], "not enough parameters");
  }
  int x = atoi(argv[1]);
  int y = atoi(argv[2]);
  struct closure *cl = adder(x);
  int i;
  for (i = 0; i < y; i++) {
    printf("cl(%d)=%d\n", i, cl->fun(cl, i));
  }
}

 

spinlock implementation example

#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <stdio.h>

//Reference: http://opensource.apple.com/source/samba/samba-26/source/tdb/spinlock.c
//cc -pthread -o spinlock spinlock.c

typedef int spinlock_t;

#define cpu_relax() asm volatile("pause\n": : :"memory")

static inline int spin_trylock(spinlock_t *lock)
{
	int oldval;

	asm volatile("xchgl %0,%1"
		: "=r" (oldval), "=m" (*lock)
		: "0" (0)
		: "memory");

	return oldval > 0 ? 0 : 1;
}

static inline int spin_is_locked(spinlock_t *lock)
{
	return (*lock != 1);
}

static inline void spin_lock(spinlock_t *lock)
{
	while (spin_trylock(lock)) {
		while (spin_is_locked(lock))
			cpu_relax();
	}
}

static inline void spin_unlock(spinlock_t *lock)
{
	asm volatile("":::"memory");
	*lock = 1;
}

static inline void spin_lock_init(spinlock_t *lock)
{
	*lock = 1;
}

static int gbl_data;
static spinlock_t mylock;

static void *child_func(void *p)
{
	while (1) {
		spin_lock(&mylock);

		gbl_data = 0;
		assert(gbl_data == 0);

		spin_unlock(&mylock);
	}
}

int main()
{
	pthread_t thread;

	spin_lock_init(&mylock);

	if(pthread_create(&thread, NULL, child_func, NULL)) {
		fprintf(stderr, "Error creating thread\n");
		return 1;
	}

	while (1) {
		spin_lock(&mylock);

		gbl_data = 1;
		assert(gbl_data == 1);

		spin_unlock(&mylock);
	}

	return 0;
}