update exfat to latest commits for io issues
diff --git a/exfat/libexfat/COPYING b/exfat/libexfat/COPYING
new file mode 100644
index 0000000..d159169
--- /dev/null
+++ b/exfat/libexfat/COPYING
@@ -0,0 +1,339 @@
+                    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 6792f37..10746e1 100644
--- a/exfat/libexfat/byteorder.h
+++ b/exfat/libexfat/byteorder.h
@@ -2,11 +2,12 @@
 	byteorder.h (12.01.10)
 	Endianness stuff. exFAT uses little-endian byte order.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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 BYTEORDER_H_INCLUDED
@@ -78,7 +80,6 @@
 #error No byte order macros available for your platform
 #endif
 
-#define __BYTE_ORDER __LITTLE_ENDIAN
 typedef struct { uint16_t __u16; } le16_t;
 typedef struct { uint32_t __u32; } le32_t;
 typedef struct { uint64_t __u64; } le64_t;
diff --git a/exfat/libexfat/cluster.c b/exfat/libexfat/cluster.c
index d10a5df..2215636 100644
--- a/exfat/libexfat/cluster.c
+++ b/exfat/libexfat/cluster.c
@@ -2,11 +2,12 @@
 	cluster.c (03.09.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,13 +15,15 @@
 	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.
 */
 
 #include "exfat.h"
 #include <errno.h>
 #include <string.h>
+#include <inttypes.h>
 
 /*
  * Sector to absolute offset.
@@ -80,7 +83,9 @@
 		return cluster + 1;
 	fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
 		+ cluster * sizeof(cluster_t);
-	exfat_pread(ef->dev, &next, sizeof(next), fat_offset);
+	/* 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 le32_to_cpu(next);
 }
 
@@ -106,18 +111,22 @@
 	return node->fptr_cluster;
 }
 
-static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
+static cluster_t find_bit_and_set(bitmap_t* bitmap, size_t start, size_t end)
 {
-	const size_t start_index = start / 8;
-	const size_t end_index = DIV_ROUND_UP(end, 8);
+	const size_t start_index = start / sizeof(bitmap_t) / 8;
+	const size_t end_index = DIV_ROUND_UP(end, sizeof(bitmap_t) * 8);
 	size_t i;
+	size_t start_bitindex;
+	size_t end_bitindex;
 	size_t c;
 
 	for (i = start_index; i < end_index; i++)
 	{
-		if (bitmap[i] == 0xff)
+		if (bitmap[i] == ~((bitmap_t) 0))
 			continue;
-		for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
+		start_bitindex = MAX(i * sizeof(bitmap_t) * 8, start);
+		end_bitindex = MIN((i + 1) * sizeof(bitmap_t) * 8, end);
+		for (c = start_bitindex; c < end_bitindex; c++)
 			if (BMAP_GET(bitmap, c) == 0)
 			{
 				BMAP_SET(bitmap, c);
@@ -127,25 +136,40 @@
 	return EXFAT_CLUSTER_END;
 }
 
-void exfat_flush_cmap(struct exfat* ef)
+int exfat_flush(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 = false;
+	if (ef->cmap.dirty)
+	{
+		if (exfat_pwrite(ef->dev, ef->cmap.chunk,
+				BMAP_SIZE(ef->cmap.chunk_size),
+				exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
+		{
+			exfat_error("failed to write clusters bitmap");
+			return -EIO;
+		}
+		ef->cmap.dirty = false;
+	}
+	return 0;
 }
 
-static void set_next_cluster(const struct exfat* ef, bool contiguous,
+static bool set_next_cluster(const struct exfat* ef, bool contiguous,
 		cluster_t current, cluster_t next)
 {
 	off64_t fat_offset;
 	le32_t next_le32;
 
 	if (contiguous)
-		return;
+		return true;
 	fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
 		+ current * sizeof(cluster_t);
 	next_le32 = cpu_to_le32(next);
-	exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset);
+	if (exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset) < 0)
+	{
+		exfat_error("failed to write the next cluster %#x after %#x", next,
+				current);
+		return false;
+	}
+	return true;
 }
 
 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
@@ -181,13 +205,15 @@
 	ef->cmap.dirty = true;
 }
 
-static void make_noncontiguous(const struct exfat* ef, cluster_t first,
+static bool make_noncontiguous(const struct exfat* ef, cluster_t first,
 		cluster_t last)
 {
 	cluster_t c;
 
 	for (c = first; c < last; c++)
-		set_next_cluster(ef, false, c, c + 1);
+		if (!set_next_cluster(ef, false, c, c + 1))
+			return false;
+	return true;
 }
 
 static int shrink_file(struct exfat* ef, struct exfat_node* node,
@@ -241,16 +267,20 @@
 		{
 			/* it's a pity, but we are not able to keep the file contiguous
 			   anymore */
-			make_noncontiguous(ef, node->start_cluster, previous);
+			if (!make_noncontiguous(ef, node->start_cluster, previous))
+				return -EIO;
 			node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
 			node->flags |= EXFAT_ATTRIB_DIRTY;
 		}
