update exfat from current head

Change-Id: I7d93474296612fda1dde23f6e8690668d6880e27
diff --git a/exfat/dump/main.c b/exfat/dump/main.c
index 7ef571d..5d70fde 100644
--- a/exfat/dump/main.c
+++ b/exfat/dump/main.c
@@ -3,7 +3,7 @@
 	Prints detailed information about exFAT volume.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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
@@ -85,13 +85,13 @@
 	if (exfat_read(dev, &sb, sizeof(struct exfat_super_block)) < 0)
 	{
 		exfat_close(dev);
-		exfat_error("failed to read from `%s'", spec);
+		exfat_error("failed to read from '%s'", spec);
 		return 1;
 	}
 	if (memcmp(sb.oem_name, "EXFAT   ", sizeof(sb.oem_name)) != 0)
 	{
 		exfat_close(dev);
-		exfat_error("exFAT file system is not found on `%s'", spec);
+		exfat_error("exFAT file system is not found on '%s'", spec);
 		return 1;
 	}
 
@@ -106,7 +106,7 @@
 
 static void dump_sectors(struct exfat* ef)
 {
-	off_t a = 0, b = 0;
+	off64_t a = 0, b = 0;
 
 	printf("Used sectors ");
 	while (exfat_find_used_sectors(ef, &a, &b) == 0)
@@ -167,7 +167,7 @@
 			used_sectors = true;
 			break;
 		case 'V':
-			puts("Copyright (C) 2011-2013  Andrew Nayenko");
+			puts("Copyright (C) 2011-2014  Andrew Nayenko");
 			return 0;
 		default:
 			usage(argv[0]);
diff --git a/exfat/exfat-fuse/main.c b/exfat/exfat-fuse/main.c
index abb60e7..30ea4bc 100644
--- a/exfat/exfat-fuse/main.c
+++ b/exfat/exfat-fuse/main.c
@@ -3,7 +3,7 @@
 	FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -83,6 +83,13 @@
 		return rc;
 
 	rc = exfat_truncate(&ef, node, size, true);
+	if (rc != 0)
+	{
+		exfat_flush_node(&ef, node);	/* ignore return code */
+		exfat_put_node(&ef, node);
+		return rc;
+	}
+	rc = exfat_flush_node(&ef, node);
 	exfat_put_node(&ef, node);
 	return rc;
 }
@@ -104,7 +111,7 @@
 	if (!(parent->flags & EXFAT_ATTRIB_DIR))
 	{
 		exfat_put_node(&ef, parent);
-		exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
+		exfat_error("'%s' is not a directory (0x%x)", path, parent->flags);
 		return -ENOTDIR;
 	}
 
@@ -115,7 +122,7 @@
 	if (rc != 0)
 	{
 		exfat_put_node(&ef, parent);
-		exfat_error("failed to open directory `%s'", path);
+		exfat_error("failed to open directory '%s'", path);
 		return rc;
 	}
 	while ((node = exfat_readdir(&ef, &it)))
@@ -149,9 +156,28 @@
 
 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
 {
+	/*
+	   This handler is called by FUSE on close() syscall. If the FUSE
+	   implementation does not call flush handler, we will flush node here.
+	   But in this case we will not be able to return an error to the caller.
+	   See fuse_exfat_flush() below.
+	*/
 	exfat_debug("[%s] %s", __func__, path);
+ 	exfat_flush_node(&ef, get_node(fi));
 	exfat_put_node(&ef, get_node(fi));
-	return 0;
+	return 0; /* FUSE ignores this return value */
+}
+
+static int fuse_exfat_flush(const char* path, struct fuse_file_info* fi)
+{
+	/*
+	   This handler may be called by FUSE on close() syscall. FUSE also deals
+	   with removals of open files, so we don't free clusters on close but
+	   only on rmdir and unlink. If the FUSE implementation does not call this
+	   handler we will flush node on release. See fuse_exfat_relase() above.
+	*/
+	exfat_debug("[%s] %s", __func__, path);
+	return exfat_flush_node(&ef, get_node(fi));
 }
 
 static int fuse_exfat_fsync(const char* path, int datasync,
@@ -160,9 +186,6 @@
 	int rc;
 
 	exfat_debug("[%s] %s", __func__, path);
-	rc = exfat_flush_node(&ef, get_node(fi));
-	if (rc != 0)
-		return rc;
 	rc = exfat_flush(&ef);
 	if (rc != 0)
 		return rc;
@@ -206,7 +229,9 @@
 
 	rc = exfat_unlink(&ef, node);
 	exfat_put_node(&ef, node);
-	return rc;
+	if (rc != 0)
+		return rc;
+	return exfat_cleanup_node(&ef, node);
 }
 
 static int fuse_exfat_rmdir(const char* path)
@@ -222,7 +247,9 @@
 
 	rc = exfat_rmdir(&ef, node);
 	exfat_put_node(&ef, node);
-	return rc;
+	if (rc != 0)
+		return rc;
+	return exfat_cleanup_node(&ef, node);
 }
 
 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
@@ -255,8 +282,9 @@
 		return rc;
 
 	exfat_utimes(node, tv);
+	rc = exfat_flush_node(&ef, node);
 	exfat_put_node(&ef, node);
-	return 0;
+	return rc;
 }
 
 static int fuse_exfat_chmod(const char* path, mode_t mode)
@@ -295,7 +323,7 @@
 	   b) no such thing as inode;
 	   So here we assume that inode = cluster.
 	*/
-	sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
+	sfs->f_files = le32_to_cpu(ef.sb->cluster_count);
 	sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
 	sfs->f_ffree = sfs->f_bavail;
 
@@ -330,6 +358,7 @@
 	.readdir	= fuse_exfat_readdir,
 	.open		= fuse_exfat_open,
 	.release	= fuse_exfat_release,
+	.flush		= fuse_exfat_flush,
 	.fsync		= fuse_exfat_fsync,
 	.fsyncdir	= fuse_exfat_fsync,
 	.read		= fuse_exfat_read,
@@ -350,6 +379,7 @@
 static char* add_option(char* options, const char* name, const char* value)
 {
 	size_t size;
+	char* optionsf = options;
 
 	if (value)
 		size = strlen(options) + strlen(name) + strlen(value) + 3;
@@ -359,6 +389,7 @@
 	options = realloc(options, size);
 	if (options == NULL)
 	{
+		free(optionsf);
 		exfat_error("failed to reallocate options string");
 		return NULL;
 	}
@@ -454,7 +485,7 @@
 			break;
 		case 'V':
 			free(mount_options);
-			puts("Copyright (C) 2010-2013  Andrew Nayenko");
+			puts("Copyright (C) 2010-2014  Andrew Nayenko");
 			return 0;
 		case 'v':
 			break;
diff --git a/exfat/fsck/main.c b/exfat/fsck/main.c
index 91f7545..67920aa 100644
--- a/exfat/fsck/main.c
+++ b/exfat/fsck/main.c
@@ -3,7 +3,7 @@
 	exFAT file system checker.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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
@@ -45,7 +45,7 @@
 			char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
 
 			exfat_get_name(node, name, sizeof(name) - 1);
-			exfat_error("file `%s' has invalid cluster 0x%x", name, c);
+			exfat_error("file '%s' has invalid cluster 0x%x", name, c);
 			rc = 1;
 			break;
 		}
@@ -54,7 +54,7 @@
 			char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
 
 			exfat_get_name(node, name, sizeof(name) - 1);
-			exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
+			exfat_error("cluster 0x%x of file '%s' is not allocated", c, name);
 			rc = 1;
 		}
 		c = exfat_next_cluster(ef, node, c);
@@ -72,16 +72,20 @@
 	char* entry_path;
 
 	if (exfat_lookup(ef, &parent, path) != 0)
-		exfat_bug("directory `%s' is not found", path);
+		exfat_bug("directory '%s' is not found", path);
 	if (!(parent->flags & EXFAT_ATTRIB_DIR))
-		exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
+		exfat_bug("'%s' is not a directory (0x%x)", path, parent->flags);
 	if (nodeck(ef, parent) != 0)
+	{
+		exfat_put_node(ef, parent);
 		return;
+	}
 
 	path_length = strlen(path);
 	entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
 	if (entry_path == NULL)
 	{
+		exfat_put_node(ef, parent);
 		exfat_error("out of memory");
 		return;
 	}
@@ -93,7 +97,6 @@
 	{
 		free(entry_path);
 		exfat_put_node(ef, parent);
-		exfat_error("failed to open directory `%s'", path);
 		return;
 	}
 	while ((node = exfat_readdir(ef, &it)))
