From: Sven Hoexter <sven@timegate.de>
Date: Tue, 22 Jan 2013 12:39:23 +0000 (+0100)
Subject: Imported Upstream version 1.0.0
X-Git-Tag: upstream/1.0.0^0
X-Git-Url: https://git.sven.stormbind.net/?a=commitdiff_plain;h=c1c45856c432c68749276ecbeff56c88f85bcd3e;p=sven%2Ffuse-exfat.git

Imported Upstream version 1.0.0
---

diff --git a/ChangeLog b/ChangeLog
index e42a095..e24b2d6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,30 @@
+1.0.0 (2013-01-19)
+
+* Fixed crash when renaming a file within a single directory and a new name
+differs only in case.
+* Fixed clusters allocation: a cluster beyond valid clusters range could be
+allocated.
+* Fixed crash when a volume is unmounted while some files are open.
+* SConscript now respects AR and RANLIB environment variables.
+* Improved error handling.
+
+Linux:
+
+* Enabled big_writes. This improves write speed (larger block size means less
+switches between kernel- and user-space).
+* Do BLKROGET ioctl to make sure the device is not read-only: after
+"blockdev --setro" kernel still allows to open the device in read-write mode
+but fails writes.
+
+OS X:
+
+* Fixed OS X 10.8 support.
+* Switched to 64-bit inode numbers (now Mac OS X 10.5 or later is required).
+* Switched from unmaintained MacFUSE to OSXFUSE (http://osxfuse.github.com).
+* Fixed device size detection. Now mkfs works.
+* Workarounded some utilities failures due to missing chmod() support.
+* Disabled (senseless) permission checks made by FUSE.
+
 0.9.8 (2012-08-09)
 
 * The mkfs utility can now create huge file systems (up to several exabytes).
diff --git a/SConstruct b/SConstruct
index 3206c54..e40a340 100644
--- a/SConstruct
+++ b/SConstruct
@@ -2,7 +2,7 @@
 #	SConstruct (10.09.09)
 #	SConscript for all components.
 #
-#	Copyright (C) 2010-2012  Andrew Nayenko
+#	Copyright (C) 2010-2013  Andrew Nayenko
 #
 #	This program is free software: you can redistribute it and/or modify
 #	it under the terms of the GNU General Public License as published by
@@ -23,53 +23,79 @@ import platform
 import SCons
 
 env = Environment(**ARGUMENTS)
-conf = Configure(env)
+for var in ['PATH', 'SYSROOT']:
+	if var in os.environ:
+		env['ENV'][var] = os.environ[var]
 
 destdir = env.get('DESTDIR', '/sbin');
-targets = []
 libs = ['exfat']
+libfuse = 'fuse'
+
+if not env.GetOption('clean'):
+	conf = Configure(env)
+
+	if 'AR' in os.environ:
+		conf.env.Replace(AR = os.environ['AR'])
+	if 'RANLIB' in os.environ:
+		conf.env.Replace(RANLIB = os.environ['RANLIB'])
+	if 'CC' in os.environ:
+		conf.env.Replace(CC = os.environ['CC'])
+	if 'CCFLAGS' in os.environ:
+		conf.env.Replace(CCFLAGS = os.environ['CCFLAGS'])
+	# Set default CCFLAGS for known compilers
+	if not conf.env['CCFLAGS']:
+		if conf.env['CC'] == 'gcc':
+			conf.env.Replace(CCFLAGS = '-Wall -O2 -ggdb -std=c99')
+		elif conf.env['CC'] == 'clang':
+			conf.env.Replace(CCFLAGS = '-Wall -O2 -g -std=c99')
+	if 'CPPFLAGS' in os.environ:
+		conf.env.Replace(CPPFLAGS = os.environ['CPPFLAGS'])
+	conf.env.Append(CPPDEFINES = {'_FILE_OFFSET_BITS' : 64})
+	conf.env.Append(CPPPATH = ['libexfat'])
+	if 'LDFLAGS' in os.environ:
+		conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
+	conf.env.Append(LIBPATH = ['libexfat'])
+
+	# GNU/Linux requires _BSD_SOURCE define for vsyslog(), _XOPEN_SOURCE >= 500
+	# for pread(), pwrite(), snprintf(), strdup(), etc. Everything needed is
+	# enabled by _GNU_SOURCE.
+	if platform.system() == 'Linux':
+		conf.env.Append(CPPDEFINES = '_GNU_SOURCE');
+
+	# Use 64-bit inode numbers (introduced in Mac OS X 10.5 Leopard). Require
+	# OSXFUSE (http://osxfuse.github.com).
+	if platform.system() == 'Darwin':
+		conf.env.Append(CPPDEFINES = '_DARWIN_USE_64_BIT_INODE')
+		conf.env.Append(CPPDEFINES = {'__DARWIN_UNIX03' : 1})
+		conf.env.Append(CPPPATH = ['/usr/local/include/osxfuse'])
+		conf.env.Append(CFLAGS    = '-mmacosx-version-min=10.5')
+		conf.env.Append(LINKFLAGS = '-mmacosx-version-min=10.5')
+		libfuse = 'osxfuse_i64'
+
+	# FreeBSD does not support block devices, only raw devices. Ublio is
+	# required for unaligned I/O and caching.
+	if platform.system() == 'FreeBSD':
+		conf.env.Append(CPPDEFINES = 'USE_UBLIO')
+		libs.append('ublio')
+		conf.env.Append(CPPPATH = ['/usr/local/include'])
+		conf.env.Append(LIBPATH = ['/usr/local/lib'])
+
+	if not conf.CheckCC():
+		print '''
+	A working C compiler is needed very much.
+'''
+		Exit(1)
+
+	if not conf.CheckTypeSize('off_t', '#include <sys/types.h>', 'C', 8):
+		print '''
+	The size of off_t type must be 64 bits. File systems larger than
+	2 GB will be corrupted with 32-bit off_t.
+'''
+		Exit(1)
+
+	env = conf.Finish()
+
 
-if 'CC' in os.environ:
-	conf.env.Replace(CC = os.environ['CC'])
-if 'CCFLAGS' in os.environ:
-	conf.env.Replace(CCFLAGS = os.environ['CCFLAGS'])
-# Set default CCFLAGS for known compilers
-if not conf.env['CCFLAGS']:
-	if conf.env['CC'] == 'gcc':
-		conf.env.Replace(CCFLAGS = '-Wall -O2 -ggdb -std=c99')
-	elif conf.env['CC'] == 'clang':
-		conf.env.Replace(CCFLAGS = '-Wall -O2 -g -std=c99')
-if 'CPPFLAGS' in os.environ:
-	conf.env.Replace(CPPFLAGS = os.environ['CPPFLAGS'])
-conf.env.Append(CPPDEFINES = {'_FILE_OFFSET_BITS' : 64})
-conf.env.Append(CPPPATH = ['libexfat'])
-if 'LDFLAGS' in os.environ:
-	conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
-conf.env.Append(LIBPATH = ['libexfat'])
-
-# GNU/Linux requires _BSD_SOURCE define for vsyslog(), _XOPEN_SOURCE >= 500 for
-# pread(), pwrite(), snprintf(), strdup(), etc. Everything needed is enabled by
-# _GNU_SOURCE.
-if platform.system() == 'Linux':
-	conf.env.Append(CPPDEFINES = '_GNU_SOURCE');
-
-# __DARWIN_64_BIT_INO_T=0 define is needed because since Snow Leopard inode
-# numbers are 64-bit by default, but libfuse operates 32-bit ones. This define
-# forces 32-bit inode declaration in system headers, but it's also possible to
-# link against libfuse_ino64 instead.
-if platform.system() == 'Darwin':
-	conf.env.Append(CPPDEFINES = {'__DARWIN_64_BIT_INO_T' : 0})
-	conf.env.Append(CPPDEFINES = {'__DARWIN_UNIX03' : 1})
-
-# FreeBSD does not support block devices, only raw devices. Ublio is required
-# for unaligned I/O and caching.
-if platform.system() == 'FreeBSD':
-	conf.env.Append(CPPDEFINES = 'USE_UBLIO')
-	libs.append('ublio')
-	conf.env.Append(CPPPATH = ['/usr/local/include'])
-	conf.env.Append(LIBPATH = ['/usr/local/lib'])
-
-env = conf.Finish()
 
 def make_symlink(dir, target, link_name):
 	workdir = os.getcwd()
@@ -91,18 +117,16 @@ def program(pattern, output, alias, libs):
 		return
 	target = env.Program(output, sources, LIBS = libs)
 	if alias:
-		Alias('install', Install(destdir, target),
-				symlink(destdir, os.path.basename(output), alias))
+		Clean(Alias('install', Install(destdir, target),
+				symlink(destdir, os.path.basename(output), alias)),
+				destdir + '/' + alias)
 	else:
 		Alias('install', Install(destdir, target))
-	targets.append(target)
 
 env.Library('libexfat/exfat', Glob('libexfat/*.c'))
 
-program('fuse/*.c', 'fuse/mount.exfat-fuse', 'mount.exfat', [libs + ['fuse']])
+program('fuse/*.c', 'fuse/mount.exfat-fuse', 'mount.exfat', [libs + [libfuse]])
 program('dump/*.c', 'dump/dumpexfat', None, libs)
 program('fsck/*.c', 'fsck/exfatfsck', 'fsck.exfat', libs)
 program('mkfs/*.c', 'mkfs/mkexfatfs', 'mkfs.exfat', libs)
 program('label/*.c', 'label/exfatlabel', None, libs)
-
-Default(targets)
diff --git a/fuse/main.c b/fuse/main.c
index 643df3f..1527347 100644
--- a/fuse/main.c
+++ b/fuse/main.c
@@ -2,7 +2,7 @@
 	main.c (01.09.09)
 	FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -38,7 +38,8 @@
 	#error FUSE 2.6 or later is required
 #endif
 
-const char* default_options = "ro_fallback,allow_other,blkdev";
+const char* default_options = "ro_fallback,allow_other,blkdev,big_writes,"
+		"defer_permissions";
 
 struct exfat ef;
 
@@ -57,7 +58,7 @@ static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
 	struct exfat_node* node;
 	int rc;
 
-	exfat_debug("[fuse_exfat_getattr] %s", path);
+	exfat_debug("[%s] %s", __func__, path);
 
 	rc = exfat_lookup(&ef, &node, path);
 	if (rc != 0)
@@ -73,7 +74,7 @@ static int fuse_exfat_truncate(const char* path, off_t size)
 	struct exfat_node* node;
 	int rc;
 
-	exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
+	exfat_debug("[%s] %s, %"PRId64, __func__, path, size);
 
 	rc = exfat_lookup(&ef, &node, path);
 	if (rc != 0)
@@ -93,7 +94,7 @@ static int fuse_exfat_readdir(const char* path, void* buffer,
 	int rc;
 	char name[EXFAT_NAME_MAX + 1];
 
-	exfat_debug("[fuse_exfat_readdir] %s", path);
+	exfat_debug("[%s] %s", __func__, path);
 
 	rc = exfat_lookup(&ef, &parent, path);
 	if (rc != 0)
@@ -118,9 +119,9 @@ static int fuse_exfat_readdir(const char* path, void* buffer,
 	while ((node = exfat_readdir(&ef, &it)))
 	{
 		exfat_get_name(node, name, EXFAT_NAME_MAX);
-		exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
+		exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__,
 				name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
-				(uint64_t) node->size, node->start_cluster);
+				node->size, node->start_cluster);
 		filler(buffer, name, NULL, 0);
 		exfat_put_node(&ef, node);
 	}
@@ -134,7 +135,7 @@ static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
 	struct exfat_node* node;
 	int rc;
 
-	exfat_debug("[fuse_exfat_open] %s", path);
+	exfat_debug("[%s] %s", __func__, path);
 
 	rc = exfat_lookup(&ef, &node, path);
 	if (rc != 0)
@@ -146,6 +147,7 @@ static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
 
 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
 {
+	exfat_debug("[%s] %s", __func__, path);
 	exfat_put_node(&ef, get_node(fi));
 	return 0;
 }
@@ -153,15 +155,19 @@ static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
 		off_t offset, struct fuse_file_info* fi)
 {
-	exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
-	return exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
+	exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
+	if (exfat_generic_pread(&ef, get_node(fi), buffer, size, offset) != size)
+		return EOF;
+	return size;
 }
 
 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
 		off_t offset, struct fuse_file_info* fi)
 {
-	exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
-	return exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
+	exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
+	if (exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset) != size)
+		return EOF;
+	return size;
 }
 
 static int fuse_exfat_unlink(const char* path)
