From: Matthew Fioravante Subject: [PATCH mini-os enhancements for vtpm 2/8] add posix io to blkfront Date: Mon, 17 Sep 2012 17:55:43 -0400 Message-ID: <50579C5F.5040004@jhuapl.edu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============6453934534774364880==" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Samuel Thibault , "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a cryptographically signed message in MIME format. --===============6453934534774364880== Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="------------ms030904070101060306020108" This is a cryptographically signed message in MIME format. --------------ms030904070101060306020108 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable This patch adds posix io support (read,write,lseek) to block devices using blkfront Signed off by Matthew Fioravante matthew.fioravante@jhuapl.edu diff --git a/extras/mini-os/blkfront.c b/extras/mini-os/blkfront.c --- a/extras/mini-os/blkfront.c +++ b/extras/mini-os/blkfront.c @@ -392,6 +392,7 @@ void blkfront_aio(struct blkfront_aiocb *aiocbp, int write) static void blkfront_aio_cb(struct blkfront_aiocb *aiocbp, int ret) { aiocbp->data =3D (void*) 1; + aiocbp->aio_cb =3D NULL; } =20 void blkfront_io(struct blkfront_aiocb *aiocbp, int write) @@ -547,9 +548,150 @@ moretodo: #ifdef HAVE_LIBC int blkfront_open(struct blkfront_dev *dev) { + /* Silently prevent multiple opens */ + if(dev->fd !=3D -1) { + return dev->fd; + } dev->fd =3D alloc_fd(FTYPE_BLK); printk("blk_open(%s) -> %d\n", dev->nodename, dev->fd); files[dev->fd].blk.dev =3D dev; + files[dev->fd].blk.offset =3D 0; return dev->fd; } + +int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write) +{ + struct blkfront_dev* dev =3D files[fd].blk.dev; + off_t offset =3D files[fd].blk.offset; + struct blkfront_aiocb aiocb; + unsigned long long disksize =3D dev->info.sectors * dev->info.sector_= size; + unsigned int blocksize =3D dev->info.sector_size; + + int blknum; + int blkoff; + size_t bytes; + int rc =3D 0; + int alignedbuf =3D 0; + uint8_t* copybuf =3D NULL; + + /* RW 0 bytes is just a NOP */ + if(count =3D=3D 0) { + return 0; + } + /* Check for NULL buffer */ + if( buf =3D=3D NULL ) { + errno =3D EFAULT; + return -1; + } + + /* Write mode checks */ + if(write) { + /*Make sure we have write permission */ + if(dev->info.info & VDISK_READONLY || (dev->info.mode !=3D O_RDWR = && dev->info.mode !=3D O_WRONLY)) { + errno =3D EACCES; + return -1; + } + /*Make sure disk is big enough for this write */ + if(offset + count > disksize) { + errno =3D ENOSPC; + return -1; + } + } + /* Read mode checks */ + else + { + /*If the requested read is bigger than the disk, just + * read as much as we can until the end */ + if(offset + count > disksize) { + count =3D offset >=3D disksize ? 0 : disksize - offset; + } + } + /* Determine which block to start at and which offset inside of it */= + blknum =3D offset / blocksize; + blkoff =3D offset % blocksize; + + /* Optimization: We need to check if buf is aligned to the sector siz= e. + * This is somewhat tricky code. We have to add the blocksize - block offset + * because the first block may be a partial block and then for every subsequent + * block rw the buffer will be offset.*/ + if(!((uintptr_t) (buf +(blocksize - blkoff)) & (dev->info.sector_size-1))) { + alignedbuf =3D 1; + } + + /* Setup aiocb block object */ + aiocb.aio_dev =3D dev; + aiocb.aio_nbytes =3D blocksize; + aiocb.aio_offset =3D blknum * blocksize; + aiocb.aio_cb =3D NULL; + aiocb.data =3D NULL; + + /* If our buffer is unaligned or its aligned but we will need to rw a partial block + * then a copy will have to be done */ + if(!alignedbuf || blkoff !=3D 0 || count % blocksize !=3D 0) { + copybuf =3D _xmalloc(blocksize, dev->info.sector_size); + } + + rc =3D count; + while(count > 0) { + /* determine how many bytes to read/write from/to the current block buffer */ + bytes =3D count > (blocksize - blkoff) ? blocksize - blkoff : coun= t; + + /* read operation */ + if(!write) { + if (alignedbuf && bytes >=3D blocksize) { + /* If aligned and were reading a whole block, just read right into buf */ + aiocb.aio_buf =3D buf; + blkfront_read(&aiocb); + } else { + /* If not then we have to do a copy */ + aiocb.aio_buf =3D copybuf; + blkfront_read(&aiocb); + memcpy(buf, ©buf[blkoff], bytes); + } + } + /* Write operation */ + else { + if(alignedbuf && bytes >=3D blocksize) { + /* If aligned and were writing a whole block, just write directly from buf */ + aiocb.aio_buf =3D buf; + blkfront_write(&aiocb); + } else { + /* If not then we have to do a copy. */ + aiocb.aio_buf =3D copybuf; + /* If we're writing a partial block, we need to read the current contents first + * so we don't overwrite the extra bits with garbage */ + if(blkoff !=3D 0 || bytes < blocksize) { + blkfront_read(&aiocb); + } + memcpy(©buf[blkoff], buf, bytes); + blkfront_write(&aiocb); + } + } + /* Will start at beginning of all remaining blocks */ + blkoff =3D 0; + + /* Increment counters and continue */ + count -=3D bytes; + buf +=3D bytes; + aiocb.aio_offset +=3D blocksize; + } + + free(copybuf); + files[fd].blk.offset +=3D rc; + return rc; + +} + +int blkfront_posix_fstat(int fd, struct stat* buf) +{ + struct blkfront_dev* dev =3D files[fd].blk.dev; + + buf->st_mode =3D dev->info.mode; + buf->st_uid =3D 0; + buf->st_gid =3D 0; + buf->st_size =3D dev->info.sectors * dev->info.sector_size; + buf->st_atime =3D buf->st_mtime =3D buf->st_ctime =3D time(NULL); + + return 0; +} #endif diff --git a/extras/mini-os/include/blkfront.h b/extras/mini-os/include/blkfront.h --- a/extras/mini-os/include/blkfront.h +++ b/extras/mini-os/include/blkfront.h @@ -28,7 +28,17 @@ struct blkfront_info }; struct blkfront_dev *init_blkfront(char *nodename, struct blkfront_info *info); #ifdef HAVE_LIBC +#include +/* POSIX IO functions: + * use blkfront_open() to get a file descriptor to the block device + * Don't use the other blkfront posix functions here directly, instead u= se + * read(), write(), lseek() and fstat() on the file descriptor + */ int blkfront_open(struct blkfront_dev *dev); +int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write); +#define blkfront_posix_write(fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 1) +#define blkfront_posix_read(fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 0) +int blkfront_posix_fstat(int fd, struct stat* buf); #endif void blkfront_aio(struct blkfront_aiocb *aiocbp, int write); #define blkfront_aio_read(aiocbp) blkfront_aio(aiocbp, 0) diff --git a/extras/mini-os/include/lib.h b/extras/mini-os/include/lib.h --- a/extras/mini-os/include/lib.h +++ b/extras/mini-os/include/lib.h @@ -174,6 +174,7 @@ extern struct file { } tap; struct { struct blkfront_dev *dev; + off_t offset; } blk; struct { struct kbdfront_dev *dev; diff --git a/extras/mini-os/lib/sys.c b/extras/mini-os/lib/sys.c --- a/extras/mini-os/lib/sys.c +++ b/extras/mini-os/lib/sys.c @@ -289,6 +289,11 @@ int read(int fd, void *buf, size_t nbytes) return ret * sizeof(union xenfb_in_event); } #endif +#ifdef CONFIG_BLKFRONT + case FTYPE_BLK: { + return blkfront_posix_read(fd, buf, nbytes); + } +#endif default: break; } @@ -321,6 +326,10 @@ int write(int fd, const void *buf, size_t nbytes) netfront_xmit(files[fd].tap.dev, (void*) buf, nbytes); return nbytes; #endif +#ifdef CONFIG_BLKFRONT + case FTYPE_BLK: + return blkfront_posix_write(fd, buf, nbytes); +#endif default: break; } @@ -331,8 +340,37 @@ int write(int fd, const void *buf, size_t nbytes) =20 off_t lseek(int fd, off_t offset, int whence) { - errno =3D ESPIPE; - return (off_t) -1; + switch(files[fd].type) { +#ifdef CONFIG_BLKFRONT + case FTYPE_BLK: + switch (whence) { + case SEEK_SET: + files[fd].file.offset =3D offset; + break; + case SEEK_CUR: + files[fd].file.offset +=3D offset; + break; + case SEEK_END: + { + struct stat st; + int ret; + ret =3D fstat(fd, &st); + if (ret) + return -1; + files[fd].file.offset =3D st.st_size + offset; + break; + } + default: + errno =3D EINVAL; + return -1; + } + return files[fd].file.offset; + break; +#endif + default: /* Not implemented on this FTYPE */ + errno =3D ESPIPE; + return (off_t) -1; + } } =20 int fsync(int fd) { @@ -445,6 +483,10 @@ int fstat(int fd, struct stat *buf) buf->st_ctime =3D time(NULL); return 0; } +#ifdef CONFIG_BLKFRONT + case FTYPE_BLK: + return blkfront_posix_fstat(fd, buf); +#endif default: break; } --------------ms030904070101060306020108 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIDyjCC A8YwggMvoAMCAQICBD/xyf0wDQYJKoZIhvcNAQEFBQAwLzELMAkGA1UEBhMCVVMxDzANBgNV BAoTBkpIVUFQTDEPMA0GA1UECxMGQklTRENBMB4XDTEwMDYxMTE4MjIwNloXDTEzMDYxMTE4 NTIwNlowZjELMAkGA1UEBhMCVVMxDzANBgNVBAoTBkpIVUFQTDEPMA0GA1UECxMGUGVvcGxl MTUwFgYDVQQLEw9WUE5Hcm91cC1CSVNEQ0EwGwYDVQQDExRNYXR0aGV3IEUgRmlvcmF2YW50 ZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAnpbwVSP6o1Nb5lcW7dd3yTo9iBJdi7qz 4nANOMFPK7JOy5npKN1iiousl28U/scUJES55gPwAWYJK3uVyQAsA4adgDKi5DoD1UHDQEwp bY7iHLJeq0NPr4BqYNqnCFPbE6HC8zSJrr4qKn+gVUQT39SIFqdiIPJwZL8FYTRQ/zsCAwEA AaOCAbYwggGyMAsGA1UdDwQEAwIHgDArBgNVHRAEJDAigA8yMDEwMDYxMTE4MjIwNlqBDzIw MTIwNzE3MjI1MjA2WjAbBg0rBgEEAbMlCwMBAQEBBAoWCGZpb3JhbWUxMBsGDSsGAQQBsyUL AwEBAQIEChIIMDAxMDQyNjEwWAYJYIZIAYb6ax4BBEsMSVRoZSBwcml2YXRlIGtleSBjb3Jy ZXNwb25kaW5nIHRvIHRoaXMgY2VydGlmaWNhdGUgbWF5IGhhdmUgYmVlbiBleHBvcnRlZC4w KAYD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HHR8aSX4995Dx4MtzK/scz7d5LnAQAAAAAA AA== --------------ms030904070101060306020108-- --===============6453934534774364880== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --===============6453934534774364880==--