@@ -146,7 +149,7 @@
 		switch (opt)
 		{
 		case 'V':
-			puts("Copyright (C) 2011-2013  Andrew Nayenko");
+			puts("Copyright (C) 2011-2014  Andrew Nayenko");
 			return 0;
 		default:
 			usage(argv[0]);
diff --git a/exfat/label/main.c b/exfat/label/main.c
index 2e5b5ca..33f0435 100644
--- a/exfat/label/main.c
+++ b/exfat/label/main.c
@@ -3,7 +3,7 @@
 	Prints or changes exFAT volume label.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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
@@ -35,7 +35,7 @@
 		{
 			printf("exfatlabel %u.%u.%u\n", EXFAT_VERSION_MAJOR,
 					EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
-			puts("Copyright (C) 2011-2013  Andrew Nayenko");
+			puts("Copyright (C) 2011-2014  Andrew Nayenko");
 			return 0;
 		}
 
diff --git a/exfat/libexfat/COPYING b/exfat/libexfat/COPYING
deleted file mode 100644
index d159169..0000000
--- a/exfat/libexfat/COPYING
+++ /dev/null
@@ -1,339 +0,0 @@
-                    GNU GENERAL PUBLIC LICENSE
-                       Version 2, June 1991
-
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
-                            Preamble
-
-  The licenses for most software are designed to take away your
-freedom to share and change it.  By contrast, the GNU General Public
-License is intended to guarantee your freedom to share and change free
-software--to make sure the software is free for all its users.  This
-General Public License applies to most of the Free Software
-Foundation's software and to any other program whose authors commit to
-using it.  (Some other Free Software Foundation software is covered by
-the GNU Lesser General Public License instead.)  You can apply it to
-your programs, too.
-
-  When we speak of free software, we are referring to freedom, not
-price.  Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-this service if you wish), that you receive source code or can get it
-if you want it, that you can change the software or use pieces of it
-in new free programs; and that you know you can do these things.
-
-  To protect your rights, we need to make restrictions that forbid
-anyone to deny you these rights or to ask you to surrender the rights.
-These restrictions translate to certain responsibilities for you if you
-distribute copies of the software, or if you modify it.
-
-  For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must give the recipients all the rights that
-you have.  You must make sure that they, too, receive or can get the
-source code.  And you must show them these terms so they know their
-rights.
-
-  We protect your rights with two steps: (1) copyright the software, and
-(2) offer you this license which gives you legal permission to copy,
-distribute and/or modify the software.
-
-  Also, for each author's protection and ours, we want to make certain
-that everyone understands that there is no warranty for this free
-software.  If the software is modified by someone else and passed on, we
-want its recipients to know that what they have is not the original, so
-that any problems introduced by others will not reflect on the original
-authors' reputations.
-
-  Finally, any free program is threatened constantly by software
-patents.  We wish to avoid the danger that redistributors of a free
-program will individually obtain patent licenses, in effect making the
-program proprietary.  To prevent this, we have made it clear that any
-patent must be licensed for everyone's free use or not licensed at all.
-
-  The precise terms and conditions for copying, distribution and
-modification follow.
-
-                    GNU GENERAL PUBLIC LICENSE
-   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-
-  0. This License applies to any program or other work which contains
-a notice placed by the copyright holder saying it may be distributed
-under the terms of this General Public License.  The "Program", below,
-refers to any such program or work, and a "work based on the Program"
-means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
-either verbatim or with modifications and/or translated into another
-language.  (Hereinafter, translation is included without limitation in
-the term "modification".)  Each licensee is addressed as "you".
-
-Activities other than copying, distribution and modification are not
-covered by this License; they are outside its scope.  The act of
-running the Program is not restricted, and the output from the Program
-is covered only if its contents constitute a work based on the
-Program (independent of having been made by running the Program).
-Whether that is true depends on what the Program does.
-
-  1. You may copy and distribute verbatim copies of the Program's
-source code as you receive it, in any medium, provided that you
-conspicuously and appropriately publish on each copy an appropriate
-copyright notice and disclaimer of warranty; keep intact all the
-notices that refer to this License and to the absence of any warranty;
-and give any other recipients of the Program a copy of this License
-along with the Program.
-
-You may charge a fee for the physical act of transferring a copy, and
-you may at your option offer warranty protection in exchange for a fee.
-
-  2. You may modify your copy or copies of the Program or any portion
-of it, thus forming a work based on the Program, and copy and
-distribute such modifications or work under the terms of Section 1
-above, provided that you also meet all of these conditions:
-
-    a) You must cause the modified files to carry prominent notices
-    stating that you changed the files and the date of any change.
-
-    b) You must cause any work that you distribute or publish, that in
-    whole or in part contains or is derived from the Program or any
-    part thereof, to be licensed as a whole at no charge to all third
-    parties under the terms of this License.
-
-    c) If the modified program normally reads commands interactively
-    when run, you must cause it, when started running for such
-    interactive use in the most ordinary way, to print or display an
-    announcement including an appropriate copyright notice and a
-    notice that there is no warranty (or else, saying that you provide
-    a warranty) and that users may redistribute the program under
-    these conditions, and telling the user how to view a copy of this
-    License.  (Exception: if the Program itself is interactive but
-    does not normally print such an announcement, your work based on
-    the Program is not required to print an announcement.)
-
-These requirements apply to the modified work as a whole.  If
-identifiable sections of that work are not derived from the Program,
-and can be reasonably considered independent and separate works in
-themselves, then this License, and its terms, do not apply to those
-sections when you distribute them as separate works.  But when you
-distribute the same sections as part of a whole which is a work based
-on the Program, the distribution of the whole must be on the terms of
-this License, whose permissions for other licensees extend to the
-entire whole, and thus to each and every part regardless of who wrote it.
-
-Thus, it is not the intent of this section to claim rights or contest
-your rights to work written entirely by you; rather, the intent is to
-exercise the right to control the distribution of derivative or
-collective works based on the Program.
-
-In addition, mere aggregation of another work not based on the Program
-with the Program (or with a work based on the Program) on a volume of
-a storage or distribution medium does not bring the other work under
-the scope of this License.
-
-  3. You may copy and distribute the Program (or a work based on it,
-under Section 2) in object code or executable form under the terms of
-Sections 1 and 2 above provided that you also do one of the following:
-
-    a) Accompany it with the complete corresponding machine-readable
-    source code, which must be distributed under the terms of Sections
-    1 and 2 above on a medium customarily used for software interchange; or,
-
-    b) Accompany it with a written offer, valid for at least three
-    years, to give any third party, for a charge no more than your
-    cost of physically performing source distribution, a complete
-    machine-readable copy of the corresponding source code, to be
-    distributed under the terms of Sections 1 and 2 above on a medium
-    customarily used for software interchange; or,
-
-    c) Accompany it with the information you received as to the offer
-    to distribute corresponding source code.  (This alternative is
-    allowed only for noncommercial distribution and only if you
-    received the program in object code or executable form with such
-    an offer, in accord with Subsection b above.)
-
-The source code for a work means the preferred form of the work for
-making modifications to it.  For an executable work, complete source
-code means all the source code for all modules it contains, plus any
-associated interface definition files, plus the scripts used to
-control compilation and installation of the executable.  However, as a
-special exception, the source code distributed need not include
-anything that is normally distributed (in either source or binary
-form) with the major components (compiler, kernel, and so on) of the
-operating system on which the executable runs, unless that component
-itself accompanies the executable.
-
-If distribution of executable or object code is made by offering
-access to copy from a designated place, then offering equivalent
-access to copy the source code from the same place counts as
-distribution of the source code, even though third parties are not
-compelled to copy the source along with the object code.
-
-  4. You may not copy, modify, sublicense, or distribute the Program
-except as expressly provided under this License.  Any attempt
-otherwise to copy, modify, sublicense or distribute the Program is
-void, and will automatically terminate your rights under this License.
-However, parties who have received copies, or rights, from you under
-this License will not have their licenses terminated so long as such
-parties remain in full compliance.
-
-  5. You are not required to accept this License, since you have not
-signed it.  However, nothing else grants you permission to modify or
-distribute the Program or its derivative works.  These actions are
-prohibited by law if you do not accept this License.  Therefore, by
-modifying or distributing the Program (or any work based on the
-Program), you indicate your acceptance of this License to do so, and
-all its terms and conditions for copying, distributing or modifying
-the Program or works based on it.
-
-  6. Each time you redistribute the Program (or any work based on the
-Program), the recipient automatically receives a license from the
-original licensor to copy, distribute or modify the Program subject to
-these terms and conditions.  You may not impose any further
-restrictions on the recipients' exercise of the rights granted herein.
-You are not responsible for enforcing compliance by third parties to
-this License.
-
-  7. If, as a consequence of a court judgment or allegation of patent
-infringement or for any other reason (not limited to patent issues),
-conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License.  If you cannot
-distribute so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you
-may not distribute the Program at all.  For example, if a patent
-license would not permit royalty-free redistribution of the Program by
-all those who receive copies directly or indirectly through you, then
-the only way you could satisfy both it and this License would be to
-refrain entirely from distribution of the Program.
-
-If any portion of this section is held invalid or unenforceable under
-any particular circumstance, the balance of the section is intended to
-apply and the section as a whole is intended to apply in other
-circumstances.
-
-It is not the purpose of this section to induce you to infringe any
-patents or other property right claims or to contest validity of any
-such claims; this section has the sole purpose of protecting the
-integrity of the free software distribution system, which is
-implemented by public license practices.  Many people have made
-generous contributions to the wide range of software distributed
-through that system in reliance on consistent application of that
-system; it is up to the author/donor to decide if he or she is willing
-to distribute software through any other system and a licensee cannot
-impose that choice.
-
-This section is intended to make thoroughly clear what is believed to
-be a consequence of the rest of this License.
-
-  8. If the distribution and/or use of the Program is restricted in
-certain countries either by patents or by copyrighted interfaces, the
-original copyright holder who places the Program under this License
-may add an explicit geographical distribution limitation excluding
-those countries, so that distribution is permitted only in or among
-countries not thus excluded.  In such case, this License incorporates
-the limitation as if written in the body of this License.
-
-  9. The Free Software Foundation may publish revised and/or new versions
-of the General Public License from time to time.  Such new versions will
-be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
-Each version is given a distinguishing version number.  If the Program
-specifies a version number of this License which applies to it and "any
-later version", you have the option of following the terms and conditions
-either of that version or of any later version published by the Free
-Software Foundation.  If the Program does not specify a version number of
-this License, you may choose any version ever published by the Free Software
-Foundation.
-
-  10. If you wish to incorporate parts of the Program into other free
-programs whose distribution conditions are different, write to the author
-to ask for permission.  For software which is copyrighted by the Free
-Software Foundation, write to the Free Software Foundation; we sometimes
-make exceptions for this.  Our decision will be guided by the two goals
-of preserving the free status of all derivatives of our free software and
-of promoting the sharing and reuse of software generally.
-
-                            NO WARRANTY
-
-  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
-FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
-OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
-PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
-OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
-TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
-PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
-REPAIR OR CORRECTION.
-
-  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
-REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
-INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
-OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
-TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
-YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGES.
-
-                     END OF TERMS AND CONDITIONS
-
-            How to Apply These Terms to Your New Programs
-
-  If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
-  To do so, attach the following notices to the program.  It is safest
-to attach them to the start of each source file to most effectively
-convey the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
-    <one line to give the program's name and a brief idea of what it does.>
-    Copyright (C) <year>  <name of author>
-
-    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
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program 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; if not, write to the Free Software Foundation, Inc.,
-    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-Also add information on how to contact you by electronic and paper mail.
-
-If the program is interactive, make it output a short notice like this
-when it starts in an interactive mode:
-
-    Gnomovision version 69, Copyright (C) year name of author
-    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
-    This is free software, and you are welcome to redistribute it
-    under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License.  Of course, the commands you use may
-be called something other than `show w' and `show c'; they could even be
-mouse-clicks or menu items--whatever suits your program.
-
-You should also get your employer (if you work as a programmer) or your
-school, if any, to sign a "copyright disclaimer" for the program, if
-necessary.  Here is a sample; alter the names:
-
-  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
-  `Gnomovision' (which makes passes at compilers) written by James Hacker.
-
-  <signature of Ty Coon>, 1 April 1989
-  Ty Coon, President of Vice
-
-This General Public License does not permit incorporating your program into
-proprietary programs.  If your program is a subroutine library, you may
-consider it more useful to permit linking proprietary applications with the
-library.  If this is what you want to do, use the GNU Lesser General
-Public License instead of this License.
diff --git a/exfat/libexfat/byteorder.h b/exfat/libexfat/byteorder.h
index 10746e1..dd039f2 100644
--- a/exfat/libexfat/byteorder.h
+++ b/exfat/libexfat/byteorder.h
@@ -3,7 +3,7 @@
 	Endianness stuff. exFAT uses little-endian byte order.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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,68 +23,15 @@
 #ifndef BYTEORDER_H_INCLUDED
 #define BYTEORDER_H_INCLUDED
 
-#define __GLIBC__
 #include <stdint.h>
-
-#if defined(__GLIBC__)
-
-#include <endian.h>
-#include <byteswap.h>
-
-#elif defined(__APPLE__)
-
-#include <machine/endian.h>
-#include <libkern/OSByteOrder.h>
-#define bswap_16(x) OSSwapInt16(x)
-#define bswap_32(x) OSSwapInt32(x)
-#define bswap_64(x) OSSwapInt64(x)
-#define __BYTE_ORDER BYTE_ORDER
-#define __LITTLE_ENDIAN LITTLE_ENDIAN
-#define __BIG_ENDIAN BIG_ENDIAN
-
-#elif defined(__FreeBSD__) || defined(__DragonFlyBSD__) || defined(__NetBSD__)
-
-#include <sys/endian.h>
-#define bswap_16(x) bswap16(x)
-#define bswap_32(x) bswap32(x)
-#define bswap_64(x) bswap64(x)
-#define __BYTE_ORDER _BYTE_ORDER
-#define __LITTLE_ENDIAN _LITTLE_ENDIAN
-#define __BIG_ENDIAN _BIG_ENDIAN
-
-#elif defined(__OpenBSD__)
-
-#include <machine/endian.h>
-#define bswap_16(x) swap16(x)
-#define bswap_32(x) swap32(x)
-#define bswap_64(x) swap64(x)
-#define __BYTE_ORDER _BYTE_ORDER
-#define __LITTLE_ENDIAN _LITTLE_ENDIAN
-#define __BIG_ENDIAN _BIG_ENDIAN
-
-#elif defined(__sun)
-
-#include <sys/byteorder.h>
-#define bswap_16(x) BSWAP_16(x)
-#define bswap_32(x) BSWAP_32(x)
-#define bswap_64(x) BSWAP_64(x)
-#define __LITTLE_ENDIAN 1234
-#define __BIG_ENDIAN 4321
-#ifdef _LITTLE_ENDIAN
-#define __BYTE_ORDER __LITTLE_ENDIAN
-#else
-#define __BYTE_ORDER __BIG_ENDIAN
-#endif
-
-#else 
-#error No byte order macros available for your platform
-#endif
+#include "platform.h"
 
 typedef struct { uint16_t __u16; } le16_t;
 typedef struct { uint32_t __u32; } le32_t;
 typedef struct { uint64_t __u64; } le64_t;
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#if EXFAT_BYTE_ORDER == EXFAT_LITTLE_ENDIAN
+
 static inline uint16_t le16_to_cpu(le16_t v) { return v.__u16; }
 static inline uint32_t le32_to_cpu(le32_t v) { return v.__u32; }
 static inline uint64_t le64_to_cpu(le64_t v) { return v.__u64; }
@@ -92,17 +39,27 @@
 static inline le16_t cpu_to_le16(uint16_t v) { le16_t t = {v}; return t; }
 static inline le32_t cpu_to_le32(uint32_t v) { le32_t t = {v}; return t; }
 static inline le64_t cpu_to_le64(uint64_t v) { le64_t t = {v}; return t; }
-#elif __BYTE_ORDER == __BIG_ENDIAN
-static inline uint16_t le16_to_cpu(le16_t v) { return bswap_16(v.__u16); }
-static inline uint32_t le32_to_cpu(le32_t v) { return bswap_32(v.__u32); }
-static inline uint64_t le64_to_cpu(le64_t v) { return bswap_64(v.__u64); }
+
+typedef size_t bitmap_t;
+
+#elif EXFAT_BYTE_ORDER == EXFAT_BIG_ENDIAN
+
+static inline uint16_t le16_to_cpu(le16_t v)
+	{ return exfat_bswap16(v.__u16); }
+static inline uint32_t le32_to_cpu(le32_t v)
+	{ return exfat_bswap32(v.__u32); }
+static inline uint64_t le64_to_cpu(le64_t v)
+	{ return exfat_bswap64(v.__u64); }
 
 static inline le16_t cpu_to_le16(uint16_t v)
-	{ le16_t t = {bswap_16(v)}; return t; }
+	{ le16_t t = {exfat_bswap16(v)}; return t; }
 static inline le32_t cpu_to_le32(uint32_t v)
-	{ le32_t t = {bswap_32(v)}; return t; }
+	{ le32_t t = {exfat_bswap32(v)}; return t; }
 static inline le64_t cpu_to_le64(uint64_t v)
-	{ le64_t t = {bswap_64(v)}; return t; }
+	{ le64_t t = {exfat_bswap64(v)}; return t; }
+
+typedef unsigned char bitmap_t;
+
 #else
 #error Wow! You have a PDP machine?!
 #endif
diff --git a/exfat/libexfat/cluster.c b/exfat/libexfat/cluster.c
index 2215636..80e3792 100644
--- a/exfat/libexfat/cluster.c
+++ b/exfat/libexfat/cluster.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -83,9 +83,9 @@
 		return cluster + 1;
 	fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
 		+ cluster * sizeof(cluster_t);
-	/* FIXME handle I/O error */
 	if (exfat_pread(ef->dev, &next, sizeof(next), fat_offset) < 0)
-		exfat_bug("failed to read the next cluster after %#x", cluster);
+		return EXFAT_CLUSTER_BAD; /* the caller should handle this and print
+		                             appropriate error message */
 	return le32_to_cpu(next);
 }
 
@@ -136,8 +136,23 @@
 	return EXFAT_CLUSTER_END;
 }
 
+static int flush_nodes(struct exfat* ef, struct exfat_node* node)
+{
+	struct exfat_node* p;
+
+	for (p = node->child; p != NULL; p = p->next)
+	{
+		int rc = flush_nodes(ef, p);
+		if (rc != 0)
+			return rc;
+	}
+	return exfat_flush_node(ef, node);
+}
+
 int exfat_flush(struct exfat* ef)
 {
+	int rc = flush_nodes(ef, ef->root);
+
 	if (ef->cmap.dirty)
 	{
 		if (exfat_pwrite(ef->dev, ef->cmap.chunk,
@@ -149,7 +164,8 @@
 		}
 		ef->cmap.dirty = false;
 	}
-	return 0;
+
+	return rc;
 }
 
 static bool set_next_cluster(const struct exfat* ef, bool contiguous,
@@ -316,6 +332,7 @@
 	{
 		previous = node->start_cluster;
 		node->start_cluster = EXFAT_CLUSTER_FREE;
+		node->flags |= EXFAT_ATTRIB_DIRTY;
 	}
 	node->fptr_index = 0;
 	node->fptr_cluster = node->start_cluster;
diff --git a/exfat/libexfat/compiler.h b/exfat/libexfat/compiler.h
index 6edd8ec..473726d 100644
--- a/exfat/libexfat/compiler.h
+++ b/exfat/libexfat/compiler.h
@@ -4,7 +4,7 @@
 	showstopper.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -24,6 +24,12 @@
 #ifndef COMPILER_H_INCLUDED
 #define COMPILER_H_INCLUDED
 
+/*
+#if __STDC_VERSION__ < 199901L
+#error C99-compliant compiler is required
+#endif
+*/
+
 #if defined(__clang__)
 
 #define PRINTF __attribute__((format(printf, 1, 2)))
diff --git a/exfat/libexfat/exfat.h b/exfat/libexfat/exfat.h
index ff58e84..69fb45d 100644
--- a/exfat/libexfat/exfat.h
+++ b/exfat/libexfat/exfat.h
@@ -4,7 +4,7 @@
 	implementation.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -51,7 +51,6 @@
 #define ROUND_UP(x, d) (DIV_ROUND_UP(x, d) * (d))
 #define UTF8_BYTES(c) ((c) * 6) /* UTF-8 character can occupy up to 6 bytes */
 
-typedef size_t bitmap_t;
 #define BMAP_SIZE(count) (ROUND_UP(count, sizeof(bitmap_t) * 8) / 8)
 #define BMAP_BLOCK(index) ((index) / sizeof(bitmap_t) / 8)
 #define BMAP_MASK(index) ((bitmap_t) 1 << ((index) % (sizeof(bitmap_t) * 8)))
@@ -194,6 +193,7 @@
 
 struct exfat_node* exfat_get_node(struct exfat_node* node);
 void exfat_put_node(struct exfat* ef, struct exfat_node* node);
+int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node);
 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir);
 void exfat_reset_cache(struct exfat* ef);
 int exfat_flush_node(struct exfat* ef, struct exfat_node* node);
diff --git a/exfat/libexfat/exfatfs.h b/exfat/libexfat/exfatfs.h
index a436daa..fbec79f 100644
--- a/exfat/libexfat/exfatfs.h
+++ b/exfat/libexfat/exfatfs.h
@@ -3,7 +3,7 @@
 	Definitions of structures and constants used in exFAT file system.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -153,10 +153,10 @@
 	uint8_t name_length;
 	le16_t name_hash;
 	le16_t __unknown2;
-	le64_t real_size;				/* in bytes, equals to size */
+	le64_t valid_size;				/* in bytes, less or equal to size */
 	uint8_t __unknown3[4];
 	le32_t start_cluster;
-	le64_t size;					/* in bytes, equals to real_size */
+	le64_t size;					/* in bytes */
 }
 PACKED;
 STATIC_ASSERT(sizeof(struct exfat_entry_meta2) == 32);
diff --git a/exfat/libexfat/io.c b/exfat/libexfat/io.c
index 306c5dd..a861bdd 100644
--- a/exfat/libexfat/io.c
+++ b/exfat/libexfat/io.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -24,13 +24,19 @@
 #include <inttypes.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mount.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
-#ifdef __APPLE__
+#include <errno.h>
+#if defined(__APPLE__)
 #include <sys/disk.h>
+#elif defined(__OpenBSD__)
+#include <sys/param.h>
+#include <sys/disklabel.h>
+#include <sys/dkio.h>
+#include <sys/ioctl.h>
 #endif
+#include <sys/mount.h>
 #ifdef USE_UBLIO
 #include <sys/uio.h>
 #include <ublio.h>
@@ -65,6 +71,7 @@
 	if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
 	{
 		close(fd);
+		errno = EROFS;
 		return -1;
 	}
 #endif
@@ -93,7 +100,8 @@
 		if (dev->fd == -1)
 		{
 			free(dev);
-			exfat_error("failed to open `%s' in read-only mode", spec);
+			exfat_error("failed to open '%s' in read-only mode: %s", spec,
+					strerror(errno));
 			return NULL;
 		}
 		dev->mode = EXFAT_MODE_RO;