-		set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
+		if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next))
+			return -EIO;
 		previous = next;
 		allocated++;
 	}
 
-	set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
+	if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
+			EXFAT_CLUSTER_END))
+		return -EIO;
 	return 0;
 }
 
@@ -278,7 +308,9 @@
 			return -EIO;
 		}
 		previous = exfat_next_cluster(ef, node, last);
-		set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
+		if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), last,
+				EXFAT_CLUSTER_END))
+			return -EIO;
 	}
 	else
 	{
@@ -298,17 +330,23 @@
 			return -EIO;
 		}
 		next = exfat_next_cluster(ef, node, previous);
-		set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
-				EXFAT_CLUSTER_FREE);
+		if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
+				EXFAT_CLUSTER_FREE))
+			return -EIO;
 		free_cluster(ef, previous);
 		previous = next;
 	}
 	return 0;
 }
 
-static void erase_raw(struct exfat* ef, size_t size, off64_t offset)
+static bool erase_raw(struct exfat* ef, size_t size, off64_t offset)
 {
-	exfat_pwrite(ef->dev, ef->zero_cluster, size, offset);
+	if (exfat_pwrite(ef->dev, ef->zero_cluster, size, offset) < 0)
+	{
+		exfat_error("failed to erase %zu bytes at %"PRId64, size, offset);
+		return false;
+	}
+	return true;
 }
 
 static int erase_range(struct exfat* ef, struct exfat_node* node,
@@ -329,8 +367,9 @@
 		return -EIO;
 	}
 	/* erase from the beginning to the closest cluster boundary */
-	erase_raw(ef, MIN(cluster_boundary, end) - begin,
-			exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb));
+	if (!erase_raw(ef, MIN(cluster_boundary, end) - begin,
+			exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb)))
+		return -EIO;
 	/* erase whole clusters */
 	while (cluster_boundary < end)
 	{
@@ -338,7 +377,8 @@
 		/* the cluster cannot be invalid because we have just allocated it */
 		if (CLUSTER_INVALID(cluster))
 			exfat_bug("invalid cluster 0x%x after allocation", cluster);
-		erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
+		if (!erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster)))
+			return -EIO;
 		cluster_boundary += CLUSTER_SIZE(*ef->sb);
 	}
 	return 0;
diff --git a/exfat/libexfat/compiler.h b/exfat/libexfat/compiler.h
new file mode 100644
index 0000000..6edd8ec
--- /dev/null
+++ b/exfat/libexfat/compiler.h
@@ -0,0 +1,62 @@
+/*
+	compiler.h (09.06.13)
+	Compiler-specific definitions. Note that unknown compiler is not a
+	showstopper.
+
+	Free exFAT implementation.
+	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
+	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.
+*/
+
+#ifndef COMPILER_H_INCLUDED
+#define COMPILER_H_INCLUDED
+
+#if defined(__clang__)
+
+#define PRINTF __attribute__((format(printf, 1, 2)))
+#define NORETURN __attribute__((noreturn))
+#define PACKED __attribute__((packed))
+#if __has_extension(c_static_assert)
+#define USE_C11_STATIC_ASSERT
+#endif
+
+#elif defined(__GNUC__)
+
+#define PRINTF __attribute__((format(printf, 1, 2)))
+#define NORETURN __attribute__((noreturn))
+#define PACKED __attribute__((packed))
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#define USE_C11_STATIC_ASSERT
+#endif
+
+#else
+
+#define PRINTF
+#define NORETURN
+#define PACKED
+
+#endif
+
+#ifdef USE_C11_STATIC_ASSERT
+#define STATIC_ASSERT(cond) _Static_assert(cond, #cond)
+#else
+#define CONCAT2(a, b) a ## b
+#define CONCAT1(a, b) CONCAT2(a, b)
+#define STATIC_ASSERT(cond) \
+	extern void CONCAT1(static_assert, __LINE__)(int x[(cond) ? 1 : -1])
+#endif
+
+#endif /* ifndef COMPILER_H_INCLUDED */
diff --git a/exfat/libexfat/exfat.h b/exfat/libexfat/exfat.h
index a6a9f70..ff58e84 100644
--- a/exfat/libexfat/exfat.h
+++ b/exfat/libexfat/exfat.h
@@ -3,11 +3,12 @@
 	Definitions of structures and constants used in exFAT file system
 	implementation.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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 EXFAT_H_INCLUDED