@@ -169,7 +175,7 @@ static int fuse_exfat_unlink(const char* path)
 	struct exfat_node* node;
 	int rc;
 
-	exfat_debug("[fuse_exfat_unlink] %s", path);
+	exfat_debug("[%s] %s", __func__, path);
 
 	rc = exfat_lookup(&ef, &node, path);
 	if (rc != 0)
@@ -185,7 +191,7 @@ static int fuse_exfat_rmdir(const char* path)
 	struct exfat_node* node;
 	int rc;
 
-	exfat_debug("[fuse_exfat_rmdir] %s", path);
+	exfat_debug("[%s] %s", __func__, path);
 
 	rc = exfat_lookup(&ef, &node, path);
 	if (rc != 0)
@@ -198,19 +204,19 @@ static int fuse_exfat_rmdir(const char* path)
 
 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
 {
-	exfat_debug("[fuse_exfat_mknod] %s", path);
+	exfat_debug("[%s] %s 0%ho", __func__, path, mode);
 	return exfat_mknod(&ef, path);
 }
 
 static int fuse_exfat_mkdir(const char* path, mode_t mode)
 {
-	exfat_debug("[fuse_exfat_mkdir] %s", path);
+	exfat_debug("[%s] %s 0%ho", __func__, path, mode);
 	return exfat_mkdir(&ef, path);
 }
 
 static int fuse_exfat_rename(const char* old_path, const char* new_path)
 {
-	exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
+	exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
 	return exfat_rename(&ef, old_path, new_path);
 }
 