@@ -103,7 +111,8 @@
 		if (dev->fd == -1)
 		{
 			free(dev);
-			exfat_error("failed to open `%s' in read-write mode", spec);
+			exfat_error("failed to open '%s' in read-write mode: %s", spec,
+					strerror(errno));
 			return NULL;
 		}
 		dev->mode = EXFAT_MODE_RW;
@@ -119,11 +128,11 @@
 		if (dev->fd != -1)
 		{
 			dev->mode = EXFAT_MODE_RO;
-			exfat_warn("`%s' is write-protected, mounting read-only", spec);
+			exfat_warn("'%s' is write-protected, mounting read-only", spec);
 			break;
 		}
 		free(dev);
-		exfat_error("failed to open `%s'", spec);
+		exfat_error("failed to open '%s': %s", spec, strerror(errno));
 		return NULL;
 	}
 
@@ -131,7 +140,7 @@
 	{
 		close(dev->fd);
 		free(dev);
-		exfat_error("failed to fstat `%s'", spec);
+		exfat_error("failed to fstat '%s'", spec);
 		return NULL;
 	}
 	if (!S_ISBLK(stbuf.st_mode) &&
@@ -140,11 +149,11 @@
 	{
 		close(dev->fd);
 		free(dev);
-		exfat_error("`%s' is neither a device, nor a regular file", spec);
+		exfat_error("'%s' is neither a device, nor a regular file", spec);
 		return NULL;
 	}
 
-#ifdef __APPLE__
+#if defined(__APPLE__)
 	if (!S_ISREG(stbuf.st_mode))
 	{
 		uint32_t block_size = 0;
@@ -167,6 +176,32 @@
 		dev->size = blocks * block_size;
 	}
 	else
+#elif defined(__OpenBSD__)
+	if (!S_ISREG(stbuf.st_mode))
+	{
+		struct disklabel lab;
+		struct partition* pp;
+		char* partition;
+
+		if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
+		{
+			close(dev->fd);
+			free(dev);
+			exfat_error("failed to get disklabel");
+			return NULL;
+		}
+
+		/* Don't need to check that partition letter is valid as we won't get
+		   this far otherwise. */
+		partition = strchr(spec, '\0') - 1;
+		pp = &(lab.d_partitions[*partition - 'a']);
+		dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
+
+		if (pp->p_fstype != FS_NTFS)
+			exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
+					"you can fix this with fdisk(8)");
+	}
+	else
 #endif
 	{
 		/* works for Linux, FreeBSD, Solaris */
@@ -175,14 +210,14 @@
 		{
 			close(dev->fd);
 			free(dev);
-			exfat_error("failed to get size of `%s'", spec);
+			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);
+			exfat_error("failed to seek to the beginning of '%s'", spec);
 			return NULL;
 		}
 	}