@@ -28,6 +30,7 @@
 #include <stdbool.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include "compiler.h"
 #include "exfatfs.h"
 #include "version.h"
 
@@ -48,12 +51,16 @@
 #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)))
 #define BMAP_GET(bitmap, index) \
-	(((uint8_t*) bitmap)[(index) / 8] & (1u << ((index) % 8)))
+	((bitmap)[BMAP_BLOCK(index)] & BMAP_MASK(index))
 #define BMAP_SET(bitmap, index) \
-	((uint8_t*) bitmap)[(index) / 8] |= (1u << ((index) % 8))
+	((bitmap)[BMAP_BLOCK(index)] |= BMAP_MASK(index))
 #define BMAP_CLR(bitmap, index) \
-	((uint8_t*) bitmap)[(index) / 8] &= ~(1u << ((index) % 8))
+	((bitmap)[BMAP_BLOCK(index)] &= ~BMAP_MASK(index))
 
 struct exfat_node
 {
@@ -94,7 +101,7 @@
 	{
 		cluster_t start_cluster;
 		uint32_t size;				/* in bits */
-		uint8_t* chunk;
+		bitmap_t* chunk;
 		uint32_t chunk_size;		/* in bits */
 		bool dirty;
 	}
@@ -123,14 +130,10 @@
 
 extern int exfat_errors;
 
-void exfat_bug(const char* format, ...)
-	__attribute__((format(printf, 1, 2), noreturn));
-void exfat_error(const char* format, ...)
-	__attribute__((format(printf, 1, 2)));
-void exfat_warn(const char* format, ...)
-	__attribute__((format(printf, 1, 2)));
-void exfat_debug(const char* format, ...)
-	__attribute__((format(printf, 1, 2)));
+void exfat_bug(const char* format, ...) PRINTF NORETURN;
+void exfat_error(const char* format, ...) PRINTF;
+void exfat_warn(const char* format, ...) PRINTF;
+void exfat_debug(const char* format, ...) PRINTF;
 
 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode);
 int exfat_close(struct exfat_dev* dev);
@@ -140,9 +143,9 @@
 off64_t exfat_seek(struct exfat_dev* dev, off64_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);
-void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
+ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
 		off64_t offset);
-void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
+ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
 		off64_t offset);
 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
 		void* buffer, size_t size, off64_t offset);
@@ -163,7 +166,7 @@
 		const struct exfat_node* node, cluster_t cluster);
 cluster_t exfat_advance_cluster(const struct exfat* ef,
 		struct exfat_node* node, uint32_t count);
-void exfat_flush_cmap(struct exfat* ef);
+int exfat_flush(struct exfat* ef);
 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
 		bool erase);
 uint32_t exfat_count_free_clusters(const struct exfat* ef);
@@ -193,7 +196,7 @@
 void exfat_put_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);
-void exfat_flush_node(struct exfat* ef, struct exfat_node* node);
+int exfat_flush_node(struct exfat* ef, struct exfat_node* node);
 int exfat_unlink(struct exfat* ef, struct exfat_node* node);
 int exfat_rmdir(struct exfat* ef, struct exfat_node* node);
 int exfat_mknod(struct exfat* ef, const char* path);
