Bug 117015

Summary: net/iscsi-target fails when exporting a disk device
Product: Ports & Packages Reporter: Javier Martín Rueda <jmrueda>
Component: Individual Port(s)Assignee: Boris Samorodov <bsam>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Javier Martín Rueda 2007-10-08 15:10:00 UTC
This report tackles two bugs at the same time, but I report them at the same time because the fix is common (actually, I found bug number two when trying to correct bug number one).

Number one:

If you define an extent based on a disk device, such as /dev/da0, /dev/da0s1 or similar, the iscsi-target daemon complains that it cannot allocate space for the target and then aborts.

Number two:

If you define and extent based on an existing disk file of the exact size, the initialization code will append one byte of rubbish at the end of it, although everything will work fine appart from that.

See the detailed examples in "how to repeat the problem".

Fix: The initialization code attempts to see if the extent file exists by lseeking to its size minus 1, and then reads and writes 1 byte. The write is done to create the file if it did not exist previously. That provokes two bugs:

Number one:

If the extent file is a disk device, you cannot just read 1 byte, because the kernel will reply with EINVAL. You have to read a multiple of the sector size, and I think it has to be aligned with the sector size too. My proposed fix is to lseek to the extent size minus 512 and then read and write 512 bytes. This accomplish the same effect as the original code, but does not fail when the extent file is a disk device. Note: instead of 512, maybe I should use some predefined constant. Maybe S_BLKSIZE in /usr/include/sys/stat.h?

Number two:

If the extent file exists and its size is at least as big as what you specified in the iscsi_target config file, the read operation will advance the file pointer to the end, and the subsequent write will append an extra superfluos byte at the end, or overwrite one extra byte out of range if the file was larger. Obviously, the code was written thinking of the case in which the file does not exist or is smaller than the exported size, because in such case the read does not advance the file pointer, and everything works fine.

My proposed fix basically is to write only if the read did not get anything.

I enclose a patch file for the port. Just drop it in /usr/ports/net/iscsi-target/files, recompile and reinstall.

Patch attached with submission follows:
How-To-Repeat: Number one:

Create the following config file in /usr/local/etc/iscsi/targets:

extent0         /dev/da0                0       100MB
target0         rw      extent0         0.0.0.0/0

Then start the daemon and you will get the error message specified in the full description of the problem report:

# /usr/local/etc/rc.d/iscsi_target start
Starting iscsi_target.
Reading configuration from `/usr/local/etc/iscsi/targets'
target0:rw:0.0.0.0/0
        extent0:/dev/da0:0:104857600
DISK: 1 logical unit (204800 blocks, 512 bytes/block), type iscsi fs
DISK: LUN 0: pid 4623:disk.c:770: ***ERROR*** error reading "target0"pid 4623:disk.c:911: ***ERROR*** error allocating space for "target0"pid 4623:target.c:1496: ***ERROR*** device_init() failed
pid 4623:iscsi-target.c:145: ***ERROR*** target_init() failed


Number two:

Create the following config file in /usr/local/etc/iscsi/targets:

extent0         /my_example                0       100MB
target0         rw      extent0         0.0.0.0/0

Now execute the following:

# dd if=/dev/zero of=/my_example bs=1m count=100
# ls -l /my_example
-rw-r--r--  1 root  wheel  104857600 Oct  8 15:43 /my_example
# /usr/local/etc/rc.d/iscsi_target start
Starting iscsi_target.
Reading configuration from `/usr/local/etc/iscsi/targets'
target0:rw:0.0.0.0/0
        extent0:/my_example:0:104857600
DISK: 1 logical unit (204800 blocks, 512 bytes/block), type iscsi fs
DISK: LUN 0: 100 MB disk storage for "target0"
TARGET: TargetName is iqn.1994-04.org.netbsd.iscsi-target
# ls -l /my_example
-rw-r--r--  1 root  wheel  104857601 Oct  8 15:44 /my_example

As you can see, now the file is one byte longer than before.
Comment 1 Edwin Groothuis freebsd_committer freebsd_triage 2007-10-08 22:06:24 UTC
State Changed
From-To: open->feedback

Awaiting maintainers feedback (via the GNATS Auto Assign Tool)
Comment 2 mark 2007-10-08 23:38:43 UTC
Thanks for your report and suggested patch. The inability to use block
devices for storage has been an ongoing concern for the iscsi-target port.

I have forwarded this bug report on to Alistair Crooks, the author and
maintainer of the netbsd-iscsi package for review. In my opinion the
patch is better suited for inclusion into the upstream package. If he is
unwilling or unable to patch the source I will consider it as a local
patch for the FreeBSD derivative. Stay tuned.

-- 
Said one park ranger, 'There is considerable overlap between the 
 intelligence of the smartest bears and the dumbest tourists.'
Mark D. Foster, CISSP <mark@foster.cc>  http://mark.foster.cc/
Comment 3 mark 2007-10-09 15:35:56 UTC
Author has a fix for this issue already in the development code. He has
indicated it will be included in the next release.

-- 
Said one park ranger, 'There is considerable overlap between the 
 intelligence of the smartest bears and the dumbest tourists.'
Mark D. Foster, CISSP <mark@foster.cc>  http://mark.foster.cc/
Comment 4 Boris Samorodov freebsd_committer freebsd_triage 2007-11-05 12:19:56 UTC
Responsible Changed
From-To: freebsd-ports-bugs->bsam

Take.
Comment 5 Boris Samorodov freebsd_committer freebsd_triage 2007-11-05 12:21:24 UTC
State Changed
From-To: feedback->suspended

PR/117690 should fix the case.
Comment 6 Boris B.Samorodov 2007-11-05 12:39:37 UTC
Hi!

Can you confirm that the patch from
http://www.freebsd.org/cgi/query-pr.cgi?pr=ports/117690
fixes errors for you?

WBR
-- 
bsam
Comment 7 dfilter service freebsd_committer freebsd_triage 2007-11-12 17:51:40 UTC
bsam        2007-11-12 17:51:25 UTC

  FreeBSD ports repository

  Modified files:
    net/iscsi-target     Makefile distinfo 
  Log:
  . update port to 20071025 snapshot;
  . fix the bugs from ports/117015 (OK feedback received by email).
  
  PR:             117015 [1], 117690 [2]
  Submitted by:   Javier Martin Rueda <jmrueda at diatel.upm.es>  [1]
                  Mark Foster <mark at foster.cc> (maintainer)    [2]
  Feedback by:    Javier Martin Rueda <jmrueda at diatel.upm.es>
  Approved by:    portmgr (linimon)
  
  Revision  Changes    Path
  1.7       +2 -2      ports/net/iscsi-target/Makefile
  1.4       +3 -3      ports/net/iscsi-target/distinfo
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 8 Boris Samorodov freebsd_committer freebsd_triage 2007-11-12 17:53:27 UTC
State Changed
From-To: suspended->closed

Fixed by PR/117690. Confirmed by private email. 
Thanks for the report!