@@ -210,32 +245,41 @@
 
 int exfat_close(struct exfat_dev* dev)
 {
+	int rc = 0;
+
 #ifdef USE_UBLIO
 	if (ublio_close(dev->ufh) != 0)
+	{
 		exfat_error("failed to close ublio");
+		rc = -EIO;
+	}
 #endif
 	if (close(dev->fd) != 0)
 	{
-		free(dev);
-		exfat_error("failed to close device");
-		return 1;
+		exfat_error("failed to close device: %s", strerror(errno));
+		rc = -EIO;
 	}
 	free(dev);
-	return 0;
+	return rc;
 }
 
 int exfat_fsync(struct exfat_dev* dev)
 {
+	int rc = 0;
+
 #ifdef USE_UBLIO
 	if (ublio_fsync(dev->ufh) != 0)
-#else
-	if (fsync(dev->fd) != 0)
-#endif
 	{
-		exfat_error("fsync failed");
-		return 1;
+		exfat_error("ublio fsync failed");
+		rc = -EIO;
 	}
-	return 0;
+#endif
+	if (fsync(dev->fd) != 0)
+	{
+		exfat_error("fsync failed: %s", strerror(errno));
+		rc = -EIO;
+	}
+	return rc;
 }
 
 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
diff --git a/exfat/libexfat/log.c b/exfat/libexfat/log.c
index 7e541aa..9c29550 100644
--- a/exfat/libexfat/log.c
+++ b/exfat/libexfat/log.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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/exfat/libexfat/lookup.c b/exfat/libexfat/lookup.c
index 00cd533..51915e6 100644
--- a/exfat/libexfat/lookup.c
+++ b/exfat/libexfat/lookup.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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/exfat/libexfat/mount.c b/exfat/libexfat/mount.c
index 928c561..027f8b0 100644
--- a/exfat/libexfat/mount.c
+++ b/exfat/libexfat/mount.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <unistd.h>
 #include <sys/types.h>
 