diff --git a/exfat/libexfat/exfatfs.h b/exfat/libexfat/exfatfs.h
index 6c58a84..a436daa 100644
--- a/exfat/libexfat/exfatfs.h
+++ b/exfat/libexfat/exfatfs.h
@@ -2,11 +2,12 @@
 	exfatfs.h (29.08.09)
 	Definitions of structures and constants used in exFAT file system.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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 EXFATFS_H_INCLUDED
@@ -62,7 +64,8 @@
 	uint8_t __unused2[397];			/* 0x71 always 0 */
 	le16_t boot_signature;			/* the value of 0xAA55 */
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_super_block) == 512);
 
 #define EXFAT_ENTRY_VALID     0x80
 #define EXFAT_ENTRY_CONTINUED 0x40
@@ -79,7 +82,8 @@
 	uint8_t type;					/* any of EXFAT_ENTRY_xxx */
 	uint8_t data[31];
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry) == 32);
 
 #define EXFAT_ENAME_MAX 15
 
@@ -90,7 +94,8 @@
 	le32_t start_cluster;
 	le64_t size;					/* in bytes */
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry_bitmap) == 32);
 
 struct exfat_entry_upcase			/* upper case translation table */
 {
@@ -101,7 +106,8 @@
 	le32_t start_cluster;
 	le64_t size;					/* in bytes */
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry_upcase) == 32);
 
 struct exfat_entry_label			/* volume label */
 {
@@ -109,7 +115,8 @@
 	uint8_t length;					/* number of characters */
 	le16_t name[EXFAT_ENAME_MAX];	/* in UTF-16LE */
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry_label) == 32);
 
 #define EXFAT_ATTRIB_RO     0x01
 #define EXFAT_ATTRIB_HIDDEN 0x02
@@ -132,7 +139,8 @@
 	uint8_t mtime_cs;				/* latest modification time in cs */
 	uint8_t __unknown2[10];
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry_meta1) == 32);
 
 #define EXFAT_FLAG_ALWAYS1		(1u << 0)
 #define EXFAT_FLAG_CONTIGUOUS	(1u << 1)
@@ -150,7 +158,8 @@
 	le32_t start_cluster;
 	le64_t size;					/* in bytes, equals to real_size */
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry_meta2) == 32);
 
 struct exfat_entry_name				/* file or directory name */
 {
@@ -158,6 +167,7 @@
 	uint8_t __unknown;
 	le16_t name[EXFAT_ENAME_MAX];	/* in UTF-16LE */
 }
-__attribute__((__packed__));
+PACKED;
+STATIC_ASSERT(sizeof(struct exfat_entry_name) == 32);
 
 #endif /* ifndef EXFATFS_H_INCLUDED */
diff --git a/exfat/libexfat/io.c b/exfat/libexfat/io.c
index 7f79d97..306c5dd 100644
--- a/exfat/libexfat/io.c
+++ b/exfat/libexfat/io.c
@@ -2,11 +2,12 @@
 	io.c (02.09.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
@@ -280,28 +282,24 @@
 #endif
 }
 
-void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
+ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
 		off64_t offset)
 {
 #ifdef USE_UBLIO
-	if (ublio_pread(dev->ufh, buffer, size, offset) != size)
+	return ublio_pread(dev->ufh, buffer, size, offset);
 #else
-	if (pread64(dev->fd, buffer, size, offset) != size)
+	return pread64(dev->fd, buffer, size, offset);
 #endif
-		exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
-				(uint64_t) offset);
 }
 
-void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
+ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
 		off64_t offset)
 {
 #ifdef USE_UBLIO
-	if (ublio_pwrite(dev->ufh, buffer, size, offset) != size)
+	return ublio_pwrite(dev->ufh, buffer, size, offset);
 #else
-	if (pwrite64(dev->fd, buffer, size, offset) != size)
+	return pwrite64(dev->fd, buffer, size, offset);
 #endif
-		exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
-				(uint64_t) offset);
 }
 
 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
@@ -333,7 +331,12 @@
 			return -1;
 		}
 		lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
-		exfat_pread(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
+		if (exfat_pread(ef->dev, bufp, lsize,
+					exfat_c2o(ef, cluster) + loffset) < 0)
+		{
+			exfat_error("failed to read cluster %#x", cluster);
+			return -1;
+		}
 		bufp += lsize;
 		loffset = 0;
 		remainder -= lsize;
@@ -377,7 +380,12 @@
 			return -1;
 		}
 		lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
