List, Tomo (right list this time:-) ) Please find attached a small patch to the three bs_*.c files used for SBC and MMC devices. The patch changes the *_open() function in such way that if the normal open for O_RDWR fails with EACCES, we try to open the file with O_RDONLY instead, and set the LUN to read-only. This allows to set the backing file to "chattr +i" for those filesystems that support these attributes and automatically flags them to read-only. In particular for ISO images I find it very nice to have them set to immutable to prevent any process from accidentally writing/modifying them. regards ronnie sahlberg
From 4325f9d89ac3c76441bd3dae44491af999407654 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx> Date: Sun, 10 Oct 2010 15:37:23 +1100 Subject: [PATCH] Update bs_*_open() to try to open the backend file in read-only mode in case the normal open in O_RDWR fails with EACCES. If so, automatically flag this lun as read-only. This allows files for read-only LUNS (read-only disk files and/or cd/dvd iso extra attributes. Signed-off-by: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx> --- usr/bs_aio.c | 6 ++++++ usr/bs_mmap.c | 7 ++++++- usr/bs_rdwr.c | 8 +++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/usr/bs_aio.c b/usr/bs_aio.c index 0d6a640..ce56b68 100644 --- a/usr/bs_aio.c +++ b/usr/bs_aio.c @@ -113,6 +113,12 @@ get_events: static int bs_aio_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size) { *fd = backed_file_open(path, O_RDWR|O_LARGEFILE|O_DIRECT, size); + /* If we get access denied, try opening the file in readonly mode */ + if (*fd == -1 && errno == EACCES) { + *fd = backed_file_open(path, O_RDONLY|O_LARGEFILE|O_DIRECT, + size); + lu->attrs.readonly = 1; + } if (*fd < 0) return *fd; return 0; diff --git a/usr/bs_mmap.c b/usr/bs_mmap.c index b62c8e6..f7718f2 100644 --- a/usr/bs_mmap.c +++ b/usr/bs_mmap.c @@ -78,7 +78,12 @@ static void bs_mmap_request(struct scsi_cmd *cmd) static int bs_mmap_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size) { - *fd = backed_file_open(path, O_RDWR| O_LARGEFILE, size); + *fd = backed_file_open(path, O_RDWR|O_LARGEFILE, size); + /* If we get access denied, try opening the file in readonly mode */ + if (*fd == -1 && errno == EACCES) { + *fd = backed_file_open(path, O_RDONLY|O_LARGEFILE, size); + lu->attrs.readonly = 1; + } if (*fd < 0) return *fd; diff --git a/usr/bs_rdwr.c b/usr/bs_rdwr.c index 5d265e5..77d52e3 100644 --- a/usr/bs_rdwr.c +++ b/usr/bs_rdwr.c @@ -130,7 +130,13 @@ static void bs_rdwr_request(struct scsi_cmd *cmd) static int bs_rdwr_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size) { - *fd = backed_file_open(path, O_RDWR | O_LARGEFILE | lu->bsoflags, size); + *fd = backed_file_open(path, O_RDWR|O_LARGEFILE|lu->bsoflags, size); + /* If we get access denied, try opening the file in readonly mode */ + if (*fd == -1 && errno == EACCES) { + *fd = backed_file_open(path, O_RDONLY|O_LARGEFILE|lu->bsoflags, + size); + lu->attrs.readonly = 1; + } if (*fd < 0) return *fd; -- 1.7.3.1
Attachment:
0001-Update-bs_-_open-to-try-to-open-the-backend-file-in-.patch.gz
Description: GNU Zip compressed data