@@ -39,6 +40,12 @@
 		   to indicate this */
 		rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
 	}
+	if (rootdir_cluster != EXFAT_CLUSTER_END)
+	{
+		exfat_error("bad cluster %#x while reading root directory",
+				rootdir_cluster);
+		return 0;
+	}
 	return clusters * CLUSTER_SIZE(*ef->sb);
 }
 
@@ -91,7 +98,7 @@
 	ef->noatime = match_option(options, "noatime");
 }
 
-static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
+static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
 		off64_t sector_size)
 {
 	uint32_t vbr_checksum;
@@ -100,7 +107,7 @@
 	if (exfat_pread(dev, sector, sector_size, 0) < 0)
 	{
 		exfat_error("failed to read boot sector");
-		return 1;
+		return false;
 	}
 	vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
 	for (i = 1; i < 11; i++)
@@ -108,7 +115,7 @@
 		if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
 		{
 			exfat_error("failed to read VBR sector");
-			return 1;
+			return false;
 		}
 		vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
 				vbr_checksum);
@@ -116,16 +123,16 @@
 	if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
 	{
 		exfat_error("failed to read VBR checksum sector");
-		return 1;
+		return false;
 	}
 	for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
 		if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
 		{
 			exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
 					le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
-			return 1;
+			return false;
 		}
-	return 0;
+	return true;
 }
 
 static int commit_super_block(const struct exfat* ef)
@@ -201,31 +208,6 @@
 		exfat_error("exFAT file system is not found");
 		return -EIO;
 	}
-	if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
-	{
-		exfat_close(ef->dev);
-		exfat_error("unsupported exFAT version: %hhu.%hhu",
-				ef->sb->version.major, ef->sb->version.minor);
-		free(ef->sb);
-		return -EIO;
-	}
-	if (ef->sb->fat_count != 1)
-	{
-		exfat_close(ef->dev);
-		free(ef->sb);
-		exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
-		return -EIO;
-	}
-	/* officially exFAT supports cluster size up to 32 MB */
-	if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
-	{
-		exfat_close(ef->dev);
-		free(ef->sb);
-		exfat_error("too big cluster size: 2^%d",
-				(int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
-		return -EIO;
-	}
-
 	ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
 	if (ef->zero_cluster == NULL)
 	{
@@ -235,8 +217,7 @@
 		return -ENOMEM;
 	}
 	/* use zero_cluster as a temporary buffer for VBR checksum verification */
-	if (verify_vbr_checksum(ef->dev, ef->zero_cluster,
-			SECTOR_SIZE(*ef->sb)) != 0)
+	if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb)))
 	{
 		free(ef->zero_cluster);
 		exfat_close(ef->dev);
@@ -244,6 +225,45 @@
 		return -EIO;
 	}
 	memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
+	if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
+	{
+		free(ef->zero_cluster);
+		exfat_close(ef->dev);
+		exfat_error("unsupported exFAT version: %hhu.%hhu",
+				ef->sb->version.major, ef->sb->version.minor);
+		free(ef->sb);
+		return -EIO;
+	}
+	if (ef->sb->fat_count != 1)
+	{
+		free(ef->zero_cluster);
+		exfat_close(ef->dev);
+		exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
+		free(ef->sb);
+		return -EIO;
+	}
+	/* officially exFAT supports cluster size up to 32 MB */
+	if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
+	{
+		free(ef->zero_cluster);
+		exfat_close(ef->dev);
+		exfat_error("too big cluster size: 2^%d",
+				(int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
+		free(ef->sb);
+		return -EIO;
+	}
+	if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
+			exfat_get_size(ef->dev))
+	{
+		free(ef->zero_cluster);
+		exfat_error("file system is larger than underlying device: "
+				"%"PRIu64" > %"PRIu64,
+				le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb),
+				exfat_get_size(ef->dev));
+		exfat_close(ef->dev);
+		free(ef->sb);
+		return -EIO;
+	}
 
 	ef->root = malloc(sizeof(struct exfat_node));
 	if (ef->root == NULL)
@@ -260,6 +280,14 @@
 	ef->root->fptr_cluster = ef->root->start_cluster;
 	ef->root->name[0] = cpu_to_le16('\0');
 	ef->root->size = rootdir_size(ef);
+	if (ef->root->size == 0)
+	{
+		free(ef->root);
+		free(ef->zero_cluster);
+		exfat_close(ef->dev);
+		free(ef->sb);
+		return -EIO;
+	}
 	/* exFAT does not have time attributes for the root directory */
 	ef->root->mtime = 0;
 	ef->root->atime = 0;
@@ -314,11 +342,12 @@
 		ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
 	}
 