@@ -219,7 +225,7 @@ static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
 	struct exfat_node* node;
 	int rc;
 
-	exfat_debug("[fuse_exfat_utimens] %s", path);
+	exfat_debug("[%s] %s", __func__, path);
 
 	rc = exfat_lookup(&ef, &node, path);
 	if (rc != 0)
@@ -230,8 +236,19 @@ static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
 	return 0;
 }
 
+#ifdef __APPLE__
+static int fuse_exfat_chmod(const char* path, mode_t mode)
+{
+	exfat_debug("[%s] %s 0%ho", __func__, path, mode);
+	/* make OS X utilities happy */
+	return 0;
+}
+#endif
+
 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
 {
+	exfat_debug("[%s]", __func__);
+
 	sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
 	sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
 	sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
@@ -252,8 +269,18 @@ static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
 	return 0;
 }
 
+static void* fuse_exfat_init(struct fuse_conn_info* fci)
+{
+	exfat_debug("[%s]", __func__);
+#ifdef FUSE_CAP_BIG_WRITES
+	fci->want |= FUSE_CAP_BIG_WRITES;
+#endif
+	return NULL;
+}
+
 static void fuse_exfat_destroy(void* unused)
 {
+	exfat_debug("[%s]", __func__);
 	exfat_unmount(&ef);
 }
 
@@ -278,7 +305,11 @@ static struct fuse_operations fuse_exfat_ops =
 	.mkdir		= fuse_exfat_mkdir,
 	.rename		= fuse_exfat_rename,
 	.utimens	= fuse_exfat_utimens,
+#ifdef __APPLE__
+	.chmod		= fuse_exfat_chmod,
+#endif
 	.statfs		= fuse_exfat_statfs,
+	.init		= fuse_exfat_init,
 	.destroy	= fuse_exfat_destroy,
 };
 
@@ -309,15 +340,17 @@ static char* add_option(char* options, const char* name, const char* value)
 
 static char* add_fsname_option(char* options, const char* spec)
 {
-	char spec_abs[PATH_MAX];
+	char* spec_abs = realpath(spec, NULL);
 
-	if (realpath(spec, spec_abs) == NULL)
+	if (spec_abs == NULL)
 	{
 		free(options);
 		exfat_error("failed to get absolute path for `%s'", spec);
 		return NULL;
 	}
-	return add_option(options, "fsname", spec_abs);
+	options = add_option(options, "fsname", spec_abs);
+	free(spec_abs);
+	return options;
 }
 
 static char* add_user_option(char* options)
@@ -402,7 +435,7 @@ int main(int argc, char* argv[])
 		else if (strcmp(*pp, "-v") == 0)
 		{
 			free(mount_options);
-			puts("Copyright (C) 2010-2012  Andrew Nayenko");
+			puts("Copyright (C) 2010-2013  Andrew Nayenko");
 			return 0;
 		}
 		else if (spec == NULL)
@@ -427,7 +460,7 @@ int main(int argc, char* argv[])
 		return 1;
 	}
 
-	if (ef.ro_fallback)
+	if (ef.ro == -1) /* read-only fallback was used */
 	{
 		mount_options = add_option(mount_options, "ro", NULL);
 		if (mount_options == NULL)
@@ -486,22 +519,27 @@ int main(int argc, char* argv[])
 	}
 
 	/* exit session on HUP, TERM and INT signals and ignore PIPE signal */
-	if (fuse_set_signal_handlers(fuse_get_session(fh)))
+	if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
 	{
 		fuse_unmount(mount_point, fc);
 		fuse_destroy(fh);
 		exfat_unmount(&ef);
+		exfat_error("failed to set signal handlers");
 		return 1;
 	}
 