-		exfat_pwrite(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
+		if (exfat_pwrite(ef->dev, bufp, lsize,
+				exfat_c2o(ef, cluster) + loffset) < 0)
+		{
+			exfat_error("failed to write cluster %#x", cluster);
+			return -1;
+		}
 		bufp += lsize;
 		loffset = 0;
 		remainder -= lsize;
diff --git a/exfat/libexfat/log.c b/exfat/libexfat/log.c
index 034cc09..7e541aa 100644
--- a/exfat/libexfat/log.c
+++ b/exfat/libexfat/log.c
@@ -2,11 +2,12 @@
 	log.c (02.09.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
diff --git a/exfat/libexfat/lookup.c b/exfat/libexfat/lookup.c
index 2e1aa81..00cd533 100644
--- a/exfat/libexfat/lookup.c
+++ b/exfat/libexfat/lookup.c
@@ -2,11 +2,12 @@
 	lookup.c (02.09.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
diff --git a/exfat/libexfat/mount.c b/exfat/libexfat/mount.c
index 4daf126..928c561 100644
--- a/exfat/libexfat/mount.c
+++ b/exfat/libexfat/mount.c
@@ -2,11 +2,12 @@
 	mount.c (22.10.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
@@ -95,15 +97,27 @@
 	uint32_t vbr_checksum;
 	int i;
 
-	exfat_pread(dev, sector, sector_size, 0);
+	if (exfat_pread(dev, sector, sector_size, 0) < 0)
+	{
+		exfat_error("failed to read boot sector");
+		return 1;
+	}
 	vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
 	for (i = 1; i < 11; i++)
 	{
-		exfat_pread(dev, sector, sector_size, i * sector_size);
+		if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
+		{
+			exfat_error("failed to read VBR sector");
+			return 1;
+		}
 		vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
 				vbr_checksum);
 	}
-	exfat_pread(dev, sector, sector_size, i * sector_size);
+	if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
+	{
+		exfat_error("failed to read VBR checksum sector");
+		return 1;
+	}
 	for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
 		if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
 		{
@@ -116,7 +130,11 @@
 
 static int commit_super_block(const struct exfat* ef)
 {
-	exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0);
+	if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
+	{
+		exfat_error("failed to write super block");
+		return 1;
+	}
 	return exfat_fsync(ef->dev);
 }
 
@@ -169,7 +187,13 @@
 	}
 	memset(ef->sb, 0, sizeof(struct exfat_super_block));
 
-	exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0);
+	if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
+	{
+		exfat_close(ef->dev);
+		free(ef->sb);
+		exfat_error("failed to read boot sector");
+		return -EIO;
+	}
 	if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
 	{
 		exfat_close(ef->dev);
diff --git a/exfat/libexfat/node.c b/exfat/libexfat/node.c
index f89b573..56205fc 100644
--- a/exfat/libexfat/node.c
+++ b/exfat/libexfat/node.c
@@ -2,11 +2,12 @@
 	node.c (09.10.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
@@ -51,16 +53,18 @@
 
 	if (node->references == 0)
 	{
-		if (node->flags & EXFAT_ATTRIB_DIRTY)
-			exfat_flush_node(ef, node);
+		/* 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);
 		}
-		if (ef->cmap.dirty)
-			exfat_flush_cmap(ef);
+		/* FIXME handle I/O error */
+		if (exfat_flush(ef) != 0)
+			exfat_bug("flush failed");
 	}
 }
 
@@ -86,8 +90,12 @@
 		exfat_error("out of memory");
 		return -ENOMEM;
 	}
-	exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
-			exfat_c2o(ef, it->cluster));
+	if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
+			exfat_c2o(ef, it->cluster)) < 0)
+	{
+		exfat_error("failed to read directory cluster %#x", it->cluster);
+		return -EIO;
+	}
 	return 0;
 }
 
@@ -119,8 +127,13 @@
 					it->cluster);
 			return 1;
 		}
-		exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
-				exfat_c2o(ef, it->cluster));
+		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 0;
 }
@@ -218,6 +231,12 @@
 				exfat_error("too few continuations (%hhu)", continuations);
 				goto error;
 			}