-	commit_super_block(ef);
+	commit_super_block(ef);	/* ignore return code */
 }
 
 void exfat_unmount(struct exfat* ef)
 {
+	exfat_flush(ef);	/* ignore return code */
 	exfat_put_node(ef, ef->root);
 	exfat_reset_cache(ef);
 	free(ef->root);
diff --git a/exfat/libexfat/node.c b/exfat/libexfat/node.c
index 56205fc..1d66387 100644
--- a/exfat/libexfat/node.c
+++ b/exfat/libexfat/node.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -44,28 +44,44 @@
 
 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
 {
-	if (--node->references < 0)
-	{
-		char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
-		exfat_get_name(node, buffer, sizeof(buffer) - 1);
-		exfat_bug("reference counter of `%s' is below zero", buffer);
-	}
+	char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
 
-	if (node->references == 0)
+	--node->references;
+	if (node->references < 0)
 	{
-		/* FIXME handle I/O error */
-		if (exfat_flush_node(ef, node) != 0)
-			exfat_bug("node flush failed");
-		if (node->flags & EXFAT_ATTRIB_UNLINKED)
-		{
-			/* free all clusters and node structure itself */
-			exfat_truncate(ef, node, 0, true);
-			free(node);
-		}
-		/* FIXME handle I/O error */
-		if (exfat_flush(ef) != 0)
-			exfat_bug("flush failed");
+		exfat_get_name(node, buffer, sizeof(buffer) - 1);
+		exfat_bug("reference counter of '%s' is below zero", buffer);
 	}
+	else if (node->references == 0 && node != ef->root)
+	{
+		if (node->flags & EXFAT_ATTRIB_DIRTY)
+		{
+			exfat_get_name(node, buffer, sizeof(buffer) - 1);
+			exfat_warn("dirty node '%s' with zero references", buffer);
+		}
+	}
+}
+
+/**
+ * This function must be called on rmdir and unlink (after the last
+ * exfat_put_node()) to free clusters.
+ */
+int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
+{
+	int rc = 0;
+
+	if (node->references != 0)
+		exfat_bug("unable to cleanup a node with %d references",
+				node->references);
+
+	if (node->flags & EXFAT_ATTRIB_UNLINKED)
+	{
+		/* free all clusters and node structure itself */
+		rc = exfat_truncate(ef, node, 0, true);
+		/* free the node even in case of error or its memory will be lost */
+		free(node);
+	}
+	return rc;
 }
 
 /**
@@ -108,7 +124,7 @@
 	it->chunk = NULL;
 }
 
-static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
+static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
 		struct iterator* it)
 {
 	/* move iterator to the next entry in the directory */
@@ -119,23 +135,23 @@
 		/* reached the end of directory; the caller should check this
 		   condition too */
 		if (it->offset >= parent->size)
-			return 0;
+			return true;
 		it->cluster = exfat_next_cluster(ef, parent, it->cluster);
 		if (CLUSTER_INVALID(it->cluster))
 		{
 			exfat_error("invalid cluster 0x%x while reading directory",
 					it->cluster);
-			return 1;
+			return false;
 		}
 		if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
 				exfat_c2o(ef, it->cluster)) < 0)
 		{
 			exfat_error("failed to read the next directory cluster %#x",
 					it->cluster);
-			return 1;
+			return false;
 		}
 	}
-	return 0;
+	return true;
 }
 
 static struct exfat_node* allocate_node(void)
@@ -177,6 +193,40 @@
 			(it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
 }
 
+static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
+		uint16_t reference_checksum, uint64_t valid_size)
+{
+	char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+
+	/*
+	   Validate checksum first. If it's invalid all other fields probably
+	   contain just garbage.
+	*/
+	if (actual_checksum != reference_checksum)
+	{
+		exfat_get_name(node, buffer, sizeof(buffer) - 1);
+		exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
+				actual_checksum, reference_checksum);
+		return false;
+	}
+
+	/*
+	   exFAT does not support sparse files but allows files with uninitialized
+	   clusters. For such files valid_size means initialized data size and
+	   cannot be greater than file size. See SetFileValidData() function
+	   description in MSDN.
+	*/
+	if (valid_size > node->size)
+	{
+		exfat_get_name(node, buffer, sizeof(buffer) - 1);
+		exfat_error("'%s' has valid size (%"PRIu64") greater than size "
+				"(%"PRIu64")", buffer, valid_size, node->size);
+		return false;
+	}
+
+	return true;
+}
+
 /*
  * Reads one entry in directory at position pointed by iterator and fills
  * node structure.
@@ -196,7 +246,7 @@
 	le16_t* namep = NULL;
 	uint16_t reference_checksum = 0;
 	uint16_t actual_checksum = 0;
-	uint64_t real_size = 0;
+	uint64_t valid_size = 0;
 
 	*node = NULL;
 
@@ -267,7 +317,7 @@
 			}
 			init_node_meta2(*node, meta2);
 			actual_checksum = exfat_add_checksum(entry, actual_checksum);
-			real_size = le64_to_cpu(meta2->real_size);
+			valid_size = le64_to_cpu(meta2->valid_size);
 			/* empty files must be marked as non-contiguous */
 			if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
 			{
@@ -302,37 +352,10 @@
 			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[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
-
-					exfat_get_name(*node, buffer, sizeof(buffer) - 1);
-					exfat_error("`%s' real size does not equal to size "
-							"(%"PRIu64" != %"PRIu64")", buffer,
-							real_size, (*node)->size);
+				if (!check_node(*node, actual_checksum, reference_checksum,
+						valid_size))
 					goto error;
-				}
-				if (actual_checksum != reference_checksum)
-				{
-					char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
-
-					exfat_get_name(*node, buffer, sizeof(buffer) - 1);
-					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)
+				if (!fetch_next_entry(ef, parent, it))
 					goto error;
 				return 0; /* entry completed */
 			}
@@ -439,7 +462,7 @@
 			break;
 		}
 
-		if (fetch_next_entry(ef, parent, it) != 0)
+		if (!fetch_next_entry(ef, parent, it))
 			goto error;
 	}
 	/* we never reach here */
@@ -520,6 +543,8 @@
 
 static void reset_cache(struct exfat* ef, struct exfat_node* node)
 {
+	char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+
 	while (node->child)
 	{
 		struct exfat_node* p = node->child;
@@ -530,11 +555,15 @@
 	node->flags &= ~EXFAT_ATTRIB_CACHED;
 	if (node->references != 0)
 	{
-		char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
 		exfat_get_name(node, buffer, sizeof(buffer) - 1);
-		exfat_warn("non-zero reference counter (%d) for `%s'",
+		exfat_warn("non-zero reference counter (%d) for '%s'",
 				node->references, buffer);
 	}
+	if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
+	{
+		exfat_get_name(node, buffer, sizeof(buffer) - 1);
+		exfat_bug("node '%s' is dirty", buffer);
+	}
 	while (node->references)
 		exfat_put_node(ef, node);
 }
@@ -544,13 +573,21 @@
 	reset_cache(ef, ef->root);
 }
 
-static void next_entry(struct exfat* ef, const struct exfat_node* parent,
+static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
 		cluster_t* cluster, off64_t* offset)
 {
 	*offset += sizeof(struct exfat_entry);
 	if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
-		/* next cluster cannot be invalid */
+	{
 		*cluster = exfat_next_cluster(ef, parent, *cluster);
+		if (CLUSTER_INVALID(*cluster))
+		{
+			exfat_error("invalid cluster %#x while getting next entry",
+					*cluster);
+			return false;
+		}
+	}
+	return true;
 }
 
 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
@@ -573,7 +610,8 @@
 	cluster = node->entry_cluster;
 	offset = node->entry_offset;
 	meta1_offset = co2o(ef, cluster, offset);
-	next_entry(ef, node->parent, &cluster, &offset);
+	if (!next_entry(ef, node->parent, &cluster, &offset))
+		return -EIO;
 	meta2_offset = co2o(ef, cluster, offset);
 
 	if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
@@ -594,7 +632,7 @@
 	}
 	if (meta2.type != EXFAT_ENTRY_FILE_INFO)
 		exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
-	meta2.size = meta2.real_size = cpu_to_le64(node->size);
+	meta2.size = meta2.valid_size = cpu_to_le64(node->size);
 	meta2.start_cluster = cpu_to_le32(node->start_cluster);
 	meta2.flags = EXFAT_FLAG_ALWAYS1;
 	/* empty files must not be marked as contiguous */
@@ -633,7 +671,8 @@
 		return false;
 	}
 
-	next_entry(ef, node->parent, &cluster, &offset);
+	if (!next_entry(ef, node->parent, &cluster, &offset))
+		return false;
 	entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
 	if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
 	{
@@ -643,7 +682,8 @@
 
 	while (name_entries--)
 	{
-		next_entry(ef, node->parent, &cluster, &offset);
+		if (!next_entry(ef, node->parent, &cluster, &offset))
+			return false;
 		entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
 		if (exfat_pwrite(ef->dev, &entry_type, 1,
 				co2o(ef, cluster, offset)) < 0)
@@ -662,7 +702,6 @@
 	const struct exfat_node* last_node;
 	uint64_t entries = 0;
 	uint64_t new_size;
-	int rc;
 
 	if (!(dir->flags & EXFAT_ATTRIB_DIR))
 		exfat_bug("attempted to shrink a file");
@@ -698,10 +737,7 @@
 		new_size = CLUSTER_SIZE(*ef->sb);
 	if (new_size == dir->size)
 		return 0;
-	rc = exfat_truncate(ef, dir, new_size, true);
-	if (rc != 0)
-		return rc;
-	return 0;
+	return exfat_truncate(ef, dir, new_size, true);
 }
 
 static int delete(struct exfat* ef, struct exfat_node* node)
@@ -719,9 +755,15 @@
 	exfat_update_mtime(parent);
 	tree_detach(node);
 	rc = shrink_directory(ef, parent, deleted_offset);
-	exfat_put_node(ef, parent);
-	/* file clusters will be freed when node reference counter becomes 0 */
 	node->flags |= EXFAT_ATTRIB_UNLINKED;
+	if (rc != 0)
+	{
+		exfat_flush_node(ef, parent);
+		exfat_put_node(ef, parent);
+		return rc;
+	}
+	rc = exfat_flush_node(ef, parent);
+	exfat_put_node(ef, parent);
 	return rc;
 }
 