-	/* go to background unless "-d" option is passed */
-	fuse_daemonize(debug);
-
-	/* FUSE main loop */
-	fuse_loop(fh);
+	/* go to background (unless "-d" option is passed) and run FUSE
+	   main loop */
+	if (fuse_daemonize(debug) == 0)
+	{
+		if (fuse_loop(fh) != 0)
+			exfat_error("FUSE loop failure");
+	}
+	else
+		exfat_error("failed to daemonize");
 
-	/* it's quite illogical but fuse_unmount() must be called BEFORE
-	   fuse_destroy() */
+	fuse_remove_signal_handlers(fuse_get_session(fh));
+	/* note that fuse_unmount() must be called BEFORE fuse_destroy() */
 	fuse_unmount(mount_point, fc);
 	fuse_destroy(fh);
 	return 0;
diff --git a/libexfat/byteorder.h b/libexfat/byteorder.h
index abcf811..5108d08 100644
--- a/libexfat/byteorder.h
+++ b/libexfat/byteorder.h
@@ -2,7 +2,7 @@
 	byteorder.h (12.01.10)
 	Endianness stuff. exFAT uses little-endian byte order.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
diff --git a/libexfat/cluster.c b/libexfat/cluster.c
index 0418932..980e1cd 100644
--- a/libexfat/cluster.c
+++ b/libexfat/cluster.c
@@ -2,7 +2,7 @@
 	cluster.c (03.09.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -99,47 +99,31 @@ cluster_t exfat_advance_cluster(const struct exfat* ef,
 	{
 		node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
 		if (CLUSTER_INVALID(node->fptr_cluster))
-			break;
+			break; /* the caller should handle this and print appropriate 
+			          error message */
 	}
 	node->fptr_index = count;
 	return node->fptr_cluster;
 }
 
-static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
-		cluster_t end)
+static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
 {
-	const cluster_t mid_start = (start + 7) / 8 * 8;
-	const cluster_t mid_end = end / 8 * 8;
-	cluster_t c;
-	cluster_t byte;
-
-	for (c = start; c < mid_start; c++)
-		if (BMAP_GET(bitmap, c) == 0)
-		{
-			BMAP_SET(bitmap, c);
-			return c + EXFAT_FIRST_DATA_CLUSTER;
-		}
-
-	for (byte = mid_start / 8; byte < mid_end / 8; byte++)
-		if (bitmap[byte] != 0xff)
-		{
-			cluster_t bit;
-
-			for (bit = 0; bit < 8; bit++)
-				if (!(bitmap[byte] & (1u << bit)))
-				{
-					bitmap[byte] |= (1u << bit);
-					return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
-				}
-		}
-
-	for (c = mid_end; c < end; c++)
-		if (BMAP_GET(bitmap, c) == 0)
-		{
-			BMAP_SET(bitmap, c);
-			return c + EXFAT_FIRST_DATA_CLUSTER;
-		}
+	const size_t start_index = start / 8;
+	const size_t end_index = DIV_ROUND_UP(end, 8);
+	size_t i;
+	size_t c;
 
+	for (i = start_index; i < end_index; i++)
+	{
+		if (bitmap[i] == 0xff)
+			continue;
+		for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
+			if (BMAP_GET(bitmap, c) == 0)
+			{
+				BMAP_SET(bitmap, c);
+				return c + EXFAT_FIRST_DATA_CLUSTER;
+			}
+	}
 	return EXFAT_CLUSTER_END;
 }
 
@@ -147,7 +131,7 @@ void exfat_flush_cmap(struct exfat* ef)
 {
 	exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
 			exfat_c2o(ef, ef->cmap.start_cluster));
-	ef->cmap.dirty = 0;
+	ef->cmap.dirty = false;
 }
 
 static void set_next_cluster(const struct exfat* ef, int contiguous,
@@ -181,20 +165,20 @@ static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
 		return EXFAT_CLUSTER_END;
 	}
 
-	ef->cmap.dirty = 1;
+	ef->cmap.dirty = true;
 	return cluster;
 }
 
 static void free_cluster(struct exfat* ef, cluster_t cluster)
 {
 	if (CLUSTER_INVALID(cluster))
-		exfat_bug("attempting to free invalid cluster");
-	if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
-		cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
-		exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
+		exfat_bug("freeing invalid cluster 0x%x", cluster);
+	if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
+		exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
+				ef->cmap.size);
 
 	BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
-	ef->cmap.dirty = 1;
+	ef->cmap.dirty = true;
 }
 
 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
@@ -225,7 +209,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
 		previous = exfat_advance_cluster(ef, node, current - 1);
 		if (CLUSTER_INVALID(previous))
 		{
-			exfat_error("invalid cluster in file");
+			exfat_error("invalid cluster 0x%x while growing", previous);
 			return -EIO;
 		}
 	}
@@ -290,7 +274,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
 				current - difference - 1);
 		if (CLUSTER_INVALID(last))
 		{
-			exfat_error("invalid cluster in file");
+			exfat_error("invalid cluster 0x%x while shrinking", last);
 			return -EIO;
 		}
 		previous = exfat_next_cluster(ef, node, last);