+			if (continuations > 1 +
+					DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
+			{
+				exfat_error("too many continuations (%hhu)", continuations);
+				goto error;
+			}
 			reference_checksum = le16_to_cpu(meta1->checksum);
 			actual_checksum = exfat_start_checksum(meta1);
 			*node = allocate_node();
@@ -276,7 +295,10 @@
 			file_name = (const struct exfat_entry_name*) entry;
 			actual_checksum = exfat_add_checksum(entry, actual_checksum);
 
-			memcpy(namep, file_name->name, EXFAT_ENAME_MAX * sizeof(le16_t));
+			memcpy(namep, file_name->name,
+					MIN(EXFAT_ENAME_MAX,
+						((*node)->name + EXFAT_NAME_MAX - namep)) *
+					sizeof(le16_t));
 			namep += EXFAT_ENAME_MAX;
 			if (--continuations == 0)
 			{
@@ -344,8 +366,15 @@
 			}
 			ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);
 
-			exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
-					exfat_c2o(ef, le32_to_cpu(upcase->start_cluster)));
+			if (exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size),
+					exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
+			{
+				exfat_error("failed to read upper case table "
+						"(%"PRIu64" bytes starting at cluster %#x)",
+						le64_to_cpu(upcase->size),
+						le32_to_cpu(upcase->start_cluster));
+				goto error;
+			}
 			break;
 
 		case EXFAT_ENTRY_BITMAP:
@@ -359,16 +388,17 @@
 			}
 			ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
 				EXFAT_FIRST_DATA_CLUSTER;
-			if (le64_to_cpu(bitmap->size) < (ef->cmap.size + 7) / 8)
+			if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
 			{
 				exfat_error("invalid clusters bitmap size: %"PRIu64
 						" (expected at least %u)",
-						le64_to_cpu(bitmap->size), (ef->cmap.size + 7) / 8);
+						le64_to_cpu(bitmap->size),
+						DIV_ROUND_UP(ef->cmap.size, 8));
 				goto error;
 			}
 			/* 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));
+			ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
 			if (ef->cmap.chunk == NULL)
 			{
 				exfat_error("failed to allocate clusters bitmap chunk "
@@ -377,8 +407,15 @@
 				goto error;
 			}
 
-			exfat_pread(ef->dev, ef->cmap.chunk, le64_to_cpu(bitmap->size),
-					exfat_c2o(ef, ef->cmap.start_cluster));
+			if (exfat_pread(ef->dev, ef->cmap.chunk,
+					BMAP_SIZE(ef->cmap.chunk_size),
+					exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
+			{
+				exfat_error("failed to read clusters bitmap "
+						"(%"PRIu64" bytes starting at cluster %#x)",
+						le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
+				goto error;
+			}
 			break;
 
 		case EXFAT_ENTRY_LABEL:
@@ -516,7 +553,7 @@
 		*cluster = exfat_next_cluster(ef, parent, *cluster);
 }
 
-void exfat_flush_node(struct exfat* ef, struct exfat_node* node)
+int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
 {
 	cluster_t cluster;
 	off64_t offset;
@@ -524,11 +561,14 @@
 	struct exfat_entry_meta1 meta1;
 	struct exfat_entry_meta2 meta2;
 
+	if (!(node->flags & EXFAT_ATTRIB_DIRTY))
+		return 0; /* no need to flush */
+
 	if (ef->ro)
 		exfat_bug("unable to flush node to read-only FS");
 
 	if (node->parent == NULL)
-		return; /* do not flush unlinked node */
+		return 0; /* do not flush unlinked node */
 
 	cluster = node->entry_cluster;
 	offset = node->entry_offset;
@@ -536,14 +576,22 @@
 	next_entry(ef, node->parent, &cluster, &offset);
 	meta2_offset = co2o(ef, cluster, offset);
 
-	exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset);
+	if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
+	{
+		exfat_error("failed to read meta1 entry on flush");
+		return -EIO;
+	}
 	if (meta1.type != EXFAT_ENTRY_FILE)
 		exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
 	meta1.attrib = cpu_to_le16(node->flags);
 	exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
 	exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
 
-	exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset);
+	if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
+	{
+		exfat_error("failed to read meta2 entry on flush");
+		return -EIO;
+	}
 	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);
@@ -556,13 +604,22 @@
 
 	meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
 
