aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ntfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ntfs')
-rw-r--r--fs/ntfs/ChangeLog253
-rw-r--r--fs/ntfs/Makefile4
-rw-r--r--fs/ntfs/aops.c455
-rw-r--r--fs/ntfs/attrib.c719
-rw-r--r--fs/ntfs/attrib.h18
-rw-r--r--fs/ntfs/compress.c54
-rw-r--r--fs/ntfs/debug.c15
-rw-r--r--fs/ntfs/dir.c35
-rw-r--r--fs/ntfs/file.c11
-rw-r--r--fs/ntfs/index.c17
-rw-r--r--fs/ntfs/inode.c603
-rw-r--r--fs/ntfs/inode.h7
-rw-r--r--fs/ntfs/layout.h83
-rw-r--r--fs/ntfs/lcnalloc.c103
-rw-r--r--fs/ntfs/lcnalloc.h21
-rw-r--r--fs/ntfs/logfile.c262
-rw-r--r--fs/ntfs/logfile.h8
-rw-r--r--fs/ntfs/malloc.h48
-rw-r--r--fs/ntfs/mft.c233
-rw-r--r--fs/ntfs/namei.c34
-rw-r--r--fs/ntfs/ntfs.h8
-rw-r--r--fs/ntfs/runlist.c652
-rw-r--r--fs/ntfs/runlist.h19
-rw-r--r--fs/ntfs/super.c722
-rw-r--r--fs/ntfs/sysctl.c4
-rw-r--r--fs/ntfs/sysctl.h2
-rw-r--r--fs/ntfs/time.h4
-rw-r--r--fs/ntfs/types.h10
-rw-r--r--fs/ntfs/unistr.c5
-rw-r--r--fs/ntfs/usnjrnl.c84
-rw-r--r--fs/ntfs/usnjrnl.h205
-rw-r--r--fs/ntfs/volume.h12
32 files changed, 3472 insertions, 1238 deletions
diff --git a/fs/ntfs/ChangeLog b/fs/ntfs/ChangeLog
index 1d2ad15f1533..49eafbdb15c1 100644
--- a/fs/ntfs/ChangeLog
+++ b/fs/ntfs/ChangeLog
@@ -1,21 +1,18 @@
1ToDo/Notes: 1ToDo/Notes:
2 - Find and fix bugs. 2 - Find and fix bugs.
3 - Checkpoint or disable the user space journal ($UsnJrnl).
4 - In between ntfs_prepare/commit_write, need exclusion between 3 - In between ntfs_prepare/commit_write, need exclusion between
5 simultaneous file extensions. Need perhaps an NInoResizeUnderway() 4 simultaneous file extensions. This is given to us by holding i_sem
6 flag which we can set in ntfs_prepare_write() and clear again in 5 on the inode. The only places in the kernel when a file is resized
7 ntfs_commit_write(). Just have to be careful in readpage/writepage, 6 are prepare/commit write and truncate for both of which i_sem is
8 as well as in truncate, that we play nice... We might need to have 7 held. Just have to be careful in readpage/writepage and all other
9 a data_size field in the ntfs_inode to store the real attribute 8 helpers not running under i_sem that we play nice...
10 length. Also need to be careful with initialized_size extention in 9 Also need to be careful with initialized_size extention in
11 ntfs_prepare_write. Basically, just be _very_ careful in this code... 10 ntfs_prepare_write. Basically, just be _very_ careful in this code...
12 OTOH, perhaps i_sem, which is held accross generic_file_write is 11 UPDATE: The only things that need to be checked are read/writepage
13 sufficient for synchronisation here. We then just need to make sure 12 which do not hold i_sem. Note writepage cannot change i_size but it
14 ntfs_readpage/writepage/truncate interoperate properly with us. 13 needs to cope with a concurrent i_size change, just like readpage.
15 UPDATE: The above is all ok as it is due to i_sem held. The only 14 Also both need to cope with concurrent changes to the other sizes,
16 thing that needs to be checked is ntfs_writepage() which does not 15 i.e. initialized/allocated/compressed size, as well.
17 hold i_sem. It cannot change i_size but it needs to cope with a
18 concurrent i_size change.
19 - Implement mft.c::sync_mft_mirror_umount(). We currently will just 16 - Implement mft.c::sync_mft_mirror_umount(). We currently will just
20 leave the volume dirty on umount if the final iput(vol->mft_ino) 17 leave the volume dirty on umount if the final iput(vol->mft_ino)
21 causes a write of any mirrored mft records due to the mft mirror 18 causes a write of any mirrored mft records due to the mft mirror
@@ -25,12 +22,232 @@ ToDo/Notes:
25 - Enable the code for setting the NT4 compatibility flag when we start 22 - Enable the code for setting the NT4 compatibility flag when we start
26 making NTFS 1.2 specific modifications. 23 making NTFS 1.2 specific modifications.
27 24
282.1.23-WIP 252.1.24 - Lots of bug fixes and support more clean journal states.
26
27 - Support journals ($LogFile) which have been modified by chkdsk. This
28 means users can boot into Windows after we marked the volume dirty.
29 The Windows boot will run chkdsk and then reboot. The user can then
30 immediately boot into Linux rather than having to do a full Windows
31 boot first before rebooting into Linux and we will recognize such a
32 journal and empty it as it is clean by definition.
33 - Support journals ($LogFile) with only one restart page as well as
34 journals with two different restart pages. We sanity check both and
35 either use the only sane one or the more recent one of the two in the
36 case that both are valid.
37 - Add fs/ntfs/malloc.h::ntfs_malloc_nofs_nofail() which is analogous to
38 ntfs_malloc_nofs() but it performs allocations with __GFP_NOFAIL and
39 hence cannot fail.
40 - Use ntfs_malloc_nofs_nofail() in the two critical regions in
41 fs/ntfs/runlist.c::ntfs_runlists_merge(). This means we no longer
42 need to panic() if the allocation fails as it now cannot fail.
43 - Fix two nasty runlist merging bugs that had gone unnoticed so far.
44 Thanks to Stefano Picerno for the bug report.
45 - Remove two bogus BUG_ON()s from fs/ntfs/mft.c.
46 - Fix handling of valid but empty mapping pairs array in
47 fs/ntfs/runlist.c::ntfs_mapping_pairs_decompress().
48 - Report unrepresentable inodes during ntfs_readdir() as KERN_WARNING
49 messages and include the inode number. Thanks to Yura Pakhuchiy for
50 pointing this out.
51 - Change ntfs_rl_truncate_nolock() to throw away the runlist if the new
52 length is zero.
53 - Add runlist.[hc]::ntfs_rl_punch_nolock() which punches a caller
54 specified hole into a runlist.
55 - Fix a bug in fs/ntfs/index.c::ntfs_index_lookup(). When the returned
56 index entry is in the index root, we forgot to set the @ir pointer in
57 the index context. Thanks to Yura Pakhuchiy for finding this bug.
58 - Remove bogus setting of PageError in ntfs_read_compressed_block().
59 - Add fs/ntfs/attrib.[hc]::ntfs_resident_attr_value_resize().
60 - Fix a bug in ntfs_map_runlist_nolock() where we forgot to protect
61 access to the allocated size in the ntfs inode with the size lock.
62 - Fix ntfs_attr_vcn_to_lcn_nolock() and ntfs_attr_find_vcn_nolock() to
63 return LCN_ENOENT when there is no runlist and the allocated size is
64 zero.
65 - Fix load_attribute_list() to handle the case of a NULL runlist.
66 - Fix handling of sparse attributes in ntfs_attr_make_non_resident().
67 - Add BUG() checks to ntfs_attr_make_non_resident() and ntfs_attr_set()
68 to ensure that these functions are never called for compressed or
69 encrypted attributes.
70 - Fix cluster (de)allocators to work when the runlist is NULL and more
71 importantly to take a locked runlist rather than them locking it
72 which leads to lock reversal.
73 - Truncate {a,c,m}time to the ntfs supported time granularity when
74 updating the times in the inode in ntfs_setattr().
75 - Fixup handling of sparse, compressed, and encrypted attributes in
76 fs/ntfs/inode.c::ntfs_read_locked_{,attr_,index_}inode(),
77 fs/ntfs/aops.c::ntfs_{read,write}page().
78 - Make ntfs_write_block() not instantiate sparse blocks if they contain
79 only zeroes.
80 - Optimize fs/ntfs/aops.c::ntfs_write_block() by extending the page
81 lock protection over the buffer submission for i/o which allows the
82 removal of the get_bh()/put_bh() pairs for each buffer.
83 - Fix fs/ntfs/aops.c::ntfs_{read,write}_block() to handle the case
84 where a concurrent truncate has truncated the runlist under our feet.
85 - Fix page_has_buffers()/page_buffers() handling in fs/ntfs/aops.c.
86 - In fs/ntfs/aops.c::ntfs_end_buffer_async_read(), use a bit spin lock
87 in the first buffer head instead of a driver global spin lock to
88 improve scalability.
89 - Minor fix to error handling and error message display in
90 fs/ntfs/aops.c::ntfs_prepare_nonresident_write().
91 - Change the mount options {u,f,d}mask to always parse the number as
92 an octal number to conform to how chmod(1) works, too. Thanks to
93 Giuseppe Bilotta and Horst von Brand for pointing out the errors of
94 my ways.
95
962.1.23 - Implement extension of resident files and make writing safe as well as
97 many bug fixes, cleanups, and enhancements...
29 98
30 - Add printk rate limiting for ntfs_warning() and ntfs_error() when 99 - Add printk rate limiting for ntfs_warning() and ntfs_error() when
31 compiled without debug. This avoids a possible denial of service 100 compiled without debug. This avoids a possible denial of service
32 attack. Thanks to Carl-Daniel Hailfinger from SuSE for pointing this 101 attack. Thanks to Carl-Daniel Hailfinger from SuSE for pointing this
33 out. 102 out.
103 - Fix compilation warnings on ia64. (Randy Dunlap)
104 - Use i_size_{read,write}() instead of reading i_size by hand and cache
105 the value where apropriate.
106 - Add size_lock to the ntfs_inode structure. This is an rw spinlock
107 and it locks against access to the inode sizes. Note, ->size_lock
108 is also accessed from irq context so you must use the _irqsave and
109 _irqrestore lock and unlock functions, respectively. Protect all
110 accesses to allocated_size, initialized_size, and compressed_size.
111 - Minor optimization to fs/ntfs/super.c::ntfs_statfs() and its helpers.
112 - Implement extension of resident files in the regular file write code
113 paths (fs/ntfs/aops.c::ntfs_{prepare,commit}_write()). At present
114 this only works until the data attribute becomes too big for the mft
115 record after which we abort the write returning -EOPNOTSUPP from
116 ntfs_prepare_write().
117 - Add disable_sparse mount option together with a per volume sparse
118 enable bit which is set appropriately and a per inode sparse disable
119 bit which is preset on some system file inodes as appropriate.
120 - Enforce that sparse support is disabled on NTFS volumes pre 3.0.
121 - Fix a bug in fs/ntfs/runlist.c::ntfs_mapping_pairs_decompress() in
122 the creation of the unmapped runlist element for the base attribute
123 extent.
124 - Split ntfs_map_runlist() into ntfs_map_runlist() and a non-locking
125 helper ntfs_map_runlist_nolock() which is used by ntfs_map_runlist().
126 This allows us to map runlist fragments with the runlist lock already
127 held without having to drop and reacquire it around the call. Adapt
128 all callers.
129 - Change ntfs_find_vcn() to ntfs_find_vcn_nolock() which takes a locked
130 runlist. This allows us to find runlist elements with the runlist
131 lock already held without having to drop and reacquire it around the
132 call. Adapt all callers.
133 - Change time to u64 in time.h::ntfs2utc() as it otherwise generates a
134 warning in the do_div() call on sparc32. Thanks to Meelis Roos for
135 the report and analysis of the warning.
136 - Fix a nasty runlist merge bug when merging two holes.
137 - Set the ntfs_inode->allocated_size to the real allocated size in the
138 mft record for resident attributes (fs/ntfs/inode.c).
139 - Small readability cleanup to use "a" instead of "ctx->attr"
140 everywhere (fs/ntfs/inode.c).
141 - Make fs/ntfs/namei.c::ntfs_get_{parent,dentry} static and move the
142 definition of ntfs_export_ops from fs/ntfs/super.c to namei.c. Also,
143 declare ntfs_export_ops in fs/ntfs/ntfs.h.
144 - Correct sparse file handling. The compressed values need to be
145 checked and set in the ntfs inode as done for compressed files and
146 the compressed size needs to be used for vfs inode->i_blocks instead
147 of the allocated size, again, as done for compressed files.
148 - Add AT_EA in addition to AT_DATA to whitelist for being allowed to be
149 non-resident in fs/ntfs/attrib.c::ntfs_attr_can_be_non_resident().
150 - Add fs/ntfs/attrib.c::ntfs_attr_vcn_to_lcn_nolock() used by the new
151 write code.
152 - Fix bug in fs/ntfs/attrib.c::ntfs_find_vcn_nolock() where after
153 dropping the read lock and taking the write lock we were not checking
154 whether someone else did not already do the work we wanted to do.
155 - Rename fs/ntfs/attrib.c::ntfs_find_vcn_nolock() to
156 ntfs_attr_find_vcn_nolock() and update all callers.
157 - Add fs/ntfs/attrib.[hc]::ntfs_attr_make_non_resident().
158 - Fix sign of various error return values to be negative in
159 fs/ntfs/lcnalloc.c.
160 - Modify ->readpage and ->writepage (fs/ntfs/aops.c) so they detect and
161 handle the case where an attribute is converted from resident to
162 non-resident by a concurrent file write.
163 - Remove checks for NULL before calling kfree() since kfree() does the
164 checking itself. (Jesper Juhl)
165 - Some utilities modify the boot sector but do not update the checksum.
166 Thus, relax the checking in fs/ntfs/super.c::is_boot_sector_ntfs() to
167 only emit a warning when the checksum is incorrect rather than
168 refusing the mount. Thanks to Bernd Casimir for pointing this
169 problem out.
170 - Update attribute definition handling.
171 - Add NTFS_MAX_CLUSTER_SIZE and NTFS_MAX_PAGES_PER_CLUSTER constants.
172 - Use NTFS_MAX_CLUSTER_SIZE in super.c instead of hard coding 0x10000.
173 - Use MAX_BUF_PER_PAGE instead of variable sized array allocation for
174 better code generation and one less sparse warning in fs/ntfs/aops.c.
175 - Remove spurious void pointer casts from fs/ntfs/. (Pekka Enberg)
176 - Use C99 style structure initialization after memory allocation where
177 possible (fs/ntfs/{attrib.c,index.c,super.c}). Thanks to Al Viro and
178 Pekka Enberg.
179 - Stamp the transaction log ($UsnJrnl), aka user space journal, if it
180 is active on the volume and we are mounting read-write or remounting
181 from read-only to read-write.
182 - Fix a bug in address space operations error recovery code paths where
183 if the runlist was not mapped at all and a mapping error occured we
184 would leave the runlist locked on exit to the function so that the
185 next access to the same file would try to take the lock and deadlock.
186 - Detect the case when Windows has been suspended to disk on the volume
187 to be mounted and if this is the case do not allow (re)mounting
188 read-write. This is done by parsing hiberfil.sys if present.
189 - Fix several occurences of a bug where we would perform 'var & ~const'
190 with a 64-bit variable and a int, i.e. 32-bit, constant. This causes
191 the higher order 32-bits of the 64-bit variable to be zeroed. To fix
192 this cast the 'const' to the same 64-bit type as 'var'.
193 - Change the runlist terminator of the newly allocated cluster(s) to
194 LCN_ENOENT in ntfs_attr_make_non_resident(). Otherwise the runlist
195 code gets confused.
196 - Add an extra parameter @last_vcn to ntfs_get_size_for_mapping_pairs()
197 and ntfs_mapping_pairs_build() to allow the runlist encoding to be
198 partial which is desirable when filling holes in sparse attributes.
199 Update all callers.
200 - Change ntfs_map_runlist_nolock() to only decompress the mapping pairs
201 if the requested vcn is inside it. Otherwise we get into problems
202 when we try to map an out of bounds vcn because we then try to map
203 the already mapped runlist fragment which causes
204 ntfs_mapping_pairs_decompress() to fail and return error. Update
205 ntfs_attr_find_vcn_nolock() accordingly.
206 - Fix a nasty deadlock that appeared in recent kernels.
207 The situation: VFS inode X on a mounted ntfs volume is dirty. For
208 same inode X, the ntfs_inode is dirty and thus corresponding on-disk
209 inode, i.e. mft record, which is in a dirty PAGE_CACHE_PAGE belonging
210 to the table of inodes, i.e. $MFT, inode 0.
211 What happens:
212 Process 1: sys_sync()/umount()/whatever... calls
213 __sync_single_inode() for $MFT -> do_writepages() -> write_page for
214 the dirty page containing the on-disk inode X, the page is now locked
215 -> ntfs_write_mst_block() which clears PageUptodate() on the page to
216 prevent anyone else getting hold of it whilst it does the write out.
217 This is necessary as the on-disk inode needs "fixups" applied before
218 the write to disk which are removed again after the write and
219 PageUptodate is then set again. It then analyses the page looking
220 for dirty on-disk inodes and when it finds one it calls
221 ntfs_may_write_mft_record() to see if it is safe to write this
222 on-disk inode. This then calls ilookup5() to check if the
223 corresponding VFS inode is in icache(). This in turn calls ifind()
224 which waits on the inode lock via wait_on_inode whilst holding the
225 global inode_lock.
226 Process 2: pdflush results in a call to __sync_single_inode for the
227 same VFS inode X on the ntfs volume. This locks the inode (I_LOCK)
228 then calls write-inode -> ntfs_write_inode -> map_mft_record() ->
229 read_cache_page() for the page (in page cache of table of inodes
230 $MFT, inode 0) containing the on-disk inode. This page has
231 PageUptodate() clear because of Process 1 (see above) so
232 read_cache_page() blocks when it tries to take the page lock for the
233 page so it can call ntfs_read_page().
234 Thus Process 1 is holding the page lock on the page containing the
235 on-disk inode X and it is waiting on the inode X to be unlocked in
236 ifind() so it can write the page out and then unlock the page.
237 And Process 2 is holding the inode lock on inode X and is waiting for
238 the page to be unlocked so it can call ntfs_readpage() or discover
239 that Process 1 set PageUptodate() again and use the page.
240 Thus we have a deadlock due to ifind() waiting on the inode lock.
241 The solution: The fix is to use the newly introduced
242 ilookup5_nowait() which does not wait on the inode's lock and hence
243 avoids the deadlock. This is safe as we do not care about the VFS
244 inode and only use the fact that it is in the VFS inode cache and the
245 fact that the vfs and ntfs inodes are one struct in memory to find
246 the ntfs inode in memory if present. Also, the ntfs inode has its
247 own locking so it does not matter if the vfs inode is locked.
248 - Fix bug in mft record writing where we forgot to set the device in
249 the buffers when mapping them after the VM had discarded them.
250 Thanks to Martin MOKREJÅ  for the bug report.
34 251
352.1.22 - Many bug and race fixes and error handling improvements. 2522.1.22 - Many bug and race fixes and error handling improvements.
36 253
@@ -1037,7 +1254,7 @@ tng-0.0.8 - 08/03/2002 - Now using BitKeeper, http://linux-ntfs.bkbits.net/
1037 - Further runlist merging work. (Richard Russon) 1254 - Further runlist merging work. (Richard Russon)
1038 - Backwards compatibility for gcc-2.95. (Richard Russon) 1255 - Backwards compatibility for gcc-2.95. (Richard Russon)
1039 - Update to kernel 2.5.5-pre1 and rediff the now tiny patch. 1256 - Update to kernel 2.5.5-pre1 and rediff the now tiny patch.
1040 - Convert to new file system declaration using ->ntfs_get_sb() and 1257 - Convert to new filesystem declaration using ->ntfs_get_sb() and
1041 replacing ntfs_read_super() with ntfs_fill_super(). 1258 replacing ntfs_read_super() with ntfs_fill_super().
1042 - Set s_maxbytes to MAX_LFS_FILESIZE to avoid page cache page index 1259 - Set s_maxbytes to MAX_LFS_FILESIZE to avoid page cache page index
1043 overflow on 32-bit architectures. 1260 overflow on 32-bit architectures.
@@ -1333,7 +1550,7 @@ tng-0.0.1 - The first useful version.
1333 The driver is now actually useful! Yey. (-: It undoubtedly has got bugs 1550 The driver is now actually useful! Yey. (-: It undoubtedly has got bugs
1334 though and it doesn't implement accesssing compressed files yet. Also, 1551 though and it doesn't implement accesssing compressed files yet. Also,
1335 accessing files with attribute list attributes is not implemented yet 1552 accessing files with attribute list attributes is not implemented yet
1336 either. But for small or simple file systems it should work and allow 1553 either. But for small or simple filesystems it should work and allow
1337 you to list directories, use stat on directory entries and the file 1554 you to list directories, use stat on directory entries and the file
1338 system, open, read, mmap and llseek around in files. A big mile stone 1555 system, open, read, mmap and llseek around in files. A big mile stone
1339 has been reached! 1556 has been reached!
@@ -1341,7 +1558,7 @@ tng-0.0.1 - The first useful version.
1341tng-0.0.0 - Initial version tag. 1558tng-0.0.0 - Initial version tag.
1342 1559
1343 Initial driver implementation. The driver can mount and umount simple 1560 Initial driver implementation. The driver can mount and umount simple
1344 NTFS file systems (i.e. ones without attribute lists in the system 1561 NTFS filesystems (i.e. ones without attribute lists in the system
1345 files). If the mount fails there might be problems in the error handling 1562 files). If the mount fails there might be problems in the error handling
1346 code paths, so be warned. Otherwise it seems to be loading the system 1563 code paths, so be warned. Otherwise it seems to be loading the system
1347 files nicely and the mft record read mapping/unmapping seems to be 1564 files nicely and the mft record read mapping/unmapping seems to be
diff --git a/fs/ntfs/Makefile b/fs/ntfs/Makefile
index 7b66381a0b0f..894b2b876d35 100644
--- a/fs/ntfs/Makefile
+++ b/fs/ntfs/Makefile
@@ -6,7 +6,7 @@ ntfs-objs := aops.o attrib.o collate.o compress.o debug.o dir.o file.o \
6 index.o inode.o mft.o mst.o namei.o runlist.o super.o sysctl.o \ 6 index.o inode.o mft.o mst.o namei.o runlist.o super.o sysctl.o \
7 unistr.o upcase.o 7 unistr.o upcase.o
8 8
9EXTRA_CFLAGS = -DNTFS_VERSION=\"2.1.22\" 9EXTRA_CFLAGS = -DNTFS_VERSION=\"2.1.24\"
10 10
11ifeq ($(CONFIG_NTFS_DEBUG),y) 11ifeq ($(CONFIG_NTFS_DEBUG),y)
12EXTRA_CFLAGS += -DDEBUG 12EXTRA_CFLAGS += -DDEBUG
@@ -15,5 +15,5 @@ endif
15ifeq ($(CONFIG_NTFS_RW),y) 15ifeq ($(CONFIG_NTFS_RW),y)
16EXTRA_CFLAGS += -DNTFS_RW 16EXTRA_CFLAGS += -DNTFS_RW
17 17
18ntfs-objs += bitmap.o lcnalloc.o logfile.o quota.o 18ntfs-objs += bitmap.o lcnalloc.o logfile.o quota.o usnjrnl.o
19endif 19endif
diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
index 45d56e41ed98..b6cc8cf24626 100644
--- a/fs/ntfs/aops.c
+++ b/fs/ntfs/aops.c
@@ -2,7 +2,7 @@
2 * aops.c - NTFS kernel address space operations and page cache handling. 2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project. 3 * Part of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -27,6 +27,7 @@
27#include <linux/swap.h> 27#include <linux/swap.h>
28#include <linux/buffer_head.h> 28#include <linux/buffer_head.h>
29#include <linux/writeback.h> 29#include <linux/writeback.h>
30#include <linux/bit_spinlock.h>
30 31
31#include "aops.h" 32#include "aops.h"
32#include "attrib.h" 33#include "attrib.h"
@@ -55,9 +56,8 @@
55 */ 56 */
56static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate) 57static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
57{ 58{
58 static DEFINE_SPINLOCK(page_uptodate_lock);
59 unsigned long flags; 59 unsigned long flags;
60 struct buffer_head *tmp; 60 struct buffer_head *first, *tmp;
61 struct page *page; 61 struct page *page;
62 ntfs_inode *ni; 62 ntfs_inode *ni;
63 int page_uptodate = 1; 63 int page_uptodate = 1;
@@ -66,19 +66,22 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
66 ni = NTFS_I(page->mapping->host); 66 ni = NTFS_I(page->mapping->host);
67 67
68 if (likely(uptodate)) { 68 if (likely(uptodate)) {
69 s64 file_ofs; 69 s64 file_ofs, initialized_size;
70 70
71 set_buffer_uptodate(bh); 71 set_buffer_uptodate(bh);
72 72
73 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) + 73 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
74 bh_offset(bh); 74 bh_offset(bh);
75 read_lock_irqsave(&ni->size_lock, flags);
76 initialized_size = ni->initialized_size;
77 read_unlock_irqrestore(&ni->size_lock, flags);
75 /* Check for the current buffer head overflowing. */ 78 /* Check for the current buffer head overflowing. */
76 if (file_ofs + bh->b_size > ni->initialized_size) { 79 if (file_ofs + bh->b_size > initialized_size) {
77 char *addr; 80 char *addr;
78 int ofs = 0; 81 int ofs = 0;
79 82
80 if (file_ofs < ni->initialized_size) 83 if (file_ofs < initialized_size)
81 ofs = ni->initialized_size - file_ofs; 84 ofs = initialized_size - file_ofs;
82 addr = kmap_atomic(page, KM_BIO_SRC_IRQ); 85 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
83 memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs); 86 memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
84 flush_dcache_page(page); 87 flush_dcache_page(page);
@@ -86,11 +89,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
86 } 89 }
87 } else { 90 } else {
88 clear_buffer_uptodate(bh); 91 clear_buffer_uptodate(bh);
92 SetPageError(page);
89 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.", 93 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.",
90 (unsigned long long)bh->b_blocknr); 94 (unsigned long long)bh->b_blocknr);
91 SetPageError(page);
92 } 95 }
93 spin_lock_irqsave(&page_uptodate_lock, flags); 96 first = page_buffers(page);
97 local_irq_save(flags);
98 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
94 clear_buffer_async_read(bh); 99 clear_buffer_async_read(bh);
95 unlock_buffer(bh); 100 unlock_buffer(bh);
96 tmp = bh; 101 tmp = bh;
@@ -105,7 +110,8 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
105 } 110 }
106 tmp = tmp->b_this_page; 111 tmp = tmp->b_this_page;
107 } while (tmp != bh); 112 } while (tmp != bh);
108 spin_unlock_irqrestore(&page_uptodate_lock, flags); 113 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
114 local_irq_restore(flags);
109 /* 115 /*
110 * If none of the buffers had errors then we can set the page uptodate, 116 * If none of the buffers had errors then we can set the page uptodate,
111 * but we first have to perform the post read mst fixups, if the 117 * but we first have to perform the post read mst fixups, if the
@@ -132,13 +138,14 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
132 i * rec_size), rec_size); 138 i * rec_size), rec_size);
133 flush_dcache_page(page); 139 flush_dcache_page(page);
134 kunmap_atomic(addr, KM_BIO_SRC_IRQ); 140 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
135 if (likely(!PageError(page) && page_uptodate)) 141 if (likely(page_uptodate && !PageError(page)))
136 SetPageUptodate(page); 142 SetPageUptodate(page);
137 } 143 }
138 unlock_page(page); 144 unlock_page(page);
139 return; 145 return;
140still_busy: 146still_busy:
141 spin_unlock_irqrestore(&page_uptodate_lock, flags); 147 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
148 local_irq_restore(flags);
142 return; 149 return;
143} 150}
144 151
@@ -168,6 +175,7 @@ static int ntfs_read_block(struct page *page)
168 runlist_element *rl; 175 runlist_element *rl;
169 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; 176 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
170 sector_t iblock, lblock, zblock; 177 sector_t iblock, lblock, zblock;
178 unsigned long flags;
171 unsigned int blocksize, vcn_ofs; 179 unsigned int blocksize, vcn_ofs;
172 int i, nr; 180 int i, nr;
173 unsigned char blocksize_bits; 181 unsigned char blocksize_bits;
@@ -181,23 +189,28 @@ static int ntfs_read_block(struct page *page)
181 blocksize_bits = VFS_I(ni)->i_blkbits; 189 blocksize_bits = VFS_I(ni)->i_blkbits;
182 blocksize = 1 << blocksize_bits; 190 blocksize = 1 << blocksize_bits;
183 191
184 if (!page_has_buffers(page)) 192 if (!page_has_buffers(page)) {
185 create_empty_buffers(page, blocksize, 0); 193 create_empty_buffers(page, blocksize, 0);
186 bh = head = page_buffers(page); 194 if (unlikely(!page_has_buffers(page))) {
187 if (unlikely(!bh)) { 195 unlock_page(page);
188 unlock_page(page); 196 return -ENOMEM;
189 return -ENOMEM; 197 }
190 } 198 }
199 bh = head = page_buffers(page);
200 BUG_ON(!bh);
191 201
192 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 202 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
203 read_lock_irqsave(&ni->size_lock, flags);
193 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits; 204 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
194 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits; 205 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
206 read_unlock_irqrestore(&ni->size_lock, flags);
195 207
196 /* Loop through all the buffers in the page. */ 208 /* Loop through all the buffers in the page. */
197 rl = NULL; 209 rl = NULL;
198 nr = i = 0; 210 nr = i = 0;
199 do { 211 do {
200 u8 *kaddr; 212 u8 *kaddr;
213 int err;
201 214
202 if (unlikely(buffer_uptodate(bh))) 215 if (unlikely(buffer_uptodate(bh)))
203 continue; 216 continue;
@@ -205,6 +218,7 @@ static int ntfs_read_block(struct page *page)
205 arr[nr++] = bh; 218 arr[nr++] = bh;
206 continue; 219 continue;
207 } 220 }
221 err = 0;
208 bh->b_bdev = vol->sb->s_bdev; 222 bh->b_bdev = vol->sb->s_bdev;
209 /* Is the block within the allowed limits? */ 223 /* Is the block within the allowed limits? */
210 if (iblock < lblock) { 224 if (iblock < lblock) {
@@ -246,7 +260,6 @@ lock_retry_remap:
246 goto handle_hole; 260 goto handle_hole;
247 /* If first try and runlist unmapped, map and retry. */ 261 /* If first try and runlist unmapped, map and retry. */
248 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) { 262 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
249 int err;
250 is_retry = TRUE; 263 is_retry = TRUE;
251 /* 264 /*
252 * Attempt to map runlist, dropping lock for 265 * Attempt to map runlist, dropping lock for
@@ -257,19 +270,30 @@ lock_retry_remap:
257 if (likely(!err)) 270 if (likely(!err))
258 goto lock_retry_remap; 271 goto lock_retry_remap;
259 rl = NULL; 272 rl = NULL;
260 lcn = err; 273 } else if (!rl)
274 up_read(&ni->runlist.lock);
275 /*
276 * If buffer is outside the runlist, treat it as a
277 * hole. This can happen due to concurrent truncate
278 * for example.
279 */
280 if (err == -ENOENT || lcn == LCN_ENOENT) {
281 err = 0;
282 goto handle_hole;
261 } 283 }
262 /* Hard error, zero out region. */ 284 /* Hard error, zero out region. */
285 if (!err)
286 err = -EIO;
263 bh->b_blocknr = -1; 287 bh->b_blocknr = -1;
264 SetPageError(page); 288 SetPageError(page);
265 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, " 289 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
266 "attribute type 0x%x, vcn 0x%llx, " 290 "attribute type 0x%x, vcn 0x%llx, "
267 "offset 0x%x because its location on " 291 "offset 0x%x because its location on "
268 "disk could not be determined%s " 292 "disk could not be determined%s "
269 "(error code %lli).", ni->mft_no, 293 "(error code %i).", ni->mft_no,
270 ni->type, (unsigned long long)vcn, 294 ni->type, (unsigned long long)vcn,
271 vcn_ofs, is_retry ? " even after " 295 vcn_ofs, is_retry ? " even after "
272 "retrying" : "", (long long)lcn); 296 "retrying" : "", err);
273 } 297 }
274 /* 298 /*
275 * Either iblock was outside lblock limits or 299 * Either iblock was outside lblock limits or
@@ -282,9 +306,10 @@ handle_hole:
282handle_zblock: 306handle_zblock:
283 kaddr = kmap_atomic(page, KM_USER0); 307 kaddr = kmap_atomic(page, KM_USER0);
284 memset(kaddr + i * blocksize, 0, blocksize); 308 memset(kaddr + i * blocksize, 0, blocksize);
285 flush_dcache_page(page);
286 kunmap_atomic(kaddr, KM_USER0); 309 kunmap_atomic(kaddr, KM_USER0);
287 set_buffer_uptodate(bh); 310 flush_dcache_page(page);
311 if (likely(!err))
312 set_buffer_uptodate(bh);
288 } while (i++, iblock++, (bh = bh->b_this_page) != head); 313 } while (i++, iblock++, (bh = bh->b_this_page) != head);
289 314
290 /* Release the lock if we took it. */ 315 /* Release the lock if we took it. */
@@ -341,14 +366,15 @@ handle_zblock:
341 */ 366 */
342static int ntfs_readpage(struct file *file, struct page *page) 367static int ntfs_readpage(struct file *file, struct page *page)
343{ 368{
344 loff_t i_size;
345 ntfs_inode *ni, *base_ni; 369 ntfs_inode *ni, *base_ni;
346 u8 *kaddr; 370 u8 *kaddr;
347 ntfs_attr_search_ctx *ctx; 371 ntfs_attr_search_ctx *ctx;
348 MFT_RECORD *mrec; 372 MFT_RECORD *mrec;
373 unsigned long flags;
349 u32 attr_len; 374 u32 attr_len;
350 int err = 0; 375 int err = 0;
351 376
377retry_readpage:
352 BUG_ON(!PageLocked(page)); 378 BUG_ON(!PageLocked(page));
353 /* 379 /*
354 * This can potentially happen because we clear PageUptodate() during 380 * This can potentially happen because we clear PageUptodate() during
@@ -359,33 +385,40 @@ static int ntfs_readpage(struct file *file, struct page *page)
359 return 0; 385 return 0;
360 } 386 }
361 ni = NTFS_I(page->mapping->host); 387 ni = NTFS_I(page->mapping->host);
362 388 /*
389 * Only $DATA attributes can be encrypted and only unnamed $DATA
390 * attributes can be compressed. Index root can have the flags set but
391 * this means to create compressed/encrypted files, not that the
392 * attribute is compressed/encrypted.
393 */
394 if (ni->type != AT_INDEX_ROOT) {
395 /* If attribute is encrypted, deny access, just like NT4. */
396 if (NInoEncrypted(ni)) {
397 BUG_ON(ni->type != AT_DATA);
398 err = -EACCES;
399 goto err_out;
400 }
401 /* Compressed data streams are handled in compress.c. */
402 if (NInoNonResident(ni) && NInoCompressed(ni)) {
403 BUG_ON(ni->type != AT_DATA);
404 BUG_ON(ni->name_len);
405 return ntfs_read_compressed_block(page);
406 }
407 }
363 /* NInoNonResident() == NInoIndexAllocPresent() */ 408 /* NInoNonResident() == NInoIndexAllocPresent() */
364 if (NInoNonResident(ni)) { 409 if (NInoNonResident(ni)) {
365 /* 410 /* Normal, non-resident data stream. */
366 * Only unnamed $DATA attributes can be compressed or
367 * encrypted.
368 */
369 if (ni->type == AT_DATA && !ni->name_len) {
370 /* If file is encrypted, deny access, just like NT4. */
371 if (NInoEncrypted(ni)) {
372 err = -EACCES;
373 goto err_out;
374 }
375 /* Compressed data streams are handled in compress.c. */
376 if (NInoCompressed(ni))
377 return ntfs_read_compressed_block(page);
378 }
379 /* Normal data stream. */
380 return ntfs_read_block(page); 411 return ntfs_read_block(page);
381 } 412 }
382 /* 413 /*
383 * Attribute is resident, implying it is not compressed or encrypted. 414 * Attribute is resident, implying it is not compressed or encrypted.
384 * This also means the attribute is smaller than an mft record and 415 * This also means the attribute is smaller than an mft record and
385 * hence smaller than a page, so can simply zero out any pages with 416 * hence smaller than a page, so can simply zero out any pages with
386 * index above 0. We can also do this if the file size is 0. 417 * index above 0. Note the attribute can actually be marked compressed
418 * but if it is resident the actual data is not compressed so we are
419 * ok to ignore the compressed flag here.
387 */ 420 */
388 if (unlikely(page->index > 0 || !i_size_read(VFS_I(ni)))) { 421 if (unlikely(page->index > 0)) {
389 kaddr = kmap_atomic(page, KM_USER0); 422 kaddr = kmap_atomic(page, KM_USER0);
390 memset(kaddr, 0, PAGE_CACHE_SIZE); 423 memset(kaddr, 0, PAGE_CACHE_SIZE);
391 flush_dcache_page(page); 424 flush_dcache_page(page);
@@ -402,6 +435,14 @@ static int ntfs_readpage(struct file *file, struct page *page)
402 err = PTR_ERR(mrec); 435 err = PTR_ERR(mrec);
403 goto err_out; 436 goto err_out;
404 } 437 }
438 /*
439 * If a parallel write made the attribute non-resident, drop the mft
440 * record and retry the readpage.
441 */
442 if (unlikely(NInoNonResident(ni))) {
443 unmap_mft_record(base_ni);
444 goto retry_readpage;
445 }
405 ctx = ntfs_attr_get_search_ctx(base_ni, mrec); 446 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
406 if (unlikely(!ctx)) { 447 if (unlikely(!ctx)) {
407 err = -ENOMEM; 448 err = -ENOMEM;
@@ -412,9 +453,10 @@ static int ntfs_readpage(struct file *file, struct page *page)
412 if (unlikely(err)) 453 if (unlikely(err))
413 goto put_unm_err_out; 454 goto put_unm_err_out;
414 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 455 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
415 i_size = i_size_read(VFS_I(ni)); 456 read_lock_irqsave(&ni->size_lock, flags);
416 if (unlikely(attr_len > i_size)) 457 if (unlikely(attr_len > ni->initialized_size))
417 attr_len = i_size; 458 attr_len = ni->initialized_size;
459 read_unlock_irqrestore(&ni->size_lock, flags);
418 kaddr = kmap_atomic(page, KM_USER0); 460 kaddr = kmap_atomic(page, KM_USER0);
419 /* Copy the data to the page. */ 461 /* Copy the data to the page. */
420 memcpy(kaddr, (u8*)ctx->attr + 462 memcpy(kaddr, (u8*)ctx->attr +
@@ -463,12 +505,15 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
463{ 505{
464 VCN vcn; 506 VCN vcn;
465 LCN lcn; 507 LCN lcn;
508 s64 initialized_size;
509 loff_t i_size;
466 sector_t block, dblock, iblock; 510 sector_t block, dblock, iblock;
467 struct inode *vi; 511 struct inode *vi;
468 ntfs_inode *ni; 512 ntfs_inode *ni;
469 ntfs_volume *vol; 513 ntfs_volume *vol;
470 runlist_element *rl; 514 runlist_element *rl;
471 struct buffer_head *bh, *head; 515 struct buffer_head *bh, *head;
516 unsigned long flags;
472 unsigned int blocksize, vcn_ofs; 517 unsigned int blocksize, vcn_ofs;
473 int err; 518 int err;
474 BOOL need_end_writeback; 519 BOOL need_end_writeback;
@@ -491,30 +536,37 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
491 BUG_ON(!PageUptodate(page)); 536 BUG_ON(!PageUptodate(page));
492 create_empty_buffers(page, blocksize, 537 create_empty_buffers(page, blocksize,
493 (1 << BH_Uptodate) | (1 << BH_Dirty)); 538 (1 << BH_Uptodate) | (1 << BH_Dirty));
539 if (unlikely(!page_has_buffers(page))) {
540 ntfs_warning(vol->sb, "Error allocating page "
541 "buffers. Redirtying page so we try "
542 "again later.");
543 /*
544 * Put the page back on mapping->dirty_pages, but leave
545 * its buffers' dirty state as-is.
546 */
547 redirty_page_for_writepage(wbc, page);
548 unlock_page(page);
549 return 0;
550 }
494 } 551 }
495 bh = head = page_buffers(page); 552 bh = head = page_buffers(page);
496 if (unlikely(!bh)) { 553 BUG_ON(!bh);
497 ntfs_warning(vol->sb, "Error allocating page buffers. "
498 "Redirtying page so we try again later.");
499 /*
500 * Put the page back on mapping->dirty_pages, but leave its
501 * buffer's dirty state as-is.
502 */
503 redirty_page_for_writepage(wbc, page);
504 unlock_page(page);
505 return 0;
506 }
507 554
508 /* NOTE: Different naming scheme to ntfs_read_block()! */ 555 /* NOTE: Different naming scheme to ntfs_read_block()! */
509 556
510 /* The first block in the page. */ 557 /* The first block in the page. */
511 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 558 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
512 559
560 read_lock_irqsave(&ni->size_lock, flags);
561 i_size = i_size_read(vi);
562 initialized_size = ni->initialized_size;
563 read_unlock_irqrestore(&ni->size_lock, flags);
564
513 /* The first out of bounds block for the data size. */ 565 /* The first out of bounds block for the data size. */
514 dblock = (vi->i_size + blocksize - 1) >> blocksize_bits; 566 dblock = (i_size + blocksize - 1) >> blocksize_bits;
515 567
516 /* The last (fully or partially) initialized block. */ 568 /* The last (fully or partially) initialized block. */
517 iblock = ni->initialized_size >> blocksize_bits; 569 iblock = initialized_size >> blocksize_bits;
518 570
519 /* 571 /*
520 * Be very careful. We have no exclusion from __set_page_dirty_buffers 572 * Be very careful. We have no exclusion from __set_page_dirty_buffers
@@ -559,7 +611,7 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
559 611
560 /* Make sure we have enough initialized size. */ 612 /* Make sure we have enough initialized size. */
561 if (unlikely((block >= iblock) && 613 if (unlikely((block >= iblock) &&
562 (ni->initialized_size < vi->i_size))) { 614 (initialized_size < i_size))) {
563 /* 615 /*
564 * If this page is fully outside initialized size, zero 616 * If this page is fully outside initialized size, zero
565 * out all pages between the current initialized size 617 * out all pages between the current initialized size
@@ -645,6 +697,27 @@ lock_retry_remap:
645 } 697 }
646 /* It is a hole, need to instantiate it. */ 698 /* It is a hole, need to instantiate it. */
647 if (lcn == LCN_HOLE) { 699 if (lcn == LCN_HOLE) {
700 u8 *kaddr;
701 unsigned long *bpos, *bend;
702
703 /* Check if the buffer is zero. */
704 kaddr = kmap_atomic(page, KM_USER0);
705 bpos = (unsigned long *)(kaddr + bh_offset(bh));
706 bend = (unsigned long *)((u8*)bpos + blocksize);
707 do {
708 if (unlikely(*bpos))
709 break;
710 } while (likely(++bpos < bend));
711 kunmap_atomic(kaddr, KM_USER0);
712 if (bpos == bend) {
713 /*
714 * Buffer is zero and sparse, no need to write
715 * it.
716 */
717 bh->b_blocknr = -1;
718 clear_buffer_dirty(bh);
719 continue;
720 }
648 // TODO: Instantiate the hole. 721 // TODO: Instantiate the hole.
649 // clear_buffer_new(bh); 722 // clear_buffer_new(bh);
650 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); 723 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
@@ -665,19 +738,37 @@ lock_retry_remap:
665 if (likely(!err)) 738 if (likely(!err))
666 goto lock_retry_remap; 739 goto lock_retry_remap;
667 rl = NULL; 740 rl = NULL;
668 lcn = err; 741 } else if (!rl)
742 up_read(&ni->runlist.lock);
743 /*
744 * If buffer is outside the runlist, truncate has cut it out
745 * of the runlist. Just clean and clear the buffer and set it
746 * uptodate so it can get discarded by the VM.
747 */
748 if (err == -ENOENT || lcn == LCN_ENOENT) {
749 u8 *kaddr;
750
751 bh->b_blocknr = -1;
752 clear_buffer_dirty(bh);
753 kaddr = kmap_atomic(page, KM_USER0);
754 memset(kaddr + bh_offset(bh), 0, blocksize);
755 kunmap_atomic(kaddr, KM_USER0);
756 flush_dcache_page(page);
757 set_buffer_uptodate(bh);
758 err = 0;
759 continue;
669 } 760 }
670 /* Failed to map the buffer, even after retrying. */ 761 /* Failed to map the buffer, even after retrying. */
762 if (!err)
763 err = -EIO;
671 bh->b_blocknr = -1; 764 bh->b_blocknr = -1;
672 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, " 765 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
673 "attribute type 0x%x, vcn 0x%llx, offset 0x%x " 766 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
674 "because its location on disk could not be " 767 "because its location on disk could not be "
675 "determined%s (error code %lli).", ni->mft_no, 768 "determined%s (error code %i).", ni->mft_no,
676 ni->type, (unsigned long long)vcn, 769 ni->type, (unsigned long long)vcn,
677 vcn_ofs, is_retry ? " even after " 770 vcn_ofs, is_retry ? " even after "
678 "retrying" : "", (long long)lcn); 771 "retrying" : "", err);
679 if (!err)
680 err = -EIO;
681 break; 772 break;
682 } while (block++, (bh = bh->b_this_page) != head); 773 } while (block++, (bh = bh->b_this_page) != head);
683 774
@@ -688,7 +779,7 @@ lock_retry_remap:
688 /* For the error case, need to reset bh to the beginning. */ 779 /* For the error case, need to reset bh to the beginning. */
689 bh = head; 780 bh = head;
690 781
691 /* Just an optimization, so ->readpage() isn't called later. */ 782 /* Just an optimization, so ->readpage() is not called later. */
692 if (unlikely(!PageUptodate(page))) { 783 if (unlikely(!PageUptodate(page))) {
693 int uptodate = 1; 784 int uptodate = 1;
694 do { 785 do {
@@ -704,7 +795,6 @@ lock_retry_remap:
704 795
705 /* Setup all mapped, dirty buffers for async write i/o. */ 796 /* Setup all mapped, dirty buffers for async write i/o. */
706 do { 797 do {
707 get_bh(bh);
708 if (buffer_mapped(bh) && buffer_dirty(bh)) { 798 if (buffer_mapped(bh) && buffer_dirty(bh)) {
709 lock_buffer(bh); 799 lock_buffer(bh);
710 if (test_clear_buffer_dirty(bh)) { 800 if (test_clear_buffer_dirty(bh)) {
@@ -742,14 +832,8 @@ lock_retry_remap:
742 832
743 BUG_ON(PageWriteback(page)); 833 BUG_ON(PageWriteback(page));
744 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */ 834 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
745 unlock_page(page);
746 835
747 /* 836 /* Submit the prepared buffers for i/o. */
748 * Submit the prepared buffers for i/o. Note the page is unlocked,
749 * and the async write i/o completion handler can end_page_writeback()
750 * at any time after the *first* submit_bh(). So the buffers can then
751 * disappear...
752 */
753 need_end_writeback = TRUE; 837 need_end_writeback = TRUE;
754 do { 838 do {
755 struct buffer_head *next = bh->b_this_page; 839 struct buffer_head *next = bh->b_this_page;
@@ -757,9 +841,9 @@ lock_retry_remap:
757 submit_bh(WRITE, bh); 841 submit_bh(WRITE, bh);
758 need_end_writeback = FALSE; 842 need_end_writeback = FALSE;
759 } 843 }
760 put_bh(bh);
761 bh = next; 844 bh = next;
762 } while (bh != head); 845 } while (bh != head);
846 unlock_page(page);
763 847
764 /* If no i/o was started, need to end_page_writeback(). */ 848 /* If no i/o was started, need to end_page_writeback(). */
765 if (unlikely(need_end_writeback)) 849 if (unlikely(need_end_writeback))
@@ -801,17 +885,15 @@ static int ntfs_write_mst_block(struct page *page,
801 ntfs_inode *ni = NTFS_I(vi); 885 ntfs_inode *ni = NTFS_I(vi);
802 ntfs_volume *vol = ni->vol; 886 ntfs_volume *vol = ni->vol;
803 u8 *kaddr; 887 u8 *kaddr;
804 unsigned char bh_size_bits = vi->i_blkbits;
805 unsigned int bh_size = 1 << bh_size_bits;
806 unsigned int rec_size = ni->itype.index.block_size; 888 unsigned int rec_size = ni->itype.index.block_size;
807 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size]; 889 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
808 struct buffer_head *bh, *head, *tbh, *rec_start_bh; 890 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
809 int max_bhs = PAGE_CACHE_SIZE / bh_size; 891 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
810 struct buffer_head *bhs[max_bhs];
811 runlist_element *rl; 892 runlist_element *rl;
812 int i, nr_locked_nis, nr_recs, nr_bhs, bhs_per_rec, err, err2; 893 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
813 unsigned rec_size_bits; 894 unsigned bh_size, rec_size_bits;
814 BOOL sync, is_mft, page_is_dirty, rec_is_dirty; 895 BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
896 unsigned char bh_size_bits;
815 897
816 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " 898 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
817 "0x%lx.", vi->i_ino, ni->type, page->index); 899 "0x%lx.", vi->i_ino, ni->type, page->index);
@@ -826,13 +908,16 @@ static int ntfs_write_mst_block(struct page *page,
826 */ 908 */
827 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) || 909 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
828 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION))); 910 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
911 bh_size_bits = vi->i_blkbits;
912 bh_size = 1 << bh_size_bits;
913 max_bhs = PAGE_CACHE_SIZE / bh_size;
829 BUG_ON(!max_bhs); 914 BUG_ON(!max_bhs);
915 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
830 916
831 /* Were we called for sync purposes? */ 917 /* Were we called for sync purposes? */
832 sync = (wbc->sync_mode == WB_SYNC_ALL); 918 sync = (wbc->sync_mode == WB_SYNC_ALL);
833 919
834 /* Make sure we have mapped buffers. */ 920 /* Make sure we have mapped buffers. */
835 BUG_ON(!page_has_buffers(page));
836 bh = head = page_buffers(page); 921 bh = head = page_buffers(page);
837 BUG_ON(!bh); 922 BUG_ON(!bh);
838 923
@@ -846,7 +931,7 @@ static int ntfs_write_mst_block(struct page *page,
846 (PAGE_CACHE_SHIFT - bh_size_bits); 931 (PAGE_CACHE_SHIFT - bh_size_bits);
847 932
848 /* The first out of bounds block for the data size. */ 933 /* The first out of bounds block for the data size. */
849 dblock = (vi->i_size + bh_size - 1) >> bh_size_bits; 934 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
850 935
851 rl = NULL; 936 rl = NULL;
852 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0; 937 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
@@ -858,6 +943,7 @@ static int ntfs_write_mst_block(struct page *page,
858 if (likely(block < rec_block)) { 943 if (likely(block < rec_block)) {
859 if (unlikely(block >= dblock)) { 944 if (unlikely(block >= dblock)) {
860 clear_buffer_dirty(bh); 945 clear_buffer_dirty(bh);
946 set_buffer_uptodate(bh);
861 continue; 947 continue;
862 } 948 }
863 /* 949 /*
@@ -895,6 +981,7 @@ static int ntfs_write_mst_block(struct page *page,
895 LCN lcn; 981 LCN lcn;
896 unsigned int vcn_ofs; 982 unsigned int vcn_ofs;
897 983
984 bh->b_bdev = vol->sb->s_bdev;
898 /* Obtain the vcn and offset of the current block. */ 985 /* Obtain the vcn and offset of the current block. */
899 vcn = (VCN)block << bh_size_bits; 986 vcn = (VCN)block << bh_size_bits;
900 vcn_ofs = vcn & vol->cluster_size_mask; 987 vcn_ofs = vcn & vol->cluster_size_mask;
@@ -938,8 +1025,11 @@ lock_retry_remap:
938 if (err2 == -ENOMEM) 1025 if (err2 == -ENOMEM)
939 page_is_dirty = TRUE; 1026 page_is_dirty = TRUE;
940 lcn = err2; 1027 lcn = err2;
941 } else 1028 } else {
942 err2 = -EIO; 1029 err2 = -EIO;
1030 if (!rl)
1031 up_read(&ni->runlist.lock);
1032 }
943 /* Hard error. Abort writing this record. */ 1033 /* Hard error. Abort writing this record. */
944 if (!err || err == -ENOMEM) 1034 if (!err || err == -ENOMEM)
945 err = err2; 1035 err = err2;
@@ -949,7 +1039,8 @@ lock_retry_remap:
949 "attribute type 0x%x) because " 1039 "attribute type 0x%x) because "
950 "its location on disk could " 1040 "its location on disk could "
951 "not be determined (error " 1041 "not be determined (error "
952 "code %lli).", (s64)block << 1042 "code %lli).",
1043 (long long)block <<
953 bh_size_bits >> 1044 bh_size_bits >>
954 vol->mft_record_size_bits, 1045 vol->mft_record_size_bits,
955 ni->mft_no, ni->type, 1046 ni->mft_no, ni->type,
@@ -1223,19 +1314,17 @@ done:
1223static int ntfs_writepage(struct page *page, struct writeback_control *wbc) 1314static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1224{ 1315{
1225 loff_t i_size; 1316 loff_t i_size;
1226 struct inode *vi; 1317 struct inode *vi = page->mapping->host;
1227 ntfs_inode *ni, *base_ni; 1318 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1228 char *kaddr; 1319 char *kaddr;
1229 ntfs_attr_search_ctx *ctx; 1320 ntfs_attr_search_ctx *ctx = NULL;
1230 MFT_RECORD *m; 1321 MFT_RECORD *m = NULL;
1231 u32 attr_len; 1322 u32 attr_len;
1232 int err; 1323 int err;
1233 1324
1325retry_writepage:
1234 BUG_ON(!PageLocked(page)); 1326 BUG_ON(!PageLocked(page));
1235
1236 vi = page->mapping->host;
1237 i_size = i_size_read(vi); 1327 i_size = i_size_read(vi);
1238
1239 /* Is the page fully outside i_size? (truncate in progress) */ 1328 /* Is the page fully outside i_size? (truncate in progress) */
1240 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >> 1329 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1241 PAGE_CACHE_SHIFT)) { 1330 PAGE_CACHE_SHIFT)) {
@@ -1248,40 +1337,42 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1248 ntfs_debug("Write outside i_size - truncated?"); 1337 ntfs_debug("Write outside i_size - truncated?");
1249 return 0; 1338 return 0;
1250 } 1339 }
1251 ni = NTFS_I(vi); 1340 /*
1252 1341 * Only $DATA attributes can be encrypted and only unnamed $DATA
1342 * attributes can be compressed. Index root can have the flags set but
1343 * this means to create compressed/encrypted files, not that the
1344 * attribute is compressed/encrypted.
1345 */
1346 if (ni->type != AT_INDEX_ROOT) {
1347 /* If file is encrypted, deny access, just like NT4. */
1348 if (NInoEncrypted(ni)) {
1349 unlock_page(page);
1350 BUG_ON(ni->type != AT_DATA);
1351 ntfs_debug("Denying write access to encrypted "
1352 "file.");
1353 return -EACCES;
1354 }
1355 /* Compressed data streams are handled in compress.c. */
1356 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1357 BUG_ON(ni->type != AT_DATA);
1358 BUG_ON(ni->name_len);
1359 // TODO: Implement and replace this with
1360 // return ntfs_write_compressed_block(page);
1361 unlock_page(page);
1362 ntfs_error(vi->i_sb, "Writing to compressed files is "
1363 "not supported yet. Sorry.");
1364 return -EOPNOTSUPP;
1365 }
1366 // TODO: Implement and remove this check.
1367 if (NInoNonResident(ni) && NInoSparse(ni)) {
1368 unlock_page(page);
1369 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1370 "supported yet. Sorry.");
1371 return -EOPNOTSUPP;
1372 }
1373 }
1253 /* NInoNonResident() == NInoIndexAllocPresent() */ 1374 /* NInoNonResident() == NInoIndexAllocPresent() */
1254 if (NInoNonResident(ni)) { 1375 if (NInoNonResident(ni)) {
1255 /*
1256 * Only unnamed $DATA attributes can be compressed, encrypted,
1257 * and/or sparse.
1258 */
1259 if (ni->type == AT_DATA && !ni->name_len) {
1260 /* If file is encrypted, deny access, just like NT4. */
1261 if (NInoEncrypted(ni)) {
1262 unlock_page(page);
1263 ntfs_debug("Denying write access to encrypted "
1264 "file.");
1265 return -EACCES;
1266 }
1267 /* Compressed data streams are handled in compress.c. */
1268 if (NInoCompressed(ni)) {
1269 // TODO: Implement and replace this check with
1270 // return ntfs_write_compressed_block(page);
1271 unlock_page(page);
1272 ntfs_error(vi->i_sb, "Writing to compressed "
1273 "files is not supported yet. "
1274 "Sorry.");
1275 return -EOPNOTSUPP;
1276 }
1277 // TODO: Implement and remove this check.
1278 if (NInoSparse(ni)) {
1279 unlock_page(page);
1280 ntfs_error(vi->i_sb, "Writing to sparse files "
1281 "is not supported yet. Sorry.");
1282 return -EOPNOTSUPP;
1283 }
1284 }
1285 /* We have to zero every time due to mmap-at-end-of-file. */ 1376 /* We have to zero every time due to mmap-at-end-of-file. */
1286 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) { 1377 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1287 /* The page straddles i_size. */ 1378 /* The page straddles i_size. */
@@ -1294,14 +1385,16 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1294 /* Handle mst protected attributes. */ 1385 /* Handle mst protected attributes. */
1295 if (NInoMstProtected(ni)) 1386 if (NInoMstProtected(ni))
1296 return ntfs_write_mst_block(page, wbc); 1387 return ntfs_write_mst_block(page, wbc);
1297 /* Normal data stream. */ 1388 /* Normal, non-resident data stream. */
1298 return ntfs_write_block(page, wbc); 1389 return ntfs_write_block(page, wbc);
1299 } 1390 }
1300 /* 1391 /*
1301 * Attribute is resident, implying it is not compressed, encrypted, 1392 * Attribute is resident, implying it is not compressed, encrypted, or
1302 * sparse, or mst protected. This also means the attribute is smaller 1393 * mst protected. This also means the attribute is smaller than an mft
1303 * than an mft record and hence smaller than a page, so can simply 1394 * record and hence smaller than a page, so can simply return error on
1304 * return error on any pages with index above 0. 1395 * any pages with index above 0. Note the attribute can actually be
1396 * marked compressed but if it is resident the actual data is not
1397 * compressed so we are ok to ignore the compressed flag here.
1305 */ 1398 */
1306 BUG_ON(page_has_buffers(page)); 1399 BUG_ON(page_has_buffers(page));
1307 BUG_ON(!PageUptodate(page)); 1400 BUG_ON(!PageUptodate(page));
@@ -1326,6 +1419,14 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1326 ctx = NULL; 1419 ctx = NULL;
1327 goto err_out; 1420 goto err_out;
1328 } 1421 }
1422 /*
1423 * If a parallel write made the attribute non-resident, drop the mft
1424 * record and retry the writepage.
1425 */
1426 if (unlikely(NInoNonResident(ni))) {
1427 unmap_mft_record(base_ni);
1428 goto retry_writepage;
1429 }
1329 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1430 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1330 if (unlikely(!ctx)) { 1431 if (unlikely(!ctx)) {
1331 err = -ENOMEM; 1432 err = -ENOMEM;
@@ -1342,40 +1443,21 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1342 BUG_ON(PageWriteback(page)); 1443 BUG_ON(PageWriteback(page));
1343 set_page_writeback(page); 1444 set_page_writeback(page);
1344 unlock_page(page); 1445 unlock_page(page);
1345
1346 /* 1446 /*
1347 * Here, we don't need to zero the out of bounds area everytime because 1447 * Here, we do not need to zero the out of bounds area everytime
1348 * the below memcpy() already takes care of the mmap-at-end-of-file 1448 * because the below memcpy() already takes care of the
1349 * requirements. If the file is converted to a non-resident one, then 1449 * mmap-at-end-of-file requirements. If the file is converted to a
1350 * the code path use is switched to the non-resident one where the 1450 * non-resident one, then the code path use is switched to the
1351 * zeroing happens on each ntfs_writepage() invocation. 1451 * non-resident one where the zeroing happens on each ntfs_writepage()
1352 * 1452 * invocation.
1353 * The above also applies nicely when i_size is decreased.
1354 *
1355 * When i_size is increased, the memory between the old and new i_size
1356 * _must_ be zeroed (or overwritten with new data). Otherwise we will
1357 * expose data to userspace/disk which should never have been exposed.
1358 *
1359 * FIXME: Ensure that i_size increases do the zeroing/overwriting and
1360 * if we cannot guarantee that, then enable the zeroing below. If the
1361 * zeroing below is enabled, we MUST move the unlock_page() from above
1362 * to after the kunmap_atomic(), i.e. just before the
1363 * end_page_writeback().
1364 * UPDATE: ntfs_prepare/commit_write() do the zeroing on i_size
1365 * increases for resident attributes so those are ok.
1366 * TODO: ntfs_truncate(), others?
1367 */ 1453 */
1368
1369 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 1454 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1370 i_size = i_size_read(VFS_I(ni)); 1455 i_size = i_size_read(vi);
1371 kaddr = kmap_atomic(page, KM_USER0);
1372 if (unlikely(attr_len > i_size)) { 1456 if (unlikely(attr_len > i_size)) {
1373 /* Zero out of bounds area in the mft record. */
1374 memset((u8*)ctx->attr + le16_to_cpu(
1375 ctx->attr->data.resident.value_offset) +
1376 i_size, 0, attr_len - i_size);
1377 attr_len = i_size; 1457 attr_len = i_size;
1458 ctx->attr->data.resident.value_length = cpu_to_le32(attr_len);
1378 } 1459 }
1460 kaddr = kmap_atomic(page, KM_USER0);
1379 /* Copy the data from the page to the mft record. */ 1461 /* Copy the data from the page to the mft record. */
1380 memcpy((u8*)ctx->attr + 1462 memcpy((u8*)ctx->attr +
1381 le16_to_cpu(ctx->attr->data.resident.value_offset), 1463 le16_to_cpu(ctx->attr->data.resident.value_offset),
@@ -1405,8 +1487,10 @@ err_out:
1405 err = 0; 1487 err = 0;
1406 } else { 1488 } else {
1407 ntfs_error(vi->i_sb, "Resident attribute write failed with " 1489 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1408 "error %i. Setting page error flag.", err); 1490 "error %i.", err);
1409 SetPageError(page); 1491 SetPageError(page);
1492 NVolSetErrors(ni->vol);
1493 make_bad_inode(vi);
1410 } 1494 }
1411 unlock_page(page); 1495 unlock_page(page);
1412 if (ctx) 1496 if (ctx)
@@ -1425,12 +1509,15 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1425{ 1509{
1426 VCN vcn; 1510 VCN vcn;
1427 LCN lcn; 1511 LCN lcn;
1512 s64 initialized_size;
1513 loff_t i_size;
1428 sector_t block, ablock, iblock; 1514 sector_t block, ablock, iblock;
1429 struct inode *vi; 1515 struct inode *vi;
1430 ntfs_inode *ni; 1516 ntfs_inode *ni;
1431 ntfs_volume *vol; 1517 ntfs_volume *vol;
1432 runlist_element *rl; 1518 runlist_element *rl;
1433 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait; 1519 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1520 unsigned long flags;
1434 unsigned int vcn_ofs, block_start, block_end, blocksize; 1521 unsigned int vcn_ofs, block_start, block_end, blocksize;
1435 int err; 1522 int err;
1436 BOOL is_retry; 1523 BOOL is_retry;
@@ -1462,16 +1549,20 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1462 /* The first block in the page. */ 1549 /* The first block in the page. */
1463 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 1550 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1464 1551
1552 read_lock_irqsave(&ni->size_lock, flags);
1465 /* 1553 /*
1466 * The first out of bounds block for the allocated size. No need to 1554 * The first out of bounds block for the allocated size. No need to
1467 * round up as allocated_size is in multiples of cluster size and the 1555 * round up as allocated_size is in multiples of cluster size and the
1468 * minimum cluster size is 512 bytes, which is equal to the smallest 1556 * minimum cluster size is 512 bytes, which is equal to the smallest
1469 * blocksize. 1557 * blocksize.
1470 */ 1558 */
1471 ablock = ni->allocated_size >> blocksize_bits; 1559 ablock = ni->allocated_size >> blocksize_bits;
1560 i_size = i_size_read(vi);
1561 initialized_size = ni->initialized_size;
1562 read_unlock_irqrestore(&ni->size_lock, flags);
1472 1563
1473 /* The last (fully or partially) initialized block. */ 1564 /* The last (fully or partially) initialized block. */
1474 iblock = ni->initialized_size >> blocksize_bits; 1565 iblock = initialized_size >> blocksize_bits;
1475 1566
1476 /* Loop through all the buffers in the page. */ 1567 /* Loop through all the buffers in the page. */
1477 block_start = 0; 1568 block_start = 0;
@@ -1518,7 +1609,7 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1518 * request, i.e. block < ablock is true. 1609 * request, i.e. block < ablock is true.
1519 */ 1610 */
1520 if (unlikely((block >= iblock) && 1611 if (unlikely((block >= iblock) &&
1521 (ni->initialized_size < vi->i_size))) { 1612 (initialized_size < i_size))) {
1522 /* 1613 /*
1523 * If this page is fully outside initialized size, zero 1614 * If this page is fully outside initialized size, zero
1524 * out all pages between the current initialized size 1615 * out all pages between the current initialized size
@@ -1622,6 +1713,8 @@ lock_retry_remap:
1622 "not supported yet. " 1713 "not supported yet. "
1623 "Sorry."); 1714 "Sorry.");
1624 err = -EOPNOTSUPP; 1715 err = -EOPNOTSUPP;
1716 if (!rl)
1717 up_read(&ni->runlist.lock);
1625 goto err_out; 1718 goto err_out;
1626 } else if (!is_retry && 1719 } else if (!is_retry &&
1627 lcn == LCN_RL_NOT_MAPPED) { 1720 lcn == LCN_RL_NOT_MAPPED) {
@@ -1635,26 +1728,25 @@ lock_retry_remap:
1635 if (likely(!err)) 1728 if (likely(!err))
1636 goto lock_retry_remap; 1729 goto lock_retry_remap;
1637 rl = NULL; 1730 rl = NULL;
1638 lcn = err; 1731 } else if (!rl)
1639 } 1732 up_read(&ni->runlist.lock);
1640 /* 1733 /*
1641 * Failed to map the buffer, even after 1734 * Failed to map the buffer, even after
1642 * retrying. 1735 * retrying.
1643 */ 1736 */
1737 if (!err)
1738 err = -EIO;
1644 bh->b_blocknr = -1; 1739 bh->b_blocknr = -1;
1645 ntfs_error(vol->sb, "Failed to write to inode " 1740 ntfs_error(vol->sb, "Failed to write to inode "
1646 "0x%lx, attribute type 0x%x, " 1741 "0x%lx, attribute type 0x%x, "
1647 "vcn 0x%llx, offset 0x%x " 1742 "vcn 0x%llx, offset 0x%x "
1648 "because its location on disk " 1743 "because its location on disk "
1649 "could not be determined%s " 1744 "could not be determined%s "
1650 "(error code %lli).", 1745 "(error code %i).",
1651 ni->mft_no, ni->type, 1746 ni->mft_no, ni->type,
1652 (unsigned long long)vcn, 1747 (unsigned long long)vcn,
1653 vcn_ofs, is_retry ? " even " 1748 vcn_ofs, is_retry ? " even "
1654 "after retrying" : "", 1749 "after retrying" : "", err);
1655 (long long)lcn);
1656 if (!err)
1657 err = -EIO;
1658 goto err_out; 1750 goto err_out;
1659 } 1751 }
1660 /* We now have a successful remap, i.e. lcn >= 0. */ 1752 /* We now have a successful remap, i.e. lcn >= 0. */
@@ -1797,6 +1889,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1797 unsigned from, unsigned to) 1889 unsigned from, unsigned to)
1798{ 1890{
1799 s64 new_size; 1891 s64 new_size;
1892 loff_t i_size;
1800 struct inode *vi = page->mapping->host; 1893 struct inode *vi = page->mapping->host;
1801 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); 1894 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1802 ntfs_volume *vol = ni->vol; 1895 ntfs_volume *vol = ni->vol;
@@ -1868,14 +1961,8 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1868 BUG_ON(page_has_buffers(page)); 1961 BUG_ON(page_has_buffers(page));
1869 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to; 1962 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1870 /* If we do not need to resize the attribute allocation we are done. */ 1963 /* If we do not need to resize the attribute allocation we are done. */
1871 if (new_size <= vi->i_size) 1964 if (new_size <= i_size_read(vi))
1872 goto done; 1965 goto done;
1873
1874 // FIXME: We abort for now as this code is not safe.
1875 ntfs_error(vi->i_sb, "Changing the file size is not supported yet. "
1876 "Sorry.");
1877 return -EOPNOTSUPP;
1878
1879 /* Map, pin, and lock the (base) mft record. */ 1966 /* Map, pin, and lock the (base) mft record. */
1880 if (!NInoAttr(ni)) 1967 if (!NInoAttr(ni))
1881 base_ni = ni; 1968 base_ni = ni;
@@ -1904,7 +1991,15 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1904 a = ctx->attr; 1991 a = ctx->attr;
1905 /* The total length of the attribute value. */ 1992 /* The total length of the attribute value. */
1906 attr_len = le32_to_cpu(a->data.resident.value_length); 1993 attr_len = le32_to_cpu(a->data.resident.value_length);
1907 BUG_ON(vi->i_size != attr_len); 1994 /* Fix an eventual previous failure of ntfs_commit_write(). */
1995 i_size = i_size_read(vi);
1996 if (unlikely(attr_len > i_size)) {
1997 attr_len = i_size;
1998 a->data.resident.value_length = cpu_to_le32(attr_len);
1999 }
2000 /* If we do not need to resize the attribute allocation we are done. */
2001 if (new_size <= attr_len)
2002 goto done_unm;
1908 /* Check if new size is allowed in $AttrDef. */ 2003 /* Check if new size is allowed in $AttrDef. */
1909 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); 2004 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
1910 if (unlikely(err)) { 2005 if (unlikely(err)) {
@@ -1962,6 +2057,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1962 } 2057 }
1963 flush_dcache_mft_record_page(ctx->ntfs_ino); 2058 flush_dcache_mft_record_page(ctx->ntfs_ino);
1964 mark_mft_record_dirty(ctx->ntfs_ino); 2059 mark_mft_record_dirty(ctx->ntfs_ino);
2060done_unm:
1965 ntfs_attr_put_search_ctx(ctx); 2061 ntfs_attr_put_search_ctx(ctx);
1966 unmap_mft_record(base_ni); 2062 unmap_mft_record(base_ni);
1967 /* 2063 /*
@@ -2047,7 +2143,7 @@ static int ntfs_commit_nonresident_write(struct page *page,
2047 * now we know ntfs_prepare_write() would have failed in the write 2143 * now we know ntfs_prepare_write() would have failed in the write
2048 * exceeds i_size case, so this will never trigger which is fine. 2144 * exceeds i_size case, so this will never trigger which is fine.
2049 */ 2145 */
2050 if (pos > vi->i_size) { 2146 if (pos > i_size_read(vi)) {
2051 ntfs_error(vi->i_sb, "Writing beyond the existing file size is " 2147 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
2052 "not supported yet. Sorry."); 2148 "not supported yet. Sorry.");
2053 return -EOPNOTSUPP; 2149 return -EOPNOTSUPP;
@@ -2183,9 +2279,13 @@ static int ntfs_commit_write(struct file *file, struct page *page,
2183 } 2279 }
2184 kunmap_atomic(kaddr, KM_USER0); 2280 kunmap_atomic(kaddr, KM_USER0);
2185 /* Update i_size if necessary. */ 2281 /* Update i_size if necessary. */
2186 if (vi->i_size < attr_len) { 2282 if (i_size_read(vi) < attr_len) {
2283 unsigned long flags;
2284
2285 write_lock_irqsave(&ni->size_lock, flags);
2187 ni->allocated_size = ni->initialized_size = attr_len; 2286 ni->allocated_size = ni->initialized_size = attr_len;
2188 i_size_write(vi, attr_len); 2287 i_size_write(vi, attr_len);
2288 write_unlock_irqrestore(&ni->size_lock, flags);
2189 } 2289 }
2190 /* Mark the mft record dirty, so it gets written back. */ 2290 /* Mark the mft record dirty, so it gets written back. */
2191 flush_dcache_mft_record_page(ctx->ntfs_ino); 2291 flush_dcache_mft_record_page(ctx->ntfs_ino);
@@ -2302,6 +2402,7 @@ void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
2302 buffers_to_free = bh; 2402 buffers_to_free = bh;
2303 } 2403 }
2304 bh = head = page_buffers(page); 2404 bh = head = page_buffers(page);
2405 BUG_ON(!bh);
2305 do { 2406 do {
2306 bh_ofs = bh_offset(bh); 2407 bh_ofs = bh_offset(bh);
2307 if (bh_ofs + bh_size <= ofs) 2408 if (bh_ofs + bh_size <= ofs)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 1ff7f90a18b0..3f9a4ff42ee5 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -1,7 +1,7 @@
1/** 1/**
2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project. 2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon 5 * Copyright (c) 2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -21,88 +21,233 @@
21 */ 21 */
22 22
23#include <linux/buffer_head.h> 23#include <linux/buffer_head.h>
24#include <linux/swap.h>
24 25
25#include "attrib.h" 26#include "attrib.h"
26#include "debug.h" 27#include "debug.h"
27#include "layout.h" 28#include "layout.h"
29#include "lcnalloc.h"
30#include "malloc.h"
28#include "mft.h" 31#include "mft.h"
29#include "ntfs.h" 32#include "ntfs.h"
30#include "types.h" 33#include "types.h"
31 34
32/** 35/**
33 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode 36 * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
34 * @ni: ntfs inode for which to map (part of) a runlist 37 * @ni: ntfs inode for which to map (part of) a runlist
35 * @vcn: map runlist part containing this vcn 38 * @vcn: map runlist part containing this vcn
36 * 39 *
37 * Map the part of a runlist containing the @vcn of the ntfs inode @ni. 40 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
38 * 41 *
39 * Return 0 on success and -errno on error. 42 * Return 0 on success and -errno on error. There is one special error code
43 * which is not an error as such. This is -ENOENT. It means that @vcn is out
44 * of bounds of the runlist.
40 * 45 *
41 * Locking: - The runlist must be unlocked on entry and is unlocked on return. 46 * Note the runlist can be NULL after this function returns if @vcn is zero and
42 * - This function takes the lock for writing and modifies the runlist. 47 * the attribute has zero allocated size, i.e. there simply is no runlist.
48 *
49 * Locking: - The runlist must be locked for writing.
50 * - This function modifies the runlist.
43 */ 51 */
44int ntfs_map_runlist(ntfs_inode *ni, VCN vcn) 52int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn)
45{ 53{
54 VCN end_vcn;
46 ntfs_inode *base_ni; 55 ntfs_inode *base_ni;
56 MFT_RECORD *m;
57 ATTR_RECORD *a;
47 ntfs_attr_search_ctx *ctx; 58 ntfs_attr_search_ctx *ctx;
48 MFT_RECORD *mrec; 59 runlist_element *rl;
60 unsigned long flags;
49 int err = 0; 61 int err = 0;
50 62
51 ntfs_debug("Mapping runlist part containing vcn 0x%llx.", 63 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
52 (unsigned long long)vcn); 64 (unsigned long long)vcn);
53
54 if (!NInoAttr(ni)) 65 if (!NInoAttr(ni))
55 base_ni = ni; 66 base_ni = ni;
56 else 67 else
57 base_ni = ni->ext.base_ntfs_ino; 68 base_ni = ni->ext.base_ntfs_ino;
58 69 m = map_mft_record(base_ni);
59 mrec = map_mft_record(base_ni); 70 if (IS_ERR(m))
60 if (IS_ERR(mrec)) 71 return PTR_ERR(m);
61 return PTR_ERR(mrec); 72 ctx = ntfs_attr_get_search_ctx(base_ni, m);
62 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
63 if (unlikely(!ctx)) { 73 if (unlikely(!ctx)) {
64 err = -ENOMEM; 74 err = -ENOMEM;
65 goto err_out; 75 goto err_out;
66 } 76 }
67 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 77 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
68 CASE_SENSITIVE, vcn, NULL, 0, ctx); 78 CASE_SENSITIVE, vcn, NULL, 0, ctx);
69 if (unlikely(err)) 79 if (unlikely(err)) {
70 goto put_err_out; 80 if (err == -ENOENT)
81 err = -EIO;
82 goto err_out;
83 }
84 a = ctx->attr;
85 /*
86 * Only decompress the mapping pairs if @vcn is inside it. Otherwise
87 * we get into problems when we try to map an out of bounds vcn because
88 * we then try to map the already mapped runlist fragment and
89 * ntfs_mapping_pairs_decompress() fails.
90 */
91 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
92 if (unlikely(!a->data.non_resident.lowest_vcn && end_vcn <= 1)) {
93 read_lock_irqsave(&ni->size_lock, flags);
94 end_vcn = ni->allocated_size >> ni->vol->cluster_size_bits;
95 read_unlock_irqrestore(&ni->size_lock, flags);
96 }
97 if (unlikely(vcn >= end_vcn)) {
98 err = -ENOENT;
99 goto err_out;
100 }
101 rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
102 if (IS_ERR(rl))
103 err = PTR_ERR(rl);
104 else
105 ni->runlist.rl = rl;
106err_out:
107 if (likely(ctx))
108 ntfs_attr_put_search_ctx(ctx);
109 unmap_mft_record(base_ni);
110 return err;
111}
112
113/**
114 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
115 * @ni: ntfs inode for which to map (part of) a runlist
116 * @vcn: map runlist part containing this vcn
117 *
118 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
119 *
120 * Return 0 on success and -errno on error. There is one special error code
121 * which is not an error as such. This is -ENOENT. It means that @vcn is out
122 * of bounds of the runlist.
123 *
124 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
125 * - This function takes the runlist lock for writing and modifies the
126 * runlist.
127 */
128int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
129{
130 int err = 0;
71 131
72 down_write(&ni->runlist.lock); 132 down_write(&ni->runlist.lock);
73 /* Make sure someone else didn't do the work while we were sleeping. */ 133 /* Make sure someone else didn't do the work while we were sleeping. */
74 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <= 134 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
75 LCN_RL_NOT_MAPPED)) { 135 LCN_RL_NOT_MAPPED))
76 runlist_element *rl; 136 err = ntfs_map_runlist_nolock(ni, vcn);
137 up_write(&ni->runlist.lock);
138 return err;
139}
77 140
78 rl = ntfs_mapping_pairs_decompress(ni->vol, ctx->attr, 141/**
79 ni->runlist.rl); 142 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
80 if (IS_ERR(rl)) 143 * @ni: ntfs inode of the attribute whose runlist to search
81 err = PTR_ERR(rl); 144 * @vcn: vcn to convert
82 else 145 * @write_locked: true if the runlist is locked for writing
83 ni->runlist.rl = rl; 146 *
147 * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
148 * described by the ntfs inode @ni and return the corresponding logical cluster
149 * number (lcn).
150 *
151 * If the @vcn is not mapped yet, the attempt is made to map the attribute
152 * extent containing the @vcn and the vcn to lcn conversion is retried.
153 *
154 * If @write_locked is true the caller has locked the runlist for writing and
155 * if false for reading.
156 *
157 * Since lcns must be >= 0, we use negative return codes with special meaning:
158 *
159 * Return code Meaning / Description
160 * ==========================================
161 * LCN_HOLE Hole / not allocated on disk.
162 * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
163 * LCN_ENOMEM Not enough memory to map runlist.
164 * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
165 *
166 * Locking: - The runlist must be locked on entry and is left locked on return.
167 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
168 * the lock may be dropped inside the function so you cannot rely on
169 * the runlist still being the same when this function returns.
170 */
171LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
172 const BOOL write_locked)
173{
174 LCN lcn;
175 unsigned long flags;
176 BOOL is_retry = FALSE;
177
178 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
179 ni->mft_no, (unsigned long long)vcn,
180 write_locked ? "write" : "read");
181 BUG_ON(!ni);
182 BUG_ON(!NInoNonResident(ni));
183 BUG_ON(vcn < 0);
184 if (!ni->runlist.rl) {
185 read_lock_irqsave(&ni->size_lock, flags);
186 if (!ni->allocated_size) {
187 read_unlock_irqrestore(&ni->size_lock, flags);
188 return LCN_ENOENT;
189 }
190 read_unlock_irqrestore(&ni->size_lock, flags);
84 } 191 }
85 up_write(&ni->runlist.lock); 192retry_remap:
193 /* Convert vcn to lcn. If that fails map the runlist and retry once. */
194 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
195 if (likely(lcn >= LCN_HOLE)) {
196 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
197 return lcn;
198 }
199 if (lcn != LCN_RL_NOT_MAPPED) {
200 if (lcn != LCN_ENOENT)
201 lcn = LCN_EIO;
202 } else if (!is_retry) {
203 int err;
86 204
87put_err_out: 205 if (!write_locked) {
88 ntfs_attr_put_search_ctx(ctx); 206 up_read(&ni->runlist.lock);
89err_out: 207 down_write(&ni->runlist.lock);
90 unmap_mft_record(base_ni); 208 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
91 return err; 209 LCN_RL_NOT_MAPPED)) {
210 up_write(&ni->runlist.lock);
211 down_read(&ni->runlist.lock);
212 goto retry_remap;
213 }
214 }
215 err = ntfs_map_runlist_nolock(ni, vcn);
216 if (!write_locked) {
217 up_write(&ni->runlist.lock);
218 down_read(&ni->runlist.lock);
219 }
220 if (likely(!err)) {
221 is_retry = TRUE;
222 goto retry_remap;
223 }
224 if (err == -ENOENT)
225 lcn = LCN_ENOENT;
226 else if (err == -ENOMEM)
227 lcn = LCN_ENOMEM;
228 else
229 lcn = LCN_EIO;
230 }
231 if (lcn != LCN_ENOENT)
232 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
233 (long long)lcn);
234 return lcn;
92} 235}
93 236
94/** 237/**
95 * ntfs_find_vcn - find a vcn in the runlist described by an ntfs inode 238 * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
96 * @ni: ntfs inode describing the runlist to search 239 * @ni: ntfs inode describing the runlist to search
97 * @vcn: vcn to find 240 * @vcn: vcn to find
98 * @need_write: if false, lock for reading and if true, lock for writing 241 * @write_locked: true if the runlist is locked for writing
99 * 242 *
100 * Find the virtual cluster number @vcn in the runlist described by the ntfs 243 * Find the virtual cluster number @vcn in the runlist described by the ntfs
101 * inode @ni and return the address of the runlist element containing the @vcn. 244 * inode @ni and return the address of the runlist element containing the @vcn.
102 * The runlist is left locked and the caller has to unlock it. If @need_write 245 *
103 * is true, the runlist is locked for writing and if @need_write is false, the 246 * If the @vcn is not mapped yet, the attempt is made to map the attribute
104 * runlist is locked for reading. In the error case, the runlist is not left 247 * extent containing the @vcn and the vcn to lcn conversion is retried.
105 * locked. 248 *
249 * If @write_locked is true the caller has locked the runlist for writing and
250 * if false for reading.
106 * 251 *
107 * Note you need to distinguish between the lcn of the returned runlist element 252 * Note you need to distinguish between the lcn of the returned runlist element
108 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on 253 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
@@ -118,34 +263,38 @@ err_out:
118 * -ENOMEM - Not enough memory to map runlist. 263 * -ENOMEM - Not enough memory to map runlist.
119 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc). 264 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
120 * 265 *
121 * Locking: - The runlist must be unlocked on entry. 266 * Locking: - The runlist must be locked on entry and is left locked on return.
122 * - On failing return, the runlist is unlocked. 267 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
123 * - On successful return, the runlist is locked. If @need_write us 268 * the lock may be dropped inside the function so you cannot rely on
124 * true, it is locked for writing. Otherwise is is locked for 269 * the runlist still being the same when this function returns.
125 * reading.
126 */ 270 */
127runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn, 271runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
128 const BOOL need_write) 272 const BOOL write_locked)
129{ 273{
274 unsigned long flags;
130 runlist_element *rl; 275 runlist_element *rl;
131 int err = 0; 276 int err = 0;
132 BOOL is_retry = FALSE; 277 BOOL is_retry = FALSE;
133 278
134 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, lock for %sing.", 279 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
135 ni->mft_no, (unsigned long long)vcn, 280 ni->mft_no, (unsigned long long)vcn,
136 !need_write ? "read" : "writ"); 281 write_locked ? "write" : "read");
137 BUG_ON(!ni); 282 BUG_ON(!ni);
138 BUG_ON(!NInoNonResident(ni)); 283 BUG_ON(!NInoNonResident(ni));
139 BUG_ON(vcn < 0); 284 BUG_ON(vcn < 0);
140lock_retry_remap: 285 if (!ni->runlist.rl) {
141 if (!need_write) 286 read_lock_irqsave(&ni->size_lock, flags);
142 down_read(&ni->runlist.lock); 287 if (!ni->allocated_size) {
143 else 288 read_unlock_irqrestore(&ni->size_lock, flags);
144 down_write(&ni->runlist.lock); 289 return ERR_PTR(-ENOENT);
290 }
291 read_unlock_irqrestore(&ni->size_lock, flags);
292 }
293retry_remap:
145 rl = ni->runlist.rl; 294 rl = ni->runlist.rl;
146 if (likely(rl && vcn >= rl[0].vcn)) { 295 if (likely(rl && vcn >= rl[0].vcn)) {
147 while (likely(rl->length)) { 296 while (likely(rl->length)) {
148 if (likely(vcn < rl[1].vcn)) { 297 if (unlikely(vcn < rl[1].vcn)) {
149 if (likely(rl->lcn >= LCN_HOLE)) { 298 if (likely(rl->lcn >= LCN_HOLE)) {
150 ntfs_debug("Done."); 299 ntfs_debug("Done.");
151 return rl; 300 return rl;
@@ -161,30 +310,41 @@ lock_retry_remap:
161 err = -EIO; 310 err = -EIO;
162 } 311 }
163 } 312 }
164 if (!need_write)
165 up_read(&ni->runlist.lock);
166 else
167 up_write(&ni->runlist.lock);
168 if (!err && !is_retry) { 313 if (!err && !is_retry) {
169 /* 314 /*
170 * The @vcn is in an unmapped region, map the runlist and 315 * The @vcn is in an unmapped region, map the runlist and
171 * retry. 316 * retry.
172 */ 317 */
173 err = ntfs_map_runlist(ni, vcn); 318 if (!write_locked) {
319 up_read(&ni->runlist.lock);
320 down_write(&ni->runlist.lock);
321 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
322 LCN_RL_NOT_MAPPED)) {
323 up_write(&ni->runlist.lock);
324 down_read(&ni->runlist.lock);
325 goto retry_remap;
326 }
327 }
328 err = ntfs_map_runlist_nolock(ni, vcn);
329 if (!write_locked) {
330 up_write(&ni->runlist.lock);
331 down_read(&ni->runlist.lock);
332 }
174 if (likely(!err)) { 333 if (likely(!err)) {
175 is_retry = TRUE; 334 is_retry = TRUE;
176 goto lock_retry_remap; 335 goto retry_remap;
177 } 336 }
178 /* 337 /*
179 * -EINVAL and -ENOENT coming from a failed mapping attempt are 338 * -EINVAL coming from a failed mapping attempt is equivalent
180 * equivalent to i/o errors for us as they should not happen in 339 * to i/o error for us as it should not happen in our code
181 * our code paths. 340 * paths.
182 */ 341 */
183 if (err == -EINVAL || err == -ENOENT) 342 if (err == -EINVAL)
184 err = -EIO; 343 err = -EIO;
185 } else if (!err) 344 } else if (!err)
186 err = -EIO; 345 err = -EIO;
187 ntfs_error(ni->vol->sb, "Failed with error code %i.", err); 346 if (err != -ENOENT)
347 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
188 return ERR_PTR(err); 348 return ERR_PTR(err);
189} 349}
190 350
@@ -393,6 +553,11 @@ int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
393 block_size_bits = sb->s_blocksize_bits; 553 block_size_bits = sb->s_blocksize_bits;
394 down_read(&runlist->lock); 554 down_read(&runlist->lock);
395 rl = runlist->rl; 555 rl = runlist->rl;
556 if (!rl) {
557 ntfs_error(sb, "Cannot read attribute list since runlist is "
558 "missing.");
559 goto err_out;
560 }
396 /* Read all clusters specified by the runlist one run at a time. */ 561 /* Read all clusters specified by the runlist one run at a time. */
397 while (rl->length) { 562 while (rl->length) {
398 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn); 563 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
@@ -870,15 +1035,14 @@ int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
870static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx, 1035static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
871 ntfs_inode *ni, MFT_RECORD *mrec) 1036 ntfs_inode *ni, MFT_RECORD *mrec)
872{ 1037{
873 ctx->mrec = mrec; 1038 *ctx = (ntfs_attr_search_ctx) {
874 /* Sanity checks are performed elsewhere. */ 1039 .mrec = mrec,
875 ctx->attr = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset)); 1040 /* Sanity checks are performed elsewhere. */
876 ctx->is_first = TRUE; 1041 .attr = (ATTR_RECORD*)((u8*)mrec +
877 ctx->ntfs_ino = ni; 1042 le16_to_cpu(mrec->attrs_offset)),
878 ctx->al_entry = NULL; 1043 .is_first = TRUE,
879 ctx->base_ntfs_ino = NULL; 1044 .ntfs_ino = ni,
880 ctx->base_mrec = NULL; 1045 };
881 ctx->base_attr = NULL;
882} 1046}
883 1047
884/** 1048/**
@@ -945,6 +1109,8 @@ void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
945 return; 1109 return;
946} 1110}
947 1111
1112#ifdef NTFS_RW
1113
948/** 1114/**
949 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file 1115 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
950 * @vol: ntfs volume to which the attribute belongs 1116 * @vol: ntfs volume to which the attribute belongs
@@ -1024,27 +1190,21 @@ int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1024 * Check whether the attribute of @type on the ntfs volume @vol is allowed to 1190 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1025 * be non-resident. This information is obtained from $AttrDef system file. 1191 * be non-resident. This information is obtained from $AttrDef system file.
1026 * 1192 *
1027 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, or 1193 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1028 * -ENOENT if the attribute is not listed in $AttrDef. 1194 * -ENOENT if the attribute is not listed in $AttrDef.
1029 */ 1195 */
1030int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type) 1196int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1031{ 1197{
1032 ATTR_DEF *ad; 1198 ATTR_DEF *ad;
1033 1199
1034 /*
1035 * $DATA is always allowed to be non-resident even if $AttrDef does not
1036 * specify this in the flags of the $DATA attribute definition record.
1037 */
1038 if (type == AT_DATA)
1039 return 0;
1040 /* Find the attribute definition record in $AttrDef. */ 1200 /* Find the attribute definition record in $AttrDef. */
1041 ad = ntfs_attr_find_in_attrdef(vol, type); 1201 ad = ntfs_attr_find_in_attrdef(vol, type);
1042 if (unlikely(!ad)) 1202 if (unlikely(!ad))
1043 return -ENOENT; 1203 return -ENOENT;
1044 /* Check the flags and return the result. */ 1204 /* Check the flags and return the result. */
1045 if (ad->flags & CAN_BE_NON_RESIDENT) 1205 if (ad->flags & ATTR_DEF_RESIDENT)
1046 return 0; 1206 return -EPERM;
1047 return -EPERM; 1207 return 0;
1048} 1208}
1049 1209
1050/** 1210/**
@@ -1067,9 +1227,9 @@ int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1067 */ 1227 */
1068int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type) 1228int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1069{ 1229{
1070 if (type != AT_INDEX_ALLOCATION && type != AT_EA) 1230 if (type == AT_INDEX_ALLOCATION || type == AT_EA)
1071 return 0; 1231 return -EPERM;
1072 return -EPERM; 1232 return 0;
1073} 1233}
1074 1234
1075/** 1235/**
@@ -1117,6 +1277,381 @@ int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1117} 1277}
1118 1278
1119/** 1279/**
1280 * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1281 * @m: mft record containing attribute record
1282 * @a: attribute record whose value to resize
1283 * @new_size: new size in bytes to which to resize the attribute value of @a
1284 *
1285 * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1286 * If the value is made bigger, the newly allocated space is cleared.
1287 *
1288 * Return 0 on success and -errno on error. The following error codes are
1289 * defined:
1290 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1291 *
1292 * Note: On error, no modifications have been performed whatsoever.
1293 *
1294 * Warning: If you make a record smaller without having copied all the data you
1295 * are interested in the data may be overwritten.
1296 */
1297int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1298 const u32 new_size)
1299{
1300 u32 old_size;
1301
1302 /* Resize the resident part of the attribute record. */
1303 if (ntfs_attr_record_resize(m, a,
1304 le16_to_cpu(a->data.resident.value_offset) + new_size))
1305 return -ENOSPC;
1306 /*
1307 * The resize succeeded! If we made the attribute value bigger, clear
1308 * the area between the old size and @new_size.
1309 */
1310 old_size = le32_to_cpu(a->data.resident.value_length);
1311 if (new_size > old_size)
1312 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1313 old_size, 0, new_size - old_size);
1314 /* Finally update the length of the attribute value. */
1315 a->data.resident.value_length = cpu_to_le32(new_size);
1316 return 0;
1317}
1318
1319/**
1320 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1321 * @ni: ntfs inode describing the attribute to convert
1322 *
1323 * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1324 * non-resident one.
1325 *
1326 * Return 0 on success and -errno on error. The following error return codes
1327 * are defined:
1328 * -EPERM - The attribute is not allowed to be non-resident.
1329 * -ENOMEM - Not enough memory.
1330 * -ENOSPC - Not enough disk space.
1331 * -EINVAL - Attribute not defined on the volume.
1332 * -EIO - I/o error or other error.
1333 * Note that -ENOSPC is also returned in the case that there is not enough
1334 * space in the mft record to do the conversion. This can happen when the mft
1335 * record is already very full. The caller is responsible for trying to make
1336 * space in the mft record and trying again. FIXME: Do we need a separate
1337 * error return code for this kind of -ENOSPC or is it always worth trying
1338 * again in case the attribute may then fit in a resident state so no need to
1339 * make it non-resident at all? Ho-hum... (AIA)
1340 *
1341 * NOTE to self: No changes in the attribute list are required to move from
1342 * a resident to a non-resident attribute.
1343 *
1344 * Locking: - The caller must hold i_sem on the inode.
1345 */
1346int ntfs_attr_make_non_resident(ntfs_inode *ni)
1347{
1348 s64 new_size;
1349 struct inode *vi = VFS_I(ni);
1350 ntfs_volume *vol = ni->vol;
1351 ntfs_inode *base_ni;
1352 MFT_RECORD *m;
1353 ATTR_RECORD *a;
1354 ntfs_attr_search_ctx *ctx;
1355 struct page *page;
1356 runlist_element *rl;
1357 u8 *kaddr;
1358 unsigned long flags;
1359 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1360 u32 attr_size;
1361 u8 old_res_attr_flags;
1362
1363 /* Check that the attribute is allowed to be non-resident. */
1364 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1365 if (unlikely(err)) {
1366 if (err == -EPERM)
1367 ntfs_debug("Attribute is not allowed to be "
1368 "non-resident.");
1369 else
1370 ntfs_debug("Attribute not defined on the NTFS "
1371 "volume!");
1372 return err;
1373 }
1374 /*
1375 * FIXME: Compressed and encrypted attributes are not supported when
1376 * writing and we should never have gotten here for them.
1377 */
1378 BUG_ON(NInoCompressed(ni));
1379 BUG_ON(NInoEncrypted(ni));
1380 /*
1381 * The size needs to be aligned to a cluster boundary for allocation
1382 * purposes.
1383 */
1384 new_size = (i_size_read(vi) + vol->cluster_size - 1) &
1385 ~(vol->cluster_size - 1);
1386 if (new_size > 0) {
1387 runlist_element *rl2;
1388
1389 /*
1390 * Will need the page later and since the page lock nests
1391 * outside all ntfs locks, we need to get the page now.
1392 */
1393 page = find_or_create_page(vi->i_mapping, 0,
1394 mapping_gfp_mask(vi->i_mapping));
1395 if (unlikely(!page))
1396 return -ENOMEM;
1397 /* Start by allocating clusters to hold the attribute value. */
1398 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1399 vol->cluster_size_bits, -1, DATA_ZONE);
1400 if (IS_ERR(rl)) {
1401 err = PTR_ERR(rl);
1402 ntfs_debug("Failed to allocate cluster%s, error code "
1403 "%i.", (new_size >>
1404 vol->cluster_size_bits) > 1 ? "s" : "",
1405 err);
1406 goto page_err_out;
1407 }
1408 /* Change the runlist terminator to LCN_ENOENT. */
1409 rl2 = rl;
1410 while (rl2->length)
1411 rl2++;
1412 BUG_ON(rl2->lcn != LCN_RL_NOT_MAPPED);
1413 rl2->lcn = LCN_ENOENT;
1414 } else {
1415 rl = NULL;
1416 page = NULL;
1417 }
1418 /* Determine the size of the mapping pairs array. */
1419 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1420 if (unlikely(mp_size < 0)) {
1421 err = mp_size;
1422 ntfs_debug("Failed to get size for mapping pairs array, error "
1423 "code %i.", err);
1424 goto rl_err_out;
1425 }
1426 down_write(&ni->runlist.lock);
1427 if (!NInoAttr(ni))
1428 base_ni = ni;
1429 else
1430 base_ni = ni->ext.base_ntfs_ino;
1431 m = map_mft_record(base_ni);
1432 if (IS_ERR(m)) {
1433 err = PTR_ERR(m);
1434 m = NULL;
1435 ctx = NULL;
1436 goto err_out;
1437 }
1438 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1439 if (unlikely(!ctx)) {
1440 err = -ENOMEM;
1441 goto err_out;
1442 }
1443 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1444 CASE_SENSITIVE, 0, NULL, 0, ctx);
1445 if (unlikely(err)) {
1446 if (err == -ENOENT)
1447 err = -EIO;
1448 goto err_out;
1449 }
1450 m = ctx->mrec;
1451 a = ctx->attr;
1452 BUG_ON(NInoNonResident(ni));
1453 BUG_ON(a->non_resident);
1454 /*
1455 * Calculate new offsets for the name and the mapping pairs array.
1456 */
1457 if (NInoSparse(ni) || NInoCompressed(ni))
1458 name_ofs = (offsetof(ATTR_REC,
1459 data.non_resident.compressed_size) +
1460 sizeof(a->data.non_resident.compressed_size) +
1461 7) & ~7;
1462 else
1463 name_ofs = (offsetof(ATTR_REC,
1464 data.non_resident.compressed_size) + 7) & ~7;
1465 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1466 /*
1467 * Determine the size of the resident part of the now non-resident
1468 * attribute record.
1469 */
1470 arec_size = (mp_ofs + mp_size + 7) & ~7;
1471 /*
1472 * If the page is not uptodate bring it uptodate by copying from the
1473 * attribute value.
1474 */
1475 attr_size = le32_to_cpu(a->data.resident.value_length);
1476 BUG_ON(attr_size != i_size_read(vi));
1477 if (page && !PageUptodate(page)) {
1478 kaddr = kmap_atomic(page, KM_USER0);
1479 memcpy(kaddr, (u8*)a +
1480 le16_to_cpu(a->data.resident.value_offset),
1481 attr_size);
1482 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1483 kunmap_atomic(kaddr, KM_USER0);
1484 flush_dcache_page(page);
1485 SetPageUptodate(page);
1486 }
1487 /* Backup the attribute flag. */
1488 old_res_attr_flags = a->data.resident.flags;
1489 /* Resize the resident part of the attribute record. */
1490 err = ntfs_attr_record_resize(m, a, arec_size);
1491 if (unlikely(err))
1492 goto err_out;
1493 /*
1494 * Convert the resident part of the attribute record to describe a
1495 * non-resident attribute.
1496 */
1497 a->non_resident = 1;
1498 /* Move the attribute name if it exists and update the offset. */
1499 if (a->name_length)
1500 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1501 a->name_length * sizeof(ntfschar));
1502 a->name_offset = cpu_to_le16(name_ofs);
1503 /* Setup the fields specific to non-resident attributes. */
1504 a->data.non_resident.lowest_vcn = 0;
1505 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1506 vol->cluster_size_bits);
1507 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1508 memset(&a->data.non_resident.reserved, 0,
1509 sizeof(a->data.non_resident.reserved));
1510 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1511 a->data.non_resident.data_size =
1512 a->data.non_resident.initialized_size =
1513 cpu_to_sle64(attr_size);
1514 if (NInoSparse(ni) || NInoCompressed(ni)) {
1515 a->data.non_resident.compression_unit = 4;
1516 a->data.non_resident.compressed_size =
1517 a->data.non_resident.allocated_size;
1518 } else
1519 a->data.non_resident.compression_unit = 0;
1520 /* Generate the mapping pairs array into the attribute record. */
1521 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1522 arec_size - mp_ofs, rl, 0, -1, NULL);
1523 if (unlikely(err)) {
1524 ntfs_debug("Failed to build mapping pairs, error code %i.",
1525 err);
1526 goto undo_err_out;
1527 }
1528 /* Setup the in-memory attribute structure to be non-resident. */
1529 ni->runlist.rl = rl;
1530 write_lock_irqsave(&ni->size_lock, flags);
1531 ni->allocated_size = new_size;
1532 if (NInoSparse(ni) || NInoCompressed(ni)) {
1533 ni->itype.compressed.size = ni->allocated_size;
1534 ni->itype.compressed.block_size = 1U <<
1535 (a->data.non_resident.compression_unit +
1536 vol->cluster_size_bits);
1537 ni->itype.compressed.block_size_bits =
1538 ffs(ni->itype.compressed.block_size) - 1;
1539 ni->itype.compressed.block_clusters = 1U <<
1540 a->data.non_resident.compression_unit;
1541 }
1542 write_unlock_irqrestore(&ni->size_lock, flags);
1543 /*
1544 * This needs to be last since the address space operations ->readpage
1545 * and ->writepage can run concurrently with us as they are not
1546 * serialized on i_sem. Note, we are not allowed to fail once we flip
1547 * this switch, which is another reason to do this last.
1548 */
1549 NInoSetNonResident(ni);
1550 /* Mark the mft record dirty, so it gets written back. */
1551 flush_dcache_mft_record_page(ctx->ntfs_ino);
1552 mark_mft_record_dirty(ctx->ntfs_ino);
1553 ntfs_attr_put_search_ctx(ctx);
1554 unmap_mft_record(base_ni);
1555 up_write(&ni->runlist.lock);
1556 if (page) {
1557 set_page_dirty(page);
1558 unlock_page(page);
1559 mark_page_accessed(page);
1560 page_cache_release(page);
1561 }
1562 ntfs_debug("Done.");
1563 return 0;
1564undo_err_out:
1565 /* Convert the attribute back into a resident attribute. */
1566 a->non_resident = 0;
1567 /* Move the attribute name if it exists and update the offset. */
1568 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1569 sizeof(a->data.resident.reserved) + 7) & ~7;
1570 if (a->name_length)
1571 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1572 a->name_length * sizeof(ntfschar));
1573 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1574 a->name_offset = cpu_to_le16(name_ofs);
1575 arec_size = (mp_ofs + attr_size + 7) & ~7;
1576 /* Resize the resident part of the attribute record. */
1577 err2 = ntfs_attr_record_resize(m, a, arec_size);
1578 if (unlikely(err2)) {
1579 /*
1580 * This cannot happen (well if memory corruption is at work it
1581 * could happen in theory), but deal with it as well as we can.
1582 * If the old size is too small, truncate the attribute,
1583 * otherwise simply give it a larger allocated size.
1584 * FIXME: Should check whether chkdsk complains when the
1585 * allocated size is much bigger than the resident value size.
1586 */
1587 arec_size = le32_to_cpu(a->length);
1588 if ((mp_ofs + attr_size) > arec_size) {
1589 err2 = attr_size;
1590 attr_size = arec_size - mp_ofs;
1591 ntfs_error(vol->sb, "Failed to undo partial resident "
1592 "to non-resident attribute "
1593 "conversion. Truncating inode 0x%lx, "
1594 "attribute type 0x%x from %i bytes to "
1595 "%i bytes to maintain metadata "
1596 "consistency. THIS MEANS YOU ARE "
1597 "LOSING %i BYTES DATA FROM THIS %s.",
1598 vi->i_ino,
1599 (unsigned)le32_to_cpu(ni->type),
1600 err2, attr_size, err2 - attr_size,
1601 ((ni->type == AT_DATA) &&
1602 !ni->name_len) ? "FILE": "ATTRIBUTE");
1603 write_lock_irqsave(&ni->size_lock, flags);
1604 ni->initialized_size = attr_size;
1605 i_size_write(vi, attr_size);
1606 write_unlock_irqrestore(&ni->size_lock, flags);
1607 }
1608 }
1609 /* Setup the fields specific to resident attributes. */
1610 a->data.resident.value_length = cpu_to_le32(attr_size);
1611 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1612 a->data.resident.flags = old_res_attr_flags;
1613 memset(&a->data.resident.reserved, 0,
1614 sizeof(a->data.resident.reserved));
1615 /* Copy the data from the page back to the attribute value. */
1616 if (page) {
1617 kaddr = kmap_atomic(page, KM_USER0);
1618 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1619 kunmap_atomic(kaddr, KM_USER0);
1620 }
1621 /* Setup the allocated size in the ntfs inode in case it changed. */
1622 write_lock_irqsave(&ni->size_lock, flags);
1623 ni->allocated_size = arec_size - mp_ofs;
1624 write_unlock_irqrestore(&ni->size_lock, flags);
1625 /* Mark the mft record dirty, so it gets written back. */
1626 flush_dcache_mft_record_page(ctx->ntfs_ino);
1627 mark_mft_record_dirty(ctx->ntfs_ino);
1628err_out:
1629 if (ctx)
1630 ntfs_attr_put_search_ctx(ctx);
1631 if (m)
1632 unmap_mft_record(base_ni);
1633 ni->runlist.rl = NULL;
1634 up_write(&ni->runlist.lock);
1635rl_err_out:
1636 if (rl) {
1637 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1638 ntfs_error(vol->sb, "Failed to release allocated "
1639 "cluster(s) in error code path. Run "
1640 "chkdsk to recover the lost "
1641 "cluster(s).");
1642 NVolSetErrors(vol);
1643 }
1644 ntfs_free(rl);
1645page_err_out:
1646 unlock_page(page);
1647 page_cache_release(page);
1648 }
1649 if (err == -EINVAL)
1650 err = -EIO;
1651 return err;
1652}
1653
1654/**
1120 * ntfs_attr_set - fill (a part of) an attribute with a byte 1655 * ntfs_attr_set - fill (a part of) an attribute with a byte
1121 * @ni: ntfs inode describing the attribute to fill 1656 * @ni: ntfs inode describing the attribute to fill
1122 * @ofs: offset inside the attribute at which to start to fill 1657 * @ofs: offset inside the attribute at which to start to fill
@@ -1127,6 +1662,10 @@ int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1127 * byte offset @ofs inside the attribute with the constant byte @val. 1662 * byte offset @ofs inside the attribute with the constant byte @val.
1128 * 1663 *
1129 * This function is effectively like memset() applied to an ntfs attribute. 1664 * This function is effectively like memset() applied to an ntfs attribute.
1665 * Note thie function actually only operates on the page cache pages belonging
1666 * to the ntfs attribute and it marks them dirty after doing the memset().
1667 * Thus it relies on the vm dirty page write code paths to cause the modified
1668 * pages to be written to the mft record/disk.
1130 * 1669 *
1131 * Return 0 on success and -errno on error. An error code of -ESPIPE means 1670 * Return 0 on success and -errno on error. An error code of -ESPIPE means
1132 * that @ofs + @cnt were outside the end of the attribute and no write was 1671 * that @ofs + @cnt were outside the end of the attribute and no write was
@@ -1147,6 +1686,12 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
1147 BUG_ON(cnt < 0); 1686 BUG_ON(cnt < 0);
1148 if (!cnt) 1687 if (!cnt)
1149 goto done; 1688 goto done;
1689 /*
1690 * FIXME: Compressed and encrypted attributes are not supported when
1691 * writing and we should never have gotten here for them.
1692 */
1693 BUG_ON(NInoCompressed(ni));
1694 BUG_ON(NInoEncrypted(ni));
1150 mapping = VFS_I(ni)->i_mapping; 1695 mapping = VFS_I(ni)->i_mapping;
1151 /* Work out the starting index and page offset. */ 1696 /* Work out the starting index and page offset. */
1152 idx = ofs >> PAGE_CACHE_SHIFT; 1697 idx = ofs >> PAGE_CACHE_SHIFT;
@@ -1155,7 +1700,7 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
1155 end = ofs + cnt; 1700 end = ofs + cnt;
1156 end_ofs = end & ~PAGE_CACHE_MASK; 1701 end_ofs = end & ~PAGE_CACHE_MASK;
1157 /* If the end is outside the inode size return -ESPIPE. */ 1702 /* If the end is outside the inode size return -ESPIPE. */
1158 if (unlikely(end > VFS_I(ni)->i_size)) { 1703 if (unlikely(end > i_size_read(VFS_I(ni)))) {
1159 ntfs_error(vol->sb, "Request exceeds end of attribute."); 1704 ntfs_error(vol->sb, "Request exceeds end of attribute.");
1160 return -ESPIPE; 1705 return -ESPIPE;
1161 } 1706 }
@@ -1256,3 +1801,5 @@ done:
1256 ntfs_debug("Done."); 1801 ntfs_debug("Done.");
1257 return 0; 1802 return 0;
1258} 1803}
1804
1805#endif /* NTFS_RW */
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index e0c2c6c81bc0..0618ed6fd7b3 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -2,7 +2,7 @@
2 * attrib.h - Defines for attribute handling in NTFS Linux kernel driver. 2 * attrib.h - Defines for attribute handling in NTFS Linux kernel driver.
3 * Part of the Linux-NTFS project. 3 * Part of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -60,10 +60,14 @@ typedef struct {
60 ATTR_RECORD *base_attr; 60 ATTR_RECORD *base_attr;
61} ntfs_attr_search_ctx; 61} ntfs_attr_search_ctx;
62 62
63extern int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn);
63extern int ntfs_map_runlist(ntfs_inode *ni, VCN vcn); 64extern int ntfs_map_runlist(ntfs_inode *ni, VCN vcn);
64 65
65extern runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn, 66extern LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
66 const BOOL need_write); 67 const BOOL write_locked);
68
69extern runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni,
70 const VCN vcn, const BOOL write_locked);
67 71
68int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name, 72int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
69 const u32 name_len, const IGNORE_CASE_BOOL ic, 73 const u32 name_len, const IGNORE_CASE_BOOL ic,
@@ -85,6 +89,8 @@ extern ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni,
85 MFT_RECORD *mrec); 89 MFT_RECORD *mrec);
86extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx); 90extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx);
87 91
92#ifdef NTFS_RW
93
88extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol, 94extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol,
89 const ATTR_TYPE type, const s64 size); 95 const ATTR_TYPE type, const s64 size);
90extern int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, 96extern int ntfs_attr_can_be_non_resident(const ntfs_volume *vol,
@@ -93,8 +99,14 @@ extern int ntfs_attr_can_be_resident(const ntfs_volume *vol,
93 const ATTR_TYPE type); 99 const ATTR_TYPE type);
94 100
95extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size); 101extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size);
102extern int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
103 const u32 new_size);
104
105extern int ntfs_attr_make_non_resident(ntfs_inode *ni);
96 106
97extern int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, 107extern int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt,
98 const u8 val); 108 const u8 val);
99 109
110#endif /* NTFS_RW */
111
100#endif /* _LINUX_NTFS_ATTRIB_H */ 112#endif /* _LINUX_NTFS_ATTRIB_H */
diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
index ee5ae706f861..25d24106f893 100644
--- a/fs/ntfs/compress.c
+++ b/fs/ntfs/compress.c
@@ -96,13 +96,14 @@ void free_compression_buffers(void)
96/** 96/**
97 * zero_partial_compressed_page - zero out of bounds compressed page region 97 * zero_partial_compressed_page - zero out of bounds compressed page region
98 */ 98 */
99static void zero_partial_compressed_page(ntfs_inode *ni, struct page *page) 99static void zero_partial_compressed_page(struct page *page,
100 const s64 initialized_size)
100{ 101{
101 u8 *kp = page_address(page); 102 u8 *kp = page_address(page);
102 unsigned int kp_ofs; 103 unsigned int kp_ofs;
103 104
104 ntfs_debug("Zeroing page region outside initialized size."); 105 ntfs_debug("Zeroing page region outside initialized size.");
105 if (((s64)page->index << PAGE_CACHE_SHIFT) >= ni->initialized_size) { 106 if (((s64)page->index << PAGE_CACHE_SHIFT) >= initialized_size) {
106 /* 107 /*
107 * FIXME: Using clear_page() will become wrong when we get 108 * FIXME: Using clear_page() will become wrong when we get
108 * PAGE_CACHE_SIZE != PAGE_SIZE but for now there is no problem. 109 * PAGE_CACHE_SIZE != PAGE_SIZE but for now there is no problem.
@@ -110,7 +111,7 @@ static void zero_partial_compressed_page(ntfs_inode *ni, struct page *page)
110 clear_page(kp); 111 clear_page(kp);
111 return; 112 return;
112 } 113 }
113 kp_ofs = ni->initialized_size & ~PAGE_CACHE_MASK; 114 kp_ofs = initialized_size & ~PAGE_CACHE_MASK;
114 memset(kp + kp_ofs, 0, PAGE_CACHE_SIZE - kp_ofs); 115 memset(kp + kp_ofs, 0, PAGE_CACHE_SIZE - kp_ofs);
115 return; 116 return;
116} 117}
@@ -118,12 +119,12 @@ static void zero_partial_compressed_page(ntfs_inode *ni, struct page *page)
118/** 119/**
119 * handle_bounds_compressed_page - test for&handle out of bounds compressed page 120 * handle_bounds_compressed_page - test for&handle out of bounds compressed page
120 */ 121 */
121static inline void handle_bounds_compressed_page(ntfs_inode *ni, 122static inline void handle_bounds_compressed_page(struct page *page,
122 struct page *page) 123 const loff_t i_size, const s64 initialized_size)
123{ 124{
124 if ((page->index >= (ni->initialized_size >> PAGE_CACHE_SHIFT)) && 125 if ((page->index >= (initialized_size >> PAGE_CACHE_SHIFT)) &&
125 (ni->initialized_size < VFS_I(ni)->i_size)) 126 (initialized_size < i_size))
126 zero_partial_compressed_page(ni, page); 127 zero_partial_compressed_page(page, initialized_size);
127 return; 128 return;
128} 129}
129 130
@@ -138,6 +139,8 @@ static inline void handle_bounds_compressed_page(ntfs_inode *ni,
138 * @xpage_done: set to 1 if xpage was completed successfully (IN/OUT) 139 * @xpage_done: set to 1 if xpage was completed successfully (IN/OUT)
139 * @cb_start: compression block to decompress (IN) 140 * @cb_start: compression block to decompress (IN)
140 * @cb_size: size of compression block @cb_start in bytes (IN) 141 * @cb_size: size of compression block @cb_start in bytes (IN)
142 * @i_size: file size when we started the read (IN)
143 * @initialized_size: initialized file size when we started the read (IN)
141 * 144 *
142 * The caller must have disabled preemption. ntfs_decompress() reenables it when 145 * The caller must have disabled preemption. ntfs_decompress() reenables it when
143 * the critical section is finished. 146 * the critical section is finished.
@@ -165,7 +168,8 @@ static inline void handle_bounds_compressed_page(ntfs_inode *ni,
165static int ntfs_decompress(struct page *dest_pages[], int *dest_index, 168static int ntfs_decompress(struct page *dest_pages[], int *dest_index,
166 int *dest_ofs, const int dest_max_index, const int dest_max_ofs, 169 int *dest_ofs, const int dest_max_index, const int dest_max_ofs,
167 const int xpage, char *xpage_done, u8 *const cb_start, 170 const int xpage, char *xpage_done, u8 *const cb_start,
168 const u32 cb_size) 171 const u32 cb_size, const loff_t i_size,
172 const s64 initialized_size)
169{ 173{
170 /* 174 /*
171 * Pointers into the compressed data, i.e. the compression block (cb), 175 * Pointers into the compressed data, i.e. the compression block (cb),
@@ -219,9 +223,6 @@ return_error:
219 spin_unlock(&ntfs_cb_lock); 223 spin_unlock(&ntfs_cb_lock);
220 /* Second stage: finalize completed pages. */ 224 /* Second stage: finalize completed pages. */
221 if (nr_completed_pages > 0) { 225 if (nr_completed_pages > 0) {
222 struct page *page = dest_pages[completed_pages[0]];
223 ntfs_inode *ni = NTFS_I(page->mapping->host);
224
225 for (i = 0; i < nr_completed_pages; i++) { 226 for (i = 0; i < nr_completed_pages; i++) {
226 int di = completed_pages[i]; 227 int di = completed_pages[i];
227 228
@@ -230,7 +231,8 @@ return_error:
230 * If we are outside the initialized size, zero 231 * If we are outside the initialized size, zero
231 * the out of bounds page range. 232 * the out of bounds page range.
232 */ 233 */
233 handle_bounds_compressed_page(ni, dp); 234 handle_bounds_compressed_page(dp, i_size,
235 initialized_size);
234 flush_dcache_page(dp); 236 flush_dcache_page(dp);
235 kunmap(dp); 237 kunmap(dp);
236 SetPageUptodate(dp); 238 SetPageUptodate(dp);
@@ -478,12 +480,14 @@ return_overflow:
478 */ 480 */
479int ntfs_read_compressed_block(struct page *page) 481int ntfs_read_compressed_block(struct page *page)
480{ 482{
483 loff_t i_size;
484 s64 initialized_size;
481 struct address_space *mapping = page->mapping; 485 struct address_space *mapping = page->mapping;
482 ntfs_inode *ni = NTFS_I(mapping->host); 486 ntfs_inode *ni = NTFS_I(mapping->host);
483 ntfs_volume *vol = ni->vol; 487 ntfs_volume *vol = ni->vol;
484 struct super_block *sb = vol->sb; 488 struct super_block *sb = vol->sb;
485 runlist_element *rl; 489 runlist_element *rl;
486 unsigned long block_size = sb->s_blocksize; 490 unsigned long flags, block_size = sb->s_blocksize;
487 unsigned char block_size_bits = sb->s_blocksize_bits; 491 unsigned char block_size_bits = sb->s_blocksize_bits;
488 u8 *cb, *cb_pos, *cb_end; 492 u8 *cb, *cb_pos, *cb_end;
489 struct buffer_head **bhs; 493 struct buffer_head **bhs;
@@ -535,7 +539,6 @@ int ntfs_read_compressed_block(struct page *page)
535 if (unlikely(!pages || !bhs)) { 539 if (unlikely(!pages || !bhs)) {
536 kfree(bhs); 540 kfree(bhs);
537 kfree(pages); 541 kfree(pages);
538 SetPageError(page);
539 unlock_page(page); 542 unlock_page(page);
540 ntfs_error(vol->sb, "Failed to allocate internal buffers."); 543 ntfs_error(vol->sb, "Failed to allocate internal buffers.");
541 return -ENOMEM; 544 return -ENOMEM;
@@ -552,8 +555,12 @@ int ntfs_read_compressed_block(struct page *page)
552 * The remaining pages need to be allocated and inserted into the page 555 * The remaining pages need to be allocated and inserted into the page
553 * cache, alignment guarantees keep all the below much simpler. (-8 556 * cache, alignment guarantees keep all the below much simpler. (-8
554 */ 557 */
555 max_page = ((VFS_I(ni)->i_size + PAGE_CACHE_SIZE - 1) >> 558 read_lock_irqsave(&ni->size_lock, flags);
556 PAGE_CACHE_SHIFT) - offset; 559 i_size = i_size_read(VFS_I(ni));
560 initialized_size = ni->initialized_size;
561 read_unlock_irqrestore(&ni->size_lock, flags);
562 max_page = ((i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
563 offset;
557 if (nr_pages < max_page) 564 if (nr_pages < max_page)
558 max_page = nr_pages; 565 max_page = nr_pages;
559 for (i = 0; i < max_page; i++, offset++) { 566 for (i = 0; i < max_page; i++, offset++) {
@@ -824,7 +831,8 @@ lock_retry_remap:
824 * If we are outside the initialized size, zero 831 * If we are outside the initialized size, zero
825 * the out of bounds page range. 832 * the out of bounds page range.
826 */ 833 */
827 handle_bounds_compressed_page(ni, page); 834 handle_bounds_compressed_page(page, i_size,
835 initialized_size);
828 flush_dcache_page(page); 836 flush_dcache_page(page);
829 kunmap(page); 837 kunmap(page);
830 SetPageUptodate(page); 838 SetPageUptodate(page);
@@ -847,7 +855,8 @@ lock_retry_remap:
847 ntfs_debug("Found compressed compression block."); 855 ntfs_debug("Found compressed compression block.");
848 err = ntfs_decompress(pages, &cur_page, &cur_ofs, 856 err = ntfs_decompress(pages, &cur_page, &cur_ofs,
849 cb_max_page, cb_max_ofs, xpage, &xpage_done, 857 cb_max_page, cb_max_ofs, xpage, &xpage_done,
850 cb_pos, cb_size - (cb_pos - cb)); 858 cb_pos, cb_size - (cb_pos - cb), i_size,
859 initialized_size);
851 /* 860 /*
852 * We can sleep from now on, lock already dropped by 861 * We can sleep from now on, lock already dropped by
853 * ntfs_decompress(). 862 * ntfs_decompress().
@@ -861,9 +870,6 @@ lock_retry_remap:
861 for (; prev_cur_page < cur_page; prev_cur_page++) { 870 for (; prev_cur_page < cur_page; prev_cur_page++) {
862 page = pages[prev_cur_page]; 871 page = pages[prev_cur_page];
863 if (page) { 872 if (page) {
864 if (prev_cur_page == xpage &&
865 !xpage_done)
866 SetPageError(page);
867 flush_dcache_page(page); 873 flush_dcache_page(page);
868 kunmap(page); 874 kunmap(page);
869 unlock_page(page); 875 unlock_page(page);
@@ -894,8 +900,6 @@ lock_retry_remap:
894 "Terminating them with extreme " 900 "Terminating them with extreme "
895 "prejudice. Inode 0x%lx, page index " 901 "prejudice. Inode 0x%lx, page index "
896 "0x%lx.", ni->mft_no, page->index); 902 "0x%lx.", ni->mft_no, page->index);
897 if (cur_page == xpage && !xpage_done)
898 SetPageError(page);
899 flush_dcache_page(page); 903 flush_dcache_page(page);
900 kunmap(page); 904 kunmap(page);
901 unlock_page(page); 905 unlock_page(page);
@@ -943,8 +947,6 @@ err_out:
943 for (i = cur_page; i < max_page; i++) { 947 for (i = cur_page; i < max_page; i++) {
944 page = pages[i]; 948 page = pages[i];
945 if (page) { 949 if (page) {
946 if (i == xpage && !xpage_done)
947 SetPageError(page);
948 flush_dcache_page(page); 950 flush_dcache_page(page);
949 kunmap(page); 951 kunmap(page);
950 unlock_page(page); 952 unlock_page(page);
diff --git a/fs/ntfs/debug.c b/fs/ntfs/debug.c
index 6fb6bb5e3723..807150e2c2b9 100644
--- a/fs/ntfs/debug.c
+++ b/fs/ntfs/debug.c
@@ -164,14 +164,17 @@ void ntfs_debug_dump_runlist(const runlist_element *rl)
164 if (index > -LCN_ENOENT - 1) 164 if (index > -LCN_ENOENT - 1)
165 index = 3; 165 index = 3;
166 printk(KERN_DEBUG "%-16Lx %s %-16Lx%s\n", 166 printk(KERN_DEBUG "%-16Lx %s %-16Lx%s\n",
167 (rl + i)->vcn, lcn_str[index], 167 (long long)(rl + i)->vcn, lcn_str[index],
168 (rl + i)->length, (rl + i)->length ? 168 (long long)(rl + i)->length,
169 "" : " (runlist end)"); 169 (rl + i)->length ? "" :
170 " (runlist end)");
170 } else 171 } else
171 printk(KERN_DEBUG "%-16Lx %-16Lx %-16Lx%s\n", 172 printk(KERN_DEBUG "%-16Lx %-16Lx %-16Lx%s\n",
172 (rl + i)->vcn, (rl + i)->lcn, 173 (long long)(rl + i)->vcn,
173 (rl + i)->length, (rl + i)->length ? 174 (long long)(rl + i)->lcn,
174 "" : " (runlist end)"); 175 (long long)(rl + i)->length,
176 (rl + i)->length ? "" :
177 " (runlist end)");
175 if (!(rl + i)->length) 178 if (!(rl + i)->length)
176 break; 179 break;
177 } 180 }
diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c
index 93577561cdbe..795c3d1930f5 100644
--- a/fs/ntfs/dir.c
+++ b/fs/ntfs/dir.c
@@ -1,7 +1,7 @@
1/** 1/**
2 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project. 2 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon 5 * Copyright (c) 2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -183,8 +183,7 @@ found_it:
183 name->len = 0; 183 name->len = 0;
184 *res = name; 184 *res = name;
185 } else { 185 } else {
186 if (name) 186 kfree(name);
187 kfree(name);
188 *res = NULL; 187 *res = NULL;
189 } 188 }
190 mref = le64_to_cpu(ie->data.dir.indexed_file); 189 mref = le64_to_cpu(ie->data.dir.indexed_file);
@@ -444,8 +443,7 @@ found_it2:
444 name->len = 0; 443 name->len = 0;
445 *res = name; 444 *res = name;
446 } else { 445 } else {
447 if (name) 446 kfree(name);
448 kfree(name);
449 *res = NULL; 447 *res = NULL;
450 } 448 }
451 mref = le64_to_cpu(ie->data.dir.indexed_file); 449 mref = le64_to_cpu(ie->data.dir.indexed_file);
@@ -610,7 +608,7 @@ dir_err_out:
610// TODO: (AIA) 608// TODO: (AIA)
611// The algorithm embedded in this code will be required for the time when we 609// The algorithm embedded in this code will be required for the time when we
612// want to support adding of entries to directories, where we require correct 610// want to support adding of entries to directories, where we require correct
613// collation of file names in order not to cause corruption of the file system. 611// collation of file names in order not to cause corruption of the filesystem.
614 612
615/** 613/**
616 * ntfs_lookup_inode_by_name - find an inode in a directory given its name 614 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
@@ -1053,7 +1051,8 @@ static inline int ntfs_filldir(ntfs_volume *vol, loff_t fpos,
1053 ie->key.file_name.file_name_length, &name, 1051 ie->key.file_name.file_name_length, &name,
1054 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1); 1052 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
1055 if (name_len <= 0) { 1053 if (name_len <= 0) {
1056 ntfs_debug("Skipping unrepresentable file."); 1054 ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
1055 (long long)MREF_LE(ie->data.dir.indexed_file));
1057 return 0; 1056 return 0;
1058 } 1057 }
1059 if (ie->key.file_name.file_attributes & 1058 if (ie->key.file_name.file_attributes &
@@ -1101,7 +1100,7 @@ static inline int ntfs_filldir(ntfs_volume *vol, loff_t fpos,
1101static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 1100static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1102{ 1101{
1103 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos; 1102 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1104 loff_t fpos; 1103 loff_t fpos, i_size;
1105 struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode; 1104 struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode;
1106 struct super_block *sb = vdir->i_sb; 1105 struct super_block *sb = vdir->i_sb;
1107 ntfs_inode *ndir = NTFS_I(vdir); 1106 ntfs_inode *ndir = NTFS_I(vdir);
@@ -1122,7 +1121,8 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1122 vdir->i_ino, fpos); 1121 vdir->i_ino, fpos);
1123 rc = err = 0; 1122 rc = err = 0;
1124 /* Are we at end of dir yet? */ 1123 /* Are we at end of dir yet? */
1125 if (fpos >= vdir->i_size + vol->mft_record_size) 1124 i_size = i_size_read(vdir);
1125 if (fpos >= i_size + vol->mft_record_size)
1126 goto done; 1126 goto done;
1127 /* Emulate . and .. for all directories. */ 1127 /* Emulate . and .. for all directories. */
1128 if (!fpos) { 1128 if (!fpos) {
@@ -1264,7 +1264,7 @@ skip_index_root:
1264 bmp_mapping = bmp_vi->i_mapping; 1264 bmp_mapping = bmp_vi->i_mapping;
1265 /* Get the starting bitmap bit position and sanity check it. */ 1265 /* Get the starting bitmap bit position and sanity check it. */
1266 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits; 1266 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1267 if (unlikely(bmp_pos >> 3 >= bmp_vi->i_size)) { 1267 if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
1268 ntfs_error(sb, "Current index allocation position exceeds " 1268 ntfs_error(sb, "Current index allocation position exceeds "
1269 "index bitmap size."); 1269 "index bitmap size.");
1270 goto err_out; 1270 goto err_out;
@@ -1301,7 +1301,7 @@ find_next_index_buffer:
1301 goto get_next_bmp_page; 1301 goto get_next_bmp_page;
1302 } 1302 }
1303 /* If we have reached the end of the bitmap, we are done. */ 1303 /* If we have reached the end of the bitmap, we are done. */
1304 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= vdir->i_size)) 1304 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
1305 goto unm_EOD; 1305 goto unm_EOD;
1306 ia_pos = (bmp_pos + cur_bmp_pos) << 1306 ia_pos = (bmp_pos + cur_bmp_pos) <<
1307 ndir->itype.index.block_size_bits; 1307 ndir->itype.index.block_size_bits;
@@ -1309,7 +1309,8 @@ find_next_index_buffer:
1309 ntfs_debug("Handling index buffer 0x%llx.", 1309 ntfs_debug("Handling index buffer 0x%llx.",
1310 (unsigned long long)bmp_pos + cur_bmp_pos); 1310 (unsigned long long)bmp_pos + cur_bmp_pos);
1311 /* If the current index buffer is in the same page we reuse the page. */ 1311 /* If the current index buffer is in the same page we reuse the page. */
1312 if ((prev_ia_pos & PAGE_CACHE_MASK) != (ia_pos & PAGE_CACHE_MASK)) { 1312 if ((prev_ia_pos & (s64)PAGE_CACHE_MASK) !=
1313 (ia_pos & (s64)PAGE_CACHE_MASK)) {
1313 prev_ia_pos = ia_pos; 1314 prev_ia_pos = ia_pos;
1314 if (likely(ia_page != NULL)) { 1315 if (likely(ia_page != NULL)) {
1315 unlock_page(ia_page); 1316 unlock_page(ia_page);
@@ -1441,7 +1442,7 @@ unm_EOD:
1441 ntfs_unmap_page(bmp_page); 1442 ntfs_unmap_page(bmp_page);
1442EOD: 1443EOD:
1443 /* We are finished, set fpos to EOD. */ 1444 /* We are finished, set fpos to EOD. */
1444 fpos = vdir->i_size + vol->mft_record_size; 1445 fpos = i_size + vol->mft_record_size;
1445abort: 1446abort:
1446 kfree(name); 1447 kfree(name);
1447done: 1448done:
@@ -1461,10 +1462,8 @@ err_out:
1461 unlock_page(ia_page); 1462 unlock_page(ia_page);
1462 ntfs_unmap_page(ia_page); 1463 ntfs_unmap_page(ia_page);
1463 } 1464 }
1464 if (ir) 1465 kfree(ir);
1465 kfree(ir); 1466 kfree(name);
1466 if (name)
1467 kfree(name);
1468 if (ctx) 1467 if (ctx)
1469 ntfs_attr_put_search_ctx(ctx); 1468 ntfs_attr_put_search_ctx(ctx);
1470 if (m) 1469 if (m)
@@ -1495,7 +1494,7 @@ err_out:
1495static int ntfs_dir_open(struct inode *vi, struct file *filp) 1494static int ntfs_dir_open(struct inode *vi, struct file *filp)
1496{ 1495{
1497 if (sizeof(unsigned long) < 8) { 1496 if (sizeof(unsigned long) < 8) {
1498 if (vi->i_size > MAX_LFS_FILESIZE) 1497 if (i_size_read(vi) > MAX_LFS_FILESIZE)
1499 return -EFBIG; 1498 return -EFBIG;
1500 } 1499 }
1501 return 0; 1500 return 0;
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index db8713ea0d27..be9fd1dd423d 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * file.c - NTFS kernel file operations. Part of the Linux-NTFS project. 2 * file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * 5 *
6 * This program/include file is free software; you can redistribute it and/or 6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published 7 * modify it under the terms of the GNU General Public License as published
@@ -47,7 +47,7 @@
47static int ntfs_file_open(struct inode *vi, struct file *filp) 47static int ntfs_file_open(struct inode *vi, struct file *filp)
48{ 48{
49 if (sizeof(unsigned long) < 8) { 49 if (sizeof(unsigned long) < 8) {
50 if (vi->i_size > MAX_LFS_FILESIZE) 50 if (i_size_read(vi) > MAX_LFS_FILESIZE)
51 return -EFBIG; 51 return -EFBIG;
52 } 52 }
53 return generic_file_open(vi, filp); 53 return generic_file_open(vi, filp);
@@ -94,6 +94,11 @@ static int ntfs_file_fsync(struct file *filp, struct dentry *dentry,
94 if (!datasync || !NInoNonResident(NTFS_I(vi))) 94 if (!datasync || !NInoNonResident(NTFS_I(vi)))
95 ret = ntfs_write_inode(vi, 1); 95 ret = ntfs_write_inode(vi, 1);
96 write_inode_now(vi, !datasync); 96 write_inode_now(vi, !datasync);
97 /*
98 * NOTE: If we were to use mapping->private_list (see ext2 and
99 * fs/buffer.c) for dirty blocks then we could optimize the below to be
100 * sync_mapping_buffers(vi->i_mapping).
101 */
97 err = sync_blockdev(vi->i_sb->s_bdev); 102 err = sync_blockdev(vi->i_sb->s_bdev);
98 if (unlikely(err && !ret)) 103 if (unlikely(err && !ret))
99 ret = err; 104 ret = err;
diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c
index 71bd2cd7a4d9..8f2d5727546f 100644
--- a/fs/ntfs/index.c
+++ b/fs/ntfs/index.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * index.c - NTFS kernel index handling. Part of the Linux-NTFS project. 2 * index.c - NTFS kernel index handling. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2004 Anton Altaparmakov 4 * Copyright (c) 2004-2005 Anton Altaparmakov
5 * 5 *
6 * This program/include file is free software; you can redistribute it and/or 6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published 7 * modify it under the terms of the GNU General Public License as published
@@ -39,18 +39,8 @@ ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni)
39 ntfs_index_context *ictx; 39 ntfs_index_context *ictx;
40 40
41 ictx = kmem_cache_alloc(ntfs_index_ctx_cache, SLAB_NOFS); 41 ictx = kmem_cache_alloc(ntfs_index_ctx_cache, SLAB_NOFS);
42 if (ictx) { 42 if (ictx)
43 ictx->idx_ni = idx_ni; 43 *ictx = (ntfs_index_context){ .idx_ni = idx_ni };
44 ictx->entry = NULL;
45 ictx->data = NULL;
46 ictx->data_len = 0;
47 ictx->is_in_root = 0;
48 ictx->ir = NULL;
49 ictx->actx = NULL;
50 ictx->base_ni = NULL;
51 ictx->ia = NULL;
52 ictx->page = NULL;
53 }
54 return ictx; 44 return ictx;
55} 45}
56 46
@@ -215,6 +205,7 @@ int ntfs_index_lookup(const void *key, const int key_len,
215 &ie->key, key_len)) { 205 &ie->key, key_len)) {
216ir_done: 206ir_done:
217 ictx->is_in_root = TRUE; 207 ictx->is_in_root = TRUE;
208 ictx->ir = ir;
218 ictx->actx = actx; 209 ictx->actx = actx;
219 ictx->base_ni = base_ni; 210 ictx->base_ni = base_ni;
220 ictx->ia = NULL; 211 ictx->ia = NULL;
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 31840ba0b38c..dc4bbe3acf5c 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -1,7 +1,7 @@
1/** 1/**
2 * inode.c - NTFS kernel inode handling. Part of the Linux-NTFS project. 2 * inode.c - NTFS kernel inode handling. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * 5 *
6 * This program/include file is free software; you can redistribute it and/or 6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published 7 * modify it under the terms of the GNU General Public License as published
@@ -174,7 +174,7 @@ struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
174 174
175 vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode, 175 vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode,
176 (set_t)ntfs_init_locked_inode, &na); 176 (set_t)ntfs_init_locked_inode, &na);
177 if (!vi) 177 if (unlikely(!vi))
178 return ERR_PTR(-ENOMEM); 178 return ERR_PTR(-ENOMEM);
179 179
180 err = 0; 180 err = 0;
@@ -188,7 +188,7 @@ struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
188 * There is no point in keeping bad inodes around if the failure was 188 * There is no point in keeping bad inodes around if the failure was
189 * due to ENOMEM. We want to be able to retry again later. 189 * due to ENOMEM. We want to be able to retry again later.
190 */ 190 */
191 if (err == -ENOMEM) { 191 if (unlikely(err == -ENOMEM)) {
192 iput(vi); 192 iput(vi);
193 vi = ERR_PTR(err); 193 vi = ERR_PTR(err);
194 } 194 }
@@ -235,7 +235,7 @@ struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
235 235
236 vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode, 236 vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
237 (set_t)ntfs_init_locked_inode, &na); 237 (set_t)ntfs_init_locked_inode, &na);
238 if (!vi) 238 if (unlikely(!vi))
239 return ERR_PTR(-ENOMEM); 239 return ERR_PTR(-ENOMEM);
240 240
241 err = 0; 241 err = 0;
@@ -250,7 +250,7 @@ struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
250 * simplifies things in that we never need to check for bad attribute 250 * simplifies things in that we never need to check for bad attribute
251 * inodes elsewhere. 251 * inodes elsewhere.
252 */ 252 */
253 if (err) { 253 if (unlikely(err)) {
254 iput(vi); 254 iput(vi);
255 vi = ERR_PTR(err); 255 vi = ERR_PTR(err);
256 } 256 }
@@ -290,7 +290,7 @@ struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
290 290
291 vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode, 291 vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
292 (set_t)ntfs_init_locked_inode, &na); 292 (set_t)ntfs_init_locked_inode, &na);
293 if (!vi) 293 if (unlikely(!vi))
294 return ERR_PTR(-ENOMEM); 294 return ERR_PTR(-ENOMEM);
295 295
296 err = 0; 296 err = 0;
@@ -305,7 +305,7 @@ struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
305 * simplifies things in that we never need to check for bad index 305 * simplifies things in that we never need to check for bad index
306 * inodes elsewhere. 306 * inodes elsewhere.
307 */ 307 */
308 if (err) { 308 if (unlikely(err)) {
309 iput(vi); 309 iput(vi);
310 vi = ERR_PTR(err); 310 vi = ERR_PTR(err);
311 } 311 }
@@ -317,8 +317,7 @@ struct inode *ntfs_alloc_big_inode(struct super_block *sb)
317 ntfs_inode *ni; 317 ntfs_inode *ni;
318 318
319 ntfs_debug("Entering."); 319 ntfs_debug("Entering.");
320 ni = (ntfs_inode *)kmem_cache_alloc(ntfs_big_inode_cache, 320 ni = kmem_cache_alloc(ntfs_big_inode_cache, SLAB_NOFS);
321 SLAB_NOFS);
322 if (likely(ni != NULL)) { 321 if (likely(ni != NULL)) {
323 ni->state = 0; 322 ni->state = 0;
324 return VFS_I(ni); 323 return VFS_I(ni);
@@ -343,7 +342,7 @@ static inline ntfs_inode *ntfs_alloc_extent_inode(void)
343 ntfs_inode *ni; 342 ntfs_inode *ni;
344 343
345 ntfs_debug("Entering."); 344 ntfs_debug("Entering.");
346 ni = (ntfs_inode *)kmem_cache_alloc(ntfs_inode_cache, SLAB_NOFS); 345 ni = kmem_cache_alloc(ntfs_inode_cache, SLAB_NOFS);
347 if (likely(ni != NULL)) { 346 if (likely(ni != NULL)) {
348 ni->state = 0; 347 ni->state = 0;
349 return ni; 348 return ni;
@@ -376,6 +375,7 @@ static void ntfs_destroy_extent_inode(ntfs_inode *ni)
376void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni) 375void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
377{ 376{
378 ntfs_debug("Entering."); 377 ntfs_debug("Entering.");
378 rwlock_init(&ni->size_lock);
379 ni->initialized_size = ni->allocated_size = 0; 379 ni->initialized_size = ni->allocated_size = 0;
380 ni->seq_no = 0; 380 ni->seq_no = 0;
381 atomic_set(&ni->count, 1); 381 atomic_set(&ni->count, 1);
@@ -524,6 +524,7 @@ static int ntfs_read_locked_inode(struct inode *vi)
524 ntfs_volume *vol = NTFS_SB(vi->i_sb); 524 ntfs_volume *vol = NTFS_SB(vi->i_sb);
525 ntfs_inode *ni; 525 ntfs_inode *ni;
526 MFT_RECORD *m; 526 MFT_RECORD *m;
527 ATTR_RECORD *a;
527 STANDARD_INFORMATION *si; 528 STANDARD_INFORMATION *si;
528 ntfs_attr_search_ctx *ctx; 529 ntfs_attr_search_ctx *ctx;
529 int err = 0; 530 int err = 0;
@@ -632,9 +633,10 @@ static int ntfs_read_locked_inode(struct inode *vi)
632 } 633 }
633 goto unm_err_out; 634 goto unm_err_out;
634 } 635 }
636 a = ctx->attr;
635 /* Get the standard information attribute value. */ 637 /* Get the standard information attribute value. */
636 si = (STANDARD_INFORMATION*)((char*)ctx->attr + 638 si = (STANDARD_INFORMATION*)((u8*)a +
637 le16_to_cpu(ctx->attr->data.resident.value_offset)); 639 le16_to_cpu(a->data.resident.value_offset));
638 640
639 /* Transfer information from the standard information into vi. */ 641 /* Transfer information from the standard information into vi. */
640 /* 642 /*
@@ -673,15 +675,16 @@ static int ntfs_read_locked_inode(struct inode *vi)
673 goto skip_attr_list_load; 675 goto skip_attr_list_load;
674 ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino); 676 ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
675 NInoSetAttrList(ni); 677 NInoSetAttrList(ni);
676 if (ctx->attr->flags & ATTR_IS_ENCRYPTED || 678 a = ctx->attr;
677 ctx->attr->flags & ATTR_COMPRESSION_MASK || 679 if (a->flags & ATTR_IS_ENCRYPTED ||
678 ctx->attr->flags & ATTR_IS_SPARSE) { 680 a->flags & ATTR_COMPRESSION_MASK ||
681 a->flags & ATTR_IS_SPARSE) {
679 ntfs_error(vi->i_sb, "Attribute list attribute is " 682 ntfs_error(vi->i_sb, "Attribute list attribute is "
680 "compressed/encrypted/sparse."); 683 "compressed/encrypted/sparse.");
681 goto unm_err_out; 684 goto unm_err_out;
682 } 685 }
683 /* Now allocate memory for the attribute list. */ 686 /* Now allocate memory for the attribute list. */
684 ni->attr_list_size = (u32)ntfs_attr_size(ctx->attr); 687 ni->attr_list_size = (u32)ntfs_attr_size(a);
685 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 688 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
686 if (!ni->attr_list) { 689 if (!ni->attr_list) {
687 ntfs_error(vi->i_sb, "Not enough memory to allocate " 690 ntfs_error(vi->i_sb, "Not enough memory to allocate "
@@ -689,9 +692,9 @@ static int ntfs_read_locked_inode(struct inode *vi)
689 err = -ENOMEM; 692 err = -ENOMEM;
690 goto unm_err_out; 693 goto unm_err_out;
691 } 694 }
692 if (ctx->attr->non_resident) { 695 if (a->non_resident) {
693 NInoSetAttrListNonResident(ni); 696 NInoSetAttrListNonResident(ni);
694 if (ctx->attr->data.non_resident.lowest_vcn) { 697 if (a->data.non_resident.lowest_vcn) {
695 ntfs_error(vi->i_sb, "Attribute list has non " 698 ntfs_error(vi->i_sb, "Attribute list has non "
696 "zero lowest_vcn."); 699 "zero lowest_vcn.");
697 goto unm_err_out; 700 goto unm_err_out;
@@ -701,7 +704,7 @@ static int ntfs_read_locked_inode(struct inode *vi)
701 * exclusive access to the inode at this time. 704 * exclusive access to the inode at this time.
702 */ 705 */
703 ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 706 ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
704 ctx->attr, NULL); 707 a, NULL);
705 if (IS_ERR(ni->attr_list_rl.rl)) { 708 if (IS_ERR(ni->attr_list_rl.rl)) {
706 err = PTR_ERR(ni->attr_list_rl.rl); 709 err = PTR_ERR(ni->attr_list_rl.rl);
707 ni->attr_list_rl.rl = NULL; 710 ni->attr_list_rl.rl = NULL;
@@ -712,27 +715,26 @@ static int ntfs_read_locked_inode(struct inode *vi)
712 /* Now load the attribute list. */ 715 /* Now load the attribute list. */
713 if ((err = load_attribute_list(vol, &ni->attr_list_rl, 716 if ((err = load_attribute_list(vol, &ni->attr_list_rl,
714 ni->attr_list, ni->attr_list_size, 717 ni->attr_list, ni->attr_list_size,
715 sle64_to_cpu(ctx->attr->data. 718 sle64_to_cpu(a->data.non_resident.
716 non_resident.initialized_size)))) { 719 initialized_size)))) {
717 ntfs_error(vi->i_sb, "Failed to load " 720 ntfs_error(vi->i_sb, "Failed to load "
718 "attribute list attribute."); 721 "attribute list attribute.");
719 goto unm_err_out; 722 goto unm_err_out;
720 } 723 }
721 } else /* if (!ctx.attr->non_resident) */ { 724 } else /* if (!a->non_resident) */ {
722 if ((u8*)ctx->attr + le16_to_cpu( 725 if ((u8*)a + le16_to_cpu(a->data.resident.value_offset)
723 ctx->attr->data.resident.value_offset) + 726 + le32_to_cpu(
724 le32_to_cpu( 727 a->data.resident.value_length) >
725 ctx->attr->data.resident.value_length) >
726 (u8*)ctx->mrec + vol->mft_record_size) { 728 (u8*)ctx->mrec + vol->mft_record_size) {
727 ntfs_error(vi->i_sb, "Corrupt attribute list " 729 ntfs_error(vi->i_sb, "Corrupt attribute list "
728 "in inode."); 730 "in inode.");
729 goto unm_err_out; 731 goto unm_err_out;
730 } 732 }
731 /* Now copy the attribute list. */ 733 /* Now copy the attribute list. */
732 memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu( 734 memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
733 ctx->attr->data.resident.value_offset), 735 a->data.resident.value_offset),
734 le32_to_cpu( 736 le32_to_cpu(
735 ctx->attr->data.resident.value_length)); 737 a->data.resident.value_length));
736 } 738 }
737 } 739 }
738skip_attr_list_load: 740skip_attr_list_load:
@@ -741,10 +743,11 @@ skip_attr_list_load:
741 * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes. 743 * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
742 */ 744 */
743 if (S_ISDIR(vi->i_mode)) { 745 if (S_ISDIR(vi->i_mode)) {
746 loff_t bvi_size;
744 struct inode *bvi; 747 struct inode *bvi;
745 ntfs_inode *bni; 748 ntfs_inode *bni;
746 INDEX_ROOT *ir; 749 INDEX_ROOT *ir;
747 char *ir_end, *index_end; 750 u8 *ir_end, *index_end;
748 751
749 /* It is a directory, find index root attribute. */ 752 /* It is a directory, find index root attribute. */
750 ntfs_attr_reinit_search_ctx(ctx); 753 ntfs_attr_reinit_search_ctx(ctx);
@@ -760,17 +763,16 @@ skip_attr_list_load:
760 } 763 }
761 goto unm_err_out; 764 goto unm_err_out;
762 } 765 }
766 a = ctx->attr;
763 /* Set up the state. */ 767 /* Set up the state. */
764 if (unlikely(ctx->attr->non_resident)) { 768 if (unlikely(a->non_resident)) {
765 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not " 769 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not "
766 "resident."); 770 "resident.");
767 goto unm_err_out; 771 goto unm_err_out;
768 } 772 }
769 /* Ensure the attribute name is placed before the value. */ 773 /* Ensure the attribute name is placed before the value. */
770 if (unlikely(ctx->attr->name_length && 774 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
771 (le16_to_cpu(ctx->attr->name_offset) >= 775 le16_to_cpu(a->data.resident.value_offset)))) {
772 le16_to_cpu(ctx->attr->data.resident.
773 value_offset)))) {
774 ntfs_error(vol->sb, "$INDEX_ROOT attribute name is " 776 ntfs_error(vol->sb, "$INDEX_ROOT attribute name is "
775 "placed after the attribute value."); 777 "placed after the attribute value.");
776 goto unm_err_out; 778 goto unm_err_out;
@@ -781,28 +783,27 @@ skip_attr_list_load:
781 * encrypted. However index root cannot be both compressed and 783 * encrypted. However index root cannot be both compressed and
782 * encrypted. 784 * encrypted.
783 */ 785 */
784 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) 786 if (a->flags & ATTR_COMPRESSION_MASK)
785 NInoSetCompressed(ni); 787 NInoSetCompressed(ni);
786 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 788 if (a->flags & ATTR_IS_ENCRYPTED) {
787 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 789 if (a->flags & ATTR_COMPRESSION_MASK) {
788 ntfs_error(vi->i_sb, "Found encrypted and " 790 ntfs_error(vi->i_sb, "Found encrypted and "
789 "compressed attribute."); 791 "compressed attribute.");
790 goto unm_err_out; 792 goto unm_err_out;
791 } 793 }
792 NInoSetEncrypted(ni); 794 NInoSetEncrypted(ni);
793 } 795 }
794 if (ctx->attr->flags & ATTR_IS_SPARSE) 796 if (a->flags & ATTR_IS_SPARSE)
795 NInoSetSparse(ni); 797 NInoSetSparse(ni);
796 ir = (INDEX_ROOT*)((char*)ctx->attr + le16_to_cpu( 798 ir = (INDEX_ROOT*)((u8*)a +
797 ctx->attr->data.resident.value_offset)); 799 le16_to_cpu(a->data.resident.value_offset));
798 ir_end = (char*)ir + le32_to_cpu( 800 ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
799 ctx->attr->data.resident.value_length); 801 if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
800 if (ir_end > (char*)ctx->mrec + vol->mft_record_size) {
801 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is " 802 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
802 "corrupt."); 803 "corrupt.");
803 goto unm_err_out; 804 goto unm_err_out;
804 } 805 }
805 index_end = (char*)&ir->index + 806 index_end = (u8*)&ir->index +
806 le32_to_cpu(ir->index.index_length); 807 le32_to_cpu(ir->index.index_length);
807 if (index_end > ir_end) { 808 if (index_end > ir_end) {
808 ntfs_error(vi->i_sb, "Directory index is corrupt."); 809 ntfs_error(vi->i_sb, "Directory index is corrupt.");
@@ -889,7 +890,8 @@ skip_attr_list_load:
889 "attribute."); 890 "attribute.");
890 goto unm_err_out; 891 goto unm_err_out;
891 } 892 }
892 if (!ctx->attr->non_resident) { 893 a = ctx->attr;
894 if (!a->non_resident) {
893 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 895 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
894 "is resident."); 896 "is resident.");
895 goto unm_err_out; 897 goto unm_err_out;
@@ -898,42 +900,40 @@ skip_attr_list_load:
898 * Ensure the attribute name is placed before the mapping pairs 900 * Ensure the attribute name is placed before the mapping pairs
899 * array. 901 * array.
900 */ 902 */
901 if (unlikely(ctx->attr->name_length && 903 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
902 (le16_to_cpu(ctx->attr->name_offset) >= 904 le16_to_cpu(
903 le16_to_cpu(ctx->attr->data.non_resident. 905 a->data.non_resident.mapping_pairs_offset)))) {
904 mapping_pairs_offset)))) {
905 ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name " 906 ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name "
906 "is placed after the mapping pairs " 907 "is placed after the mapping pairs "
907 "array."); 908 "array.");
908 goto unm_err_out; 909 goto unm_err_out;
909 } 910 }
910 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 911 if (a->flags & ATTR_IS_ENCRYPTED) {
911 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 912 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
912 "is encrypted."); 913 "is encrypted.");
913 goto unm_err_out; 914 goto unm_err_out;
914 } 915 }
915 if (ctx->attr->flags & ATTR_IS_SPARSE) { 916 if (a->flags & ATTR_IS_SPARSE) {
916 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 917 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
917 "is sparse."); 918 "is sparse.");
918 goto unm_err_out; 919 goto unm_err_out;
919 } 920 }
920 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 921 if (a->flags & ATTR_COMPRESSION_MASK) {
921 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 922 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
922 "is compressed."); 923 "is compressed.");
923 goto unm_err_out; 924 goto unm_err_out;
924 } 925 }
925 if (ctx->attr->data.non_resident.lowest_vcn) { 926 if (a->data.non_resident.lowest_vcn) {
926 ntfs_error(vi->i_sb, "First extent of " 927 ntfs_error(vi->i_sb, "First extent of "
927 "$INDEX_ALLOCATION attribute has non " 928 "$INDEX_ALLOCATION attribute has non "
928 "zero lowest_vcn."); 929 "zero lowest_vcn.");
929 goto unm_err_out; 930 goto unm_err_out;
930 } 931 }
931 vi->i_size = sle64_to_cpu( 932 vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
932 ctx->attr->data.non_resident.data_size);
933 ni->initialized_size = sle64_to_cpu( 933 ni->initialized_size = sle64_to_cpu(
934 ctx->attr->data.non_resident.initialized_size); 934 a->data.non_resident.initialized_size);
935 ni->allocated_size = sle64_to_cpu( 935 ni->allocated_size = sle64_to_cpu(
936 ctx->attr->data.non_resident.allocated_size); 936 a->data.non_resident.allocated_size);
937 /* 937 /*
938 * We are done with the mft record, so we release it. Otherwise 938 * We are done with the mft record, so we release it. Otherwise
939 * we would deadlock in ntfs_attr_iget(). 939 * we would deadlock in ntfs_attr_iget().
@@ -958,11 +958,12 @@ skip_attr_list_load:
958 goto unm_err_out; 958 goto unm_err_out;
959 } 959 }
960 /* Consistency check bitmap size vs. index allocation size. */ 960 /* Consistency check bitmap size vs. index allocation size. */
961 if ((bvi->i_size << 3) < (vi->i_size >> 961 bvi_size = i_size_read(bvi);
962 if ((bvi_size << 3) < (vi->i_size >>
962 ni->itype.index.block_size_bits)) { 963 ni->itype.index.block_size_bits)) {
963 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) " 964 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
964 "for index allocation (0x%llx).", 965 "for index allocation (0x%llx).",
965 bvi->i_size << 3, vi->i_size); 966 bvi_size << 3, vi->i_size);
966 goto unm_err_out; 967 goto unm_err_out;
967 } 968 }
968skip_large_dir_stuff: 969skip_large_dir_stuff:
@@ -1010,87 +1011,93 @@ skip_large_dir_stuff:
1010 ntfs_error(vi->i_sb, "$DATA attribute is missing."); 1011 ntfs_error(vi->i_sb, "$DATA attribute is missing.");
1011 goto unm_err_out; 1012 goto unm_err_out;
1012 } 1013 }
1014 a = ctx->attr;
1013 /* Setup the state. */ 1015 /* Setup the state. */
1014 if (ctx->attr->non_resident) { 1016 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
1015 NInoSetNonResident(ni); 1017 if (a->flags & ATTR_COMPRESSION_MASK) {
1016 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1017 NInoSetCompressed(ni); 1018 NInoSetCompressed(ni);
1018 if (vol->cluster_size > 4096) { 1019 if (vol->cluster_size > 4096) {
1019 ntfs_error(vi->i_sb, "Found " 1020 ntfs_error(vi->i_sb, "Found "
1020 "compressed data but " 1021 "compressed data but "
1021 "compression is disabled due " 1022 "compression is "
1022 "to cluster size (%i) > 4kiB.", 1023 "disabled due to "
1023 vol->cluster_size); 1024 "cluster size (%i) > "
1025 "4kiB.",
1026 vol->cluster_size);
1024 goto unm_err_out; 1027 goto unm_err_out;
1025 } 1028 }
1026 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK) 1029 if ((a->flags & ATTR_COMPRESSION_MASK)
1027 != ATTR_IS_COMPRESSED) { 1030 != ATTR_IS_COMPRESSED) {
1028 ntfs_error(vi->i_sb, "Found " 1031 ntfs_error(vi->i_sb, "Found unknown "
1029 "unknown compression method or " 1032 "compression method "
1030 "corrupt file."); 1033 "or corrupt file.");
1031 goto unm_err_out; 1034 goto unm_err_out;
1032 } 1035 }
1033 ni->itype.compressed.block_clusters = 1U << 1036 }
1034 ctx->attr->data.non_resident. 1037 if (a->flags & ATTR_IS_SPARSE)
1035 compression_unit; 1038 NInoSetSparse(ni);
1036 if (ctx->attr->data.non_resident. 1039 }
1037 compression_unit != 4) { 1040 if (a->flags & ATTR_IS_ENCRYPTED) {
1041 if (NInoCompressed(ni)) {
1042 ntfs_error(vi->i_sb, "Found encrypted and "
1043 "compressed data.");
1044 goto unm_err_out;
1045 }
1046 NInoSetEncrypted(ni);
1047 }
1048 if (a->non_resident) {
1049 NInoSetNonResident(ni);
1050 if (NInoCompressed(ni) || NInoSparse(ni)) {
1051 if (a->data.non_resident.compression_unit !=
1052 4) {
1038 ntfs_error(vi->i_sb, "Found " 1053 ntfs_error(vi->i_sb, "Found "
1039 "nonstandard compression unit " 1054 "nonstandard "
1040 "(%u instead of 4). Cannot " 1055 "compression unit (%u "
1041 "handle this.", 1056 "instead of 4). "
1042 ctx->attr->data.non_resident. 1057 "Cannot handle this.",
1043 compression_unit); 1058 a->data.non_resident.
1059 compression_unit);
1044 err = -EOPNOTSUPP; 1060 err = -EOPNOTSUPP;
1045 goto unm_err_out; 1061 goto unm_err_out;
1046 } 1062 }
1063 ni->itype.compressed.block_clusters = 1U <<
1064 a->data.non_resident.
1065 compression_unit;
1047 ni->itype.compressed.block_size = 1U << ( 1066 ni->itype.compressed.block_size = 1U << (
1048 ctx->attr->data.non_resident. 1067 a->data.non_resident.
1049 compression_unit + 1068 compression_unit +
1050 vol->cluster_size_bits); 1069 vol->cluster_size_bits);
1051 ni->itype.compressed.block_size_bits = ffs( 1070 ni->itype.compressed.block_size_bits = ffs(
1052 ni->itype.compressed.block_size) - 1; 1071 ni->itype.compressed.
1053 } 1072 block_size) - 1;
1054 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 1073 ni->itype.compressed.size = sle64_to_cpu(
1055 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1074 a->data.non_resident.
1056 ntfs_error(vi->i_sb, "Found encrypted " 1075 compressed_size);
1057 "and compressed data.");
1058 goto unm_err_out;
1059 }
1060 NInoSetEncrypted(ni);
1061 } 1076 }
1062 if (ctx->attr->flags & ATTR_IS_SPARSE) 1077 if (a->data.non_resident.lowest_vcn) {
1063 NInoSetSparse(ni);
1064 if (ctx->attr->data.non_resident.lowest_vcn) {
1065 ntfs_error(vi->i_sb, "First extent of $DATA " 1078 ntfs_error(vi->i_sb, "First extent of $DATA "
1066 "attribute has non zero " 1079 "attribute has non zero "
1067 "lowest_vcn."); 1080 "lowest_vcn.");
1068 goto unm_err_out; 1081 goto unm_err_out;
1069 } 1082 }
1070 /* Setup all the sizes. */
1071 vi->i_size = sle64_to_cpu( 1083 vi->i_size = sle64_to_cpu(
1072 ctx->attr->data.non_resident.data_size); 1084 a->data.non_resident.data_size);
1073 ni->initialized_size = sle64_to_cpu( 1085 ni->initialized_size = sle64_to_cpu(
1074 ctx->attr->data.non_resident. 1086 a->data.non_resident.initialized_size);
1075 initialized_size);
1076 ni->allocated_size = sle64_to_cpu( 1087 ni->allocated_size = sle64_to_cpu(
1077 ctx->attr->data.non_resident. 1088 a->data.non_resident.allocated_size);
1078 allocated_size);
1079 if (NInoCompressed(ni)) {
1080 ni->itype.compressed.size = sle64_to_cpu(
1081 ctx->attr->data.non_resident.
1082 compressed_size);
1083 }
1084 } else { /* Resident attribute. */ 1089 } else { /* Resident attribute. */
1085 /* 1090 vi->i_size = ni->initialized_size = le32_to_cpu(
1086 * Make all sizes equal for simplicity in read code 1091 a->data.resident.value_length);
1087 * paths. FIXME: Need to keep this in mind when 1092 ni->allocated_size = le32_to_cpu(a->length) -
1088 * converting to non-resident attribute in write code 1093 le16_to_cpu(
1089 * path. (Probably only affects truncate().) 1094 a->data.resident.value_offset);
1090 */ 1095 if (vi->i_size > ni->allocated_size) {
1091 vi->i_size = ni->initialized_size = ni->allocated_size = 1096 ntfs_error(vi->i_sb, "Resident data attribute "
1092 le32_to_cpu( 1097 "is corrupt (size exceeds "
1093 ctx->attr->data.resident.value_length); 1098 "allocation).");
1099 goto unm_err_out;
1100 }
1094 } 1101 }
1095no_data_attr_special_case: 1102no_data_attr_special_case:
1096 /* We are done with the mft record, so we release it. */ 1103 /* We are done with the mft record, so we release it. */
@@ -1117,11 +1124,10 @@ no_data_attr_special_case:
1117 * sizes of all non-resident attributes present to give us the Linux 1124 * sizes of all non-resident attributes present to give us the Linux
1118 * correct size that should go into i_blocks (after division by 512). 1125 * correct size that should go into i_blocks (after division by 512).
1119 */ 1126 */
1120 if (S_ISDIR(vi->i_mode) || !NInoCompressed(ni)) 1127 if (S_ISREG(vi->i_mode) && (NInoCompressed(ni) || NInoSparse(ni)))
1121 vi->i_blocks = ni->allocated_size >> 9;
1122 else
1123 vi->i_blocks = ni->itype.compressed.size >> 9; 1128 vi->i_blocks = ni->itype.compressed.size >> 9;
1124 1129 else
1130 vi->i_blocks = ni->allocated_size >> 9;
1125 ntfs_debug("Done."); 1131 ntfs_debug("Done.");
1126 return 0; 1132 return 0;
1127 1133
@@ -1166,6 +1172,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1166 ntfs_volume *vol = NTFS_SB(vi->i_sb); 1172 ntfs_volume *vol = NTFS_SB(vi->i_sb);
1167 ntfs_inode *ni, *base_ni; 1173 ntfs_inode *ni, *base_ni;
1168 MFT_RECORD *m; 1174 MFT_RECORD *m;
1175 ATTR_RECORD *a;
1169 ntfs_attr_search_ctx *ctx; 1176 ntfs_attr_search_ctx *ctx;
1170 int err = 0; 1177 int err = 0;
1171 1178
@@ -1200,61 +1207,14 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1200 err = -ENOMEM; 1207 err = -ENOMEM;
1201 goto unm_err_out; 1208 goto unm_err_out;
1202 } 1209 }
1203
1204 /* Find the attribute. */ 1210 /* Find the attribute. */
1205 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1211 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1206 CASE_SENSITIVE, 0, NULL, 0, ctx); 1212 CASE_SENSITIVE, 0, NULL, 0, ctx);
1207 if (unlikely(err)) 1213 if (unlikely(err))
1208 goto unm_err_out; 1214 goto unm_err_out;
1209 1215 a = ctx->attr;
1210 if (!ctx->attr->non_resident) { 1216 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
1211 /* Ensure the attribute name is placed before the value. */ 1217 if (a->flags & ATTR_COMPRESSION_MASK) {
1212 if (unlikely(ctx->attr->name_length &&
1213 (le16_to_cpu(ctx->attr->name_offset) >=
1214 le16_to_cpu(ctx->attr->data.resident.
1215 value_offset)))) {
1216 ntfs_error(vol->sb, "Attribute name is placed after "
1217 "the attribute value.");
1218 goto unm_err_out;
1219 }
1220 if (NInoMstProtected(ni) || ctx->attr->flags) {
1221 ntfs_error(vi->i_sb, "Found mst protected attribute "
1222 "or attribute with non-zero flags but "
1223 "the attribute is resident. Please "
1224 "report you saw this message to "
1225 "linux-ntfs-dev@lists.sourceforge.net");
1226 goto unm_err_out;
1227 }
1228 /*
1229 * Resident attribute. Make all sizes equal for simplicity in
1230 * read code paths.
1231 */
1232 vi->i_size = ni->initialized_size = ni->allocated_size =
1233 le32_to_cpu(ctx->attr->data.resident.value_length);
1234 } else {
1235 NInoSetNonResident(ni);
1236 /*
1237 * Ensure the attribute name is placed before the mapping pairs
1238 * array.
1239 */
1240 if (unlikely(ctx->attr->name_length &&
1241 (le16_to_cpu(ctx->attr->name_offset) >=
1242 le16_to_cpu(ctx->attr->data.non_resident.
1243 mapping_pairs_offset)))) {
1244 ntfs_error(vol->sb, "Attribute name is placed after "
1245 "the mapping pairs array.");
1246 goto unm_err_out;
1247 }
1248 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1249 if (NInoMstProtected(ni)) {
1250 ntfs_error(vi->i_sb, "Found mst protected "
1251 "attribute but the attribute "
1252 "is compressed. Please report "
1253 "you saw this message to "
1254 "linux-ntfs-dev@lists."
1255 "sourceforge.net");
1256 goto unm_err_out;
1257 }
1258 NInoSetCompressed(ni); 1218 NInoSetCompressed(ni);
1259 if ((ni->type != AT_DATA) || (ni->type == AT_DATA && 1219 if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
1260 ni->name_len)) { 1220 ni->name_len)) {
@@ -1274,80 +1234,123 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1274 vol->cluster_size); 1234 vol->cluster_size);
1275 goto unm_err_out; 1235 goto unm_err_out;
1276 } 1236 }
1277 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK) 1237 if ((a->flags & ATTR_COMPRESSION_MASK) !=
1278 != ATTR_IS_COMPRESSED) { 1238 ATTR_IS_COMPRESSED) {
1279 ntfs_error(vi->i_sb, "Found unknown " 1239 ntfs_error(vi->i_sb, "Found unknown "
1280 "compression method."); 1240 "compression method.");
1281 goto unm_err_out; 1241 goto unm_err_out;
1282 } 1242 }
1283 ni->itype.compressed.block_clusters = 1U << 1243 }
1284 ctx->attr->data.non_resident. 1244 /*
1285 compression_unit; 1245 * The encryption flag set in an index root just means to
1286 if (ctx->attr->data.non_resident.compression_unit != 1246 * compress all files.
1287 4) { 1247 */
1248 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
1249 ntfs_error(vi->i_sb, "Found mst protected attribute "
1250 "but the attribute is %s. Please "
1251 "report you saw this message to "
1252 "linux-ntfs-dev@lists.sourceforge.net",
1253 NInoCompressed(ni) ? "compressed" :
1254 "sparse");
1255 goto unm_err_out;
1256 }
1257 if (a->flags & ATTR_IS_SPARSE)
1258 NInoSetSparse(ni);
1259 }
1260 if (a->flags & ATTR_IS_ENCRYPTED) {
1261 if (NInoCompressed(ni)) {
1262 ntfs_error(vi->i_sb, "Found encrypted and compressed "
1263 "data.");
1264 goto unm_err_out;
1265 }
1266 /*
1267 * The encryption flag set in an index root just means to
1268 * encrypt all files.
1269 */
1270 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
1271 ntfs_error(vi->i_sb, "Found mst protected attribute "
1272 "but the attribute is encrypted. "
1273 "Please report you saw this message "
1274 "to linux-ntfs-dev@lists.sourceforge."
1275 "net");
1276 goto unm_err_out;
1277 }
1278 if (ni->type != AT_DATA) {
1279 ntfs_error(vi->i_sb, "Found encrypted non-data "
1280 "attribute.");
1281 goto unm_err_out;
1282 }
1283 NInoSetEncrypted(ni);
1284 }
1285 if (!a->non_resident) {
1286 /* Ensure the attribute name is placed before the value. */
1287 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1288 le16_to_cpu(a->data.resident.value_offset)))) {
1289 ntfs_error(vol->sb, "Attribute name is placed after "
1290 "the attribute value.");
1291 goto unm_err_out;
1292 }
1293 if (NInoMstProtected(ni)) {
1294 ntfs_error(vi->i_sb, "Found mst protected attribute "
1295 "but the attribute is resident. "
1296 "Please report you saw this message to "
1297 "linux-ntfs-dev@lists.sourceforge.net");
1298 goto unm_err_out;
1299 }
1300 vi->i_size = ni->initialized_size = le32_to_cpu(
1301 a->data.resident.value_length);
1302 ni->allocated_size = le32_to_cpu(a->length) -
1303 le16_to_cpu(a->data.resident.value_offset);
1304 if (vi->i_size > ni->allocated_size) {
1305 ntfs_error(vi->i_sb, "Resident attribute is corrupt "
1306 "(size exceeds allocation).");
1307 goto unm_err_out;
1308 }
1309 } else {
1310 NInoSetNonResident(ni);
1311 /*
1312 * Ensure the attribute name is placed before the mapping pairs
1313 * array.
1314 */
1315 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1316 le16_to_cpu(
1317 a->data.non_resident.mapping_pairs_offset)))) {
1318 ntfs_error(vol->sb, "Attribute name is placed after "
1319 "the mapping pairs array.");
1320 goto unm_err_out;
1321 }
1322 if ((NInoCompressed(ni) || NInoSparse(ni)) &&
1323 ni->type != AT_INDEX_ROOT) {
1324 if (a->data.non_resident.compression_unit != 4) {
1288 ntfs_error(vi->i_sb, "Found nonstandard " 1325 ntfs_error(vi->i_sb, "Found nonstandard "
1289 "compression unit (%u instead " 1326 "compression unit (%u instead "
1290 "of 4). Cannot handle this.", 1327 "of 4). Cannot handle this.",
1291 ctx->attr->data.non_resident. 1328 a->data.non_resident.
1292 compression_unit); 1329 compression_unit);
1293 err = -EOPNOTSUPP; 1330 err = -EOPNOTSUPP;
1294 goto unm_err_out; 1331 goto unm_err_out;
1295 } 1332 }
1333 ni->itype.compressed.block_clusters = 1U <<
1334 a->data.non_resident.compression_unit;
1296 ni->itype.compressed.block_size = 1U << ( 1335 ni->itype.compressed.block_size = 1U << (
1297 ctx->attr->data.non_resident. 1336 a->data.non_resident.compression_unit +
1298 compression_unit +
1299 vol->cluster_size_bits); 1337 vol->cluster_size_bits);
1300 ni->itype.compressed.block_size_bits = ffs( 1338 ni->itype.compressed.block_size_bits = ffs(
1301 ni->itype.compressed.block_size) - 1; 1339 ni->itype.compressed.block_size) - 1;
1302 } 1340 ni->itype.compressed.size = sle64_to_cpu(
1303 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 1341 a->data.non_resident.compressed_size);
1304 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1305 ntfs_error(vi->i_sb, "Found encrypted "
1306 "and compressed data.");
1307 goto unm_err_out;
1308 }
1309 if (NInoMstProtected(ni)) {
1310 ntfs_error(vi->i_sb, "Found mst protected "
1311 "attribute but the attribute "
1312 "is encrypted. Please report "
1313 "you saw this message to "
1314 "linux-ntfs-dev@lists."
1315 "sourceforge.net");
1316 goto unm_err_out;
1317 }
1318 NInoSetEncrypted(ni);
1319 }
1320 if (ctx->attr->flags & ATTR_IS_SPARSE) {
1321 if (NInoMstProtected(ni)) {
1322 ntfs_error(vi->i_sb, "Found mst protected "
1323 "attribute but the attribute "
1324 "is sparse. Please report "
1325 "you saw this message to "
1326 "linux-ntfs-dev@lists."
1327 "sourceforge.net");
1328 goto unm_err_out;
1329 }
1330 NInoSetSparse(ni);
1331 } 1342 }
1332 if (ctx->attr->data.non_resident.lowest_vcn) { 1343 if (a->data.non_resident.lowest_vcn) {
1333 ntfs_error(vi->i_sb, "First extent of attribute has " 1344 ntfs_error(vi->i_sb, "First extent of attribute has "
1334 "non-zero lowest_vcn."); 1345 "non-zero lowest_vcn.");
1335 goto unm_err_out; 1346 goto unm_err_out;
1336 } 1347 }
1337 /* Setup all the sizes. */ 1348 vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
1338 vi->i_size = sle64_to_cpu(
1339 ctx->attr->data.non_resident.data_size);
1340 ni->initialized_size = sle64_to_cpu( 1349 ni->initialized_size = sle64_to_cpu(
1341 ctx->attr->data.non_resident.initialized_size); 1350 a->data.non_resident.initialized_size);
1342 ni->allocated_size = sle64_to_cpu( 1351 ni->allocated_size = sle64_to_cpu(
1343 ctx->attr->data.non_resident.allocated_size); 1352 a->data.non_resident.allocated_size);
1344 if (NInoCompressed(ni)) {
1345 ni->itype.compressed.size = sle64_to_cpu(
1346 ctx->attr->data.non_resident.
1347 compressed_size);
1348 }
1349 } 1353 }
1350
1351 /* Setup the operations for this attribute inode. */ 1354 /* Setup the operations for this attribute inode. */
1352 vi->i_op = NULL; 1355 vi->i_op = NULL;
1353 vi->i_fop = NULL; 1356 vi->i_fop = NULL;
@@ -1355,14 +1358,12 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1355 vi->i_mapping->a_ops = &ntfs_mst_aops; 1358 vi->i_mapping->a_ops = &ntfs_mst_aops;
1356 else 1359 else
1357 vi->i_mapping->a_ops = &ntfs_aops; 1360 vi->i_mapping->a_ops = &ntfs_aops;
1358 1361 if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT)
1359 if (!NInoCompressed(ni))
1360 vi->i_blocks = ni->allocated_size >> 9;
1361 else
1362 vi->i_blocks = ni->itype.compressed.size >> 9; 1362 vi->i_blocks = ni->itype.compressed.size >> 9;
1363 1363 else
1364 vi->i_blocks = ni->allocated_size >> 9;
1364 /* 1365 /*
1365 * Make sure the base inode doesn't go away and attach it to the 1366 * Make sure the base inode does not go away and attach it to the
1366 * attribute inode. 1367 * attribute inode.
1367 */ 1368 */
1368 igrab(base_vi); 1369 igrab(base_vi);
@@ -1429,10 +1430,12 @@ err_out:
1429 */ 1430 */
1430static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi) 1431static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1431{ 1432{
1433 loff_t bvi_size;
1432 ntfs_volume *vol = NTFS_SB(vi->i_sb); 1434 ntfs_volume *vol = NTFS_SB(vi->i_sb);
1433 ntfs_inode *ni, *base_ni, *bni; 1435 ntfs_inode *ni, *base_ni, *bni;
1434 struct inode *bvi; 1436 struct inode *bvi;
1435 MFT_RECORD *m; 1437 MFT_RECORD *m;
1438 ATTR_RECORD *a;
1436 ntfs_attr_search_ctx *ctx; 1439 ntfs_attr_search_ctx *ctx;
1437 INDEX_ROOT *ir; 1440 INDEX_ROOT *ir;
1438 u8 *ir_end, *index_end; 1441 u8 *ir_end, *index_end;
@@ -1474,30 +1477,31 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1474 "missing."); 1477 "missing.");
1475 goto unm_err_out; 1478 goto unm_err_out;
1476 } 1479 }
1480 a = ctx->attr;
1477 /* Set up the state. */ 1481 /* Set up the state. */
1478 if (unlikely(ctx->attr->non_resident)) { 1482 if (unlikely(a->non_resident)) {
1479 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident."); 1483 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
1480 goto unm_err_out; 1484 goto unm_err_out;
1481 } 1485 }
1482 /* Ensure the attribute name is placed before the value. */ 1486 /* Ensure the attribute name is placed before the value. */
1483 if (unlikely(ctx->attr->name_length && 1487 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1484 (le16_to_cpu(ctx->attr->name_offset) >= 1488 le16_to_cpu(a->data.resident.value_offset)))) {
1485 le16_to_cpu(ctx->attr->data.resident.
1486 value_offset)))) {
1487 ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed " 1489 ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
1488 "after the attribute value."); 1490 "after the attribute value.");
1489 goto unm_err_out; 1491 goto unm_err_out;
1490 } 1492 }
1491 /* Compressed/encrypted/sparse index root is not allowed. */ 1493 /*
1492 if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED | 1494 * Compressed/encrypted/sparse index root is not allowed, except for
1495 * directories of course but those are not dealt with here.
1496 */
1497 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
1493 ATTR_IS_SPARSE)) { 1498 ATTR_IS_SPARSE)) {
1494 ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index " 1499 ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
1495 "root attribute."); 1500 "root attribute.");
1496 goto unm_err_out; 1501 goto unm_err_out;
1497 } 1502 }
1498 ir = (INDEX_ROOT*)((u8*)ctx->attr + 1503 ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset));
1499 le16_to_cpu(ctx->attr->data.resident.value_offset)); 1504 ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
1500 ir_end = (u8*)ir + le32_to_cpu(ctx->attr->data.resident.value_length);
1501 if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) { 1505 if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
1502 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt."); 1506 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
1503 goto unm_err_out; 1507 goto unm_err_out;
@@ -1570,7 +1574,7 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1570 "$INDEX_ALLOCATION attribute."); 1574 "$INDEX_ALLOCATION attribute.");
1571 goto unm_err_out; 1575 goto unm_err_out;
1572 } 1576 }
1573 if (!ctx->attr->non_resident) { 1577 if (!a->non_resident) {
1574 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1578 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1575 "resident."); 1579 "resident.");
1576 goto unm_err_out; 1580 goto unm_err_out;
@@ -1578,37 +1582,36 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1578 /* 1582 /*
1579 * Ensure the attribute name is placed before the mapping pairs array. 1583 * Ensure the attribute name is placed before the mapping pairs array.
1580 */ 1584 */
1581 if (unlikely(ctx->attr->name_length && (le16_to_cpu( 1585 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1582 ctx->attr->name_offset) >= le16_to_cpu( 1586 le16_to_cpu(
1583 ctx->attr->data.non_resident.mapping_pairs_offset)))) { 1587 a->data.non_resident.mapping_pairs_offset)))) {
1584 ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is " 1588 ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
1585 "placed after the mapping pairs array."); 1589 "placed after the mapping pairs array.");
1586 goto unm_err_out; 1590 goto unm_err_out;
1587 } 1591 }
1588 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 1592 if (a->flags & ATTR_IS_ENCRYPTED) {
1589 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1593 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1590 "encrypted."); 1594 "encrypted.");
1591 goto unm_err_out; 1595 goto unm_err_out;
1592 } 1596 }
1593 if (ctx->attr->flags & ATTR_IS_SPARSE) { 1597 if (a->flags & ATTR_IS_SPARSE) {
1594 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse."); 1598 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
1595 goto unm_err_out; 1599 goto unm_err_out;
1596 } 1600 }
1597 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1601 if (a->flags & ATTR_COMPRESSION_MASK) {
1598 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1602 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1599 "compressed."); 1603 "compressed.");
1600 goto unm_err_out; 1604 goto unm_err_out;
1601 } 1605 }
1602 if (ctx->attr->data.non_resident.lowest_vcn) { 1606 if (a->data.non_resident.lowest_vcn) {
1603 ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION " 1607 ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
1604 "attribute has non zero lowest_vcn."); 1608 "attribute has non zero lowest_vcn.");
1605 goto unm_err_out; 1609 goto unm_err_out;
1606 } 1610 }
1607 vi->i_size = sle64_to_cpu(ctx->attr->data.non_resident.data_size); 1611 vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
1608 ni->initialized_size = sle64_to_cpu( 1612 ni->initialized_size = sle64_to_cpu(
1609 ctx->attr->data.non_resident.initialized_size); 1613 a->data.non_resident.initialized_size);
1610 ni->allocated_size = sle64_to_cpu( 1614 ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size);
1611 ctx->attr->data.non_resident.allocated_size);
1612 /* 1615 /*
1613 * We are done with the mft record, so we release it. Otherwise 1616 * We are done with the mft record, so we release it. Otherwise
1614 * we would deadlock in ntfs_attr_iget(). 1617 * we would deadlock in ntfs_attr_iget().
@@ -1632,10 +1635,10 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1632 goto iput_unm_err_out; 1635 goto iput_unm_err_out;
1633 } 1636 }
1634 /* Consistency check bitmap size vs. index allocation size. */ 1637 /* Consistency check bitmap size vs. index allocation size. */
1635 if ((bvi->i_size << 3) < (vi->i_size >> 1638 bvi_size = i_size_read(bvi);
1636 ni->itype.index.block_size_bits)) { 1639 if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) {
1637 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for " 1640 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
1638 "index allocation (0x%llx).", bvi->i_size << 3, 1641 "index allocation (0x%llx).", bvi_size << 3,
1639 vi->i_size); 1642 vi->i_size);
1640 goto iput_unm_err_out; 1643 goto iput_unm_err_out;
1641 } 1644 }
@@ -1646,7 +1649,6 @@ skip_large_index_stuff:
1646 vi->i_fop = NULL; 1649 vi->i_fop = NULL;
1647 vi->i_mapping->a_ops = &ntfs_mst_aops; 1650 vi->i_mapping->a_ops = &ntfs_mst_aops;
1648 vi->i_blocks = ni->allocated_size >> 9; 1651 vi->i_blocks = ni->allocated_size >> 9;
1649
1650 /* 1652 /*
1651 * Make sure the base inode doesn't go away and attach it to the 1653 * Make sure the base inode doesn't go away and attach it to the
1652 * index inode. 1654 * index inode.
@@ -1712,7 +1714,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1712 struct buffer_head *bh; 1714 struct buffer_head *bh;
1713 ntfs_inode *ni; 1715 ntfs_inode *ni;
1714 MFT_RECORD *m = NULL; 1716 MFT_RECORD *m = NULL;
1715 ATTR_RECORD *attr; 1717 ATTR_RECORD *a;
1716 ntfs_attr_search_ctx *ctx; 1718 ntfs_attr_search_ctx *ctx;
1717 unsigned int i, nr_blocks; 1719 unsigned int i, nr_blocks;
1718 int err; 1720 int err;
@@ -1727,10 +1729,10 @@ int ntfs_read_inode_mount(struct inode *vi)
1727 /* Setup the data attribute. It is special as it is mst protected. */ 1729 /* Setup the data attribute. It is special as it is mst protected. */
1728 NInoSetNonResident(ni); 1730 NInoSetNonResident(ni);
1729 NInoSetMstProtected(ni); 1731 NInoSetMstProtected(ni);
1732 NInoSetSparseDisabled(ni);
1730 ni->type = AT_DATA; 1733 ni->type = AT_DATA;
1731 ni->name = NULL; 1734 ni->name = NULL;
1732 ni->name_len = 0; 1735 ni->name_len = 0;
1733
1734 /* 1736 /*
1735 * This sets up our little cheat allowing us to reuse the async read io 1737 * This sets up our little cheat allowing us to reuse the async read io
1736 * completion handler for directories. 1738 * completion handler for directories.
@@ -1808,9 +1810,10 @@ int ntfs_read_inode_mount(struct inode *vi)
1808 1810
1809 ntfs_debug("Attribute list attribute found in $MFT."); 1811 ntfs_debug("Attribute list attribute found in $MFT.");
1810 NInoSetAttrList(ni); 1812 NInoSetAttrList(ni);
1811 if (ctx->attr->flags & ATTR_IS_ENCRYPTED || 1813 a = ctx->attr;
1812 ctx->attr->flags & ATTR_COMPRESSION_MASK || 1814 if (a->flags & ATTR_IS_ENCRYPTED ||
1813 ctx->attr->flags & ATTR_IS_SPARSE) { 1815 a->flags & ATTR_COMPRESSION_MASK ||
1816 a->flags & ATTR_IS_SPARSE) {
1814 ntfs_error(sb, "Attribute list attribute is " 1817 ntfs_error(sb, "Attribute list attribute is "
1815 "compressed/encrypted/sparse. Not " 1818 "compressed/encrypted/sparse. Not "
1816 "allowed. $MFT is corrupt. You should " 1819 "allowed. $MFT is corrupt. You should "
@@ -1818,16 +1821,16 @@ int ntfs_read_inode_mount(struct inode *vi)
1818 goto put_err_out; 1821 goto put_err_out;
1819 } 1822 }
1820 /* Now allocate memory for the attribute list. */ 1823 /* Now allocate memory for the attribute list. */
1821 ni->attr_list_size = (u32)ntfs_attr_size(ctx->attr); 1824 ni->attr_list_size = (u32)ntfs_attr_size(a);
1822 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 1825 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
1823 if (!ni->attr_list) { 1826 if (!ni->attr_list) {
1824 ntfs_error(sb, "Not enough memory to allocate buffer " 1827 ntfs_error(sb, "Not enough memory to allocate buffer "
1825 "for attribute list."); 1828 "for attribute list.");
1826 goto put_err_out; 1829 goto put_err_out;
1827 } 1830 }
1828 if (ctx->attr->non_resident) { 1831 if (a->non_resident) {
1829 NInoSetAttrListNonResident(ni); 1832 NInoSetAttrListNonResident(ni);
1830 if (ctx->attr->data.non_resident.lowest_vcn) { 1833 if (a->data.non_resident.lowest_vcn) {
1831 ntfs_error(sb, "Attribute list has non zero " 1834 ntfs_error(sb, "Attribute list has non zero "
1832 "lowest_vcn. $MFT is corrupt. " 1835 "lowest_vcn. $MFT is corrupt. "
1833 "You should run chkdsk."); 1836 "You should run chkdsk.");
@@ -1835,7 +1838,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1835 } 1838 }
1836 /* Setup the runlist. */ 1839 /* Setup the runlist. */
1837 ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 1840 ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
1838 ctx->attr, NULL); 1841 a, NULL);
1839 if (IS_ERR(ni->attr_list_rl.rl)) { 1842 if (IS_ERR(ni->attr_list_rl.rl)) {
1840 err = PTR_ERR(ni->attr_list_rl.rl); 1843 err = PTR_ERR(ni->attr_list_rl.rl);
1841 ni->attr_list_rl.rl = NULL; 1844 ni->attr_list_rl.rl = NULL;
@@ -1847,7 +1850,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1847 /* Now load the attribute list. */ 1850 /* Now load the attribute list. */
1848 if ((err = load_attribute_list(vol, &ni->attr_list_rl, 1851 if ((err = load_attribute_list(vol, &ni->attr_list_rl,
1849 ni->attr_list, ni->attr_list_size, 1852 ni->attr_list, ni->attr_list_size,
1850 sle64_to_cpu(ctx->attr->data. 1853 sle64_to_cpu(a->data.
1851 non_resident.initialized_size)))) { 1854 non_resident.initialized_size)))) {
1852 ntfs_error(sb, "Failed to load attribute list " 1855 ntfs_error(sb, "Failed to load attribute list "
1853 "attribute with error code %i.", 1856 "attribute with error code %i.",
@@ -1855,20 +1858,20 @@ int ntfs_read_inode_mount(struct inode *vi)
1855 goto put_err_out; 1858 goto put_err_out;
1856 } 1859 }
1857 } else /* if (!ctx.attr->non_resident) */ { 1860 } else /* if (!ctx.attr->non_resident) */ {
1858 if ((u8*)ctx->attr + le16_to_cpu( 1861 if ((u8*)a + le16_to_cpu(
1859 ctx->attr->data.resident.value_offset) + 1862 a->data.resident.value_offset) +
1860 le32_to_cpu( 1863 le32_to_cpu(
1861 ctx->attr->data.resident.value_length) > 1864 a->data.resident.value_length) >
1862 (u8*)ctx->mrec + vol->mft_record_size) { 1865 (u8*)ctx->mrec + vol->mft_record_size) {
1863 ntfs_error(sb, "Corrupt attribute list " 1866 ntfs_error(sb, "Corrupt attribute list "
1864 "attribute."); 1867 "attribute.");
1865 goto put_err_out; 1868 goto put_err_out;
1866 } 1869 }
1867 /* Now copy the attribute list. */ 1870 /* Now copy the attribute list. */
1868 memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu( 1871 memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
1869 ctx->attr->data.resident.value_offset), 1872 a->data.resident.value_offset),
1870 le32_to_cpu( 1873 le32_to_cpu(
1871 ctx->attr->data.resident.value_length)); 1874 a->data.resident.value_length));
1872 } 1875 }
1873 /* The attribute list is now setup in memory. */ 1876 /* The attribute list is now setup in memory. */
1874 /* 1877 /*
@@ -1934,25 +1937,25 @@ int ntfs_read_inode_mount(struct inode *vi)
1934 ntfs_attr_reinit_search_ctx(ctx); 1937 ntfs_attr_reinit_search_ctx(ctx);
1935 1938
1936 /* Now load all attribute extents. */ 1939 /* Now load all attribute extents. */
1937 attr = NULL; 1940 a = NULL;
1938 next_vcn = last_vcn = highest_vcn = 0; 1941 next_vcn = last_vcn = highest_vcn = 0;
1939 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, 1942 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
1940 ctx))) { 1943 ctx))) {
1941 runlist_element *nrl; 1944 runlist_element *nrl;
1942 1945
1943 /* Cache the current attribute. */ 1946 /* Cache the current attribute. */
1944 attr = ctx->attr; 1947 a = ctx->attr;
1945 /* $MFT must be non-resident. */ 1948 /* $MFT must be non-resident. */
1946 if (!attr->non_resident) { 1949 if (!a->non_resident) {
1947 ntfs_error(sb, "$MFT must be non-resident but a " 1950 ntfs_error(sb, "$MFT must be non-resident but a "
1948 "resident extent was found. $MFT is " 1951 "resident extent was found. $MFT is "
1949 "corrupt. Run chkdsk."); 1952 "corrupt. Run chkdsk.");
1950 goto put_err_out; 1953 goto put_err_out;
1951 } 1954 }
1952 /* $MFT must be uncompressed and unencrypted. */ 1955 /* $MFT must be uncompressed and unencrypted. */
1953 if (attr->flags & ATTR_COMPRESSION_MASK || 1956 if (a->flags & ATTR_COMPRESSION_MASK ||
1954 attr->flags & ATTR_IS_ENCRYPTED || 1957 a->flags & ATTR_IS_ENCRYPTED ||
1955 attr->flags & ATTR_IS_SPARSE) { 1958 a->flags & ATTR_IS_SPARSE) {
1956 ntfs_error(sb, "$MFT must be uncompressed, " 1959 ntfs_error(sb, "$MFT must be uncompressed, "
1957 "non-sparse, and unencrypted but a " 1960 "non-sparse, and unencrypted but a "
1958 "compressed/sparse/encrypted extent " 1961 "compressed/sparse/encrypted extent "
@@ -1966,7 +1969,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1966 * as we have exclusive access to the inode at this time and we 1969 * as we have exclusive access to the inode at this time and we
1967 * are a mount in progress task, too. 1970 * are a mount in progress task, too.
1968 */ 1971 */
1969 nrl = ntfs_mapping_pairs_decompress(vol, attr, ni->runlist.rl); 1972 nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
1970 if (IS_ERR(nrl)) { 1973 if (IS_ERR(nrl)) {
1971 ntfs_error(sb, "ntfs_mapping_pairs_decompress() " 1974 ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
1972 "failed with error code %ld. $MFT is " 1975 "failed with error code %ld. $MFT is "
@@ -1977,7 +1980,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1977 1980
1978 /* Are we in the first extent? */ 1981 /* Are we in the first extent? */
1979 if (!next_vcn) { 1982 if (!next_vcn) {
1980 if (attr->data.non_resident.lowest_vcn) { 1983 if (a->data.non_resident.lowest_vcn) {
1981 ntfs_error(sb, "First extent of $DATA " 1984 ntfs_error(sb, "First extent of $DATA "
1982 "attribute has non zero " 1985 "attribute has non zero "
1983 "lowest_vcn. $MFT is corrupt. " 1986 "lowest_vcn. $MFT is corrupt. "
@@ -1986,15 +1989,15 @@ int ntfs_read_inode_mount(struct inode *vi)
1986 } 1989 }
1987 /* Get the last vcn in the $DATA attribute. */ 1990 /* Get the last vcn in the $DATA attribute. */
1988 last_vcn = sle64_to_cpu( 1991 last_vcn = sle64_to_cpu(
1989 attr->data.non_resident.allocated_size) 1992 a->data.non_resident.allocated_size)
1990 >> vol->cluster_size_bits; 1993 >> vol->cluster_size_bits;
1991 /* Fill in the inode size. */ 1994 /* Fill in the inode size. */
1992 vi->i_size = sle64_to_cpu( 1995 vi->i_size = sle64_to_cpu(
1993 attr->data.non_resident.data_size); 1996 a->data.non_resident.data_size);
1994 ni->initialized_size = sle64_to_cpu(attr->data. 1997 ni->initialized_size = sle64_to_cpu(
1995 non_resident.initialized_size); 1998 a->data.non_resident.initialized_size);
1996 ni->allocated_size = sle64_to_cpu( 1999 ni->allocated_size = sle64_to_cpu(
1997 attr->data.non_resident.allocated_size); 2000 a->data.non_resident.allocated_size);
1998 /* 2001 /*
1999 * Verify the number of mft records does not exceed 2002 * Verify the number of mft records does not exceed
2000 * 2^32 - 1. 2003 * 2^32 - 1.
@@ -2051,7 +2054,7 @@ int ntfs_read_inode_mount(struct inode *vi)
2051 } 2054 }
2052 2055
2053 /* Get the lowest vcn for the next extent. */ 2056 /* Get the lowest vcn for the next extent. */
2054 highest_vcn = sle64_to_cpu(attr->data.non_resident.highest_vcn); 2057 highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
2055 next_vcn = highest_vcn + 1; 2058 next_vcn = highest_vcn + 1;
2056 2059
2057 /* Only one extent or error, which we catch below. */ 2060 /* Only one extent or error, which we catch below. */
@@ -2060,7 +2063,7 @@ int ntfs_read_inode_mount(struct inode *vi)
2060 2063
2061 /* Avoid endless loops due to corruption. */ 2064 /* Avoid endless loops due to corruption. */
2062 if (next_vcn < sle64_to_cpu( 2065 if (next_vcn < sle64_to_cpu(
2063 attr->data.non_resident.lowest_vcn)) { 2066 a->data.non_resident.lowest_vcn)) {
2064 ntfs_error(sb, "$MFT has corrupt attribute list " 2067 ntfs_error(sb, "$MFT has corrupt attribute list "
2065 "attribute. Run chkdsk."); 2068 "attribute. Run chkdsk.");
2066 goto put_err_out; 2069 goto put_err_out;
@@ -2071,7 +2074,7 @@ int ntfs_read_inode_mount(struct inode *vi)
2071 "$MFT is corrupt. Run chkdsk."); 2074 "$MFT is corrupt. Run chkdsk.");
2072 goto put_err_out; 2075 goto put_err_out;
2073 } 2076 }
2074 if (!attr) { 2077 if (!a) {
2075 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is " 2078 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
2076 "corrupt. Run chkdsk."); 2079 "corrupt. Run chkdsk.");
2077 goto put_err_out; 2080 goto put_err_out;
@@ -2275,6 +2278,8 @@ int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
2275 seq_printf(sf, ",case_sensitive"); 2278 seq_printf(sf, ",case_sensitive");
2276 if (NVolShowSystemFiles(vol)) 2279 if (NVolShowSystemFiles(vol))
2277 seq_printf(sf, ",show_sys_files"); 2280 seq_printf(sf, ",show_sys_files");
2281 if (!NVolSparseEnabled(vol))
2282 seq_printf(sf, ",disable_sparse");
2278 for (i = 0; on_errors_arr[i].val; i++) { 2283 for (i = 0; on_errors_arr[i].val; i++) {
2279 if (on_errors_arr[i].val & vol->on_errors) 2284 if (on_errors_arr[i].val & vol->on_errors)
2280 seq_printf(sf, ",errors=%s", on_errors_arr[i].str); 2285 seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
@@ -2311,6 +2316,7 @@ int ntfs_truncate(struct inode *vi)
2311 ntfs_volume *vol = ni->vol; 2316 ntfs_volume *vol = ni->vol;
2312 ntfs_attr_search_ctx *ctx; 2317 ntfs_attr_search_ctx *ctx;
2313 MFT_RECORD *m; 2318 MFT_RECORD *m;
2319 ATTR_RECORD *a;
2314 const char *te = " Leaving file length out of sync with i_size."; 2320 const char *te = " Leaving file length out of sync with i_size.";
2315 int err; 2321 int err;
2316 2322
@@ -2347,14 +2353,15 @@ int ntfs_truncate(struct inode *vi)
2347 vi->i_ino, err); 2353 vi->i_ino, err);
2348 goto err_out; 2354 goto err_out;
2349 } 2355 }
2356 a = ctx->attr;
2350 /* If the size has not changed there is nothing to do. */ 2357 /* If the size has not changed there is nothing to do. */
2351 if (ntfs_attr_size(ctx->attr) == i_size_read(vi)) 2358 if (ntfs_attr_size(a) == i_size_read(vi))
2352 goto done; 2359 goto done;
2353 // TODO: Implement the truncate... 2360 // TODO: Implement the truncate...
2354 ntfs_error(vi->i_sb, "Inode size has changed but this is not " 2361 ntfs_error(vi->i_sb, "Inode size has changed but this is not "
2355 "implemented yet. Resetting inode size to old value. " 2362 "implemented yet. Resetting inode size to old value. "
2356 " This is most likely a bug in the ntfs driver!"); 2363 " This is most likely a bug in the ntfs driver!");
2357 i_size_write(vi, ntfs_attr_size(ctx->attr)); 2364 i_size_write(vi, ntfs_attr_size(a));
2358done: 2365done:
2359 ntfs_attr_put_search_ctx(ctx); 2366 ntfs_attr_put_search_ctx(ctx);
2360 unmap_mft_record(ni); 2367 unmap_mft_record(ni);
@@ -2436,16 +2443,18 @@ int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
2436 * We skipped the truncate but must still update 2443 * We skipped the truncate but must still update
2437 * timestamps. 2444 * timestamps.
2438 */ 2445 */
2439 ia_valid |= ATTR_MTIME|ATTR_CTIME; 2446 ia_valid |= ATTR_MTIME | ATTR_CTIME;
2440 } 2447 }
2441 } 2448 }
2442
2443 if (ia_valid & ATTR_ATIME) 2449 if (ia_valid & ATTR_ATIME)
2444 vi->i_atime = attr->ia_atime; 2450 vi->i_atime = timespec_trunc(attr->ia_atime,
2451 vi->i_sb->s_time_gran);
2445 if (ia_valid & ATTR_MTIME) 2452 if (ia_valid & ATTR_MTIME)
2446 vi->i_mtime = attr->ia_mtime; 2453 vi->i_mtime = timespec_trunc(attr->ia_mtime,
2454 vi->i_sb->s_time_gran);
2447 if (ia_valid & ATTR_CTIME) 2455 if (ia_valid & ATTR_CTIME)
2448 vi->i_ctime = attr->ia_ctime; 2456 vi->i_ctime = timespec_trunc(attr->ia_ctime,
2457 vi->i_sb->s_time_gran);
2449 mark_inode_dirty(vi); 2458 mark_inode_dirty(vi);
2450out: 2459out:
2451 return err; 2460 return err;
@@ -2515,18 +2524,18 @@ int ntfs_write_inode(struct inode *vi, int sync)
2515 nt = utc2ntfs(vi->i_mtime); 2524 nt = utc2ntfs(vi->i_mtime);
2516 if (si->last_data_change_time != nt) { 2525 if (si->last_data_change_time != nt) {
2517 ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, " 2526 ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
2518 "new = 0x%llx", vi->i_ino, 2527 "new = 0x%llx", vi->i_ino, (long long)
2519 sle64_to_cpu(si->last_data_change_time), 2528 sle64_to_cpu(si->last_data_change_time),
2520 sle64_to_cpu(nt)); 2529 (long long)sle64_to_cpu(nt));
2521 si->last_data_change_time = nt; 2530 si->last_data_change_time = nt;
2522 modified = TRUE; 2531 modified = TRUE;
2523 } 2532 }
2524 nt = utc2ntfs(vi->i_ctime); 2533 nt = utc2ntfs(vi->i_ctime);
2525 if (si->last_mft_change_time != nt) { 2534 if (si->last_mft_change_time != nt) {
2526 ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, " 2535 ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
2527 "new = 0x%llx", vi->i_ino, 2536 "new = 0x%llx", vi->i_ino, (long long)
2528 sle64_to_cpu(si->last_mft_change_time), 2537 sle64_to_cpu(si->last_mft_change_time),
2529 sle64_to_cpu(nt)); 2538 (long long)sle64_to_cpu(nt));
2530 si->last_mft_change_time = nt; 2539 si->last_mft_change_time = nt;
2531 modified = TRUE; 2540 modified = TRUE;
2532 } 2541 }
@@ -2534,8 +2543,8 @@ int ntfs_write_inode(struct inode *vi, int sync)
2534 if (si->last_access_time != nt) { 2543 if (si->last_access_time != nt) {
2535 ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, " 2544 ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
2536 "new = 0x%llx", vi->i_ino, 2545 "new = 0x%llx", vi->i_ino,
2537 sle64_to_cpu(si->last_access_time), 2546 (long long)sle64_to_cpu(si->last_access_time),
2538 sle64_to_cpu(nt)); 2547 (long long)sle64_to_cpu(nt));
2539 si->last_access_time = nt; 2548 si->last_access_time = nt;
2540 modified = TRUE; 2549 modified = TRUE;
2541 } 2550 }
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index 99580455f2ed..3de5c0231966 100644
--- a/fs/ntfs/inode.h
+++ b/fs/ntfs/inode.h
@@ -2,7 +2,7 @@
2 * inode.h - Defines for inode structures NTFS Linux kernel driver. Part of 2 * inode.h - Defines for inode structures NTFS Linux kernel driver. Part of
3 * the Linux-NTFS project. 3 * the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -44,6 +44,7 @@ typedef struct _ntfs_inode ntfs_inode;
44 * fields already provided in the VFS inode. 44 * fields already provided in the VFS inode.
45 */ 45 */
46struct _ntfs_inode { 46struct _ntfs_inode {
47 rwlock_t size_lock; /* Lock serializing access to inode sizes. */
47 s64 initialized_size; /* Copy from the attribute record. */ 48 s64 initialized_size; /* Copy from the attribute record. */
48 s64 allocated_size; /* Copy from the attribute record. */ 49 s64 allocated_size; /* Copy from the attribute record. */
49 unsigned long state; /* NTFS specific flags describing this inode. 50 unsigned long state; /* NTFS specific flags describing this inode.
@@ -109,7 +110,7 @@ struct _ntfs_inode {
109 u8 block_size_bits; /* Log2 of the above. */ 110 u8 block_size_bits; /* Log2 of the above. */
110 u8 vcn_size_bits; /* Log2 of the above. */ 111 u8 vcn_size_bits; /* Log2 of the above. */
111 } index; 112 } index;
112 struct { /* It is a compressed file or an attribute inode. */ 113 struct { /* It is a compressed/sparse file/attribute inode. */
113 s64 size; /* Copy of compressed_size from 114 s64 size; /* Copy of compressed_size from
114 $DATA. */ 115 $DATA. */
115 u32 block_size; /* Size of a compression block 116 u32 block_size; /* Size of a compression block
@@ -165,6 +166,7 @@ typedef enum {
165 NI_Sparse, /* 1: Unnamed data attr is sparse (f). 166 NI_Sparse, /* 1: Unnamed data attr is sparse (f).
166 1: Create sparse files by default (d). 167 1: Create sparse files by default (d).
167 1: Attribute is sparse (a). */ 168 1: Attribute is sparse (a). */
169 NI_SparseDisabled, /* 1: May not create sparse regions. */
168 NI_TruncateFailed, /* 1: Last ntfs_truncate() call failed. */ 170 NI_TruncateFailed, /* 1: Last ntfs_truncate() call failed. */
169} ntfs_inode_state_bits; 171} ntfs_inode_state_bits;
170 172
@@ -217,6 +219,7 @@ NINO_FNS(IndexAllocPresent)
217NINO_FNS(Compressed) 219NINO_FNS(Compressed)
218NINO_FNS(Encrypted) 220NINO_FNS(Encrypted)
219NINO_FNS(Sparse) 221NINO_FNS(Sparse)
222NINO_FNS(SparseDisabled)
220NINO_FNS(TruncateFailed) 223NINO_FNS(TruncateFailed)
221 224
222/* 225/*
diff --git a/fs/ntfs/layout.h b/fs/ntfs/layout.h
index 47b338999921..609ad1728ce4 100644
--- a/fs/ntfs/layout.h
+++ b/fs/ntfs/layout.h
@@ -2,7 +2,7 @@
2 * layout.h - All NTFS associated on-disk structures. Part of the Linux-NTFS 2 * layout.h - All NTFS associated on-disk structures. Part of the Linux-NTFS
3 * project. 3 * project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -547,26 +547,44 @@ enum {
547 COLLATION_NTOFS_ULONG = const_cpu_to_le32(0x10), 547 COLLATION_NTOFS_ULONG = const_cpu_to_le32(0x10),
548 COLLATION_NTOFS_SID = const_cpu_to_le32(0x11), 548 COLLATION_NTOFS_SID = const_cpu_to_le32(0x11),
549 COLLATION_NTOFS_SECURITY_HASH = const_cpu_to_le32(0x12), 549 COLLATION_NTOFS_SECURITY_HASH = const_cpu_to_le32(0x12),
550 COLLATION_NTOFS_ULONGS = const_cpu_to_le32(0x13) 550 COLLATION_NTOFS_ULONGS = const_cpu_to_le32(0x13),
551}; 551};
552 552
553typedef le32 COLLATION_RULE; 553typedef le32 COLLATION_RULE;
554 554
555/* 555/*
556 * The flags (32-bit) describing attribute properties in the attribute 556 * The flags (32-bit) describing attribute properties in the attribute
557 * definition structure. FIXME: This information is from Regis's information 557 * definition structure. FIXME: This information is based on Regis's
558 * and, according to him, it is not certain and probably incomplete. 558 * information and, according to him, it is not certain and probably
559 * The INDEXABLE flag is fairly certainly correct as only the file name 559 * incomplete. The INDEXABLE flag is fairly certainly correct as only the file
560 * attribute has this flag set and this is the only attribute indexed in NT4. 560 * name attribute has this flag set and this is the only attribute indexed in
561 * NT4.
561 */ 562 */
562enum { 563enum {
563 INDEXABLE = const_cpu_to_le32(0x02), /* Attribute can be 564 ATTR_DEF_INDEXABLE = const_cpu_to_le32(0x02), /* Attribute can be
564 indexed. */ 565 indexed. */
565 NEED_TO_REGENERATE = const_cpu_to_le32(0x40), /* Need to regenerate 566 ATTR_DEF_MULTIPLE = const_cpu_to_le32(0x04), /* Attribute type
566 during regeneration 567 can be present multiple times in the
567 phase. */ 568 mft records of an inode. */
568 CAN_BE_NON_RESIDENT = const_cpu_to_le32(0x80), /* Attribute can be 569 ATTR_DEF_NOT_ZERO = const_cpu_to_le32(0x08), /* Attribute value
569 non-resident. */ 570 must contain at least one non-zero
571 byte. */
572 ATTR_DEF_INDEXED_UNIQUE = const_cpu_to_le32(0x10), /* Attribute must be
573 indexed and the attribute value must be
574 unique for the attribute type in all of
575 the mft records of an inode. */
576 ATTR_DEF_NAMED_UNIQUE = const_cpu_to_le32(0x20), /* Attribute must be
577 named and the name must be unique for
578 the attribute type in all of the mft
579 records of an inode. */
580 ATTR_DEF_RESIDENT = const_cpu_to_le32(0x40), /* Attribute must be
581 resident. */
582 ATTR_DEF_ALWAYS_LOG = const_cpu_to_le32(0x80), /* Always log
583 modifications to this attribute,
584 regardless of whether it is resident or
585 non-resident. Without this, only log
586 modifications if the attribute is
587 resident. */
570}; 588};
571 589
572typedef le32 ATTR_DEF_FLAGS; 590typedef le32 ATTR_DEF_FLAGS;
@@ -749,10 +767,11 @@ typedef struct {
749 record header aligned to 8-byte boundary. */ 767 record header aligned to 8-byte boundary. */
750/* 34*/ u8 compression_unit; /* The compression unit expressed 768/* 34*/ u8 compression_unit; /* The compression unit expressed
751 as the log to the base 2 of the number of 769 as the log to the base 2 of the number of
752 clusters in a compression unit. 0 means not 770 clusters in a compression unit. 0 means not
753 compressed. (This effectively limits the 771 compressed. (This effectively limits the
754 compression unit size to be a power of two 772 compression unit size to be a power of two
755 clusters.) WinNT4 only uses a value of 4. */ 773 clusters.) WinNT4 only uses a value of 4.
774 Sparse files also have this set to 4. */
756/* 35*/ u8 reserved[5]; /* Align to 8-byte boundary. */ 775/* 35*/ u8 reserved[5]; /* Align to 8-byte boundary. */
757/* The sizes below are only used when lowest_vcn is zero, as otherwise it would 776/* The sizes below are only used when lowest_vcn is zero, as otherwise it would
758 be difficult to keep them up-to-date.*/ 777 be difficult to keep them up-to-date.*/
@@ -772,10 +791,10 @@ typedef struct {
772 data_size. */ 791 data_size. */
773/* sizeof(uncompressed attr) = 64*/ 792/* sizeof(uncompressed attr) = 64*/
774/* 64*/ sle64 compressed_size; /* Byte size of the attribute 793/* 64*/ sle64 compressed_size; /* Byte size of the attribute
775 value after compression. Only present when 794 value after compression. Only present when
776 compressed. Always is a multiple of the 795 compressed or sparse. Always is a multiple of
777 cluster size. Represents the actual amount of 796 the cluster size. Represents the actual amount
778 disk space being used on the disk. */ 797 of disk space being used on the disk. */
779/* sizeof(compressed attr) = 72*/ 798/* sizeof(compressed attr) = 72*/
780 } __attribute__ ((__packed__)) non_resident; 799 } __attribute__ ((__packed__)) non_resident;
781 } __attribute__ ((__packed__)) data; 800 } __attribute__ ((__packed__)) data;
@@ -834,7 +853,7 @@ enum {
834 /* Note, this is a copy of the corresponding bit from the mft record, 853 /* Note, this is a copy of the corresponding bit from the mft record,
835 telling us whether this file has a view index present (eg. object id 854 telling us whether this file has a view index present (eg. object id
836 index, quota index, one of the security indexes or the encrypting 855 index, quota index, one of the security indexes or the encrypting
837 file system related indexes). */ 856 filesystem related indexes). */
838}; 857};
839 858
840typedef le32 FILE_ATTR_FLAGS; 859typedef le32 FILE_ATTR_FLAGS;
@@ -917,20 +936,12 @@ typedef struct {
917 /* 56*/ le64 quota_charged; /* Byte size of the charge to 936 /* 56*/ le64 quota_charged; /* Byte size of the charge to
918 the quota for all streams of the file. Note: Is 937 the quota for all streams of the file. Note: Is
919 zero if quotas are disabled. */ 938 zero if quotas are disabled. */
920 /* 64*/ le64 usn; /* Last update sequence number 939 /* 64*/ leUSN usn; /* Last update sequence number
921 of the file. This is a direct index into the 940 of the file. This is a direct index into the
922 change (aka usn) journal file. It is zero if 941 transaction log file ($UsnJrnl). It is zero if
923 the usn journal is disabled. 942 the usn journal is disabled or this file has
924 NOTE: To disable the journal need to delete 943 not been subject to logging yet. See usnjrnl.h
925 the journal file itself and to then walk the 944 for details. */
926 whole mft and set all Usn entries in all mft
927 records to zero! (This can take a while!)
928 The journal is FILE_Extend/$UsnJrnl. Win2k
929 will recreate the journal and initiate
930 logging if necessary when mounting the
931 partition. This, in contrast to disabling the
932 journal is a very fast process, so the user
933 won't even notice it. */
934 } __attribute__ ((__packed__)) v3; 945 } __attribute__ ((__packed__)) v3;
935 /* sizeof() = 72 bytes (NTFS 3.x) */ 946 /* sizeof() = 72 bytes (NTFS 3.x) */
936 } __attribute__ ((__packed__)) ver; 947 } __attribute__ ((__packed__)) ver;
@@ -1893,7 +1904,7 @@ enum {
1893 VOLUME_FLAGS_MASK = const_cpu_to_le16(0x803f), 1904 VOLUME_FLAGS_MASK = const_cpu_to_le16(0x803f),
1894 1905
1895 /* To make our life easier when checking if we must mount read-only. */ 1906 /* To make our life easier when checking if we must mount read-only. */
1896 VOLUME_MUST_MOUNT_RO_MASK = const_cpu_to_le16(0x8037), 1907 VOLUME_MUST_MOUNT_RO_MASK = const_cpu_to_le16(0x8027),
1897} __attribute__ ((__packed__)); 1908} __attribute__ ((__packed__));
1898 1909
1899typedef le16 VOLUME_FLAGS; 1910typedef le16 VOLUME_FLAGS;
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c
index 23fd911078b1..7b5934290685 100644
--- a/fs/ntfs/lcnalloc.c
+++ b/fs/ntfs/lcnalloc.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * lcnalloc.c - Cluster (de)allocation code. Part of the Linux-NTFS project. 2 * lcnalloc.c - Cluster (de)allocation code. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2004 Anton Altaparmakov 4 * Copyright (c) 2004-2005 Anton Altaparmakov
5 * 5 *
6 * This program/include file is free software; you can redistribute it and/or 6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published 7 * modify it under the terms of the GNU General Public License as published
@@ -54,13 +54,15 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
54 int ret = 0; 54 int ret = 0;
55 55
56 ntfs_debug("Entering."); 56 ntfs_debug("Entering.");
57 if (!rl)
58 return 0;
57 for (; rl->length; rl++) { 59 for (; rl->length; rl++) {
58 int err; 60 int err;
59 61
60 if (rl->lcn < 0) 62 if (rl->lcn < 0)
61 continue; 63 continue;
62 err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->lcn, rl->length); 64 err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->lcn, rl->length);
63 if (unlikely(err && (!ret || ret == ENOMEM) && ret != err)) 65 if (unlikely(err && (!ret || ret == -ENOMEM) && ret != err))
64 ret = err; 66 ret = err;
65 } 67 }
66 ntfs_debug("Done."); 68 ntfs_debug("Done.");
@@ -140,6 +142,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
140 LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn; 142 LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn;
141 LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size; 143 LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size;
142 s64 clusters; 144 s64 clusters;
145 loff_t i_size;
143 struct inode *lcnbmp_vi; 146 struct inode *lcnbmp_vi;
144 runlist_element *rl = NULL; 147 runlist_element *rl = NULL;
145 struct address_space *mapping; 148 struct address_space *mapping;
@@ -162,17 +165,9 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
162 BUG_ON(zone < FIRST_ZONE); 165 BUG_ON(zone < FIRST_ZONE);
163 BUG_ON(zone > LAST_ZONE); 166 BUG_ON(zone > LAST_ZONE);
164 167
165 /* Return empty runlist if @count == 0 */ 168 /* Return NULL if @count is zero. */
166 // FIXME: Do we want to just return NULL instead? (AIA) 169 if (!count)
167 if (!count) { 170 return NULL;
168 rl = ntfs_malloc_nofs(PAGE_SIZE);
169 if (!rl)
170 return ERR_PTR(-ENOMEM);
171 rl[0].vcn = start_vcn;
172 rl[0].lcn = LCN_RL_NOT_MAPPED;
173 rl[0].length = 0;
174 return rl;
175 }
176 /* Take the lcnbmp lock for writing. */ 171 /* Take the lcnbmp lock for writing. */
177 down_write(&vol->lcnbmp_lock); 172 down_write(&vol->lcnbmp_lock);
178 /* 173 /*
@@ -249,6 +244,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
249 clusters = count; 244 clusters = count;
250 rlpos = rlsize = 0; 245 rlpos = rlsize = 0;
251 mapping = lcnbmp_vi->i_mapping; 246 mapping = lcnbmp_vi->i_mapping;
247 i_size = i_size_read(lcnbmp_vi);
252 while (1) { 248 while (1) {
253 ntfs_debug("Start of outer while loop: done_zones 0x%x, " 249 ntfs_debug("Start of outer while loop: done_zones 0x%x, "
254 "search_zone %i, pass %i, zone_start 0x%llx, " 250 "search_zone %i, pass %i, zone_start 0x%llx, "
@@ -263,7 +259,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
263 last_read_pos = bmp_pos >> 3; 259 last_read_pos = bmp_pos >> 3;
264 ntfs_debug("last_read_pos 0x%llx.", 260 ntfs_debug("last_read_pos 0x%llx.",
265 (unsigned long long)last_read_pos); 261 (unsigned long long)last_read_pos);
266 if (last_read_pos > lcnbmp_vi->i_size) { 262 if (last_read_pos > i_size) {
267 ntfs_debug("End of attribute reached. " 263 ntfs_debug("End of attribute reached. "
268 "Skipping to zone_pass_done."); 264 "Skipping to zone_pass_done.");
269 goto zone_pass_done; 265 goto zone_pass_done;
@@ -287,11 +283,11 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
287 buf_size = last_read_pos & ~PAGE_CACHE_MASK; 283 buf_size = last_read_pos & ~PAGE_CACHE_MASK;
288 buf = page_address(page) + buf_size; 284 buf = page_address(page) + buf_size;
289 buf_size = PAGE_CACHE_SIZE - buf_size; 285 buf_size = PAGE_CACHE_SIZE - buf_size;
290 if (unlikely(last_read_pos + buf_size > lcnbmp_vi->i_size)) 286 if (unlikely(last_read_pos + buf_size > i_size))
291 buf_size = lcnbmp_vi->i_size - last_read_pos; 287 buf_size = i_size - last_read_pos;
292 buf_size <<= 3; 288 buf_size <<= 3;
293 lcn = bmp_pos & 7; 289 lcn = bmp_pos & 7;
294 bmp_pos &= ~7; 290 bmp_pos &= ~(LCN)7;
295 ntfs_debug("Before inner while loop: buf_size %i, lcn 0x%llx, " 291 ntfs_debug("Before inner while loop: buf_size %i, lcn 0x%llx, "
296 "bmp_pos 0x%llx, need_writeback %i.", buf_size, 292 "bmp_pos 0x%llx, need_writeback %i.", buf_size,
297 (unsigned long long)lcn, 293 (unsigned long long)lcn,
@@ -309,7 +305,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
309 (unsigned int)*byte); 305 (unsigned int)*byte);
310 /* Skip full bytes. */ 306 /* Skip full bytes. */
311 if (*byte == 0xff) { 307 if (*byte == 0xff) {
312 lcn = (lcn + 8) & ~7; 308 lcn = (lcn + 8) & ~(LCN)7;
313 ntfs_debug("Continuing while loop 1."); 309 ntfs_debug("Continuing while loop 1.");
314 continue; 310 continue;
315 } 311 }
@@ -691,7 +687,7 @@ switch_to_data1_zone: search_zone = 2;
691 if (zone == MFT_ZONE || mft_zone_size <= 0) { 687 if (zone == MFT_ZONE || mft_zone_size <= 0) {
692 ntfs_debug("No free clusters left, going to out."); 688 ntfs_debug("No free clusters left, going to out.");
693 /* Really no more space left on device. */ 689 /* Really no more space left on device. */
694 err = ENOSPC; 690 err = -ENOSPC;
695 goto out; 691 goto out;
696 } /* zone == DATA_ZONE && mft_zone_size > 0 */ 692 } /* zone == DATA_ZONE && mft_zone_size > 0 */
697 ntfs_debug("Shrinking mft zone."); 693 ntfs_debug("Shrinking mft zone.");
@@ -755,13 +751,13 @@ out:
755 if (rl) { 751 if (rl) {
756 int err2; 752 int err2;
757 753
758 if (err == ENOSPC) 754 if (err == -ENOSPC)
759 ntfs_debug("Not enough space to complete allocation, " 755 ntfs_debug("Not enough space to complete allocation, "
760 "err ENOSPC, first free lcn 0x%llx, " 756 "err -ENOSPC, first free lcn 0x%llx, "
761 "could allocate up to 0x%llx " 757 "could allocate up to 0x%llx "
762 "clusters.", 758 "clusters.",
763 (unsigned long long)rl[0].lcn, 759 (unsigned long long)rl[0].lcn,
764 (unsigned long long)count - clusters); 760 (unsigned long long)(count - clusters));
765 /* Deallocate all allocated clusters. */ 761 /* Deallocate all allocated clusters. */
766 ntfs_debug("Attempting rollback..."); 762 ntfs_debug("Attempting rollback...");
767 err2 = ntfs_cluster_free_from_rl_nolock(vol, rl); 763 err2 = ntfs_cluster_free_from_rl_nolock(vol, rl);
@@ -773,10 +769,10 @@ out:
773 } 769 }
774 /* Free the runlist. */ 770 /* Free the runlist. */
775 ntfs_free(rl); 771 ntfs_free(rl);
776 } else if (err == ENOSPC) 772 } else if (err == -ENOSPC)
777 ntfs_debug("No space left at all, err = ENOSPC, " 773 ntfs_debug("No space left at all, err = -ENOSPC, first free "
778 "first free lcn = 0x%llx.", 774 "lcn = 0x%llx.",
779 (unsigned long long)vol->data1_zone_pos); 775 (long long)vol->data1_zone_pos);
780 up_write(&vol->lcnbmp_lock); 776 up_write(&vol->lcnbmp_lock);
781 return ERR_PTR(err); 777 return ERR_PTR(err);
782} 778}
@@ -786,7 +782,8 @@ out:
786 * @vi: vfs inode whose runlist describes the clusters to free 782 * @vi: vfs inode whose runlist describes the clusters to free
787 * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters 783 * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters
788 * @count: number of clusters to free or -1 for all clusters 784 * @count: number of clusters to free or -1 for all clusters
789 * @is_rollback: if TRUE this is a rollback operation 785 * @write_locked: true if the runlist is locked for writing
786 * @is_rollback: true if this is a rollback operation
790 * 787 *
791 * Free @count clusters starting at the cluster @start_vcn in the runlist 788 * Free @count clusters starting at the cluster @start_vcn in the runlist
792 * described by the vfs inode @vi. 789 * described by the vfs inode @vi.
@@ -804,17 +801,17 @@ out:
804 * Return the number of deallocated clusters (not counting sparse ones) on 801 * Return the number of deallocated clusters (not counting sparse ones) on
805 * success and -errno on error. 802 * success and -errno on error.
806 * 803 *
807 * Locking: - The runlist described by @vi must be unlocked on entry and is 804 * Locking: - The runlist described by @vi must be locked on entry and is
808 * unlocked on return. 805 * locked on return. Note if the runlist is locked for reading the
809 * - This function takes the runlist lock of @vi for reading and 806 * lock may be dropped and reacquired. Note the runlist may be
810 * sometimes for writing and sometimes modifies the runlist. 807 * modified when needed runlist fragments need to be mapped.
811 * - The volume lcn bitmap must be unlocked on entry and is unlocked 808 * - The volume lcn bitmap must be unlocked on entry and is unlocked
812 * on return. 809 * on return.
813 * - This function takes the volume lcn bitmap lock for writing and 810 * - This function takes the volume lcn bitmap lock for writing and
814 * modifies the bitmap contents. 811 * modifies the bitmap contents.
815 */ 812 */
816s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count, 813s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
817 const BOOL is_rollback) 814 const BOOL write_locked, const BOOL is_rollback)
818{ 815{
819 s64 delta, to_free, total_freed, real_freed; 816 s64 delta, to_free, total_freed, real_freed;
820 ntfs_inode *ni; 817 ntfs_inode *ni;
@@ -846,8 +843,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
846 843
847 total_freed = real_freed = 0; 844 total_freed = real_freed = 0;
848 845
849 /* This returns with ni->runlist locked for reading on success. */ 846 rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, write_locked);
850 rl = ntfs_find_vcn(ni, start_vcn, FALSE);
851 if (IS_ERR(rl)) { 847 if (IS_ERR(rl)) {
852 if (!is_rollback) 848 if (!is_rollback)
853 ntfs_error(vol->sb, "Failed to find first runlist " 849 ntfs_error(vol->sb, "Failed to find first runlist "
@@ -861,7 +857,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
861 ntfs_error(vol->sb, "First runlist element has " 857 ntfs_error(vol->sb, "First runlist element has "
862 "invalid lcn, aborting."); 858 "invalid lcn, aborting.");
863 err = -EIO; 859 err = -EIO;
864 goto unl_err_out; 860 goto err_out;
865 } 861 }
866 /* Find the starting cluster inside the run that needs freeing. */ 862 /* Find the starting cluster inside the run that needs freeing. */
867 delta = start_vcn - rl->vcn; 863 delta = start_vcn - rl->vcn;
@@ -879,7 +875,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
879 if (!is_rollback) 875 if (!is_rollback)
880 ntfs_error(vol->sb, "Failed to clear first run " 876 ntfs_error(vol->sb, "Failed to clear first run "
881 "(error %i), aborting.", err); 877 "(error %i), aborting.", err);
882 goto unl_err_out; 878 goto err_out;
883 } 879 }
884 /* We have freed @to_free real clusters. */ 880 /* We have freed @to_free real clusters. */
885 real_freed = to_free; 881 real_freed = to_free;
@@ -899,30 +895,15 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
899 if (unlikely(rl->lcn < LCN_HOLE)) { 895 if (unlikely(rl->lcn < LCN_HOLE)) {
900 VCN vcn; 896 VCN vcn;
901 897
902 /* 898 /* Attempt to map runlist. */
903 * Attempt to map runlist, dropping runlist lock for
904 * the duration.
905 */
906 vcn = rl->vcn; 899 vcn = rl->vcn;
907 up_read(&ni->runlist.lock); 900 rl = ntfs_attr_find_vcn_nolock(ni, vcn, write_locked);
908 err = ntfs_map_runlist(ni, vcn);
909 if (err) {
910 if (!is_rollback)
911 ntfs_error(vol->sb, "Failed to map "
912 "runlist fragment.");
913 if (err == -EINVAL || err == -ENOENT)
914 err = -EIO;
915 goto err_out;
916 }
917 /*
918 * This returns with ni->runlist locked for reading on
919 * success.
920 */
921 rl = ntfs_find_vcn(ni, vcn, FALSE);
922 if (IS_ERR(rl)) { 901 if (IS_ERR(rl)) {
923 err = PTR_ERR(rl); 902 err = PTR_ERR(rl);
924 if (!is_rollback) 903 if (!is_rollback)
925 ntfs_error(vol->sb, "Failed to find " 904 ntfs_error(vol->sb, "Failed to map "
905 "runlist fragment or "
906 "failed to find "
926 "subsequent runlist " 907 "subsequent runlist "
927 "element."); 908 "element.");
928 goto err_out; 909 goto err_out;
@@ -935,7 +916,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
935 (unsigned long long) 916 (unsigned long long)
936 rl->lcn); 917 rl->lcn);
937 err = -EIO; 918 err = -EIO;
938 goto unl_err_out; 919 goto err_out;
939 } 920 }
940 } 921 }
941 /* The number of clusters in this run that need freeing. */ 922 /* The number of clusters in this run that need freeing. */
@@ -951,7 +932,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
951 if (!is_rollback) 932 if (!is_rollback)
952 ntfs_error(vol->sb, "Failed to clear " 933 ntfs_error(vol->sb, "Failed to clear "
953 "subsequent run."); 934 "subsequent run.");
954 goto unl_err_out; 935 goto err_out;
955 } 936 }
956 /* We have freed @to_free real clusters. */ 937 /* We have freed @to_free real clusters. */
957 real_freed += to_free; 938 real_freed += to_free;
@@ -963,7 +944,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
963 /* Update the total done clusters. */ 944 /* Update the total done clusters. */
964 total_freed += to_free; 945 total_freed += to_free;
965 } 946 }
966 up_read(&ni->runlist.lock);
967 if (likely(!is_rollback)) 947 if (likely(!is_rollback))
968 up_write(&vol->lcnbmp_lock); 948 up_write(&vol->lcnbmp_lock);
969 949
@@ -972,8 +952,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
972 /* We are done. Return the number of actually freed clusters. */ 952 /* We are done. Return the number of actually freed clusters. */
973 ntfs_debug("Done."); 953 ntfs_debug("Done.");
974 return real_freed; 954 return real_freed;
975unl_err_out:
976 up_read(&ni->runlist.lock);
977err_out: 955err_out:
978 if (is_rollback) 956 if (is_rollback)
979 return err; 957 return err;
@@ -987,7 +965,8 @@ err_out:
987 * If rollback fails, set the volume errors flag, emit an error 965 * If rollback fails, set the volume errors flag, emit an error
988 * message, and return the error code. 966 * message, and return the error code.
989 */ 967 */
990 delta = __ntfs_cluster_free(vi, start_vcn, total_freed, TRUE); 968 delta = __ntfs_cluster_free(vi, start_vcn, total_freed, write_locked,
969 TRUE);
991 if (delta < 0) { 970 if (delta < 0) {
992 ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving " 971 ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving "
993 "inconsistent metadata! Unmount and run " 972 "inconsistent metadata! Unmount and run "
diff --git a/fs/ntfs/lcnalloc.h b/fs/ntfs/lcnalloc.h
index 4cac1c024af6..e4d7fb98d685 100644
--- a/fs/ntfs/lcnalloc.h
+++ b/fs/ntfs/lcnalloc.h
@@ -43,13 +43,14 @@ extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol,
43 const NTFS_CLUSTER_ALLOCATION_ZONES zone); 43 const NTFS_CLUSTER_ALLOCATION_ZONES zone);
44 44
45extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, 45extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
46 s64 count, const BOOL is_rollback); 46 s64 count, const BOOL write_locked, const BOOL is_rollback);
47 47
48/** 48/**
49 * ntfs_cluster_free - free clusters on an ntfs volume 49 * ntfs_cluster_free - free clusters on an ntfs volume
50 * @vi: vfs inode whose runlist describes the clusters to free 50 * @vi: vfs inode whose runlist describes the clusters to free
51 * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters 51 * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters
52 * @count: number of clusters to free or -1 for all clusters 52 * @count: number of clusters to free or -1 for all clusters
53 * @write_locked: true if the runlist is locked for writing
53 * 54 *
54 * Free @count clusters starting at the cluster @start_vcn in the runlist 55 * Free @count clusters starting at the cluster @start_vcn in the runlist
55 * described by the vfs inode @vi. 56 * described by the vfs inode @vi.
@@ -64,19 +65,19 @@ extern s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
64 * Return the number of deallocated clusters (not counting sparse ones) on 65 * Return the number of deallocated clusters (not counting sparse ones) on
65 * success and -errno on error. 66 * success and -errno on error.
66 * 67 *
67 * Locking: - The runlist described by @vi must be unlocked on entry and is 68 * Locking: - The runlist described by @vi must be locked on entry and is
68 * unlocked on return. 69 * locked on return. Note if the runlist is locked for reading the
69 * - This function takes the runlist lock of @vi for reading and 70 * lock may be dropped and reacquired. Note the runlist may be
70 * sometimes for writing and sometimes modifies the runlist. 71 * modified when needed runlist fragments need to be mapped.
71 * - The volume lcn bitmap must be unlocked on entry and is unlocked 72 * - The volume lcn bitmap must be unlocked on entry and is unlocked
72 * on return. 73 * on return.
73 * - This function takes the volume lcn bitmap lock for writing and 74 * - This function takes the volume lcn bitmap lock for writing and
74 * modifies the bitmap contents. 75 * modifies the bitmap contents.
75 */ 76 */
76static inline s64 ntfs_cluster_free(struct inode *vi, const VCN start_vcn, 77static inline s64 ntfs_cluster_free(struct inode *vi, const VCN start_vcn,
77 s64 count) 78 s64 count, const BOOL write_locked)
78{ 79{
79 return __ntfs_cluster_free(vi, start_vcn, count, FALSE); 80 return __ntfs_cluster_free(vi, start_vcn, count, write_locked, FALSE);
80} 81}
81 82
82extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, 83extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
@@ -93,8 +94,10 @@ extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
93 * 94 *
94 * Return 0 on success and -errno on error. 95 * Return 0 on success and -errno on error.
95 * 96 *
96 * Locking: This function takes the volume lcn bitmap lock for writing and 97 * Locking: - This function takes the volume lcn bitmap lock for writing and
97 * modifies the bitmap contents. 98 * modifies the bitmap contents.
99 * - The caller must have locked the runlist @rl for reading or
100 * writing.
98 */ 101 */
99static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol, 102static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol,
100 const runlist_element *rl) 103 const runlist_element *rl)
diff --git a/fs/ntfs/logfile.c b/fs/ntfs/logfile.c
index 5e280abafab3..0173e95500d9 100644
--- a/fs/ntfs/logfile.c
+++ b/fs/ntfs/logfile.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project. 2 * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2002-2004 Anton Altaparmakov 4 * Copyright (c) 2002-2005 Anton Altaparmakov
5 * 5 *
6 * This program/include file is free software; you can redistribute it and/or 6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published 7 * modify it under the terms of the GNU General Public License as published
@@ -121,7 +121,7 @@ static BOOL ntfs_check_restart_page_header(struct inode *vi,
121 */ 121 */
122 if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) { 122 if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) {
123 ntfs_error(vi->i_sb, "$LogFile restart page is not modified " 123 ntfs_error(vi->i_sb, "$LogFile restart page is not modified "
124 "chkdsk but a chkdsk LSN is specified."); 124 "by chkdsk but a chkdsk LSN is specified.");
125 return FALSE; 125 return FALSE;
126 } 126 }
127 ntfs_debug("Done."); 127 ntfs_debug("Done.");
@@ -312,10 +312,12 @@ err_out:
312 * @vi: $LogFile inode to which the restart page belongs 312 * @vi: $LogFile inode to which the restart page belongs
313 * @rp: restart page to check 313 * @rp: restart page to check
314 * @pos: position in @vi at which the restart page resides 314 * @pos: position in @vi at which the restart page resides
315 * @wrp: copy of the multi sector transfer deprotected restart page 315 * @wrp: [OUT] copy of the multi sector transfer deprotected restart page
316 * @lsn: [OUT] set to the current logfile lsn on success
316 * 317 *
317 * Check the restart page @rp for consistency and return TRUE if it is 318 * Check the restart page @rp for consistency and return 0 if it is consistent
318 * consistent and FALSE otherwise. 319 * and -errno otherwise. The restart page may have been modified by chkdsk in
320 * which case its magic is CHKD instead of RSTR.
319 * 321 *
320 * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not 322 * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
321 * require the full restart page. 323 * require the full restart page.
@@ -323,25 +325,33 @@ err_out:
323 * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a 325 * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a
324 * copy of the complete multi sector transfer deprotected page. On failure, 326 * copy of the complete multi sector transfer deprotected page. On failure,
325 * *@wrp is undefined. 327 * *@wrp is undefined.
328 *
329 * Simillarly, if @lsn is not NULL, on succes *@lsn will be set to the current
330 * logfile lsn according to this restart page. On failure, *@lsn is undefined.
331 *
332 * The following error codes are defined:
333 * -EINVAL - The restart page is inconsistent.
334 * -ENOMEM - Not enough memory to load the restart page.
335 * -EIO - Failed to reading from $LogFile.
326 */ 336 */
327static BOOL ntfs_check_and_load_restart_page(struct inode *vi, 337static int ntfs_check_and_load_restart_page(struct inode *vi,
328 RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp) 338 RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp,
339 LSN *lsn)
329{ 340{
330 RESTART_AREA *ra; 341 RESTART_AREA *ra;
331 RESTART_PAGE_HEADER *trp; 342 RESTART_PAGE_HEADER *trp;
332 int size; 343 int size, err;
333 BOOL ret;
334 344
335 ntfs_debug("Entering."); 345 ntfs_debug("Entering.");
336 /* Check the restart page header for consistency. */ 346 /* Check the restart page header for consistency. */
337 if (!ntfs_check_restart_page_header(vi, rp, pos)) { 347 if (!ntfs_check_restart_page_header(vi, rp, pos)) {
338 /* Error output already done inside the function. */ 348 /* Error output already done inside the function. */
339 return FALSE; 349 return -EINVAL;
340 } 350 }
341 /* Check the restart area for consistency. */ 351 /* Check the restart area for consistency. */
342 if (!ntfs_check_restart_area(vi, rp)) { 352 if (!ntfs_check_restart_area(vi, rp)) {
343 /* Error output already done inside the function. */ 353 /* Error output already done inside the function. */
344 return FALSE; 354 return -EINVAL;
345 } 355 }
346 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); 356 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
347 /* 357 /*
@@ -352,7 +362,7 @@ static BOOL ntfs_check_and_load_restart_page(struct inode *vi,
352 if (!trp) { 362 if (!trp) {
353 ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile " 363 ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile "
354 "restart page buffer."); 364 "restart page buffer.");
355 return FALSE; 365 return -ENOMEM;
356 } 366 }
357 /* 367 /*
358 * Read the whole of the restart page into the buffer. If it fits 368 * Read the whole of the restart page into the buffer. If it fits
@@ -379,6 +389,9 @@ static BOOL ntfs_check_and_load_restart_page(struct inode *vi,
379 if (IS_ERR(page)) { 389 if (IS_ERR(page)) {
380 ntfs_error(vi->i_sb, "Error mapping $LogFile " 390 ntfs_error(vi->i_sb, "Error mapping $LogFile "
381 "page (index %lu).", idx); 391 "page (index %lu).", idx);
392 err = PTR_ERR(page);
393 if (err != -EIO && err != -ENOMEM)
394 err = -EIO;
382 goto err_out; 395 goto err_out;
383 } 396 }
384 size = min_t(int, to_read, PAGE_CACHE_SIZE); 397 size = min_t(int, to_read, PAGE_CACHE_SIZE);
@@ -392,29 +405,57 @@ static BOOL ntfs_check_and_load_restart_page(struct inode *vi,
392 /* Perform the multi sector transfer deprotection on the buffer. */ 405 /* Perform the multi sector transfer deprotection on the buffer. */
393 if (post_read_mst_fixup((NTFS_RECORD*)trp, 406 if (post_read_mst_fixup((NTFS_RECORD*)trp,
394 le32_to_cpu(rp->system_page_size))) { 407 le32_to_cpu(rp->system_page_size))) {
395 ntfs_error(vi->i_sb, "Multi sector transfer error detected in " 408 /*
396 "$LogFile restart page."); 409 * A multi sector tranfer error was detected. We only need to
397 goto err_out; 410 * abort if the restart page contents exceed the multi sector
411 * transfer fixup of the first sector.
412 */
413 if (le16_to_cpu(rp->restart_area_offset) +
414 le16_to_cpu(ra->restart_area_length) >
415 NTFS_BLOCK_SIZE - sizeof(u16)) {
416 ntfs_error(vi->i_sb, "Multi sector transfer error "
417 "detected in $LogFile restart page.");
418 err = -EINVAL;
419 goto err_out;
420 }
421 }
422 /*
423 * If the restart page is modified by chkdsk or there are no active
424 * logfile clients, the logfile is consistent. Otherwise, need to
425 * check the log client records for consistency, too.
426 */
427 err = 0;
428 if (ntfs_is_rstr_record(rp->magic) &&
429 ra->client_in_use_list != LOGFILE_NO_CLIENT) {
430 if (!ntfs_check_log_client_array(vi, trp)) {
431 err = -EINVAL;
432 goto err_out;
433 }
434 }
435 if (lsn) {
436 if (ntfs_is_rstr_record(rp->magic))
437 *lsn = sle64_to_cpu(ra->current_lsn);
438 else /* if (ntfs_is_chkd_record(rp->magic)) */
439 *lsn = sle64_to_cpu(rp->chkdsk_lsn);
398 } 440 }
399 /* Check the log client records for consistency. */
400 ret = ntfs_check_log_client_array(vi, trp);
401 if (ret && wrp)
402 *wrp = trp;
403 else
404 ntfs_free(trp);
405 ntfs_debug("Done."); 441 ntfs_debug("Done.");
406 return ret; 442 if (wrp)
443 *wrp = trp;
444 else {
407err_out: 445err_out:
408 ntfs_free(trp); 446 ntfs_free(trp);
409 return FALSE; 447 }
448 return err;
410} 449}
411 450
412/** 451/**
413 * ntfs_ckeck_logfile - check in the journal if the volume is consistent 452 * ntfs_check_logfile - check the journal for consistency
414 * @log_vi: struct inode of loaded journal $LogFile to check 453 * @log_vi: struct inode of loaded journal $LogFile to check
454 * @rp: [OUT] on success this is a copy of the current restart page
415 * 455 *
416 * Check the $LogFile journal for consistency and return TRUE if it is 456 * Check the $LogFile journal for consistency and return TRUE if it is
417 * consistent and FALSE if not. 457 * consistent and FALSE if not. On success, the current restart page is
458 * returned in *@rp. Caller must call ntfs_free(*@rp) when finished with it.
418 * 459 *
419 * At present we only check the two restart pages and ignore the log record 460 * At present we only check the two restart pages and ignore the log record
420 * pages. 461 * pages.
@@ -424,26 +465,25 @@ err_out:
424 * if the $LogFile was created on a system with a different page size to ours 465 * if the $LogFile was created on a system with a different page size to ours
425 * yet and mst deprotection would fail if our page size is smaller. 466 * yet and mst deprotection would fail if our page size is smaller.
426 */ 467 */
427BOOL ntfs_check_logfile(struct inode *log_vi) 468BOOL ntfs_check_logfile(struct inode *log_vi, RESTART_PAGE_HEADER **rp)
428{ 469{
429 s64 size, pos, rstr1_pos, rstr2_pos; 470 s64 size, pos;
471 LSN rstr1_lsn, rstr2_lsn;
430 ntfs_volume *vol = NTFS_SB(log_vi->i_sb); 472 ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
431 struct address_space *mapping = log_vi->i_mapping; 473 struct address_space *mapping = log_vi->i_mapping;
432 struct page *page = NULL; 474 struct page *page = NULL;
433 u8 *kaddr = NULL; 475 u8 *kaddr = NULL;
434 RESTART_PAGE_HEADER *rstr1_ph = NULL; 476 RESTART_PAGE_HEADER *rstr1_ph = NULL;
435 RESTART_PAGE_HEADER *rstr2_ph = NULL; 477 RESTART_PAGE_HEADER *rstr2_ph = NULL;
436 int log_page_size, log_page_mask, ofs; 478 int log_page_size, log_page_mask, err;
437 BOOL logfile_is_empty = TRUE; 479 BOOL logfile_is_empty = TRUE;
438 BOOL rstr1_found = FALSE;
439 BOOL rstr2_found = FALSE;
440 u8 log_page_bits; 480 u8 log_page_bits;
441 481
442 ntfs_debug("Entering."); 482 ntfs_debug("Entering.");
443 /* An empty $LogFile must have been clean before it got emptied. */ 483 /* An empty $LogFile must have been clean before it got emptied. */
444 if (NVolLogFileEmpty(vol)) 484 if (NVolLogFileEmpty(vol))
445 goto is_empty; 485 goto is_empty;
446 size = log_vi->i_size; 486 size = i_size_read(log_vi);
447 /* Make sure the file doesn't exceed the maximum allowed size. */ 487 /* Make sure the file doesn't exceed the maximum allowed size. */
448 if (size > MaxLogFileSize) 488 if (size > MaxLogFileSize)
449 size = MaxLogFileSize; 489 size = MaxLogFileSize;
@@ -464,7 +504,7 @@ BOOL ntfs_check_logfile(struct inode *log_vi)
464 * optimize log_page_size and log_page_bits into constants. 504 * optimize log_page_size and log_page_bits into constants.
465 */ 505 */
466 log_page_bits = generic_ffs(log_page_size) - 1; 506 log_page_bits = generic_ffs(log_page_size) - 1;
467 size &= ~(log_page_size - 1); 507 size &= ~(s64)(log_page_size - 1);
468 /* 508 /*
469 * Ensure the log file is big enough to store at least the two restart 509 * Ensure the log file is big enough to store at least the two restart
470 * pages and the minimum number of log record pages. 510 * pages and the minimum number of log record pages.
@@ -491,7 +531,7 @@ BOOL ntfs_check_logfile(struct inode *log_vi)
491 if (IS_ERR(page)) { 531 if (IS_ERR(page)) {
492 ntfs_error(vol->sb, "Error mapping $LogFile " 532 ntfs_error(vol->sb, "Error mapping $LogFile "
493 "page (index %lu).", idx); 533 "page (index %lu).", idx);
494 return FALSE; 534 goto err_out;
495 } 535 }
496 } 536 }
497 kaddr = (u8*)page_address(page) + (pos & ~PAGE_CACHE_MASK); 537 kaddr = (u8*)page_address(page) + (pos & ~PAGE_CACHE_MASK);
@@ -510,99 +550,95 @@ BOOL ntfs_check_logfile(struct inode *log_vi)
510 */ 550 */
511 if (ntfs_is_rcrd_recordp((le32*)kaddr)) 551 if (ntfs_is_rcrd_recordp((le32*)kaddr))
512 break; 552 break;
513 /* 553 /* If not a (modified by chkdsk) restart page, continue. */
514 * A modified by chkdsk restart page means we cannot handle 554 if (!ntfs_is_rstr_recordp((le32*)kaddr) &&
515 * this log file. 555 !ntfs_is_chkd_recordp((le32*)kaddr)) {
516 */
517 if (ntfs_is_chkd_recordp((le32*)kaddr)) {
518 ntfs_error(vol->sb, "$LogFile has been modified by "
519 "chkdsk. Mount this volume in "
520 "Windows.");
521 goto err_out;
522 }
523 /* If not a restart page, continue. */
524 if (!ntfs_is_rstr_recordp((le32*)kaddr)) {
525 /* Skip to the minimum page size for the next one. */
526 if (!pos) 556 if (!pos)
527 pos = NTFS_BLOCK_SIZE >> 1; 557 pos = NTFS_BLOCK_SIZE >> 1;
528 continue; 558 continue;
529 } 559 }
530 /* We now know we have a restart page. */
531 if (!pos) {
532 rstr1_found = TRUE;
533 rstr1_pos = pos;
534 } else {
535 if (rstr2_found) {
536 ntfs_error(vol->sb, "Found more than two "
537 "restart pages in $LogFile.");
538 goto err_out;
539 }
540 rstr2_found = TRUE;
541 rstr2_pos = pos;
542 }
543 /* 560 /*
544 * Check the restart page for consistency and get a copy of the 561 * Check the (modified by chkdsk) restart page for consistency
545 * complete multi sector transfer deprotected restart page. 562 * and get a copy of the complete multi sector transfer
563 * deprotected restart page.
546 */ 564 */
547 if (!ntfs_check_and_load_restart_page(log_vi, 565 err = ntfs_check_and_load_restart_page(log_vi,
548 (RESTART_PAGE_HEADER*)kaddr, pos, 566 (RESTART_PAGE_HEADER*)kaddr, pos,
549 !pos ? &rstr1_ph : &rstr2_ph)) { 567 !rstr1_ph ? &rstr1_ph : &rstr2_ph,
550 /* Error output already done inside the function. */ 568 !rstr1_ph ? &rstr1_lsn : &rstr2_lsn);
551 goto err_out; 569 if (!err) {
570 /*
571 * If we have now found the first (modified by chkdsk)
572 * restart page, continue looking for the second one.
573 */
574 if (!pos) {
575 pos = NTFS_BLOCK_SIZE >> 1;
576 continue;
577 }
578 /*
579 * We have now found the second (modified by chkdsk)
580 * restart page, so we can stop looking.
581 */
582 break;
552 } 583 }
553 /* 584 /*
554 * We have a valid restart page. The next one must be after 585 * Error output already done inside the function. Note, we do
555 * a whole system page size as specified by the valid restart 586 * not abort if the restart page was invalid as we might still
556 * page. 587 * find a valid one further in the file.
557 */ 588 */
589 if (err != -EINVAL) {
590 ntfs_unmap_page(page);
591 goto err_out;
592 }
593 /* Continue looking. */
558 if (!pos) 594 if (!pos)
559 pos = le32_to_cpu(rstr1_ph->system_page_size) >> 1; 595 pos = NTFS_BLOCK_SIZE >> 1;
560 } 596 }
561 if (page) { 597 if (page)
562 ntfs_unmap_page(page); 598 ntfs_unmap_page(page);
563 page = NULL;
564 }
565 if (logfile_is_empty) { 599 if (logfile_is_empty) {
566 NVolSetLogFileEmpty(vol); 600 NVolSetLogFileEmpty(vol);
567is_empty: 601is_empty:
568 ntfs_debug("Done. ($LogFile is empty.)"); 602 ntfs_debug("Done. ($LogFile is empty.)");
569 return TRUE; 603 return TRUE;
570 } 604 }
571 if (!rstr1_found || !rstr2_found) { 605 if (!rstr1_ph) {
572 ntfs_error(vol->sb, "Did not find two restart pages in " 606 BUG_ON(rstr2_ph);
573 "$LogFile."); 607 ntfs_error(vol->sb, "Did not find any restart pages in "
574 goto err_out; 608 "$LogFile and it was not empty.");
609 return FALSE;
610 }
611 /* If both restart pages were found, use the more recent one. */
612 if (rstr2_ph) {
613 /*
614 * If the second restart area is more recent, switch to it.
615 * Otherwise just throw it away.
616 */
617 if (rstr2_lsn > rstr1_lsn) {
618 ntfs_free(rstr1_ph);
619 rstr1_ph = rstr2_ph;
620 /* rstr1_lsn = rstr2_lsn; */
621 } else
622 ntfs_free(rstr2_ph);
623 rstr2_ph = NULL;
575 } 624 }
576 /*
577 * The two restart areas must be identical except for the update
578 * sequence number.
579 */
580 ofs = le16_to_cpu(rstr1_ph->usa_ofs);
581 if (memcmp(rstr1_ph, rstr2_ph, ofs) || (ofs += sizeof(u16),
582 memcmp((u8*)rstr1_ph + ofs, (u8*)rstr2_ph + ofs,
583 le32_to_cpu(rstr1_ph->system_page_size) - ofs))) {
584 ntfs_error(vol->sb, "The two restart pages in $LogFile do not "
585 "match.");
586 goto err_out;
587 }
588 ntfs_free(rstr1_ph);
589 ntfs_free(rstr2_ph);
590 /* All consistency checks passed. */ 625 /* All consistency checks passed. */
626 if (rp)
627 *rp = rstr1_ph;
628 else
629 ntfs_free(rstr1_ph);
591 ntfs_debug("Done."); 630 ntfs_debug("Done.");
592 return TRUE; 631 return TRUE;
593err_out: 632err_out:
594 if (page)
595 ntfs_unmap_page(page);
596 if (rstr1_ph) 633 if (rstr1_ph)
597 ntfs_free(rstr1_ph); 634 ntfs_free(rstr1_ph);
598 if (rstr2_ph)
599 ntfs_free(rstr2_ph);
600 return FALSE; 635 return FALSE;
601} 636}
602 637
603/** 638/**
604 * ntfs_is_logfile_clean - check in the journal if the volume is clean 639 * ntfs_is_logfile_clean - check in the journal if the volume is clean
605 * @log_vi: struct inode of loaded journal $LogFile to check 640 * @log_vi: struct inode of loaded journal $LogFile to check
641 * @rp: copy of the current restart page
606 * 642 *
607 * Analyze the $LogFile journal and return TRUE if it indicates the volume was 643 * Analyze the $LogFile journal and return TRUE if it indicates the volume was
608 * shutdown cleanly and FALSE if not. 644 * shutdown cleanly and FALSE if not.
@@ -619,11 +655,9 @@ err_out:
619 * is empty this function requires that NVolLogFileEmpty() is true otherwise an 655 * is empty this function requires that NVolLogFileEmpty() is true otherwise an
620 * empty volume will be reported as dirty. 656 * empty volume will be reported as dirty.
621 */ 657 */
622BOOL ntfs_is_logfile_clean(struct inode *log_vi) 658BOOL ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp)
623{ 659{
624 ntfs_volume *vol = NTFS_SB(log_vi->i_sb); 660 ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
625 struct page *page;
626 RESTART_PAGE_HEADER *rp;
627 RESTART_AREA *ra; 661 RESTART_AREA *ra;
628 662
629 ntfs_debug("Entering."); 663 ntfs_debug("Entering.");
@@ -632,24 +666,15 @@ BOOL ntfs_is_logfile_clean(struct inode *log_vi)
632 ntfs_debug("Done. ($LogFile is empty.)"); 666 ntfs_debug("Done. ($LogFile is empty.)");
633 return TRUE; 667 return TRUE;
634 } 668 }
635 /* 669 BUG_ON(!rp);
636 * Read the first restart page. It will be possibly incomplete and 670 if (!ntfs_is_rstr_record(rp->magic) &&
637 * will not be multi sector transfer deprotected but we only need the 671 !ntfs_is_chkd_record(rp->magic)) {
638 * first NTFS_BLOCK_SIZE bytes so it does not matter. 672 ntfs_error(vol->sb, "Restart page buffer is invalid. This is "
639 */ 673 "probably a bug in that the $LogFile should "
640 page = ntfs_map_page(log_vi->i_mapping, 0); 674 "have been consistency checked before calling "
641 if (IS_ERR(page)) { 675 "this function.");
642 ntfs_error(vol->sb, "Error mapping $LogFile page (index 0).");
643 return FALSE; 676 return FALSE;
644 } 677 }
645 rp = (RESTART_PAGE_HEADER*)page_address(page);
646 if (!ntfs_is_rstr_record(rp->magic)) {
647 ntfs_error(vol->sb, "No restart page found at offset zero in "
648 "$LogFile. This is probably a bug in that "
649 "the $LogFile should have been consistency "
650 "checked before calling this function.");
651 goto err_out;
652 }
653 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); 678 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
654 /* 679 /*
655 * If the $LogFile has active clients, i.e. it is open, and we do not 680 * If the $LogFile has active clients, i.e. it is open, and we do not
@@ -659,15 +684,11 @@ BOOL ntfs_is_logfile_clean(struct inode *log_vi)
659 if (ra->client_in_use_list != LOGFILE_NO_CLIENT && 684 if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
660 !(ra->flags & RESTART_VOLUME_IS_CLEAN)) { 685 !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
661 ntfs_debug("Done. $LogFile indicates a dirty shutdown."); 686 ntfs_debug("Done. $LogFile indicates a dirty shutdown.");
662 goto err_out; 687 return FALSE;
663 } 688 }
664 ntfs_unmap_page(page);
665 /* $LogFile indicates a clean shutdown. */ 689 /* $LogFile indicates a clean shutdown. */
666 ntfs_debug("Done. $LogFile indicates a clean shutdown."); 690 ntfs_debug("Done. $LogFile indicates a clean shutdown.");
667 return TRUE; 691 return TRUE;
668err_out:
669 ntfs_unmap_page(page);
670 return FALSE;
671} 692}
672 693
673/** 694/**
@@ -689,7 +710,8 @@ BOOL ntfs_empty_logfile(struct inode *log_vi)
689 if (!NVolLogFileEmpty(vol)) { 710 if (!NVolLogFileEmpty(vol)) {
690 int err; 711 int err;
691 712
692 err = ntfs_attr_set(NTFS_I(log_vi), 0, log_vi->i_size, 0xff); 713 err = ntfs_attr_set(NTFS_I(log_vi), 0, i_size_read(log_vi),
714 0xff);
693 if (unlikely(err)) { 715 if (unlikely(err)) {
694 ntfs_error(vol->sb, "Failed to fill $LogFile with " 716 ntfs_error(vol->sb, "Failed to fill $LogFile with "
695 "0xff bytes (error code %i).", err); 717 "0xff bytes (error code %i).", err);
diff --git a/fs/ntfs/logfile.h b/fs/ntfs/logfile.h
index 4ee4378de061..42388f95ea6d 100644
--- a/fs/ntfs/logfile.h
+++ b/fs/ntfs/logfile.h
@@ -2,7 +2,7 @@
2 * logfile.h - Defines for NTFS kernel journal ($LogFile) handling. Part of 2 * logfile.h - Defines for NTFS kernel journal ($LogFile) handling. Part of
3 * the Linux-NTFS project. 3 * the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2000-2004 Anton Altaparmakov 5 * Copyright (c) 2000-2005 Anton Altaparmakov
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published 8 * modify it under the terms of the GNU General Public License as published
@@ -296,9 +296,11 @@ typedef struct {
296/* sizeof() = 160 (0xa0) bytes */ 296/* sizeof() = 160 (0xa0) bytes */
297} __attribute__ ((__packed__)) LOG_CLIENT_RECORD; 297} __attribute__ ((__packed__)) LOG_CLIENT_RECORD;
298 298
299extern BOOL ntfs_check_logfile(struct inode *log_vi); 299extern BOOL ntfs_check_logfile(struct inode *log_vi,
300 RESTART_PAGE_HEADER **rp);
300 301
301extern BOOL ntfs_is_logfile_clean(struct inode *log_vi); 302extern BOOL ntfs_is_logfile_clean(struct inode *log_vi,
303 const RESTART_PAGE_HEADER *rp);
302 304
303extern BOOL ntfs_empty_logfile(struct inode *log_vi); 305extern BOOL ntfs_empty_logfile(struct inode *log_vi);
304 306
diff --git a/fs/ntfs/malloc.h b/fs/ntfs/malloc.h
index fac5944df6d8..3288bcc2c4aa 100644
--- a/fs/ntfs/malloc.h
+++ b/fs/ntfs/malloc.h
@@ -27,27 +27,63 @@
27#include <linux/highmem.h> 27#include <linux/highmem.h>
28 28
29/** 29/**
30 * ntfs_malloc_nofs - allocate memory in multiples of pages 30 * __ntfs_malloc - allocate memory in multiples of pages
31 * @size number of bytes to allocate 31 * @size: number of bytes to allocate
32 * @gfp_mask: extra flags for the allocator
33 *
34 * Internal function. You probably want ntfs_malloc_nofs()...
32 * 35 *
33 * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and 36 * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
34 * returns a pointer to the allocated memory. 37 * returns a pointer to the allocated memory.
35 * 38 *
36 * If there was insufficient memory to complete the request, return NULL. 39 * If there was insufficient memory to complete the request, return NULL.
40 * Depending on @gfp_mask the allocation may be guaranteed to succeed.
37 */ 41 */
38static inline void *ntfs_malloc_nofs(unsigned long size) 42static inline void *__ntfs_malloc(unsigned long size,
43 unsigned int __nocast gfp_mask)
39{ 44{
40 if (likely(size <= PAGE_SIZE)) { 45 if (likely(size <= PAGE_SIZE)) {
41 BUG_ON(!size); 46 BUG_ON(!size);
42 /* kmalloc() has per-CPU caches so is faster for now. */ 47 /* kmalloc() has per-CPU caches so is faster for now. */
43 return kmalloc(PAGE_SIZE, GFP_NOFS); 48 return kmalloc(PAGE_SIZE, gfp_mask & ~__GFP_HIGHMEM);
44 /* return (void *)__get_free_page(GFP_NOFS | __GFP_HIGHMEM); */ 49 /* return (void *)__get_free_page(gfp_mask); */
45 } 50 }
46 if (likely(size >> PAGE_SHIFT < num_physpages)) 51 if (likely(size >> PAGE_SHIFT < num_physpages))
47 return __vmalloc(size, GFP_NOFS | __GFP_HIGHMEM, PAGE_KERNEL); 52 return __vmalloc(size, gfp_mask, PAGE_KERNEL);
48 return NULL; 53 return NULL;
49} 54}
50 55
56/**
57 * ntfs_malloc_nofs - allocate memory in multiples of pages
58 * @size: number of bytes to allocate
59 *
60 * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
61 * returns a pointer to the allocated memory.
62 *
63 * If there was insufficient memory to complete the request, return NULL.
64 */
65static inline void *ntfs_malloc_nofs(unsigned long size)
66{
67 return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM);
68}
69
70/**
71 * ntfs_malloc_nofs_nofail - allocate memory in multiples of pages
72 * @size: number of bytes to allocate
73 *
74 * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
75 * returns a pointer to the allocated memory.
76 *
77 * This function guarantees that the allocation will succeed. It will sleep
78 * for as long as it takes to complete the allocation.
79 *
80 * If there was insufficient memory to complete the request, return NULL.
81 */
82static inline void *ntfs_malloc_nofs_nofail(unsigned long size)
83{
84 return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM | __GFP_NOFAIL);
85}
86
51static inline void ntfs_free(void *addr) 87static inline void ntfs_free(void *addr)
52{ 88{
53 if (likely(((unsigned long)addr < VMALLOC_START) || 89 if (likely(((unsigned long)addr < VMALLOC_START) ||
diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
index dfa85ac2f8ba..2c32b84385a8 100644
--- a/fs/ntfs/mft.c
+++ b/fs/ntfs/mft.c
@@ -1,7 +1,7 @@
1/** 1/**
2 * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project. 2 * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon 5 * Copyright (c) 2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -45,6 +45,7 @@
45 */ 45 */
46static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni) 46static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
47{ 47{
48 loff_t i_size;
48 ntfs_volume *vol = ni->vol; 49 ntfs_volume *vol = ni->vol;
49 struct inode *mft_vi = vol->mft_ino; 50 struct inode *mft_vi = vol->mft_ino;
50 struct page *page; 51 struct page *page;
@@ -60,13 +61,14 @@ static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
60 index = ni->mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT; 61 index = ni->mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
61 ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK; 62 ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
62 63
64 i_size = i_size_read(mft_vi);
63 /* The maximum valid index into the page cache for $MFT's data. */ 65 /* The maximum valid index into the page cache for $MFT's data. */
64 end_index = mft_vi->i_size >> PAGE_CACHE_SHIFT; 66 end_index = i_size >> PAGE_CACHE_SHIFT;
65 67
66 /* If the wanted index is out of bounds the mft record doesn't exist. */ 68 /* If the wanted index is out of bounds the mft record doesn't exist. */
67 if (unlikely(index >= end_index)) { 69 if (unlikely(index >= end_index)) {
68 if (index > end_index || (mft_vi->i_size & ~PAGE_CACHE_MASK) < 70 if (index > end_index || (i_size & ~PAGE_CACHE_MASK) < ofs +
69 ofs + vol->mft_record_size) { 71 vol->mft_record_size) {
70 page = ERR_PTR(-ENOENT); 72 page = ERR_PTR(-ENOENT);
71 ntfs_error(vol->sb, "Attemt to read mft record 0x%lx, " 73 ntfs_error(vol->sb, "Attemt to read mft record 0x%lx, "
72 "which is beyond the end of the mft. " 74 "which is beyond the end of the mft. "
@@ -285,7 +287,7 @@ MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref,
285 } 287 }
286 unmap_mft_record(ni); 288 unmap_mft_record(ni);
287 ntfs_error(base_ni->vol->sb, "Found stale extent mft " 289 ntfs_error(base_ni->vol->sb, "Found stale extent mft "
288 "reference! Corrupt file system. " 290 "reference! Corrupt filesystem. "
289 "Run chkdsk."); 291 "Run chkdsk.");
290 return ERR_PTR(-EIO); 292 return ERR_PTR(-EIO);
291 } 293 }
@@ -316,7 +318,7 @@ map_err_out:
316 /* Verify the sequence number if it is present. */ 318 /* Verify the sequence number if it is present. */
317 if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) { 319 if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
318 ntfs_error(base_ni->vol->sb, "Found stale extent mft " 320 ntfs_error(base_ni->vol->sb, "Found stale extent mft "
319 "reference! Corrupt file system. Run chkdsk."); 321 "reference! Corrupt filesystem. Run chkdsk.");
320 destroy_ni = TRUE; 322 destroy_ni = TRUE;
321 m = ERR_PTR(-EIO); 323 m = ERR_PTR(-EIO);
322 goto unm_err_out; 324 goto unm_err_out;
@@ -509,7 +511,6 @@ int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
509 } while (bh); 511 } while (bh);
510 tail->b_this_page = head; 512 tail->b_this_page = head;
511 attach_page_buffers(page, head); 513 attach_page_buffers(page, head);
512 BUG_ON(!page_has_buffers(page));
513 } 514 }
514 bh = head = page_buffers(page); 515 bh = head = page_buffers(page);
515 BUG_ON(!bh); 516 BUG_ON(!bh);
@@ -531,6 +532,7 @@ int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
531 LCN lcn; 532 LCN lcn;
532 unsigned int vcn_ofs; 533 unsigned int vcn_ofs;
533 534
535 bh->b_bdev = vol->sb->s_bdev;
534 /* Obtain the vcn and offset of the current block. */ 536 /* Obtain the vcn and offset of the current block. */
535 vcn = ((VCN)mft_no << vol->mft_record_size_bits) + 537 vcn = ((VCN)mft_no << vol->mft_record_size_bits) +
536 (block_start - m_start); 538 (block_start - m_start);
@@ -689,7 +691,6 @@ int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
689 */ 691 */
690 if (!NInoTestClearDirty(ni)) 692 if (!NInoTestClearDirty(ni))
691 goto done; 693 goto done;
692 BUG_ON(!page_has_buffers(page));
693 bh = head = page_buffers(page); 694 bh = head = page_buffers(page);
694 BUG_ON(!bh); 695 BUG_ON(!bh);
695 rl = NULL; 696 rl = NULL;
@@ -723,6 +724,7 @@ int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
723 LCN lcn; 724 LCN lcn;
724 unsigned int vcn_ofs; 725 unsigned int vcn_ofs;
725 726
727 bh->b_bdev = vol->sb->s_bdev;
726 /* Obtain the vcn and offset of the current block. */ 728 /* Obtain the vcn and offset of the current block. */
727 vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) + 729 vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) +
728 (block_start - m_start); 730 (block_start - m_start);
@@ -946,20 +948,23 @@ BOOL ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
946 na.name_len = 0; 948 na.name_len = 0;
947 na.type = AT_UNUSED; 949 na.type = AT_UNUSED;
948 /* 950 /*
949 * For inode 0, i.e. $MFT itself, we cannot use ilookup5() from here or 951 * Optimize inode 0, i.e. $MFT itself, since we have it in memory and
950 * we deadlock because the inode is already locked by the kernel 952 * we get here for it rather often.
951 * (fs/fs-writeback.c::__sync_single_inode()) and ilookup5() waits
952 * until the inode is unlocked before returning it and it never gets
953 * unlocked because ntfs_should_write_mft_record() never returns. )-:
954 * Fortunately, we have inode 0 pinned in icache for the duration of
955 * the mount so we can access it directly.
956 */ 953 */
957 if (!mft_no) { 954 if (!mft_no) {
958 /* Balance the below iput(). */ 955 /* Balance the below iput(). */
959 vi = igrab(mft_vi); 956 vi = igrab(mft_vi);
960 BUG_ON(vi != mft_vi); 957 BUG_ON(vi != mft_vi);
961 } else 958 } else {
962 vi = ilookup5(sb, mft_no, (test_t)ntfs_test_inode, &na); 959 /*
960 * Have to use ilookup5_nowait() since ilookup5() waits for the
961 * inode lock which causes ntfs to deadlock when a concurrent
962 * inode write via the inode dirty code paths and the page
963 * dirty code path of the inode dirty code path when writing
964 * $MFT occurs.
965 */
966 vi = ilookup5_nowait(sb, mft_no, (test_t)ntfs_test_inode, &na);
967 }
963 if (vi) { 968 if (vi) {
964 ntfs_debug("Base inode 0x%lx is in icache.", mft_no); 969 ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
965 /* The inode is in icache. */ 970 /* The inode is in icache. */
@@ -1014,7 +1019,13 @@ BOOL ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
1014 na.mft_no = MREF_LE(m->base_mft_record); 1019 na.mft_no = MREF_LE(m->base_mft_record);
1015 ntfs_debug("Mft record 0x%lx is an extent record. Looking for base " 1020 ntfs_debug("Mft record 0x%lx is an extent record. Looking for base "
1016 "inode 0x%lx in icache.", mft_no, na.mft_no); 1021 "inode 0x%lx in icache.", mft_no, na.mft_no);
1017 vi = ilookup5(sb, na.mft_no, (test_t)ntfs_test_inode, &na); 1022 if (!na.mft_no) {
1023 /* Balance the below iput(). */
1024 vi = igrab(mft_vi);
1025 BUG_ON(vi != mft_vi);
1026 } else
1027 vi = ilookup5_nowait(sb, na.mft_no, (test_t)ntfs_test_inode,
1028 &na);
1018 if (!vi) { 1029 if (!vi) {
1019 /* 1030 /*
1020 * The base inode is not in icache, write this extent mft 1031 * The base inode is not in icache, write this extent mft
@@ -1121,6 +1132,7 @@ static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
1121 ntfs_inode *base_ni) 1132 ntfs_inode *base_ni)
1122{ 1133{
1123 s64 pass_end, ll, data_pos, pass_start, ofs, bit; 1134 s64 pass_end, ll, data_pos, pass_start, ofs, bit;
1135 unsigned long flags;
1124 struct address_space *mftbmp_mapping; 1136 struct address_space *mftbmp_mapping;
1125 u8 *buf, *byte; 1137 u8 *buf, *byte;
1126 struct page *page; 1138 struct page *page;
@@ -1134,9 +1146,13 @@ static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
1134 * Set the end of the pass making sure we do not overflow the mft 1146 * Set the end of the pass making sure we do not overflow the mft
1135 * bitmap. 1147 * bitmap.
1136 */ 1148 */
1149 read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
1137 pass_end = NTFS_I(vol->mft_ino)->allocated_size >> 1150 pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
1138 vol->mft_record_size_bits; 1151 vol->mft_record_size_bits;
1152 read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
1153 read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1139 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3; 1154 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
1155 read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1140 if (pass_end > ll) 1156 if (pass_end > ll)
1141 pass_end = ll; 1157 pass_end = ll;
1142 pass = 1; 1158 pass = 1;
@@ -1263,6 +1279,7 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1263{ 1279{
1264 LCN lcn; 1280 LCN lcn;
1265 s64 ll; 1281 s64 ll;
1282 unsigned long flags;
1266 struct page *page; 1283 struct page *page;
1267 ntfs_inode *mft_ni, *mftbmp_ni; 1284 ntfs_inode *mft_ni, *mftbmp_ni;
1268 runlist_element *rl, *rl2 = NULL; 1285 runlist_element *rl, *rl2 = NULL;
@@ -1284,17 +1301,20 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1284 /* 1301 /*
1285 * Determine the last lcn of the mft bitmap. The allocated size of the 1302 * Determine the last lcn of the mft bitmap. The allocated size of the
1286 * mft bitmap cannot be zero so we are ok to do this. 1303 * mft bitmap cannot be zero so we are ok to do this.
1287 * ntfs_find_vcn() returns the runlist locked on success.
1288 */ 1304 */
1289 rl = ntfs_find_vcn(mftbmp_ni, (mftbmp_ni->allocated_size - 1) >> 1305 down_write(&mftbmp_ni->runlist.lock);
1290 vol->cluster_size_bits, TRUE); 1306 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1307 ll = mftbmp_ni->allocated_size;
1308 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1309 rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
1310 (ll - 1) >> vol->cluster_size_bits, TRUE);
1291 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) { 1311 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
1312 up_write(&mftbmp_ni->runlist.lock);
1292 ntfs_error(vol->sb, "Failed to determine last allocated " 1313 ntfs_error(vol->sb, "Failed to determine last allocated "
1293 "cluster of mft bitmap attribute."); 1314 "cluster of mft bitmap attribute.");
1294 if (!IS_ERR(rl)) { 1315 if (!IS_ERR(rl))
1295 up_write(&mftbmp_ni->runlist.lock);
1296 ret = -EIO; 1316 ret = -EIO;
1297 } else 1317 else
1298 ret = PTR_ERR(rl); 1318 ret = PTR_ERR(rl);
1299 return ret; 1319 return ret;
1300 } 1320 }
@@ -1396,7 +1416,7 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1396 BUG_ON(ll < rl2->vcn); 1416 BUG_ON(ll < rl2->vcn);
1397 BUG_ON(ll >= rl2->vcn + rl2->length); 1417 BUG_ON(ll >= rl2->vcn + rl2->length);
1398 /* Get the size for the new mapping pairs array for this extent. */ 1418 /* Get the size for the new mapping pairs array for this extent. */
1399 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll); 1419 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1400 if (unlikely(mp_size <= 0)) { 1420 if (unlikely(mp_size <= 0)) {
1401 ntfs_error(vol->sb, "Get size for mapping pairs failed for " 1421 ntfs_error(vol->sb, "Get size for mapping pairs failed for "
1402 "mft bitmap attribute extent."); 1422 "mft bitmap attribute extent.");
@@ -1418,6 +1438,8 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1418 // TODO: Deal with this by moving this extent to a new mft 1438 // TODO: Deal with this by moving this extent to a new mft
1419 // record or by starting a new extent in a new mft record or by 1439 // record or by starting a new extent in a new mft record or by
1420 // moving other attributes out of this mft record. 1440 // moving other attributes out of this mft record.
1441 // Note: It will need to be a special mft record and if none of
1442 // those are available it gets rather complicated...
1421 ntfs_error(vol->sb, "Not enough space in this mft record to " 1443 ntfs_error(vol->sb, "Not enough space in this mft record to "
1422 "accomodate extended mft bitmap attribute " 1444 "accomodate extended mft bitmap attribute "
1423 "extent. Cannot handle this yet."); 1445 "extent. Cannot handle this yet.");
@@ -1428,7 +1450,7 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1428 /* Generate the mapping pairs array directly into the attr record. */ 1450 /* Generate the mapping pairs array directly into the attr record. */
1429 ret = ntfs_mapping_pairs_build(vol, (u8*)a + 1451 ret = ntfs_mapping_pairs_build(vol, (u8*)a +
1430 le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 1452 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1431 mp_size, rl2, ll, NULL); 1453 mp_size, rl2, ll, -1, NULL);
1432 if (unlikely(ret)) { 1454 if (unlikely(ret)) {
1433 ntfs_error(vol->sb, "Failed to build mapping pairs array for " 1455 ntfs_error(vol->sb, "Failed to build mapping pairs array for "
1434 "mft bitmap attribute."); 1456 "mft bitmap attribute.");
@@ -1458,9 +1480,11 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1458 } 1480 }
1459 a = ctx->attr; 1481 a = ctx->attr;
1460 } 1482 }
1483 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1461 mftbmp_ni->allocated_size += vol->cluster_size; 1484 mftbmp_ni->allocated_size += vol->cluster_size;
1462 a->data.non_resident.allocated_size = 1485 a->data.non_resident.allocated_size =
1463 cpu_to_sle64(mftbmp_ni->allocated_size); 1486 cpu_to_sle64(mftbmp_ni->allocated_size);
1487 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1464 /* Ensure the changes make it to disk. */ 1488 /* Ensure the changes make it to disk. */
1465 flush_dcache_mft_record_page(ctx->ntfs_ino); 1489 flush_dcache_mft_record_page(ctx->ntfs_ino);
1466 mark_mft_record_dirty(ctx->ntfs_ino); 1490 mark_mft_record_dirty(ctx->ntfs_ino);
@@ -1476,7 +1500,9 @@ restore_undo_alloc:
1476 0, ctx)) { 1500 0, ctx)) {
1477 ntfs_error(vol->sb, "Failed to find last attribute extent of " 1501 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1478 "mft bitmap attribute.%s", es); 1502 "mft bitmap attribute.%s", es);
1503 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1479 mftbmp_ni->allocated_size += vol->cluster_size; 1504 mftbmp_ni->allocated_size += vol->cluster_size;
1505 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1480 ntfs_attr_put_search_ctx(ctx); 1506 ntfs_attr_put_search_ctx(ctx);
1481 unmap_mft_record(mft_ni); 1507 unmap_mft_record(mft_ni);
1482 up_write(&mftbmp_ni->runlist.lock); 1508 up_write(&mftbmp_ni->runlist.lock);
@@ -1512,7 +1538,7 @@ undo_alloc:
1512 a->data.non_resident.mapping_pairs_offset), 1538 a->data.non_resident.mapping_pairs_offset),
1513 old_alen - le16_to_cpu( 1539 old_alen - le16_to_cpu(
1514 a->data.non_resident.mapping_pairs_offset), 1540 a->data.non_resident.mapping_pairs_offset),
1515 rl2, ll, NULL)) { 1541 rl2, ll, -1, NULL)) {
1516 ntfs_error(vol->sb, "Failed to restore mapping pairs " 1542 ntfs_error(vol->sb, "Failed to restore mapping pairs "
1517 "array.%s", es); 1543 "array.%s", es);
1518 NVolSetErrors(vol); 1544 NVolSetErrors(vol);
@@ -1550,6 +1576,7 @@ undo_alloc:
1550static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol) 1576static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1551{ 1577{
1552 s64 old_data_size, old_initialized_size; 1578 s64 old_data_size, old_initialized_size;
1579 unsigned long flags;
1553 struct inode *mftbmp_vi; 1580 struct inode *mftbmp_vi;
1554 ntfs_inode *mft_ni, *mftbmp_ni; 1581 ntfs_inode *mft_ni, *mftbmp_ni;
1555 ntfs_attr_search_ctx *ctx; 1582 ntfs_attr_search_ctx *ctx;
@@ -1583,7 +1610,8 @@ static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1583 goto put_err_out; 1610 goto put_err_out;
1584 } 1611 }
1585 a = ctx->attr; 1612 a = ctx->attr;
1586 old_data_size = mftbmp_vi->i_size; 1613 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1614 old_data_size = i_size_read(mftbmp_vi);
1587 old_initialized_size = mftbmp_ni->initialized_size; 1615 old_initialized_size = mftbmp_ni->initialized_size;
1588 /* 1616 /*
1589 * We can simply update the initialized_size before filling the space 1617 * We can simply update the initialized_size before filling the space
@@ -1593,11 +1621,12 @@ static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1593 mftbmp_ni->initialized_size += 8; 1621 mftbmp_ni->initialized_size += 8;
1594 a->data.non_resident.initialized_size = 1622 a->data.non_resident.initialized_size =
1595 cpu_to_sle64(mftbmp_ni->initialized_size); 1623 cpu_to_sle64(mftbmp_ni->initialized_size);
1596 if (mftbmp_ni->initialized_size > mftbmp_vi->i_size) { 1624 if (mftbmp_ni->initialized_size > old_data_size) {
1597 mftbmp_vi->i_size = mftbmp_ni->initialized_size; 1625 i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
1598 a->data.non_resident.data_size = 1626 a->data.non_resident.data_size =
1599 cpu_to_sle64(mftbmp_vi->i_size); 1627 cpu_to_sle64(mftbmp_ni->initialized_size);
1600 } 1628 }
1629 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1601 /* Ensure the changes make it to disk. */ 1630 /* Ensure the changes make it to disk. */
1602 flush_dcache_mft_record_page(ctx->ntfs_ino); 1631 flush_dcache_mft_record_page(ctx->ntfs_ino);
1603 mark_mft_record_dirty(ctx->ntfs_ino); 1632 mark_mft_record_dirty(ctx->ntfs_ino);
@@ -1636,22 +1665,28 @@ unm_err_out:
1636 goto err_out; 1665 goto err_out;
1637 } 1666 }
1638 a = ctx->attr; 1667 a = ctx->attr;
1668 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1639 mftbmp_ni->initialized_size = old_initialized_size; 1669 mftbmp_ni->initialized_size = old_initialized_size;
1640 a->data.non_resident.initialized_size = 1670 a->data.non_resident.initialized_size =
1641 cpu_to_sle64(old_initialized_size); 1671 cpu_to_sle64(old_initialized_size);
1642 if (mftbmp_vi->i_size != old_data_size) { 1672 if (i_size_read(mftbmp_vi) != old_data_size) {
1643 mftbmp_vi->i_size = old_data_size; 1673 i_size_write(mftbmp_vi, old_data_size);
1644 a->data.non_resident.data_size = cpu_to_sle64(old_data_size); 1674 a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
1645 } 1675 }
1676 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1646 flush_dcache_mft_record_page(ctx->ntfs_ino); 1677 flush_dcache_mft_record_page(ctx->ntfs_ino);
1647 mark_mft_record_dirty(ctx->ntfs_ino); 1678 mark_mft_record_dirty(ctx->ntfs_ino);
1648 ntfs_attr_put_search_ctx(ctx); 1679 ntfs_attr_put_search_ctx(ctx);
1649 unmap_mft_record(mft_ni); 1680 unmap_mft_record(mft_ni);
1681#ifdef DEBUG
1682 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1650 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, " 1683 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
1651 "data_size 0x%llx, initialized_size 0x%llx.", 1684 "data_size 0x%llx, initialized_size 0x%llx.",
1652 (long long)mftbmp_ni->allocated_size, 1685 (long long)mftbmp_ni->allocated_size,
1653 (long long)mftbmp_vi->i_size, 1686 (long long)i_size_read(mftbmp_vi),
1654 (long long)mftbmp_ni->initialized_size); 1687 (long long)mftbmp_ni->initialized_size);
1688 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1689#endif /* DEBUG */
1655err_out: 1690err_out:
1656 return ret; 1691 return ret;
1657} 1692}
@@ -1679,7 +1714,8 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1679{ 1714{
1680 LCN lcn; 1715 LCN lcn;
1681 VCN old_last_vcn; 1716 VCN old_last_vcn;
1682 s64 min_nr, nr, ll = 0; 1717 s64 min_nr, nr, ll;
1718 unsigned long flags;
1683 ntfs_inode *mft_ni; 1719 ntfs_inode *mft_ni;
1684 runlist_element *rl, *rl2; 1720 runlist_element *rl, *rl2;
1685 ntfs_attr_search_ctx *ctx = NULL; 1721 ntfs_attr_search_ctx *ctx = NULL;
@@ -1695,23 +1731,25 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1695 * Determine the preferred allocation location, i.e. the last lcn of 1731 * Determine the preferred allocation location, i.e. the last lcn of
1696 * the mft data attribute. The allocated size of the mft data 1732 * the mft data attribute. The allocated size of the mft data
1697 * attribute cannot be zero so we are ok to do this. 1733 * attribute cannot be zero so we are ok to do this.
1698 * ntfs_find_vcn() returns the runlist locked on success.
1699 */ 1734 */
1700 rl = ntfs_find_vcn(mft_ni, (mft_ni->allocated_size - 1) >> 1735 down_write(&mft_ni->runlist.lock);
1701 vol->cluster_size_bits, TRUE); 1736 read_lock_irqsave(&mft_ni->size_lock, flags);
1737 ll = mft_ni->allocated_size;
1738 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1739 rl = ntfs_attr_find_vcn_nolock(mft_ni,
1740 (ll - 1) >> vol->cluster_size_bits, TRUE);
1702 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) { 1741 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
1742 up_write(&mft_ni->runlist.lock);
1703 ntfs_error(vol->sb, "Failed to determine last allocated " 1743 ntfs_error(vol->sb, "Failed to determine last allocated "
1704 "cluster of mft data attribute."); 1744 "cluster of mft data attribute.");
1705 if (!IS_ERR(rl)) { 1745 if (!IS_ERR(rl))
1706 up_write(&mft_ni->runlist.lock);
1707 ret = -EIO; 1746 ret = -EIO;
1708 } else 1747 else
1709 ret = PTR_ERR(rl); 1748 ret = PTR_ERR(rl);
1710 return ret; 1749 return ret;
1711 } 1750 }
1712 lcn = rl->lcn + rl->length; 1751 lcn = rl->lcn + rl->length;
1713 ntfs_debug("Last lcn of mft data attribute is 0x%llx.", 1752 ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn);
1714 (long long)lcn);
1715 /* Minimum allocation is one mft record worth of clusters. */ 1753 /* Minimum allocation is one mft record worth of clusters. */
1716 min_nr = vol->mft_record_size >> vol->cluster_size_bits; 1754 min_nr = vol->mft_record_size >> vol->cluster_size_bits;
1717 if (!min_nr) 1755 if (!min_nr)
@@ -1721,12 +1759,13 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1721 if (!nr) 1759 if (!nr)
1722 nr = min_nr; 1760 nr = min_nr;
1723 /* Ensure we do not go above 2^32-1 mft records. */ 1761 /* Ensure we do not go above 2^32-1 mft records. */
1724 if (unlikely((mft_ni->allocated_size + 1762 read_lock_irqsave(&mft_ni->size_lock, flags);
1725 (nr << vol->cluster_size_bits)) >> 1763 ll = mft_ni->allocated_size;
1764 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1765 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1726 vol->mft_record_size_bits >= (1ll << 32))) { 1766 vol->mft_record_size_bits >= (1ll << 32))) {
1727 nr = min_nr; 1767 nr = min_nr;
1728 if (unlikely((mft_ni->allocated_size + 1768 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1729 (nr << vol->cluster_size_bits)) >>
1730 vol->mft_record_size_bits >= (1ll << 32))) { 1769 vol->mft_record_size_bits >= (1ll << 32))) {
1731 ntfs_warning(vol->sb, "Cannot allocate mft record " 1770 ntfs_warning(vol->sb, "Cannot allocate mft record "
1732 "because the maximum number of inodes " 1771 "because the maximum number of inodes "
@@ -1772,7 +1811,7 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1772 return PTR_ERR(rl); 1811 return PTR_ERR(rl);
1773 } 1812 }
1774 mft_ni->runlist.rl = rl; 1813 mft_ni->runlist.rl = rl;
1775 ntfs_debug("Allocated %lli clusters.", nr); 1814 ntfs_debug("Allocated %lli clusters.", (long long)nr);
1776 /* Find the last run in the new runlist. */ 1815 /* Find the last run in the new runlist. */
1777 for (; rl[1].length; rl++) 1816 for (; rl[1].length; rl++)
1778 ; 1817 ;
@@ -1808,7 +1847,7 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1808 BUG_ON(ll < rl2->vcn); 1847 BUG_ON(ll < rl2->vcn);
1809 BUG_ON(ll >= rl2->vcn + rl2->length); 1848 BUG_ON(ll >= rl2->vcn + rl2->length);
1810 /* Get the size for the new mapping pairs array for this extent. */ 1849 /* Get the size for the new mapping pairs array for this extent. */
1811 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll); 1850 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1812 if (unlikely(mp_size <= 0)) { 1851 if (unlikely(mp_size <= 0)) {
1813 ntfs_error(vol->sb, "Get size for mapping pairs failed for " 1852 ntfs_error(vol->sb, "Get size for mapping pairs failed for "
1814 "mft data attribute extent."); 1853 "mft data attribute extent.");
@@ -1832,7 +1871,11 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1832 // moving other attributes out of this mft record. 1871 // moving other attributes out of this mft record.
1833 // Note: Use the special reserved mft records and ensure that 1872 // Note: Use the special reserved mft records and ensure that
1834 // this extent is not required to find the mft record in 1873 // this extent is not required to find the mft record in
1835 // question. 1874 // question. If no free special records left we would need to
1875 // move an existing record away, insert ours in its place, and
1876 // then place the moved record into the newly allocated space
1877 // and we would then need to update all references to this mft
1878 // record appropriately. This is rather complicated...
1836 ntfs_error(vol->sb, "Not enough space in this mft record to " 1879 ntfs_error(vol->sb, "Not enough space in this mft record to "
1837 "accomodate extended mft data attribute " 1880 "accomodate extended mft data attribute "
1838 "extent. Cannot handle this yet."); 1881 "extent. Cannot handle this yet.");
@@ -1843,7 +1886,7 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1843 /* Generate the mapping pairs array directly into the attr record. */ 1886 /* Generate the mapping pairs array directly into the attr record. */
1844 ret = ntfs_mapping_pairs_build(vol, (u8*)a + 1887 ret = ntfs_mapping_pairs_build(vol, (u8*)a +
1845 le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 1888 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1846 mp_size, rl2, ll, NULL); 1889 mp_size, rl2, ll, -1, NULL);
1847 if (unlikely(ret)) { 1890 if (unlikely(ret)) {
1848 ntfs_error(vol->sb, "Failed to build mapping pairs array of " 1891 ntfs_error(vol->sb, "Failed to build mapping pairs array of "
1849 "mft data attribute."); 1892 "mft data attribute.");
@@ -1875,9 +1918,11 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1875 } 1918 }
1876 a = ctx->attr; 1919 a = ctx->attr;
1877 } 1920 }
1921 write_lock_irqsave(&mft_ni->size_lock, flags);
1878 mft_ni->allocated_size += nr << vol->cluster_size_bits; 1922 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1879 a->data.non_resident.allocated_size = 1923 a->data.non_resident.allocated_size =
1880 cpu_to_sle64(mft_ni->allocated_size); 1924 cpu_to_sle64(mft_ni->allocated_size);
1925 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1881 /* Ensure the changes make it to disk. */ 1926 /* Ensure the changes make it to disk. */
1882 flush_dcache_mft_record_page(ctx->ntfs_ino); 1927 flush_dcache_mft_record_page(ctx->ntfs_ino);
1883 mark_mft_record_dirty(ctx->ntfs_ino); 1928 mark_mft_record_dirty(ctx->ntfs_ino);
@@ -1892,7 +1937,9 @@ restore_undo_alloc:
1892 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) { 1937 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
1893 ntfs_error(vol->sb, "Failed to find last attribute extent of " 1938 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1894 "mft data attribute.%s", es); 1939 "mft data attribute.%s", es);
1940 write_lock_irqsave(&mft_ni->size_lock, flags);
1895 mft_ni->allocated_size += nr << vol->cluster_size_bits; 1941 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1942 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1896 ntfs_attr_put_search_ctx(ctx); 1943 ntfs_attr_put_search_ctx(ctx);
1897 unmap_mft_record(mft_ni); 1944 unmap_mft_record(mft_ni);
1898 up_write(&mft_ni->runlist.lock); 1945 up_write(&mft_ni->runlist.lock);
@@ -1906,7 +1953,7 @@ restore_undo_alloc:
1906 a = ctx->attr; 1953 a = ctx->attr;
1907 a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1); 1954 a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1);
1908undo_alloc: 1955undo_alloc:
1909 if (ntfs_cluster_free(vol->mft_ino, old_last_vcn, -1) < 0) { 1956 if (ntfs_cluster_free(vol->mft_ino, old_last_vcn, -1, TRUE) < 0) {
1910 ntfs_error(vol->sb, "Failed to free clusters from mft data " 1957 ntfs_error(vol->sb, "Failed to free clusters from mft data "
1911 "attribute.%s", es); 1958 "attribute.%s", es);
1912 NVolSetErrors(vol); 1959 NVolSetErrors(vol);
@@ -1921,7 +1968,7 @@ undo_alloc:
1921 a->data.non_resident.mapping_pairs_offset), 1968 a->data.non_resident.mapping_pairs_offset),
1922 old_alen - le16_to_cpu( 1969 old_alen - le16_to_cpu(
1923 a->data.non_resident.mapping_pairs_offset), 1970 a->data.non_resident.mapping_pairs_offset),
1924 rl2, ll, NULL)) { 1971 rl2, ll, -1, NULL)) {
1925 ntfs_error(vol->sb, "Failed to restore mapping pairs " 1972 ntfs_error(vol->sb, "Failed to restore mapping pairs "
1926 "array.%s", es); 1973 "array.%s", es);
1927 NVolSetErrors(vol); 1974 NVolSetErrors(vol);
@@ -1991,7 +2038,7 @@ static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
1991 "reports this as corruption, please email " 2038 "reports this as corruption, please email "
1992 "linux-ntfs-dev@lists.sourceforge.net stating " 2039 "linux-ntfs-dev@lists.sourceforge.net stating "
1993 "that you saw this message and that the " 2040 "that you saw this message and that the "
1994 "modified file system created was corrupt. " 2041 "modified filesystem created was corrupt. "
1995 "Thank you."); 2042 "Thank you.");
1996 } 2043 }
1997 /* Set the update sequence number to 1. */ 2044 /* Set the update sequence number to 1. */
@@ -2036,6 +2083,7 @@ static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
2036 */ 2083 */
2037static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no) 2084static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
2038{ 2085{
2086 loff_t i_size;
2039 struct inode *mft_vi = vol->mft_ino; 2087 struct inode *mft_vi = vol->mft_ino;
2040 struct page *page; 2088 struct page *page;
2041 MFT_RECORD *m; 2089 MFT_RECORD *m;
@@ -2051,10 +2099,11 @@ static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
2051 index = mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT; 2099 index = mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
2052 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK; 2100 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
2053 /* The maximum valid index into the page cache for $MFT's data. */ 2101 /* The maximum valid index into the page cache for $MFT's data. */
2054 end_index = mft_vi->i_size >> PAGE_CACHE_SHIFT; 2102 i_size = i_size_read(mft_vi);
2103 end_index = i_size >> PAGE_CACHE_SHIFT;
2055 if (unlikely(index >= end_index)) { 2104 if (unlikely(index >= end_index)) {
2056 if (unlikely(index > end_index || ofs + vol->mft_record_size >= 2105 if (unlikely(index > end_index || ofs + vol->mft_record_size >=
2057 (mft_vi->i_size & ~PAGE_CACHE_MASK))) { 2106 (i_size & ~PAGE_CACHE_MASK))) {
2058 ntfs_error(vol->sb, "Tried to format non-existing mft " 2107 ntfs_error(vol->sb, "Tried to format non-existing mft "
2059 "record 0x%llx.", (long long)mft_no); 2108 "record 0x%llx.", (long long)mft_no);
2060 return -ENOENT; 2109 return -ENOENT;
@@ -2188,6 +2237,7 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2188 ntfs_inode *base_ni, MFT_RECORD **mrec) 2237 ntfs_inode *base_ni, MFT_RECORD **mrec)
2189{ 2238{
2190 s64 ll, bit, old_data_initialized, old_data_size; 2239 s64 ll, bit, old_data_initialized, old_data_size;
2240 unsigned long flags;
2191 struct inode *vi; 2241 struct inode *vi;
2192 struct page *page; 2242 struct page *page;
2193 ntfs_inode *mft_ni, *mftbmp_ni, *ni; 2243 ntfs_inode *mft_ni, *mftbmp_ni, *ni;
@@ -2237,9 +2287,13 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2237 * the first 24 mft records as they are special and whilst they may not 2287 * the first 24 mft records as they are special and whilst they may not
2238 * be in use, we do not allocate from them. 2288 * be in use, we do not allocate from them.
2239 */ 2289 */
2290 read_lock_irqsave(&mft_ni->size_lock, flags);
2240 ll = mft_ni->initialized_size >> vol->mft_record_size_bits; 2291 ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
2241 if (mftbmp_ni->initialized_size << 3 > ll && 2292 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2242 mftbmp_ni->initialized_size > 3) { 2293 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2294 old_data_initialized = mftbmp_ni->initialized_size;
2295 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2296 if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
2243 bit = ll; 2297 bit = ll;
2244 if (bit < 24) 2298 if (bit < 24)
2245 bit = 24; 2299 bit = 24;
@@ -2254,15 +2308,18 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2254 * mft record that we can allocate. 2308 * mft record that we can allocate.
2255 * Note: The smallest mft record we allocate is mft record 24. 2309 * Note: The smallest mft record we allocate is mft record 24.
2256 */ 2310 */
2257 bit = mftbmp_ni->initialized_size << 3; 2311 bit = old_data_initialized << 3;
2258 if (unlikely(bit >= (1ll << 32))) 2312 if (unlikely(bit >= (1ll << 32)))
2259 goto max_err_out; 2313 goto max_err_out;
2314 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2315 old_data_size = mftbmp_ni->allocated_size;
2260 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, " 2316 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
2261 "data_size 0x%llx, initialized_size 0x%llx.", 2317 "data_size 0x%llx, initialized_size 0x%llx.",
2262 (long long)mftbmp_ni->allocated_size, 2318 (long long)old_data_size,
2263 (long long)vol->mftbmp_ino->i_size, 2319 (long long)i_size_read(vol->mftbmp_ino),
2264 (long long)mftbmp_ni->initialized_size); 2320 (long long)old_data_initialized);
2265 if (mftbmp_ni->initialized_size + 8 > mftbmp_ni->allocated_size) { 2321 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2322 if (old_data_initialized + 8 > old_data_size) {
2266 /* Need to extend bitmap by one more cluster. */ 2323 /* Need to extend bitmap by one more cluster. */
2267 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size."); 2324 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
2268 err = ntfs_mft_bitmap_extend_allocation_nolock(vol); 2325 err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
@@ -2270,12 +2327,16 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2270 up_write(&vol->mftbmp_lock); 2327 up_write(&vol->mftbmp_lock);
2271 goto err_out; 2328 goto err_out;
2272 } 2329 }
2330#ifdef DEBUG
2331 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2273 ntfs_debug("Status of mftbmp after allocation extension: " 2332 ntfs_debug("Status of mftbmp after allocation extension: "
2274 "allocated_size 0x%llx, data_size 0x%llx, " 2333 "allocated_size 0x%llx, data_size 0x%llx, "
2275 "initialized_size 0x%llx.", 2334 "initialized_size 0x%llx.",
2276 (long long)mftbmp_ni->allocated_size, 2335 (long long)mftbmp_ni->allocated_size,
2277 (long long)vol->mftbmp_ino->i_size, 2336 (long long)i_size_read(vol->mftbmp_ino),
2278 (long long)mftbmp_ni->initialized_size); 2337 (long long)mftbmp_ni->initialized_size);
2338 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2339#endif /* DEBUG */
2279 } 2340 }
2280 /* 2341 /*
2281 * We now have sufficient allocated space, extend the initialized_size 2342 * We now have sufficient allocated space, extend the initialized_size
@@ -2287,12 +2348,16 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2287 up_write(&vol->mftbmp_lock); 2348 up_write(&vol->mftbmp_lock);
2288 goto err_out; 2349 goto err_out;
2289 } 2350 }
2351#ifdef DEBUG
2352 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2290 ntfs_debug("Status of mftbmp after initialized extention: " 2353 ntfs_debug("Status of mftbmp after initialized extention: "
2291 "allocated_size 0x%llx, data_size 0x%llx, " 2354 "allocated_size 0x%llx, data_size 0x%llx, "
2292 "initialized_size 0x%llx.", 2355 "initialized_size 0x%llx.",
2293 (long long)mftbmp_ni->allocated_size, 2356 (long long)mftbmp_ni->allocated_size,
2294 (long long)vol->mftbmp_ino->i_size, 2357 (long long)i_size_read(vol->mftbmp_ino),
2295 (long long)mftbmp_ni->initialized_size); 2358 (long long)mftbmp_ni->initialized_size);
2359 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2360#endif /* DEBUG */
2296 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit); 2361 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
2297found_free_rec: 2362found_free_rec:
2298 /* @bit is the found free mft record, allocate it in the mft bitmap. */ 2363 /* @bit is the found free mft record, allocate it in the mft bitmap. */
@@ -2314,7 +2379,10 @@ have_alloc_rec:
2314 * parallel allocation could allocate the same mft record as this one. 2379 * parallel allocation could allocate the same mft record as this one.
2315 */ 2380 */
2316 ll = (bit + 1) << vol->mft_record_size_bits; 2381 ll = (bit + 1) << vol->mft_record_size_bits;
2317 if (ll <= mft_ni->initialized_size) { 2382 read_lock_irqsave(&mft_ni->size_lock, flags);
2383 old_data_initialized = mft_ni->initialized_size;
2384 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2385 if (ll <= old_data_initialized) {
2318 ntfs_debug("Allocated mft record already initialized."); 2386 ntfs_debug("Allocated mft record already initialized.");
2319 goto mft_rec_already_initialized; 2387 goto mft_rec_already_initialized;
2320 } 2388 }
@@ -2325,26 +2393,30 @@ have_alloc_rec:
2325 * actually traversed more than once when a freshly formatted volume is 2393 * actually traversed more than once when a freshly formatted volume is
2326 * first written to so it optimizes away nicely in the common case. 2394 * first written to so it optimizes away nicely in the common case.
2327 */ 2395 */
2396 read_lock_irqsave(&mft_ni->size_lock, flags);
2328 ntfs_debug("Status of mft data before extension: " 2397 ntfs_debug("Status of mft data before extension: "
2329 "allocated_size 0x%llx, data_size 0x%llx, " 2398 "allocated_size 0x%llx, data_size 0x%llx, "
2330 "initialized_size 0x%llx.", 2399 "initialized_size 0x%llx.",
2331 (long long)mft_ni->allocated_size, 2400 (long long)mft_ni->allocated_size,
2332 (long long)vol->mft_ino->i_size, 2401 (long long)i_size_read(vol->mft_ino),
2333 (long long)mft_ni->initialized_size); 2402 (long long)mft_ni->initialized_size);
2334 while (ll > mft_ni->allocated_size) { 2403 while (ll > mft_ni->allocated_size) {
2404 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2335 err = ntfs_mft_data_extend_allocation_nolock(vol); 2405 err = ntfs_mft_data_extend_allocation_nolock(vol);
2336 if (unlikely(err)) { 2406 if (unlikely(err)) {
2337 ntfs_error(vol->sb, "Failed to extend mft data " 2407 ntfs_error(vol->sb, "Failed to extend mft data "
2338 "allocation."); 2408 "allocation.");
2339 goto undo_mftbmp_alloc_nolock; 2409 goto undo_mftbmp_alloc_nolock;
2340 } 2410 }
2411 read_lock_irqsave(&mft_ni->size_lock, flags);
2341 ntfs_debug("Status of mft data after allocation extension: " 2412 ntfs_debug("Status of mft data after allocation extension: "
2342 "allocated_size 0x%llx, data_size 0x%llx, " 2413 "allocated_size 0x%llx, data_size 0x%llx, "
2343 "initialized_size 0x%llx.", 2414 "initialized_size 0x%llx.",
2344 (long long)mft_ni->allocated_size, 2415 (long long)mft_ni->allocated_size,
2345 (long long)vol->mft_ino->i_size, 2416 (long long)i_size_read(vol->mft_ino),
2346 (long long)mft_ni->initialized_size); 2417 (long long)mft_ni->initialized_size);
2347 } 2418 }
2419 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2348 /* 2420 /*
2349 * Extend mft data initialized size (and data size of course) to reach 2421 * Extend mft data initialized size (and data size of course) to reach
2350 * the allocated mft record, formatting the mft records allong the way. 2422 * the allocated mft record, formatting the mft records allong the way.
@@ -2352,6 +2424,7 @@ have_alloc_rec:
2352 * needed by ntfs_mft_record_format(). We will update the attribute 2424 * needed by ntfs_mft_record_format(). We will update the attribute
2353 * record itself in one fell swoop later on. 2425 * record itself in one fell swoop later on.
2354 */ 2426 */
2427 write_lock_irqsave(&mft_ni->size_lock, flags);
2355 old_data_initialized = mft_ni->initialized_size; 2428 old_data_initialized = mft_ni->initialized_size;
2356 old_data_size = vol->mft_ino->i_size; 2429 old_data_size = vol->mft_ino->i_size;
2357 while (ll > mft_ni->initialized_size) { 2430 while (ll > mft_ni->initialized_size) {
@@ -2360,8 +2433,9 @@ have_alloc_rec:
2360 new_initialized_size = mft_ni->initialized_size + 2433 new_initialized_size = mft_ni->initialized_size +
2361 vol->mft_record_size; 2434 vol->mft_record_size;
2362 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits; 2435 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
2363 if (new_initialized_size > vol->mft_ino->i_size) 2436 if (new_initialized_size > i_size_read(vol->mft_ino))
2364 vol->mft_ino->i_size = new_initialized_size; 2437 i_size_write(vol->mft_ino, new_initialized_size);
2438 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2365 ntfs_debug("Initializing mft record 0x%llx.", 2439 ntfs_debug("Initializing mft record 0x%llx.",
2366 (long long)mft_no); 2440 (long long)mft_no);
2367 err = ntfs_mft_record_format(vol, mft_no); 2441 err = ntfs_mft_record_format(vol, mft_no);
@@ -2369,8 +2443,10 @@ have_alloc_rec:
2369 ntfs_error(vol->sb, "Failed to format mft record."); 2443 ntfs_error(vol->sb, "Failed to format mft record.");
2370 goto undo_data_init; 2444 goto undo_data_init;
2371 } 2445 }
2446 write_lock_irqsave(&mft_ni->size_lock, flags);
2372 mft_ni->initialized_size = new_initialized_size; 2447 mft_ni->initialized_size = new_initialized_size;
2373 } 2448 }
2449 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2374 record_formatted = TRUE; 2450 record_formatted = TRUE;
2375 /* Update the mft data attribute record to reflect the new sizes. */ 2451 /* Update the mft data attribute record to reflect the new sizes. */
2376 m = map_mft_record(mft_ni); 2452 m = map_mft_record(mft_ni);
@@ -2396,22 +2472,27 @@ have_alloc_rec:
2396 goto undo_data_init; 2472 goto undo_data_init;
2397 } 2473 }
2398 a = ctx->attr; 2474 a = ctx->attr;
2475 read_lock_irqsave(&mft_ni->size_lock, flags);
2399 a->data.non_resident.initialized_size = 2476 a->data.non_resident.initialized_size =
2400 cpu_to_sle64(mft_ni->initialized_size); 2477 cpu_to_sle64(mft_ni->initialized_size);
2401 a->data.non_resident.data_size = cpu_to_sle64(vol->mft_ino->i_size); 2478 a->data.non_resident.data_size =
2479 cpu_to_sle64(i_size_read(vol->mft_ino));
2480 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2402 /* Ensure the changes make it to disk. */ 2481 /* Ensure the changes make it to disk. */
2403 flush_dcache_mft_record_page(ctx->ntfs_ino); 2482 flush_dcache_mft_record_page(ctx->ntfs_ino);
2404 mark_mft_record_dirty(ctx->ntfs_ino); 2483 mark_mft_record_dirty(ctx->ntfs_ino);
2405 ntfs_attr_put_search_ctx(ctx); 2484 ntfs_attr_put_search_ctx(ctx);
2406 unmap_mft_record(mft_ni); 2485 unmap_mft_record(mft_ni);
2486 read_lock_irqsave(&mft_ni->size_lock, flags);
2407 ntfs_debug("Status of mft data after mft record initialization: " 2487 ntfs_debug("Status of mft data after mft record initialization: "
2408 "allocated_size 0x%llx, data_size 0x%llx, " 2488 "allocated_size 0x%llx, data_size 0x%llx, "
2409 "initialized_size 0x%llx.", 2489 "initialized_size 0x%llx.",
2410 (long long)mft_ni->allocated_size, 2490 (long long)mft_ni->allocated_size,
2411 (long long)vol->mft_ino->i_size, 2491 (long long)i_size_read(vol->mft_ino),
2412 (long long)mft_ni->initialized_size); 2492 (long long)mft_ni->initialized_size);
2413 BUG_ON(vol->mft_ino->i_size > mft_ni->allocated_size); 2493 BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
2414 BUG_ON(mft_ni->initialized_size > vol->mft_ino->i_size); 2494 BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
2495 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2415mft_rec_already_initialized: 2496mft_rec_already_initialized:
2416 /* 2497 /*
2417 * We can finally drop the mft bitmap lock as the mft data attribute 2498 * We can finally drop the mft bitmap lock as the mft data attribute
@@ -2652,8 +2733,10 @@ mft_rec_already_initialized:
2652 *mrec = m; 2733 *mrec = m;
2653 return ni; 2734 return ni;
2654undo_data_init: 2735undo_data_init:
2736 write_lock_irqsave(&mft_ni->size_lock, flags);
2655 mft_ni->initialized_size = old_data_initialized; 2737 mft_ni->initialized_size = old_data_initialized;
2656 vol->mft_ino->i_size = old_data_size; 2738 i_size_write(vol->mft_ino, old_data_size);
2739 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2657 goto undo_mftbmp_alloc_nolock; 2740 goto undo_mftbmp_alloc_nolock;
2658undo_mftbmp_alloc: 2741undo_mftbmp_alloc:
2659 down_write(&vol->mftbmp_lock); 2742 down_write(&vol->mftbmp_lock);
diff --git a/fs/ntfs/namei.c b/fs/ntfs/namei.c
index 7c7e13b43b2e..351dbc3b6e40 100644
--- a/fs/ntfs/namei.c
+++ b/fs/ntfs/namei.c
@@ -153,8 +153,7 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
153 ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with " 153 ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
154 "error code %li.", dent_ino, 154 "error code %li.", dent_ino,
155 PTR_ERR(dent_inode)); 155 PTR_ERR(dent_inode));
156 if (name) 156 kfree(name);
157 kfree(name);
158 /* Return the error code. */ 157 /* Return the error code. */
159 return (struct dentry *)dent_inode; 158 return (struct dentry *)dent_inode;
160 } 159 }
@@ -380,7 +379,7 @@ struct inode_operations ntfs_dir_inode_ops = {
380 * Return the dentry of the parent directory on success or the error code on 379 * Return the dentry of the parent directory on success or the error code on
381 * error (IS_ERR() is true). 380 * error (IS_ERR() is true).
382 */ 381 */
383struct dentry *ntfs_get_parent(struct dentry *child_dent) 382static struct dentry *ntfs_get_parent(struct dentry *child_dent)
384{ 383{
385 struct inode *vi = child_dent->d_inode; 384 struct inode *vi = child_dent->d_inode;
386 ntfs_inode *ni = NTFS_I(vi); 385 ntfs_inode *ni = NTFS_I(vi);
@@ -465,7 +464,7 @@ try_next:
465 * 464 *
466 * Return the dentry on success or the error code on error (IS_ERR() is true). 465 * Return the dentry on success or the error code on error (IS_ERR() is true).
467 */ 466 */
468struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh) 467static struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
469{ 468{
470 struct inode *vi; 469 struct inode *vi;
471 struct dentry *dent; 470 struct dentry *dent;
@@ -496,3 +495,30 @@ struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh)
496 ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen); 495 ntfs_debug("Done for inode 0x%lx, generation 0x%x.", ino, gen);
497 return dent; 496 return dent;
498} 497}
498
499/**
500 * Export operations allowing NFS exporting of mounted NTFS partitions.
501 *
502 * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
503 * use 32 bits to store the inode number which is an unsigned long so on 64-bit
504 * architectures is usually 64 bits so it would all fail horribly on huge
505 * volumes. I guess we need to define our own encode and decode fh functions
506 * that store 64-bit inode numbers at some point but for now we will ignore the
507 * problem...
508 *
509 * We also use the default ->get_name() helper (used by ->decode_fh() via
510 * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
511 * independent.
512 *
513 * The default ->get_parent() just returns -EACCES so we have to provide our
514 * own and the default ->get_dentry() is incompatible with NTFS due to not
515 * allowing the inode number 0 which is used in NTFS for the system file $MFT
516 * and due to using iget() whereas NTFS needs ntfs_iget().
517 */
518struct export_operations ntfs_export_ops = {
519 .get_parent = ntfs_get_parent, /* Find the parent of a given
520 directory. */
521 .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode
522 given a file handle
523 sub-fragment. */
524};
diff --git a/fs/ntfs/ntfs.h b/fs/ntfs/ntfs.h
index 720ffb71bab8..446b5014115c 100644
--- a/fs/ntfs/ntfs.h
+++ b/fs/ntfs/ntfs.h
@@ -2,7 +2,7 @@
2 * ntfs.h - Defines for NTFS Linux kernel driver. Part of the Linux-NTFS 2 * ntfs.h - Defines for NTFS Linux kernel driver. Part of the Linux-NTFS
3 * project. 3 * project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (C) 2002 Richard Russon 6 * Copyright (C) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -31,6 +31,7 @@
31#include <linux/fs.h> 31#include <linux/fs.h>
32#include <linux/nls.h> 32#include <linux/nls.h>
33#include <linux/smp.h> 33#include <linux/smp.h>
34#include <linux/pagemap.h>
34 35
35#include "types.h" 36#include "types.h"
36#include "volume.h" 37#include "volume.h"
@@ -41,6 +42,9 @@ typedef enum {
41 NTFS_BLOCK_SIZE_BITS = 9, 42 NTFS_BLOCK_SIZE_BITS = 9,
42 NTFS_SB_MAGIC = 0x5346544e, /* 'NTFS' */ 43 NTFS_SB_MAGIC = 0x5346544e, /* 'NTFS' */
43 NTFS_MAX_NAME_LEN = 255, 44 NTFS_MAX_NAME_LEN = 255,
45 NTFS_MAX_ATTR_NAME_LEN = 255,
46 NTFS_MAX_CLUSTER_SIZE = 64 * 1024, /* 64kiB */
47 NTFS_MAX_PAGES_PER_CLUSTER = NTFS_MAX_CLUSTER_SIZE / PAGE_CACHE_SIZE,
44} NTFS_CONSTANTS; 48} NTFS_CONSTANTS;
45 49
46/* Global variables. */ 50/* Global variables. */
@@ -65,6 +69,8 @@ extern struct inode_operations ntfs_dir_inode_ops;
65extern struct file_operations ntfs_empty_file_ops; 69extern struct file_operations ntfs_empty_file_ops;
66extern struct inode_operations ntfs_empty_inode_ops; 70extern struct inode_operations ntfs_empty_inode_ops;
67 71
72extern struct export_operations ntfs_export_ops;
73
68/** 74/**
69 * NTFS_SB - return the ntfs volume given a vfs super block 75 * NTFS_SB - return the ntfs volume given a vfs super block
70 * @sb: VFS super block 76 * @sb: VFS super block
diff --git a/fs/ntfs/runlist.c b/fs/ntfs/runlist.c
index 8438fb1da219..f5b2ac929081 100644
--- a/fs/ntfs/runlist.c
+++ b/fs/ntfs/runlist.c
@@ -1,7 +1,7 @@
1/** 1/**
2 * runlist.c - NTFS runlist handling code. Part of the Linux-NTFS project. 2 * runlist.c - NTFS runlist handling code. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon 5 * Copyright (c) 2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -35,7 +35,7 @@ static inline void ntfs_rl_mm(runlist_element *base, int dst, int src,
35 int size) 35 int size)
36{ 36{
37 if (likely((dst != src) && (size > 0))) 37 if (likely((dst != src) && (size > 0)))
38 memmove(base + dst, base + src, size * sizeof (*base)); 38 memmove(base + dst, base + src, size * sizeof(*base));
39} 39}
40 40
41/** 41/**
@@ -59,7 +59,7 @@ static inline void ntfs_rl_mc(runlist_element *dstbase, int dst,
59 * 59 *
60 * As the runlists grow, more memory will be required. To prevent the 60 * As the runlists grow, more memory will be required. To prevent the
61 * kernel having to allocate and reallocate large numbers of small bits of 61 * kernel having to allocate and reallocate large numbers of small bits of
62 * memory, this function returns and entire page of memory. 62 * memory, this function returns an entire page of memory.
63 * 63 *
64 * It is up to the caller to serialize access to the runlist @rl. 64 * It is up to the caller to serialize access to the runlist @rl.
65 * 65 *
@@ -95,6 +95,51 @@ static inline runlist_element *ntfs_rl_realloc(runlist_element *rl,
95} 95}
96 96
97/** 97/**
98 * ntfs_rl_realloc_nofail - Reallocate memory for runlists
99 * @rl: original runlist
100 * @old_size: number of runlist elements in the original runlist @rl
101 * @new_size: number of runlist elements we need space for
102 *
103 * As the runlists grow, more memory will be required. To prevent the
104 * kernel having to allocate and reallocate large numbers of small bits of
105 * memory, this function returns an entire page of memory.
106 *
107 * This function guarantees that the allocation will succeed. It will sleep
108 * for as long as it takes to complete the allocation.
109 *
110 * It is up to the caller to serialize access to the runlist @rl.
111 *
112 * N.B. If the new allocation doesn't require a different number of pages in
113 * memory, the function will return the original pointer.
114 *
115 * On success, return a pointer to the newly allocated, or recycled, memory.
116 * On error, return -errno. The following error codes are defined:
117 * -ENOMEM - Not enough memory to allocate runlist array.
118 * -EINVAL - Invalid parameters were passed in.
119 */
120static inline runlist_element *ntfs_rl_realloc_nofail(runlist_element *rl,
121 int old_size, int new_size)
122{
123 runlist_element *new_rl;
124
125 old_size = PAGE_ALIGN(old_size * sizeof(*rl));
126 new_size = PAGE_ALIGN(new_size * sizeof(*rl));
127 if (old_size == new_size)
128 return rl;
129
130 new_rl = ntfs_malloc_nofs_nofail(new_size);
131 BUG_ON(!new_rl);
132
133 if (likely(rl != NULL)) {
134 if (unlikely(old_size > new_size))
135 old_size = new_size;
136 memcpy(new_rl, rl, old_size);
137 ntfs_free(rl);
138 }
139 return new_rl;
140}
141
142/**
98 * ntfs_are_rl_mergeable - test if two runlists can be joined together 143 * ntfs_are_rl_mergeable - test if two runlists can be joined together
99 * @dst: original runlist 144 * @dst: original runlist
100 * @src: new runlist to test for mergeability with @dst 145 * @src: new runlist to test for mergeability with @dst
@@ -113,8 +158,11 @@ static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
113 BUG_ON(!dst); 158 BUG_ON(!dst);
114 BUG_ON(!src); 159 BUG_ON(!src);
115 160
116 if ((dst->lcn < 0) || (src->lcn < 0)) /* Are we merging holes? */ 161 if ((dst->lcn < 0) || (src->lcn < 0)) { /* Are we merging holes? */
162 if (dst->lcn == LCN_HOLE && src->lcn == LCN_HOLE)
163 return TRUE;
117 return FALSE; 164 return FALSE;
165 }
118 if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */ 166 if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */
119 return FALSE; 167 return FALSE;
120 if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */ 168 if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */
@@ -494,6 +542,7 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
494 /* Scan to the end of the source runlist. */ 542 /* Scan to the end of the source runlist. */
495 for (dend = 0; likely(drl[dend].length); dend++) 543 for (dend = 0; likely(drl[dend].length); dend++)
496 ; 544 ;
545 dend++;
497 drl = ntfs_rl_realloc(drl, dend, dend + 1); 546 drl = ntfs_rl_realloc(drl, dend, dend + 1);
498 if (IS_ERR(drl)) 547 if (IS_ERR(drl))
499 return drl; 548 return drl;
@@ -563,8 +612,8 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
563 ((drl[dins].vcn + drl[dins].length) <= /* End of hole */ 612 ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
564 (srl[send - 1].vcn + srl[send - 1].length))); 613 (srl[send - 1].vcn + srl[send - 1].length)));
565 614
566 /* Or we'll lose an end marker */ 615 /* Or we will lose an end marker. */
567 if (start && finish && (drl[dins].length == 0)) 616 if (finish && !drl[dins].length)
568 ss++; 617 ss++;
569 if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn)) 618 if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
570 finish = FALSE; 619 finish = FALSE;
@@ -618,11 +667,8 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
618 if (drl[ds].lcn != LCN_RL_NOT_MAPPED) { 667 if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
619 /* Add an unmapped runlist element. */ 668 /* Add an unmapped runlist element. */
620 if (!slots) { 669 if (!slots) {
621 /* FIXME/TODO: We need to have the 670 drl = ntfs_rl_realloc_nofail(drl, ds,
622 * extra memory already! (AIA) */ 671 ds + 2);
623 drl = ntfs_rl_realloc(drl, ds, ds + 2);
624 if (!drl)
625 goto critical_error;
626 slots = 2; 672 slots = 2;
627 } 673 }
628 ds++; 674 ds++;
@@ -637,13 +683,8 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
637 drl[ds].length = marker_vcn - drl[ds].vcn; 683 drl[ds].length = marker_vcn - drl[ds].vcn;
638 /* Finally add the ENOENT terminator. */ 684 /* Finally add the ENOENT terminator. */
639 ds++; 685 ds++;
640 if (!slots) { 686 if (!slots)
641 /* FIXME/TODO: We need to have the extra 687 drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
642 * memory already! (AIA) */
643 drl = ntfs_rl_realloc(drl, ds, ds + 1);
644 if (!drl)
645 goto critical_error;
646 }
647 drl[ds].vcn = marker_vcn; 688 drl[ds].vcn = marker_vcn;
648 drl[ds].lcn = LCN_ENOENT; 689 drl[ds].lcn = LCN_ENOENT;
649 drl[ds].length = (s64)0; 690 drl[ds].length = (s64)0;
@@ -656,11 +697,6 @@ finished:
656 ntfs_debug("Merged runlist:"); 697 ntfs_debug("Merged runlist:");
657 ntfs_debug_dump_runlist(drl); 698 ntfs_debug_dump_runlist(drl);
658 return drl; 699 return drl;
659
660critical_error:
661 /* Critical error! We cannot afford to fail here. */
662 ntfs_error(NULL, "Critical error! Not enough memory.");
663 panic("NTFS: Cannot continue.");
664} 700}
665 701
666/** 702/**
@@ -724,6 +760,9 @@ runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
724 ntfs_error(vol->sb, "Corrupt attribute."); 760 ntfs_error(vol->sb, "Corrupt attribute.");
725 return ERR_PTR(-EIO); 761 return ERR_PTR(-EIO);
726 } 762 }
763 /* If the mapping pairs array is valid but empty, nothing to do. */
764 if (!vcn && !*buf)
765 return old_rl;
727 /* Current position in runlist array. */ 766 /* Current position in runlist array. */
728 rlpos = 0; 767 rlpos = 0;
729 /* Allocate first page and set current runlist size to one page. */ 768 /* Allocate first page and set current runlist size to one page. */
@@ -855,30 +894,42 @@ mpa_err:
855 if (!attr->data.non_resident.lowest_vcn) { 894 if (!attr->data.non_resident.lowest_vcn) {
856 VCN max_cluster; 895 VCN max_cluster;
857 896
858 max_cluster = (sle64_to_cpu( 897 max_cluster = ((sle64_to_cpu(
859 attr->data.non_resident.allocated_size) + 898 attr->data.non_resident.allocated_size) +
860 vol->cluster_size - 1) >> 899 vol->cluster_size - 1) >>
861 vol->cluster_size_bits; 900 vol->cluster_size_bits) - 1;
862 /* 901 /*
863 * If there is a difference between the highest_vcn and the 902 * A highest_vcn of zero means this is a single extent
864 * highest cluster, the runlist is either corrupt or, more 903 * attribute so simply terminate the runlist with LCN_ENOENT).
865 * likely, there are more extents following this one.
866 */ 904 */
867 if (deltaxcn < --max_cluster) { 905 if (deltaxcn) {
868 ntfs_debug("More extents to follow; deltaxcn = 0x%llx, " 906 /*
869 "max_cluster = 0x%llx", 907 * If there is a difference between the highest_vcn and
870 (unsigned long long)deltaxcn, 908 * the highest cluster, the runlist is either corrupt
871 (unsigned long long)max_cluster); 909 * or, more likely, there are more extents following
872 rl[rlpos].vcn = vcn; 910 * this one.
873 vcn += rl[rlpos].length = max_cluster - deltaxcn; 911 */
874 rl[rlpos].lcn = LCN_RL_NOT_MAPPED; 912 if (deltaxcn < max_cluster) {
875 rlpos++; 913 ntfs_debug("More extents to follow; deltaxcn "
876 } else if (unlikely(deltaxcn > max_cluster)) { 914 "= 0x%llx, max_cluster = "
877 ntfs_error(vol->sb, "Corrupt attribute. deltaxcn = " 915 "0x%llx",
878 "0x%llx, max_cluster = 0x%llx", 916 (unsigned long long)deltaxcn,
879 (unsigned long long)deltaxcn, 917 (unsigned long long)
880 (unsigned long long)max_cluster); 918 max_cluster);
881 goto mpa_err; 919 rl[rlpos].vcn = vcn;
920 vcn += rl[rlpos].length = max_cluster -
921 deltaxcn;
922 rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
923 rlpos++;
924 } else if (unlikely(deltaxcn > max_cluster)) {
925 ntfs_error(vol->sb, "Corrupt attribute. "
926 "deltaxcn = 0x%llx, "
927 "max_cluster = 0x%llx",
928 (unsigned long long)deltaxcn,
929 (unsigned long long)
930 max_cluster);
931 goto mpa_err;
932 }
882 } 933 }
883 rl[rlpos].lcn = LCN_ENOENT; 934 rl[rlpos].lcn = LCN_ENOENT;
884 } else /* Not the base extent. There may be more extents to follow. */ 935 } else /* Not the base extent. There may be more extents to follow. */
@@ -918,17 +969,18 @@ err_out:
918 * 969 *
919 * It is up to the caller to serialize access to the runlist @rl. 970 * It is up to the caller to serialize access to the runlist @rl.
920 * 971 *
921 * Since lcns must be >= 0, we use negative return values with special meaning: 972 * Since lcns must be >= 0, we use negative return codes with special meaning:
922 * 973 *
923 * Return value Meaning / Description 974 * Return code Meaning / Description
924 * ================================================== 975 * ==================================================
925 * -1 = LCN_HOLE Hole / not allocated on disk. 976 * LCN_HOLE Hole / not allocated on disk.
926 * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been 977 * LCN_RL_NOT_MAPPED This is part of the runlist which has not been
927 * inserted into the runlist yet. 978 * inserted into the runlist yet.
928 * -3 = LCN_ENOENT There is no such vcn in the attribute. 979 * LCN_ENOENT There is no such vcn in the attribute.
929 * 980 *
930 * Locking: - The caller must have locked the runlist (for reading or writing). 981 * Locking: - The caller must have locked the runlist (for reading or writing).
931 * - This function does not touch the lock. 982 * - This function does not touch the lock, nor does it modify the
983 * runlist.
932 */ 984 */
933LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn) 985LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
934{ 986{
@@ -964,6 +1016,39 @@ LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
964 return LCN_ENOENT; 1016 return LCN_ENOENT;
965} 1017}
966 1018
1019#ifdef NTFS_RW
1020
1021/**
1022 * ntfs_rl_find_vcn_nolock - find a vcn in a runlist
1023 * @rl: runlist to search
1024 * @vcn: vcn to find
1025 *
1026 * Find the virtual cluster number @vcn in the runlist @rl and return the
1027 * address of the runlist element containing the @vcn on success.
1028 *
1029 * Return NULL if @rl is NULL or @vcn is in an unmapped part/out of bounds of
1030 * the runlist.
1031 *
1032 * Locking: The runlist must be locked on entry.
1033 */
1034runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, const VCN vcn)
1035{
1036 BUG_ON(vcn < 0);
1037 if (unlikely(!rl || vcn < rl[0].vcn))
1038 return NULL;
1039 while (likely(rl->length)) {
1040 if (unlikely(vcn < rl[1].vcn)) {
1041 if (likely(rl->lcn >= LCN_HOLE))
1042 return rl;
1043 return NULL;
1044 }
1045 rl++;
1046 }
1047 if (likely(rl->lcn == LCN_ENOENT))
1048 return rl;
1049 return NULL;
1050}
1051
967/** 1052/**
968 * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number 1053 * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
969 * @n: number for which to get the number of bytes for 1054 * @n: number for which to get the number of bytes for
@@ -999,10 +1084,17 @@ static inline int ntfs_get_nr_significant_bytes(const s64 n)
999 * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array 1084 * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
1000 * @vol: ntfs volume (needed for the ntfs version) 1085 * @vol: ntfs volume (needed for the ntfs version)
1001 * @rl: locked runlist to determine the size of the mapping pairs of 1086 * @rl: locked runlist to determine the size of the mapping pairs of
1002 * @start_vcn: vcn at which to start the mapping pairs array 1087 * @first_vcn: first vcn which to include in the mapping pairs array
1088 * @last_vcn: last vcn which to include in the mapping pairs array
1003 * 1089 *
1004 * Walk the locked runlist @rl and calculate the size in bytes of the mapping 1090 * Walk the locked runlist @rl and calculate the size in bytes of the mapping
1005 * pairs array corresponding to the runlist @rl, starting at vcn @start_vcn. 1091 * pairs array corresponding to the runlist @rl, starting at vcn @first_vcn and
1092 * finishing with vcn @last_vcn.
1093 *
1094 * A @last_vcn of -1 means end of runlist and in that case the size of the
1095 * mapping pairs array corresponding to the runlist starting at vcn @first_vcn
1096 * and finishing at the end of the runlist is determined.
1097 *
1006 * This for example allows us to allocate a buffer of the right size when 1098 * This for example allows us to allocate a buffer of the right size when
1007 * building the mapping pairs array. 1099 * building the mapping pairs array.
1008 * 1100 *
@@ -1018,34 +1110,50 @@ static inline int ntfs_get_nr_significant_bytes(const s64 n)
1018 * remains locked throughout, and is left locked upon return. 1110 * remains locked throughout, and is left locked upon return.
1019 */ 1111 */
1020int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol, 1112int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1021 const runlist_element *rl, const VCN start_vcn) 1113 const runlist_element *rl, const VCN first_vcn,
1114 const VCN last_vcn)
1022{ 1115{
1023 LCN prev_lcn; 1116 LCN prev_lcn;
1024 int rls; 1117 int rls;
1118 BOOL the_end = FALSE;
1025 1119
1026 BUG_ON(start_vcn < 0); 1120 BUG_ON(first_vcn < 0);
1121 BUG_ON(last_vcn < -1);
1122 BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
1027 if (!rl) { 1123 if (!rl) {
1028 BUG_ON(start_vcn); 1124 BUG_ON(first_vcn);
1125 BUG_ON(last_vcn > 0);
1029 return 1; 1126 return 1;
1030 } 1127 }
1031 /* Skip to runlist element containing @start_vcn. */ 1128 /* Skip to runlist element containing @first_vcn. */
1032 while (rl->length && start_vcn >= rl[1].vcn) 1129 while (rl->length && first_vcn >= rl[1].vcn)
1033 rl++; 1130 rl++;
1034 if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn) 1131 if (unlikely((!rl->length && first_vcn > rl->vcn) ||
1132 first_vcn < rl->vcn))
1035 return -EINVAL; 1133 return -EINVAL;
1036 prev_lcn = 0; 1134 prev_lcn = 0;
1037 /* Always need the termining zero byte. */ 1135 /* Always need the termining zero byte. */
1038 rls = 1; 1136 rls = 1;
1039 /* Do the first partial run if present. */ 1137 /* Do the first partial run if present. */
1040 if (start_vcn > rl->vcn) { 1138 if (first_vcn > rl->vcn) {
1041 s64 delta; 1139 s64 delta, length = rl->length;
1042 1140
1043 /* We know rl->length != 0 already. */ 1141 /* We know rl->length != 0 already. */
1044 if (rl->length < 0 || rl->lcn < LCN_HOLE) 1142 if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1045 goto err_out; 1143 goto err_out;
1046 delta = start_vcn - rl->vcn; 1144 /*
1145 * If @stop_vcn is given and finishes inside this run, cap the
1146 * run length.
1147 */
1148 if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1149 s64 s1 = last_vcn + 1;
1150 if (unlikely(rl[1].vcn > s1))
1151 length = s1 - rl->vcn;
1152 the_end = TRUE;
1153 }
1154 delta = first_vcn - rl->vcn;
1047 /* Header byte + length. */ 1155 /* Header byte + length. */
1048 rls += 1 + ntfs_get_nr_significant_bytes(rl->length - delta); 1156 rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
1049 /* 1157 /*
1050 * If the logical cluster number (lcn) denotes a hole and we 1158 * If the logical cluster number (lcn) denotes a hole and we
1051 * are on NTFS 3.0+, we don't store it at all, i.e. we need 1159 * are on NTFS 3.0+, we don't store it at all, i.e. we need
@@ -1053,9 +1161,9 @@ int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1053 * Note: this assumes that on NTFS 1.2-, holes are stored with 1161 * Note: this assumes that on NTFS 1.2-, holes are stored with
1054 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1). 1162 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1055 */ 1163 */
1056 if (rl->lcn >= 0 || vol->major_ver < 3) { 1164 if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1057 prev_lcn = rl->lcn; 1165 prev_lcn = rl->lcn;
1058 if (rl->lcn >= 0) 1166 if (likely(rl->lcn >= 0))
1059 prev_lcn += delta; 1167 prev_lcn += delta;
1060 /* Change in lcn. */ 1168 /* Change in lcn. */
1061 rls += ntfs_get_nr_significant_bytes(prev_lcn); 1169 rls += ntfs_get_nr_significant_bytes(prev_lcn);
@@ -1064,11 +1172,23 @@ int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1064 rl++; 1172 rl++;
1065 } 1173 }
1066 /* Do the full runs. */ 1174 /* Do the full runs. */
1067 for (; rl->length; rl++) { 1175 for (; rl->length && !the_end; rl++) {
1068 if (rl->length < 0 || rl->lcn < LCN_HOLE) 1176 s64 length = rl->length;
1177
1178 if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1069 goto err_out; 1179 goto err_out;
1180 /*
1181 * If @stop_vcn is given and finishes inside this run, cap the
1182 * run length.
1183 */
1184 if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1185 s64 s1 = last_vcn + 1;
1186 if (unlikely(rl[1].vcn > s1))
1187 length = s1 - rl->vcn;
1188 the_end = TRUE;
1189 }
1070 /* Header byte + length. */ 1190 /* Header byte + length. */
1071 rls += 1 + ntfs_get_nr_significant_bytes(rl->length); 1191 rls += 1 + ntfs_get_nr_significant_bytes(length);
1072 /* 1192 /*
1073 * If the logical cluster number (lcn) denotes a hole and we 1193 * If the logical cluster number (lcn) denotes a hole and we
1074 * are on NTFS 3.0+, we don't store it at all, i.e. we need 1194 * are on NTFS 3.0+, we don't store it at all, i.e. we need
@@ -1076,7 +1196,7 @@ int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1076 * Note: this assumes that on NTFS 1.2-, holes are stored with 1196 * Note: this assumes that on NTFS 1.2-, holes are stored with
1077 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1). 1197 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1078 */ 1198 */
1079 if (rl->lcn >= 0 || vol->major_ver < 3) { 1199 if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1080 /* Change in lcn. */ 1200 /* Change in lcn. */
1081 rls += ntfs_get_nr_significant_bytes(rl->lcn - 1201 rls += ntfs_get_nr_significant_bytes(rl->lcn -
1082 prev_lcn); 1202 prev_lcn);
@@ -1119,7 +1239,7 @@ static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
1119 1239
1120 i = 0; 1240 i = 0;
1121 do { 1241 do {
1122 if (dst > dst_max) 1242 if (unlikely(dst > dst_max))
1123 goto err_out; 1243 goto err_out;
1124 *dst++ = l & 0xffll; 1244 *dst++ = l & 0xffll;
1125 l >>= 8; 1245 l >>= 8;
@@ -1128,12 +1248,12 @@ static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
1128 j = (n >> 8 * (i - 1)) & 0xff; 1248 j = (n >> 8 * (i - 1)) & 0xff;
1129 /* If the sign bit is wrong, we need an extra byte. */ 1249 /* If the sign bit is wrong, we need an extra byte. */
1130 if (n < 0 && j >= 0) { 1250 if (n < 0 && j >= 0) {
1131 if (dst > dst_max) 1251 if (unlikely(dst > dst_max))
1132 goto err_out; 1252 goto err_out;
1133 i++; 1253 i++;
1134 *dst = (s8)-1; 1254 *dst = (s8)-1;
1135 } else if (n > 0 && j < 0) { 1255 } else if (n > 0 && j < 0) {
1136 if (dst > dst_max) 1256 if (unlikely(dst > dst_max))
1137 goto err_out; 1257 goto err_out;
1138 i++; 1258 i++;
1139 *dst = (s8)0; 1259 *dst = (s8)0;
@@ -1149,13 +1269,18 @@ err_out:
1149 * @dst: destination buffer to which to write the mapping pairs array 1269 * @dst: destination buffer to which to write the mapping pairs array
1150 * @dst_len: size of destination buffer @dst in bytes 1270 * @dst_len: size of destination buffer @dst in bytes
1151 * @rl: locked runlist for which to build the mapping pairs array 1271 * @rl: locked runlist for which to build the mapping pairs array
1152 * @start_vcn: vcn at which to start the mapping pairs array 1272 * @first_vcn: first vcn which to include in the mapping pairs array
1273 * @last_vcn: last vcn which to include in the mapping pairs array
1153 * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC 1274 * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC
1154 * 1275 *
1155 * Create the mapping pairs array from the locked runlist @rl, starting at vcn 1276 * Create the mapping pairs array from the locked runlist @rl, starting at vcn
1156 * @start_vcn and save the array in @dst. @dst_len is the size of @dst in 1277 * @first_vcn and finishing with vcn @last_vcn and save the array in @dst.
1157 * bytes and it should be at least equal to the value obtained by calling 1278 * @dst_len is the size of @dst in bytes and it should be at least equal to the
1158 * ntfs_get_size_for_mapping_pairs(). 1279 * value obtained by calling ntfs_get_size_for_mapping_pairs().
1280 *
1281 * A @last_vcn of -1 means end of runlist and in that case the mapping pairs
1282 * array corresponding to the runlist starting at vcn @first_vcn and finishing
1283 * at the end of the runlist is created.
1159 * 1284 *
1160 * If @rl is NULL, just write a single terminator byte to @dst. 1285 * If @rl is NULL, just write a single terminator byte to @dst.
1161 * 1286 *
@@ -1164,7 +1289,7 @@ err_out:
1164 * been filled with all the mapping pairs that will fit, thus it can be treated 1289 * been filled with all the mapping pairs that will fit, thus it can be treated
1165 * as partial success, in that a new attribute extent needs to be created or 1290 * as partial success, in that a new attribute extent needs to be created or
1166 * the next extent has to be used and the mapping pairs build has to be 1291 * the next extent has to be used and the mapping pairs build has to be
1167 * continued with @start_vcn set to *@stop_vcn. 1292 * continued with @first_vcn set to *@stop_vcn.
1168 * 1293 *
1169 * Return 0 on success and -errno on error. The following error codes are 1294 * Return 0 on success and -errno on error. The following error codes are
1170 * defined: 1295 * defined:
@@ -1178,27 +1303,32 @@ err_out:
1178 */ 1303 */
1179int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, 1304int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1180 const int dst_len, const runlist_element *rl, 1305 const int dst_len, const runlist_element *rl,
1181 const VCN start_vcn, VCN *const stop_vcn) 1306 const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn)
1182{ 1307{
1183 LCN prev_lcn; 1308 LCN prev_lcn;
1184 s8 *dst_max, *dst_next; 1309 s8 *dst_max, *dst_next;
1185 int err = -ENOSPC; 1310 int err = -ENOSPC;
1311 BOOL the_end = FALSE;
1186 s8 len_len, lcn_len; 1312 s8 len_len, lcn_len;
1187 1313
1188 BUG_ON(start_vcn < 0); 1314 BUG_ON(first_vcn < 0);
1315 BUG_ON(last_vcn < -1);
1316 BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
1189 BUG_ON(dst_len < 1); 1317 BUG_ON(dst_len < 1);
1190 if (!rl) { 1318 if (!rl) {
1191 BUG_ON(start_vcn); 1319 BUG_ON(first_vcn);
1320 BUG_ON(last_vcn > 0);
1192 if (stop_vcn) 1321 if (stop_vcn)
1193 *stop_vcn = 0; 1322 *stop_vcn = 0;
1194 /* Terminator byte. */ 1323 /* Terminator byte. */
1195 *dst = 0; 1324 *dst = 0;
1196 return 0; 1325 return 0;
1197 } 1326 }
1198 /* Skip to runlist element containing @start_vcn. */ 1327 /* Skip to runlist element containing @first_vcn. */
1199 while (rl->length && start_vcn >= rl[1].vcn) 1328 while (rl->length && first_vcn >= rl[1].vcn)
1200 rl++; 1329 rl++;
1201 if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn) 1330 if (unlikely((!rl->length && first_vcn > rl->vcn) ||
1331 first_vcn < rl->vcn))
1202 return -EINVAL; 1332 return -EINVAL;
1203 /* 1333 /*
1204 * @dst_max is used for bounds checking in 1334 * @dst_max is used for bounds checking in
@@ -1207,17 +1337,27 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1207 dst_max = dst + dst_len - 1; 1337 dst_max = dst + dst_len - 1;
1208 prev_lcn = 0; 1338 prev_lcn = 0;
1209 /* Do the first partial run if present. */ 1339 /* Do the first partial run if present. */
1210 if (start_vcn > rl->vcn) { 1340 if (first_vcn > rl->vcn) {
1211 s64 delta; 1341 s64 delta, length = rl->length;
1212 1342
1213 /* We know rl->length != 0 already. */ 1343 /* We know rl->length != 0 already. */
1214 if (rl->length < 0 || rl->lcn < LCN_HOLE) 1344 if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1215 goto err_out; 1345 goto err_out;
1216 delta = start_vcn - rl->vcn; 1346 /*
1347 * If @stop_vcn is given and finishes inside this run, cap the
1348 * run length.
1349 */
1350 if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1351 s64 s1 = last_vcn + 1;
1352 if (unlikely(rl[1].vcn > s1))
1353 length = s1 - rl->vcn;
1354 the_end = TRUE;
1355 }
1356 delta = first_vcn - rl->vcn;
1217 /* Write length. */ 1357 /* Write length. */
1218 len_len = ntfs_write_significant_bytes(dst + 1, dst_max, 1358 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1219 rl->length - delta); 1359 length - delta);
1220 if (len_len < 0) 1360 if (unlikely(len_len < 0))
1221 goto size_err; 1361 goto size_err;
1222 /* 1362 /*
1223 * If the logical cluster number (lcn) denotes a hole and we 1363 * If the logical cluster number (lcn) denotes a hole and we
@@ -1228,19 +1368,19 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1228 * case on NT4. - We assume that we just need to write the lcn 1368 * case on NT4. - We assume that we just need to write the lcn
1229 * change until someone tells us otherwise... (AIA) 1369 * change until someone tells us otherwise... (AIA)
1230 */ 1370 */
1231 if (rl->lcn >= 0 || vol->major_ver < 3) { 1371 if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1232 prev_lcn = rl->lcn; 1372 prev_lcn = rl->lcn;
1233 if (rl->lcn >= 0) 1373 if (likely(rl->lcn >= 0))
1234 prev_lcn += delta; 1374 prev_lcn += delta;
1235 /* Write change in lcn. */ 1375 /* Write change in lcn. */
1236 lcn_len = ntfs_write_significant_bytes(dst + 1 + 1376 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1237 len_len, dst_max, prev_lcn); 1377 len_len, dst_max, prev_lcn);
1238 if (lcn_len < 0) 1378 if (unlikely(lcn_len < 0))
1239 goto size_err; 1379 goto size_err;
1240 } else 1380 } else
1241 lcn_len = 0; 1381 lcn_len = 0;
1242 dst_next = dst + len_len + lcn_len + 1; 1382 dst_next = dst + len_len + lcn_len + 1;
1243 if (dst_next > dst_max) 1383 if (unlikely(dst_next > dst_max))
1244 goto size_err; 1384 goto size_err;
1245 /* Update header byte. */ 1385 /* Update header byte. */
1246 *dst = lcn_len << 4 | len_len; 1386 *dst = lcn_len << 4 | len_len;
@@ -1250,13 +1390,25 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1250 rl++; 1390 rl++;
1251 } 1391 }
1252 /* Do the full runs. */ 1392 /* Do the full runs. */
1253 for (; rl->length; rl++) { 1393 for (; rl->length && !the_end; rl++) {
1254 if (rl->length < 0 || rl->lcn < LCN_HOLE) 1394 s64 length = rl->length;
1395
1396 if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1255 goto err_out; 1397 goto err_out;
1398 /*
1399 * If @stop_vcn is given and finishes inside this run, cap the
1400 * run length.
1401 */
1402 if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1403 s64 s1 = last_vcn + 1;
1404 if (unlikely(rl[1].vcn > s1))
1405 length = s1 - rl->vcn;
1406 the_end = TRUE;
1407 }
1256 /* Write length. */ 1408 /* Write length. */
1257 len_len = ntfs_write_significant_bytes(dst + 1, dst_max, 1409 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1258 rl->length); 1410 length);
1259 if (len_len < 0) 1411 if (unlikely(len_len < 0))
1260 goto size_err; 1412 goto size_err;
1261 /* 1413 /*
1262 * If the logical cluster number (lcn) denotes a hole and we 1414 * If the logical cluster number (lcn) denotes a hole and we
@@ -1267,17 +1419,17 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1267 * case on NT4. - We assume that we just need to write the lcn 1419 * case on NT4. - We assume that we just need to write the lcn
1268 * change until someone tells us otherwise... (AIA) 1420 * change until someone tells us otherwise... (AIA)
1269 */ 1421 */
1270 if (rl->lcn >= 0 || vol->major_ver < 3) { 1422 if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1271 /* Write change in lcn. */ 1423 /* Write change in lcn. */
1272 lcn_len = ntfs_write_significant_bytes(dst + 1 + 1424 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1273 len_len, dst_max, rl->lcn - prev_lcn); 1425 len_len, dst_max, rl->lcn - prev_lcn);
1274 if (lcn_len < 0) 1426 if (unlikely(lcn_len < 0))
1275 goto size_err; 1427 goto size_err;
1276 prev_lcn = rl->lcn; 1428 prev_lcn = rl->lcn;
1277 } else 1429 } else
1278 lcn_len = 0; 1430 lcn_len = 0;
1279 dst_next = dst + len_len + lcn_len + 1; 1431 dst_next = dst + len_len + lcn_len + 1;
1280 if (dst_next > dst_max) 1432 if (unlikely(dst_next > dst_max))
1281 goto size_err; 1433 goto size_err;
1282 /* Update header byte. */ 1434 /* Update header byte. */
1283 *dst = lcn_len << 4 | len_len; 1435 *dst = lcn_len << 4 | len_len;
@@ -1303,6 +1455,7 @@ err_out:
1303 1455
1304/** 1456/**
1305 * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn 1457 * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
1458 * @vol: ntfs volume (needed for error output)
1306 * @runlist: runlist to truncate 1459 * @runlist: runlist to truncate
1307 * @new_length: the new length of the runlist in VCNs 1460 * @new_length: the new length of the runlist in VCNs
1308 * 1461 *
@@ -1310,12 +1463,16 @@ err_out:
1310 * holding the runlist elements to a length of @new_length VCNs. 1463 * holding the runlist elements to a length of @new_length VCNs.
1311 * 1464 *
1312 * If @new_length lies within the runlist, the runlist elements with VCNs of 1465 * If @new_length lies within the runlist, the runlist elements with VCNs of
1313 * @new_length and above are discarded. 1466 * @new_length and above are discarded. As a special case if @new_length is
1467 * zero, the runlist is discarded and set to NULL.
1314 * 1468 *
1315 * If @new_length lies beyond the runlist, a sparse runlist element is added to 1469 * If @new_length lies beyond the runlist, a sparse runlist element is added to
1316 * the end of the runlist @runlist or if the last runlist element is a sparse 1470 * the end of the runlist @runlist or if the last runlist element is a sparse
1317 * one already, this is extended. 1471 * one already, this is extended.
1318 * 1472 *
1473 * Note, no checking is done for unmapped runlist elements. It is assumed that
1474 * the caller has mapped any elements that need to be mapped already.
1475 *
1319 * Return 0 on success and -errno on error. 1476 * Return 0 on success and -errno on error.
1320 * 1477 *
1321 * Locking: The caller must hold @runlist->lock for writing. 1478 * Locking: The caller must hold @runlist->lock for writing.
@@ -1330,6 +1487,13 @@ int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
1330 BUG_ON(!runlist); 1487 BUG_ON(!runlist);
1331 BUG_ON(new_length < 0); 1488 BUG_ON(new_length < 0);
1332 rl = runlist->rl; 1489 rl = runlist->rl;
1490 if (!new_length) {
1491 ntfs_debug("Freeing runlist.");
1492 runlist->rl = NULL;
1493 if (rl)
1494 ntfs_free(rl);
1495 return 0;
1496 }
1333 if (unlikely(!rl)) { 1497 if (unlikely(!rl)) {
1334 /* 1498 /*
1335 * Create a runlist consisting of a sparse runlist element of 1499 * Create a runlist consisting of a sparse runlist element of
@@ -1436,3 +1600,289 @@ int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
1436 ntfs_debug("Done."); 1600 ntfs_debug("Done.");
1437 return 0; 1601 return 0;
1438} 1602}
1603
1604/**
1605 * ntfs_rl_punch_nolock - punch a hole into a runlist
1606 * @vol: ntfs volume (needed for error output)
1607 * @runlist: runlist to punch a hole into
1608 * @start: starting VCN of the hole to be created
1609 * @length: size of the hole to be created in units of clusters
1610 *
1611 * Punch a hole into the runlist @runlist starting at VCN @start and of size
1612 * @length clusters.
1613 *
1614 * Return 0 on success and -errno on error, in which case @runlist has not been
1615 * modified.
1616 *
1617 * If @start and/or @start + @length are outside the runlist return error code
1618 * -ENOENT.
1619 *
1620 * If the runlist contains unmapped or error elements between @start and @start
1621 * + @length return error code -EINVAL.
1622 *
1623 * Locking: The caller must hold @runlist->lock for writing.
1624 */
1625int ntfs_rl_punch_nolock(const ntfs_volume *vol, runlist *const runlist,
1626 const VCN start, const s64 length)
1627{
1628 const VCN end = start + length;
1629 s64 delta;
1630 runlist_element *rl, *rl_end, *rl_real_end, *trl;
1631 int old_size;
1632 BOOL lcn_fixup = FALSE;
1633
1634 ntfs_debug("Entering for start 0x%llx, length 0x%llx.",
1635 (long long)start, (long long)length);
1636 BUG_ON(!runlist);
1637 BUG_ON(start < 0);
1638 BUG_ON(length < 0);
1639 BUG_ON(end < 0);
1640 rl = runlist->rl;
1641 if (unlikely(!rl)) {
1642 if (likely(!start && !length))
1643 return 0;
1644 return -ENOENT;
1645 }
1646 /* Find @start in the runlist. */
1647 while (likely(rl->length && start >= rl[1].vcn))
1648 rl++;
1649 rl_end = rl;
1650 /* Find @end in the runlist. */
1651 while (likely(rl_end->length && end >= rl_end[1].vcn)) {
1652 /* Verify there are no unmapped or error elements. */
1653 if (unlikely(rl_end->lcn < LCN_HOLE))
1654 return -EINVAL;
1655 rl_end++;
1656 }
1657 /* Check the last element. */
1658 if (unlikely(rl_end->length && rl_end->lcn < LCN_HOLE))
1659 return -EINVAL;
1660 /* This covers @start being out of bounds, too. */
1661 if (!rl_end->length && end > rl_end->vcn)
1662 return -ENOENT;
1663 if (!length)
1664 return 0;
1665 if (!rl->length)
1666 return -ENOENT;
1667 rl_real_end = rl_end;
1668 /* Determine the runlist size. */
1669 while (likely(rl_real_end->length))
1670 rl_real_end++;
1671 old_size = rl_real_end - runlist->rl + 1;
1672 /* If @start is in a hole simply extend the hole. */
1673 if (rl->lcn == LCN_HOLE) {
1674 /*
1675 * If both @start and @end are in the same sparse run, we are
1676 * done.
1677 */
1678 if (end <= rl[1].vcn) {
1679 ntfs_debug("Done (requested hole is already sparse).");
1680 return 0;
1681 }
1682extend_hole:
1683 /* Extend the hole. */
1684 rl->length = end - rl->vcn;
1685 /* If @end is in a hole, merge it with the current one. */
1686 if (rl_end->lcn == LCN_HOLE) {
1687 rl_end++;
1688 rl->length = rl_end->vcn - rl->vcn;
1689 }
1690 /* We have done the hole. Now deal with the remaining tail. */
1691 rl++;
1692 /* Cut out all runlist elements up to @end. */
1693 if (rl < rl_end)
1694 memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
1695 sizeof(*rl));
1696 /* Adjust the beginning of the tail if necessary. */
1697 if (end > rl->vcn) {
1698 s64 delta = end - rl->vcn;
1699 rl->vcn = end;
1700 rl->length -= delta;
1701 /* Only adjust the lcn if it is real. */
1702 if (rl->lcn >= 0)
1703 rl->lcn += delta;
1704 }
1705shrink_allocation:
1706 /* Reallocate memory if the allocation changed. */
1707 if (rl < rl_end) {
1708 rl = ntfs_rl_realloc(runlist->rl, old_size,
1709 old_size - (rl_end - rl));
1710 if (IS_ERR(rl))
1711 ntfs_warning(vol->sb, "Failed to shrink "
1712 "runlist buffer. This just "
1713 "wastes a bit of memory "
1714 "temporarily so we ignore it "
1715 "and return success.");
1716 else
1717 runlist->rl = rl;
1718 }
1719 ntfs_debug("Done (extend hole).");
1720 return 0;
1721 }
1722 /*
1723 * If @start is at the beginning of a run things are easier as there is
1724 * no need to split the first run.
1725 */
1726 if (start == rl->vcn) {
1727 /*
1728 * @start is at the beginning of a run.
1729 *
1730 * If the previous run is sparse, extend its hole.
1731 *
1732 * If @end is not in the same run, switch the run to be sparse
1733 * and extend the newly created hole.
1734 *
1735 * Thus both of these cases reduce the problem to the above
1736 * case of "@start is in a hole".
1737 */
1738 if (rl > runlist->rl && (rl - 1)->lcn == LCN_HOLE) {
1739 rl--;
1740 goto extend_hole;
1741 }
1742 if (end >= rl[1].vcn) {
1743 rl->lcn = LCN_HOLE;
1744 goto extend_hole;
1745 }
1746 /*
1747 * The final case is when @end is in the same run as @start.
1748 * For this need to split the run into two. One run for the
1749 * sparse region between the beginning of the old run, i.e.
1750 * @start, and @end and one for the remaining non-sparse
1751 * region, i.e. between @end and the end of the old run.
1752 */
1753 trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
1754 if (IS_ERR(trl))
1755 goto enomem_out;
1756 old_size++;
1757 if (runlist->rl != trl) {
1758 rl = trl + (rl - runlist->rl);
1759 rl_end = trl + (rl_end - runlist->rl);
1760 rl_real_end = trl + (rl_real_end - runlist->rl);
1761 runlist->rl = trl;
1762 }
1763split_end:
1764 /* Shift all the runs up by one. */
1765 memmove(rl + 1, rl, (rl_real_end - rl + 1) * sizeof(*rl));
1766 /* Finally, setup the two split runs. */
1767 rl->lcn = LCN_HOLE;
1768 rl->length = length;
1769 rl++;
1770 rl->vcn += length;
1771 /* Only adjust the lcn if it is real. */
1772 if (rl->lcn >= 0 || lcn_fixup)
1773 rl->lcn += length;
1774 rl->length -= length;
1775 ntfs_debug("Done (split one).");
1776 return 0;
1777 }
1778 /*
1779 * @start is neither in a hole nor at the beginning of a run.
1780 *
1781 * If @end is in a hole, things are easier as simply truncating the run
1782 * @start is in to end at @start - 1, deleting all runs after that up
1783 * to @end, and finally extending the beginning of the run @end is in
1784 * to be @start is all that is needed.
1785 */
1786 if (rl_end->lcn == LCN_HOLE) {
1787 /* Truncate the run containing @start. */
1788 rl->length = start - rl->vcn;
1789 rl++;
1790 /* Cut out all runlist elements up to @end. */
1791 if (rl < rl_end)
1792 memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
1793 sizeof(*rl));
1794 /* Extend the beginning of the run @end is in to be @start. */
1795 rl->vcn = start;
1796 rl->length = rl[1].vcn - start;
1797 goto shrink_allocation;
1798 }
1799 /*
1800 * If @end is not in a hole there are still two cases to distinguish.
1801 * Either @end is or is not in the same run as @start.
1802 *
1803 * The second case is easier as it can be reduced to an already solved
1804 * problem by truncating the run @start is in to end at @start - 1.
1805 * Then, if @end is in the next run need to split the run into a sparse
1806 * run followed by a non-sparse run (already covered above) and if @end
1807 * is not in the next run switching it to be sparse, again reduces the
1808 * problem to the already covered case of "@start is in a hole".
1809 */
1810 if (end >= rl[1].vcn) {
1811 /*
1812 * If @end is not in the next run, reduce the problem to the
1813 * case of "@start is in a hole".
1814 */
1815 if (rl[1].length && end >= rl[2].vcn) {
1816 /* Truncate the run containing @start. */
1817 rl->length = start - rl->vcn;
1818 rl++;
1819 rl->vcn = start;
1820 rl->lcn = LCN_HOLE;
1821 goto extend_hole;
1822 }
1823 trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
1824 if (IS_ERR(trl))
1825 goto enomem_out;
1826 old_size++;
1827 if (runlist->rl != trl) {
1828 rl = trl + (rl - runlist->rl);
1829 rl_end = trl + (rl_end - runlist->rl);
1830 rl_real_end = trl + (rl_real_end - runlist->rl);
1831 runlist->rl = trl;
1832 }
1833 /* Truncate the run containing @start. */
1834 rl->length = start - rl->vcn;
1835 rl++;
1836 /*
1837 * @end is in the next run, reduce the problem to the case
1838 * where "@start is at the beginning of a run and @end is in
1839 * the same run as @start".
1840 */
1841 delta = rl->vcn - start;
1842 rl->vcn = start;
1843 if (rl->lcn >= 0) {
1844 rl->lcn -= delta;
1845 /* Need this in case the lcn just became negative. */
1846 lcn_fixup = TRUE;
1847 }
1848 rl->length += delta;
1849 goto split_end;
1850 }
1851 /*
1852 * The first case from above, i.e. @end is in the same run as @start.
1853 * We need to split the run into three. One run for the non-sparse
1854 * region between the beginning of the old run and @start, one for the
1855 * sparse region between @start and @end, and one for the remaining
1856 * non-sparse region, i.e. between @end and the end of the old run.
1857 */
1858 trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 2);
1859 if (IS_ERR(trl))
1860 goto enomem_out;
1861 old_size += 2;
1862 if (runlist->rl != trl) {
1863 rl = trl + (rl - runlist->rl);
1864 rl_end = trl + (rl_end - runlist->rl);
1865 rl_real_end = trl + (rl_real_end - runlist->rl);
1866 runlist->rl = trl;
1867 }
1868 /* Shift all the runs up by two. */
1869 memmove(rl + 2, rl, (rl_real_end - rl + 1) * sizeof(*rl));
1870 /* Finally, setup the three split runs. */
1871 rl->length = start - rl->vcn;
1872 rl++;
1873 rl->vcn = start;
1874 rl->lcn = LCN_HOLE;
1875 rl->length = length;
1876 rl++;
1877 delta = end - rl->vcn;
1878 rl->vcn = end;
1879 rl->lcn += delta;
1880 rl->length -= delta;
1881 ntfs_debug("Done (split both).");
1882 return 0;
1883enomem_out:
1884 ntfs_error(vol->sb, "Not enough memory to extend runlist buffer.");
1885 return -ENOMEM;
1886}
1887
1888#endif /* NTFS_RW */
diff --git a/fs/ntfs/runlist.h b/fs/ntfs/runlist.h
index 7107fde59df9..47728fbb610b 100644
--- a/fs/ntfs/runlist.h
+++ b/fs/ntfs/runlist.h
@@ -2,7 +2,7 @@
2 * runlist.h - Defines for runlist handling in NTFS Linux kernel driver. 2 * runlist.h - Defines for runlist handling in NTFS Linux kernel driver.
3 * Part of the Linux-NTFS project. 3 * Part of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -66,6 +66,8 @@ typedef enum {
66 LCN_HOLE = -1, /* Keep this as highest value or die! */ 66 LCN_HOLE = -1, /* Keep this as highest value or die! */
67 LCN_RL_NOT_MAPPED = -2, 67 LCN_RL_NOT_MAPPED = -2,
68 LCN_ENOENT = -3, 68 LCN_ENOENT = -3,
69 LCN_ENOMEM = -4,
70 LCN_EIO = -5,
69} LCN_SPECIAL_VALUES; 71} LCN_SPECIAL_VALUES;
70 72
71extern runlist_element *ntfs_runlists_merge(runlist_element *drl, 73extern runlist_element *ntfs_runlists_merge(runlist_element *drl,
@@ -76,14 +78,25 @@ extern runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
76 78
77extern LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn); 79extern LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn);
78 80
81#ifdef NTFS_RW
82
83extern runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl,
84 const VCN vcn);
85
79extern int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol, 86extern int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
80 const runlist_element *rl, const VCN start_vcn); 87 const runlist_element *rl, const VCN first_vcn,
88 const VCN last_vcn);
81 89
82extern int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, 90extern int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
83 const int dst_len, const runlist_element *rl, 91 const int dst_len, const runlist_element *rl,
84 const VCN start_vcn, VCN *const stop_vcn); 92 const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn);
85 93
86extern int ntfs_rl_truncate_nolock(const ntfs_volume *vol, 94extern int ntfs_rl_truncate_nolock(const ntfs_volume *vol,
87 runlist *const runlist, const s64 new_length); 95 runlist *const runlist, const s64 new_length);
88 96
97int ntfs_rl_punch_nolock(const ntfs_volume *vol, runlist *const runlist,
98 const VCN start, const s64 length);
99
100#endif /* NTFS_RW */
101
89#endif /* _LINUX_NTFS_RUNLIST_H */ 102#endif /* _LINUX_NTFS_RUNLIST_H */
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 212a3d0f2073..453d0d51ea4b 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project. 2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2001,2002 Richard Russon 5 * Copyright (c) 2001,2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -34,14 +34,16 @@
34#include "sysctl.h" 34#include "sysctl.h"
35#include "logfile.h" 35#include "logfile.h"
36#include "quota.h" 36#include "quota.h"
37#include "usnjrnl.h"
37#include "dir.h" 38#include "dir.h"
38#include "debug.h" 39#include "debug.h"
39#include "index.h" 40#include "index.h"
40#include "aops.h" 41#include "aops.h"
42#include "layout.h"
41#include "malloc.h" 43#include "malloc.h"
42#include "ntfs.h" 44#include "ntfs.h"
43 45
44/* Number of mounted file systems which have compression enabled. */ 46/* Number of mounted filesystems which have compression enabled. */
45static unsigned long ntfs_nr_compression_users; 47static unsigned long ntfs_nr_compression_users;
46 48
47/* A global default upcase table and a corresponding reference count. */ 49/* A global default upcase table and a corresponding reference count. */
@@ -102,7 +104,7 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
102 gid_t gid = (gid_t)-1; 104 gid_t gid = (gid_t)-1;
103 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1; 105 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
104 int mft_zone_multiplier = -1, on_errors = -1; 106 int mft_zone_multiplier = -1, on_errors = -1;
105 int show_sys_files = -1, case_sensitive = -1; 107 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
106 struct nls_table *nls_map = NULL, *old_nls; 108 struct nls_table *nls_map = NULL, *old_nls;
107 109
108 /* I am lazy... (-8 */ 110 /* I am lazy... (-8 */
@@ -124,6 +126,14 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
124 if (*v) \ 126 if (*v) \
125 goto needs_val; \ 127 goto needs_val; \
126 } 128 }
129#define NTFS_GETOPT_OCTAL(option, variable) \
130 if (!strcmp(p, option)) { \
131 if (!v || !*v) \
132 goto needs_arg; \
133 variable = simple_strtoul(ov = v, &v, 8); \
134 if (*v) \
135 goto needs_val; \
136 }
127#define NTFS_GETOPT_BOOL(option, variable) \ 137#define NTFS_GETOPT_BOOL(option, variable) \
128 if (!strcmp(p, option)) { \ 138 if (!strcmp(p, option)) { \
129 BOOL val; \ 139 BOOL val; \
@@ -155,13 +165,14 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
155 *v++ = 0; 165 *v++ = 0;
156 NTFS_GETOPT("uid", uid) 166 NTFS_GETOPT("uid", uid)
157 else NTFS_GETOPT("gid", gid) 167 else NTFS_GETOPT("gid", gid)
158 else NTFS_GETOPT("umask", fmask = dmask) 168 else NTFS_GETOPT_OCTAL("umask", fmask = dmask)
159 else NTFS_GETOPT("fmask", fmask) 169 else NTFS_GETOPT_OCTAL("fmask", fmask)
160 else NTFS_GETOPT("dmask", dmask) 170 else NTFS_GETOPT_OCTAL("dmask", dmask)
161 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier) 171 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
162 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE) 172 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
163 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files) 173 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
164 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive) 174 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
175 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
165 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors, 176 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
166 on_errors_arr) 177 on_errors_arr)
167 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes")) 178 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
@@ -291,6 +302,21 @@ no_mount_options:
291 else 302 else
292 NVolClearCaseSensitive(vol); 303 NVolClearCaseSensitive(vol);
293 } 304 }
305 if (disable_sparse != -1) {
306 if (disable_sparse)
307 NVolClearSparseEnabled(vol);
308 else {
309 if (!NVolSparseEnabled(vol) &&
310 vol->major_ver && vol->major_ver < 3)
311 ntfs_warning(vol->sb, "Not enabling sparse "
312 "support due to NTFS volume "
313 "version %i.%i (need at least "
314 "version 3.0).", vol->major_ver,
315 vol->minor_ver);
316 else
317 NVolSetSparseEnabled(vol);
318 }
319 }
294 return TRUE; 320 return TRUE;
295needs_arg: 321needs_arg:
296 ntfs_error(vol->sb, "The %s option requires an argument.", p); 322 ntfs_error(vol->sb, "The %s option requires an argument.", p);
@@ -480,6 +506,12 @@ static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
480 NVolSetErrors(vol); 506 NVolSetErrors(vol);
481 return -EROFS; 507 return -EROFS;
482 } 508 }
509 if (!ntfs_stamp_usnjrnl(vol)) {
510 ntfs_error(sb, "Failed to stamp transation log "
511 "($UsnJrnl)%s", es);
512 NVolSetErrors(vol);
513 return -EROFS;
514 }
483 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) { 515 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
484 /* Remounting read-only. */ 516 /* Remounting read-only. */
485 if (!NVolErrors(vol)) { 517 if (!NVolErrors(vol)) {
@@ -516,16 +548,19 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
516{ 548{
517 /* 549 /*
518 * Check that checksum == sum of u32 values from b to the checksum 550 * Check that checksum == sum of u32 values from b to the checksum
519 * field. If checksum is zero, no checking is done. 551 * field. If checksum is zero, no checking is done. We will work when
552 * the checksum test fails, since some utilities update the boot sector
553 * ignoring the checksum which leaves the checksum out-of-date. We
554 * report a warning if this is the case.
520 */ 555 */
521 if ((void*)b < (void*)&b->checksum && b->checksum) { 556 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
522 le32 *u; 557 le32 *u;
523 u32 i; 558 u32 i;
524 559
525 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u) 560 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
526 i += le32_to_cpup(u); 561 i += le32_to_cpup(u);
527 if (le32_to_cpu(b->checksum) != i) 562 if (le32_to_cpu(b->checksum) != i)
528 goto not_ntfs; 563 ntfs_warning(sb, "Invalid boot sector checksum.");
529 } 564 }
530 /* Check OEMidentifier is "NTFS " */ 565 /* Check OEMidentifier is "NTFS " */
531 if (b->oem_id != magicNTFS) 566 if (b->oem_id != magicNTFS)
@@ -541,9 +576,9 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
541 default: 576 default:
542 goto not_ntfs; 577 goto not_ntfs;
543 } 578 }
544 /* Check the cluster size is not above 65536 bytes. */ 579 /* Check the cluster size is not above the maximum (64kiB). */
545 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) * 580 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
546 b->bpb.sectors_per_cluster > 0x10000) 581 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
547 goto not_ntfs; 582 goto not_ntfs;
548 /* Check reserved/unused fields are really zero. */ 583 /* Check reserved/unused fields are really zero. */
549 if (le16_to_cpu(b->bpb.reserved_sectors) || 584 if (le16_to_cpu(b->bpb.reserved_sectors) ||
@@ -575,7 +610,7 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
575 * many BIOSes will refuse to boot from a bootsector if the magic is 610 * many BIOSes will refuse to boot from a bootsector if the magic is
576 * incorrect, so we emit a warning. 611 * incorrect, so we emit a warning.
577 */ 612 */
578 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55)) 613 if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
579 ntfs_warning(sb, "Invalid end of sector marker."); 614 ntfs_warning(sb, "Invalid end of sector marker.");
580 return TRUE; 615 return TRUE;
581not_ntfs: 616not_ntfs:
@@ -967,6 +1002,7 @@ static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
967 tmp_ni = NTFS_I(tmp_ino); 1002 tmp_ni = NTFS_I(tmp_ino);
968 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */ 1003 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
969 NInoSetMstProtected(tmp_ni); 1004 NInoSetMstProtected(tmp_ni);
1005 NInoSetSparseDisabled(tmp_ni);
970 /* 1006 /*
971 * Set up our little cheat allowing us to reuse the async read io 1007 * Set up our little cheat allowing us to reuse the async read io
972 * completion handler for directories. 1008 * completion handler for directories.
@@ -990,12 +1026,12 @@ static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
990 */ 1026 */
991static BOOL check_mft_mirror(ntfs_volume *vol) 1027static BOOL check_mft_mirror(ntfs_volume *vol)
992{ 1028{
993 unsigned long index;
994 struct super_block *sb = vol->sb; 1029 struct super_block *sb = vol->sb;
995 ntfs_inode *mirr_ni; 1030 ntfs_inode *mirr_ni;
996 struct page *mft_page, *mirr_page; 1031 struct page *mft_page, *mirr_page;
997 u8 *kmft, *kmirr; 1032 u8 *kmft, *kmirr;
998 runlist_element *rl, rl2[2]; 1033 runlist_element *rl, rl2[2];
1034 pgoff_t index;
999 int mrecs_per_page, i; 1035 int mrecs_per_page, i;
1000 1036
1001 ntfs_debug("Entering."); 1037 ntfs_debug("Entering.");
@@ -1105,7 +1141,8 @@ mft_unmap_out:
1105 * 1141 *
1106 * Return TRUE on success or FALSE on error. 1142 * Return TRUE on success or FALSE on error.
1107 */ 1143 */
1108static BOOL load_and_check_logfile(ntfs_volume *vol) 1144static BOOL load_and_check_logfile(ntfs_volume *vol,
1145 RESTART_PAGE_HEADER **rp)
1109{ 1146{
1110 struct inode *tmp_ino; 1147 struct inode *tmp_ino;
1111 1148
@@ -1117,16 +1154,135 @@ static BOOL load_and_check_logfile(ntfs_volume *vol)
1117 /* Caller will display error message. */ 1154 /* Caller will display error message. */
1118 return FALSE; 1155 return FALSE;
1119 } 1156 }
1120 if (!ntfs_check_logfile(tmp_ino)) { 1157 if (!ntfs_check_logfile(tmp_ino, rp)) {
1121 iput(tmp_ino); 1158 iput(tmp_ino);
1122 /* ntfs_check_logfile() will have displayed error output. */ 1159 /* ntfs_check_logfile() will have displayed error output. */
1123 return FALSE; 1160 return FALSE;
1124 } 1161 }
1162 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1125 vol->logfile_ino = tmp_ino; 1163 vol->logfile_ino = tmp_ino;
1126 ntfs_debug("Done."); 1164 ntfs_debug("Done.");
1127 return TRUE; 1165 return TRUE;
1128} 1166}
1129 1167
1168#define NTFS_HIBERFIL_HEADER_SIZE 4096
1169
1170/**
1171 * check_windows_hibernation_status - check if Windows is suspended on a volume
1172 * @vol: ntfs super block of device to check
1173 *
1174 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1175 * looking for the file hiberfil.sys in the root directory of the volume. If
1176 * the file is not present Windows is definitely not suspended.
1177 *
1178 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1179 * definitely suspended (this volume is not the system volume). Caveat: on a
1180 * system with many volumes it is possible that the < 4kiB check is bogus but
1181 * for now this should do fine.
1182 *
1183 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1184 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1185 * Windows is definitely suspended. If it is completely full of zeroes,
1186 * Windows is definitely not hibernated. Any other case is treated as if
1187 * Windows is suspended. This caters for the above mentioned caveat of a
1188 * system with many volumes where no "hibr" magic would be present and there is
1189 * no zero header.
1190 *
1191 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1192 * hibernated on the volume, and -errno on error.
1193 */
1194static int check_windows_hibernation_status(ntfs_volume *vol)
1195{
1196 MFT_REF mref;
1197 struct inode *vi;
1198 ntfs_inode *ni;
1199 struct page *page;
1200 u32 *kaddr, *kend;
1201 ntfs_name *name = NULL;
1202 int ret = 1;
1203 static const ntfschar hiberfil[13] = { const_cpu_to_le16('h'),
1204 const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1205 const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1206 const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1207 const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1208 const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1209 const_cpu_to_le16('s'), 0 };
1210
1211 ntfs_debug("Entering.");
1212 /*
1213 * Find the inode number for the hibernation file by looking up the
1214 * filename hiberfil.sys in the root directory.
1215 */
1216 down(&vol->root_ino->i_sem);
1217 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1218 &name);
1219 up(&vol->root_ino->i_sem);
1220 if (IS_ERR_MREF(mref)) {
1221 ret = MREF_ERR(mref);
1222 /* If the file does not exist, Windows is not hibernated. */
1223 if (ret == -ENOENT) {
1224 ntfs_debug("hiberfil.sys not present. Windows is not "
1225 "hibernated on the volume.");
1226 return 0;
1227 }
1228 /* A real error occured. */
1229 ntfs_error(vol->sb, "Failed to find inode number for "
1230 "hiberfil.sys.");
1231 return ret;
1232 }
1233 /* We do not care for the type of match that was found. */
1234 kfree(name);
1235 /* Get the inode. */
1236 vi = ntfs_iget(vol->sb, MREF(mref));
1237 if (IS_ERR(vi) || is_bad_inode(vi)) {
1238 if (!IS_ERR(vi))
1239 iput(vi);
1240 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1241 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1242 }
1243 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1244 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1245 "Windows is hibernated on the volume. This "
1246 "is not the system volume.", i_size_read(vi));
1247 goto iput_out;
1248 }
1249 ni = NTFS_I(vi);
1250 page = ntfs_map_page(vi->i_mapping, 0);
1251 if (IS_ERR(page)) {
1252 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1253 ret = PTR_ERR(page);
1254 goto iput_out;
1255 }
1256 kaddr = (u32*)page_address(page);
1257 if (*(le32*)kaddr == const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1258 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1259 "hibernated on the volume. This is the "
1260 "system volume.");
1261 goto unm_iput_out;
1262 }
1263 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1264 do {
1265 if (unlikely(*kaddr)) {
1266 ntfs_debug("hiberfil.sys is larger than 4kiB "
1267 "(0x%llx), does not contain the "
1268 "\"hibr\" magic, and does not have a "
1269 "zero header. Windows is hibernated "
1270 "on the volume. This is not the "
1271 "system volume.", i_size_read(vi));
1272 goto unm_iput_out;
1273 }
1274 } while (++kaddr < kend);
1275 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1276 "hibernated on the volume. This is the system "
1277 "volume.");
1278 ret = 0;
1279unm_iput_out:
1280 ntfs_unmap_page(page);
1281iput_out:
1282 iput(vi);
1283 return ret;
1284}
1285
1130/** 1286/**
1131 * load_and_init_quota - load and setup the quota file for a volume if present 1287 * load_and_init_quota - load and setup the quota file for a volume if present
1132 * @vol: ntfs super block describing device whose quota file to load 1288 * @vol: ntfs super block describing device whose quota file to load
@@ -1175,8 +1331,7 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1175 return FALSE; 1331 return FALSE;
1176 } 1332 }
1177 /* We do not care for the type of match that was found. */ 1333 /* We do not care for the type of match that was found. */
1178 if (name) 1334 kfree(name);
1179 kfree(name);
1180 /* Get the inode. */ 1335 /* Get the inode. */
1181 tmp_ino = ntfs_iget(vol->sb, MREF(mref)); 1336 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1182 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) { 1337 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
@@ -1198,6 +1353,167 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1198} 1353}
1199 1354
1200/** 1355/**
1356 * load_and_init_usnjrnl - load and setup the transaction log if present
1357 * @vol: ntfs super block describing device whose usnjrnl file to load
1358 *
1359 * Return TRUE on success or FALSE on error.
1360 *
1361 * If $UsnJrnl is not present or in the process of being disabled, we set
1362 * NVolUsnJrnlStamped() and return success.
1363 *
1364 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1365 * i.e. transaction logging has only just been enabled or the journal has been
1366 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1367 * and return success.
1368 */
1369static BOOL load_and_init_usnjrnl(ntfs_volume *vol)
1370{
1371 MFT_REF mref;
1372 struct inode *tmp_ino;
1373 ntfs_inode *tmp_ni;
1374 struct page *page;
1375 ntfs_name *name = NULL;
1376 USN_HEADER *uh;
1377 static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'),
1378 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1379 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1380 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1381 const_cpu_to_le16('l'), 0 };
1382 static ntfschar Max[5] = { const_cpu_to_le16('$'),
1383 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1384 const_cpu_to_le16('x'), 0 };
1385 static ntfschar J[3] = { const_cpu_to_le16('$'),
1386 const_cpu_to_le16('J'), 0 };
1387
1388 ntfs_debug("Entering.");
1389 /*
1390 * Find the inode number for the transaction log file by looking up the
1391 * filename $UsnJrnl in the extended system files directory $Extend.
1392 */
1393 down(&vol->extend_ino->i_sem);
1394 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1395 &name);
1396 up(&vol->extend_ino->i_sem);
1397 if (IS_ERR_MREF(mref)) {
1398 /*
1399 * If the file does not exist, transaction logging is disabled,
1400 * just return success.
1401 */
1402 if (MREF_ERR(mref) == -ENOENT) {
1403 ntfs_debug("$UsnJrnl not present. Volume does not "
1404 "have transaction logging enabled.");
1405not_enabled:
1406 /*
1407 * No need to try to stamp the transaction log if
1408 * transaction logging is not enabled.
1409 */
1410 NVolSetUsnJrnlStamped(vol);
1411 return TRUE;
1412 }
1413 /* A real error occured. */
1414 ntfs_error(vol->sb, "Failed to find inode number for "
1415 "$UsnJrnl.");
1416 return FALSE;
1417 }
1418 /* We do not care for the type of match that was found. */
1419 kfree(name);
1420 /* Get the inode. */
1421 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1422 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1423 if (!IS_ERR(tmp_ino))
1424 iput(tmp_ino);
1425 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1426 return FALSE;
1427 }
1428 vol->usnjrnl_ino = tmp_ino;
1429 /*
1430 * If the transaction log is in the process of being deleted, we can
1431 * ignore it.
1432 */
1433 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1434 ntfs_debug("$UsnJrnl in the process of being disabled. "
1435 "Volume does not have transaction logging "
1436 "enabled.");
1437 goto not_enabled;
1438 }
1439 /* Get the $DATA/$Max attribute. */
1440 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1441 if (IS_ERR(tmp_ino)) {
1442 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1443 "attribute.");
1444 return FALSE;
1445 }
1446 vol->usnjrnl_max_ino = tmp_ino;
1447 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1448 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1449 "attribute (size is 0x%llx but should be at "
1450 "least 0x%x bytes).", i_size_read(tmp_ino),
1451 sizeof(USN_HEADER));
1452 return FALSE;
1453 }
1454 /* Get the $DATA/$J attribute. */
1455 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1456 if (IS_ERR(tmp_ino)) {
1457 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1458 "attribute.");
1459 return FALSE;
1460 }
1461 vol->usnjrnl_j_ino = tmp_ino;
1462 /* Verify $J is non-resident and sparse. */
1463 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1464 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1465 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1466 "and/or not sparse.");
1467 return FALSE;
1468 }
1469 /* Read the USN_HEADER from $DATA/$Max. */
1470 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1471 if (IS_ERR(page)) {
1472 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1473 "attribute.");
1474 return FALSE;
1475 }
1476 uh = (USN_HEADER*)page_address(page);
1477 /* Sanity check the $Max. */
1478 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1479 sle64_to_cpu(uh->maximum_size))) {
1480 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1481 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1482 (long long)sle64_to_cpu(uh->allocation_delta),
1483 (long long)sle64_to_cpu(uh->maximum_size));
1484 ntfs_unmap_page(page);
1485 return FALSE;
1486 }
1487 /*
1488 * If the transaction log has been stamped and nothing has been written
1489 * to it since, we do not need to stamp it.
1490 */
1491 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1492 i_size_read(vol->usnjrnl_j_ino))) {
1493 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1494 i_size_read(vol->usnjrnl_j_ino))) {
1495 ntfs_unmap_page(page);
1496 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1497 "logged since it was last stamped. "
1498 "Treating this as if the volume does "
1499 "not have transaction logging "
1500 "enabled.");
1501 goto not_enabled;
1502 }
1503 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1504 "which is out of bounds (0x%llx). $UsnJrnl "
1505 "is corrupt.",
1506 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1507 i_size_read(vol->usnjrnl_j_ino));
1508 ntfs_unmap_page(page);
1509 return FALSE;
1510 }
1511 ntfs_unmap_page(page);
1512 ntfs_debug("Done.");
1513 return TRUE;
1514}
1515
1516/**
1201 * load_and_init_attrdef - load the attribute definitions table for a volume 1517 * load_and_init_attrdef - load the attribute definitions table for a volume
1202 * @vol: ntfs super block describing device whose attrdef to load 1518 * @vol: ntfs super block describing device whose attrdef to load
1203 * 1519 *
@@ -1205,10 +1521,11 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1205 */ 1521 */
1206static BOOL load_and_init_attrdef(ntfs_volume *vol) 1522static BOOL load_and_init_attrdef(ntfs_volume *vol)
1207{ 1523{
1524 loff_t i_size;
1208 struct super_block *sb = vol->sb; 1525 struct super_block *sb = vol->sb;
1209 struct inode *ino; 1526 struct inode *ino;
1210 struct page *page; 1527 struct page *page;
1211 unsigned long index, max_index; 1528 pgoff_t index, max_index;
1212 unsigned int size; 1529 unsigned int size;
1213 1530
1214 ntfs_debug("Entering."); 1531 ntfs_debug("Entering.");
@@ -1219,14 +1536,16 @@ static BOOL load_and_init_attrdef(ntfs_volume *vol)
1219 iput(ino); 1536 iput(ino);
1220 goto failed; 1537 goto failed;
1221 } 1538 }
1539 NInoSetSparseDisabled(NTFS_I(ino));
1222 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */ 1540 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1223 if (!ino->i_size || ino->i_size > 0x7fffffff) 1541 i_size = i_size_read(ino);
1542 if (i_size <= 0 || i_size > 0x7fffffff)
1224 goto iput_failed; 1543 goto iput_failed;
1225 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(ino->i_size); 1544 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1226 if (!vol->attrdef) 1545 if (!vol->attrdef)
1227 goto iput_failed; 1546 goto iput_failed;
1228 index = 0; 1547 index = 0;
1229 max_index = ino->i_size >> PAGE_CACHE_SHIFT; 1548 max_index = i_size >> PAGE_CACHE_SHIFT;
1230 size = PAGE_CACHE_SIZE; 1549 size = PAGE_CACHE_SIZE;
1231 while (index < max_index) { 1550 while (index < max_index) {
1232 /* Read the attrdef table and copy it into the linear buffer. */ 1551 /* Read the attrdef table and copy it into the linear buffer. */
@@ -1239,12 +1558,12 @@ read_partial_attrdef_page:
1239 ntfs_unmap_page(page); 1558 ntfs_unmap_page(page);
1240 }; 1559 };
1241 if (size == PAGE_CACHE_SIZE) { 1560 if (size == PAGE_CACHE_SIZE) {
1242 size = ino->i_size & ~PAGE_CACHE_MASK; 1561 size = i_size & ~PAGE_CACHE_MASK;
1243 if (size) 1562 if (size)
1244 goto read_partial_attrdef_page; 1563 goto read_partial_attrdef_page;
1245 } 1564 }
1246 vol->attrdef_size = ino->i_size; 1565 vol->attrdef_size = i_size;
1247 ntfs_debug("Read %llu bytes from $AttrDef.", ino->i_size); 1566 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1248 iput(ino); 1567 iput(ino);
1249 return TRUE; 1568 return TRUE;
1250free_iput_failed: 1569free_iput_failed:
@@ -1267,10 +1586,11 @@ failed:
1267 */ 1586 */
1268static BOOL load_and_init_upcase(ntfs_volume *vol) 1587static BOOL load_and_init_upcase(ntfs_volume *vol)
1269{ 1588{
1589 loff_t i_size;
1270 struct super_block *sb = vol->sb; 1590 struct super_block *sb = vol->sb;
1271 struct inode *ino; 1591 struct inode *ino;
1272 struct page *page; 1592 struct page *page;
1273 unsigned long index, max_index; 1593 pgoff_t index, max_index;
1274 unsigned int size; 1594 unsigned int size;
1275 int i, max; 1595 int i, max;
1276 1596
@@ -1286,14 +1606,15 @@ static BOOL load_and_init_upcase(ntfs_volume *vol)
1286 * The upcase size must not be above 64k Unicode characters, must not 1606 * The upcase size must not be above 64k Unicode characters, must not
1287 * be zero and must be a multiple of sizeof(ntfschar). 1607 * be zero and must be a multiple of sizeof(ntfschar).
1288 */ 1608 */
1289 if (!ino->i_size || ino->i_size & (sizeof(ntfschar) - 1) || 1609 i_size = i_size_read(ino);
1290 ino->i_size > 64ULL * 1024 * sizeof(ntfschar)) 1610 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1611 i_size > 64ULL * 1024 * sizeof(ntfschar))
1291 goto iput_upcase_failed; 1612 goto iput_upcase_failed;
1292 vol->upcase = (ntfschar*)ntfs_malloc_nofs(ino->i_size); 1613 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1293 if (!vol->upcase) 1614 if (!vol->upcase)
1294 goto iput_upcase_failed; 1615 goto iput_upcase_failed;
1295 index = 0; 1616 index = 0;
1296 max_index = ino->i_size >> PAGE_CACHE_SHIFT; 1617 max_index = i_size >> PAGE_CACHE_SHIFT;
1297 size = PAGE_CACHE_SIZE; 1618 size = PAGE_CACHE_SIZE;
1298 while (index < max_index) { 1619 while (index < max_index) {
1299 /* Read the upcase table and copy it into the linear buffer. */ 1620 /* Read the upcase table and copy it into the linear buffer. */
@@ -1306,13 +1627,13 @@ read_partial_upcase_page:
1306 ntfs_unmap_page(page); 1627 ntfs_unmap_page(page);
1307 }; 1628 };
1308 if (size == PAGE_CACHE_SIZE) { 1629 if (size == PAGE_CACHE_SIZE) {
1309 size = ino->i_size & ~PAGE_CACHE_MASK; 1630 size = i_size & ~PAGE_CACHE_MASK;
1310 if (size) 1631 if (size)
1311 goto read_partial_upcase_page; 1632 goto read_partial_upcase_page;
1312 } 1633 }
1313 vol->upcase_len = ino->i_size >> UCHAR_T_SIZE_BITS; 1634 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1314 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).", 1635 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1315 ino->i_size, 64 * 1024 * sizeof(ntfschar)); 1636 i_size, 64 * 1024 * sizeof(ntfschar));
1316 iput(ino); 1637 iput(ino);
1317 down(&ntfs_lock); 1638 down(&ntfs_lock);
1318 if (!default_upcase) { 1639 if (!default_upcase) {
@@ -1376,6 +1697,10 @@ static BOOL load_system_files(ntfs_volume *vol)
1376 MFT_RECORD *m; 1697 MFT_RECORD *m;
1377 VOLUME_INFORMATION *vi; 1698 VOLUME_INFORMATION *vi;
1378 ntfs_attr_search_ctx *ctx; 1699 ntfs_attr_search_ctx *ctx;
1700#ifdef NTFS_RW
1701 RESTART_PAGE_HEADER *rp;
1702 int err;
1703#endif /* NTFS_RW */
1379 1704
1380 ntfs_debug("Entering."); 1705 ntfs_debug("Entering.");
1381#ifdef NTFS_RW 1706#ifdef NTFS_RW
@@ -1435,7 +1760,8 @@ static BOOL load_system_files(ntfs_volume *vol)
1435 iput(vol->lcnbmp_ino); 1760 iput(vol->lcnbmp_ino);
1436 goto bitmap_failed; 1761 goto bitmap_failed;
1437 } 1762 }
1438 if ((vol->nr_clusters + 7) >> 3 > vol->lcnbmp_ino->i_size) { 1763 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1764 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1439 iput(vol->lcnbmp_ino); 1765 iput(vol->lcnbmp_ino);
1440bitmap_failed: 1766bitmap_failed:
1441 ntfs_error(sb, "Failed to load $Bitmap."); 1767 ntfs_error(sb, "Failed to load $Bitmap.");
@@ -1486,6 +1812,12 @@ get_ctx_vol_failed:
1486 unmap_mft_record(NTFS_I(vol->vol_ino)); 1812 unmap_mft_record(NTFS_I(vol->vol_ino));
1487 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver, 1813 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1488 vol->minor_ver); 1814 vol->minor_ver);
1815 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1816 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1817 "volume version %i.%i (need at least version "
1818 "3.0).", vol->major_ver, vol->minor_ver);
1819 NVolClearSparseEnabled(vol);
1820 }
1489#ifdef NTFS_RW 1821#ifdef NTFS_RW
1490 /* Make sure that no unsupported volume flags are set. */ 1822 /* Make sure that no unsupported volume flags are set. */
1491 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 1823 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
@@ -1519,8 +1851,9 @@ get_ctx_vol_failed:
1519 * Get the inode for the logfile, check it and determine if the volume 1851 * Get the inode for the logfile, check it and determine if the volume
1520 * was shutdown cleanly. 1852 * was shutdown cleanly.
1521 */ 1853 */
1522 if (!load_and_check_logfile(vol) || 1854 rp = NULL;
1523 !ntfs_is_logfile_clean(vol->logfile_ino)) { 1855 if (!load_and_check_logfile(vol, &rp) ||
1856 !ntfs_is_logfile_clean(vol->logfile_ino, rp)) {
1524 static const char *es1a = "Failed to load $LogFile"; 1857 static const char *es1a = "Failed to load $LogFile";
1525 static const char *es1b = "$LogFile is not clean"; 1858 static const char *es1b = "$LogFile is not clean";
1526 static const char *es2 = ". Mount in Windows."; 1859 static const char *es2 = ". Mount in Windows.";
@@ -1535,6 +1868,10 @@ get_ctx_vol_failed:
1535 "continue nor on_errors=" 1868 "continue nor on_errors="
1536 "remount-ro was specified%s", 1869 "remount-ro was specified%s",
1537 es1, es2); 1870 es1, es2);
1871 if (vol->logfile_ino) {
1872 BUG_ON(!rp);
1873 ntfs_free(rp);
1874 }
1538 goto iput_logfile_err_out; 1875 goto iput_logfile_err_out;
1539 } 1876 }
1540 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1877 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
@@ -1545,6 +1882,51 @@ get_ctx_vol_failed:
1545 /* This will prevent a read-write remount. */ 1882 /* This will prevent a read-write remount. */
1546 NVolSetErrors(vol); 1883 NVolSetErrors(vol);
1547 } 1884 }
1885 ntfs_free(rp);
1886#endif /* NTFS_RW */
1887 /* Get the root directory inode so we can do path lookups. */
1888 vol->root_ino = ntfs_iget(sb, FILE_root);
1889 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1890 if (!IS_ERR(vol->root_ino))
1891 iput(vol->root_ino);
1892 ntfs_error(sb, "Failed to load root directory.");
1893 goto iput_logfile_err_out;
1894 }
1895#ifdef NTFS_RW
1896 /*
1897 * Check if Windows is suspended to disk on the target volume. If it
1898 * is hibernated, we must not write *anything* to the disk so set
1899 * NVolErrors() without setting the dirty volume flag and mount
1900 * read-only. This will prevent read-write remounting and it will also
1901 * prevent all writes.
1902 */
1903 err = check_windows_hibernation_status(vol);
1904 if (unlikely(err)) {
1905 static const char *es1a = "Failed to determine if Windows is "
1906 "hibernated";
1907 static const char *es1b = "Windows is hibernated";
1908 static const char *es2 = ". Run chkdsk.";
1909 const char *es1;
1910
1911 es1 = err < 0 ? es1a : es1b;
1912 /* If a read-write mount, convert it to a read-only mount. */
1913 if (!(sb->s_flags & MS_RDONLY)) {
1914 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1915 ON_ERRORS_CONTINUE))) {
1916 ntfs_error(sb, "%s and neither on_errors="
1917 "continue nor on_errors="
1918 "remount-ro was specified%s",
1919 es1, es2);
1920 goto iput_root_err_out;
1921 }
1922 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1923 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1924 } else
1925 ntfs_warning(sb, "%s. Will not be able to remount "
1926 "read-write%s", es1, es2);
1927 /* This will prevent a read-write remount. */
1928 NVolSetErrors(vol);
1929 }
1548 /* If (still) a read-write mount, mark the volume dirty. */ 1930 /* If (still) a read-write mount, mark the volume dirty. */
1549 if (!(sb->s_flags & MS_RDONLY) && 1931 if (!(sb->s_flags & MS_RDONLY) &&
1550 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) { 1932 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
@@ -1558,7 +1940,7 @@ get_ctx_vol_failed:
1558 ntfs_error(sb, "%s and neither on_errors=continue nor " 1940 ntfs_error(sb, "%s and neither on_errors=continue nor "
1559 "on_errors=remount-ro was specified%s", 1941 "on_errors=remount-ro was specified%s",
1560 es1, es2); 1942 es1, es2);
1561 goto iput_logfile_err_out; 1943 goto iput_root_err_out;
1562 } 1944 }
1563 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1945 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1564 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1946 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
@@ -1585,7 +1967,7 @@ get_ctx_vol_failed:
1585 ntfs_error(sb, "%s and neither on_errors=continue nor " 1967 ntfs_error(sb, "%s and neither on_errors=continue nor "
1586 "on_errors=remount-ro was specified%s", 1968 "on_errors=remount-ro was specified%s",
1587 es1, es2); 1969 es1, es2);
1588 goto iput_logfile_err_out; 1970 goto iput_root_err_out;
1589 } 1971 }
1590 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1972 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1591 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1973 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
@@ -1604,23 +1986,15 @@ get_ctx_vol_failed:
1604 ntfs_error(sb, "%s and neither on_errors=continue nor " 1986 ntfs_error(sb, "%s and neither on_errors=continue nor "
1605 "on_errors=remount-ro was specified%s", 1987 "on_errors=remount-ro was specified%s",
1606 es1, es2); 1988 es1, es2);
1607 goto iput_logfile_err_out; 1989 goto iput_root_err_out;
1608 } 1990 }
1609 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1991 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1610 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1992 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1611 NVolSetErrors(vol); 1993 NVolSetErrors(vol);
1612 } 1994 }
1613#endif /* NTFS_RW */ 1995#endif /* NTFS_RW */
1614 /* Get the root directory inode. */
1615 vol->root_ino = ntfs_iget(sb, FILE_root);
1616 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1617 if (!IS_ERR(vol->root_ino))
1618 iput(vol->root_ino);
1619 ntfs_error(sb, "Failed to load root directory.");
1620 goto iput_logfile_err_out;
1621 }
1622 /* If on NTFS versions before 3.0, we are done. */ 1996 /* If on NTFS versions before 3.0, we are done. */
1623 if (vol->major_ver < 3) 1997 if (unlikely(vol->major_ver < 3))
1624 return TRUE; 1998 return TRUE;
1625 /* NTFS 3.0+ specific initialization. */ 1999 /* NTFS 3.0+ specific initialization. */
1626 /* Get the security descriptors inode. */ 2000 /* Get the security descriptors inode. */
@@ -1631,7 +2005,7 @@ get_ctx_vol_failed:
1631 ntfs_error(sb, "Failed to load $Secure."); 2005 ntfs_error(sb, "Failed to load $Secure.");
1632 goto iput_root_err_out; 2006 goto iput_root_err_out;
1633 } 2007 }
1634 // FIXME: Initialize security. 2008 // TODO: Initialize security.
1635 /* Get the extended system files' directory inode. */ 2009 /* Get the extended system files' directory inode. */
1636 vol->extend_ino = ntfs_iget(sb, FILE_Extend); 2010 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
1637 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) { 2011 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
@@ -1682,10 +2056,60 @@ get_ctx_vol_failed:
1682 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 2056 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1683 NVolSetErrors(vol); 2057 NVolSetErrors(vol);
1684 } 2058 }
1685 // TODO: Delete or checkpoint the $UsnJrnl if it exists. 2059 /*
2060 * Find the transaction log file ($UsnJrnl), load it if present, check
2061 * it, and set it up.
2062 */
2063 if (!load_and_init_usnjrnl(vol)) {
2064 static const char *es1 = "Failed to load $UsnJrnl";
2065 static const char *es2 = ". Run chkdsk.";
2066
2067 /* If a read-write mount, convert it to a read-only mount. */
2068 if (!(sb->s_flags & MS_RDONLY)) {
2069 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2070 ON_ERRORS_CONTINUE))) {
2071 ntfs_error(sb, "%s and neither on_errors="
2072 "continue nor on_errors="
2073 "remount-ro was specified%s",
2074 es1, es2);
2075 goto iput_usnjrnl_err_out;
2076 }
2077 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2078 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2079 } else
2080 ntfs_warning(sb, "%s. Will not be able to remount "
2081 "read-write%s", es1, es2);
2082 /* This will prevent a read-write remount. */
2083 NVolSetErrors(vol);
2084 }
2085 /* If (still) a read-write mount, stamp the transaction log. */
2086 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2087 static const char *es1 = "Failed to stamp transaction log "
2088 "($UsnJrnl)";
2089 static const char *es2 = ". Run chkdsk.";
2090
2091 /* Convert to a read-only mount. */
2092 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2093 ON_ERRORS_CONTINUE))) {
2094 ntfs_error(sb, "%s and neither on_errors=continue nor "
2095 "on_errors=remount-ro was specified%s",
2096 es1, es2);
2097 goto iput_usnjrnl_err_out;
2098 }
2099 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2100 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2101 NVolSetErrors(vol);
2102 }
1686#endif /* NTFS_RW */ 2103#endif /* NTFS_RW */
1687 return TRUE; 2104 return TRUE;
1688#ifdef NTFS_RW 2105#ifdef NTFS_RW
2106iput_usnjrnl_err_out:
2107 if (vol->usnjrnl_j_ino)
2108 iput(vol->usnjrnl_j_ino);
2109 if (vol->usnjrnl_max_ino)
2110 iput(vol->usnjrnl_max_ino);
2111 if (vol->usnjrnl_ino)
2112 iput(vol->usnjrnl_ino);
1689iput_quota_err_out: 2113iput_quota_err_out:
1690 if (vol->quota_q_ino) 2114 if (vol->quota_q_ino)
1691 iput(vol->quota_q_ino); 2115 iput(vol->quota_q_ino);
@@ -1759,6 +2183,12 @@ static void ntfs_put_super(struct super_block *sb)
1759 2183
1760 /* NTFS 3.0+ specific. */ 2184 /* NTFS 3.0+ specific. */
1761 if (vol->major_ver >= 3) { 2185 if (vol->major_ver >= 3) {
2186 if (vol->usnjrnl_j_ino)
2187 ntfs_commit_inode(vol->usnjrnl_j_ino);
2188 if (vol->usnjrnl_max_ino)
2189 ntfs_commit_inode(vol->usnjrnl_max_ino);
2190 if (vol->usnjrnl_ino)
2191 ntfs_commit_inode(vol->usnjrnl_ino);
1762 if (vol->quota_q_ino) 2192 if (vol->quota_q_ino)
1763 ntfs_commit_inode(vol->quota_q_ino); 2193 ntfs_commit_inode(vol->quota_q_ino);
1764 if (vol->quota_ino) 2194 if (vol->quota_ino)
@@ -1814,6 +2244,18 @@ static void ntfs_put_super(struct super_block *sb)
1814 /* NTFS 3.0+ specific clean up. */ 2244 /* NTFS 3.0+ specific clean up. */
1815 if (vol->major_ver >= 3) { 2245 if (vol->major_ver >= 3) {
1816#ifdef NTFS_RW 2246#ifdef NTFS_RW
2247 if (vol->usnjrnl_j_ino) {
2248 iput(vol->usnjrnl_j_ino);
2249 vol->usnjrnl_j_ino = NULL;
2250 }
2251 if (vol->usnjrnl_max_ino) {
2252 iput(vol->usnjrnl_max_ino);
2253 vol->usnjrnl_max_ino = NULL;
2254 }
2255 if (vol->usnjrnl_ino) {
2256 iput(vol->usnjrnl_ino);
2257 vol->usnjrnl_ino = NULL;
2258 }
1817 if (vol->quota_q_ino) { 2259 if (vol->quota_q_ino) {
1818 iput(vol->quota_q_ino); 2260 iput(vol->quota_q_ino);
1819 vol->quota_q_ino = NULL; 2261 vol->quota_q_ino = NULL;
@@ -1959,8 +2401,7 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
1959 struct address_space *mapping = vol->lcnbmp_ino->i_mapping; 2401 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
1960 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2402 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
1961 struct page *page; 2403 struct page *page;
1962 unsigned long index, max_index; 2404 pgoff_t index, max_index;
1963 unsigned int max_size;
1964 2405
1965 ntfs_debug("Entering."); 2406 ntfs_debug("Entering.");
1966 /* Serialize accesses to the cluster bitmap. */ 2407 /* Serialize accesses to the cluster bitmap. */
@@ -1972,11 +2413,10 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
1972 */ 2413 */
1973 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> 2414 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
1974 PAGE_CACHE_SHIFT; 2415 PAGE_CACHE_SHIFT;
1975 /* Use multiples of 4 bytes. */ 2416 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
1976 max_size = PAGE_CACHE_SIZE >> 2; 2417 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
1977 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%x.", 2418 max_index, PAGE_CACHE_SIZE / 4);
1978 max_index, max_size); 2419 for (index = 0; index < max_index; index++) {
1979 for (index = 0UL; index < max_index; index++) {
1980 unsigned int i; 2420 unsigned int i;
1981 /* 2421 /*
1982 * Read the page from page cache, getting it from backing store 2422 * Read the page from page cache, getting it from backing store
@@ -2008,7 +2448,7 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2008 * the result as all out of range bytes are set to zero by 2448 * the result as all out of range bytes are set to zero by
2009 * ntfs_readpage(). 2449 * ntfs_readpage().
2010 */ 2450 */
2011 for (i = 0; i < max_size; i++) 2451 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2012 nr_free -= (s64)hweight32(kaddr[i]); 2452 nr_free -= (s64)hweight32(kaddr[i]);
2013 kunmap_atomic(kaddr, KM_USER0); 2453 kunmap_atomic(kaddr, KM_USER0);
2014 page_cache_release(page); 2454 page_cache_release(page);
@@ -2031,6 +2471,8 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2031/** 2471/**
2032 * __get_nr_free_mft_records - return the number of free inodes on a volume 2472 * __get_nr_free_mft_records - return the number of free inodes on a volume
2033 * @vol: ntfs volume for which to obtain free inode count 2473 * @vol: ntfs volume for which to obtain free inode count
2474 * @nr_free: number of mft records in filesystem
2475 * @max_index: maximum number of pages containing set bits
2034 * 2476 *
2035 * Calculate the number of free mft records (inodes) on the mounted NTFS 2477 * Calculate the number of free mft records (inodes) on the mounted NTFS
2036 * volume @vol. We actually calculate the number of mft records in use instead 2478 * volume @vol. We actually calculate the number of mft records in use instead
@@ -2043,32 +2485,20 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2043 * 2485 *
2044 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing. 2486 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2045 */ 2487 */
2046static unsigned long __get_nr_free_mft_records(ntfs_volume *vol) 2488static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2489 s64 nr_free, const pgoff_t max_index)
2047{ 2490{
2048 s64 nr_free;
2049 u32 *kaddr; 2491 u32 *kaddr;
2050 struct address_space *mapping = vol->mftbmp_ino->i_mapping; 2492 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2051 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2493 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2052 struct page *page; 2494 struct page *page;
2053 unsigned long index, max_index; 2495 pgoff_t index;
2054 unsigned int max_size;
2055 2496
2056 ntfs_debug("Entering."); 2497 ntfs_debug("Entering.");
2057 /* Number of mft records in file system (at this point in time). */ 2498 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2058 nr_free = vol->mft_ino->i_size >> vol->mft_record_size_bits;
2059 /*
2060 * Convert the maximum number of set bits into bytes rounded up, then
2061 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2062 * have one full and one partial page max_index = 2.
2063 */
2064 max_index = ((((NTFS_I(vol->mft_ino)->initialized_size >>
2065 vol->mft_record_size_bits) + 7) >> 3) +
2066 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2067 /* Use multiples of 4 bytes. */
2068 max_size = PAGE_CACHE_SIZE >> 2;
2069 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = " 2499 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2070 "0x%x.", max_index, max_size); 2500 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2071 for (index = 0UL; index < max_index; index++) { 2501 for (index = 0; index < max_index; index++) {
2072 unsigned int i; 2502 unsigned int i;
2073 /* 2503 /*
2074 * Read the page from page cache, getting it from backing store 2504 * Read the page from page cache, getting it from backing store
@@ -2100,7 +2530,7 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
2100 * the result as all out of range bytes are set to zero by 2530 * the result as all out of range bytes are set to zero by
2101 * ntfs_readpage(). 2531 * ntfs_readpage().
2102 */ 2532 */
2103 for (i = 0; i < max_size; i++) 2533 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2104 nr_free -= (s64)hweight32(kaddr[i]); 2534 nr_free -= (s64)hweight32(kaddr[i]);
2105 kunmap_atomic(kaddr, KM_USER0); 2535 kunmap_atomic(kaddr, KM_USER0);
2106 page_cache_release(page); 2536 page_cache_release(page);
@@ -2134,8 +2564,11 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
2134 */ 2564 */
2135static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs) 2565static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2136{ 2566{
2137 ntfs_volume *vol = NTFS_SB(sb);
2138 s64 size; 2567 s64 size;
2568 ntfs_volume *vol = NTFS_SB(sb);
2569 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2570 pgoff_t max_index;
2571 unsigned long flags;
2139 2572
2140 ntfs_debug("Entering."); 2573 ntfs_debug("Entering.");
2141 /* Type of filesystem. */ 2574 /* Type of filesystem. */
@@ -2143,13 +2576,13 @@ static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2143 /* Optimal transfer block size. */ 2576 /* Optimal transfer block size. */
2144 sfs->f_bsize = PAGE_CACHE_SIZE; 2577 sfs->f_bsize = PAGE_CACHE_SIZE;
2145 /* 2578 /*
2146 * Total data blocks in file system in units of f_bsize and since 2579 * Total data blocks in filesystem in units of f_bsize and since
2147 * inodes are also stored in data blocs ($MFT is a file) this is just 2580 * inodes are also stored in data blocs ($MFT is a file) this is just
2148 * the total clusters. 2581 * the total clusters.
2149 */ 2582 */
2150 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >> 2583 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2151 PAGE_CACHE_SHIFT; 2584 PAGE_CACHE_SHIFT;
2152 /* Free data blocks in file system in units of f_bsize. */ 2585 /* Free data blocks in filesystem in units of f_bsize. */
2153 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >> 2586 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2154 PAGE_CACHE_SHIFT; 2587 PAGE_CACHE_SHIFT;
2155 if (size < 0LL) 2588 if (size < 0LL)
@@ -2158,17 +2591,27 @@ static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2158 sfs->f_bavail = sfs->f_bfree = size; 2591 sfs->f_bavail = sfs->f_bfree = size;
2159 /* Serialize accesses to the inode bitmap. */ 2592 /* Serialize accesses to the inode bitmap. */
2160 down_read(&vol->mftbmp_lock); 2593 down_read(&vol->mftbmp_lock);
2161 /* Number of inodes in file system (at this point in time). */ 2594 read_lock_irqsave(&mft_ni->size_lock, flags);
2162 sfs->f_files = vol->mft_ino->i_size >> vol->mft_record_size_bits; 2595 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2596 /*
2597 * Convert the maximum number of set bits into bytes rounded up, then
2598 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2599 * have one full and one partial page max_index = 2.
2600 */
2601 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2602 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2603 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2604 /* Number of inodes in filesystem (at this point in time). */
2605 sfs->f_files = size;
2163 /* Free inodes in fs (based on current total count). */ 2606 /* Free inodes in fs (based on current total count). */
2164 sfs->f_ffree = __get_nr_free_mft_records(vol); 2607 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2165 up_read(&vol->mftbmp_lock); 2608 up_read(&vol->mftbmp_lock);
2166 /* 2609 /*
2167 * File system id. This is extremely *nix flavour dependent and even 2610 * File system id. This is extremely *nix flavour dependent and even
2168 * within Linux itself all fs do their own thing. I interpret this to 2611 * within Linux itself all fs do their own thing. I interpret this to
2169 * mean a unique id associated with the mounted fs and not the id 2612 * mean a unique id associated with the mounted fs and not the id
2170 * associated with the file system driver, the latter is already given 2613 * associated with the filesystem driver, the latter is already given
2171 * by the file system type in sfs->f_type. Thus we use the 64-bit 2614 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2172 * volume serial number splitting it into two 32-bit parts. We enter 2615 * volume serial number splitting it into two 32-bit parts. We enter
2173 * the least significant 32-bits in f_fsid[0] and the most significant 2616 * the least significant 32-bits in f_fsid[0] and the most significant
2174 * 32-bits in f_fsid[1]. 2617 * 32-bits in f_fsid[1].
@@ -2219,53 +2662,19 @@ static struct super_operations ntfs_sops = {
2219 proc. */ 2662 proc. */
2220}; 2663};
2221 2664
2222
2223/**
2224 * Declarations for NTFS specific export operations (fs/ntfs/namei.c).
2225 */
2226extern struct dentry *ntfs_get_parent(struct dentry *child_dent);
2227extern struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh);
2228
2229/**
2230 * Export operations allowing NFS exporting of mounted NTFS partitions.
2231 *
2232 * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
2233 * use 32 bits to store the inode number which is an unsigned long so on 64-bit
2234 * architectures is usually 64 bits so it would all fail horribly on huge
2235 * volumes. I guess we need to define our own encode and decode fh functions
2236 * that store 64-bit inode numbers at some point but for now we will ignore the
2237 * problem...
2238 *
2239 * We also use the default ->get_name() helper (used by ->decode_fh() via
2240 * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
2241 * independent.
2242 *
2243 * The default ->get_parent() just returns -EACCES so we have to provide our
2244 * own and the default ->get_dentry() is incompatible with NTFS due to not
2245 * allowing the inode number 0 which is used in NTFS for the system file $MFT
2246 * and due to using iget() whereas NTFS needs ntfs_iget().
2247 */
2248static struct export_operations ntfs_export_ops = {
2249 .get_parent = ntfs_get_parent, /* Find the parent of a given
2250 directory. */
2251 .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode
2252 given a file handle
2253 sub-fragment. */
2254};
2255
2256/** 2665/**
2257 * ntfs_fill_super - mount an ntfs files system 2666 * ntfs_fill_super - mount an ntfs filesystem
2258 * @sb: super block of ntfs file system to mount 2667 * @sb: super block of ntfs filesystem to mount
2259 * @opt: string containing the mount options 2668 * @opt: string containing the mount options
2260 * @silent: silence error output 2669 * @silent: silence error output
2261 * 2670 *
2262 * ntfs_fill_super() is called by the VFS to mount the device described by @sb 2671 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2263 * with the mount otions in @data with the NTFS file system. 2672 * with the mount otions in @data with the NTFS filesystem.
2264 * 2673 *
2265 * If @silent is true, remain silent even if errors are detected. This is used 2674 * If @silent is true, remain silent even if errors are detected. This is used
2266 * during bootup, when the kernel tries to mount the root file system with all 2675 * during bootup, when the kernel tries to mount the root filesystem with all
2267 * registered file systems one after the other until one succeeds. This implies 2676 * registered filesystems one after the other until one succeeds. This implies
2268 * that all file systems except the correct one will quite correctly and 2677 * that all filesystems except the correct one will quite correctly and
2269 * expectedly return an error, but nobody wants to see error messages when in 2678 * expectedly return an error, but nobody wants to see error messages when in
2270 * fact this is what is supposed to happen. 2679 * fact this is what is supposed to happen.
2271 * 2680 *
@@ -2292,39 +2701,25 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2292 return -ENOMEM; 2701 return -ENOMEM;
2293 } 2702 }
2294 /* Initialize ntfs_volume structure. */ 2703 /* Initialize ntfs_volume structure. */
2295 memset(vol, 0, sizeof(ntfs_volume)); 2704 *vol = (ntfs_volume) {
2296 vol->sb = sb; 2705 .sb = sb,
2297 vol->upcase = NULL; 2706 /*
2298 vol->attrdef = NULL; 2707 * Default is group and other don't have any access to files or
2299 vol->mft_ino = NULL; 2708 * directories while owner has full access. Further, files by
2300 vol->mftbmp_ino = NULL; 2709 * default are not executable but directories are of course
2710 * browseable.
2711 */
2712 .fmask = 0177,
2713 .dmask = 0077,
2714 };
2301 init_rwsem(&vol->mftbmp_lock); 2715 init_rwsem(&vol->mftbmp_lock);
2302#ifdef NTFS_RW
2303 vol->mftmirr_ino = NULL;
2304 vol->logfile_ino = NULL;
2305#endif /* NTFS_RW */
2306 vol->lcnbmp_ino = NULL;
2307 init_rwsem(&vol->lcnbmp_lock); 2716 init_rwsem(&vol->lcnbmp_lock);
2308 vol->vol_ino = NULL;
2309 vol->root_ino = NULL;
2310 vol->secure_ino = NULL;
2311 vol->extend_ino = NULL;
2312#ifdef NTFS_RW
2313 vol->quota_ino = NULL;
2314 vol->quota_q_ino = NULL;
2315#endif /* NTFS_RW */
2316 vol->nls_map = NULL;
2317
2318 /*
2319 * Default is group and other don't have any access to files or
2320 * directories while owner has full access. Further, files by default
2321 * are not executable but directories are of course browseable.
2322 */
2323 vol->fmask = 0177;
2324 vol->dmask = 0077;
2325 2717
2326 unlock_kernel(); 2718 unlock_kernel();
2327 2719
2720 /* By default, enable sparse support. */
2721 NVolSetSparseEnabled(vol);
2722
2328 /* Important to get the mount options dealt with now. */ 2723 /* Important to get the mount options dealt with now. */
2329 if (!parse_options(vol, (char*)opt)) 2724 if (!parse_options(vol, (char*)opt))
2330 goto err_out_now; 2725 goto err_out_now;
@@ -2347,7 +2742,8 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2347 } 2742 }
2348 2743
2349 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */ 2744 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2350 vol->nr_blocks = sb->s_bdev->bd_inode->i_size >> NTFS_BLOCK_SIZE_BITS; 2745 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2746 NTFS_BLOCK_SIZE_BITS;
2351 2747
2352 /* Read the boot sector and return unlocked buffer head to it. */ 2748 /* Read the boot sector and return unlocked buffer head to it. */
2353 if (!(bh = read_ntfs_boot_sector(sb, silent))) { 2749 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
@@ -2476,6 +2872,18 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2476 /* NTFS 3.0+ specific clean up. */ 2872 /* NTFS 3.0+ specific clean up. */
2477 if (vol->major_ver >= 3) { 2873 if (vol->major_ver >= 3) {
2478#ifdef NTFS_RW 2874#ifdef NTFS_RW
2875 if (vol->usnjrnl_j_ino) {
2876 iput(vol->usnjrnl_j_ino);
2877 vol->usnjrnl_j_ino = NULL;
2878 }
2879 if (vol->usnjrnl_max_ino) {
2880 iput(vol->usnjrnl_max_ino);
2881 vol->usnjrnl_max_ino = NULL;
2882 }
2883 if (vol->usnjrnl_ino) {
2884 iput(vol->usnjrnl_ino);
2885 vol->usnjrnl_ino = NULL;
2886 }
2479 if (vol->quota_q_ino) { 2887 if (vol->quota_q_ino) {
2480 iput(vol->quota_q_ino); 2888 iput(vol->quota_q_ino);
2481 vol->quota_q_ino = NULL; 2889 vol->quota_q_ino = NULL;
@@ -2581,7 +2989,7 @@ err_out_now:
2581 */ 2989 */
2582kmem_cache_t *ntfs_name_cache; 2990kmem_cache_t *ntfs_name_cache;
2583 2991
2584/* Slab caches for efficient allocation/deallocation of of inodes. */ 2992/* Slab caches for efficient allocation/deallocation of inodes. */
2585kmem_cache_t *ntfs_inode_cache; 2993kmem_cache_t *ntfs_inode_cache;
2586kmem_cache_t *ntfs_big_inode_cache; 2994kmem_cache_t *ntfs_big_inode_cache;
2587 2995
@@ -2705,7 +3113,7 @@ static int __init init_ntfs_fs(void)
2705 ntfs_debug("NTFS driver registered successfully."); 3113 ntfs_debug("NTFS driver registered successfully.");
2706 return 0; /* Success! */ 3114 return 0; /* Success! */
2707 } 3115 }
2708 printk(KERN_CRIT "NTFS: Failed to register NTFS file system driver!\n"); 3116 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
2709 3117
2710sysctl_err_out: 3118sysctl_err_out:
2711 kmem_cache_destroy(ntfs_big_inode_cache); 3119 kmem_cache_destroy(ntfs_big_inode_cache);
@@ -2719,7 +3127,7 @@ actx_err_out:
2719 kmem_cache_destroy(ntfs_index_ctx_cache); 3127 kmem_cache_destroy(ntfs_index_ctx_cache);
2720ictx_err_out: 3128ictx_err_out:
2721 if (!err) { 3129 if (!err) {
2722 printk(KERN_CRIT "NTFS: Aborting NTFS file system driver " 3130 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
2723 "registration...\n"); 3131 "registration...\n");
2724 err = -ENOMEM; 3132 err = -ENOMEM;
2725 } 3133 }
@@ -2759,7 +3167,7 @@ static void __exit exit_ntfs_fs(void)
2759} 3167}
2760 3168
2761MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>"); 3169MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
2762MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2004 Anton Altaparmakov"); 3170MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
2763MODULE_VERSION(NTFS_VERSION); 3171MODULE_VERSION(NTFS_VERSION);
2764MODULE_LICENSE("GPL"); 3172MODULE_LICENSE("GPL");
2765#ifdef DEBUG 3173#ifdef DEBUG
diff --git a/fs/ntfs/sysctl.c b/fs/ntfs/sysctl.c
index 75067e4f3036..1c23138d00b3 100644
--- a/fs/ntfs/sysctl.c
+++ b/fs/ntfs/sysctl.c
@@ -3,7 +3,7 @@
3 * the Linux-NTFS project. Adapted from the old NTFS driver, 3 * the Linux-NTFS project. Adapted from the old NTFS driver,
4 * Copyright (C) 1997 Martin von Löwis, Régis Duchesne 4 * Copyright (C) 1997 Martin von Löwis, Régis Duchesne
5 * 5 *
6 * Copyright (c) 2002-2004 Anton Altaparmakov 6 * Copyright (c) 2002-2005 Anton Altaparmakov
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published 9 * modify it under the terms of the GNU General Public License as published
@@ -67,7 +67,7 @@ int ntfs_sysctl(int add)
67 return -ENOMEM; 67 return -ENOMEM;
68#ifdef CONFIG_PROC_FS 68#ifdef CONFIG_PROC_FS
69 /* 69 /*
70 * If the proc file system is in use and we are a module, need 70 * If the proc filesystem is in use and we are a module, need
71 * to set the owner of our proc entry to our module. In the 71 * to set the owner of our proc entry to our module. In the
72 * non-modular case, THIS_MODULE is NULL, so this is ok. 72 * non-modular case, THIS_MODULE is NULL, so this is ok.
73 */ 73 */
diff --git a/fs/ntfs/sysctl.h b/fs/ntfs/sysctl.h
index df749cc0aac8..c8064cae8f17 100644
--- a/fs/ntfs/sysctl.h
+++ b/fs/ntfs/sysctl.h
@@ -26,7 +26,7 @@
26 26
27#include <linux/config.h> 27#include <linux/config.h>
28 28
29#if (DEBUG && CONFIG_SYSCTL) 29#if defined(DEBUG) && defined(CONFIG_SYSCTL)
30 30
31extern int ntfs_sysctl(int add); 31extern int ntfs_sysctl(int add);
32 32
diff --git a/fs/ntfs/time.h b/fs/ntfs/time.h
index a09a51dabe4e..01233989d5d1 100644
--- a/fs/ntfs/time.h
+++ b/fs/ntfs/time.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * time.h - NTFS time conversion functions. Part of the Linux-NTFS project. 2 * time.h - NTFS time conversion functions. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * 5 *
6 * This program/include file is free software; you can redistribute it and/or 6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published 7 * modify it under the terms of the GNU General Public License as published
@@ -87,7 +87,7 @@ static inline struct timespec ntfs2utc(const sle64 time)
87 struct timespec ts; 87 struct timespec ts;
88 88
89 /* Subtract the NTFS time offset. */ 89 /* Subtract the NTFS time offset. */
90 s64 t = sle64_to_cpu(time) - NTFS_TIME_OFFSET; 90 u64 t = (u64)(sle64_to_cpu(time) - NTFS_TIME_OFFSET);
91 /* 91 /*
92 * Convert the time to 1-second intervals and the remainder to 92 * Convert the time to 1-second intervals and the remainder to
93 * 1-nano-second intervals. 93 * 1-nano-second intervals.
diff --git a/fs/ntfs/types.h b/fs/ntfs/types.h
index 08a55aa53d4e..6e4a7e3343f2 100644
--- a/fs/ntfs/types.h
+++ b/fs/ntfs/types.h
@@ -2,7 +2,7 @@
2 * types.h - Defines for NTFS Linux kernel driver specific types. 2 * types.h - Defines for NTFS Linux kernel driver specific types.
3 * Part of the Linux-NTFS project. 3 * Part of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published 8 * modify it under the terms of the GNU General Public License as published
@@ -53,6 +53,14 @@ typedef sle64 leLCN;
53typedef s64 LSN; 53typedef s64 LSN;
54typedef sle64 leLSN; 54typedef sle64 leLSN;
55 55
56/*
57 * The NTFS transaction log $UsnJrnl uses usn which are signed 64-bit values.
58 * We define our own type USN, to allow for type checking and better code
59 * readability.
60 */
61typedef s64 USN;
62typedef sle64 leUSN;
63
56typedef enum { 64typedef enum {
57 FALSE = 0, 65 FALSE = 0,
58 TRUE = 1 66 TRUE = 1
diff --git a/fs/ntfs/unistr.c b/fs/ntfs/unistr.c
index 560b0ea255b0..a389a5a16c84 100644
--- a/fs/ntfs/unistr.c
+++ b/fs/ntfs/unistr.c
@@ -264,7 +264,7 @@ int ntfs_nlstoucs(const ntfs_volume *vol, const char *ins,
264 264
265 /* We don't trust outside sources. */ 265 /* We don't trust outside sources. */
266 if (ins) { 266 if (ins) {
267 ucs = (ntfschar*)kmem_cache_alloc(ntfs_name_cache, SLAB_NOFS); 267 ucs = kmem_cache_alloc(ntfs_name_cache, SLAB_NOFS);
268 if (ucs) { 268 if (ucs) {
269 for (i = o = 0; i < ins_len; i += wc_len) { 269 for (i = o = 0; i < ins_len; i += wc_len) {
270 wc_len = nls->char2uni(ins + i, ins_len - i, 270 wc_len = nls->char2uni(ins + i, ins_len - i,
@@ -372,7 +372,8 @@ retry: wc = nls->uni2char(le16_to_cpu(ins[i]), ns + o,
372 return -EINVAL; 372 return -EINVAL;
373conversion_err: 373conversion_err:
374 ntfs_error(vol->sb, "Unicode name contains characters that cannot be " 374 ntfs_error(vol->sb, "Unicode name contains characters that cannot be "
375 "converted to character set %s.", nls->charset); 375 "converted to character set %s. You might want to "
376 "try to use the mount option nls=utf8.", nls->charset);
376 if (ns != *outs) 377 if (ns != *outs)
377 kfree(ns); 378 kfree(ns);
378 if (wc != -ENAMETOOLONG) 379 if (wc != -ENAMETOOLONG)
diff --git a/fs/ntfs/usnjrnl.c b/fs/ntfs/usnjrnl.c
new file mode 100644
index 000000000000..77773240d139
--- /dev/null
+++ b/fs/ntfs/usnjrnl.c
@@ -0,0 +1,84 @@
1/*
2 * usnjrnl.h - NTFS kernel transaction log ($UsnJrnl) handling. Part of the
3 * Linux-NTFS project.
4 *
5 * Copyright (c) 2005 Anton Altaparmakov
6 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifdef NTFS_RW
24
25#include <linux/fs.h>
26#include <linux/highmem.h>
27#include <linux/mm.h>
28
29#include "aops.h"
30#include "debug.h"
31#include "endian.h"
32#include "time.h"
33#include "types.h"
34#include "usnjrnl.h"
35#include "volume.h"
36
37/**
38 * ntfs_stamp_usnjrnl - stamp the transaction log ($UsnJrnl) on an ntfs volume
39 * @vol: ntfs volume on which to stamp the transaction log
40 *
41 * Stamp the transaction log ($UsnJrnl) on the ntfs volume @vol and return
42 * TRUE on success and FALSE on error.
43 *
44 * This function assumes that the transaction log has already been loaded and
45 * consistency checked by a call to fs/ntfs/super.c::load_and_init_usnjrnl().
46 */
47BOOL ntfs_stamp_usnjrnl(ntfs_volume *vol)
48{
49 ntfs_debug("Entering.");
50 if (likely(!NVolUsnJrnlStamped(vol))) {
51 sle64 stamp;
52 struct page *page;
53 USN_HEADER *uh;
54
55 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
56 if (IS_ERR(page)) {
57 ntfs_error(vol->sb, "Failed to read from "
58 "$UsnJrnl/$DATA/$Max attribute.");
59 return FALSE;
60 }
61 uh = (USN_HEADER*)page_address(page);
62 stamp = get_current_ntfs_time();
63 ntfs_debug("Stamping transaction log ($UsnJrnl): old "
64 "journal_id 0x%llx, old lowest_valid_usn "
65 "0x%llx, new journal_id 0x%llx, new "
66 "lowest_valid_usn 0x%llx.",
67 (long long)sle64_to_cpu(uh->journal_id),
68 (long long)sle64_to_cpu(uh->lowest_valid_usn),
69 (long long)sle64_to_cpu(stamp),
70 i_size_read(vol->usnjrnl_j_ino));
71 uh->lowest_valid_usn =
72 cpu_to_sle64(i_size_read(vol->usnjrnl_j_ino));
73 uh->journal_id = stamp;
74 flush_dcache_page(page);
75 set_page_dirty(page);
76 ntfs_unmap_page(page);
77 /* Set the flag so we do not have to do it again on remount. */
78 NVolSetUsnJrnlStamped(vol);
79 }
80 ntfs_debug("Done.");
81 return TRUE;
82}
83
84#endif /* NTFS_RW */
diff --git a/fs/ntfs/usnjrnl.h b/fs/ntfs/usnjrnl.h
new file mode 100644
index 000000000000..ff988b0deb45
--- /dev/null
+++ b/fs/ntfs/usnjrnl.h
@@ -0,0 +1,205 @@
1/*
2 * usnjrnl.h - Defines for NTFS kernel transaction log ($UsnJrnl) handling.
3 * Part of the Linux-NTFS project.
4 *
5 * Copyright (c) 2005 Anton Altaparmakov
6 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifndef _LINUX_NTFS_USNJRNL_H
24#define _LINUX_NTFS_USNJRNL_H
25
26#ifdef NTFS_RW
27
28#include "types.h"
29#include "endian.h"
30#include "layout.h"
31#include "volume.h"
32
33/*
34 * Transaction log ($UsnJrnl) organization:
35 *
36 * The transaction log records whenever a file is modified in any way. So for
37 * example it will record that file "blah" was written to at a particular time
38 * but not what was written. If will record that a file was deleted or
39 * created, that a file was truncated, etc. See below for all the reason
40 * codes used.
41 *
42 * The transaction log is in the $Extend directory which is in the root
43 * directory of each volume. If it is not present it means transaction
44 * logging is disabled. If it is present it means transaction logging is
45 * either enabled or in the process of being disabled in which case we can
46 * ignore it as it will go away as soon as Windows gets its hands on it.
47 *
48 * To determine whether the transaction logging is enabled or in the process
49 * of being disabled, need to check the volume flags in the
50 * $VOLUME_INFORMATION attribute in the $Volume system file (which is present
51 * in the root directory and has a fixed mft record number, see layout.h).
52 * If the flag VOLUME_DELETE_USN_UNDERWAY is set it means the transaction log
53 * is in the process of being disabled and if this flag is clear it means the
54 * transaction log is enabled.
55 *
56 * The transaction log consists of two parts; the $DATA/$Max attribute as well
57 * as the $DATA/$J attribute. $Max is a header describing the transaction
58 * log whilst $J is the transaction log data itself as a sequence of variable
59 * sized USN_RECORDs (see below for all the structures).
60 *
61 * We do not care about transaction logging at this point in time but we still
62 * need to let windows know that the transaction log is out of date. To do
63 * this we need to stamp the transaction log. This involves setting the
64 * lowest_valid_usn field in the $DATA/$Max attribute to the usn to be used
65 * for the next added USN_RECORD to the $DATA/$J attribute as well as
66 * generating a new journal_id in $DATA/$Max.
67 *
68 * The journal_id is as of the current version (2.0) of the transaction log
69 * simply the 64-bit timestamp of when the journal was either created or last
70 * stamped.
71 *
72 * To determine the next usn there are two ways. The first is to parse
73 * $DATA/$J and to find the last USN_RECORD in it and to add its record_length
74 * to its usn (which is the byte offset in the $DATA/$J attribute). The
75 * second is simply to take the data size of the attribute. Since the usns
76 * are simply byte offsets into $DATA/$J, this is exactly the next usn. For
77 * obvious reasons we use the second method as it is much simpler and faster.
78 *
79 * As an aside, note that to actually disable the transaction log, one would
80 * need to set the VOLUME_DELETE_USN_UNDERWAY flag (see above), then go
81 * through all the mft records on the volume and set the usn field in their
82 * $STANDARD_INFORMATION attribute to zero. Once that is done, one would need
83 * to delete the transaction log file, i.e. \$Extent\$UsnJrnl, and finally,
84 * one would need to clear the VOLUME_DELETE_USN_UNDERWAY flag.
85 *
86 * Note that if a volume is unmounted whilst the transaction log is being
87 * disabled, the process will continue the next time the volume is mounted.
88 * This is why we can safely mount read-write when we see a transaction log
89 * in the process of being deleted.
90 */
91
92/* Some $UsnJrnl related constants. */
93#define UsnJrnlMajorVer 2
94#define UsnJrnlMinorVer 0
95
96/*
97 * $DATA/$Max attribute. This is (always?) resident and has a fixed size of
98 * 32 bytes. It contains the header describing the transaction log.
99 */
100typedef struct {
101/*Ofs*/
102/* 0*/sle64 maximum_size; /* The maximum on-disk size of the $DATA/$J
103 attribute. */
104/* 8*/sle64 allocation_delta; /* Number of bytes by which to increase the
105 size of the $DATA/$J attribute. */
106/*0x10*/sle64 journal_id; /* Current id of the transaction log. */
107/*0x18*/leUSN lowest_valid_usn; /* Lowest valid usn in $DATA/$J for the
108 current journal_id. */
109/* sizeof() = 32 (0x20) bytes */
110} __attribute__ ((__packed__)) USN_HEADER;
111
112/*
113 * Reason flags (32-bit). Cumulative flags describing the change(s) to the
114 * file since it was last opened. I think the names speak for themselves but
115 * if you disagree check out the descriptions in the Linux NTFS project NTFS
116 * documentation: http://linux-ntfs.sourceforge.net/ntfs/files/usnjrnl.html
117 */
118enum {
119 USN_REASON_DATA_OVERWRITE = const_cpu_to_le32(0x00000001),
120 USN_REASON_DATA_EXTEND = const_cpu_to_le32(0x00000002),
121 USN_REASON_DATA_TRUNCATION = const_cpu_to_le32(0x00000004),
122 USN_REASON_NAMED_DATA_OVERWRITE = const_cpu_to_le32(0x00000010),
123 USN_REASON_NAMED_DATA_EXTEND = const_cpu_to_le32(0x00000020),
124 USN_REASON_NAMED_DATA_TRUNCATION= const_cpu_to_le32(0x00000040),
125 USN_REASON_FILE_CREATE = const_cpu_to_le32(0x00000100),
126 USN_REASON_FILE_DELETE = const_cpu_to_le32(0x00000200),
127 USN_REASON_EA_CHANGE = const_cpu_to_le32(0x00000400),
128 USN_REASON_SECURITY_CHANGE = const_cpu_to_le32(0x00000800),
129 USN_REASON_RENAME_OLD_NAME = const_cpu_to_le32(0x00001000),
130 USN_REASON_RENAME_NEW_NAME = const_cpu_to_le32(0x00002000),
131 USN_REASON_INDEXABLE_CHANGE = const_cpu_to_le32(0x00004000),
132 USN_REASON_BASIC_INFO_CHANGE = const_cpu_to_le32(0x00008000),
133 USN_REASON_HARD_LINK_CHANGE = const_cpu_to_le32(0x00010000),
134 USN_REASON_COMPRESSION_CHANGE = const_cpu_to_le32(0x00020000),
135 USN_REASON_ENCRYPTION_CHANGE = const_cpu_to_le32(0x00040000),
136 USN_REASON_OBJECT_ID_CHANGE = const_cpu_to_le32(0x00080000),
137 USN_REASON_REPARSE_POINT_CHANGE = const_cpu_to_le32(0x00100000),
138 USN_REASON_STREAM_CHANGE = const_cpu_to_le32(0x00200000),
139 USN_REASON_CLOSE = const_cpu_to_le32(0x80000000),
140};
141
142typedef le32 USN_REASON_FLAGS;
143
144/*
145 * Source info flags (32-bit). Information about the source of the change(s)
146 * to the file. For detailed descriptions of what these mean, see the Linux
147 * NTFS project NTFS documentation:
148 * http://linux-ntfs.sourceforge.net/ntfs/files/usnjrnl.html
149 */
150enum {
151 USN_SOURCE_DATA_MANAGEMENT = const_cpu_to_le32(0x00000001),
152 USN_SOURCE_AUXILIARY_DATA = const_cpu_to_le32(0x00000002),
153 USN_SOURCE_REPLICATION_MANAGEMENT = const_cpu_to_le32(0x00000004),
154};
155
156typedef le32 USN_SOURCE_INFO_FLAGS;
157
158/*
159 * $DATA/$J attribute. This is always non-resident, is marked as sparse, and
160 * is of variabled size. It consists of a sequence of variable size
161 * USN_RECORDS. The minimum allocated_size is allocation_delta as
162 * specified in $DATA/$Max. When the maximum_size specified in $DATA/$Max is
163 * exceeded by more than allocation_delta bytes, allocation_delta bytes are
164 * allocated and appended to the $DATA/$J attribute and an equal number of
165 * bytes at the beginning of the attribute are freed and made sparse. Note the
166 * making sparse only happens at volume checkpoints and hence the actual
167 * $DATA/$J size can exceed maximum_size + allocation_delta temporarily.
168 */
169typedef struct {
170/*Ofs*/
171/* 0*/le32 length; /* Byte size of this record (8-byte
172 aligned). */
173/* 4*/le16 major_ver; /* Major version of the transaction log used
174 for this record. */
175/* 6*/le16 minor_ver; /* Minor version of the transaction log used
176 for this record. */
177/* 8*/leMFT_REF mft_reference;/* The mft reference of the file (or
178 directory) described by this record. */
179/*0x10*/leMFT_REF parent_directory;/* The mft reference of the parent
180 directory of the file described by this
181 record. */
182/*0x18*/leUSN usn; /* The usn of this record. Equals the offset
183 within the $DATA/$J attribute. */
184/*0x20*/sle64 time; /* Time when this record was created. */
185/*0x28*/USN_REASON_FLAGS reason;/* Reason flags (see above). */
186/*0x2c*/USN_SOURCE_INFO_FLAGS source_info;/* Source info flags (see above). */
187/*0x30*/le32 security_id; /* File security_id copied from
188 $STANDARD_INFORMATION. */
189/*0x34*/FILE_ATTR_FLAGS file_attributes; /* File attributes copied from
190 $STANDARD_INFORMATION or $FILE_NAME (not
191 sure which). */
192/*0x38*/le16 file_name_size; /* Size of the file name in bytes. */
193/*0x3a*/le16 file_name_offset; /* Offset to the file name in bytes from the
194 start of this record. */
195/*0x3c*/ntfschar file_name[0]; /* Use when creating only. When reading use
196 file_name_offset to determine the location
197 of the name. */
198/* sizeof() = 60 (0x3c) bytes */
199} __attribute__ ((__packed__)) USN_RECORD;
200
201extern BOOL ntfs_stamp_usnjrnl(ntfs_volume *vol);
202
203#endif /* NTFS_RW */
204
205#endif /* _LINUX_NTFS_USNJRNL_H */
diff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h
index 4b97fa8635a8..375cd20a9f61 100644
--- a/fs/ntfs/volume.h
+++ b/fs/ntfs/volume.h
@@ -2,7 +2,7 @@
2 * volume.h - Defines for volume structures in NTFS Linux kernel driver. Part 2 * volume.h - Defines for volume structures in NTFS Linux kernel driver. Part
3 * of the Linux-NTFS project. 3 * of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon 6 * Copyright (c) 2002 Richard Russon
7 * 7 *
8 * This program/include file is free software; you can redistribute it and/or 8 * This program/include file is free software; you can redistribute it and/or
@@ -54,7 +54,7 @@ typedef struct {
54 mode_t dmask; /* The mask for directory 54 mode_t dmask; /* The mask for directory
55 permissions. */ 55 permissions. */
56 u8 mft_zone_multiplier; /* Initial mft zone multiplier. */ 56 u8 mft_zone_multiplier; /* Initial mft zone multiplier. */
57 u8 on_errors; /* What to do on file system errors. */ 57 u8 on_errors; /* What to do on filesystem errors. */
58 /* NTFS bootsector provided information. */ 58 /* NTFS bootsector provided information. */
59 u16 sector_size; /* in bytes */ 59 u16 sector_size; /* in bytes */
60 u8 sector_size_bits; /* log2(sector_size) */ 60 u8 sector_size_bits; /* log2(sector_size) */
@@ -125,6 +125,10 @@ typedef struct {
125 /* $Quota stuff is NTFS3.0+ specific. Unused/NULL otherwise. */ 125 /* $Quota stuff is NTFS3.0+ specific. Unused/NULL otherwise. */
126 struct inode *quota_ino; /* The VFS inode of $Quota. */ 126 struct inode *quota_ino; /* The VFS inode of $Quota. */
127 struct inode *quota_q_ino; /* Attribute inode for $Quota/$Q. */ 127 struct inode *quota_q_ino; /* Attribute inode for $Quota/$Q. */
128 /* $UsnJrnl stuff is NTFS3.0+ specific. Unused/NULL otherwise. */
129 struct inode *usnjrnl_ino; /* The VFS inode of $UsnJrnl. */
130 struct inode *usnjrnl_max_ino; /* Attribute inode for $UsnJrnl/$Max. */
131 struct inode *usnjrnl_j_ino; /* Attribute inode for $UsnJrnl/$J. */
128#endif /* NTFS_RW */ 132#endif /* NTFS_RW */
129 struct nls_table *nls_map; 133 struct nls_table *nls_map;
130} ntfs_volume; 134} ntfs_volume;
@@ -141,6 +145,8 @@ typedef enum {
141 file names in WIN32 namespace. */ 145 file names in WIN32 namespace. */
142 NV_LogFileEmpty, /* 1: $LogFile journal is empty. */ 146 NV_LogFileEmpty, /* 1: $LogFile journal is empty. */
143 NV_QuotaOutOfDate, /* 1: $Quota is out of date. */ 147 NV_QuotaOutOfDate, /* 1: $Quota is out of date. */
148 NV_UsnJrnlStamped, /* 1: $UsnJrnl has been stamped. */
149 NV_SparseEnabled, /* 1: May create sparse files. */
144} ntfs_volume_flags; 150} ntfs_volume_flags;
145 151
146/* 152/*
@@ -167,5 +173,7 @@ NVOL_FNS(ShowSystemFiles)
167NVOL_FNS(CaseSensitive) 173NVOL_FNS(CaseSensitive)
168NVOL_FNS(LogFileEmpty) 174NVOL_FNS(LogFileEmpty)
169NVOL_FNS(QuotaOutOfDate) 175NVOL_FNS(QuotaOutOfDate)
176NVOL_FNS(UsnJrnlStamped)
177NVOL_FNS(SparseEnabled)
170 178
171#endif /* _LINUX_NTFS_VOLUME_H */ 179#endif /* _LINUX_NTFS_VOLUME_H */