@@ -309,7 +293,8 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
 	{
 		if (CLUSTER_INVALID(previous))
 		{
-			exfat_error("invalid cluster in file");
+			exfat_error("invalid cluster 0x%x while freeing after shrink",
+					previous);
 			return -EIO;
 		}
 		next = exfat_next_cluster(ef, node, previous);
@@ -340,7 +325,7 @@ static int erase_range(struct exfat* ef, struct exfat_node* node,
 			begin / CLUSTER_SIZE(*ef->sb));
 	if (CLUSTER_INVALID(cluster))
 	{
-		exfat_error("invalid cluster in file");
+		exfat_error("invalid cluster 0x%x while erasing", cluster);
 		return -EIO;
 	}
 	/* erase from the beginning to the closest cluster boundary */
@@ -352,7 +337,7 @@ static int erase_range(struct exfat* ef, struct exfat_node* node,
 		cluster = exfat_next_cluster(ef, node, cluster);
 		/* the cluster cannot be invalid because we have just allocated it */
 		if (CLUSTER_INVALID(cluster))
-			exfat_bug("invalid cluster in file");
+			exfat_bug("invalid cluster 0x%x after allocation", cluster);
 		erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
 		cluster_boundary += CLUSTER_SIZE(*ef->sb);
 	}
diff --git a/libexfat/exfat.h b/libexfat/exfat.h
index 392c95d..e5fe625 100644
--- a/libexfat/exfat.h
+++ b/libexfat/exfat.h
@@ -3,7 +3,7 @@
 	Definitions of structures and constants used in exFAT file system
 	implementation.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <stdbool.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include "exfatfs.h"
@@ -38,7 +39,8 @@
 #define IS_CONTIGUOUS(node) (((node).flags & EXFAT_ATTRIB_CONTIGUOUS) != 0)
 #define SECTOR_SIZE(sb) (1 << (sb).sector_bits)
 #define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits)
-#define CLUSTER_INVALID(c) ((c) > EXFAT_LAST_DATA_CLUSTER)
+#define CLUSTER_INVALID(c) \
+	((c) < EXFAT_FIRST_DATA_CLUSTER || (c) > EXFAT_LAST_DATA_CLUSTER)
 
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
@@ -71,6 +73,13 @@ struct exfat_node
 	le16_t name[EXFAT_NAME_MAX + 1];
 };
 
+enum exfat_mode
+{
+	EXFAT_MODE_RO,
+	EXFAT_MODE_RW,
+	EXFAT_MODE_ANY,
+};
+
 struct exfat_dev;
 
 struct exfat
@@ -86,7 +95,7 @@ struct exfat
 		uint32_t size;				/* in bits */
 		uint8_t* chunk;
 		uint32_t chunk_size;		/* in bits */
-		int dirty;
+		bool dirty;
 	}
 	cmap;
 	char label[EXFAT_ENAME_MAX * 6 + 1]; /* a character can occupy up to
@@ -96,8 +105,7 @@ struct exfat
 	uid_t uid;
 	gid_t gid;
 	int ro;
-	int ro_fallback;
-	int noatime;
+	bool noatime;
 };
 
 /* in-core nodes iterator */
@@ -124,9 +132,11 @@ void exfat_warn(const char* format, ...)
 void exfat_debug(const char* format, ...)
 	__attribute__((format(printf, 1, 2)));
 
-struct exfat_dev* exfat_open(const char* spec, int ro);
+struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode);
 int exfat_close(struct exfat_dev* dev);
 int exfat_fsync(struct exfat_dev* dev);
+enum exfat_mode exfat_get_mode(const struct exfat_dev* dev);
+off_t exfat_get_size(const struct exfat_dev* dev);
 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence);
 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size);
 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size);
diff --git a/libexfat/exfatfs.h b/libexfat/exfatfs.h
index 5a8e39f..6c58a84 100644
--- a/libexfat/exfatfs.h
+++ b/libexfat/exfatfs.h
@@ -2,7 +2,7 @@
 	exfatfs.h (29.08.09)
 	Definitions of structures and constants used in exFAT file system.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
diff --git a/libexfat/io.c b/libexfat/io.c
index f0beddc..1a555b9 100644
--- a/libexfat/io.c
+++ b/libexfat/io.c
@@ -2,7 +2,7 @@
 	io.c (02.09.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -21,30 +21,55 @@
 #include "exfat.h"
 #include <inttypes.h>
 #include <sys/types.h>
-#include <sys/uio.h>
 #include <sys/stat.h>
+#include <sys/mount.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
+#ifdef __APPLE__
+#include <sys/disk.h>
+#endif
 #ifdef USE_UBLIO
 #include <sys/uio.h>
 #include <ublio.h>
 #endif
 
-#if !defined(_FILE_OFFSET_BITS) || (_FILE_OFFSET_BITS != 64)
-	#error You should define _FILE_OFFSET_BITS=64
-#endif
-
 struct exfat_dev
 {
 	int fd;
+	enum exfat_mode mode;
+	off_t size; /* in bytes */
 #ifdef USE_UBLIO
 	off_t pos;
 	ublio_filehandle_t ufh;
 #endif
 };
 
-struct exfat_dev* exfat_open(const char* spec, int ro)
+static int open_ro(const char* spec)
+{
+	return open(spec, O_RDONLY);
+}
+
+static int open_rw(const char* spec)
+{
+	int fd = open(spec, O_RDWR);
+#ifdef __linux__
+	int ro = 0;
+
+	/*
+	   This ioctl is needed because after "blockdev --setro" kernel still
+	   allows to open the device in read-write mode but fails writes.
+	*/
+	if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
+	{
+		close(fd);
+		return -1;
+	}
+#endif
+	return fd;
+}
+
+struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
 {
 	struct exfat_dev* dev;
 	struct stat stbuf;
@@ -59,14 +84,47 @@ struct exfat_dev* exfat_open(const char* spec, int ro)
 		return NULL;
 	}
 
-	dev->fd = open(spec, ro ? O_RDONLY : O_RDWR);
-	if (dev->fd < 0)
+	switch (mode)
 	{
+	case EXFAT_MODE_RO:
+		dev->fd = open_ro(spec);
+		if (dev->fd == -1)
+		{
+			free(dev);
+			exfat_error("failed to open `%s' in read-only mode", spec);
+			return NULL;
+		}
+		dev->mode = EXFAT_MODE_RO;
+		break;
+	case EXFAT_MODE_RW:
+		dev->fd = open_rw(spec);
+		if (dev->fd == -1)
+		{
+			free(dev);
+			exfat_error("failed to open `%s' in read-write mode", spec);
+			return NULL;
+		}
+		dev->mode = EXFAT_MODE_RW;
+		break;
+	case EXFAT_MODE_ANY:
+		dev->fd = open_rw(spec);
+		if (dev->fd != -1)
+		{
+			dev->mode = EXFAT_MODE_RW;
+			break;
+		}
+		dev->fd = open_ro(spec);
+		if (dev->fd != -1)
+		{
+			dev->mode = EXFAT_MODE_RO;
+			exfat_warn("`%s' is write-protected, mounting read-only", spec);
+			break;
+		}
 		free(dev);
-		exfat_error("failed to open `%s' in read-%s mode", spec,
-				ro ? "only" : "write");
+		exfat_error("failed to open `%s'", spec);
 		return NULL;
 	}
+
 	if (fstat(dev->fd, &stbuf) != 0)
 	{
 		close(dev->fd);
@@ -84,6 +142,49 @@ struct exfat_dev* exfat_open(const char* spec, int ro)
 		return NULL;
 	}
 
+#ifdef __APPLE__
+	if (!S_ISREG(stbuf.st_mode))
+	{
+		uint32_t block_size = 0;
+		uint64_t blocks = 0;
+
+		if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
+		{
+			close(dev->fd);
+			free(dev);
+			exfat_error("failed to get block size");
+			return NULL;
+		}
+		if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
+		{
+			close(dev->fd);
+			free(dev);
+			exfat_error("failed to get blocks count");
+			return NULL;
+		}
+		dev->size = blocks * block_size;
+	}
+	else
+#endif
+	{
+		/* works for Linux, FreeBSD, Solaris */
+		dev->size = exfat_seek(dev, 0, SEEK_END);
+		if (dev->size <= 0)
+		{
+			close(dev->fd);
+			free(dev);
+			exfat_error("failed to get size of `%s'", spec);
+			return NULL;
+		}
+		if (exfat_seek(dev, 0, SEEK_SET) == -1)
+		{
+			close(dev->fd);
+			free(dev);
+			exfat_error("failed to seek to the beginning of `%s'", spec);
+			return NULL;
+		}
+	}
+
 #ifdef USE_UBLIO
 	memset(&up, 0, sizeof(struct ublio_param));
 	up.up_blocksize = 256 * 1024;
@@ -135,6 +236,16 @@ int exfat_fsync(struct exfat_dev* dev)
 	return 0;
 }
 
+enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
+{
+	return dev->mode;
+}
+
+off_t exfat_get_size(const struct exfat_dev* dev)
+{
+	return dev->size;
+}
+
 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
 {
 #ifdef USE_UBLIO
@@ -208,7 +319,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
 	cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
 	if (CLUSTER_INVALID(cluster))
 	{
-		exfat_error("got invalid cluster");
+		exfat_error("invalid cluster 0x%x while reading", cluster);
 		return -1;
 	}
 
@@ -218,7 +329,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
 	{
 		if (CLUSTER_INVALID(cluster))
 		{
-			exfat_error("got invalid cluster");
+			exfat_error("invalid cluster 0x%x while reading", cluster);
 			return -1;
 		}
 		lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
@@ -241,18 +352,15 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
 	off_t lsize, loffset, remainder;
 
 	if (offset + size > node->size)
-	{
-		int rc = exfat_truncate(ef, node, offset + size);
-		if (rc != 0)
-			return rc;
-	}
+		if (exfat_truncate(ef, node, offset + size) != 0)
+			return -1;
 	if (size == 0)
 		return 0;
 
 	cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
 	if (CLUSTER_INVALID(cluster))
 	{
-		exfat_error("got invalid cluster");
+		exfat_error("invalid cluster 0x%x while writing", cluster);
 		return -1;
 	}
 
@@ -262,7 +370,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
 	{
 		if (CLUSTER_INVALID(cluster))
 		{
-			exfat_error("got invalid cluster");
+			exfat_error("invalid cluster 0x%x while writing", cluster);
 			return -1;
 		}
 		lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
diff --git a/libexfat/log.c b/libexfat/log.c
index 8b589b4..034cc09 100644
--- a/libexfat/log.c
+++ b/libexfat/log.c
@@ -2,7 +2,7 @@
 	log.c (02.09.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
diff --git a/libexfat/lookup.c b/libexfat/lookup.c
index d650976..2e1aa81 100644
--- a/libexfat/lookup.c
+++ b/libexfat/lookup.c
@@ -2,7 +2,7 @@
 	lookup.c (02.09.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -145,14 +145,14 @@ int exfat_lookup(struct exfat* ef, struct exfat_node** node,
 	return 0;
 }
 
-static int is_last_comp(const char* comp, size_t length)
+static bool is_last_comp(const char* comp, size_t length)
 {
 	const char* p = comp + length;
 
 	return get_comp(p, &p) == 0;
 }
 
-static int is_allowed(const char* comp, size_t length)
+static bool is_allowed(const char* comp, size_t length)
 {
 	size_t i;
 
@@ -169,9 +169,9 @@ static int is_allowed(const char* comp, size_t length)
 		case '<':
 		case '>':
 		case '|':
-			return 0;
+			return false;
 		}
-	return 1;
+	return true;
 }
 
 int exfat_split(struct exfat* ef, struct exfat_node** parent,
diff --git a/libexfat/mount.c b/libexfat/mount.c
index a62466b..ec4f52e 100644
--- a/libexfat/mount.c
+++ b/libexfat/mount.c
@@ -2,7 +2,7 @@
 	mount.c (22.10.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -61,7 +61,7 @@ static int get_int_option(const char* options, const char* option_name,
 	return strtol(p, NULL, base);
 }
 
-static int match_option(const char* options, const char* option_name)
+static bool match_option(const char* options, const char* option_name)
 {
 	const char* p;
 	size_t length = strlen(option_name);
@@ -69,8 +69,8 @@ static int match_option(const char* options, const char* option_name)
 	for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
 		if ((p == options || p[-1] == ',') &&
 				(p[length] == ',' || p[length] == '\0'))
-			return 1;
-	return 0;
+			return true;
+	return false;
 }
 
 static void parse_options(struct exfat* ef, const char* options)
@@ -86,7 +86,6 @@ static void parse_options(struct exfat* ef, const char* options)
 	ef->uid = get_int_option(options, "uid", 10, geteuid());
 	ef->gid = get_int_option(options, "gid", 10, getegid());
 
-	ef->ro = match_option(options, "ro");
 	ef->noatime = match_option(options, "noatime");
 }
 
@@ -137,22 +136,28 @@ static int prepare_super_block(const struct exfat* ef)
 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
 {
 	int rc;
+	enum exfat_mode mode;
 
 	exfat_tzset();
 	memset(ef, 0, sizeof(struct exfat));
 
 	parse_options(ef, options);
 
-	ef->dev = exfat_open(spec, ef->ro);
+	if (match_option(options, "ro"))
+		mode = EXFAT_MODE_RO;
+	else if (match_option(options, "ro_fallback"))
+		mode = EXFAT_MODE_ANY;
+	else
+		mode = EXFAT_MODE_RW;
+	ef->dev = exfat_open(spec, mode);
 	if (ef->dev == NULL)
+		return -EIO;
+	if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
 	{
-		if (ef->ro || !match_option(options, "ro_fallback"))
-			return -EIO;
-		ef->dev = exfat_open(spec, 1);
-		if (ef->dev == NULL)
-			return -EIO;
-		exfat_warn("device is write-protected, mounting read-only");
-		ef->ro_fallback = ef->ro = 1;
+		if (mode == EXFAT_MODE_ANY)
+			ef->ro = -1;
+		else
+			ef->ro = 1;
 	}
 
 	ef->sb = malloc(sizeof(struct exfat_super_block));
diff --git a/libexfat/node.c b/libexfat/node.c
index 4cbb3e2..cce1de7 100644
--- a/libexfat/node.c
+++ b/libexfat/node.c
@@ -2,7 +2,7 @@
 	node.c (09.10.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -115,7 +115,8 @@ static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
 		it->cluster = exfat_next_cluster(ef, parent, it->cluster);
 		if (CLUSTER_INVALID(it->cluster))
 		{
-			exfat_error("invalid cluster while reading directory");
+			exfat_error("invalid cluster 0x%x while reading directory",
+					it->cluster);
 			return 1;
 		}
 		exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
@@ -182,6 +183,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 	le16_t* namep = NULL;
 	uint16_t reference_checksum = 0;
 	uint16_t actual_checksum = 0;
+	uint64_t real_size = 0;
 
 	*node = NULL;
 
@@ -246,18 +248,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 			}
 			init_node_meta2(*node, meta2);
 			actual_checksum = exfat_add_checksum(entry, actual_checksum);
-			/* There are two fields that contain file size. Maybe they plan
-			   to add compression support in the future and one of those
-			   fields is visible (uncompressed) size and the other is real
-			   (compressed) size. Anyway, currently it looks like exFAT does
-			   not support compression and both fields must be equal. */
-			if (le64_to_cpu(meta2->real_size) != (*node)->size)
-			{
-				exfat_error("real size does not equal to size "
-						"(%"PRIu64" != %"PRIu64")",
-						le64_to_cpu(meta2->real_size), (*node)->size);
-				goto error;
-			}
+			real_size = le64_to_cpu(meta2->real_size);
 			/* empty files must be marked as non-contiguous */
 			if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
 			{
@@ -269,11 +260,8 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 			if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
 				(*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
 			{
-				char buffer[EXFAT_NAME_MAX + 1];
-
-				exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
-				exfat_error("directory `%s' has invalid size %"PRIu64" bytes",
-						buffer, (*node)->size);
+				exfat_error("directory has invalid size %"PRIu64" bytes",
+						(*node)->size);
 				goto error;
 			}
 			--continuations;
@@ -292,10 +280,34 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 			namep += EXFAT_ENAME_MAX;
 			if (--continuations == 0)
 			{
+				/*
+				   There are two fields that contain file size. Maybe they
+				   plan to add compression support in the future and one of
+				   those fields is visible (uncompressed) size and the other
+				   is real (compressed) size. Anyway, currently it looks like
+				   exFAT does not support compression and both fields must be
+				   equal.
+
+				   There is an exception though: pagefile.sys (its real_size
+				   is always 0).
+				*/
+				if (real_size != (*node)->size)
+				{
+					char buffer[EXFAT_NAME_MAX + 1];
+
+					exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
+					exfat_error("`%s' real size does not equal to size "
+							"(%"PRIu64" != %"PRIu64")", buffer,
+							real_size, (*node)->size);
+					goto error;
+				}
 				if (actual_checksum != reference_checksum)
 				{
-					exfat_error("invalid checksum (0x%hx != 0x%hx)",
-							actual_checksum, reference_checksum);
+					char buffer[EXFAT_NAME_MAX + 1];
+
+					exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
+					exfat_error("`%s' has invalid checksum (0x%hx != 0x%hx)",
+							buffer, actual_checksum, reference_checksum);
 					goto error;
 				}
 				if (fetch_next_entry(ef, parent, it) != 0)
@@ -310,7 +322,8 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 			upcase = (const struct exfat_entry_upcase*) entry;
 			if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
 			{
-				exfat_error("invalid cluster in upcase table");
+				exfat_error("invalid cluster 0x%x in upcase table",
+						le32_to_cpu(upcase->start_cluster));
 				goto error;
 			}
 			if (le64_to_cpu(upcase->size) == 0 ||
@@ -337,9 +350,11 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 
 		case EXFAT_ENTRY_BITMAP:
 			bitmap = (const struct exfat_entry_bitmap*) entry;
-			if (CLUSTER_INVALID(le32_to_cpu(bitmap->start_cluster)))
+			ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
+			if (CLUSTER_INVALID(ef->cmap.start_cluster))
 			{
-				exfat_error("invalid cluster in clusters bitmap");
+				exfat_error("invalid cluster 0x%x in clusters bitmap",
+						ef->cmap.start_cluster);
 				goto error;
 			}
 			ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
@@ -351,7 +366,6 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 						le64_to_cpu(bitmap->size), (ef->cmap.size + 7) / 8);
 				goto error;
 			}
-			ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
 			/* FIXME bitmap can be rather big, up to 512 MB */
 			ef->cmap.chunk_size = ef->cmap.size;
 			ef->cmap.chunk = malloc(le64_to_cpu(bitmap->size));
@@ -443,17 +457,40 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
 	return 0;
 }
 
-static void reset_cache(struct exfat* ef, struct exfat_node* node)
+static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
+{
+	node->parent = dir;
+	if (dir->child)
+	{
+		dir->child->prev = node;
+		node->next = dir->child;
+	}
+	dir->child = node;
+}
+
+static void tree_detach(struct exfat_node* node)
 {
-	struct exfat_node* child;
-	struct exfat_node* next;
+	if (node->prev)
+		node->prev->next = node->next;
+	else /* this is the first node in the list */
+		node->parent->child = node->next;
+	if (node->next)
+		node->next->prev = node->prev;
+	node->parent = NULL;
+	node->prev = NULL;
+	node->next = NULL;
+}
 
-	for (child = node->child; child; child = next)
+static void reset_cache(struct exfat* ef, struct exfat_node* node)
+{
+	while (node->child)
 	{
-		reset_cache(ef, child);
-		next = child->next;
-		free(child);
+		struct exfat_node* p = node->child;
+		reset_cache(ef, p);
+		tree_detach(p);
+		free(p);
 	}
+	node->flags &= ~EXFAT_ATTRIB_CACHED;
 	if (node->references != 0)
 	{
 		char buffer[EXFAT_NAME_MAX + 1];
@@ -461,10 +498,8 @@ static void reset_cache(struct exfat* ef, struct exfat_node* node)
 		exfat_warn("non-zero reference counter (%d) for `%s'",
 				node->references, buffer);
 	}
-	while (node->references--)
+	while (node->references)
 		exfat_put_node(ef, node);
-	node->child = NULL;
-	node->flags &= ~EXFAT_ATTRIB_CACHED;
 }
 
 void exfat_reset_cache(struct exfat* ef)
@@ -549,30 +584,6 @@ static void erase_entry(struct exfat* ef, struct exfat_node* node)
 	}
 }
 
-static void tree_detach(struct exfat_node* node)
-{
-	if (node->prev)
-		node->prev->next = node->next;
-	else /* this is the first node in the list */
-		node->parent->child = node->next;
-	if (node->next)
-		node->next->prev = node->prev;
-	node->parent = NULL;
-	node->prev = NULL;
-	node->next = NULL;
-}
-
-static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
-{
-	node->parent = dir;
-	if (dir->child)
-	{
-		dir->child->prev = node;
-		node->next = dir->child;
-	}
-	dir->child = node;
-}
-
 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
 		off_t deleted_offset)
 {
@@ -896,27 +907,33 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
 	}
 	if (existing != NULL)
 	{
-		if (existing->flags & EXFAT_ATTRIB_DIR)
+		/* remove target if it's not the same node as source */
+		if (existing != node)
 		{
-			if (node->flags & EXFAT_ATTRIB_DIR)
-				rc = exfat_rmdir(ef, existing);
+			if (existing->flags & EXFAT_ATTRIB_DIR)
+			{
+				if (node->flags & EXFAT_ATTRIB_DIR)
+					rc = exfat_rmdir(ef, existing);
+				else
+					rc = -ENOTDIR;
+			}
 			else
-				rc = -ENOTDIR;
+			{
+				if (!(node->flags & EXFAT_ATTRIB_DIR))
+					rc = exfat_unlink(ef, existing);
+				else
+					rc = -EISDIR;
+			}
+			exfat_put_node(ef, existing);
+			if (rc != 0)
+			{
+				exfat_put_node(ef, dir);
+				exfat_put_node(ef, node);
+				return rc;
+			}
 		}
 		else
-		{
-			if (!(node->flags & EXFAT_ATTRIB_DIR))
-				rc = exfat_unlink(ef, existing);
-			else
-				rc = -EISDIR;
-		}
-		exfat_put_node(ef, existing);
-		if (rc != 0)
-		{
-			exfat_put_node(ef, dir);
-			exfat_put_node(ef, node);
-			return rc;
-		}
+			exfat_put_node(ef, existing);
 	}
 
 	rc = find_slot(ef, dir, &cluster, &offset,
diff --git a/libexfat/time.c b/libexfat/time.c
index 890930e..10a826a 100644
--- a/libexfat/time.c
+++ b/libexfat/time.c
@@ -2,7 +2,7 @@
 	time.c (03.02.12)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
diff --git a/libexfat/utf.c b/libexfat/utf.c
index 983c793..fd16fe9 100644
--- a/libexfat/utf.c
+++ b/libexfat/utf.c
@@ -2,7 +2,7 @@
 	utf.c (13.09.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
diff --git a/libexfat/utils.c b/libexfat/utils.c
index 7c47f4d..74a76cb 100644
--- a/libexfat/utils.c
+++ b/libexfat/utils.c
@@ -2,7 +2,7 @@
 	utils.c (04.09.09)
 	exFAT file system implementation library.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
diff --git a/libexfat/version.h b/libexfat/version.h
index f35cb4f..b795e91 100644
--- a/libexfat/version.h
+++ b/libexfat/version.h
@@ -2,7 +2,7 @@
 	version.h (12.06.10)
 	Version constants.
 
-	Copyright (C) 2010-2012  Andrew Nayenko
+	Copyright (C) 2010-2013  Andrew Nayenko
 
 	This program is free software: you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
@@ -21,8 +21,8 @@
 #ifndef VERSION_H_INCLUDED
 #define VERSION_H_INCLUDED
 
-#define EXFAT_VERSION_MAJOR 0
-#define EXFAT_VERSION_MINOR 9
-#define EXFAT_VERSION_PATCH 8
+#define EXFAT_VERSION_MAJOR 1
+#define EXFAT_VERSION_MINOR 0
+#define EXFAT_VERSION_PATCH 0
 
 #endif /* ifndef VERSION_H_INCLUDED */