Index: fusefs-ntfs/Makefile =================================================================== RCS file: /home/pcvs/ports/sysutils/fusefs-ntfs/Makefile,v retrieving revision 1.9 diff -u -r1.9 Makefile --- fusefs-ntfs/Makefile 25 Apr 2007 18:55:38 -0000 1.9 +++ fusefs-ntfs/Makefile 2 May 2007 01:32:30 -0000 @@ -7,33 +7,59 @@ PORTNAME= ntfs PORTVERSION= 1.417 CATEGORIES= sysutils -MASTER_SITES= http://www.ntfs-3g.org/ -MASTER_SITE_SUBDIR= fuse +MASTER_SITES= http://www.ntfs-3g.org/:ntfs \ + ${MASTER_SITE_LOCAL:S|$|alepulver/:ublio|} PKGNAMEPREFIX= fusefs- DISTNAME= ${PORTNAME}-3g-${PORTVERSION} EXTRACT_SUFX= .tgz +DISTFILES= ${DISTNAME}${EXTRACT_SUFX}:ntfs MAINTAINER= fjoe@FreeBSD.org COMMENT= Mount NTFS partitions and disk images +BUILD_DEPENDS= fusefs-libs>=2.6.0:${PORTSDIR}/sysutils/fusefs-libs LIB_DEPENDS= fuse.2:${PORTSDIR}/sysutils/fusefs-libs RUN_DEPENDS= ${LOCALBASE}/modules/fuse.ko:${PORTSDIR}/sysutils/fusefs-kmod -BUILD_DEPENDS= fusefs-libs>=2.6.0:${PORTSDIR}/sysutils/fusefs-libs -USE_AUTOTOOLS= libtool:15 -GNU_CONFIGURE= yes -CONFIGURE_TARGET= --build=${ARCH}-portbld-freebsd${OSREL} ONLY_FOR_ARCHS= i386 amd64 USE_LDCONFIG= yes +USE_AUTOTOOLS= libtool:15 +CONFIGURE_TARGET= --build=${ARCH}-portbld-freebsd${OSREL} + +OPTIONS= LOCK "Lock the device when mounting (avoids access)" off \ + UBLIO "Enable user space cache for improved speed" on + MAN8= ntfs-3g.8 -post-patch: - @${CP} ${FILESDIR}/unix_io_raw.c ${WRKSRC}/libntfs-3g +UBLIO_SRC= libublio-20070103 .include -.if ${OSVERSION} < 600000 -IGNORE= depends on kernel module that requires FreeBSD 6 or later +.if defined(WITH_LOCK) +CFLAGS+= -DUSE_LOCK +.endif + +.if defined(WITH_UBLIO) +DISTFILES+= ${UBLIO_SRC}.tar.gz:ublio +EXTRA_PATCHES+= ${FILESDIR}/extra-patch-ublio +CFLAGS+= -DUSE_UBLIO +SUB_FILES+= pkg-message + +post-extract: + @cd ${WRKDIR}/${UBLIO_SRC} && \ + ${CP} ublio.c ${WRKSRC}/libntfs-3g && \ + ${CP} *.h ${WRKSRC}/include/ntfs-3g + +post-install: + @${ECHO_CMD}; ${CAT} ${PKGMESSAGE}; ${ECHO_CMD} +.elif ${OSVERSION} < 700000 +pre-everything:: + @${ECHO_MSG} + @${ECHO_MSG} "WARNING: your system does not have cache support for block devices. This will considerably reduce the performance of this application, please consider enabling the UBLIO option and following the indications in the post-installation message." | ${FMT} + @${ECHO_MSG} .endif +post-patch: + @${REINPLACE_CMD} -e 's|,nonempty||' ${WRKSRC}/src/ntfs-3g.c + .include Index: fusefs-ntfs/distinfo =================================================================== RCS file: /home/pcvs/ports/sysutils/fusefs-ntfs/distinfo,v retrieving revision 1.6 diff -u -r1.6 distinfo --- fusefs-ntfs/distinfo 25 Apr 2007 18:55:38 -0000 1.6 +++ fusefs-ntfs/distinfo 2 May 2007 01:32:30 -0000 @@ -1,3 +1,6 @@ MD5 (ntfs-3g-1.417.tgz) = 9e451f1084461795a96170adb6c4376b SHA256 (ntfs-3g-1.417.tgz) = 6eff152c670cb9cab1c3d423aac1ce7825f3bc5f53be5ff1857c0079bd27ecc0 SIZE (ntfs-3g-1.417.tgz) = 562094 +MD5 (libublio-20070103.tar.gz) = ec8b05fe4d831111f29485b0ab8a0161 +SHA256 (libublio-20070103.tar.gz) = 7c87d1cd7a65ab5ab61f9d7b5279bab6c8169d33d552148d396995c767be919f +SIZE (libublio-20070103.tar.gz) = 17238 Index: fusefs-ntfs/files/extra-patch-ublio =================================================================== RCS file: fusefs-ntfs/files/extra-patch-ublio diff -N fusefs-ntfs/files/extra-patch-ublio --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ fusefs-ntfs/files/extra-patch-ublio 2 May 2007 01:32:30 -0000 @@ -0,0 +1,69 @@ +--- ./libntfs-3g/Makefile.am.orig Fri Apr 6 17:22:09 2007 ++++ ./libntfs-3g/Makefile.am Mon Apr 30 13:34:44 2007 +@@ -54,7 +54,8 @@ + security.c \ + unistr.c \ + version.c \ +- volume.c ++ volume.c \ ++ ublio.c + + AM_CPPFLAGS = $(linux_ntfsincludedir) $(all_includes) + +--- ./src/ntfs-3g.c.orig Sun Apr 15 20:52:01 2007 ++++ ./src/ntfs-3g.c Mon Apr 30 14:12:57 2007 +@@ -1603,7 +1603,11 @@ + .mkdir = ntfs_fuse_mkdir, + .rmdir = ntfs_fuse_rmdir, + .utime = ntfs_fuse_utime, ++#ifdef __linux__ + .bmap = ntfs_fuse_bmap, ++#else ++ .bmap = NULL, ++#endif + .destroy = ntfs_fuse_destroy2, + #ifdef HAVE_SETXATTR + .getxattr = ntfs_fuse_getxattr, +--- libntfs-3g/Makefile.in.orig Sun Apr 15 20:50:19 2007 ++++ libntfs-3g/Makefile.in Mon Apr 30 14:49:21 2007 +@@ -97,7 +97,7 @@ + libntfs_3g_la-misc.lo libntfs_3g_la-mst.lo \ + libntfs_3g_la-runlist.lo libntfs_3g_la-security.lo \ + libntfs_3g_la-unistr.lo libntfs_3g_la-version.lo \ +- libntfs_3g_la-volume.lo ++ libntfs_3g_la-volume.lo libntfs_3g_la-ublio.lo + libntfs_3g_la_OBJECTS = $(am_libntfs_3g_la_OBJECTS) + DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) + depcomp = $(SHELL) $(top_srcdir)/depcomp +@@ -261,7 +261,8 @@ + security.c \ + unistr.c \ + version.c \ +- volume.c ++ volume.c \ ++ ublio.c + + AM_CPPFLAGS = $(linux_ntfsincludedir) $(all_includes) + EXTRA_DIST = unix_io.c +@@ -359,6 +360,7 @@ + @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libntfs_3g_la-unistr.Plo@am__quote@ + @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libntfs_3g_la-version.Plo@am__quote@ + @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libntfs_3g_la-volume.Plo@am__quote@ ++@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libntfs_3g_la-ublio.Plo@am__quote@ + + .c.o: + @am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \ +@@ -546,8 +548,13 @@ + @am__fastdepCC_TRUE@ if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libntfs_3g_la_CFLAGS) $(CFLAGS) -MT libntfs_3g_la-volume.lo -MD -MP -MF "$(DEPDIR)/libntfs_3g_la-volume.Tpo" -c -o libntfs_3g_la-volume.lo `test -f 'volume.c' || echo '$(srcdir)/'`volume.c; \ + @am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libntfs_3g_la-volume.Tpo" "$(DEPDIR)/libntfs_3g_la-volume.Plo"; else rm -f "$(DEPDIR)/libntfs_3g_la-volume.Tpo"; exit 1; fi + @AMDEP_TRUE@@am__fastdepCC_FALSE@ source='volume.c' object='libntfs_3g_la-volume.lo' libtool=yes @AMDEPBACKSLASH@ ++libntfs_3g_la-ublio.lo: ublio.c ++@am__fastdepCC_TRUE@ if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libntfs_3g_la_CFLAGS) $(CFLAGS) -MT libntfs_3g_la-ublio.lo -MD -MP -MF "$(DEPDIR)/libntfs_3g_la-ublio.Tpo" -c -o libntfs_3g_la-ublio.lo `test -f 'ublio.c' || echo '$(srcdir)/'`ublio.c; \ ++@am__fastdepCC_TRUE@ then mv -f "$(DEPDIR)/libntfs_3g_la-ublio.Tpo" "$(DEPDIR)/libntfs_3g_la-ublio.Plo"; else rm -f "$(DEPDIR)/libntfs_3g_la-ublio.Tpo"; exit 1; fi ++@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='ublio.c' object='libntfs_3g_la-ublio.lo' libtool=yes @AMDEPBACKSLASH@ + @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ + @am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libntfs_3g_la_CFLAGS) $(CFLAGS) -c -o libntfs_3g_la-volume.lo `test -f 'volume.c' || echo '$(srcdir)/'`volume.c ++@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libntfs_3g_la_CFLAGS) $(CFLAGS) -c -o libntfs_3g_la-ublio.lo `test -f 'ublio.c' || echo '$(srcdir)/'`ublio.c + + mostlyclean-libtool: + -rm -f *.lo Index: fusefs-ntfs/files/patch-libntfs-3g-device_io.c =================================================================== RCS file: fusefs-ntfs/files/patch-libntfs-3g-device_io.c diff -N fusefs-ntfs/files/patch-libntfs-3g-device_io.c --- fusefs-ntfs/files/patch-libntfs-3g-device_io.c 5 Dec 2006 22:15:46 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,14 +0,0 @@ ---- libntfs-3g/device_io.c.orig Wed Dec 6 04:03:06 2006 -+++ libntfs-3g/device_io.c Tue Dec 5 23:04:04 2006 -@@ -26,7 +26,11 @@ - #ifndef __CYGWIN32__ - - /* Not on Cygwin; use standard Unix style low level device operations. */ -+#if defined(__FreeBSD__) -+#include "unix_io_raw.c" -+#else - #include "unix_io.c" -+#endif - - #else /* __CYGWIN32__ */ - Index: fusefs-ntfs/files/patch-libntfs-3g__device.c =================================================================== RCS file: fusefs-ntfs/files/patch-libntfs-3g__device.c diff -N fusefs-ntfs/files/patch-libntfs-3g__device.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ fusefs-ntfs/files/patch-libntfs-3g__device.c 2 May 2007 01:32:30 -0000 @@ -0,0 +1,56 @@ +--- ./libntfs-3g/device.c.orig Tue Jan 9 18:22:57 2007 ++++ ./libntfs-3g/device.c Mon Apr 30 13:40:54 2007 +@@ -63,6 +63,9 @@ + #ifdef HAVE_LINUX_HDREG_H + #include + #endif ++#ifdef __FreeBSD__ ++#include ++#endif + + #include "types.h" + #include "mst.h" +@@ -533,6 +536,17 @@ + } + } + #endif ++#ifdef DIOCGMEDIASIZE ++ { off_t size; ++ ++ if (dev->d_ops->ioctl(dev, DIOCGMEDIASIZE, &size) >= 0) { ++ ntfs_log_debug("DIOCGMEDIASIZE nr bytes = %llu (0x%llx)\n", ++ (unsigned long long)size, ++ (unsigned long long)size); ++ return (s64)size / block_size; ++ } ++ } ++#endif + /* + * We couldn't figure it out by using a specialized ioctl, + * so do binary search to find the size of the device. +@@ -681,7 +695,24 @@ + return sect_size; + } + } +-#else ++#elif defined(DIOCGSECTORSIZE) ++ /* ++ * XXX On FreeBSD (where we have DIOCGSECTORSIZE) the low-level I/O ++ * system already knows the sector size, and doing an ioctl is needless. ++ * However, I don't know how to extract that information cleanly, ++ * without letting a bunch of platform specific #ifdef-s to sneak in. ++ * So now I rather just re-do the ioctl... ++ */ ++ { ++ size_t sect_size = 0; ++ ++ if (!dev->d_ops->ioctl(dev, DIOCGSECTORSIZE, §_size)) { ++ ntfs_log_debug("DIOCGSECTORSIZE sector size = %d bytes\n", ++ (int)sect_size); ++ return sect_size; ++ } ++ } ++#else + errno = EOPNOTSUPP; + #endif + return -1; Index: fusefs-ntfs/files/patch-libntfs-3g__unix_io.c =================================================================== RCS file: fusefs-ntfs/files/patch-libntfs-3g__unix_io.c diff -N fusefs-ntfs/files/patch-libntfs-3g__unix_io.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ fusefs-ntfs/files/patch-libntfs-3g__unix_io.c 2 May 2007 01:32:30 -0000 @@ -0,0 +1,628 @@ +--- libntfs-3g/unix_io.c.orig Tue Dec 12 20:16:43 2006 ++++ libntfs-3g/unix_io.c Tue May 1 21:14:57 2007 +@@ -54,6 +54,22 @@ + #include + #endif + ++/* ++ * The following build definitions are available: ++ * USE_ALIGNED_IO - All I/O is done by blocks. ++ * USE_UBLIO - Use the ublio user space cache library. ++ * USE_LOCK - Lock the device/file when mounted. ++ */ ++ ++#ifdef __FreeBSD__ ++#include ++#define USE_ALIGNED_IO 1 ++#endif ++ ++#if USE_UBLIO ++#include ++#endif ++ + #include "types.h" + #include "mst.h" + #include "debug.h" +@@ -61,13 +77,89 @@ + #include "logging.h" + #include "misc.h" + +-#define DEV_FD(dev) (*(int *)dev->d_private) ++#if USE_UBLIO ++#define UBLIO_USE_API 1 ++#include "ublio.h" ++#define UBLIO_DEFAULT_BLOCKSIZE 512 ++#define UBLIO_DEFAULT_ITEMS 12 ++#define UBLIO_DEFAULT_GRACE 8 ++#define UBLIO_DEFAULT_SYNC_IO 1 ++#endif ++ ++#if USE_ALIGNED_IO ++#define RAW_IO_ALIGNED(dev, offset, count) \ ++ (DEV_HANDLE(dev)->block_size == 0 || \ ++ ((offset) % DEV_HANDLE(dev)->block_size == 0 && \ ++ (count) % DEV_HANDLE(dev)->block_size == 0)) ++#define RAW_IO_ALIGN(dev, offset) \ ++ ((offset) / DEV_HANDLE(dev)->block_size * DEV_HANDLE(dev)->block_size) ++#define RAW_IO_MAX_SIZE (128 * 1024 * 1024) ++#endif ++ ++struct unix_filehandle { ++ int fd; ++#if USE_ALIGNED_IO ++ s64 pos; ++ s32 block_size; ++ s64 media_size; ++#endif ++#if USE_UBLIO ++ ublio_filehandle_t ublio_fh; ++#endif ++}; ++ ++#define DEV_HANDLE(dev) ((struct unix_filehandle *)dev->d_private) ++#define DEV_FD(dev) (DEV_HANDLE(dev)->fd) + + /* Define to nothing if not present on this system. */ + #ifndef O_EXCL + # define O_EXCL 0 + #endif + ++#if USE_ALIGNED_IO ++/** ++ * Get block_size and media_size ++ */ ++static int ++raw_io_get_size(struct ntfs_device *dev) ++{ ++ int bs; ++ off_t ms; ++ struct stat sb; ++ ++ if (fstat(DEV_FD(dev), &sb) < 0) { ++ ntfs_log_perror("Failed to stat '%s'", dev->d_name); ++ return -1; ++ } ++ ++ if (S_ISREG(sb.st_mode)) { ++ DEV_HANDLE(dev)->media_size = sb.st_size; ++ ntfs_log_trace("%s: regular file (media_size %lld)\n", ++ dev->d_name, DEV_HANDLE(dev)->media_size); ++ if (getenv("FORCE_ALIGNED_IO")) ++ DEV_HANDLE(dev)->block_size = 512; ++ return 0; ++ } ++ ++ if (ioctl(DEV_FD(dev), DIOCGSECTORSIZE, &bs) < 0) { ++ ntfs_log_perror("Failed to ioctl(DIOCGSECTORSIZE) '%s'", ++ dev->d_name); ++ return -1; ++ } ++ DEV_HANDLE(dev)->block_size = bs; ++ ntfs_log_trace("%s: block size %d\n", dev->d_name, bs); ++ ++ if (ioctl(DEV_FD(dev), DIOCGMEDIASIZE, &ms) < 0) { ++ ntfs_log_perror("Failed to ioctl(DIOCGMEDIASIZE) '%s'", ++ dev->d_name); ++ return -1; ++ } ++ DEV_HANDLE(dev)->media_size = ms; ++ ntfs_log_trace("%s: media size %lld\n", dev->d_name, ms); ++ return 0; ++} ++#endif ++ + /** + * ntfs_device_unix_io_open - Open a device and lock it exclusively + * @dev: +@@ -79,9 +171,21 @@ + */ + static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags) + { ++#if USE_ALIGNED_IO ++ size_t sectsize; ++#endif ++#if USE_LOCK + struct flock flk; ++#endif + struct stat sbuf; +- int err; ++ struct unix_filehandle *ufh; ++ int err = 0; ++ int is_special = 0; ++#if USE_UBLIO ++ struct ublio_param up; ++ int use_ublio = 0; ++ char *xenv, *xgarbage; ++#endif + + if (NDevOpen(dev)) { + errno = EBUSY; +@@ -91,20 +195,28 @@ + ntfs_log_perror("Failed to access '%s'", dev->d_name); + return -1; + } +- if (S_ISBLK(sbuf.st_mode)) +- NDevSetBlock(dev); ++ if (S_ISBLK(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)) ++ is_special = 1; + +- dev->d_private = ntfs_malloc(sizeof(int)); +- if (!dev->d_private) ++ ufh = ntfs_malloc(sizeof(*ufh)); ++ if (!ufh) + return -1; ++ dev->d_private = ufh; ++#if USE_ALIGNED_IO ++ ufh->fd = -1; ++ ufh->pos = 0; ++ ufh->block_size = 0; ++ ufh->media_size = 0; ++#endif ++ + /* + * Open file for exclusive access if mounting r/w. + * Fuseblk takes care about block devices. + */ +- if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR) ++ if (!is_special && (flags & O_RDWR) == O_RDWR) + flags |= O_EXCL; +- *(int*)dev->d_private = open(dev->d_name, flags); +- if (*(int*)dev->d_private == -1) { ++ ufh->fd = open(dev->d_name, flags); ++ if (ufh->fd == -1) { + err = errno; + goto err_out; + } +@@ -112,6 +224,35 @@ + if ((flags & O_RDWR) != O_RDWR) + NDevSetReadOnly(dev); + ++#if USE_UBLIO ++ ufh->ublio_fh = NULL; ++ if ((xenv = getenv("NTFS_USE_UBLIO")) && ++ xenv[0] == '1' && xenv[1] == '\0') ++ use_ublio = 1; ++ if ((xenv = getenv("UBLIO_BLOCKSIZE"))) ++ up.up_blocksize = strtoul(xenv, &xgarbage, 10); ++ if (!xenv || *xgarbage != '\0') ++ up.up_blocksize = UBLIO_DEFAULT_BLOCKSIZE; ++ if ((xenv = getenv("UBLIO_ITEMS"))) ++ up.up_items = strtoul(xenv, &xgarbage, 10); ++ if (!xenv || *xgarbage != '\0') ++ up.up_items = UBLIO_DEFAULT_ITEMS; ++ if ((xenv = getenv("UBLIO_GRACE"))) ++ up.up_grace = strtoul(xenv, &xgarbage, 10); ++ if (!xenv || *xgarbage != '\0') ++ up.up_grace = UBLIO_DEFAULT_GRACE; ++ if ((xenv = getenv("UBLIO_SYNC_IO")) && ++ (xenv[0] == '0' || xenv[0] == '1') && xenv[1] == '\0') ++ up.up_sync_io = (xenv[0] == '1'); ++ else ++ up.up_sync_io = UBLIO_DEFAULT_SYNC_IO; ++ up.up_priv = &ufh->fd; ++ up.up_pread = NULL; ++ up.up_preadv = NULL; ++ up.up_pwrite = NULL; ++ up.up_pwritev = NULL; ++#endif ++#if USE_LOCK + memset(&flk, 0, sizeof(flk)); + if (NDevReadOnly(dev)) + flk.l_type = F_RDLCK; +@@ -119,7 +260,21 @@ + flk.l_type = F_WRLCK; + flk.l_whence = SEEK_SET; + flk.l_start = flk.l_len = 0LL; +- if (fcntl(DEV_FD(dev), F_SETLK, &flk)) { ++#endif ++#if USE_ALIGNED_IO ++ if (raw_io_get_size(dev) < 0) { ++ err = errno; ++ close(DEV_FD(dev)); ++ goto err_out; ++ } ++ if (S_ISBLK(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)) ++ NDevSetBlock(dev); ++#else ++ if (S_ISBLK(sbuf.st_mode)) ++ NDevSetBlock(dev); ++#endif /* USE_ALIGNED_IO */ ++#if USE_LOCK ++ if (!NDevBlock(dev) && fcntl(DEV_FD(dev), F_SETLK, &flk)) { + err = errno; + ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ? + "read" : "write", dev->d_name); +@@ -127,7 +282,16 @@ + ntfs_log_perror("Failed to close '%s'", dev->d_name); + goto err_out; + } +- ++#endif ++#if USE_UBLIO ++ if (use_ublio) { ++ ufh->ublio_fh = ublio_open(&up); ++ if (!ufh->ublio_fh) { ++ close(DEV_FD(dev)); ++ goto err_out; ++ } ++ } ++#endif + NDevSetOpen(dev); + return 0; + err_out: +@@ -147,7 +311,10 @@ + */ + static int ntfs_device_unix_io_close(struct ntfs_device *dev) + { ++ /* XXX no error if fysnc, fcntl (ublio_close) fails? */ ++#if USE_LOCK + struct flock flk; ++#endif + + if (!NDevOpen(dev)) { + errno = EBADF; +@@ -155,14 +322,21 @@ + } + if (NDevDirty(dev)) + fsync(DEV_FD(dev)); ++#if USE_LOCK + /* Release exclusive (mandatory) lock on the whole device. */ + memset(&flk, 0, sizeof(flk)); + flk.l_type = F_UNLCK; + flk.l_whence = SEEK_SET; + flk.l_start = flk.l_len = 0LL; +- if (fcntl(DEV_FD(dev), F_SETLK, &flk)) ++ ++ if (!NDevBlock(dev) && fcntl(DEV_FD(dev), F_SETLK, &flk)) + ntfs_log_perror("ntfs_device_unix_io_close: Warning: Could not " +- "unlock %s", dev->d_name); ++ "unlock %s", dev->d_name); ++#endif ++#if USE_UBLIO ++ if (DEV_HANDLE(dev)->ublio_fh) ++ ublio_close(DEV_HANDLE(dev)->ublio_fh); ++#endif + /* Close the file descriptor and clear our open flag. */ + if (close(DEV_FD(dev))) + return -1; +@@ -185,10 +359,235 @@ + static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset, + int whence) + { ++#if USE_ALIGNED_IO ++ s64 abs_pos; ++ ++ ntfs_log_trace("seek offset = 0x%llx, whence = %d.\n", offset, whence); ++ switch (whence) { ++ case SEEK_SET: ++ abs_pos = offset; ++ break; ++ ++ case SEEK_CUR: ++ abs_pos = DEV_HANDLE(dev)->pos + offset; ++ break; ++ ++ case SEEK_END: ++ abs_pos = DEV_HANDLE(dev)->media_size + offset; ++ break; ++ ++ default: ++ ntfs_log_trace("Wrong mode %d.\n", whence); ++ errno = EINVAL; ++ return -1; ++ } ++ ++ if (abs_pos < 0 || abs_pos > DEV_HANDLE(dev)->media_size) { ++ ntfs_log_trace("Seeking outsize seekable area.\n"); ++ errno = EINVAL; ++ return -1; ++ } ++ DEV_HANDLE(dev)->pos = abs_pos; ++ return abs_pos; ++#else + return lseek(DEV_FD(dev), offset, whence); ++#endif ++} ++ ++#if USE_ALIGNED_IO ++ ++#if USE_UBLIO ++#define pread_wrap(fd, buf, count, off) \ ++ (DEV_HANDLE(fd)->ublio_fh ? \ ++ ublio_pread(DEV_HANDLE(fd)->ublio_fh, buf, count, off) : \ ++ pread(DEV_FD(fd), buf, count, off)) ++#define pwrite_wrap(fd, buf, count, off) \ ++ (DEV_HANDLE(fd)->ublio_fh ? \ ++ ublio_pwrite(DEV_HANDLE(fd)->ublio_fh, buf, count, off) : \ ++ pwrite(DEV_FD(fd), buf, count, off)) ++#else ++#define pread_wrap(fd, buf, count, off) \ ++ pread(DEV_FD(fd), buf, count, off) ++#define pwrite_wrap(fd, buf, count, off) \ ++ pwrite(DEV_FD(fd), buf, count, off) ++#endif ++ ++/** ++ * aligned_pread - Perform an aligned positioned read from the device ++ */ ++static s64 aligned_pread(struct ntfs_device *dev, void *buf, s64 count, s64 offset) ++{ ++ s64 start, start_aligned; ++ s64 end, end_aligned; ++ size_t count_aligned; ++ char *buf_aligned; ++ ssize_t nr; ++ ++ /* short-circuit for regular files */ ++ start = offset; ++ if (count > RAW_IO_MAX_SIZE) ++ count = RAW_IO_MAX_SIZE; ++ if (RAW_IO_ALIGNED(dev, start, count)) ++ return pread_wrap(dev, buf, count, start); ++ ++ /* ++ * +- start_aligned +- end_aligned ++ * | | ++ * | +- start +- end | ++ * v v v v ++ * |----------|----------|----------| ++ * ^ ^ ++ * +----- count ------+ ++ * ^ ^ ++ * +-------- count_aligned ---------+ ++ */ ++ start_aligned = RAW_IO_ALIGN(dev, start); ++ end = start + count; ++ end_aligned = RAW_IO_ALIGN(dev, end) + ++ (RAW_IO_ALIGNED(dev, end, 0) ? 0 : DEV_HANDLE(dev)->block_size); ++ count_aligned = end_aligned - start_aligned; ++ ntfs_log_trace( ++ "%s: count = 0x%llx/0x%x, start = 0x%llx/0x%llx, end = 0x%llx/0x%llx\n", ++ dev->d_name, count, count_aligned, ++ start, start_aligned, end, end_aligned); ++ ++ /* allocate buffer */ ++ buf_aligned = ntfs_malloc(count_aligned); ++ if (buf_aligned == NULL) { ++ ntfs_log_trace("ntfs_malloc(%d) failed\n", count_aligned); ++ return -1; ++ } ++ ++ /* read aligned data */ ++ nr = pread_wrap(dev, buf_aligned, count_aligned, start_aligned); ++ if (nr == 0) ++ return 0; ++ if (nr < 0 || nr < start - start_aligned) { ++ free(buf_aligned); ++ return -1; ++ } ++ ++ /* copy out */ ++ memcpy(buf, buf_aligned + (start - start_aligned), count); ++ free(buf_aligned); ++ ++ nr -= start - start_aligned; ++ if (nr > count) ++ nr = count; ++ return nr; + } + + /** ++ * aligned_pwrite - Perform an aligned positioned write from the device ++ */ ++static s64 aligned_pwrite(struct ntfs_device *dev, void *buf, s64 count, s64 offset) ++{ ++ s64 start, start_aligned; ++ s64 end, end_aligned; ++ size_t count_aligned; ++ char *buf_aligned; ++ ssize_t nw; ++ ++ if (NDevReadOnly(dev)) { ++ errno = EROFS; ++ return -1; ++ } ++ NDevSetDirty(dev); ++ ++ /* short-circuit for regular files */ ++ start = offset; ++ if (count > RAW_IO_MAX_SIZE) ++ count = RAW_IO_MAX_SIZE; ++ if (RAW_IO_ALIGNED(dev, start, count)) ++ return pwrite_wrap(dev, buf, count, start); ++ ++ /* ++ * +- start_aligned +- end_aligned ++ * | | ++ * | +- start +- end | ++ * v v v v ++ * |----------|----------|----------| ++ * ^ ^ ++ * +----- count ------+ ++ * ^ ^ ++ * +-------- count_aligned ---------+ ++ */ ++ start_aligned = RAW_IO_ALIGN(dev, start); ++ end = start + count; ++ end_aligned = RAW_IO_ALIGN(dev, end) + ++ (RAW_IO_ALIGNED(dev, end, 0) ? 0 : DEV_HANDLE(dev)->block_size); ++ count_aligned = end_aligned - start_aligned; ++ ntfs_log_trace( ++ "%s: count = 0x%llx/0x%x, start = 0x%llx/0x%llx, end = 0x%llx/0x%llx\n", ++ dev->d_name, count, count_aligned, ++ start, start_aligned, end, end_aligned); ++ ++ /* allocate buffer */ ++ buf_aligned = ntfs_malloc(count_aligned); ++ if (buf_aligned == NULL) { ++ ntfs_log_trace("ntfs_malloc(%d) failed\n", count_aligned); ++ return -1; ++ } ++ ++ /* read aligned lead-in */ ++ if (pread_wrap(dev, buf_aligned, DEV_HANDLE(dev)->block_size, start_aligned) != DEV_HANDLE(dev)->block_size) { ++ ntfs_log_trace("read lead-in failed\n"); ++ free(buf_aligned); ++ return -1; ++ } ++ ++ /* read aligned lead-out */ ++ if (end != end_aligned && count_aligned > DEV_HANDLE(dev)->block_size) { ++ if (pread_wrap(dev, buf_aligned + count_aligned - DEV_HANDLE(dev)->block_size, DEV_HANDLE(dev)->block_size, end_aligned - DEV_HANDLE(dev)->block_size) != DEV_HANDLE(dev)->block_size) { ++ ntfs_log_trace("read lead-out failed\n"); ++ free(buf_aligned); ++ return -1; ++ } ++ } ++ ++ /* copy data to write */ ++ memcpy(buf_aligned + (start - start_aligned), buf, count); ++ ++ /* write aligned data */ ++ nw = pwrite_wrap(dev, buf_aligned, count_aligned, start_aligned); ++ free(buf_aligned); ++ if (nw < 0 || nw < start - start_aligned) ++ return -1; ++ ++ nw -= start - start_aligned; ++ if (nw > count) ++ nw = count; ++ return nw; ++} ++ ++/** ++ * aligned_read - Perform an aligned read from the device ++ */ ++static s64 aligned_read(struct ntfs_device *dev, void *buf, s64 count) ++{ ++ s64 nr = aligned_pread(dev, buf, count, DEV_HANDLE(dev)->pos); ++ if (nr > 0) ++ DEV_HANDLE(dev)->pos += nr; ++ return nr; ++} ++ ++/** ++ * aligned_write - Perform an aligned read from the device ++ */ ++static s64 aligned_write(struct ntfs_device *dev, void *buf, s64 count) ++{ ++ s64 nw = aligned_pwrite(dev, buf, count, DEV_HANDLE(dev)->pos); ++ if (nw > 0) ++ DEV_HANDLE(dev)->pos += nw; ++ return nw; ++} ++ ++#undef ublio_pwrite ++#undef ublio_pread ++ ++#endif ++ ++/** + * ntfs_device_unix_io_read - Read from the device, from the current location + * @dev: + * @buf: +@@ -201,6 +600,29 @@ + static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf, + s64 count) + { ++#if USE_ALIGNED_IO ++ return aligned_read(dev, buf, count); ++#elif USE_UBLIO ++ if (DEV_HANDLE(dev)->ublio_fh) { ++ off_t offset; ++ ssize_t res; ++ ++ offset = lseek(DEV_FD(dev), 0, SEEK_CUR); ++ if (offset == -1) ++ return -1; ++ ++ res = ublio_pread(DEV_HANDLE(dev)->ublio_fh, buf, count, ++ offset); ++ if (res == -1) ++ return -1; ++ ++ if (lseek(DEV_FD(dev), res, SEEK_CUR) == -1) ++ return -1; ++ ++ return res; ++ } ++#endif ++ + return read(DEV_FD(dev), buf, count); + } + +@@ -222,6 +644,28 @@ + return -1; + } + NDevSetDirty(dev); ++#if USE_ALIGNED_IO ++ return aligned_write(dev, buf, count); ++#elif USE_UBLIO ++ if (DEV_HANDLE(dev)->ublio_fh) ++ off_t offset; ++ ssize_t res; ++ ++ offset = lseek(DEV_FD(dev), 0, SEEK_CUR); ++ if (offset == -1) ++ return -1; ++ ++ res = ublio_pwrite(DEV_HANDLE(dev)->ublio_fh, (void *)buf, ++ count, offset); ++ if (res == -1) ++ return -1; ++ ++ if (lseek(DEV_FD(dev), res, SEEK_CUR) == -1) ++ return -1; ++ ++ return res; ++ } ++#endif + return write(DEV_FD(dev), buf, count); + } + +@@ -239,6 +683,13 @@ + static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf, + s64 count, s64 offset) + { ++#if USE_ALIGNED_IO ++ return aligned_pread(dev, buf, count, offset); ++#elif USE_UBLIO ++ if (DEV_HANDLE(dev)->ublio_fh) ++ return ublio_pread(DEV_HANDLE(dev)->ublio_fh, buf, count, ++ offset); ++#endif + return pread(DEV_FD(dev), buf, count, offset); + } + +@@ -261,6 +712,13 @@ + return -1; + } + NDevSetDirty(dev); ++#if USE_ALIGNED_IO ++ return aligned_pwrite(dev, buf, count, offset); ++#elif USE_UBLIO ++ if (DEV_HANDLE(dev)->ublio_fh) ++ return ublio_pwrite(DEV_HANDLE(dev)->ublio_fh, (void *)buf, ++ count, offset); ++#endif + return pwrite(DEV_FD(dev), buf, count, offset); + } + +@@ -275,7 +733,16 @@ + static int ntfs_device_unix_io_sync(struct ntfs_device *dev) + { + if (!NDevReadOnly(dev)) { +- int res = fsync(DEV_FD(dev)); ++ int res; ++ ++#if USE_UBLIO ++ if (DEV_HANDLE(dev)->ublio_fh) { ++ res = ublio_fsync(DEV_HANDLE(dev)->ublio_fh); ++ if (res) ++ return res; ++ } ++#endif ++ res = fsync(DEV_FD(dev)); + if (!res) + NDevClearDirty(dev); + return res; Index: fusefs-ntfs/files/patch-src-ntfs-3g.c =================================================================== RCS file: fusefs-ntfs/files/patch-src-ntfs-3g.c diff -N fusefs-ntfs/files/patch-src-ntfs-3g.c --- fusefs-ntfs/files/patch-src-ntfs-3g.c 25 Apr 2007 18:55:38 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,11 +0,0 @@ ---- src/ntfs-3g.c.orig Thu Apr 26 01:51:08 2007 -+++ src/ntfs-3g.c Thu Apr 26 01:51:15 2007 -@@ -134,7 +134,7 @@ - } opts; - - static const char *EXEC_NAME = "ntfs-3g"; --static char def_opts[] = "silent,allow_other,nonempty,"; -+static char def_opts[] = "silent,allow_other,"; - static ntfs_fuse_context_t *ctx; - static u32 ntfs_sequence; - Index: fusefs-ntfs/files/pkg-message.in =================================================================== RCS file: fusefs-ntfs/files/pkg-message.in diff -N fusefs-ntfs/files/pkg-message.in --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ fusefs-ntfs/files/pkg-message.in 2 May 2007 01:32:30 -0000 @@ -0,0 +1,24 @@ +============================================================================== + +NTFS-3G has been installed, and was built with UBLIO support. + +The UBLIO layer is configured through the following environment variables, +which are read when 'ntfs-3g' is executed (defaults are at the end of each): + +NTFS_USE_UBLIO - Enable the UBLIO cache (1/0, default: 0). +UBLIO_BLOCKSIZE - Actual reads/writes will be multiples of this quantity (512). +UBLIO_ITEMS - Number of cache entries, each of UBLIO_BLOCKSIZE length (12). +UBLIO_GRACE - Number of times a cache entry will refuse being recycled (8). +UBLIO_SYNC_IO - If enabled, all writes will be immediatly executed (1). + +It is also possible to enforce block aligned I/O in regular files by setting +the FORCE_ALIGNED_IO variable (it will be set to 512 bytes). + +Based on my tests, I would recommend disabling UBLIO_SYNC_IO. + +For example: + +# env NTFS_USE_UBLIO=1 UBLIO_BLOCKSIZE=4194304 UBLIO_SYNC_IO=0 \ + ntfs-3g /dev/ad0s1 /mnt + +============================================================================== Index: fusefs-ntfs/files/unix_io_raw.c =================================================================== RCS file: fusefs-ntfs/files/unix_io_raw.c diff -N fusefs-ntfs/files/unix_io_raw.c --- fusefs-ntfs/files/unix_io_raw.c 10 Jan 2007 21:54:31 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,587 +0,0 @@ -/** - * unix_io_raw.c - Unix style disk io functions. Originated from the Linux-NTFS project. - * - * Copyright (c) 2006 Max Khon - * - * This program/include file is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program/include file is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program (in the main directory of the NTFS-3G - * distribution in the file COPYING); if not, write to the Free Software - * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifdef HAVE_UNISTD_H -#include -#endif -#ifdef HAVE_STDLIB_H -#include -#endif -#ifdef HAVE_STRING_H -#include -#endif -#ifdef HAVE_ERRNO_H -#include -#endif -#ifdef HAVE_STDIO_H -#include -#endif -#ifdef HAVE_SYS_TYPES_H -#include -#endif -#ifdef HAVE_SYS_STAT_H -#include -#endif -#ifdef HAVE_FCNTL_H -#include -#endif -#ifdef HAVE_SYS_IOCTL_H -#include -#endif -#ifdef HAVE_LINUX_FD_H -#include -#endif -#include - -#include "types.h" -#include "mst.h" -#include "debug.h" -#include "device.h" -#include "logging.h" -#include "misc.h" - -typedef struct { - int fd; - s64 pos; - s32 block_size; - s64 media_size; -} unix_raw_fd; - -#define DEV_FD(dev) (((unix_raw_fd *) dev->d_private)) -#define RAW_IO_ALIGNED(dev, offset, count) \ - (DEV_FD(dev)->block_size == 0 || \ - ((offset) % DEV_FD(dev)->block_size == 0 && \ - (count) % DEV_FD(dev)->block_size == 0)) -#define RAW_IO_ALIGN(dev, offset) \ - ((offset) / DEV_FD(dev)->block_size * DEV_FD(dev)->block_size) -#define RAW_IO_MAX_SIZE (128 * 1024 * 1024) - -/* Define to nothing if not present on this system. */ -#ifndef O_EXCL -# define O_EXCL 0 -#endif - -/** - * Get block_size and media_size - */ -static int -raw_io_get_size(struct ntfs_device *dev) -{ - int bs; - off_t ms; - struct stat sb; - - if (fstat(DEV_FD(dev)->fd, &sb) < 0) { - ntfs_log_perror("Failed to stat '%s'", dev->d_name); - return -1; - } - - if (S_ISREG(sb.st_mode)) { - DEV_FD(dev)->media_size = sb.st_size; - ntfs_log_trace("%s: regular file (media_size %lld)\n", - dev->d_name, DEV_FD(dev)->media_size); - return 0; - } - - if (ioctl(DEV_FD(dev)->fd, DIOCGSECTORSIZE, &bs) < 0) { - ntfs_log_perror("Failed to ioctl(DIOCGSECTORSIZE) '%s'", - dev->d_name); - return -1; - } - DEV_FD(dev)->block_size = bs; - ntfs_log_trace("%s: block size %d\n", dev->d_name, bs); - - if (ioctl(DEV_FD(dev)->fd, DIOCGMEDIASIZE, &ms) < 0) { - ntfs_log_perror("Failed to ioctl(DIOCGMEDIASIZE) '%s'", - dev->d_name); - return -1; - } - DEV_FD(dev)->media_size = ms; - ntfs_log_trace("%s: media size %lld\n", dev->d_name, ms); - return 0; -} - -/** - * Aligned read - */ -static ssize_t -raw_io_pread(struct ntfs_device *dev, char *buf, size_t count, s64 offset) -{ - return pread(DEV_FD(dev)->fd, buf, count, offset); -} - -/** - * Aligned write - */ -static ssize_t -raw_io_pwrite(struct ntfs_device *dev, const char *buf, size_t count, s64 offset) -{ - return pwrite(DEV_FD(dev)->fd, buf, count, offset); -} - -/** - * ntfs_device_unix_raw_io_open - Open a device and lock it exclusively - * @dev: - * @flags: - * - * Description... - * - * Returns: - */ -static int -ntfs_device_unix_raw_io_open(struct ntfs_device *dev, int flags) -{ -#if 0 - struct flock flk; -#endif - struct stat sbuf; - int err; - - if (NDevOpen(dev)) { - errno = EBUSY; - return -1; - } - if (stat(dev->d_name, &sbuf)) { - ntfs_log_perror("Failed to access '%s'", dev->d_name); - return -1; - } - if (S_ISBLK(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)) - NDevSetBlock(dev); - - dev->d_private = ntfs_malloc(sizeof(unix_raw_fd)); - if (!dev->d_private) - return -1; - DEV_FD(dev)->fd = -1; - DEV_FD(dev)->pos = 0; - DEV_FD(dev)->block_size = 0; - DEV_FD(dev)->media_size = 0; - - /* - * Open file for exclusive access if mounting r/w. - * Fuseblk takes care about block devices. - */ - if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR) - flags |= O_EXCL; - DEV_FD(dev)->fd = open(dev->d_name, flags); - if (DEV_FD(dev)->fd == -1) { - err = errno; - goto err_out; - } - - if ((flags & O_RDWR) != O_RDWR) - NDevSetReadOnly(dev); - -#if 0 - memset(&flk, 0, sizeof(flk)); - if (NDevReadOnly(dev)) - flk.l_type = F_RDLCK; - else - flk.l_type = F_WRLCK; - flk.l_whence = SEEK_SET; - flk.l_start = flk.l_len = 0LL; - if (fcntl(DEV_FD(dev)->fd, F_SETLK, &flk)) { - err = errno; - ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ? - "read" : "write", dev->d_name); - if (close(DEV_FD(dev)->fd)) - ntfs_log_perror("Failed to close '%s'", dev->d_name); - goto err_out; - } -#endif - - if (raw_io_get_size(dev) < 0) { - err = errno; - close(DEV_FD(dev)->fd); - goto err_out; - } - - NDevSetOpen(dev); - return 0; -err_out: - free(dev->d_private); - dev->d_private = NULL; - errno = err; - return -1; -} - -/** - * ntfs_device_unix_raw_io_close - Close the device, releasing the lock - * @dev: - * - * Description... - * - * Returns: - */ -static int -ntfs_device_unix_raw_io_close(struct ntfs_device *dev) -{ -#if 0 - struct flock flk; -#endif - - if (!NDevOpen(dev)) { - errno = EBADF; - return -1; - } - if (NDevDirty(dev)) - fsync(DEV_FD(dev)->fd); - -#if 0 - /* Release exclusive (mandatory) lock on the whole device. */ - memset(&flk, 0, sizeof(flk)); - flk.l_type = F_UNLCK; - flk.l_whence = SEEK_SET; - flk.l_start = flk.l_len = 0LL; - if (fcntl(DEV_FD(dev)->fd, F_SETLK, &flk)) - ntfs_log_perror("ntfs_device_unix_raw_io_close: Warning: Could not " - "unlock %s", dev->d_name); -#endif - - /* Close the file descriptor and clear our open flag. */ - if (close(DEV_FD(dev)->fd)) - return -1; - NDevClearOpen(dev); - free(dev->d_private); - dev->d_private = NULL; - return 0; -} - -/** - * ntfs_device_unix_raw_io_seek - Seek to a place on the device - * @dev: - * @offset: - * @whence: - * - * Description... - * - * Returns: - */ -static s64 -ntfs_device_unix_raw_io_seek(struct ntfs_device *dev, s64 offset, int whence) -{ - s64 abs_pos; - - ntfs_log_trace("seek offset = 0x%llx, whence = %d.\n", offset, whence); - switch (whence) { - case SEEK_SET: - abs_pos = offset; - break; - - case SEEK_CUR: - abs_pos = DEV_FD(dev)->pos + offset; - break; - - case SEEK_END: - abs_pos = DEV_FD(dev)->media_size + offset; - break; - - default: - ntfs_log_trace("Wrong mode %d.\n", whence); - errno = EINVAL; - return -1; - } - - if (abs_pos < 0 || abs_pos > DEV_FD(dev)->media_size) { - ntfs_log_trace("Seeking outsize seekable area.\n"); - errno = EINVAL; - return -1; - } - DEV_FD(dev)->pos = abs_pos; - return abs_pos; -} - -/** - * ntfs_device_unix_raw_io_pread - Perform a positioned read from the device - * @dev: - * @buf: - * @count: - * @offset: - * - * Description... - * - * Returns: - */ -static s64 -ntfs_device_unix_raw_io_pread(struct ntfs_device *dev, void *buf, s64 count, s64 offset) -{ - s64 start, start_aligned; - s64 end, end_aligned; - size_t count_aligned; - char *buf_aligned; - ssize_t nr; - - /* short-circuit for regular files */ - start = offset; - if (count > RAW_IO_MAX_SIZE) - count = RAW_IO_MAX_SIZE; - if (RAW_IO_ALIGNED(dev, start, count)) - return raw_io_pread(dev, buf, count, start); - - /* - * +- start_aligned +- end_aligned - * | | - * | +- start +- end | - * v v v v - * |----------|----------|----------| - * ^ ^ - * +----- count ------+ - * ^ ^ - * +-------- count_aligned ---------+ - */ - start_aligned = RAW_IO_ALIGN(dev, start); - end = start + count; - end_aligned = RAW_IO_ALIGN(dev, end) + - (RAW_IO_ALIGNED(dev, end, 0) ? 0 : DEV_FD(dev)->block_size); - count_aligned = end_aligned - start_aligned; - ntfs_log_trace( - "%s: count = 0x%llx/0x%x, start = 0x%llx/0x%llx, end = 0x%llx/0x%llx\n", - dev->d_name, count, count_aligned, - start, start_aligned, end, end_aligned); - - /* allocate buffer */ - buf_aligned = ntfs_malloc(count_aligned); - if (buf_aligned == NULL) { - ntfs_log_trace("ntfs_malloc(%d) failed\n", count_aligned); - return -1; - } - - /* read aligned data */ - nr = raw_io_pread(dev, buf_aligned, count_aligned, start_aligned); - if (nr == 0) - return 0; - if (nr < 0 || nr < start - start_aligned) { - free(buf_aligned); - return -1; - } - - /* copy out */ - memcpy(buf, buf_aligned + (start - start_aligned), count); - free(buf_aligned); - - nr -= start - start_aligned; - if (nr > count) - nr = count; - return nr; -} - -/** - * ntfs_device_unix_raw_io_pwrite - Perform a positioned write to the device - * @dev: - * @buf: - * @count: - * @offset: - * - * Description... - * - * Returns: - */ -static s64 -ntfs_device_unix_raw_io_pwrite(struct ntfs_device *dev, const void *buf, s64 count, s64 offset) -{ - s64 start, start_aligned; - s64 end, end_aligned; - size_t count_aligned; - char *buf_aligned; - ssize_t nw; - - if (NDevReadOnly(dev)) { - errno = EROFS; - return -1; - } - NDevSetDirty(dev); - - /* short-circuit for regular files */ - start = offset; - if (count > RAW_IO_MAX_SIZE) - count = RAW_IO_MAX_SIZE; - if (RAW_IO_ALIGNED(dev, start, count)) - return raw_io_pwrite(dev, buf, count, start); - - /* - * +- start_aligned +- end_aligned - * | | - * | +- start +- end | - * v v v v - * |----------|----------|----------| - * ^ ^ - * +----- count ------+ - * ^ ^ - * +-------- count_aligned ---------+ - */ - start_aligned = RAW_IO_ALIGN(dev, start); - end = start + count; - end_aligned = RAW_IO_ALIGN(dev, end) + - (RAW_IO_ALIGNED(dev, end, 0) ? 0 : DEV_FD(dev)->block_size); - count_aligned = end_aligned - start_aligned; - ntfs_log_trace( - "%s: count = 0x%llx/0x%x, start = 0x%llx/0x%llx, end = 0x%llx/0x%llx\n", - dev->d_name, count, count_aligned, - start, start_aligned, end, end_aligned); - - /* allocate buffer */ - buf_aligned = ntfs_malloc(count_aligned); - if (buf_aligned == NULL) { - ntfs_log_trace("ntfs_malloc(%d) failed\n", count_aligned); - return -1; - } - - /* read aligned lead-in */ - if (raw_io_pread(dev, buf_aligned, DEV_FD(dev)->block_size, start_aligned) != DEV_FD(dev)->block_size) { - ntfs_log_trace("read lead-in failed\n"); - free(buf_aligned); - return -1; - } - - /* read aligned lead-out */ - if (end != end_aligned && count_aligned > DEV_FD(dev)->block_size) { - if (raw_io_pread(dev, buf_aligned + count_aligned - DEV_FD(dev)->block_size, DEV_FD(dev)->block_size, end_aligned - DEV_FD(dev)->block_size) != DEV_FD(dev)->block_size) { - ntfs_log_trace("read lead-out failed\n"); - free(buf_aligned); - return -1; - } - } - - /* copy data to write */ - memcpy(buf_aligned + (start - start_aligned), buf, count); - - /* write aligned data */ - nw = raw_io_pwrite(dev, buf_aligned, count_aligned, start_aligned); - free(buf_aligned); - if (nw < 0 || nw < start - start_aligned) - return -1; - - nw -= start - start_aligned; - if (nw > count) - nw = count; - return nw; -} - -/** - * ntfs_device_unix_raw_io_read - Read from the device, from the current location - * @dev: - * @buf: - * @count: - * - * Description... - * - * Returns: - */ -static s64 -ntfs_device_unix_raw_io_read(struct ntfs_device *dev, void *buf, s64 count) -{ - s64 nr = ntfs_device_unix_raw_io_pread( - dev, buf, count, DEV_FD(dev)->pos); - if (nr > 0) - DEV_FD(dev)->pos += nr; - return nr; -} - -/** - * ntfs_device_unix_raw_io_write - Write to the device, at the current location - * @dev: - * @buf: - * @count: - * - * Description... - * - * Returns: - */ -static s64 -ntfs_device_unix_raw_io_write(struct ntfs_device *dev, const void *buf, - s64 count) -{ - s64 nw = ntfs_device_unix_raw_io_pwrite( - dev, buf, count, DEV_FD(dev)->pos); - if (nw > 0) - DEV_FD(dev)->pos += nw; - return nw; -} - -/** - * ntfs_device_unix_raw_io_sync - Flush any buffered changes to the device - * @dev: - * - * Description... - * - * Returns: - */ -static int -ntfs_device_unix_raw_io_sync(struct ntfs_device *dev) -{ - if (!NDevReadOnly(dev)) { - int res = fsync(DEV_FD(dev)->fd); - if (!res) - NDevClearDirty(dev); - return res; - } - return 0; -} - -/** - * ntfs_device_unix_raw_io_stat - Get information about the device - * @dev: - * @buf: - * - * Description... - * - * Returns: - */ -static int -ntfs_device_unix_raw_io_stat(struct ntfs_device *dev, struct stat *buf) -{ - return fstat(DEV_FD(dev)->fd, buf); -} - -/** - * ntfs_device_unix_raw_io_ioctl - Perform an ioctl on the device - * @dev: - * @request: - * @argp: - * - * Description... - * - * Returns: - */ -static int -ntfs_device_unix_raw_io_ioctl(struct ntfs_device *dev, int request, void *argp) -{ - return ioctl(DEV_FD(dev)->fd, request, argp); -} - -/** - * Device operations for working with unix style devices and files. - */ -struct ntfs_device_operations ntfs_device_unix_io_ops = { - .open = ntfs_device_unix_raw_io_open, - .close = ntfs_device_unix_raw_io_close, - .seek = ntfs_device_unix_raw_io_seek, - .read = ntfs_device_unix_raw_io_read, - .write = ntfs_device_unix_raw_io_write, - .pread = ntfs_device_unix_raw_io_pread, - .pwrite = ntfs_device_unix_raw_io_pwrite, - .sync = ntfs_device_unix_raw_io_sync, - .stat = ntfs_device_unix_raw_io_stat, - .ioctl = ntfs_device_unix_raw_io_ioctl, -};