-	exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset);
-	exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset);
+	if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
+	{
+		exfat_error("failed to write meta1 entry on flush");
+		return -EIO;
+	}
+	if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
+	{
+		exfat_error("failed to write meta2 entry on flush");
+		return -EIO;
+	}
 
 	node->flags &= ~EXFAT_ATTRIB_DIRTY;
+	return 0;
 }
 
-static void erase_entry(struct exfat* ef, struct exfat_node* node)
+static bool erase_entry(struct exfat* ef, struct exfat_node* node)
 {
 	cluster_t cluster = node->entry_cluster;
 	off64_t offset = node->entry_offset;
@@ -570,18 +627,32 @@
 	uint8_t entry_type;
 
 	entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
-	exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset));
+	if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
+	{
+		exfat_error("failed to erase meta1 entry");
+		return false;
+	}
 
 	next_entry(ef, node->parent, &cluster, &offset);
 	entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
-	exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset));
+	if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
+	{
+		exfat_error("failed to erase meta2 entry");
+		return false;
+	}
 
 	while (name_entries--)
 	{
 		next_entry(ef, node->parent, &cluster, &offset);
 		entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
-		exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset));
+		if (exfat_pwrite(ef->dev, &entry_type, 1,
+				co2o(ef, cluster, offset)) < 0)
+		{
+			exfat_error("failed to erase name entry");
+			return false;
+		}
 	}
+	return true;
 }
 
 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
@@ -640,7 +711,11 @@
 	int rc;
 
 	exfat_get_node(parent);
-	erase_entry(ef, node);
+	if (!erase_entry(ef, node))
+	{
+		exfat_put_node(ef, parent);
+		return -EIO;
+	}
 	exfat_update_mtime(parent);
 	tree_detach(node);
 	rc = shrink_directory(ef, parent, deleted_offset);
@@ -757,17 +832,32 @@
 
 	meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
 
-	exfat_pwrite(ef->dev, &meta1, sizeof(meta1), co2o(ef, cluster, offset));
+	if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
+			co2o(ef, cluster, offset)) < 0)
+	{
+		exfat_error("failed to write meta1 entry");
+		return -EIO;
+	}
 	next_entry(ef, dir, &cluster, &offset);
-	exfat_pwrite(ef->dev, &meta2, sizeof(meta2), co2o(ef, cluster, offset));
+	if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
+			co2o(ef, cluster, offset)) < 0)
+	{
+		exfat_error("failed to write meta2 entry");
+		return -EIO;
+	}
 	for (i = 0; i < name_entries; i++)
 	{
 		struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
 		memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
-				EXFAT_ENAME_MAX * sizeof(le16_t));
+				MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
+				sizeof(le16_t));
 		next_entry(ef, dir, &cluster, &offset);
-		exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
-				co2o(ef, cluster, offset));
+		if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
+				co2o(ef, cluster, offset)) < 0)
+		{
+			exfat_error("failed to write name entry");
+			return -EIO;
+		}
 	}
 
 	init_node_meta1(node, &meta1);
@@ -837,7 +927,7 @@
 	return 0;
 }
 
-static void rename_entry(struct exfat* ef, struct exfat_node* dir,
+static int rename_entry(struct exfat* ef, struct exfat_node* dir,
 		struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
 		off64_t new_offset)
 {
@@ -849,26 +939,43 @@
 	const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
 	int i;
 
-	exfat_pread(ef->dev, &meta1, sizeof(meta1),
-			co2o(ef, old_cluster, old_offset));
+	if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
+			co2o(ef, old_cluster, old_offset)) < 0)
+	{
+		exfat_error("failed to read meta1 entry on rename");
+		return -EIO;
+	}
 	next_entry(ef, node->parent, &old_cluster, &old_offset);
-	exfat_pread(ef->dev, &meta2, sizeof(meta2),
-			co2o(ef, old_cluster, old_offset));
+	if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
+			co2o(ef, old_cluster, old_offset)) < 0)
+	{
+		exfat_error("failed to read meta2 entry on rename");
+		return -EIO;
+	}
 	meta1.continuations = 1 + name_entries;
 	meta2.name_hash = exfat_calc_name_hash(ef, name);
 	meta2.name_length = name_length;
 	meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
 