@@ -734,10 +776,14 @@
 
 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
 {
+	int rc;
+
 	if (!(node->flags & EXFAT_ATTRIB_DIR))
 		return -ENOTDIR;
 	/* check that directory is empty */
-	exfat_cache_directory(ef, node);
+	rc = exfat_cache_directory(ef, node);
+	if (rc != 0)
+		return rc;
 	if (node->child)
 		return -ENOTEMPTY;
 	return delete(ef, node);
@@ -786,7 +832,7 @@
 				return rc;
 			}
 		}
-		if (fetch_next_entry(ef, dir, &it) != 0)
+		if (!fetch_next_entry(ef, dir, &it))
 		{
 			closedir(&it);
 			return -EIO;
@@ -838,7 +884,8 @@
 		exfat_error("failed to write meta1 entry");
 		return -EIO;
 	}
-	next_entry(ef, dir, &cluster, &offset);
+	if (!next_entry(ef, dir, &cluster, &offset))
+		return -EIO;
 	if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
 			co2o(ef, cluster, offset)) < 0)
 	{
@@ -851,7 +898,8 @@
 		memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
 				MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
 				sizeof(le16_t));
-		next_entry(ef, dir, &cluster, &offset);
+		if (!next_entry(ef, dir, &cluster, &offset))
+			return -EIO;
 		if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
 				co2o(ef, cluster, offset)) < 0)
 		{
@@ -895,6 +943,12 @@
 		return rc;
 	}
 	rc = write_entry(ef, dir, name, cluster, offset, attrib);
+	if (rc != 0)
+	{
+		exfat_put_node(ef, dir);
+		return rc;
+	}
+	rc = exfat_flush_node(ef, dir);
 	exfat_put_node(ef, dir);
 	return rc;
 }
@@ -923,6 +977,13 @@
 		exfat_put_node(ef, node);
 		return rc;
 	}
+	rc = exfat_flush_node(ef, node);
+	if (rc != 0)
+	{
+		delete(ef, node);
+		exfat_put_node(ef, node);
+		return rc;
+	}
 	exfat_put_node(ef, node);
 	return 0;
 }
@@ -945,7 +1006,8 @@
 		exfat_error("failed to read meta1 entry on rename");
 		return -EIO;
 	}
-	next_entry(ef, node->parent, &old_cluster, &old_offset);
+	if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
+		return -EIO;
 	if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
 			co2o(ef, old_cluster, old_offset)) < 0)
 	{
@@ -969,7 +1031,8 @@
 		exfat_error("failed to write meta1 entry on rename");
 		return -EIO;
 	}
-	next_entry(ef, dir, &new_cluster, &new_offset);
+	if (!next_entry(ef, dir, &new_cluster, &new_offset))
+		return -EIO;
 	if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
 			co2o(ef, new_cluster, new_offset)) < 0)
 	{
@@ -982,7 +1045,8 @@
 		struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
 		memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
 				EXFAT_ENAME_MAX * sizeof(le16_t));
-		next_entry(ef, dir, &new_cluster, &new_offset);
+		if (!next_entry(ef, dir, &new_cluster, &new_offset))
+			return -EIO;
 		if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
 				co2o(ef, new_cluster, new_offset)) < 0)
 		{
@@ -1076,7 +1140,7 @@
 	rc = rename_entry(ef, dir, node, name, cluster, offset);
 	exfat_put_node(ef, dir);
 	exfat_put_node(ef, node);
-	return 0;
+	return rc;
 }
 
 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
@@ -1128,7 +1192,7 @@
 			return 0;
 		}
 