-	erase_entry(ef, node);
+	if (!erase_entry(ef, node))
+		return -EIO;
 
 	node->entry_cluster = new_cluster;
 	node->entry_offset = new_offset;
 
-	exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
-			co2o(ef, new_cluster, new_offset));
+	if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
+			co2o(ef, new_cluster, new_offset)) < 0)
+	{
+		exfat_error("failed to write meta1 entry on rename");
+		return -EIO;
+	}
 	next_entry(ef, dir, &new_cluster, &new_offset);
-	exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
-			co2o(ef, new_cluster, new_offset));
+	if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
+			co2o(ef, new_cluster, new_offset)) < 0)
+	{
+		exfat_error("failed to write meta2 entry on rename");
+		return -EIO;
+	}
 
 	for (i = 0; i < name_entries; i++)
 	{
@@ -876,13 +983,18 @@
 		memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
 				EXFAT_ENAME_MAX * sizeof(le16_t));
 		next_entry(ef, dir, &new_cluster, &new_offset);
-		exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
-				co2o(ef, new_cluster, new_offset));
+		if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
+				co2o(ef, new_cluster, new_offset)) < 0)
+		{
+			exfat_error("failed to write name entry on rename");
+			return -EIO;
+		}
 	}
 
 	memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
 	tree_detach(node);
 	tree_attach(dir, node);
+	return 0;
 }
 
 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
@@ -961,7 +1073,7 @@
 		exfat_put_node(ef, node);
 		return rc;
 	}
-	rename_entry(ef, dir, node, name, cluster, offset);
+	rc = rename_entry(ef, dir, node, name, cluster, offset);
 	exfat_put_node(ef, dir);
 	exfat_put_node(ef, node);
 	return 0;
@@ -1049,8 +1161,12 @@
 	if (entry.length == 0)
 		entry.type ^= EXFAT_ENTRY_VALID;
 
-	exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
-			co2o(ef, cluster, offset));
+	if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
+			co2o(ef, cluster, offset)) < 0)
+	{
+		exfat_error("failed to write label entry");
+		return -EIO;
+	}
 	strcpy(ef->label, label);
 	return 0;
 }
diff --git a/exfat/libexfat/platform.h b/exfat/libexfat/platform.h
new file mode 100644
index 0000000..320f757
--- /dev/null
+++ b/exfat/libexfat/platform.h
@@ -0,0 +1,79 @@
+/*
+	platform.h (14.05.13)
+	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
+
+	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
+	(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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef PLATFORM_H_INCLUDED
+#define PLATFORM_H_INCLUDED
+
+#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 Unknown platform
+#endif
+
+#endif /* ifndef PLATFORM_H_INCLUDED */
diff --git a/exfat/libexfat/time.c b/exfat/libexfat/time.c
index 10a826a..43c5fa4 100644
--- a/exfat/libexfat/time.c
+++ b/exfat/libexfat/time.c
@@ -2,11 +2,12 @@
 	time.c (03.02.12)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
diff --git a/exfat/libexfat/utf.c b/exfat/libexfat/utf.c
index fd16fe9..3b7e26b 100644
--- a/exfat/libexfat/utf.c
+++ b/exfat/libexfat/utf.c
@@ -2,11 +2,12 @@
 	utf.c (13.09.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
diff --git a/exfat/libexfat/utils.c b/exfat/libexfat/utils.c
index 499833b..ef87692 100644
--- a/exfat/libexfat/utils.c
+++ b/exfat/libexfat/utils.c
@@ -2,11 +2,12 @@
 	utils.c (04.09.09)
 	exFAT file system implementation library.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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.
 */
 
 #include "exfat.h"
@@ -83,7 +85,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));
+				MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
+				sizeof(le16_t));
 		checksum = exfat_add_checksum(&name_entry, checksum);
 	}
 	return cpu_to_le16(checksum);
diff --git a/exfat/libexfat/version.h b/exfat/libexfat/version.h
index b795e91..062fa04 100644
--- a/exfat/libexfat/version.h
+++ b/exfat/libexfat/version.h
@@ -2,11 +2,12 @@
 	version.h (12.06.10)
 	Version constants.
 
+	Free exFAT implementation.
 	Copyright (C) 2010-2013  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,
@@ -14,8 +15,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 VERSION_H_INCLUDED