-		if (fetch_next_entry(ef, ef->root, &it) != 0)
+		if (!fetch_next_entry(ef, ef->root, &it))
 		{
 			closedir(&it);
 			return -EIO;
diff --git a/exfat/libexfat/platform.h b/exfat/libexfat/platform.h
index 320f757..c81e10e 100644
--- a/exfat/libexfat/platform.h
+++ b/exfat/libexfat/platform.h
@@ -3,11 +3,12 @@
 	OS-specific code (libc-specific in fact). Note that systems with the
 	same kernel can use different libc implementations.
 
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Free exFAT implementation.
+	Copyright (C) 2010-2014  Andrew Nayenko
 
-	This program is free software: you can redistribute it and/or modify
+	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
-	the Free Software Foundation, either version 3 of the License, or
+	the Free Software Foundation, either version 2 of the License, or
 	(at your option) any later version.
 
 	This program is distributed in the hope that it will be useful,
@@ -15,8 +16,9 @@
 	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.  If not, see <http://www.gnu.org/licenses/>.
+	You should have received a copy of the GNU General Public License along
+	with this program; if not, write to the Free Software Foundation, Inc.,
+	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #ifndef PLATFORM_H_INCLUDED
@@ -26,54 +28,37 @@
 
 #include <endian.h>
 #include <byteswap.h>
+#define exfat_bswap16(x) bswap_16(x)
+#define exfat_bswap32(x) bswap_32(x)
+#define exfat_bswap64(x) bswap_64(x)
+#define EXFAT_BYTE_ORDER __BYTE_ORDER
+#define EXFAT_LITTLE_ENDIAN __LITTLE_ENDIAN
+#define EXFAT_BIG_ENDIAN __BIG_ENDIAN
 
 #elif defined(__APPLE__)
 
 #include <machine/endian.h>
 #include <libkern/OSByteOrder.h>
-#define bswap_16(x) OSSwapInt16(x)
-#define bswap_32(x) OSSwapInt32(x)
-#define bswap_64(x) OSSwapInt64(x)
-#define __BYTE_ORDER BYTE_ORDER
-#define __LITTLE_ENDIAN LITTLE_ENDIAN
-#define __BIG_ENDIAN BIG_ENDIAN
+#define exfat_bswap16(x) OSSwapInt16(x)
+#define exfat_bswap32(x) OSSwapInt32(x)
+#define exfat_bswap64(x) OSSwapInt64(x)
+#define EXFAT_BYTE_ORDER BYTE_ORDER
+#define EXFAT_LITTLE_ENDIAN LITTLE_ENDIAN
+#define EXFAT_BIG_ENDIAN BIG_ENDIAN
 
-#elif defined(__FreeBSD__) || defined(__DragonFlyBSD__) || defined(__NetBSD__)
+#endif
+//#elif defined(__FreeBSD__) || defined(__DragonFlyBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
 
 #include <sys/endian.h>
-#define bswap_16(x) bswap16(x)
-#define bswap_32(x) bswap32(x)
-#define bswap_64(x) bswap64(x)
-#define __BYTE_ORDER _BYTE_ORDER
-#define __LITTLE_ENDIAN _LITTLE_ENDIAN
-#define __BIG_ENDIAN _BIG_ENDIAN
+#define exfat_bswap16(x) bswap16(x)
+#define exfat_bswap32(x) bswap32(x)
+#define exfat_bswap64(x) bswap64(x)
+#define EXFAT_BYTE_ORDER _BYTE_ORDER
+#define EXFAT_LITTLE_ENDIAN _LITTLE_ENDIAN
+#define EXFAT_BIG_ENDIAN _BIG_ENDIAN
 
-#elif defined(__OpenBSD__)
-
-#include <machine/endian.h>
-#define bswap_16(x) swap16(x)
-#define bswap_32(x) swap32(x)
-#define bswap_64(x) swap64(x)
-#define __BYTE_ORDER _BYTE_ORDER
-#define __LITTLE_ENDIAN _LITTLE_ENDIAN
-#define __BIG_ENDIAN _BIG_ENDIAN
-
-#elif defined(__sun)
-
-#include <sys/byteorder.h>
-#define bswap_16(x) BSWAP_16(x)
-#define bswap_32(x) BSWAP_32(x)
-#define bswap_64(x) BSWAP_64(x)
-#define __LITTLE_ENDIAN 1234
-#define __BIG_ENDIAN 4321
-#ifdef _LITTLE_ENDIAN
-#define __BYTE_ORDER __LITTLE_ENDIAN
-#else
-#define __BYTE_ORDER __BIG_ENDIAN
-#endif
-
+/*
 #else 
 #error Unknown platform
-#endif
-
+*/
 #endif /* ifndef PLATFORM_H_INCLUDED */
diff --git a/exfat/libexfat/time.c b/exfat/libexfat/time.c
index 43c5fa4..b264a99 100644
--- a/exfat/libexfat/time.c
+++ b/exfat/libexfat/time.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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/exfat/libexfat/utf.c b/exfat/libexfat/utf.c
index 3b7e26b..70fa856 100644
--- a/exfat/libexfat/utf.c
+++ b/exfat/libexfat/utf.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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/exfat/libexfat/utils.c b/exfat/libexfat/utils.c
index ef87692..c0ecd56 100644
--- a/exfat/libexfat/utils.c
+++ b/exfat/libexfat/utils.c
@@ -3,7 +3,7 @@
 	exFAT file system implementation library.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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/exfat/libexfat/version.h b/exfat/libexfat/version.h
index 062fa04..20c1147 100644
--- a/exfat/libexfat/version.h
+++ b/exfat/libexfat/version.h
@@ -3,7 +3,7 @@
 	Version constants.
 
 	Free exFAT implementation.
-	Copyright (C) 2010-2013  Andrew Nayenko
+	Copyright (C) 2010-2014  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
@@ -24,7 +24,7 @@
 #define VERSION_H_INCLUDED
 
 #define EXFAT_VERSION_MAJOR 1
-#define EXFAT_VERSION_MINOR 0
+#define EXFAT_VERSION_MINOR 1
 #define EXFAT_VERSION_PATCH 0
 
 #endif /* ifndef VERSION_H_INCLUDED */
diff --git a/exfat/mkfs/cbm.c b/exfat/mkfs/cbm.c
index 0110b40..189ae32 100644
--- a/exfat/mkfs/cbm.c
+++ b/exfat/mkfs/cbm.c
@@ -3,7 +3,7 @@
 	Clusters Bitmap creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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,6 +21,7 @@
 */
 
 #include <limits.h>
+#include <string.h>
 #include "cbm.h"
 #include "fat.h"
 #include "uct.h"
@@ -45,22 +46,23 @@
 			DIV_ROUND_UP(uct.get_size(), get_cluster_size()) +
 			DIV_ROUND_UP(rootdir.get_size(), get_cluster_size());
 	size_t bitmap_size = DIV_ROUND_UP(allocated_clusters, CHAR_BIT);
-	uint8_t* bitmap = malloc(bitmap_size);
+	bitmap_t* bitmap = malloc(BMAP_SIZE(bitmap_size));
 	size_t i;
 
 	if (bitmap == NULL)
 	{
-		exfat_error("failed to allocate bitmap of %zu bytes", bitmap_size);
+		exfat_error("failed to allocate bitmap of %zu bytes",
+				BMAP_SIZE(bitmap_size));
 		return 1;
 	}
+	memset(bitmap, 0, BMAP_SIZE(bitmap_size));
 
 	for (i = 0; i < bitmap_size * CHAR_BIT; i++)
 		if (i < allocated_clusters)
 			BMAP_SET(bitmap, i);
-		else
-			BMAP_CLR(bitmap, i);
 	if (exfat_write(dev, bitmap, bitmap_size) < 0)
 	{
+		free(bitmap);
 		exfat_error("failed to write bitmap of %zu bytes", bitmap_size);
 		return 1;
 	}
diff --git a/exfat/mkfs/cbm.h b/exfat/mkfs/cbm.h
index b9d2850..f959124 100644
--- a/exfat/mkfs/cbm.h
+++ b/exfat/mkfs/cbm.h
@@ -3,7 +3,7 @@
 	Clusters Bitmap creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/fat.c b/exfat/mkfs/fat.c
index 9ed9022..25d37a2 100644
--- a/exfat/mkfs/fat.c
+++ b/exfat/mkfs/fat.c
@@ -3,7 +3,7 @@
 	File Allocation Table creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/fat.h b/exfat/mkfs/fat.h
index 1327f0a..bd30fe3 100644
--- a/exfat/mkfs/fat.h
+++ b/exfat/mkfs/fat.h
@@ -3,7 +3,7 @@
 	File Allocation Table creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/main.c b/exfat/mkfs/main.c
index 3c3c382..87f25db 100644
--- a/exfat/mkfs/main.c
+++ b/exfat/mkfs/main.c
@@ -3,7 +3,7 @@
 	Creates exFAT file system.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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
@@ -224,12 +224,12 @@
 			spc_bits = logarithm2(atoi(optarg));
 			if (spc_bits < 0)
 			{
-				exfat_error("invalid option value: `%s'", optarg);
+				exfat_error("invalid option value: '%s'", optarg);
 				return 1;
 			}
 			break;
 		case 'V':
-			puts("Copyright (C) 2011-2013  Andrew Nayenko");
+			puts("Copyright (C) 2011-2014  Andrew Nayenko");
 			return 0;
 		default:
 			usage(argv[0]);
diff --git a/exfat/mkfs/mkexfat.c b/exfat/mkfs/mkexfat.c
index c5c86d9..df6391a 100644
--- a/exfat/mkfs/mkexfat.c
+++ b/exfat/mkfs/mkexfat.c
@@ -3,7 +3,7 @@
 	FS creation engine.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/mkexfat.h b/exfat/mkfs/mkexfat.h
index d0685af..a9e4948 100644
--- a/exfat/mkfs/mkexfat.h
+++ b/exfat/mkfs/mkexfat.h
@@ -3,7 +3,7 @@
 	FS creation engine.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/mkexfatfs.8 b/exfat/mkfs/mkexfatfs.8
index 5f6ba3d..8f5e53b 100644
--- a/exfat/mkfs/mkexfatfs.8
+++ b/exfat/mkfs/mkexfatfs.8
@@ -1,4 +1,4 @@
-.\" Copyright (C) 2011  Andrew Nayenko
+.\" Copyright (C) 2011-2014  Andrew Nayenko
 .\"
 .TH MKEXFATFS 8 "January 2011"
 .SH NAME
@@ -31,7 +31,10 @@
 .B mkexfatfs
 creates an exFAT file system on a block device.
 .I device
-is a special file corresponding to the device.
+is a special file corresponding to the partition on the device. Note that if
+this is an MBR partition then the file system type should be set to 0x07
+(NTFS/exFAT) otherwise other operating systems may refuse to mount the
+file system.
 
 .SH OPTIONS
 Command line options available:
@@ -65,4 +68,4 @@
 Andrew Nayenko
 
 .SH SEE ALSO
-.BR mkfs (8)
+.BR mkfs (8), fdisk (8)
diff --git a/exfat/mkfs/rootdir.c b/exfat/mkfs/rootdir.c
index 49d3643..f453131 100644
--- a/exfat/mkfs/rootdir.c
+++ b/exfat/mkfs/rootdir.c
@@ -3,7 +3,7 @@
 	Root directory creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/rootdir.h b/exfat/mkfs/rootdir.h
index 2d1d345..ddf5f41 100644
--- a/exfat/mkfs/rootdir.h
+++ b/exfat/mkfs/rootdir.h
@@ -3,7 +3,7 @@
 	Root directory creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/uct.c b/exfat/mkfs/uct.c
index 5f17323..9129403 100644
--- a/exfat/mkfs/uct.c
+++ b/exfat/mkfs/uct.c
@@ -3,7 +3,7 @@
 	Upper Case Table creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/uct.h b/exfat/mkfs/uct.h
index ef33392..6f276de 100644
--- a/exfat/mkfs/uct.h
+++ b/exfat/mkfs/uct.h
@@ -3,7 +3,7 @@
 	Upper Case Table creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/uctc.c b/exfat/mkfs/uctc.c
index 1ee8efe..b5aa17d 100644
--- a/exfat/mkfs/uctc.c
+++ b/exfat/mkfs/uctc.c
@@ -3,7 +3,7 @@
 	Upper Case Table contents.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/uctc.h b/exfat/mkfs/uctc.h
index 3e7dee0..c492ec2 100644
--- a/exfat/mkfs/uctc.h
+++ b/exfat/mkfs/uctc.h
@@ -3,7 +3,7 @@
 	Upper Case Table declaration.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/vbr.c b/exfat/mkfs/vbr.c
index bbf1304..b3889e5 100644
--- a/exfat/mkfs/vbr.c
+++ b/exfat/mkfs/vbr.c
@@ -3,7 +3,7 @@
 	Volume Boot Record creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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/exfat/mkfs/vbr.h b/exfat/mkfs/vbr.h
index fec88bc..075938f 100644
--- a/exfat/mkfs/vbr.h
+++ b/exfat/mkfs/vbr.h
@@ -3,7 +3,7 @@
 	Volume Boot Record creation code.
 
 	Free exFAT implementation.
-	Copyright (C) 2011-2013  Andrew Nayenko
+	Copyright (C) 2011-2014  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