aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/ntfs/ChangeLog120
-rw-r--r--fs/ntfs/Makefile2
-rw-r--r--fs/ntfs/aops.c150
-rw-r--r--fs/ntfs/attrib.c573
-rw-r--r--fs/ntfs/attrib.h16
-rw-r--r--fs/ntfs/compress.c46
-rw-r--r--fs/ntfs/debug.c15
-rw-r--r--fs/ntfs/dir.c29
-rw-r--r--fs/ntfs/file.c2
-rw-r--r--fs/ntfs/index.c16
-rw-r--r--fs/ntfs/inode.c530
-rw-r--r--fs/ntfs/inode.h5
-rw-r--r--fs/ntfs/layout.h61
-rw-r--r--fs/ntfs/lcnalloc.c68
-rw-r--r--fs/ntfs/logfile.c5
-rw-r--r--fs/ntfs/mft.c186
-rw-r--r--fs/ntfs/namei.c34
-rw-r--r--fs/ntfs/ntfs.h8
-rw-r--r--fs/ntfs/runlist.c111
-rw-r--r--fs/ntfs/runlist.h11
-rw-r--r--fs/ntfs/super.c259
-rw-r--r--fs/ntfs/sysctl.c4
-rw-r--r--fs/ntfs/time.h4
-rw-r--r--fs/ntfs/unistr.c2
-rw-r--r--fs/ntfs/volume.h6
25 files changed, 1496 insertions, 767 deletions
diff --git a/fs/ntfs/ChangeLog b/fs/ntfs/ChangeLog
index 1d2ad15f1533..cb86140aa2a3 100644
--- a/fs/ntfs/ChangeLog
+++ b/fs/ntfs/ChangeLog
@@ -2,20 +2,18 @@ ToDo/Notes:
2 - Find and fix bugs. 2 - Find and fix bugs.
3 - Checkpoint or disable the user space journal ($UsnJrnl). 3 - Checkpoint or disable the user space journal ($UsnJrnl).
4 - In between ntfs_prepare/commit_write, need exclusion between 4 - In between ntfs_prepare/commit_write, need exclusion between
5 simultaneous file extensions. Need perhaps an NInoResizeUnderway() 5 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 6 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, 7 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 8 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 9 helpers not running under i_sem that we play nice...
10 length. Also need to be careful with initialized_size extention in 10 Also need to be careful with initialized_size extention in
11 ntfs_prepare_write. Basically, just be _very_ careful in this code... 11 ntfs_prepare_write. Basically, just be _very_ careful in this code...
12 OTOH, perhaps i_sem, which is held accross generic_file_write is 12 UPDATE: The only things that need to be checked are read/writepage
13 sufficient for synchronisation here. We then just need to make sure 13 which do not hold i_sem. Note writepage cannot change i_size but it
14 ntfs_readpage/writepage/truncate interoperate properly with us. 14 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 15 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 16 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 17 - 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) 18 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 19 causes a write of any mirrored mft records due to the mft mirror
@@ -31,6 +29,96 @@ ToDo/Notes:
31 compiled without debug. This avoids a possible denial of service 29 compiled without debug. This avoids a possible denial of service
32 attack. Thanks to Carl-Daniel Hailfinger from SuSE for pointing this 30 attack. Thanks to Carl-Daniel Hailfinger from SuSE for pointing this
33 out. 31 out.
32 - Fix compilation warnings on ia64. (Randy Dunlap)
33 - Use i_size_read() in fs/ntfs/attrib.c::ntfs_attr_set().
34 - Use i_size_read() in fs/ntfs/logfile.c::ntfs_{check,empty}_logfile().
35 - Use i_size_read() once and then use the cached value in
36 fs/ntfs/lcnalloc.c::ntfs_cluster_alloc().
37 - Use i_size_read() in fs/ntfs/file.c::ntfs_file_open().
38 - Add size_lock to the ntfs_inode structure. This is an rw spinlock
39 and it locks against access to the inode sizes. Note, ->size_lock
40 is also accessed from irq context so you must use the _irqsave and
41 _irqrestore lock and unlock functions, respectively.
42 - Use i_size_read() in fs/ntfs/compress.c at the start of the read and
43 use the cached value afterwards. Cache the initialized_size in the
44 same way and protect access to the two sizes using the size_lock.
45 - Use i_size_read() in fs/ntfs/dir.c once and then use the cached
46 value afterwards.
47 - Use i_size_read() in fs/ntfs/super.c once and then use the cached
48 value afterwards. Cache the initialized_size in the same way and
49 protect access to the two sizes using the size_lock.
50 - Minor optimization to fs/ntfs/super.c::ntfs_statfs() and its helpers.
51 - Use i_size_read() in fs/ntfs/inode.c once and then use the cached
52 value afterwards when reading the size of the bitmap inode.
53 - Use i_size_{read,write}() in fs/ntfs/{aops.c,mft.c} and protect
54 access to the i_size and other size fields using the size_lock.
55 - Implement extension of resident files in the regular file write code
56 paths (fs/ntfs/aops.c::ntfs_{prepare,commit}_write()). At present
57 this only works until the data attribute becomes too big for the mft
58 record after which we abort the write returning -EOPNOTSUPP from
59 ntfs_prepare_write().
60 - Add disable_sparse mount option together with a per volume sparse
61 enable bit which is set appropriately and a per inode sparse disable
62 bit which is preset on some system file inodes as appropriate.
63 - Enforce that sparse support is disabled on NTFS volumes pre 3.0.
64 - Fix a bug in fs/ntfs/runlist.c::ntfs_mapping_pairs_decompress() in
65 the creation of the unmapped runlist element for the base attribute
66 extent.
67 - Split ntfs_map_runlist() into ntfs_map_runlist() and a non-locking
68 helper ntfs_map_runlist_nolock() which is used by ntfs_map_runlist().
69 This allows us to map runlist fragments with the runlist lock already
70 held without having to drop and reacquire it around the call. Adapt
71 all callers.
72 - Change ntfs_find_vcn() to ntfs_find_vcn_nolock() which takes a locked
73 runlist. This allows us to find runlist elements with the runlist
74 lock already held without having to drop and reacquire it around the
75 call. Adapt all callers.
76 - Change time to u64 in time.h::ntfs2utc() as it otherwise generates a
77 warning in the do_div() call on sparc32. Thanks to Meelis Roos for
78 the report and analysis of the warning.
79 - Fix a nasty runlist merge bug when merging two holes.
80 - Set the ntfs_inode->allocated_size to the real allocated size in the
81 mft record for resident attributes (fs/ntfs/inode.c).
82 - Small readability cleanup to use "a" instead of "ctx->attr"
83 everywhere (fs/ntfs/inode.c).
84 - Make fs/ntfs/namei.c::ntfs_get_{parent,dentry} static and move the
85 definition of ntfs_export_ops from fs/ntfs/super.c to namei.c. Also,
86 declare ntfs_export_ops in fs/ntfs/ntfs.h.
87 - Correct sparse file handling. The compressed values need to be
88 checked and set in the ntfs inode as done for compressed files and
89 the compressed size needs to be used for vfs inode->i_blocks instead
90 of the allocated size, again, as done for compressed files.
91 - Add AT_EA in addition to AT_DATA to whitelist for being allowed to be
92 non-resident in fs/ntfs/attrib.c::ntfs_attr_can_be_non_resident().
93 - Add fs/ntfs/attrib.c::ntfs_attr_vcn_to_lcn_nolock() used by the new
94 write code.
95 - Fix bug in fs/ntfs/attrib.c::ntfs_find_vcn_nolock() where after
96 dropping the read lock and taking the write lock we were not checking
97 whether someone else did not already do the work we wanted to do.
98 - Rename fs/ntfs/attrib.c::ntfs_find_vcn_nolock() to
99 ntfs_attr_find_vcn_nolock() and update all callers.
100 - Add fs/ntfs/attrib.[hc]::ntfs_attr_make_non_resident().
101 - Fix sign of various error return values to be negative in
102 fs/ntfs/lcnalloc.c.
103 - Modify ->readpage and ->writepage (fs/ntfs/aops.c) so they detect and
104 handle the case where an attribute is converted from resident to
105 non-resident by a concurrent file write.
106 - Remove checks for NULL before calling kfree() since kfree() does the
107 checking itself. (Jesper Juhl)
108 - Some utilities modify the boot sector but do not update the checksum.
109 Thus, relax the checking in fs/ntfs/super.c::is_boot_sector_ntfs() to
110 only emit a warning when the checksum is incorrect rather than
111 refusing the mount. Thanks to Bernd Casimir for pointing this
112 problem out.
113 - Update attribute definition handling.
114 - Add NTFS_MAX_CLUSTER_SIZE and NTFS_MAX_PAGES_PER_CLUSTER constants.
115 - Use NTFS_MAX_CLUSTER_SIZE in super.c instead of hard coding 0x10000.
116 - Use MAX_BUF_PER_PAGE instead of variable sized array allocation for
117 better code generation and one less sparse warning in fs/ntfs/aops.c.
118 - Remove spurious void pointer casts from fs/ntfs/. (Pekka Enberg)
119 - Use C99 style structure initialization after memory allocation where
120 possible (fs/ntfs/{attrib.c,index.c,super.c}). Thanks to Al Viro and
121 Pekka Enberg.
34 122
352.1.22 - Many bug and race fixes and error handling improvements. 1232.1.22 - Many bug and race fixes and error handling improvements.
36 124
@@ -1037,7 +1125,7 @@ tng-0.0.8 - 08/03/2002 - Now using BitKeeper, http://linux-ntfs.bkbits.net/
1037 - Further runlist merging work. (Richard Russon) 1125 - Further runlist merging work. (Richard Russon)
1038 - Backwards compatibility for gcc-2.95. (Richard Russon) 1126 - Backwards compatibility for gcc-2.95. (Richard Russon)
1039 - Update to kernel 2.5.5-pre1 and rediff the now tiny patch. 1127 - 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 1128 - Convert to new filesystem declaration using ->ntfs_get_sb() and
1041 replacing ntfs_read_super() with ntfs_fill_super(). 1129 replacing ntfs_read_super() with ntfs_fill_super().
1042 - Set s_maxbytes to MAX_LFS_FILESIZE to avoid page cache page index 1130 - Set s_maxbytes to MAX_LFS_FILESIZE to avoid page cache page index
1043 overflow on 32-bit architectures. 1131 overflow on 32-bit architectures.
@@ -1333,7 +1421,7 @@ tng-0.0.1 - The first useful version.
1333 The driver is now actually useful! Yey. (-: It undoubtedly has got bugs 1421 The driver is now actually useful! Yey. (-: It undoubtedly has got bugs
1334 though and it doesn't implement accesssing compressed files yet. Also, 1422 though and it doesn't implement accesssing compressed files yet. Also,
1335 accessing files with attribute list attributes is not implemented yet 1423 accessing files with attribute list attributes is not implemented yet
1336 either. But for small or simple file systems it should work and allow 1424 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 1425 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 1426 system, open, read, mmap and llseek around in files. A big mile stone
1339 has been reached! 1427 has been reached!
@@ -1341,7 +1429,7 @@ tng-0.0.1 - The first useful version.
1341tng-0.0.0 - Initial version tag. 1429tng-0.0.0 - Initial version tag.
1342 1430
1343 Initial driver implementation. The driver can mount and umount simple 1431 Initial driver implementation. The driver can mount and umount simple
1344 NTFS file systems (i.e. ones without attribute lists in the system 1432 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 1433 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 1434 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 1435 files nicely and the mft record read mapping/unmapping seems to be
diff --git a/fs/ntfs/Makefile b/fs/ntfs/Makefile
index 7b66381a0b0f..f8c97d41226b 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.23-WIP\"
10 10
11ifeq ($(CONFIG_NTFS_DEBUG),y) 11ifeq ($(CONFIG_NTFS_DEBUG),y)
12EXTRA_CFLAGS += -DDEBUG 12EXTRA_CFLAGS += -DDEBUG
diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
index 45d56e41ed98..24c46c200337 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
@@ -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);
@@ -132,7 +135,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
132 i * rec_size), rec_size); 135 i * rec_size), rec_size);
133 flush_dcache_page(page); 136 flush_dcache_page(page);
134 kunmap_atomic(addr, KM_BIO_SRC_IRQ); 137 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
135 if (likely(!PageError(page) && page_uptodate)) 138 if (likely(page_uptodate && !PageError(page)))
136 SetPageUptodate(page); 139 SetPageUptodate(page);
137 } 140 }
138 unlock_page(page); 141 unlock_page(page);
@@ -168,6 +171,7 @@ static int ntfs_read_block(struct page *page)
168 runlist_element *rl; 171 runlist_element *rl;
169 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; 172 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
170 sector_t iblock, lblock, zblock; 173 sector_t iblock, lblock, zblock;
174 unsigned long flags;
171 unsigned int blocksize, vcn_ofs; 175 unsigned int blocksize, vcn_ofs;
172 int i, nr; 176 int i, nr;
173 unsigned char blocksize_bits; 177 unsigned char blocksize_bits;
@@ -190,8 +194,10 @@ static int ntfs_read_block(struct page *page)
190 } 194 }
191 195
192 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 196 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
197 read_lock_irqsave(&ni->size_lock, flags);
193 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits; 198 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
194 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits; 199 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
200 read_unlock_irqrestore(&ni->size_lock, flags);
195 201
196 /* Loop through all the buffers in the page. */ 202 /* Loop through all the buffers in the page. */
197 rl = NULL; 203 rl = NULL;
@@ -341,14 +347,15 @@ handle_zblock:
341 */ 347 */
342static int ntfs_readpage(struct file *file, struct page *page) 348static int ntfs_readpage(struct file *file, struct page *page)
343{ 349{
344 loff_t i_size;
345 ntfs_inode *ni, *base_ni; 350 ntfs_inode *ni, *base_ni;
346 u8 *kaddr; 351 u8 *kaddr;
347 ntfs_attr_search_ctx *ctx; 352 ntfs_attr_search_ctx *ctx;
348 MFT_RECORD *mrec; 353 MFT_RECORD *mrec;
354 unsigned long flags;
349 u32 attr_len; 355 u32 attr_len;
350 int err = 0; 356 int err = 0;
351 357
358retry_readpage:
352 BUG_ON(!PageLocked(page)); 359 BUG_ON(!PageLocked(page));
353 /* 360 /*
354 * This can potentially happen because we clear PageUptodate() during 361 * This can potentially happen because we clear PageUptodate() during
@@ -383,9 +390,9 @@ static int ntfs_readpage(struct file *file, struct page *page)
383 * Attribute is resident, implying it is not compressed or encrypted. 390 * Attribute is resident, implying it is not compressed or encrypted.
384 * This also means the attribute is smaller than an mft record and 391 * 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 392 * 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. 393 * index above 0.
387 */ 394 */
388 if (unlikely(page->index > 0 || !i_size_read(VFS_I(ni)))) { 395 if (unlikely(page->index > 0)) {
389 kaddr = kmap_atomic(page, KM_USER0); 396 kaddr = kmap_atomic(page, KM_USER0);
390 memset(kaddr, 0, PAGE_CACHE_SIZE); 397 memset(kaddr, 0, PAGE_CACHE_SIZE);
391 flush_dcache_page(page); 398 flush_dcache_page(page);
@@ -402,6 +409,14 @@ static int ntfs_readpage(struct file *file, struct page *page)
402 err = PTR_ERR(mrec); 409 err = PTR_ERR(mrec);
403 goto err_out; 410 goto err_out;
404 } 411 }
412 /*
413 * If a parallel write made the attribute non-resident, drop the mft
414 * record and retry the readpage.
415 */
416 if (unlikely(NInoNonResident(ni))) {
417 unmap_mft_record(base_ni);
418 goto retry_readpage;
419 }
405 ctx = ntfs_attr_get_search_ctx(base_ni, mrec); 420 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
406 if (unlikely(!ctx)) { 421 if (unlikely(!ctx)) {
407 err = -ENOMEM; 422 err = -ENOMEM;
@@ -412,9 +427,10 @@ static int ntfs_readpage(struct file *file, struct page *page)
412 if (unlikely(err)) 427 if (unlikely(err))
413 goto put_unm_err_out; 428 goto put_unm_err_out;
414 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 429 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
415 i_size = i_size_read(VFS_I(ni)); 430 read_lock_irqsave(&ni->size_lock, flags);
416 if (unlikely(attr_len > i_size)) 431 if (unlikely(attr_len > ni->initialized_size))
417 attr_len = i_size; 432 attr_len = ni->initialized_size;
433 read_unlock_irqrestore(&ni->size_lock, flags);
418 kaddr = kmap_atomic(page, KM_USER0); 434 kaddr = kmap_atomic(page, KM_USER0);
419 /* Copy the data to the page. */ 435 /* Copy the data to the page. */
420 memcpy(kaddr, (u8*)ctx->attr + 436 memcpy(kaddr, (u8*)ctx->attr +
@@ -463,12 +479,15 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
463{ 479{
464 VCN vcn; 480 VCN vcn;
465 LCN lcn; 481 LCN lcn;
482 s64 initialized_size;
483 loff_t i_size;
466 sector_t block, dblock, iblock; 484 sector_t block, dblock, iblock;
467 struct inode *vi; 485 struct inode *vi;
468 ntfs_inode *ni; 486 ntfs_inode *ni;
469 ntfs_volume *vol; 487 ntfs_volume *vol;
470 runlist_element *rl; 488 runlist_element *rl;
471 struct buffer_head *bh, *head; 489 struct buffer_head *bh, *head;
490 unsigned long flags;
472 unsigned int blocksize, vcn_ofs; 491 unsigned int blocksize, vcn_ofs;
473 int err; 492 int err;
474 BOOL need_end_writeback; 493 BOOL need_end_writeback;
@@ -510,11 +529,16 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
510 /* The first block in the page. */ 529 /* The first block in the page. */
511 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 530 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
512 531
532 read_lock_irqsave(&ni->size_lock, flags);
533 i_size = i_size_read(vi);
534 initialized_size = ni->initialized_size;
535 read_unlock_irqrestore(&ni->size_lock, flags);
536
513 /* The first out of bounds block for the data size. */ 537 /* The first out of bounds block for the data size. */
514 dblock = (vi->i_size + blocksize - 1) >> blocksize_bits; 538 dblock = (i_size + blocksize - 1) >> blocksize_bits;
515 539
516 /* The last (fully or partially) initialized block. */ 540 /* The last (fully or partially) initialized block. */
517 iblock = ni->initialized_size >> blocksize_bits; 541 iblock = initialized_size >> blocksize_bits;
518 542
519 /* 543 /*
520 * Be very careful. We have no exclusion from __set_page_dirty_buffers 544 * Be very careful. We have no exclusion from __set_page_dirty_buffers
@@ -559,7 +583,7 @@ static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
559 583
560 /* Make sure we have enough initialized size. */ 584 /* Make sure we have enough initialized size. */
561 if (unlikely((block >= iblock) && 585 if (unlikely((block >= iblock) &&
562 (ni->initialized_size < vi->i_size))) { 586 (initialized_size < i_size))) {
563 /* 587 /*
564 * If this page is fully outside initialized size, zero 588 * If this page is fully outside initialized size, zero
565 * out all pages between the current initialized size 589 * out all pages between the current initialized size
@@ -801,17 +825,15 @@ static int ntfs_write_mst_block(struct page *page,
801 ntfs_inode *ni = NTFS_I(vi); 825 ntfs_inode *ni = NTFS_I(vi);
802 ntfs_volume *vol = ni->vol; 826 ntfs_volume *vol = ni->vol;
803 u8 *kaddr; 827 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; 828 unsigned int rec_size = ni->itype.index.block_size;
807 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size]; 829 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
808 struct buffer_head *bh, *head, *tbh, *rec_start_bh; 830 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
809 int max_bhs = PAGE_CACHE_SIZE / bh_size; 831 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
810 struct buffer_head *bhs[max_bhs];
811 runlist_element *rl; 832 runlist_element *rl;
812 int i, nr_locked_nis, nr_recs, nr_bhs, bhs_per_rec, err, err2; 833 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
813 unsigned rec_size_bits; 834 unsigned bh_size, rec_size_bits;
814 BOOL sync, is_mft, page_is_dirty, rec_is_dirty; 835 BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
836 unsigned char bh_size_bits;
815 837
816 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " 838 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
817 "0x%lx.", vi->i_ino, ni->type, page->index); 839 "0x%lx.", vi->i_ino, ni->type, page->index);
@@ -826,7 +848,11 @@ static int ntfs_write_mst_block(struct page *page,
826 */ 848 */
827 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) || 849 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
828 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION))); 850 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
851 bh_size_bits = vi->i_blkbits;
852 bh_size = 1 << bh_size_bits;
853 max_bhs = PAGE_CACHE_SIZE / bh_size;
829 BUG_ON(!max_bhs); 854 BUG_ON(!max_bhs);
855 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
830 856
831 /* Were we called for sync purposes? */ 857 /* Were we called for sync purposes? */
832 sync = (wbc->sync_mode == WB_SYNC_ALL); 858 sync = (wbc->sync_mode == WB_SYNC_ALL);
@@ -846,7 +872,7 @@ static int ntfs_write_mst_block(struct page *page,
846 (PAGE_CACHE_SHIFT - bh_size_bits); 872 (PAGE_CACHE_SHIFT - bh_size_bits);
847 873
848 /* The first out of bounds block for the data size. */ 874 /* The first out of bounds block for the data size. */
849 dblock = (vi->i_size + bh_size - 1) >> bh_size_bits; 875 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
850 876
851 rl = NULL; 877 rl = NULL;
852 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0; 878 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
@@ -858,6 +884,7 @@ static int ntfs_write_mst_block(struct page *page,
858 if (likely(block < rec_block)) { 884 if (likely(block < rec_block)) {
859 if (unlikely(block >= dblock)) { 885 if (unlikely(block >= dblock)) {
860 clear_buffer_dirty(bh); 886 clear_buffer_dirty(bh);
887 set_buffer_uptodate(bh);
861 continue; 888 continue;
862 } 889 }
863 /* 890 /*
@@ -949,7 +976,8 @@ lock_retry_remap:
949 "attribute type 0x%x) because " 976 "attribute type 0x%x) because "
950 "its location on disk could " 977 "its location on disk could "
951 "not be determined (error " 978 "not be determined (error "
952 "code %lli).", (s64)block << 979 "code %lli).",
980 (long long)block <<
953 bh_size_bits >> 981 bh_size_bits >>
954 vol->mft_record_size_bits, 982 vol->mft_record_size_bits,
955 ni->mft_no, ni->type, 983 ni->mft_no, ni->type,
@@ -1223,19 +1251,17 @@ done:
1223static int ntfs_writepage(struct page *page, struct writeback_control *wbc) 1251static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1224{ 1252{
1225 loff_t i_size; 1253 loff_t i_size;
1226 struct inode *vi; 1254 struct inode *vi = page->mapping->host;
1227 ntfs_inode *ni, *base_ni; 1255 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1228 char *kaddr; 1256 char *kaddr;
1229 ntfs_attr_search_ctx *ctx; 1257 ntfs_attr_search_ctx *ctx = NULL;
1230 MFT_RECORD *m; 1258 MFT_RECORD *m = NULL;
1231 u32 attr_len; 1259 u32 attr_len;
1232 int err; 1260 int err;
1233 1261
1262retry_writepage:
1234 BUG_ON(!PageLocked(page)); 1263 BUG_ON(!PageLocked(page));
1235
1236 vi = page->mapping->host;
1237 i_size = i_size_read(vi); 1264 i_size = i_size_read(vi);
1238
1239 /* Is the page fully outside i_size? (truncate in progress) */ 1265 /* Is the page fully outside i_size? (truncate in progress) */
1240 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >> 1266 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1241 PAGE_CACHE_SHIFT)) { 1267 PAGE_CACHE_SHIFT)) {
@@ -1248,8 +1274,6 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1248 ntfs_debug("Write outside i_size - truncated?"); 1274 ntfs_debug("Write outside i_size - truncated?");
1249 return 0; 1275 return 0;
1250 } 1276 }
1251 ni = NTFS_I(vi);
1252
1253 /* NInoNonResident() == NInoIndexAllocPresent() */ 1277 /* NInoNonResident() == NInoIndexAllocPresent() */
1254 if (NInoNonResident(ni)) { 1278 if (NInoNonResident(ni)) {
1255 /* 1279 /*
@@ -1326,6 +1350,14 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1326 ctx = NULL; 1350 ctx = NULL;
1327 goto err_out; 1351 goto err_out;
1328 } 1352 }
1353 /*
1354 * If a parallel write made the attribute non-resident, drop the mft
1355 * record and retry the writepage.
1356 */
1357 if (unlikely(NInoNonResident(ni))) {
1358 unmap_mft_record(base_ni);
1359 goto retry_writepage;
1360 }
1329 ctx = ntfs_attr_get_search_ctx(base_ni, m); 1361 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1330 if (unlikely(!ctx)) { 1362 if (unlikely(!ctx)) {
1331 err = -ENOMEM; 1363 err = -ENOMEM;
@@ -1367,15 +1399,12 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1367 */ 1399 */
1368 1400
1369 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 1401 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1370 i_size = i_size_read(VFS_I(ni)); 1402 i_size = i_size_read(vi);
1371 kaddr = kmap_atomic(page, KM_USER0);
1372 if (unlikely(attr_len > i_size)) { 1403 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; 1404 attr_len = i_size;
1405 ctx->attr->data.resident.value_length = cpu_to_le32(attr_len);
1378 } 1406 }
1407 kaddr = kmap_atomic(page, KM_USER0);
1379 /* Copy the data from the page to the mft record. */ 1408 /* Copy the data from the page to the mft record. */
1380 memcpy((u8*)ctx->attr + 1409 memcpy((u8*)ctx->attr +
1381 le16_to_cpu(ctx->attr->data.resident.value_offset), 1410 le16_to_cpu(ctx->attr->data.resident.value_offset),
@@ -1405,8 +1434,10 @@ err_out:
1405 err = 0; 1434 err = 0;
1406 } else { 1435 } else {
1407 ntfs_error(vi->i_sb, "Resident attribute write failed with " 1436 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1408 "error %i. Setting page error flag.", err); 1437 "error %i.", err);
1409 SetPageError(page); 1438 SetPageError(page);
1439 NVolSetErrors(ni->vol);
1440 make_bad_inode(vi);
1410 } 1441 }
1411 unlock_page(page); 1442 unlock_page(page);
1412 if (ctx) 1443 if (ctx)
@@ -1425,12 +1456,15 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1425{ 1456{
1426 VCN vcn; 1457 VCN vcn;
1427 LCN lcn; 1458 LCN lcn;
1459 s64 initialized_size;
1460 loff_t i_size;
1428 sector_t block, ablock, iblock; 1461 sector_t block, ablock, iblock;
1429 struct inode *vi; 1462 struct inode *vi;
1430 ntfs_inode *ni; 1463 ntfs_inode *ni;
1431 ntfs_volume *vol; 1464 ntfs_volume *vol;
1432 runlist_element *rl; 1465 runlist_element *rl;
1433 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait; 1466 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1467 unsigned long flags;
1434 unsigned int vcn_ofs, block_start, block_end, blocksize; 1468 unsigned int vcn_ofs, block_start, block_end, blocksize;
1435 int err; 1469 int err;
1436 BOOL is_retry; 1470 BOOL is_retry;
@@ -1462,16 +1496,20 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1462 /* The first block in the page. */ 1496 /* The first block in the page. */
1463 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); 1497 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1464 1498
1499 read_lock_irqsave(&ni->size_lock, flags);
1465 /* 1500 /*
1466 * The first out of bounds block for the allocated size. No need to 1501 * 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 1502 * 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 1503 * minimum cluster size is 512 bytes, which is equal to the smallest
1469 * blocksize. 1504 * blocksize.
1470 */ 1505 */
1471 ablock = ni->allocated_size >> blocksize_bits; 1506 ablock = ni->allocated_size >> blocksize_bits;
1507 i_size = i_size_read(vi);
1508 initialized_size = ni->initialized_size;
1509 read_unlock_irqrestore(&ni->size_lock, flags);
1472 1510
1473 /* The last (fully or partially) initialized block. */ 1511 /* The last (fully or partially) initialized block. */
1474 iblock = ni->initialized_size >> blocksize_bits; 1512 iblock = initialized_size >> blocksize_bits;
1475 1513
1476 /* Loop through all the buffers in the page. */ 1514 /* Loop through all the buffers in the page. */
1477 block_start = 0; 1515 block_start = 0;
@@ -1518,7 +1556,7 @@ static int ntfs_prepare_nonresident_write(struct page *page,
1518 * request, i.e. block < ablock is true. 1556 * request, i.e. block < ablock is true.
1519 */ 1557 */
1520 if (unlikely((block >= iblock) && 1558 if (unlikely((block >= iblock) &&
1521 (ni->initialized_size < vi->i_size))) { 1559 (initialized_size < i_size))) {
1522 /* 1560 /*
1523 * If this page is fully outside initialized size, zero 1561 * If this page is fully outside initialized size, zero
1524 * out all pages between the current initialized size 1562 * out all pages between the current initialized size
@@ -1797,6 +1835,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1797 unsigned from, unsigned to) 1835 unsigned from, unsigned to)
1798{ 1836{
1799 s64 new_size; 1837 s64 new_size;
1838 loff_t i_size;
1800 struct inode *vi = page->mapping->host; 1839 struct inode *vi = page->mapping->host;
1801 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); 1840 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1802 ntfs_volume *vol = ni->vol; 1841 ntfs_volume *vol = ni->vol;
@@ -1868,14 +1907,8 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1868 BUG_ON(page_has_buffers(page)); 1907 BUG_ON(page_has_buffers(page));
1869 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to; 1908 new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1870 /* If we do not need to resize the attribute allocation we are done. */ 1909 /* If we do not need to resize the attribute allocation we are done. */
1871 if (new_size <= vi->i_size) 1910 if (new_size <= i_size_read(vi))
1872 goto done; 1911 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. */ 1912 /* Map, pin, and lock the (base) mft record. */
1880 if (!NInoAttr(ni)) 1913 if (!NInoAttr(ni))
1881 base_ni = ni; 1914 base_ni = ni;
@@ -1904,7 +1937,15 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1904 a = ctx->attr; 1937 a = ctx->attr;
1905 /* The total length of the attribute value. */ 1938 /* The total length of the attribute value. */
1906 attr_len = le32_to_cpu(a->data.resident.value_length); 1939 attr_len = le32_to_cpu(a->data.resident.value_length);
1907 BUG_ON(vi->i_size != attr_len); 1940 /* Fix an eventual previous failure of ntfs_commit_write(). */
1941 i_size = i_size_read(vi);
1942 if (unlikely(attr_len > i_size)) {
1943 attr_len = i_size;
1944 a->data.resident.value_length = cpu_to_le32(attr_len);
1945 }
1946 /* If we do not need to resize the attribute allocation we are done. */
1947 if (new_size <= attr_len)
1948 goto done_unm;
1908 /* Check if new size is allowed in $AttrDef. */ 1949 /* Check if new size is allowed in $AttrDef. */
1909 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); 1950 err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
1910 if (unlikely(err)) { 1951 if (unlikely(err)) {
@@ -1962,6 +2003,7 @@ static int ntfs_prepare_write(struct file *file, struct page *page,
1962 } 2003 }
1963 flush_dcache_mft_record_page(ctx->ntfs_ino); 2004 flush_dcache_mft_record_page(ctx->ntfs_ino);
1964 mark_mft_record_dirty(ctx->ntfs_ino); 2005 mark_mft_record_dirty(ctx->ntfs_ino);
2006done_unm:
1965 ntfs_attr_put_search_ctx(ctx); 2007 ntfs_attr_put_search_ctx(ctx);
1966 unmap_mft_record(base_ni); 2008 unmap_mft_record(base_ni);
1967 /* 2009 /*
@@ -2047,7 +2089,7 @@ static int ntfs_commit_nonresident_write(struct page *page,
2047 * now we know ntfs_prepare_write() would have failed in the write 2089 * 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. 2090 * exceeds i_size case, so this will never trigger which is fine.
2049 */ 2091 */
2050 if (pos > vi->i_size) { 2092 if (pos > i_size_read(vi)) {
2051 ntfs_error(vi->i_sb, "Writing beyond the existing file size is " 2093 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
2052 "not supported yet. Sorry."); 2094 "not supported yet. Sorry.");
2053 return -EOPNOTSUPP; 2095 return -EOPNOTSUPP;
@@ -2183,9 +2225,13 @@ static int ntfs_commit_write(struct file *file, struct page *page,
2183 } 2225 }
2184 kunmap_atomic(kaddr, KM_USER0); 2226 kunmap_atomic(kaddr, KM_USER0);
2185 /* Update i_size if necessary. */ 2227 /* Update i_size if necessary. */
2186 if (vi->i_size < attr_len) { 2228 if (i_size_read(vi) < attr_len) {
2229 unsigned long flags;
2230
2231 write_lock_irqsave(&ni->size_lock, flags);
2187 ni->allocated_size = ni->initialized_size = attr_len; 2232 ni->allocated_size = ni->initialized_size = attr_len;
2188 i_size_write(vi, attr_len); 2233 i_size_write(vi, attr_len);
2234 write_unlock_irqrestore(&ni->size_lock, flags);
2189 } 2235 }
2190 /* Mark the mft record dirty, so it gets written back. */ 2236 /* Mark the mft record dirty, so it gets written back. */
2191 flush_dcache_mft_record_page(ctx->ntfs_ino); 2237 flush_dcache_mft_record_page(ctx->ntfs_ino);
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 1ff7f90a18b0..104eedfb2507 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,16 +21,19 @@
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 *
@@ -38,24 +41,23 @@
38 * 41 *
39 * Return 0 on success and -errno on error. 42 * Return 0 on success and -errno on error.
40 * 43 *
41 * Locking: - The runlist must be unlocked on entry and is unlocked on return. 44 * Locking: - The runlist must be locked for writing.
42 * - This function takes the lock for writing and modifies the runlist. 45 * - This function modifies the runlist.
43 */ 46 */
44int ntfs_map_runlist(ntfs_inode *ni, VCN vcn) 47int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn)
45{ 48{
46 ntfs_inode *base_ni; 49 ntfs_inode *base_ni;
47 ntfs_attr_search_ctx *ctx;
48 MFT_RECORD *mrec; 50 MFT_RECORD *mrec;
51 ntfs_attr_search_ctx *ctx;
52 runlist_element *rl;
49 int err = 0; 53 int err = 0;
50 54
51 ntfs_debug("Mapping runlist part containing vcn 0x%llx.", 55 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
52 (unsigned long long)vcn); 56 (unsigned long long)vcn);
53
54 if (!NInoAttr(ni)) 57 if (!NInoAttr(ni))
55 base_ni = ni; 58 base_ni = ni;
56 else 59 else
57 base_ni = ni->ext.base_ntfs_ino; 60 base_ni = ni->ext.base_ntfs_ino;
58
59 mrec = map_mft_record(base_ni); 61 mrec = map_mft_record(base_ni);
60 if (IS_ERR(mrec)) 62 if (IS_ERR(mrec))
61 return PTR_ERR(mrec); 63 return PTR_ERR(mrec);
@@ -66,15 +68,7 @@ int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
66 } 68 }
67 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 69 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
68 CASE_SENSITIVE, vcn, NULL, 0, ctx); 70 CASE_SENSITIVE, vcn, NULL, 0, ctx);
69 if (unlikely(err)) 71 if (likely(!err)) {
70 goto put_err_out;
71
72 down_write(&ni->runlist.lock);
73 /* 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) <=
75 LCN_RL_NOT_MAPPED)) {
76 runlist_element *rl;
77
78 rl = ntfs_mapping_pairs_decompress(ni->vol, ctx->attr, 72 rl = ntfs_mapping_pairs_decompress(ni->vol, ctx->attr,
79 ni->runlist.rl); 73 ni->runlist.rl);
80 if (IS_ERR(rl)) 74 if (IS_ERR(rl))
@@ -82,9 +76,6 @@ int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
82 else 76 else
83 ni->runlist.rl = rl; 77 ni->runlist.rl = rl;
84 } 78 }
85 up_write(&ni->runlist.lock);
86
87put_err_out:
88 ntfs_attr_put_search_ctx(ctx); 79 ntfs_attr_put_search_ctx(ctx);
89err_out: 80err_out:
90 unmap_mft_record(base_ni); 81 unmap_mft_record(base_ni);
@@ -92,17 +83,132 @@ err_out:
92} 83}
93 84
94/** 85/**
95 * ntfs_find_vcn - find a vcn in the runlist described by an ntfs inode 86 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
96 * @ni: ntfs inode describing the runlist to search 87 * @ni: ntfs inode for which to map (part of) a runlist
97 * @vcn: vcn to find 88 * @vcn: map runlist part containing this vcn
98 * @need_write: if false, lock for reading and if true, lock for writing 89 *
90 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
91 *
92 * Return 0 on success and -errno on error.
93 *
94 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
95 * - This function takes the runlist lock for writing and modifies the
96 * runlist.
97 */
98int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
99{
100 int err = 0;
101
102 down_write(&ni->runlist.lock);
103 /* Make sure someone else didn't do the work while we were sleeping. */
104 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
105 LCN_RL_NOT_MAPPED))
106 err = ntfs_map_runlist_nolock(ni, vcn);
107 up_write(&ni->runlist.lock);
108 return err;
109}
110
111/**
112 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
113 * @ni: ntfs inode of the attribute whose runlist to search
114 * @vcn: vcn to convert
115 * @write_locked: true if the runlist is locked for writing
116 *
117 * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
118 * described by the ntfs inode @ni and return the corresponding logical cluster
119 * number (lcn).
120 *
121 * If the @vcn is not mapped yet, the attempt is made to map the attribute
122 * extent containing the @vcn and the vcn to lcn conversion is retried.
123 *
124 * If @write_locked is true the caller has locked the runlist for writing and
125 * if false for reading.
126 *
127 * Since lcns must be >= 0, we use negative return codes with special meaning:
128 *
129 * Return code Meaning / Description
130 * ==========================================
131 * LCN_HOLE Hole / not allocated on disk.
132 * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
133 * LCN_ENOMEM Not enough memory to map runlist.
134 * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
135 *
136 * Locking: - The runlist must be locked on entry and is left locked on return.
137 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
138 * the lock may be dropped inside the function so you cannot rely on
139 * the runlist still being the same when this function returns.
140 */
141LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
142 const BOOL write_locked)
143{
144 LCN lcn;
145 BOOL is_retry = FALSE;
146
147 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
148 ni->mft_no, (unsigned long long)vcn,
149 write_locked ? "write" : "read");
150 BUG_ON(!ni);
151 BUG_ON(!NInoNonResident(ni));
152 BUG_ON(vcn < 0);
153retry_remap:
154 /* Convert vcn to lcn. If that fails map the runlist and retry once. */
155 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
156 if (likely(lcn >= LCN_HOLE)) {
157 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
158 return lcn;
159 }
160 if (lcn != LCN_RL_NOT_MAPPED) {
161 if (lcn != LCN_ENOENT)
162 lcn = LCN_EIO;
163 } else if (!is_retry) {
164 int err;
165
166 if (!write_locked) {
167 up_read(&ni->runlist.lock);
168 down_write(&ni->runlist.lock);
169 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
170 LCN_RL_NOT_MAPPED)) {
171 up_write(&ni->runlist.lock);
172 down_read(&ni->runlist.lock);
173 goto retry_remap;
174 }
175 }
176 err = ntfs_map_runlist_nolock(ni, vcn);
177 if (!write_locked) {
178 up_write(&ni->runlist.lock);
179 down_read(&ni->runlist.lock);
180 }
181 if (likely(!err)) {
182 is_retry = TRUE;
183 goto retry_remap;
184 }
185 if (err == -ENOENT)
186 lcn = LCN_ENOENT;
187 else if (err == -ENOMEM)
188 lcn = LCN_ENOMEM;
189 else
190 lcn = LCN_EIO;
191 }
192 if (lcn != LCN_ENOENT)
193 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
194 (long long)lcn);
195 return lcn;
196}
197
198/**
199 * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
200 * @ni: ntfs inode describing the runlist to search
201 * @vcn: vcn to find
202 * @write_locked: true if the runlist is locked for writing
99 * 203 *
100 * Find the virtual cluster number @vcn in the runlist described by the ntfs 204 * 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. 205 * 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 206 *
103 * is true, the runlist is locked for writing and if @need_write is false, the 207 * 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 208 * extent containing the @vcn and the vcn to lcn conversion is retried.
105 * locked. 209 *
210 * If @write_locked is true the caller has locked the runlist for writing and
211 * if false for reading.
106 * 212 *
107 * Note you need to distinguish between the lcn of the returned runlist element 213 * 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 214 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
@@ -118,34 +224,29 @@ err_out:
118 * -ENOMEM - Not enough memory to map runlist. 224 * -ENOMEM - Not enough memory to map runlist.
119 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc). 225 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
120 * 226 *
121 * Locking: - The runlist must be unlocked on entry. 227 * Locking: - The runlist must be locked on entry and is left locked on return.
122 * - On failing return, the runlist is unlocked. 228 * - 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 229 * 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 230 * the runlist still being the same when this function returns.
125 * reading.
126 */ 231 */
127runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn, 232runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
128 const BOOL need_write) 233 const BOOL write_locked)
129{ 234{
130 runlist_element *rl; 235 runlist_element *rl;
131 int err = 0; 236 int err = 0;
132 BOOL is_retry = FALSE; 237 BOOL is_retry = FALSE;
133 238
134 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, lock for %sing.", 239 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
135 ni->mft_no, (unsigned long long)vcn, 240 ni->mft_no, (unsigned long long)vcn,
136 !need_write ? "read" : "writ"); 241 write_locked ? "write" : "read");
137 BUG_ON(!ni); 242 BUG_ON(!ni);
138 BUG_ON(!NInoNonResident(ni)); 243 BUG_ON(!NInoNonResident(ni));
139 BUG_ON(vcn < 0); 244 BUG_ON(vcn < 0);
140lock_retry_remap: 245retry_remap:
141 if (!need_write)
142 down_read(&ni->runlist.lock);
143 else
144 down_write(&ni->runlist.lock);
145 rl = ni->runlist.rl; 246 rl = ni->runlist.rl;
146 if (likely(rl && vcn >= rl[0].vcn)) { 247 if (likely(rl && vcn >= rl[0].vcn)) {
147 while (likely(rl->length)) { 248 while (likely(rl->length)) {
148 if (likely(vcn < rl[1].vcn)) { 249 if (unlikely(vcn < rl[1].vcn)) {
149 if (likely(rl->lcn >= LCN_HOLE)) { 250 if (likely(rl->lcn >= LCN_HOLE)) {
150 ntfs_debug("Done."); 251 ntfs_debug("Done.");
151 return rl; 252 return rl;
@@ -161,19 +262,29 @@ lock_retry_remap:
161 err = -EIO; 262 err = -EIO;
162 } 263 }
163 } 264 }
164 if (!need_write)
165 up_read(&ni->runlist.lock);
166 else
167 up_write(&ni->runlist.lock);
168 if (!err && !is_retry) { 265 if (!err && !is_retry) {
169 /* 266 /*
170 * The @vcn is in an unmapped region, map the runlist and 267 * The @vcn is in an unmapped region, map the runlist and
171 * retry. 268 * retry.
172 */ 269 */
173 err = ntfs_map_runlist(ni, vcn); 270 if (!write_locked) {
271 up_read(&ni->runlist.lock);
272 down_write(&ni->runlist.lock);
273 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
274 LCN_RL_NOT_MAPPED)) {
275 up_write(&ni->runlist.lock);
276 down_read(&ni->runlist.lock);
277 goto retry_remap;
278 }
279 }
280 err = ntfs_map_runlist_nolock(ni, vcn);
281 if (!write_locked) {
282 up_write(&ni->runlist.lock);
283 down_read(&ni->runlist.lock);
284 }
174 if (likely(!err)) { 285 if (likely(!err)) {
175 is_retry = TRUE; 286 is_retry = TRUE;
176 goto lock_retry_remap; 287 goto retry_remap;
177 } 288 }
178 /* 289 /*
179 * -EINVAL and -ENOENT coming from a failed mapping attempt are 290 * -EINVAL and -ENOENT coming from a failed mapping attempt are
@@ -184,7 +295,8 @@ lock_retry_remap:
184 err = -EIO; 295 err = -EIO;
185 } else if (!err) 296 } else if (!err)
186 err = -EIO; 297 err = -EIO;
187 ntfs_error(ni->vol->sb, "Failed with error code %i.", err); 298 if (err != -ENOENT)
299 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
188 return ERR_PTR(err); 300 return ERR_PTR(err);
189} 301}
190 302
@@ -870,15 +982,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, 982static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
871 ntfs_inode *ni, MFT_RECORD *mrec) 983 ntfs_inode *ni, MFT_RECORD *mrec)
872{ 984{
873 ctx->mrec = mrec; 985 *ctx = (ntfs_attr_search_ctx) {
874 /* Sanity checks are performed elsewhere. */ 986 .mrec = mrec,
875 ctx->attr = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset)); 987 /* Sanity checks are performed elsewhere. */
876 ctx->is_first = TRUE; 988 .attr = (ATTR_RECORD*)((u8*)mrec +
877 ctx->ntfs_ino = ni; 989 le16_to_cpu(mrec->attrs_offset)),
878 ctx->al_entry = NULL; 990 .is_first = TRUE,
879 ctx->base_ntfs_ino = NULL; 991 .ntfs_ino = ni,
880 ctx->base_mrec = NULL; 992 };
881 ctx->base_attr = NULL;
882} 993}
883 994
884/** 995/**
@@ -945,6 +1056,8 @@ void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
945 return; 1056 return;
946} 1057}
947 1058
1059#ifdef NTFS_RW
1060
948/** 1061/**
949 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file 1062 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
950 * @vol: ntfs volume to which the attribute belongs 1063 * @vol: ntfs volume to which the attribute belongs
@@ -1024,27 +1137,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 1137 * 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. 1138 * be non-resident. This information is obtained from $AttrDef system file.
1026 * 1139 *
1027 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, or 1140 * 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. 1141 * -ENOENT if the attribute is not listed in $AttrDef.
1029 */ 1142 */
1030int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type) 1143int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1031{ 1144{
1032 ATTR_DEF *ad; 1145 ATTR_DEF *ad;
1033 1146
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. */ 1147 /* Find the attribute definition record in $AttrDef. */
1041 ad = ntfs_attr_find_in_attrdef(vol, type); 1148 ad = ntfs_attr_find_in_attrdef(vol, type);
1042 if (unlikely(!ad)) 1149 if (unlikely(!ad))
1043 return -ENOENT; 1150 return -ENOENT;
1044 /* Check the flags and return the result. */ 1151 /* Check the flags and return the result. */
1045 if (ad->flags & CAN_BE_NON_RESIDENT) 1152 if (ad->flags & ATTR_DEF_RESIDENT)
1046 return 0; 1153 return -EPERM;
1047 return -EPERM; 1154 return 0;
1048} 1155}
1049 1156
1050/** 1157/**
@@ -1067,9 +1174,9 @@ int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1067 */ 1174 */
1068int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type) 1175int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1069{ 1176{
1070 if (type != AT_INDEX_ALLOCATION && type != AT_EA) 1177 if (type == AT_INDEX_ALLOCATION || type == AT_EA)
1071 return 0; 1178 return -EPERM;
1072 return -EPERM; 1179 return 0;
1073} 1180}
1074 1181
1075/** 1182/**
@@ -1117,6 +1224,320 @@ int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1117} 1224}
1118 1225
1119/** 1226/**
1227 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1228 * @ni: ntfs inode describing the attribute to convert
1229 *
1230 * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1231 * non-resident one.
1232 *
1233 * Return 0 on success and -errno on error. The following error return codes
1234 * are defined:
1235 * -EPERM - The attribute is not allowed to be non-resident.
1236 * -ENOMEM - Not enough memory.
1237 * -ENOSPC - Not enough disk space.
1238 * -EINVAL - Attribute not defined on the volume.
1239 * -EIO - I/o error or other error.
1240 * Note that -ENOSPC is also returned in the case that there is not enough
1241 * space in the mft record to do the conversion. This can happen when the mft
1242 * record is already very full. The caller is responsible for trying to make
1243 * space in the mft record and trying again. FIXME: Do we need a separate
1244 * error return code for this kind of -ENOSPC or is it always worth trying
1245 * again in case the attribute may then fit in a resident state so no need to
1246 * make it non-resident at all? Ho-hum... (AIA)
1247 *
1248 * NOTE to self: No changes in the attribute list are required to move from
1249 * a resident to a non-resident attribute.
1250 *
1251 * Locking: - The caller must hold i_sem on the inode.
1252 */
1253int ntfs_attr_make_non_resident(ntfs_inode *ni)
1254{
1255 s64 new_size;
1256 struct inode *vi = VFS_I(ni);
1257 ntfs_volume *vol = ni->vol;
1258 ntfs_inode *base_ni;
1259 MFT_RECORD *m;
1260 ATTR_RECORD *a;
1261 ntfs_attr_search_ctx *ctx;
1262 struct page *page;
1263 runlist_element *rl;
1264 u8 *kaddr;
1265 unsigned long flags;
1266 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1267 u32 attr_size;
1268 u8 old_res_attr_flags;
1269
1270 /* Check that the attribute is allowed to be non-resident. */
1271 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1272 if (unlikely(err)) {
1273 if (err == -EPERM)
1274 ntfs_debug("Attribute is not allowed to be "
1275 "non-resident.");
1276 else
1277 ntfs_debug("Attribute not defined on the NTFS "
1278 "volume!");
1279 return err;
1280 }
1281 /*
1282 * The size needs to be aligned to a cluster boundary for allocation
1283 * purposes.
1284 */
1285 new_size = (i_size_read(vi) + vol->cluster_size - 1) &
1286 ~(vol->cluster_size - 1);
1287 if (new_size > 0) {
1288 /*
1289 * Will need the page later and since the page lock nests
1290 * outside all ntfs locks, we need to get the page now.
1291 */
1292 page = find_or_create_page(vi->i_mapping, 0,
1293 mapping_gfp_mask(vi->i_mapping));
1294 if (unlikely(!page))
1295 return -ENOMEM;
1296 /* Start by allocating clusters to hold the attribute value. */
1297 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1298 vol->cluster_size_bits, -1, DATA_ZONE);
1299 if (IS_ERR(rl)) {
1300 err = PTR_ERR(rl);
1301 ntfs_debug("Failed to allocate cluster%s, error code "
1302 "%i.\n", (new_size >>
1303 vol->cluster_size_bits) > 1 ? "s" : "",
1304 err);
1305 goto page_err_out;
1306 }
1307 } else {
1308 rl = NULL;
1309 page = NULL;
1310 }
1311 /* Determine the size of the mapping pairs array. */
1312 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0);
1313 if (unlikely(mp_size < 0)) {
1314 err = mp_size;
1315 ntfs_debug("Failed to get size for mapping pairs array, error "
1316 "code %i.", err);
1317 goto rl_err_out;
1318 }
1319 down_write(&ni->runlist.lock);
1320 if (!NInoAttr(ni))
1321 base_ni = ni;
1322 else
1323 base_ni = ni->ext.base_ntfs_ino;
1324 m = map_mft_record(base_ni);
1325 if (IS_ERR(m)) {
1326 err = PTR_ERR(m);
1327 m = NULL;
1328 ctx = NULL;
1329 goto err_out;
1330 }
1331 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1332 if (unlikely(!ctx)) {
1333 err = -ENOMEM;
1334 goto err_out;
1335 }
1336 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1337 CASE_SENSITIVE, 0, NULL, 0, ctx);
1338 if (unlikely(err)) {
1339 if (err == -ENOENT)
1340 err = -EIO;
1341 goto err_out;
1342 }
1343 m = ctx->mrec;
1344 a = ctx->attr;
1345 BUG_ON(NInoNonResident(ni));
1346 BUG_ON(a->non_resident);
1347 /*
1348 * Calculate new offsets for the name and the mapping pairs array.
1349 * We assume the attribute is not compressed or sparse.
1350 */
1351 name_ofs = (offsetof(ATTR_REC,
1352 data.non_resident.compressed_size) + 7) & ~7;
1353 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1354 /*
1355 * Determine the size of the resident part of the now non-resident
1356 * attribute record.
1357 */
1358 arec_size = (mp_ofs + mp_size + 7) & ~7;
1359 /*
1360 * If the page is not uptodate bring it uptodate by copying from the
1361 * attribute value.
1362 */
1363 attr_size = le32_to_cpu(a->data.resident.value_length);
1364 BUG_ON(attr_size != i_size_read(vi));
1365 if (page && !PageUptodate(page)) {
1366 kaddr = kmap_atomic(page, KM_USER0);
1367 memcpy(kaddr, (u8*)a +
1368 le16_to_cpu(a->data.resident.value_offset),
1369 attr_size);
1370 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1371 kunmap_atomic(kaddr, KM_USER0);
1372 flush_dcache_page(page);
1373 SetPageUptodate(page);
1374 }
1375 /* Backup the attribute flag. */
1376 old_res_attr_flags = a->data.resident.flags;
1377 /* Resize the resident part of the attribute record. */
1378 err = ntfs_attr_record_resize(m, a, arec_size);
1379 if (unlikely(err))
1380 goto err_out;
1381 /*
1382 * Convert the resident part of the attribute record to describe a
1383 * non-resident attribute.
1384 */
1385 a->non_resident = 1;
1386 /* Move the attribute name if it exists and update the offset. */
1387 if (a->name_length)
1388 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1389 a->name_length * sizeof(ntfschar));
1390 a->name_offset = cpu_to_le16(name_ofs);
1391 /*
1392 * FIXME: For now just clear all of these as we do not support them
1393 * when writing.
1394 */
1395 a->flags &= cpu_to_le16(0xffff & ~le16_to_cpu(ATTR_IS_SPARSE |
1396 ATTR_IS_ENCRYPTED | ATTR_COMPRESSION_MASK));
1397 /* Setup the fields specific to non-resident attributes. */
1398 a->data.non_resident.lowest_vcn = 0;
1399 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1400 vol->cluster_size_bits);
1401 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1402 a->data.non_resident.compression_unit = 0;
1403 memset(&a->data.non_resident.reserved, 0,
1404 sizeof(a->data.non_resident.reserved));
1405 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1406 a->data.non_resident.data_size =
1407 a->data.non_resident.initialized_size =
1408 cpu_to_sle64(attr_size);
1409 /* Generate the mapping pairs array into the attribute record. */
1410 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1411 arec_size - mp_ofs, rl, 0, NULL);
1412 if (unlikely(err)) {
1413 ntfs_debug("Failed to build mapping pairs, error code %i.",
1414 err);
1415 goto undo_err_out;
1416 }
1417 /* Setup the in-memory attribute structure to be non-resident. */
1418 /*
1419 * FIXME: For now just clear all of these as we do not support them
1420 * when writing.
1421 */
1422 NInoClearSparse(ni);
1423 NInoClearEncrypted(ni);
1424 NInoClearCompressed(ni);
1425 ni->runlist.rl = rl;
1426 write_lock_irqsave(&ni->size_lock, flags);
1427 ni->allocated_size = new_size;
1428 write_unlock_irqrestore(&ni->size_lock, flags);
1429 /*
1430 * This needs to be last since the address space operations ->readpage
1431 * and ->writepage can run concurrently with us as they are not
1432 * serialized on i_sem. Note, we are not allowed to fail once we flip
1433 * this switch, which is another reason to do this last.
1434 */
1435 NInoSetNonResident(ni);
1436 /* Mark the mft record dirty, so it gets written back. */
1437 flush_dcache_mft_record_page(ctx->ntfs_ino);
1438 mark_mft_record_dirty(ctx->ntfs_ino);
1439 ntfs_attr_put_search_ctx(ctx);
1440 unmap_mft_record(base_ni);
1441 up_write(&ni->runlist.lock);
1442 if (page) {
1443 set_page_dirty(page);
1444 unlock_page(page);
1445 mark_page_accessed(page);
1446 page_cache_release(page);
1447 }
1448 ntfs_debug("Done.");
1449 return 0;
1450undo_err_out:
1451 /* Convert the attribute back into a resident attribute. */
1452 a->non_resident = 0;
1453 /* Move the attribute name if it exists and update the offset. */
1454 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1455 sizeof(a->data.resident.reserved) + 7) & ~7;
1456 if (a->name_length)
1457 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1458 a->name_length * sizeof(ntfschar));
1459 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1460 a->name_offset = cpu_to_le16(name_ofs);
1461 arec_size = (mp_ofs + attr_size + 7) & ~7;
1462 /* Resize the resident part of the attribute record. */
1463 err2 = ntfs_attr_record_resize(m, a, arec_size);
1464 if (unlikely(err2)) {
1465 /*
1466 * This cannot happen (well if memory corruption is at work it
1467 * could happen in theory), but deal with it as well as we can.
1468 * If the old size is too small, truncate the attribute,
1469 * otherwise simply give it a larger allocated size.
1470 * FIXME: Should check whether chkdsk complains when the
1471 * allocated size is much bigger than the resident value size.
1472 */
1473 arec_size = le32_to_cpu(a->length);
1474 if ((mp_ofs + attr_size) > arec_size) {
1475 err2 = attr_size;
1476 attr_size = arec_size - mp_ofs;
1477 ntfs_error(vol->sb, "Failed to undo partial resident "
1478 "to non-resident attribute "
1479 "conversion. Truncating inode 0x%lx, "
1480 "attribute type 0x%x from %i bytes to "
1481 "%i bytes to maintain metadata "
1482 "consistency. THIS MEANS YOU ARE "
1483 "LOSING %i BYTES DATA FROM THIS %s.",
1484 vi->i_ino,
1485 (unsigned)le32_to_cpu(ni->type),
1486 err2, attr_size, err2 - attr_size,
1487 ((ni->type == AT_DATA) &&
1488 !ni->name_len) ? "FILE": "ATTRIBUTE");
1489 write_lock_irqsave(&ni->size_lock, flags);
1490 ni->initialized_size = attr_size;
1491 i_size_write(vi, attr_size);
1492 write_unlock_irqrestore(&ni->size_lock, flags);
1493 }
1494 }
1495 /* Setup the fields specific to resident attributes. */
1496 a->data.resident.value_length = cpu_to_le32(attr_size);
1497 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1498 a->data.resident.flags = old_res_attr_flags;
1499 memset(&a->data.resident.reserved, 0,
1500 sizeof(a->data.resident.reserved));
1501 /* Copy the data from the page back to the attribute value. */
1502 if (page) {
1503 kaddr = kmap_atomic(page, KM_USER0);
1504 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1505 kunmap_atomic(kaddr, KM_USER0);
1506 }
1507 /* Setup the allocated size in the ntfs inode in case it changed. */
1508 write_lock_irqsave(&ni->size_lock, flags);
1509 ni->allocated_size = arec_size - mp_ofs;
1510 write_unlock_irqrestore(&ni->size_lock, flags);
1511 /* Mark the mft record dirty, so it gets written back. */
1512 flush_dcache_mft_record_page(ctx->ntfs_ino);
1513 mark_mft_record_dirty(ctx->ntfs_ino);
1514err_out:
1515 if (ctx)
1516 ntfs_attr_put_search_ctx(ctx);
1517 if (m)
1518 unmap_mft_record(base_ni);
1519 ni->runlist.rl = NULL;
1520 up_write(&ni->runlist.lock);
1521rl_err_out:
1522 if (rl) {
1523 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1524 ntfs_error(vol->sb, "Failed to release allocated "
1525 "cluster(s) in error code path. Run "
1526 "chkdsk to recover the lost "
1527 "cluster(s).");
1528 NVolSetErrors(vol);
1529 }
1530 ntfs_free(rl);
1531page_err_out:
1532 unlock_page(page);
1533 page_cache_release(page);
1534 }
1535 if (err == -EINVAL)
1536 err = -EIO;
1537 return err;
1538}
1539
1540/**
1120 * ntfs_attr_set - fill (a part of) an attribute with a byte 1541 * ntfs_attr_set - fill (a part of) an attribute with a byte
1121 * @ni: ntfs inode describing the attribute to fill 1542 * @ni: ntfs inode describing the attribute to fill
1122 * @ofs: offset inside the attribute at which to start to fill 1543 * @ofs: offset inside the attribute at which to start to fill
@@ -1127,6 +1548,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. 1548 * byte offset @ofs inside the attribute with the constant byte @val.
1128 * 1549 *
1129 * This function is effectively like memset() applied to an ntfs attribute. 1550 * This function is effectively like memset() applied to an ntfs attribute.
1551 * Note thie function actually only operates on the page cache pages belonging
1552 * to the ntfs attribute and it marks them dirty after doing the memset().
1553 * Thus it relies on the vm dirty page write code paths to cause the modified
1554 * pages to be written to the mft record/disk.
1130 * 1555 *
1131 * Return 0 on success and -errno on error. An error code of -ESPIPE means 1556 * 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 1557 * that @ofs + @cnt were outside the end of the attribute and no write was
@@ -1155,7 +1580,7 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
1155 end = ofs + cnt; 1580 end = ofs + cnt;
1156 end_ofs = end & ~PAGE_CACHE_MASK; 1581 end_ofs = end & ~PAGE_CACHE_MASK;
1157 /* If the end is outside the inode size return -ESPIPE. */ 1582 /* If the end is outside the inode size return -ESPIPE. */
1158 if (unlikely(end > VFS_I(ni)->i_size)) { 1583 if (unlikely(end > i_size_read(VFS_I(ni)))) {
1159 ntfs_error(vol->sb, "Request exceeds end of attribute."); 1584 ntfs_error(vol->sb, "Request exceeds end of attribute.");
1160 return -ESPIPE; 1585 return -ESPIPE;
1161 } 1586 }
@@ -1256,3 +1681,5 @@ done:
1256 ntfs_debug("Done."); 1681 ntfs_debug("Done.");
1257 return 0; 1682 return 0;
1258} 1683}
1684
1685#endif /* NTFS_RW */
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index e0c2c6c81bc0..0e4ac6d3c0e7 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,
@@ -94,7 +100,11 @@ extern int ntfs_attr_can_be_resident(const ntfs_volume *vol,
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);
96 102
103extern int ntfs_attr_make_non_resident(ntfs_inode *ni);
104
97extern int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, 105extern int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt,
98 const u8 val); 106 const u8 val);
99 107
108#endif /* NTFS_RW */
109
100#endif /* _LINUX_NTFS_ATTRIB_H */ 110#endif /* _LINUX_NTFS_ATTRIB_H */
diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
index ee5ae706f861..6d265cfd49aa 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;
@@ -552,8 +556,12 @@ int ntfs_read_compressed_block(struct page *page)
552 * The remaining pages need to be allocated and inserted into the page 556 * The remaining pages need to be allocated and inserted into the page
553 * cache, alignment guarantees keep all the below much simpler. (-8 557 * cache, alignment guarantees keep all the below much simpler. (-8
554 */ 558 */
555 max_page = ((VFS_I(ni)->i_size + PAGE_CACHE_SIZE - 1) >> 559 read_lock_irqsave(&ni->size_lock, flags);
556 PAGE_CACHE_SHIFT) - offset; 560 i_size = i_size_read(VFS_I(ni));
561 initialized_size = ni->initialized_size;
562 read_unlock_irqrestore(&ni->size_lock, flags);
563 max_page = ((i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
564 offset;
557 if (nr_pages < max_page) 565 if (nr_pages < max_page)
558 max_page = nr_pages; 566 max_page = nr_pages;
559 for (i = 0; i < max_page; i++, offset++) { 567 for (i = 0; i < max_page; i++, offset++) {
@@ -824,7 +832,8 @@ lock_retry_remap:
824 * If we are outside the initialized size, zero 832 * If we are outside the initialized size, zero
825 * the out of bounds page range. 833 * the out of bounds page range.
826 */ 834 */
827 handle_bounds_compressed_page(ni, page); 835 handle_bounds_compressed_page(page, i_size,
836 initialized_size);
828 flush_dcache_page(page); 837 flush_dcache_page(page);
829 kunmap(page); 838 kunmap(page);
830 SetPageUptodate(page); 839 SetPageUptodate(page);
@@ -847,7 +856,8 @@ lock_retry_remap:
847 ntfs_debug("Found compressed compression block."); 856 ntfs_debug("Found compressed compression block.");
848 err = ntfs_decompress(pages, &cur_page, &cur_ofs, 857 err = ntfs_decompress(pages, &cur_page, &cur_ofs,
849 cb_max_page, cb_max_ofs, xpage, &xpage_done, 858 cb_max_page, cb_max_ofs, xpage, &xpage_done,
850 cb_pos, cb_size - (cb_pos - cb)); 859 cb_pos, cb_size - (cb_pos - cb), i_size,
860 initialized_size);
851 /* 861 /*
852 * We can sleep from now on, lock already dropped by 862 * We can sleep from now on, lock already dropped by
853 * ntfs_decompress(). 863 * ntfs_decompress().
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..a56ca1821eed 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
@@ -1101,7 +1099,7 @@ static inline int ntfs_filldir(ntfs_volume *vol, loff_t fpos,
1101static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) 1099static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1102{ 1100{
1103 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos; 1101 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1104 loff_t fpos; 1102 loff_t fpos, i_size;
1105 struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode; 1103 struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode;
1106 struct super_block *sb = vdir->i_sb; 1104 struct super_block *sb = vdir->i_sb;
1107 ntfs_inode *ndir = NTFS_I(vdir); 1105 ntfs_inode *ndir = NTFS_I(vdir);
@@ -1122,7 +1120,8 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1122 vdir->i_ino, fpos); 1120 vdir->i_ino, fpos);
1123 rc = err = 0; 1121 rc = err = 0;
1124 /* Are we at end of dir yet? */ 1122 /* Are we at end of dir yet? */
1125 if (fpos >= vdir->i_size + vol->mft_record_size) 1123 i_size = i_size_read(vdir);
1124 if (fpos >= i_size + vol->mft_record_size)
1126 goto done; 1125 goto done;
1127 /* Emulate . and .. for all directories. */ 1126 /* Emulate . and .. for all directories. */
1128 if (!fpos) { 1127 if (!fpos) {
@@ -1264,7 +1263,7 @@ skip_index_root:
1264 bmp_mapping = bmp_vi->i_mapping; 1263 bmp_mapping = bmp_vi->i_mapping;
1265 /* Get the starting bitmap bit position and sanity check it. */ 1264 /* Get the starting bitmap bit position and sanity check it. */
1266 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits; 1265 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1267 if (unlikely(bmp_pos >> 3 >= bmp_vi->i_size)) { 1266 if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
1268 ntfs_error(sb, "Current index allocation position exceeds " 1267 ntfs_error(sb, "Current index allocation position exceeds "
1269 "index bitmap size."); 1268 "index bitmap size.");
1270 goto err_out; 1269 goto err_out;
@@ -1301,7 +1300,7 @@ find_next_index_buffer:
1301 goto get_next_bmp_page; 1300 goto get_next_bmp_page;
1302 } 1301 }
1303 /* If we have reached the end of the bitmap, we are done. */ 1302 /* If we have reached the end of the bitmap, we are done. */
1304 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= vdir->i_size)) 1303 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
1305 goto unm_EOD; 1304 goto unm_EOD;
1306 ia_pos = (bmp_pos + cur_bmp_pos) << 1305 ia_pos = (bmp_pos + cur_bmp_pos) <<
1307 ndir->itype.index.block_size_bits; 1306 ndir->itype.index.block_size_bits;
@@ -1441,7 +1440,7 @@ unm_EOD:
1441 ntfs_unmap_page(bmp_page); 1440 ntfs_unmap_page(bmp_page);
1442EOD: 1441EOD:
1443 /* We are finished, set fpos to EOD. */ 1442 /* We are finished, set fpos to EOD. */
1444 fpos = vdir->i_size + vol->mft_record_size; 1443 fpos = i_size + vol->mft_record_size;
1445abort: 1444abort:
1446 kfree(name); 1445 kfree(name);
1447done: 1446done:
@@ -1461,10 +1460,8 @@ err_out:
1461 unlock_page(ia_page); 1460 unlock_page(ia_page);
1462 ntfs_unmap_page(ia_page); 1461 ntfs_unmap_page(ia_page);
1463 } 1462 }
1464 if (ir) 1463 kfree(ir);
1465 kfree(ir); 1464 kfree(name);
1466 if (name)
1467 kfree(name);
1468 if (ctx) 1465 if (ctx)
1469 ntfs_attr_put_search_ctx(ctx); 1466 ntfs_attr_put_search_ctx(ctx);
1470 if (m) 1467 if (m)
@@ -1495,7 +1492,7 @@ err_out:
1495static int ntfs_dir_open(struct inode *vi, struct file *filp) 1492static int ntfs_dir_open(struct inode *vi, struct file *filp)
1496{ 1493{
1497 if (sizeof(unsigned long) < 8) { 1494 if (sizeof(unsigned long) < 8) {
1498 if (vi->i_size > MAX_LFS_FILESIZE) 1495 if (i_size_read(vi) > MAX_LFS_FILESIZE)
1499 return -EFBIG; 1496 return -EFBIG;
1500 } 1497 }
1501 return 0; 1498 return 0;
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index db8713ea0d27..e0f530ce6b99 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -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);
diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c
index 71bd2cd7a4d9..11fd5307d780 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
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 31840ba0b38c..886214a77f90 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,92 @@ 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->non_resident) {
1015 NInoSetNonResident(ni); 1017 NInoSetNonResident(ni);
1016 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1018 if (a->flags & (ATTR_COMPRESSION_MASK |
1017 NInoSetCompressed(ni); 1019 ATTR_IS_SPARSE)) {
1018 if (vol->cluster_size > 4096) { 1020 if (a->flags & ATTR_COMPRESSION_MASK) {
1019 ntfs_error(vi->i_sb, "Found " 1021 NInoSetCompressed(ni);
1020 "compressed data but " 1022 if (vol->cluster_size > 4096) {
1021 "compression is disabled due " 1023 ntfs_error(vi->i_sb, "Found "
1022 "to cluster size (%i) > 4kiB.", 1024 "compressed data but "
1023 vol->cluster_size); 1025 "compression is "
1024 goto unm_err_out; 1026 "disabled due to "
1025 } 1027 "cluster size (%i) > "
1026 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK) 1028 "4kiB.",
1027 != ATTR_IS_COMPRESSED) { 1029 vol->cluster_size);
1028 ntfs_error(vi->i_sb, "Found " 1030 goto unm_err_out;
1029 "unknown compression method or " 1031 }
1030 "corrupt file."); 1032 if ((a->flags & ATTR_COMPRESSION_MASK)
1031 goto unm_err_out; 1033 != ATTR_IS_COMPRESSED) {
1034 ntfs_error(vi->i_sb, "Found "
1035 "unknown compression "
1036 "method or corrupt "
1037 "file.");
1038 goto unm_err_out;
1039 }
1032 } 1040 }
1033 ni->itype.compressed.block_clusters = 1U << 1041 if (a->flags & ATTR_IS_SPARSE)
1034 ctx->attr->data.non_resident. 1042 NInoSetSparse(ni);
1035 compression_unit; 1043 if (a->data.non_resident.compression_unit !=
1036 if (ctx->attr->data.non_resident. 1044 4) {
1037 compression_unit != 4) {
1038 ntfs_error(vi->i_sb, "Found " 1045 ntfs_error(vi->i_sb, "Found "
1039 "nonstandard compression unit " 1046 "nonstandard compression unit "
1040 "(%u instead of 4). Cannot " 1047 "(%u instead of 4). Cannot "
1041 "handle this.", 1048 "handle this.",
1042 ctx->attr->data.non_resident. 1049 a->data.non_resident.
1043 compression_unit); 1050 compression_unit);
1044 err = -EOPNOTSUPP; 1051 err = -EOPNOTSUPP;
1045 goto unm_err_out; 1052 goto unm_err_out;
1046 } 1053 }
1054 ni->itype.compressed.block_clusters = 1U <<
1055 a->data.non_resident.
1056 compression_unit;
1047 ni->itype.compressed.block_size = 1U << ( 1057 ni->itype.compressed.block_size = 1U << (
1048 ctx->attr->data.non_resident. 1058 a->data.non_resident.
1049 compression_unit + 1059 compression_unit +
1050 vol->cluster_size_bits); 1060 vol->cluster_size_bits);
1051 ni->itype.compressed.block_size_bits = ffs( 1061 ni->itype.compressed.block_size_bits = ffs(
1052 ni->itype.compressed.block_size) - 1; 1062 ni->itype.compressed.
1063 block_size) - 1;
1064 ni->itype.compressed.size = sle64_to_cpu(
1065 a->data.non_resident.
1066 compressed_size);
1053 } 1067 }
1054 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 1068 if (a->flags & ATTR_IS_ENCRYPTED) {
1055 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1069 if (a->flags & ATTR_COMPRESSION_MASK) {
1056 ntfs_error(vi->i_sb, "Found encrypted " 1070 ntfs_error(vi->i_sb, "Found encrypted "
1057 "and compressed data."); 1071 "and compressed data.");
1058 goto unm_err_out; 1072 goto unm_err_out;
1059 } 1073 }
1060 NInoSetEncrypted(ni); 1074 NInoSetEncrypted(ni);
1061 } 1075 }
1062 if (ctx->attr->flags & ATTR_IS_SPARSE) 1076 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 " 1077 ntfs_error(vi->i_sb, "First extent of $DATA "
1066 "attribute has non zero " 1078 "attribute has non zero "
1067 "lowest_vcn."); 1079 "lowest_vcn.");
1068 goto unm_err_out; 1080 goto unm_err_out;
1069 } 1081 }
1070 /* Setup all the sizes. */
1071 vi->i_size = sle64_to_cpu( 1082 vi->i_size = sle64_to_cpu(
1072 ctx->attr->data.non_resident.data_size); 1083 a->data.non_resident.data_size);
1073 ni->initialized_size = sle64_to_cpu( 1084 ni->initialized_size = sle64_to_cpu(
1074 ctx->attr->data.non_resident. 1085 a->data.non_resident.initialized_size);
1075 initialized_size);
1076 ni->allocated_size = sle64_to_cpu( 1086 ni->allocated_size = sle64_to_cpu(
1077 ctx->attr->data.non_resident. 1087 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. */ 1088 } else { /* Resident attribute. */
1085 /* 1089 vi->i_size = ni->initialized_size = le32_to_cpu(
1086 * Make all sizes equal for simplicity in read code 1090 a->data.resident.value_length);
1087 * paths. FIXME: Need to keep this in mind when 1091 ni->allocated_size = le32_to_cpu(a->length) -
1088 * converting to non-resident attribute in write code 1092 le16_to_cpu(
1089 * path. (Probably only affects truncate().) 1093 a->data.resident.value_offset);
1090 */ 1094 if (vi->i_size > ni->allocated_size) {
1091 vi->i_size = ni->initialized_size = ni->allocated_size = 1095 ntfs_error(vi->i_sb, "Resident data attribute "
1092 le32_to_cpu( 1096 "is corrupt (size exceeds "
1093 ctx->attr->data.resident.value_length); 1097 "allocation).");
1098 goto unm_err_out;
1099 }
1094 } 1100 }
1095no_data_attr_special_case: 1101no_data_attr_special_case:
1096 /* We are done with the mft record, so we release it. */ 1102 /* We are done with the mft record, so we release it. */
@@ -1117,11 +1123,10 @@ no_data_attr_special_case:
1117 * sizes of all non-resident attributes present to give us the Linux 1123 * 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). 1124 * correct size that should go into i_blocks (after division by 512).
1119 */ 1125 */
1120 if (S_ISDIR(vi->i_mode) || !NInoCompressed(ni)) 1126 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; 1127 vi->i_blocks = ni->itype.compressed.size >> 9;
1124 1128 else
1129 vi->i_blocks = ni->allocated_size >> 9;
1125 ntfs_debug("Done."); 1130 ntfs_debug("Done.");
1126 return 0; 1131 return 0;
1127 1132
@@ -1166,6 +1171,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1166 ntfs_volume *vol = NTFS_SB(vi->i_sb); 1171 ntfs_volume *vol = NTFS_SB(vi->i_sb);
1167 ntfs_inode *ni, *base_ni; 1172 ntfs_inode *ni, *base_ni;
1168 MFT_RECORD *m; 1173 MFT_RECORD *m;
1174 ATTR_RECORD *a;
1169 ntfs_attr_search_ctx *ctx; 1175 ntfs_attr_search_ctx *ctx;
1170 int err = 0; 1176 int err = 0;
1171 1177
@@ -1200,24 +1206,21 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1200 err = -ENOMEM; 1206 err = -ENOMEM;
1201 goto unm_err_out; 1207 goto unm_err_out;
1202 } 1208 }
1203
1204 /* Find the attribute. */ 1209 /* Find the attribute. */
1205 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1210 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1206 CASE_SENSITIVE, 0, NULL, 0, ctx); 1211 CASE_SENSITIVE, 0, NULL, 0, ctx);
1207 if (unlikely(err)) 1212 if (unlikely(err))
1208 goto unm_err_out; 1213 goto unm_err_out;
1209 1214 a = ctx->attr;
1210 if (!ctx->attr->non_resident) { 1215 if (!a->non_resident) {
1211 /* Ensure the attribute name is placed before the value. */ 1216 /* Ensure the attribute name is placed before the value. */
1212 if (unlikely(ctx->attr->name_length && 1217 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1213 (le16_to_cpu(ctx->attr->name_offset) >= 1218 le16_to_cpu(a->data.resident.value_offset)))) {
1214 le16_to_cpu(ctx->attr->data.resident.
1215 value_offset)))) {
1216 ntfs_error(vol->sb, "Attribute name is placed after " 1219 ntfs_error(vol->sb, "Attribute name is placed after "
1217 "the attribute value."); 1220 "the attribute value.");
1218 goto unm_err_out; 1221 goto unm_err_out;
1219 } 1222 }
1220 if (NInoMstProtected(ni) || ctx->attr->flags) { 1223 if (NInoMstProtected(ni) || a->flags) {
1221 ntfs_error(vi->i_sb, "Found mst protected attribute " 1224 ntfs_error(vi->i_sb, "Found mst protected attribute "
1222 "or attribute with non-zero flags but " 1225 "or attribute with non-zero flags but "
1223 "the attribute is resident. Please " 1226 "the attribute is resident. Please "
@@ -1225,85 +1228,95 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1225 "linux-ntfs-dev@lists.sourceforge.net"); 1228 "linux-ntfs-dev@lists.sourceforge.net");
1226 goto unm_err_out; 1229 goto unm_err_out;
1227 } 1230 }
1228 /* 1231 vi->i_size = ni->initialized_size = le32_to_cpu(
1229 * Resident attribute. Make all sizes equal for simplicity in 1232 a->data.resident.value_length);
1230 * read code paths. 1233 ni->allocated_size = le32_to_cpu(a->length) -
1231 */ 1234 le16_to_cpu(a->data.resident.value_offset);
1232 vi->i_size = ni->initialized_size = ni->allocated_size = 1235 if (vi->i_size > ni->allocated_size) {
1233 le32_to_cpu(ctx->attr->data.resident.value_length); 1236 ntfs_error(vi->i_sb, "Resident attribute is corrupt "
1237 "(size exceeds allocation).");
1238 goto unm_err_out;
1239 }
1234 } else { 1240 } else {
1235 NInoSetNonResident(ni); 1241 NInoSetNonResident(ni);
1236 /* 1242 /*
1237 * Ensure the attribute name is placed before the mapping pairs 1243 * Ensure the attribute name is placed before the mapping pairs
1238 * array. 1244 * array.
1239 */ 1245 */
1240 if (unlikely(ctx->attr->name_length && 1246 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1241 (le16_to_cpu(ctx->attr->name_offset) >= 1247 le16_to_cpu(
1242 le16_to_cpu(ctx->attr->data.non_resident. 1248 a->data.non_resident.mapping_pairs_offset)))) {
1243 mapping_pairs_offset)))) {
1244 ntfs_error(vol->sb, "Attribute name is placed after " 1249 ntfs_error(vol->sb, "Attribute name is placed after "
1245 "the mapping pairs array."); 1250 "the mapping pairs array.");
1246 goto unm_err_out; 1251 goto unm_err_out;
1247 } 1252 }
1248 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1253 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
1254 if (a->flags & ATTR_COMPRESSION_MASK) {
1255 NInoSetCompressed(ni);
1256 if ((ni->type != AT_DATA) || (ni->type ==
1257 AT_DATA && ni->name_len)) {
1258 ntfs_error(vi->i_sb, "Found compressed "
1259 "non-data or named "
1260 "data attribute. "
1261 "Please report you "
1262 "saw this message to "
1263 "linux-ntfs-dev@lists."
1264 "sourceforge.net");
1265 goto unm_err_out;
1266 }
1267 if (vol->cluster_size > 4096) {
1268 ntfs_error(vi->i_sb, "Found compressed "
1269 "attribute but "
1270 "compression is "
1271 "disabled due to "
1272 "cluster size (%i) > "
1273 "4kiB.",
1274 vol->cluster_size);
1275 goto unm_err_out;
1276 }
1277 if ((a->flags & ATTR_COMPRESSION_MASK) !=
1278 ATTR_IS_COMPRESSED) {
1279 ntfs_error(vi->i_sb, "Found unknown "
1280 "compression method.");
1281 goto unm_err_out;
1282 }
1283 }
1249 if (NInoMstProtected(ni)) { 1284 if (NInoMstProtected(ni)) {
1250 ntfs_error(vi->i_sb, "Found mst protected " 1285 ntfs_error(vi->i_sb, "Found mst protected "
1251 "attribute but the attribute " 1286 "attribute but the attribute "
1252 "is compressed. Please report " 1287 "is %s. Please report you "
1253 "you saw this message to " 1288 "saw this message to "
1254 "linux-ntfs-dev@lists."
1255 "sourceforge.net");
1256 goto unm_err_out;
1257 }
1258 NInoSetCompressed(ni);
1259 if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
1260 ni->name_len)) {
1261 ntfs_error(vi->i_sb, "Found compressed "
1262 "non-data or named data "
1263 "attribute. Please report "
1264 "you saw this message to "
1265 "linux-ntfs-dev@lists." 1289 "linux-ntfs-dev@lists."
1266 "sourceforge.net"); 1290 "sourceforge.net",
1267 goto unm_err_out; 1291 NInoCompressed(ni) ?
1268 } 1292 "compressed" : "sparse");
1269 if (vol->cluster_size > 4096) {
1270 ntfs_error(vi->i_sb, "Found compressed "
1271 "attribute but compression is "
1272 "disabled due to cluster size "
1273 "(%i) > 4kiB.",
1274 vol->cluster_size);
1275 goto unm_err_out; 1293 goto unm_err_out;
1276 } 1294 }
1277 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK) 1295 if (a->flags & ATTR_IS_SPARSE)
1278 != ATTR_IS_COMPRESSED) { 1296 NInoSetSparse(ni);
1279 ntfs_error(vi->i_sb, "Found unknown " 1297 if (a->data.non_resident.compression_unit != 4) {
1280 "compression method.");
1281 goto unm_err_out;
1282 }
1283 ni->itype.compressed.block_clusters = 1U <<
1284 ctx->attr->data.non_resident.
1285 compression_unit;
1286 if (ctx->attr->data.non_resident.compression_unit !=
1287 4) {
1288 ntfs_error(vi->i_sb, "Found nonstandard " 1298 ntfs_error(vi->i_sb, "Found nonstandard "
1289 "compression unit (%u instead " 1299 "compression unit (%u instead "
1290 "of 4). Cannot handle this.", 1300 "of 4). Cannot handle this.",
1291 ctx->attr->data.non_resident. 1301 a->data.non_resident.
1292 compression_unit); 1302 compression_unit);
1293 err = -EOPNOTSUPP; 1303 err = -EOPNOTSUPP;
1294 goto unm_err_out; 1304 goto unm_err_out;
1295 } 1305 }
1306 ni->itype.compressed.block_clusters = 1U <<
1307 a->data.non_resident.compression_unit;
1296 ni->itype.compressed.block_size = 1U << ( 1308 ni->itype.compressed.block_size = 1U << (
1297 ctx->attr->data.non_resident. 1309 a->data.non_resident.compression_unit +
1298 compression_unit +
1299 vol->cluster_size_bits); 1310 vol->cluster_size_bits);
1300 ni->itype.compressed.block_size_bits = ffs( 1311 ni->itype.compressed.block_size_bits = ffs(
1301 ni->itype.compressed.block_size) - 1; 1312 ni->itype.compressed.block_size) - 1;
1313 ni->itype.compressed.size = sle64_to_cpu(
1314 a->data.non_resident.compressed_size);
1302 } 1315 }
1303 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 1316 if (a->flags & ATTR_IS_ENCRYPTED) {
1304 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1317 if (a->flags & ATTR_COMPRESSION_MASK) {
1305 ntfs_error(vi->i_sb, "Found encrypted " 1318 ntfs_error(vi->i_sb, "Found encrypted and "
1306 "and compressed data."); 1319 "compressed data.");
1307 goto unm_err_out; 1320 goto unm_err_out;
1308 } 1321 }
1309 if (NInoMstProtected(ni)) { 1322 if (NInoMstProtected(ni)) {
@@ -1317,37 +1330,17 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1317 } 1330 }
1318 NInoSetEncrypted(ni); 1331 NInoSetEncrypted(ni);
1319 } 1332 }
1320 if (ctx->attr->flags & ATTR_IS_SPARSE) { 1333 if (a->data.non_resident.lowest_vcn) {
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 }
1332 if (ctx->attr->data.non_resident.lowest_vcn) {
1333 ntfs_error(vi->i_sb, "First extent of attribute has " 1334 ntfs_error(vi->i_sb, "First extent of attribute has "
1334 "non-zero lowest_vcn."); 1335 "non-zero lowest_vcn.");
1335 goto unm_err_out; 1336 goto unm_err_out;
1336 } 1337 }
1337 /* Setup all the sizes. */ 1338 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( 1339 ni->initialized_size = sle64_to_cpu(
1341 ctx->attr->data.non_resident.initialized_size); 1340 a->data.non_resident.initialized_size);
1342 ni->allocated_size = sle64_to_cpu( 1341 ni->allocated_size = sle64_to_cpu(
1343 ctx->attr->data.non_resident.allocated_size); 1342 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 } 1343 }
1350
1351 /* Setup the operations for this attribute inode. */ 1344 /* Setup the operations for this attribute inode. */
1352 vi->i_op = NULL; 1345 vi->i_op = NULL;
1353 vi->i_fop = NULL; 1346 vi->i_fop = NULL;
@@ -1355,12 +1348,10 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1355 vi->i_mapping->a_ops = &ntfs_mst_aops; 1348 vi->i_mapping->a_ops = &ntfs_mst_aops;
1356 else 1349 else
1357 vi->i_mapping->a_ops = &ntfs_aops; 1350 vi->i_mapping->a_ops = &ntfs_aops;
1358 1351 if (NInoCompressed(ni) || NInoSparse(ni))
1359 if (!NInoCompressed(ni))
1360 vi->i_blocks = ni->allocated_size >> 9;
1361 else
1362 vi->i_blocks = ni->itype.compressed.size >> 9; 1352 vi->i_blocks = ni->itype.compressed.size >> 9;
1363 1353 else
1354 vi->i_blocks = ni->allocated_size >> 9;
1364 /* 1355 /*
1365 * Make sure the base inode doesn't go away and attach it to the 1356 * Make sure the base inode doesn't go away and attach it to the
1366 * attribute inode. 1357 * attribute inode.
@@ -1429,10 +1420,12 @@ err_out:
1429 */ 1420 */
1430static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi) 1421static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1431{ 1422{
1423 loff_t bvi_size;
1432 ntfs_volume *vol = NTFS_SB(vi->i_sb); 1424 ntfs_volume *vol = NTFS_SB(vi->i_sb);
1433 ntfs_inode *ni, *base_ni, *bni; 1425 ntfs_inode *ni, *base_ni, *bni;
1434 struct inode *bvi; 1426 struct inode *bvi;
1435 MFT_RECORD *m; 1427 MFT_RECORD *m;
1428 ATTR_RECORD *a;
1436 ntfs_attr_search_ctx *ctx; 1429 ntfs_attr_search_ctx *ctx;
1437 INDEX_ROOT *ir; 1430 INDEX_ROOT *ir;
1438 u8 *ir_end, *index_end; 1431 u8 *ir_end, *index_end;
@@ -1474,30 +1467,28 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1474 "missing."); 1467 "missing.");
1475 goto unm_err_out; 1468 goto unm_err_out;
1476 } 1469 }
1470 a = ctx->attr;
1477 /* Set up the state. */ 1471 /* Set up the state. */
1478 if (unlikely(ctx->attr->non_resident)) { 1472 if (unlikely(a->non_resident)) {
1479 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident."); 1473 ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
1480 goto unm_err_out; 1474 goto unm_err_out;
1481 } 1475 }
1482 /* Ensure the attribute name is placed before the value. */ 1476 /* Ensure the attribute name is placed before the value. */
1483 if (unlikely(ctx->attr->name_length && 1477 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1484 (le16_to_cpu(ctx->attr->name_offset) >= 1478 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 " 1479 ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
1488 "after the attribute value."); 1480 "after the attribute value.");
1489 goto unm_err_out; 1481 goto unm_err_out;
1490 } 1482 }
1491 /* Compressed/encrypted/sparse index root is not allowed. */ 1483 /* Compressed/encrypted/sparse index root is not allowed. */
1492 if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED | 1484 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
1493 ATTR_IS_SPARSE)) { 1485 ATTR_IS_SPARSE)) {
1494 ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index " 1486 ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
1495 "root attribute."); 1487 "root attribute.");
1496 goto unm_err_out; 1488 goto unm_err_out;
1497 } 1489 }
1498 ir = (INDEX_ROOT*)((u8*)ctx->attr + 1490 ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset));
1499 le16_to_cpu(ctx->attr->data.resident.value_offset)); 1491 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) { 1492 if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
1502 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt."); 1493 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
1503 goto unm_err_out; 1494 goto unm_err_out;
@@ -1570,7 +1561,7 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1570 "$INDEX_ALLOCATION attribute."); 1561 "$INDEX_ALLOCATION attribute.");
1571 goto unm_err_out; 1562 goto unm_err_out;
1572 } 1563 }
1573 if (!ctx->attr->non_resident) { 1564 if (!a->non_resident) {
1574 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1565 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1575 "resident."); 1566 "resident.");
1576 goto unm_err_out; 1567 goto unm_err_out;
@@ -1578,37 +1569,36 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1578 /* 1569 /*
1579 * Ensure the attribute name is placed before the mapping pairs array. 1570 * Ensure the attribute name is placed before the mapping pairs array.
1580 */ 1571 */
1581 if (unlikely(ctx->attr->name_length && (le16_to_cpu( 1572 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
1582 ctx->attr->name_offset) >= le16_to_cpu( 1573 le16_to_cpu(
1583 ctx->attr->data.non_resident.mapping_pairs_offset)))) { 1574 a->data.non_resident.mapping_pairs_offset)))) {
1584 ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is " 1575 ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
1585 "placed after the mapping pairs array."); 1576 "placed after the mapping pairs array.");
1586 goto unm_err_out; 1577 goto unm_err_out;
1587 } 1578 }
1588 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) { 1579 if (a->flags & ATTR_IS_ENCRYPTED) {
1589 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1580 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1590 "encrypted."); 1581 "encrypted.");
1591 goto unm_err_out; 1582 goto unm_err_out;
1592 } 1583 }
1593 if (ctx->attr->flags & ATTR_IS_SPARSE) { 1584 if (a->flags & ATTR_IS_SPARSE) {
1594 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse."); 1585 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
1595 goto unm_err_out; 1586 goto unm_err_out;
1596 } 1587 }
1597 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) { 1588 if (a->flags & ATTR_COMPRESSION_MASK) {
1598 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1589 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1599 "compressed."); 1590 "compressed.");
1600 goto unm_err_out; 1591 goto unm_err_out;
1601 } 1592 }
1602 if (ctx->attr->data.non_resident.lowest_vcn) { 1593 if (a->data.non_resident.lowest_vcn) {
1603 ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION " 1594 ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
1604 "attribute has non zero lowest_vcn."); 1595 "attribute has non zero lowest_vcn.");
1605 goto unm_err_out; 1596 goto unm_err_out;
1606 } 1597 }
1607 vi->i_size = sle64_to_cpu(ctx->attr->data.non_resident.data_size); 1598 vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
1608 ni->initialized_size = sle64_to_cpu( 1599 ni->initialized_size = sle64_to_cpu(
1609 ctx->attr->data.non_resident.initialized_size); 1600 a->data.non_resident.initialized_size);
1610 ni->allocated_size = sle64_to_cpu( 1601 ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size);
1611 ctx->attr->data.non_resident.allocated_size);
1612 /* 1602 /*
1613 * We are done with the mft record, so we release it. Otherwise 1603 * We are done with the mft record, so we release it. Otherwise
1614 * we would deadlock in ntfs_attr_iget(). 1604 * we would deadlock in ntfs_attr_iget().
@@ -1632,10 +1622,10 @@ static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1632 goto iput_unm_err_out; 1622 goto iput_unm_err_out;
1633 } 1623 }
1634 /* Consistency check bitmap size vs. index allocation size. */ 1624 /* Consistency check bitmap size vs. index allocation size. */
1635 if ((bvi->i_size << 3) < (vi->i_size >> 1625 bvi_size = i_size_read(bvi);
1636 ni->itype.index.block_size_bits)) { 1626 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 " 1627 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
1638 "index allocation (0x%llx).", bvi->i_size << 3, 1628 "index allocation (0x%llx).", bvi_size << 3,
1639 vi->i_size); 1629 vi->i_size);
1640 goto iput_unm_err_out; 1630 goto iput_unm_err_out;
1641 } 1631 }
@@ -1646,7 +1636,6 @@ skip_large_index_stuff:
1646 vi->i_fop = NULL; 1636 vi->i_fop = NULL;
1647 vi->i_mapping->a_ops = &ntfs_mst_aops; 1637 vi->i_mapping->a_ops = &ntfs_mst_aops;
1648 vi->i_blocks = ni->allocated_size >> 9; 1638 vi->i_blocks = ni->allocated_size >> 9;
1649
1650 /* 1639 /*
1651 * Make sure the base inode doesn't go away and attach it to the 1640 * Make sure the base inode doesn't go away and attach it to the
1652 * index inode. 1641 * index inode.
@@ -1712,7 +1701,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1712 struct buffer_head *bh; 1701 struct buffer_head *bh;
1713 ntfs_inode *ni; 1702 ntfs_inode *ni;
1714 MFT_RECORD *m = NULL; 1703 MFT_RECORD *m = NULL;
1715 ATTR_RECORD *attr; 1704 ATTR_RECORD *a;
1716 ntfs_attr_search_ctx *ctx; 1705 ntfs_attr_search_ctx *ctx;
1717 unsigned int i, nr_blocks; 1706 unsigned int i, nr_blocks;
1718 int err; 1707 int err;
@@ -1727,10 +1716,10 @@ int ntfs_read_inode_mount(struct inode *vi)
1727 /* Setup the data attribute. It is special as it is mst protected. */ 1716 /* Setup the data attribute. It is special as it is mst protected. */
1728 NInoSetNonResident(ni); 1717 NInoSetNonResident(ni);
1729 NInoSetMstProtected(ni); 1718 NInoSetMstProtected(ni);
1719 NInoSetSparseDisabled(ni);
1730 ni->type = AT_DATA; 1720 ni->type = AT_DATA;
1731 ni->name = NULL; 1721 ni->name = NULL;
1732 ni->name_len = 0; 1722 ni->name_len = 0;
1733
1734 /* 1723 /*
1735 * This sets up our little cheat allowing us to reuse the async read io 1724 * This sets up our little cheat allowing us to reuse the async read io
1736 * completion handler for directories. 1725 * completion handler for directories.
@@ -1808,9 +1797,10 @@ int ntfs_read_inode_mount(struct inode *vi)
1808 1797
1809 ntfs_debug("Attribute list attribute found in $MFT."); 1798 ntfs_debug("Attribute list attribute found in $MFT.");
1810 NInoSetAttrList(ni); 1799 NInoSetAttrList(ni);
1811 if (ctx->attr->flags & ATTR_IS_ENCRYPTED || 1800 a = ctx->attr;
1812 ctx->attr->flags & ATTR_COMPRESSION_MASK || 1801 if (a->flags & ATTR_IS_ENCRYPTED ||
1813 ctx->attr->flags & ATTR_IS_SPARSE) { 1802 a->flags & ATTR_COMPRESSION_MASK ||
1803 a->flags & ATTR_IS_SPARSE) {
1814 ntfs_error(sb, "Attribute list attribute is " 1804 ntfs_error(sb, "Attribute list attribute is "
1815 "compressed/encrypted/sparse. Not " 1805 "compressed/encrypted/sparse. Not "
1816 "allowed. $MFT is corrupt. You should " 1806 "allowed. $MFT is corrupt. You should "
@@ -1818,16 +1808,16 @@ int ntfs_read_inode_mount(struct inode *vi)
1818 goto put_err_out; 1808 goto put_err_out;
1819 } 1809 }
1820 /* Now allocate memory for the attribute list. */ 1810 /* Now allocate memory for the attribute list. */
1821 ni->attr_list_size = (u32)ntfs_attr_size(ctx->attr); 1811 ni->attr_list_size = (u32)ntfs_attr_size(a);
1822 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 1812 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
1823 if (!ni->attr_list) { 1813 if (!ni->attr_list) {
1824 ntfs_error(sb, "Not enough memory to allocate buffer " 1814 ntfs_error(sb, "Not enough memory to allocate buffer "
1825 "for attribute list."); 1815 "for attribute list.");
1826 goto put_err_out; 1816 goto put_err_out;
1827 } 1817 }
1828 if (ctx->attr->non_resident) { 1818 if (a->non_resident) {
1829 NInoSetAttrListNonResident(ni); 1819 NInoSetAttrListNonResident(ni);
1830 if (ctx->attr->data.non_resident.lowest_vcn) { 1820 if (a->data.non_resident.lowest_vcn) {
1831 ntfs_error(sb, "Attribute list has non zero " 1821 ntfs_error(sb, "Attribute list has non zero "
1832 "lowest_vcn. $MFT is corrupt. " 1822 "lowest_vcn. $MFT is corrupt. "
1833 "You should run chkdsk."); 1823 "You should run chkdsk.");
@@ -1835,7 +1825,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1835 } 1825 }
1836 /* Setup the runlist. */ 1826 /* Setup the runlist. */
1837 ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 1827 ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
1838 ctx->attr, NULL); 1828 a, NULL);
1839 if (IS_ERR(ni->attr_list_rl.rl)) { 1829 if (IS_ERR(ni->attr_list_rl.rl)) {
1840 err = PTR_ERR(ni->attr_list_rl.rl); 1830 err = PTR_ERR(ni->attr_list_rl.rl);
1841 ni->attr_list_rl.rl = NULL; 1831 ni->attr_list_rl.rl = NULL;
@@ -1847,7 +1837,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1847 /* Now load the attribute list. */ 1837 /* Now load the attribute list. */
1848 if ((err = load_attribute_list(vol, &ni->attr_list_rl, 1838 if ((err = load_attribute_list(vol, &ni->attr_list_rl,
1849 ni->attr_list, ni->attr_list_size, 1839 ni->attr_list, ni->attr_list_size,
1850 sle64_to_cpu(ctx->attr->data. 1840 sle64_to_cpu(a->data.
1851 non_resident.initialized_size)))) { 1841 non_resident.initialized_size)))) {
1852 ntfs_error(sb, "Failed to load attribute list " 1842 ntfs_error(sb, "Failed to load attribute list "
1853 "attribute with error code %i.", 1843 "attribute with error code %i.",
@@ -1855,20 +1845,20 @@ int ntfs_read_inode_mount(struct inode *vi)
1855 goto put_err_out; 1845 goto put_err_out;
1856 } 1846 }
1857 } else /* if (!ctx.attr->non_resident) */ { 1847 } else /* if (!ctx.attr->non_resident) */ {
1858 if ((u8*)ctx->attr + le16_to_cpu( 1848 if ((u8*)a + le16_to_cpu(
1859 ctx->attr->data.resident.value_offset) + 1849 a->data.resident.value_offset) +
1860 le32_to_cpu( 1850 le32_to_cpu(
1861 ctx->attr->data.resident.value_length) > 1851 a->data.resident.value_length) >
1862 (u8*)ctx->mrec + vol->mft_record_size) { 1852 (u8*)ctx->mrec + vol->mft_record_size) {
1863 ntfs_error(sb, "Corrupt attribute list " 1853 ntfs_error(sb, "Corrupt attribute list "
1864 "attribute."); 1854 "attribute.");
1865 goto put_err_out; 1855 goto put_err_out;
1866 } 1856 }
1867 /* Now copy the attribute list. */ 1857 /* Now copy the attribute list. */
1868 memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu( 1858 memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
1869 ctx->attr->data.resident.value_offset), 1859 a->data.resident.value_offset),
1870 le32_to_cpu( 1860 le32_to_cpu(
1871 ctx->attr->data.resident.value_length)); 1861 a->data.resident.value_length));
1872 } 1862 }
1873 /* The attribute list is now setup in memory. */ 1863 /* The attribute list is now setup in memory. */
1874 /* 1864 /*
@@ -1934,25 +1924,25 @@ int ntfs_read_inode_mount(struct inode *vi)
1934 ntfs_attr_reinit_search_ctx(ctx); 1924 ntfs_attr_reinit_search_ctx(ctx);
1935 1925
1936 /* Now load all attribute extents. */ 1926 /* Now load all attribute extents. */
1937 attr = NULL; 1927 a = NULL;
1938 next_vcn = last_vcn = highest_vcn = 0; 1928 next_vcn = last_vcn = highest_vcn = 0;
1939 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, 1929 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
1940 ctx))) { 1930 ctx))) {
1941 runlist_element *nrl; 1931 runlist_element *nrl;
1942 1932
1943 /* Cache the current attribute. */ 1933 /* Cache the current attribute. */
1944 attr = ctx->attr; 1934 a = ctx->attr;
1945 /* $MFT must be non-resident. */ 1935 /* $MFT must be non-resident. */
1946 if (!attr->non_resident) { 1936 if (!a->non_resident) {
1947 ntfs_error(sb, "$MFT must be non-resident but a " 1937 ntfs_error(sb, "$MFT must be non-resident but a "
1948 "resident extent was found. $MFT is " 1938 "resident extent was found. $MFT is "
1949 "corrupt. Run chkdsk."); 1939 "corrupt. Run chkdsk.");
1950 goto put_err_out; 1940 goto put_err_out;
1951 } 1941 }
1952 /* $MFT must be uncompressed and unencrypted. */ 1942 /* $MFT must be uncompressed and unencrypted. */
1953 if (attr->flags & ATTR_COMPRESSION_MASK || 1943 if (a->flags & ATTR_COMPRESSION_MASK ||
1954 attr->flags & ATTR_IS_ENCRYPTED || 1944 a->flags & ATTR_IS_ENCRYPTED ||
1955 attr->flags & ATTR_IS_SPARSE) { 1945 a->flags & ATTR_IS_SPARSE) {
1956 ntfs_error(sb, "$MFT must be uncompressed, " 1946 ntfs_error(sb, "$MFT must be uncompressed, "
1957 "non-sparse, and unencrypted but a " 1947 "non-sparse, and unencrypted but a "
1958 "compressed/sparse/encrypted extent " 1948 "compressed/sparse/encrypted extent "
@@ -1966,7 +1956,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1966 * as we have exclusive access to the inode at this time and we 1956 * as we have exclusive access to the inode at this time and we
1967 * are a mount in progress task, too. 1957 * are a mount in progress task, too.
1968 */ 1958 */
1969 nrl = ntfs_mapping_pairs_decompress(vol, attr, ni->runlist.rl); 1959 nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
1970 if (IS_ERR(nrl)) { 1960 if (IS_ERR(nrl)) {
1971 ntfs_error(sb, "ntfs_mapping_pairs_decompress() " 1961 ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
1972 "failed with error code %ld. $MFT is " 1962 "failed with error code %ld. $MFT is "
@@ -1977,7 +1967,7 @@ int ntfs_read_inode_mount(struct inode *vi)
1977 1967
1978 /* Are we in the first extent? */ 1968 /* Are we in the first extent? */
1979 if (!next_vcn) { 1969 if (!next_vcn) {
1980 if (attr->data.non_resident.lowest_vcn) { 1970 if (a->data.non_resident.lowest_vcn) {
1981 ntfs_error(sb, "First extent of $DATA " 1971 ntfs_error(sb, "First extent of $DATA "
1982 "attribute has non zero " 1972 "attribute has non zero "
1983 "lowest_vcn. $MFT is corrupt. " 1973 "lowest_vcn. $MFT is corrupt. "
@@ -1986,15 +1976,15 @@ int ntfs_read_inode_mount(struct inode *vi)
1986 } 1976 }
1987 /* Get the last vcn in the $DATA attribute. */ 1977 /* Get the last vcn in the $DATA attribute. */
1988 last_vcn = sle64_to_cpu( 1978 last_vcn = sle64_to_cpu(
1989 attr->data.non_resident.allocated_size) 1979 a->data.non_resident.allocated_size)
1990 >> vol->cluster_size_bits; 1980 >> vol->cluster_size_bits;
1991 /* Fill in the inode size. */ 1981 /* Fill in the inode size. */
1992 vi->i_size = sle64_to_cpu( 1982 vi->i_size = sle64_to_cpu(
1993 attr->data.non_resident.data_size); 1983 a->data.non_resident.data_size);
1994 ni->initialized_size = sle64_to_cpu(attr->data. 1984 ni->initialized_size = sle64_to_cpu(
1995 non_resident.initialized_size); 1985 a->data.non_resident.initialized_size);
1996 ni->allocated_size = sle64_to_cpu( 1986 ni->allocated_size = sle64_to_cpu(
1997 attr->data.non_resident.allocated_size); 1987 a->data.non_resident.allocated_size);
1998 /* 1988 /*
1999 * Verify the number of mft records does not exceed 1989 * Verify the number of mft records does not exceed
2000 * 2^32 - 1. 1990 * 2^32 - 1.
@@ -2051,7 +2041,7 @@ int ntfs_read_inode_mount(struct inode *vi)
2051 } 2041 }
2052 2042
2053 /* Get the lowest vcn for the next extent. */ 2043 /* Get the lowest vcn for the next extent. */
2054 highest_vcn = sle64_to_cpu(attr->data.non_resident.highest_vcn); 2044 highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
2055 next_vcn = highest_vcn + 1; 2045 next_vcn = highest_vcn + 1;
2056 2046
2057 /* Only one extent or error, which we catch below. */ 2047 /* Only one extent or error, which we catch below. */
@@ -2060,7 +2050,7 @@ int ntfs_read_inode_mount(struct inode *vi)
2060 2050
2061 /* Avoid endless loops due to corruption. */ 2051 /* Avoid endless loops due to corruption. */
2062 if (next_vcn < sle64_to_cpu( 2052 if (next_vcn < sle64_to_cpu(
2063 attr->data.non_resident.lowest_vcn)) { 2053 a->data.non_resident.lowest_vcn)) {
2064 ntfs_error(sb, "$MFT has corrupt attribute list " 2054 ntfs_error(sb, "$MFT has corrupt attribute list "
2065 "attribute. Run chkdsk."); 2055 "attribute. Run chkdsk.");
2066 goto put_err_out; 2056 goto put_err_out;
@@ -2071,7 +2061,7 @@ int ntfs_read_inode_mount(struct inode *vi)
2071 "$MFT is corrupt. Run chkdsk."); 2061 "$MFT is corrupt. Run chkdsk.");
2072 goto put_err_out; 2062 goto put_err_out;
2073 } 2063 }
2074 if (!attr) { 2064 if (!a) {
2075 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is " 2065 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
2076 "corrupt. Run chkdsk."); 2066 "corrupt. Run chkdsk.");
2077 goto put_err_out; 2067 goto put_err_out;
@@ -2275,6 +2265,8 @@ int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
2275 seq_printf(sf, ",case_sensitive"); 2265 seq_printf(sf, ",case_sensitive");
2276 if (NVolShowSystemFiles(vol)) 2266 if (NVolShowSystemFiles(vol))
2277 seq_printf(sf, ",show_sys_files"); 2267 seq_printf(sf, ",show_sys_files");
2268 if (!NVolSparseEnabled(vol))
2269 seq_printf(sf, ",disable_sparse");
2278 for (i = 0; on_errors_arr[i].val; i++) { 2270 for (i = 0; on_errors_arr[i].val; i++) {
2279 if (on_errors_arr[i].val & vol->on_errors) 2271 if (on_errors_arr[i].val & vol->on_errors)
2280 seq_printf(sf, ",errors=%s", on_errors_arr[i].str); 2272 seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
@@ -2311,6 +2303,7 @@ int ntfs_truncate(struct inode *vi)
2311 ntfs_volume *vol = ni->vol; 2303 ntfs_volume *vol = ni->vol;
2312 ntfs_attr_search_ctx *ctx; 2304 ntfs_attr_search_ctx *ctx;
2313 MFT_RECORD *m; 2305 MFT_RECORD *m;
2306 ATTR_RECORD *a;
2314 const char *te = " Leaving file length out of sync with i_size."; 2307 const char *te = " Leaving file length out of sync with i_size.";
2315 int err; 2308 int err;
2316 2309
@@ -2347,14 +2340,15 @@ int ntfs_truncate(struct inode *vi)
2347 vi->i_ino, err); 2340 vi->i_ino, err);
2348 goto err_out; 2341 goto err_out;
2349 } 2342 }
2343 a = ctx->attr;
2350 /* If the size has not changed there is nothing to do. */ 2344 /* If the size has not changed there is nothing to do. */
2351 if (ntfs_attr_size(ctx->attr) == i_size_read(vi)) 2345 if (ntfs_attr_size(a) == i_size_read(vi))
2352 goto done; 2346 goto done;
2353 // TODO: Implement the truncate... 2347 // TODO: Implement the truncate...
2354 ntfs_error(vi->i_sb, "Inode size has changed but this is not " 2348 ntfs_error(vi->i_sb, "Inode size has changed but this is not "
2355 "implemented yet. Resetting inode size to old value. " 2349 "implemented yet. Resetting inode size to old value. "
2356 " This is most likely a bug in the ntfs driver!"); 2350 " This is most likely a bug in the ntfs driver!");
2357 i_size_write(vi, ntfs_attr_size(ctx->attr)); 2351 i_size_write(vi, ntfs_attr_size(a));
2358done: 2352done:
2359 ntfs_attr_put_search_ctx(ctx); 2353 ntfs_attr_put_search_ctx(ctx);
2360 unmap_mft_record(ni); 2354 unmap_mft_record(ni);
@@ -2515,18 +2509,18 @@ int ntfs_write_inode(struct inode *vi, int sync)
2515 nt = utc2ntfs(vi->i_mtime); 2509 nt = utc2ntfs(vi->i_mtime);
2516 if (si->last_data_change_time != nt) { 2510 if (si->last_data_change_time != nt) {
2517 ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, " 2511 ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
2518 "new = 0x%llx", vi->i_ino, 2512 "new = 0x%llx", vi->i_ino, (long long)
2519 sle64_to_cpu(si->last_data_change_time), 2513 sle64_to_cpu(si->last_data_change_time),
2520 sle64_to_cpu(nt)); 2514 (long long)sle64_to_cpu(nt));
2521 si->last_data_change_time = nt; 2515 si->last_data_change_time = nt;
2522 modified = TRUE; 2516 modified = TRUE;
2523 } 2517 }
2524 nt = utc2ntfs(vi->i_ctime); 2518 nt = utc2ntfs(vi->i_ctime);
2525 if (si->last_mft_change_time != nt) { 2519 if (si->last_mft_change_time != nt) {
2526 ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, " 2520 ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
2527 "new = 0x%llx", vi->i_ino, 2521 "new = 0x%llx", vi->i_ino, (long long)
2528 sle64_to_cpu(si->last_mft_change_time), 2522 sle64_to_cpu(si->last_mft_change_time),
2529 sle64_to_cpu(nt)); 2523 (long long)sle64_to_cpu(nt));
2530 si->last_mft_change_time = nt; 2524 si->last_mft_change_time = nt;
2531 modified = TRUE; 2525 modified = TRUE;
2532 } 2526 }
@@ -2534,8 +2528,8 @@ int ntfs_write_inode(struct inode *vi, int sync)
2534 if (si->last_access_time != nt) { 2528 if (si->last_access_time != nt) {
2535 ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, " 2529 ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
2536 "new = 0x%llx", vi->i_ino, 2530 "new = 0x%llx", vi->i_ino,
2537 sle64_to_cpu(si->last_access_time), 2531 (long long)sle64_to_cpu(si->last_access_time),
2538 sle64_to_cpu(nt)); 2532 (long long)sle64_to_cpu(nt));
2539 si->last_access_time = nt; 2533 si->last_access_time = nt;
2540 modified = TRUE; 2534 modified = TRUE;
2541 } 2535 }
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index 99580455f2ed..6eb99777a722 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.
@@ -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..458cb541d4dd 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;
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c
index 23fd911078b1..7087b5b0e6ce 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
@@ -60,7 +60,7 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
60 if (rl->lcn < 0) 60 if (rl->lcn < 0)
61 continue; 61 continue;
62 err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->lcn, rl->length); 62 err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->lcn, rl->length);
63 if (unlikely(err && (!ret || ret == ENOMEM) && ret != err)) 63 if (unlikely(err && (!ret || ret == -ENOMEM) && ret != err))
64 ret = err; 64 ret = err;
65 } 65 }
66 ntfs_debug("Done."); 66 ntfs_debug("Done.");
@@ -140,6 +140,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; 140 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; 141 LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size;
142 s64 clusters; 142 s64 clusters;
143 loff_t i_size;
143 struct inode *lcnbmp_vi; 144 struct inode *lcnbmp_vi;
144 runlist_element *rl = NULL; 145 runlist_element *rl = NULL;
145 struct address_space *mapping; 146 struct address_space *mapping;
@@ -249,6 +250,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
249 clusters = count; 250 clusters = count;
250 rlpos = rlsize = 0; 251 rlpos = rlsize = 0;
251 mapping = lcnbmp_vi->i_mapping; 252 mapping = lcnbmp_vi->i_mapping;
253 i_size = i_size_read(lcnbmp_vi);
252 while (1) { 254 while (1) {
253 ntfs_debug("Start of outer while loop: done_zones 0x%x, " 255 ntfs_debug("Start of outer while loop: done_zones 0x%x, "
254 "search_zone %i, pass %i, zone_start 0x%llx, " 256 "search_zone %i, pass %i, zone_start 0x%llx, "
@@ -263,7 +265,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
263 last_read_pos = bmp_pos >> 3; 265 last_read_pos = bmp_pos >> 3;
264 ntfs_debug("last_read_pos 0x%llx.", 266 ntfs_debug("last_read_pos 0x%llx.",
265 (unsigned long long)last_read_pos); 267 (unsigned long long)last_read_pos);
266 if (last_read_pos > lcnbmp_vi->i_size) { 268 if (last_read_pos > i_size) {
267 ntfs_debug("End of attribute reached. " 269 ntfs_debug("End of attribute reached. "
268 "Skipping to zone_pass_done."); 270 "Skipping to zone_pass_done.");
269 goto zone_pass_done; 271 goto zone_pass_done;
@@ -287,8 +289,8 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
287 buf_size = last_read_pos & ~PAGE_CACHE_MASK; 289 buf_size = last_read_pos & ~PAGE_CACHE_MASK;
288 buf = page_address(page) + buf_size; 290 buf = page_address(page) + buf_size;
289 buf_size = PAGE_CACHE_SIZE - buf_size; 291 buf_size = PAGE_CACHE_SIZE - buf_size;
290 if (unlikely(last_read_pos + buf_size > lcnbmp_vi->i_size)) 292 if (unlikely(last_read_pos + buf_size > i_size))
291 buf_size = lcnbmp_vi->i_size - last_read_pos; 293 buf_size = i_size - last_read_pos;
292 buf_size <<= 3; 294 buf_size <<= 3;
293 lcn = bmp_pos & 7; 295 lcn = bmp_pos & 7;
294 bmp_pos &= ~7; 296 bmp_pos &= ~7;
@@ -691,7 +693,7 @@ switch_to_data1_zone: search_zone = 2;
691 if (zone == MFT_ZONE || mft_zone_size <= 0) { 693 if (zone == MFT_ZONE || mft_zone_size <= 0) {
692 ntfs_debug("No free clusters left, going to out."); 694 ntfs_debug("No free clusters left, going to out.");
693 /* Really no more space left on device. */ 695 /* Really no more space left on device. */
694 err = ENOSPC; 696 err = -ENOSPC;
695 goto out; 697 goto out;
696 } /* zone == DATA_ZONE && mft_zone_size > 0 */ 698 } /* zone == DATA_ZONE && mft_zone_size > 0 */
697 ntfs_debug("Shrinking mft zone."); 699 ntfs_debug("Shrinking mft zone.");
@@ -755,13 +757,13 @@ out:
755 if (rl) { 757 if (rl) {
756 int err2; 758 int err2;
757 759
758 if (err == ENOSPC) 760 if (err == -ENOSPC)
759 ntfs_debug("Not enough space to complete allocation, " 761 ntfs_debug("Not enough space to complete allocation, "
760 "err ENOSPC, first free lcn 0x%llx, " 762 "err -ENOSPC, first free lcn 0x%llx, "
761 "could allocate up to 0x%llx " 763 "could allocate up to 0x%llx "
762 "clusters.", 764 "clusters.",
763 (unsigned long long)rl[0].lcn, 765 (unsigned long long)rl[0].lcn,
764 (unsigned long long)count - clusters); 766 (unsigned long long)(count - clusters));
765 /* Deallocate all allocated clusters. */ 767 /* Deallocate all allocated clusters. */
766 ntfs_debug("Attempting rollback..."); 768 ntfs_debug("Attempting rollback...");
767 err2 = ntfs_cluster_free_from_rl_nolock(vol, rl); 769 err2 = ntfs_cluster_free_from_rl_nolock(vol, rl);
@@ -773,10 +775,10 @@ out:
773 } 775 }
774 /* Free the runlist. */ 776 /* Free the runlist. */
775 ntfs_free(rl); 777 ntfs_free(rl);
776 } else if (err == ENOSPC) 778 } else if (err == -ENOSPC)
777 ntfs_debug("No space left at all, err = ENOSPC, " 779 ntfs_debug("No space left at all, err = -ENOSPC, first free "
778 "first free lcn = 0x%llx.", 780 "lcn = 0x%llx.",
779 (unsigned long long)vol->data1_zone_pos); 781 (long long)vol->data1_zone_pos);
780 up_write(&vol->lcnbmp_lock); 782 up_write(&vol->lcnbmp_lock);
781 return ERR_PTR(err); 783 return ERR_PTR(err);
782} 784}
@@ -846,8 +848,8 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
846 848
847 total_freed = real_freed = 0; 849 total_freed = real_freed = 0;
848 850
849 /* This returns with ni->runlist locked for reading on success. */ 851 down_read(&ni->runlist.lock);
850 rl = ntfs_find_vcn(ni, start_vcn, FALSE); 852 rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, FALSE);
851 if (IS_ERR(rl)) { 853 if (IS_ERR(rl)) {
852 if (!is_rollback) 854 if (!is_rollback)
853 ntfs_error(vol->sb, "Failed to find first runlist " 855 ntfs_error(vol->sb, "Failed to find first runlist "
@@ -861,7 +863,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
861 ntfs_error(vol->sb, "First runlist element has " 863 ntfs_error(vol->sb, "First runlist element has "
862 "invalid lcn, aborting."); 864 "invalid lcn, aborting.");
863 err = -EIO; 865 err = -EIO;
864 goto unl_err_out; 866 goto err_out;
865 } 867 }
866 /* Find the starting cluster inside the run that needs freeing. */ 868 /* Find the starting cluster inside the run that needs freeing. */
867 delta = start_vcn - rl->vcn; 869 delta = start_vcn - rl->vcn;
@@ -879,7 +881,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
879 if (!is_rollback) 881 if (!is_rollback)
880 ntfs_error(vol->sb, "Failed to clear first run " 882 ntfs_error(vol->sb, "Failed to clear first run "
881 "(error %i), aborting.", err); 883 "(error %i), aborting.", err);
882 goto unl_err_out; 884 goto err_out;
883 } 885 }
884 /* We have freed @to_free real clusters. */ 886 /* We have freed @to_free real clusters. */
885 real_freed = to_free; 887 real_freed = to_free;
@@ -899,30 +901,15 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
899 if (unlikely(rl->lcn < LCN_HOLE)) { 901 if (unlikely(rl->lcn < LCN_HOLE)) {
900 VCN vcn; 902 VCN vcn;
901 903
902 /* 904 /* Attempt to map runlist. */
903 * Attempt to map runlist, dropping runlist lock for
904 * the duration.
905 */
906 vcn = rl->vcn; 905 vcn = rl->vcn;
907 up_read(&ni->runlist.lock); 906 rl = ntfs_attr_find_vcn_nolock(ni, vcn, FALSE);
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)) { 907 if (IS_ERR(rl)) {
923 err = PTR_ERR(rl); 908 err = PTR_ERR(rl);
924 if (!is_rollback) 909 if (!is_rollback)
925 ntfs_error(vol->sb, "Failed to find " 910 ntfs_error(vol->sb, "Failed to map "
911 "runlist fragment or "
912 "failed to find "
926 "subsequent runlist " 913 "subsequent runlist "
927 "element."); 914 "element.");
928 goto err_out; 915 goto err_out;
@@ -935,7 +922,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
935 (unsigned long long) 922 (unsigned long long)
936 rl->lcn); 923 rl->lcn);
937 err = -EIO; 924 err = -EIO;
938 goto unl_err_out; 925 goto err_out;
939 } 926 }
940 } 927 }
941 /* The number of clusters in this run that need freeing. */ 928 /* The number of clusters in this run that need freeing. */
@@ -951,7 +938,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
951 if (!is_rollback) 938 if (!is_rollback)
952 ntfs_error(vol->sb, "Failed to clear " 939 ntfs_error(vol->sb, "Failed to clear "
953 "subsequent run."); 940 "subsequent run.");
954 goto unl_err_out; 941 goto err_out;
955 } 942 }
956 /* We have freed @to_free real clusters. */ 943 /* We have freed @to_free real clusters. */
957 real_freed += to_free; 944 real_freed += to_free;
@@ -972,9 +959,8 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
972 /* We are done. Return the number of actually freed clusters. */ 959 /* We are done. Return the number of actually freed clusters. */
973 ntfs_debug("Done."); 960 ntfs_debug("Done.");
974 return real_freed; 961 return real_freed;
975unl_err_out:
976 up_read(&ni->runlist.lock);
977err_out: 962err_out:
963 up_read(&ni->runlist.lock);
978 if (is_rollback) 964 if (is_rollback)
979 return err; 965 return err;
980 /* If no real clusters were freed, no need to rollback. */ 966 /* If no real clusters were freed, no need to rollback. */
diff --git a/fs/ntfs/logfile.c b/fs/ntfs/logfile.c
index 5e280abafab3..e680dd0cdb64 100644
--- a/fs/ntfs/logfile.c
+++ b/fs/ntfs/logfile.c
@@ -443,7 +443,7 @@ BOOL ntfs_check_logfile(struct inode *log_vi)
443 /* An empty $LogFile must have been clean before it got emptied. */ 443 /* An empty $LogFile must have been clean before it got emptied. */
444 if (NVolLogFileEmpty(vol)) 444 if (NVolLogFileEmpty(vol))
445 goto is_empty; 445 goto is_empty;
446 size = log_vi->i_size; 446 size = i_size_read(log_vi);
447 /* Make sure the file doesn't exceed the maximum allowed size. */ 447 /* Make sure the file doesn't exceed the maximum allowed size. */
448 if (size > MaxLogFileSize) 448 if (size > MaxLogFileSize)
449 size = MaxLogFileSize; 449 size = MaxLogFileSize;
@@ -689,7 +689,8 @@ BOOL ntfs_empty_logfile(struct inode *log_vi)
689 if (!NVolLogFileEmpty(vol)) { 689 if (!NVolLogFileEmpty(vol)) {
690 int err; 690 int err;
691 691
692 err = ntfs_attr_set(NTFS_I(log_vi), 0, log_vi->i_size, 0xff); 692 err = ntfs_attr_set(NTFS_I(log_vi), 0, i_size_read(log_vi),
693 0xff);
693 if (unlikely(err)) { 694 if (unlikely(err)) {
694 ntfs_error(vol->sb, "Failed to fill $LogFile with " 695 ntfs_error(vol->sb, "Failed to fill $LogFile with "
695 "0xff bytes (error code %i).", err); 696 "0xff bytes (error code %i).", err);
diff --git a/fs/ntfs/mft.c b/fs/ntfs/mft.c
index dfa85ac2f8ba..61ce09f1b652 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;
@@ -1121,6 +1123,7 @@ static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
1121 ntfs_inode *base_ni) 1123 ntfs_inode *base_ni)
1122{ 1124{
1123 s64 pass_end, ll, data_pos, pass_start, ofs, bit; 1125 s64 pass_end, ll, data_pos, pass_start, ofs, bit;
1126 unsigned long flags;
1124 struct address_space *mftbmp_mapping; 1127 struct address_space *mftbmp_mapping;
1125 u8 *buf, *byte; 1128 u8 *buf, *byte;
1126 struct page *page; 1129 struct page *page;
@@ -1134,9 +1137,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 1137 * Set the end of the pass making sure we do not overflow the mft
1135 * bitmap. 1138 * bitmap.
1136 */ 1139 */
1140 read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
1137 pass_end = NTFS_I(vol->mft_ino)->allocated_size >> 1141 pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
1138 vol->mft_record_size_bits; 1142 vol->mft_record_size_bits;
1143 read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
1144 read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1139 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3; 1145 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
1146 read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1140 if (pass_end > ll) 1147 if (pass_end > ll)
1141 pass_end = ll; 1148 pass_end = ll;
1142 pass = 1; 1149 pass = 1;
@@ -1263,6 +1270,7 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1263{ 1270{
1264 LCN lcn; 1271 LCN lcn;
1265 s64 ll; 1272 s64 ll;
1273 unsigned long flags;
1266 struct page *page; 1274 struct page *page;
1267 ntfs_inode *mft_ni, *mftbmp_ni; 1275 ntfs_inode *mft_ni, *mftbmp_ni;
1268 runlist_element *rl, *rl2 = NULL; 1276 runlist_element *rl, *rl2 = NULL;
@@ -1284,17 +1292,20 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1284 /* 1292 /*
1285 * Determine the last lcn of the mft bitmap. The allocated size of the 1293 * 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. 1294 * mft bitmap cannot be zero so we are ok to do this.
1287 * ntfs_find_vcn() returns the runlist locked on success.
1288 */ 1295 */
1289 rl = ntfs_find_vcn(mftbmp_ni, (mftbmp_ni->allocated_size - 1) >> 1296 down_write(&mftbmp_ni->runlist.lock);
1290 vol->cluster_size_bits, TRUE); 1297 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1298 ll = mftbmp_ni->allocated_size;
1299 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1300 rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
1301 (ll - 1) >> vol->cluster_size_bits, TRUE);
1291 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) { 1302 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
1303 up_write(&mftbmp_ni->runlist.lock);
1292 ntfs_error(vol->sb, "Failed to determine last allocated " 1304 ntfs_error(vol->sb, "Failed to determine last allocated "
1293 "cluster of mft bitmap attribute."); 1305 "cluster of mft bitmap attribute.");
1294 if (!IS_ERR(rl)) { 1306 if (!IS_ERR(rl))
1295 up_write(&mftbmp_ni->runlist.lock);
1296 ret = -EIO; 1307 ret = -EIO;
1297 } else 1308 else
1298 ret = PTR_ERR(rl); 1309 ret = PTR_ERR(rl);
1299 return ret; 1310 return ret;
1300 } 1311 }
@@ -1418,6 +1429,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 1429 // 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 1430 // record or by starting a new extent in a new mft record or by
1420 // moving other attributes out of this mft record. 1431 // moving other attributes out of this mft record.
1432 // Note: It will need to be a special mft record and if none of
1433 // those are available it gets rather complicated...
1421 ntfs_error(vol->sb, "Not enough space in this mft record to " 1434 ntfs_error(vol->sb, "Not enough space in this mft record to "
1422 "accomodate extended mft bitmap attribute " 1435 "accomodate extended mft bitmap attribute "
1423 "extent. Cannot handle this yet."); 1436 "extent. Cannot handle this yet.");
@@ -1458,9 +1471,11 @@ static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1458 } 1471 }
1459 a = ctx->attr; 1472 a = ctx->attr;
1460 } 1473 }
1474 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1461 mftbmp_ni->allocated_size += vol->cluster_size; 1475 mftbmp_ni->allocated_size += vol->cluster_size;
1462 a->data.non_resident.allocated_size = 1476 a->data.non_resident.allocated_size =
1463 cpu_to_sle64(mftbmp_ni->allocated_size); 1477 cpu_to_sle64(mftbmp_ni->allocated_size);
1478 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1464 /* Ensure the changes make it to disk. */ 1479 /* Ensure the changes make it to disk. */
1465 flush_dcache_mft_record_page(ctx->ntfs_ino); 1480 flush_dcache_mft_record_page(ctx->ntfs_ino);
1466 mark_mft_record_dirty(ctx->ntfs_ino); 1481 mark_mft_record_dirty(ctx->ntfs_ino);
@@ -1476,7 +1491,9 @@ restore_undo_alloc:
1476 0, ctx)) { 1491 0, ctx)) {
1477 ntfs_error(vol->sb, "Failed to find last attribute extent of " 1492 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1478 "mft bitmap attribute.%s", es); 1493 "mft bitmap attribute.%s", es);
1494 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1479 mftbmp_ni->allocated_size += vol->cluster_size; 1495 mftbmp_ni->allocated_size += vol->cluster_size;
1496 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1480 ntfs_attr_put_search_ctx(ctx); 1497 ntfs_attr_put_search_ctx(ctx);
1481 unmap_mft_record(mft_ni); 1498 unmap_mft_record(mft_ni);
1482 up_write(&mftbmp_ni->runlist.lock); 1499 up_write(&mftbmp_ni->runlist.lock);
@@ -1550,6 +1567,7 @@ undo_alloc:
1550static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol) 1567static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1551{ 1568{
1552 s64 old_data_size, old_initialized_size; 1569 s64 old_data_size, old_initialized_size;
1570 unsigned long flags;
1553 struct inode *mftbmp_vi; 1571 struct inode *mftbmp_vi;
1554 ntfs_inode *mft_ni, *mftbmp_ni; 1572 ntfs_inode *mft_ni, *mftbmp_ni;
1555 ntfs_attr_search_ctx *ctx; 1573 ntfs_attr_search_ctx *ctx;
@@ -1583,7 +1601,8 @@ static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1583 goto put_err_out; 1601 goto put_err_out;
1584 } 1602 }
1585 a = ctx->attr; 1603 a = ctx->attr;
1586 old_data_size = mftbmp_vi->i_size; 1604 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1605 old_data_size = i_size_read(mftbmp_vi);
1587 old_initialized_size = mftbmp_ni->initialized_size; 1606 old_initialized_size = mftbmp_ni->initialized_size;
1588 /* 1607 /*
1589 * We can simply update the initialized_size before filling the space 1608 * We can simply update the initialized_size before filling the space
@@ -1593,11 +1612,12 @@ static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1593 mftbmp_ni->initialized_size += 8; 1612 mftbmp_ni->initialized_size += 8;
1594 a->data.non_resident.initialized_size = 1613 a->data.non_resident.initialized_size =
1595 cpu_to_sle64(mftbmp_ni->initialized_size); 1614 cpu_to_sle64(mftbmp_ni->initialized_size);
1596 if (mftbmp_ni->initialized_size > mftbmp_vi->i_size) { 1615 if (mftbmp_ni->initialized_size > old_data_size) {
1597 mftbmp_vi->i_size = mftbmp_ni->initialized_size; 1616 i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
1598 a->data.non_resident.data_size = 1617 a->data.non_resident.data_size =
1599 cpu_to_sle64(mftbmp_vi->i_size); 1618 cpu_to_sle64(mftbmp_ni->initialized_size);
1600 } 1619 }
1620 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1601 /* Ensure the changes make it to disk. */ 1621 /* Ensure the changes make it to disk. */
1602 flush_dcache_mft_record_page(ctx->ntfs_ino); 1622 flush_dcache_mft_record_page(ctx->ntfs_ino);
1603 mark_mft_record_dirty(ctx->ntfs_ino); 1623 mark_mft_record_dirty(ctx->ntfs_ino);
@@ -1636,22 +1656,28 @@ unm_err_out:
1636 goto err_out; 1656 goto err_out;
1637 } 1657 }
1638 a = ctx->attr; 1658 a = ctx->attr;
1659 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1639 mftbmp_ni->initialized_size = old_initialized_size; 1660 mftbmp_ni->initialized_size = old_initialized_size;
1640 a->data.non_resident.initialized_size = 1661 a->data.non_resident.initialized_size =
1641 cpu_to_sle64(old_initialized_size); 1662 cpu_to_sle64(old_initialized_size);
1642 if (mftbmp_vi->i_size != old_data_size) { 1663 if (i_size_read(mftbmp_vi) != old_data_size) {
1643 mftbmp_vi->i_size = old_data_size; 1664 i_size_write(mftbmp_vi, old_data_size);
1644 a->data.non_resident.data_size = cpu_to_sle64(old_data_size); 1665 a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
1645 } 1666 }
1667 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1646 flush_dcache_mft_record_page(ctx->ntfs_ino); 1668 flush_dcache_mft_record_page(ctx->ntfs_ino);
1647 mark_mft_record_dirty(ctx->ntfs_ino); 1669 mark_mft_record_dirty(ctx->ntfs_ino);
1648 ntfs_attr_put_search_ctx(ctx); 1670 ntfs_attr_put_search_ctx(ctx);
1649 unmap_mft_record(mft_ni); 1671 unmap_mft_record(mft_ni);
1672#ifdef DEBUG
1673 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1650 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, " 1674 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
1651 "data_size 0x%llx, initialized_size 0x%llx.", 1675 "data_size 0x%llx, initialized_size 0x%llx.",
1652 (long long)mftbmp_ni->allocated_size, 1676 (long long)mftbmp_ni->allocated_size,
1653 (long long)mftbmp_vi->i_size, 1677 (long long)i_size_read(mftbmp_vi),
1654 (long long)mftbmp_ni->initialized_size); 1678 (long long)mftbmp_ni->initialized_size);
1679 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1680#endif /* DEBUG */
1655err_out: 1681err_out:
1656 return ret; 1682 return ret;
1657} 1683}
@@ -1679,7 +1705,8 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1679{ 1705{
1680 LCN lcn; 1706 LCN lcn;
1681 VCN old_last_vcn; 1707 VCN old_last_vcn;
1682 s64 min_nr, nr, ll = 0; 1708 s64 min_nr, nr, ll;
1709 unsigned long flags;
1683 ntfs_inode *mft_ni; 1710 ntfs_inode *mft_ni;
1684 runlist_element *rl, *rl2; 1711 runlist_element *rl, *rl2;
1685 ntfs_attr_search_ctx *ctx = NULL; 1712 ntfs_attr_search_ctx *ctx = NULL;
@@ -1695,23 +1722,25 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1695 * Determine the preferred allocation location, i.e. the last lcn of 1722 * Determine the preferred allocation location, i.e. the last lcn of
1696 * the mft data attribute. The allocated size of the mft data 1723 * the mft data attribute. The allocated size of the mft data
1697 * attribute cannot be zero so we are ok to do this. 1724 * attribute cannot be zero so we are ok to do this.
1698 * ntfs_find_vcn() returns the runlist locked on success.
1699 */ 1725 */
1700 rl = ntfs_find_vcn(mft_ni, (mft_ni->allocated_size - 1) >> 1726 down_write(&mft_ni->runlist.lock);
1701 vol->cluster_size_bits, TRUE); 1727 read_lock_irqsave(&mft_ni->size_lock, flags);
1728 ll = mft_ni->allocated_size;
1729 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1730 rl = ntfs_attr_find_vcn_nolock(mft_ni,
1731 (ll - 1) >> vol->cluster_size_bits, TRUE);
1702 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) { 1732 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
1733 up_write(&mft_ni->runlist.lock);
1703 ntfs_error(vol->sb, "Failed to determine last allocated " 1734 ntfs_error(vol->sb, "Failed to determine last allocated "
1704 "cluster of mft data attribute."); 1735 "cluster of mft data attribute.");
1705 if (!IS_ERR(rl)) { 1736 if (!IS_ERR(rl))
1706 up_write(&mft_ni->runlist.lock);
1707 ret = -EIO; 1737 ret = -EIO;
1708 } else 1738 else
1709 ret = PTR_ERR(rl); 1739 ret = PTR_ERR(rl);
1710 return ret; 1740 return ret;
1711 } 1741 }
1712 lcn = rl->lcn + rl->length; 1742 lcn = rl->lcn + rl->length;
1713 ntfs_debug("Last lcn of mft data attribute is 0x%llx.", 1743 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. */ 1744 /* Minimum allocation is one mft record worth of clusters. */
1716 min_nr = vol->mft_record_size >> vol->cluster_size_bits; 1745 min_nr = vol->mft_record_size >> vol->cluster_size_bits;
1717 if (!min_nr) 1746 if (!min_nr)
@@ -1721,12 +1750,13 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1721 if (!nr) 1750 if (!nr)
1722 nr = min_nr; 1751 nr = min_nr;
1723 /* Ensure we do not go above 2^32-1 mft records. */ 1752 /* Ensure we do not go above 2^32-1 mft records. */
1724 if (unlikely((mft_ni->allocated_size + 1753 read_lock_irqsave(&mft_ni->size_lock, flags);
1725 (nr << vol->cluster_size_bits)) >> 1754 ll = mft_ni->allocated_size;
1755 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1756 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1726 vol->mft_record_size_bits >= (1ll << 32))) { 1757 vol->mft_record_size_bits >= (1ll << 32))) {
1727 nr = min_nr; 1758 nr = min_nr;
1728 if (unlikely((mft_ni->allocated_size + 1759 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1729 (nr << vol->cluster_size_bits)) >>
1730 vol->mft_record_size_bits >= (1ll << 32))) { 1760 vol->mft_record_size_bits >= (1ll << 32))) {
1731 ntfs_warning(vol->sb, "Cannot allocate mft record " 1761 ntfs_warning(vol->sb, "Cannot allocate mft record "
1732 "because the maximum number of inodes " 1762 "because the maximum number of inodes "
@@ -1772,7 +1802,7 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1772 return PTR_ERR(rl); 1802 return PTR_ERR(rl);
1773 } 1803 }
1774 mft_ni->runlist.rl = rl; 1804 mft_ni->runlist.rl = rl;
1775 ntfs_debug("Allocated %lli clusters.", nr); 1805 ntfs_debug("Allocated %lli clusters.", (long long)nr);
1776 /* Find the last run in the new runlist. */ 1806 /* Find the last run in the new runlist. */
1777 for (; rl[1].length; rl++) 1807 for (; rl[1].length; rl++)
1778 ; 1808 ;
@@ -1832,7 +1862,11 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1832 // moving other attributes out of this mft record. 1862 // moving other attributes out of this mft record.
1833 // Note: Use the special reserved mft records and ensure that 1863 // Note: Use the special reserved mft records and ensure that
1834 // this extent is not required to find the mft record in 1864 // this extent is not required to find the mft record in
1835 // question. 1865 // question. If no free special records left we would need to
1866 // move an existing record away, insert ours in its place, and
1867 // then place the moved record into the newly allocated space
1868 // and we would then need to update all references to this mft
1869 // record appropriately. This is rather complicated...
1836 ntfs_error(vol->sb, "Not enough space in this mft record to " 1870 ntfs_error(vol->sb, "Not enough space in this mft record to "
1837 "accomodate extended mft data attribute " 1871 "accomodate extended mft data attribute "
1838 "extent. Cannot handle this yet."); 1872 "extent. Cannot handle this yet.");
@@ -1875,9 +1909,11 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1875 } 1909 }
1876 a = ctx->attr; 1910 a = ctx->attr;
1877 } 1911 }
1912 write_lock_irqsave(&mft_ni->size_lock, flags);
1878 mft_ni->allocated_size += nr << vol->cluster_size_bits; 1913 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1879 a->data.non_resident.allocated_size = 1914 a->data.non_resident.allocated_size =
1880 cpu_to_sle64(mft_ni->allocated_size); 1915 cpu_to_sle64(mft_ni->allocated_size);
1916 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1881 /* Ensure the changes make it to disk. */ 1917 /* Ensure the changes make it to disk. */
1882 flush_dcache_mft_record_page(ctx->ntfs_ino); 1918 flush_dcache_mft_record_page(ctx->ntfs_ino);
1883 mark_mft_record_dirty(ctx->ntfs_ino); 1919 mark_mft_record_dirty(ctx->ntfs_ino);
@@ -1892,7 +1928,9 @@ restore_undo_alloc:
1892 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) { 1928 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
1893 ntfs_error(vol->sb, "Failed to find last attribute extent of " 1929 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1894 "mft data attribute.%s", es); 1930 "mft data attribute.%s", es);
1931 write_lock_irqsave(&mft_ni->size_lock, flags);
1895 mft_ni->allocated_size += nr << vol->cluster_size_bits; 1932 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1933 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1896 ntfs_attr_put_search_ctx(ctx); 1934 ntfs_attr_put_search_ctx(ctx);
1897 unmap_mft_record(mft_ni); 1935 unmap_mft_record(mft_ni);
1898 up_write(&mft_ni->runlist.lock); 1936 up_write(&mft_ni->runlist.lock);
@@ -1991,7 +2029,7 @@ static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
1991 "reports this as corruption, please email " 2029 "reports this as corruption, please email "
1992 "linux-ntfs-dev@lists.sourceforge.net stating " 2030 "linux-ntfs-dev@lists.sourceforge.net stating "
1993 "that you saw this message and that the " 2031 "that you saw this message and that the "
1994 "modified file system created was corrupt. " 2032 "modified filesystem created was corrupt. "
1995 "Thank you."); 2033 "Thank you.");
1996 } 2034 }
1997 /* Set the update sequence number to 1. */ 2035 /* Set the update sequence number to 1. */
@@ -2036,6 +2074,7 @@ static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
2036 */ 2074 */
2037static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no) 2075static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
2038{ 2076{
2077 loff_t i_size;
2039 struct inode *mft_vi = vol->mft_ino; 2078 struct inode *mft_vi = vol->mft_ino;
2040 struct page *page; 2079 struct page *page;
2041 MFT_RECORD *m; 2080 MFT_RECORD *m;
@@ -2051,10 +2090,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; 2090 index = mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
2052 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK; 2091 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
2053 /* The maximum valid index into the page cache for $MFT's data. */ 2092 /* The maximum valid index into the page cache for $MFT's data. */
2054 end_index = mft_vi->i_size >> PAGE_CACHE_SHIFT; 2093 i_size = i_size_read(mft_vi);
2094 end_index = i_size >> PAGE_CACHE_SHIFT;
2055 if (unlikely(index >= end_index)) { 2095 if (unlikely(index >= end_index)) {
2056 if (unlikely(index > end_index || ofs + vol->mft_record_size >= 2096 if (unlikely(index > end_index || ofs + vol->mft_record_size >=
2057 (mft_vi->i_size & ~PAGE_CACHE_MASK))) { 2097 (i_size & ~PAGE_CACHE_MASK))) {
2058 ntfs_error(vol->sb, "Tried to format non-existing mft " 2098 ntfs_error(vol->sb, "Tried to format non-existing mft "
2059 "record 0x%llx.", (long long)mft_no); 2099 "record 0x%llx.", (long long)mft_no);
2060 return -ENOENT; 2100 return -ENOENT;
@@ -2188,6 +2228,7 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2188 ntfs_inode *base_ni, MFT_RECORD **mrec) 2228 ntfs_inode *base_ni, MFT_RECORD **mrec)
2189{ 2229{
2190 s64 ll, bit, old_data_initialized, old_data_size; 2230 s64 ll, bit, old_data_initialized, old_data_size;
2231 unsigned long flags;
2191 struct inode *vi; 2232 struct inode *vi;
2192 struct page *page; 2233 struct page *page;
2193 ntfs_inode *mft_ni, *mftbmp_ni, *ni; 2234 ntfs_inode *mft_ni, *mftbmp_ni, *ni;
@@ -2237,9 +2278,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 2278 * the first 24 mft records as they are special and whilst they may not
2238 * be in use, we do not allocate from them. 2279 * be in use, we do not allocate from them.
2239 */ 2280 */
2281 read_lock_irqsave(&mft_ni->size_lock, flags);
2240 ll = mft_ni->initialized_size >> vol->mft_record_size_bits; 2282 ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
2241 if (mftbmp_ni->initialized_size << 3 > ll && 2283 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2242 mftbmp_ni->initialized_size > 3) { 2284 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2285 old_data_initialized = mftbmp_ni->initialized_size;
2286 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2287 if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
2243 bit = ll; 2288 bit = ll;
2244 if (bit < 24) 2289 if (bit < 24)
2245 bit = 24; 2290 bit = 24;
@@ -2254,15 +2299,18 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2254 * mft record that we can allocate. 2299 * mft record that we can allocate.
2255 * Note: The smallest mft record we allocate is mft record 24. 2300 * Note: The smallest mft record we allocate is mft record 24.
2256 */ 2301 */
2257 bit = mftbmp_ni->initialized_size << 3; 2302 bit = old_data_initialized << 3;
2258 if (unlikely(bit >= (1ll << 32))) 2303 if (unlikely(bit >= (1ll << 32)))
2259 goto max_err_out; 2304 goto max_err_out;
2305 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2306 old_data_size = mftbmp_ni->allocated_size;
2260 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, " 2307 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
2261 "data_size 0x%llx, initialized_size 0x%llx.", 2308 "data_size 0x%llx, initialized_size 0x%llx.",
2262 (long long)mftbmp_ni->allocated_size, 2309 (long long)old_data_size,
2263 (long long)vol->mftbmp_ino->i_size, 2310 (long long)i_size_read(vol->mftbmp_ino),
2264 (long long)mftbmp_ni->initialized_size); 2311 (long long)old_data_initialized);
2265 if (mftbmp_ni->initialized_size + 8 > mftbmp_ni->allocated_size) { 2312 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2313 if (old_data_initialized + 8 > old_data_size) {
2266 /* Need to extend bitmap by one more cluster. */ 2314 /* Need to extend bitmap by one more cluster. */
2267 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size."); 2315 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
2268 err = ntfs_mft_bitmap_extend_allocation_nolock(vol); 2316 err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
@@ -2270,12 +2318,16 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2270 up_write(&vol->mftbmp_lock); 2318 up_write(&vol->mftbmp_lock);
2271 goto err_out; 2319 goto err_out;
2272 } 2320 }
2321#ifdef DEBUG
2322 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2273 ntfs_debug("Status of mftbmp after allocation extension: " 2323 ntfs_debug("Status of mftbmp after allocation extension: "
2274 "allocated_size 0x%llx, data_size 0x%llx, " 2324 "allocated_size 0x%llx, data_size 0x%llx, "
2275 "initialized_size 0x%llx.", 2325 "initialized_size 0x%llx.",
2276 (long long)mftbmp_ni->allocated_size, 2326 (long long)mftbmp_ni->allocated_size,
2277 (long long)vol->mftbmp_ino->i_size, 2327 (long long)i_size_read(vol->mftbmp_ino),
2278 (long long)mftbmp_ni->initialized_size); 2328 (long long)mftbmp_ni->initialized_size);
2329 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2330#endif /* DEBUG */
2279 } 2331 }
2280 /* 2332 /*
2281 * We now have sufficient allocated space, extend the initialized_size 2333 * We now have sufficient allocated space, extend the initialized_size
@@ -2287,12 +2339,16 @@ ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2287 up_write(&vol->mftbmp_lock); 2339 up_write(&vol->mftbmp_lock);
2288 goto err_out; 2340 goto err_out;
2289 } 2341 }
2342#ifdef DEBUG
2343 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2290 ntfs_debug("Status of mftbmp after initialized extention: " 2344 ntfs_debug("Status of mftbmp after initialized extention: "
2291 "allocated_size 0x%llx, data_size 0x%llx, " 2345 "allocated_size 0x%llx, data_size 0x%llx, "
2292 "initialized_size 0x%llx.", 2346 "initialized_size 0x%llx.",
2293 (long long)mftbmp_ni->allocated_size, 2347 (long long)mftbmp_ni->allocated_size,
2294 (long long)vol->mftbmp_ino->i_size, 2348 (long long)i_size_read(vol->mftbmp_ino),
2295 (long long)mftbmp_ni->initialized_size); 2349 (long long)mftbmp_ni->initialized_size);
2350 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2351#endif /* DEBUG */
2296 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit); 2352 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
2297found_free_rec: 2353found_free_rec:
2298 /* @bit is the found free mft record, allocate it in the mft bitmap. */ 2354 /* @bit is the found free mft record, allocate it in the mft bitmap. */
@@ -2314,7 +2370,10 @@ have_alloc_rec:
2314 * parallel allocation could allocate the same mft record as this one. 2370 * parallel allocation could allocate the same mft record as this one.
2315 */ 2371 */
2316 ll = (bit + 1) << vol->mft_record_size_bits; 2372 ll = (bit + 1) << vol->mft_record_size_bits;
2317 if (ll <= mft_ni->initialized_size) { 2373 read_lock_irqsave(&mft_ni->size_lock, flags);
2374 old_data_initialized = mft_ni->initialized_size;
2375 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2376 if (ll <= old_data_initialized) {
2318 ntfs_debug("Allocated mft record already initialized."); 2377 ntfs_debug("Allocated mft record already initialized.");
2319 goto mft_rec_already_initialized; 2378 goto mft_rec_already_initialized;
2320 } 2379 }
@@ -2325,26 +2384,30 @@ have_alloc_rec:
2325 * actually traversed more than once when a freshly formatted volume is 2384 * actually traversed more than once when a freshly formatted volume is
2326 * first written to so it optimizes away nicely in the common case. 2385 * first written to so it optimizes away nicely in the common case.
2327 */ 2386 */
2387 read_lock_irqsave(&mft_ni->size_lock, flags);
2328 ntfs_debug("Status of mft data before extension: " 2388 ntfs_debug("Status of mft data before extension: "
2329 "allocated_size 0x%llx, data_size 0x%llx, " 2389 "allocated_size 0x%llx, data_size 0x%llx, "
2330 "initialized_size 0x%llx.", 2390 "initialized_size 0x%llx.",
2331 (long long)mft_ni->allocated_size, 2391 (long long)mft_ni->allocated_size,
2332 (long long)vol->mft_ino->i_size, 2392 (long long)i_size_read(vol->mft_ino),
2333 (long long)mft_ni->initialized_size); 2393 (long long)mft_ni->initialized_size);
2334 while (ll > mft_ni->allocated_size) { 2394 while (ll > mft_ni->allocated_size) {
2395 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2335 err = ntfs_mft_data_extend_allocation_nolock(vol); 2396 err = ntfs_mft_data_extend_allocation_nolock(vol);
2336 if (unlikely(err)) { 2397 if (unlikely(err)) {
2337 ntfs_error(vol->sb, "Failed to extend mft data " 2398 ntfs_error(vol->sb, "Failed to extend mft data "
2338 "allocation."); 2399 "allocation.");
2339 goto undo_mftbmp_alloc_nolock; 2400 goto undo_mftbmp_alloc_nolock;
2340 } 2401 }
2402 read_lock_irqsave(&mft_ni->size_lock, flags);
2341 ntfs_debug("Status of mft data after allocation extension: " 2403 ntfs_debug("Status of mft data after allocation extension: "
2342 "allocated_size 0x%llx, data_size 0x%llx, " 2404 "allocated_size 0x%llx, data_size 0x%llx, "
2343 "initialized_size 0x%llx.", 2405 "initialized_size 0x%llx.",
2344 (long long)mft_ni->allocated_size, 2406 (long long)mft_ni->allocated_size,
2345 (long long)vol->mft_ino->i_size, 2407 (long long)i_size_read(vol->mft_ino),
2346 (long long)mft_ni->initialized_size); 2408 (long long)mft_ni->initialized_size);
2347 } 2409 }
2410 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2348 /* 2411 /*
2349 * Extend mft data initialized size (and data size of course) to reach 2412 * Extend mft data initialized size (and data size of course) to reach
2350 * the allocated mft record, formatting the mft records allong the way. 2413 * the allocated mft record, formatting the mft records allong the way.
@@ -2352,6 +2415,7 @@ have_alloc_rec:
2352 * needed by ntfs_mft_record_format(). We will update the attribute 2415 * needed by ntfs_mft_record_format(). We will update the attribute
2353 * record itself in one fell swoop later on. 2416 * record itself in one fell swoop later on.
2354 */ 2417 */
2418 write_lock_irqsave(&mft_ni->size_lock, flags);
2355 old_data_initialized = mft_ni->initialized_size; 2419 old_data_initialized = mft_ni->initialized_size;
2356 old_data_size = vol->mft_ino->i_size; 2420 old_data_size = vol->mft_ino->i_size;
2357 while (ll > mft_ni->initialized_size) { 2421 while (ll > mft_ni->initialized_size) {
@@ -2360,8 +2424,9 @@ have_alloc_rec:
2360 new_initialized_size = mft_ni->initialized_size + 2424 new_initialized_size = mft_ni->initialized_size +
2361 vol->mft_record_size; 2425 vol->mft_record_size;
2362 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits; 2426 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
2363 if (new_initialized_size > vol->mft_ino->i_size) 2427 if (new_initialized_size > i_size_read(vol->mft_ino))
2364 vol->mft_ino->i_size = new_initialized_size; 2428 i_size_write(vol->mft_ino, new_initialized_size);
2429 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2365 ntfs_debug("Initializing mft record 0x%llx.", 2430 ntfs_debug("Initializing mft record 0x%llx.",
2366 (long long)mft_no); 2431 (long long)mft_no);
2367 err = ntfs_mft_record_format(vol, mft_no); 2432 err = ntfs_mft_record_format(vol, mft_no);
@@ -2369,8 +2434,10 @@ have_alloc_rec:
2369 ntfs_error(vol->sb, "Failed to format mft record."); 2434 ntfs_error(vol->sb, "Failed to format mft record.");
2370 goto undo_data_init; 2435 goto undo_data_init;
2371 } 2436 }
2437 write_lock_irqsave(&mft_ni->size_lock, flags);
2372 mft_ni->initialized_size = new_initialized_size; 2438 mft_ni->initialized_size = new_initialized_size;
2373 } 2439 }
2440 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2374 record_formatted = TRUE; 2441 record_formatted = TRUE;
2375 /* Update the mft data attribute record to reflect the new sizes. */ 2442 /* Update the mft data attribute record to reflect the new sizes. */
2376 m = map_mft_record(mft_ni); 2443 m = map_mft_record(mft_ni);
@@ -2396,22 +2463,27 @@ have_alloc_rec:
2396 goto undo_data_init; 2463 goto undo_data_init;
2397 } 2464 }
2398 a = ctx->attr; 2465 a = ctx->attr;
2466 read_lock_irqsave(&mft_ni->size_lock, flags);
2399 a->data.non_resident.initialized_size = 2467 a->data.non_resident.initialized_size =
2400 cpu_to_sle64(mft_ni->initialized_size); 2468 cpu_to_sle64(mft_ni->initialized_size);
2401 a->data.non_resident.data_size = cpu_to_sle64(vol->mft_ino->i_size); 2469 a->data.non_resident.data_size =
2470 cpu_to_sle64(i_size_read(vol->mft_ino));
2471 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2402 /* Ensure the changes make it to disk. */ 2472 /* Ensure the changes make it to disk. */
2403 flush_dcache_mft_record_page(ctx->ntfs_ino); 2473 flush_dcache_mft_record_page(ctx->ntfs_ino);
2404 mark_mft_record_dirty(ctx->ntfs_ino); 2474 mark_mft_record_dirty(ctx->ntfs_ino);
2405 ntfs_attr_put_search_ctx(ctx); 2475 ntfs_attr_put_search_ctx(ctx);
2406 unmap_mft_record(mft_ni); 2476 unmap_mft_record(mft_ni);
2477 read_lock_irqsave(&mft_ni->size_lock, flags);
2407 ntfs_debug("Status of mft data after mft record initialization: " 2478 ntfs_debug("Status of mft data after mft record initialization: "
2408 "allocated_size 0x%llx, data_size 0x%llx, " 2479 "allocated_size 0x%llx, data_size 0x%llx, "
2409 "initialized_size 0x%llx.", 2480 "initialized_size 0x%llx.",
2410 (long long)mft_ni->allocated_size, 2481 (long long)mft_ni->allocated_size,
2411 (long long)vol->mft_ino->i_size, 2482 (long long)i_size_read(vol->mft_ino),
2412 (long long)mft_ni->initialized_size); 2483 (long long)mft_ni->initialized_size);
2413 BUG_ON(vol->mft_ino->i_size > mft_ni->allocated_size); 2484 BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
2414 BUG_ON(mft_ni->initialized_size > vol->mft_ino->i_size); 2485 BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
2486 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2415mft_rec_already_initialized: 2487mft_rec_already_initialized:
2416 /* 2488 /*
2417 * We can finally drop the mft bitmap lock as the mft data attribute 2489 * We can finally drop the mft bitmap lock as the mft data attribute
@@ -2652,8 +2724,10 @@ mft_rec_already_initialized:
2652 *mrec = m; 2724 *mrec = m;
2653 return ni; 2725 return ni;
2654undo_data_init: 2726undo_data_init:
2727 write_lock_irqsave(&mft_ni->size_lock, flags);
2655 mft_ni->initialized_size = old_data_initialized; 2728 mft_ni->initialized_size = old_data_initialized;
2656 vol->mft_ino->i_size = old_data_size; 2729 i_size_write(vol->mft_ino, old_data_size);
2730 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2657 goto undo_mftbmp_alloc_nolock; 2731 goto undo_mftbmp_alloc_nolock;
2658undo_mftbmp_alloc: 2732undo_mftbmp_alloc:
2659 down_write(&vol->mftbmp_lock); 2733 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..396d767c2cab 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
@@ -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 *
@@ -113,8 +113,11 @@ static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
113 BUG_ON(!dst); 113 BUG_ON(!dst);
114 BUG_ON(!src); 114 BUG_ON(!src);
115 115
116 if ((dst->lcn < 0) || (src->lcn < 0)) /* Are we merging holes? */ 116 if ((dst->lcn < 0) || (src->lcn < 0)) { /* Are we merging holes? */
117 if (dst->lcn == LCN_HOLE && src->lcn == LCN_HOLE)
118 return TRUE;
117 return FALSE; 119 return FALSE;
120 }
118 if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */ 121 if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */
119 return FALSE; 122 return FALSE;
120 if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */ 123 if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */
@@ -855,30 +858,42 @@ mpa_err:
855 if (!attr->data.non_resident.lowest_vcn) { 858 if (!attr->data.non_resident.lowest_vcn) {
856 VCN max_cluster; 859 VCN max_cluster;
857 860
858 max_cluster = (sle64_to_cpu( 861 max_cluster = ((sle64_to_cpu(
859 attr->data.non_resident.allocated_size) + 862 attr->data.non_resident.allocated_size) +
860 vol->cluster_size - 1) >> 863 vol->cluster_size - 1) >>
861 vol->cluster_size_bits; 864 vol->cluster_size_bits) - 1;
862 /* 865 /*
863 * If there is a difference between the highest_vcn and the 866 * A highest_vcn of zero means this is a single extent
864 * highest cluster, the runlist is either corrupt or, more 867 * attribute so simply terminate the runlist with LCN_ENOENT).
865 * likely, there are more extents following this one.
866 */ 868 */
867 if (deltaxcn < --max_cluster) { 869 if (deltaxcn) {
868 ntfs_debug("More extents to follow; deltaxcn = 0x%llx, " 870 /*
869 "max_cluster = 0x%llx", 871 * If there is a difference between the highest_vcn and
870 (unsigned long long)deltaxcn, 872 * the highest cluster, the runlist is either corrupt
871 (unsigned long long)max_cluster); 873 * or, more likely, there are more extents following
872 rl[rlpos].vcn = vcn; 874 * this one.
873 vcn += rl[rlpos].length = max_cluster - deltaxcn; 875 */
874 rl[rlpos].lcn = LCN_RL_NOT_MAPPED; 876 if (deltaxcn < max_cluster) {
875 rlpos++; 877 ntfs_debug("More extents to follow; deltaxcn "
876 } else if (unlikely(deltaxcn > max_cluster)) { 878 "= 0x%llx, max_cluster = "
877 ntfs_error(vol->sb, "Corrupt attribute. deltaxcn = " 879 "0x%llx",
878 "0x%llx, max_cluster = 0x%llx", 880 (unsigned long long)deltaxcn,
879 (unsigned long long)deltaxcn, 881 (unsigned long long)
880 (unsigned long long)max_cluster); 882 max_cluster);
881 goto mpa_err; 883 rl[rlpos].vcn = vcn;
884 vcn += rl[rlpos].length = max_cluster -
885 deltaxcn;
886 rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
887 rlpos++;
888 } else if (unlikely(deltaxcn > max_cluster)) {
889 ntfs_error(vol->sb, "Corrupt attribute. "
890 "deltaxcn = 0x%llx, "
891 "max_cluster = 0x%llx",
892 (unsigned long long)deltaxcn,
893 (unsigned long long)
894 max_cluster);
895 goto mpa_err;
896 }
882 } 897 }
883 rl[rlpos].lcn = LCN_ENOENT; 898 rl[rlpos].lcn = LCN_ENOENT;
884 } else /* Not the base extent. There may be more extents to follow. */ 899 } else /* Not the base extent. There may be more extents to follow. */
@@ -918,17 +933,18 @@ err_out:
918 * 933 *
919 * It is up to the caller to serialize access to the runlist @rl. 934 * It is up to the caller to serialize access to the runlist @rl.
920 * 935 *
921 * Since lcns must be >= 0, we use negative return values with special meaning: 936 * Since lcns must be >= 0, we use negative return codes with special meaning:
922 * 937 *
923 * Return value Meaning / Description 938 * Return code Meaning / Description
924 * ================================================== 939 * ==================================================
925 * -1 = LCN_HOLE Hole / not allocated on disk. 940 * LCN_HOLE Hole / not allocated on disk.
926 * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been 941 * LCN_RL_NOT_MAPPED This is part of the runlist which has not been
927 * inserted into the runlist yet. 942 * inserted into the runlist yet.
928 * -3 = LCN_ENOENT There is no such vcn in the attribute. 943 * LCN_ENOENT There is no such vcn in the attribute.
929 * 944 *
930 * Locking: - The caller must have locked the runlist (for reading or writing). 945 * Locking: - The caller must have locked the runlist (for reading or writing).
931 * - This function does not touch the lock. 946 * - This function does not touch the lock, nor does it modify the
947 * runlist.
932 */ 948 */
933LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn) 949LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
934{ 950{
@@ -964,6 +980,39 @@ LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
964 return LCN_ENOENT; 980 return LCN_ENOENT;
965} 981}
966 982
983#ifdef NTFS_RW
984
985/**
986 * ntfs_rl_find_vcn_nolock - find a vcn in a runlist
987 * @rl: runlist to search
988 * @vcn: vcn to find
989 *
990 * Find the virtual cluster number @vcn in the runlist @rl and return the
991 * address of the runlist element containing the @vcn on success.
992 *
993 * Return NULL if @rl is NULL or @vcn is in an unmapped part/out of bounds of
994 * the runlist.
995 *
996 * Locking: The runlist must be locked on entry.
997 */
998runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, const VCN vcn)
999{
1000 BUG_ON(vcn < 0);
1001 if (unlikely(!rl || vcn < rl[0].vcn))
1002 return NULL;
1003 while (likely(rl->length)) {
1004 if (unlikely(vcn < rl[1].vcn)) {
1005 if (likely(rl->lcn >= LCN_HOLE))
1006 return rl;
1007 return NULL;
1008 }
1009 rl++;
1010 }
1011 if (likely(rl->lcn == LCN_ENOENT))
1012 return rl;
1013 return NULL;
1014}
1015
967/** 1016/**
968 * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number 1017 * 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 1018 * @n: number for which to get the number of bytes for
@@ -1436,3 +1485,5 @@ int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
1436 ntfs_debug("Done."); 1485 ntfs_debug("Done.");
1437 return 0; 1486 return 0;
1438} 1487}
1488
1489#endif /* NTFS_RW */
diff --git a/fs/ntfs/runlist.h b/fs/ntfs/runlist.h
index 7107fde59df9..cf5c1b44bea8 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,6 +78,11 @@ 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 start_vcn);
81 88
@@ -86,4 +93,6 @@ extern int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
86extern int ntfs_rl_truncate_nolock(const ntfs_volume *vol, 93extern int ntfs_rl_truncate_nolock(const ntfs_volume *vol,
87 runlist *const runlist, const s64 new_length); 94 runlist *const runlist, const s64 new_length);
88 95
96#endif /* NTFS_RW */
97
89#endif /* _LINUX_NTFS_RUNLIST_H */ 98#endif /* _LINUX_NTFS_RUNLIST_H */
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 212a3d0f2073..455cbe0a6296 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
@@ -38,10 +38,11 @@
38#include "debug.h" 38#include "debug.h"
39#include "index.h" 39#include "index.h"
40#include "aops.h" 40#include "aops.h"
41#include "layout.h"
41#include "malloc.h" 42#include "malloc.h"
42#include "ntfs.h" 43#include "ntfs.h"
43 44
44/* Number of mounted file systems which have compression enabled. */ 45/* Number of mounted filesystems which have compression enabled. */
45static unsigned long ntfs_nr_compression_users; 46static unsigned long ntfs_nr_compression_users;
46 47
47/* A global default upcase table and a corresponding reference count. */ 48/* A global default upcase table and a corresponding reference count. */
@@ -102,7 +103,7 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
102 gid_t gid = (gid_t)-1; 103 gid_t gid = (gid_t)-1;
103 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1; 104 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
104 int mft_zone_multiplier = -1, on_errors = -1; 105 int mft_zone_multiplier = -1, on_errors = -1;
105 int show_sys_files = -1, case_sensitive = -1; 106 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
106 struct nls_table *nls_map = NULL, *old_nls; 107 struct nls_table *nls_map = NULL, *old_nls;
107 108
108 /* I am lazy... (-8 */ 109 /* I am lazy... (-8 */
@@ -162,6 +163,7 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
162 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE) 163 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
163 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files) 164 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
164 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive) 165 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
166 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
165 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors, 167 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
166 on_errors_arr) 168 on_errors_arr)
167 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes")) 169 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
@@ -291,6 +293,21 @@ no_mount_options:
291 else 293 else
292 NVolClearCaseSensitive(vol); 294 NVolClearCaseSensitive(vol);
293 } 295 }
296 if (disable_sparse != -1) {
297 if (disable_sparse)
298 NVolClearSparseEnabled(vol);
299 else {
300 if (!NVolSparseEnabled(vol) &&
301 vol->major_ver && vol->major_ver < 3)
302 ntfs_warning(vol->sb, "Not enabling sparse "
303 "support due to NTFS volume "
304 "version %i.%i (need at least "
305 "version 3.0).", vol->major_ver,
306 vol->minor_ver);
307 else
308 NVolSetSparseEnabled(vol);
309 }
310 }
294 return TRUE; 311 return TRUE;
295needs_arg: 312needs_arg:
296 ntfs_error(vol->sb, "The %s option requires an argument.", p); 313 ntfs_error(vol->sb, "The %s option requires an argument.", p);
@@ -516,16 +533,19 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
516{ 533{
517 /* 534 /*
518 * Check that checksum == sum of u32 values from b to the checksum 535 * Check that checksum == sum of u32 values from b to the checksum
519 * field. If checksum is zero, no checking is done. 536 * field. If checksum is zero, no checking is done. We will work when
537 * the checksum test fails, since some utilities update the boot sector
538 * ignoring the checksum which leaves the checksum out-of-date. We
539 * report a warning if this is the case.
520 */ 540 */
521 if ((void*)b < (void*)&b->checksum && b->checksum) { 541 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
522 le32 *u; 542 le32 *u;
523 u32 i; 543 u32 i;
524 544
525 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u) 545 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
526 i += le32_to_cpup(u); 546 i += le32_to_cpup(u);
527 if (le32_to_cpu(b->checksum) != i) 547 if (le32_to_cpu(b->checksum) != i)
528 goto not_ntfs; 548 ntfs_warning(sb, "Invalid boot sector checksum.");
529 } 549 }
530 /* Check OEMidentifier is "NTFS " */ 550 /* Check OEMidentifier is "NTFS " */
531 if (b->oem_id != magicNTFS) 551 if (b->oem_id != magicNTFS)
@@ -541,9 +561,9 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
541 default: 561 default:
542 goto not_ntfs; 562 goto not_ntfs;
543 } 563 }
544 /* Check the cluster size is not above 65536 bytes. */ 564 /* Check the cluster size is not above the maximum (64kiB). */
545 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) * 565 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
546 b->bpb.sectors_per_cluster > 0x10000) 566 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
547 goto not_ntfs; 567 goto not_ntfs;
548 /* Check reserved/unused fields are really zero. */ 568 /* Check reserved/unused fields are really zero. */
549 if (le16_to_cpu(b->bpb.reserved_sectors) || 569 if (le16_to_cpu(b->bpb.reserved_sectors) ||
@@ -575,7 +595,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 595 * many BIOSes will refuse to boot from a bootsector if the magic is
576 * incorrect, so we emit a warning. 596 * incorrect, so we emit a warning.
577 */ 597 */
578 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55)) 598 if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
579 ntfs_warning(sb, "Invalid end of sector marker."); 599 ntfs_warning(sb, "Invalid end of sector marker.");
580 return TRUE; 600 return TRUE;
581not_ntfs: 601not_ntfs:
@@ -967,6 +987,7 @@ static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
967 tmp_ni = NTFS_I(tmp_ino); 987 tmp_ni = NTFS_I(tmp_ino);
968 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */ 988 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
969 NInoSetMstProtected(tmp_ni); 989 NInoSetMstProtected(tmp_ni);
990 NInoSetSparseDisabled(tmp_ni);
970 /* 991 /*
971 * Set up our little cheat allowing us to reuse the async read io 992 * Set up our little cheat allowing us to reuse the async read io
972 * completion handler for directories. 993 * completion handler for directories.
@@ -990,12 +1011,12 @@ static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
990 */ 1011 */
991static BOOL check_mft_mirror(ntfs_volume *vol) 1012static BOOL check_mft_mirror(ntfs_volume *vol)
992{ 1013{
993 unsigned long index;
994 struct super_block *sb = vol->sb; 1014 struct super_block *sb = vol->sb;
995 ntfs_inode *mirr_ni; 1015 ntfs_inode *mirr_ni;
996 struct page *mft_page, *mirr_page; 1016 struct page *mft_page, *mirr_page;
997 u8 *kmft, *kmirr; 1017 u8 *kmft, *kmirr;
998 runlist_element *rl, rl2[2]; 1018 runlist_element *rl, rl2[2];
1019 pgoff_t index;
999 int mrecs_per_page, i; 1020 int mrecs_per_page, i;
1000 1021
1001 ntfs_debug("Entering."); 1022 ntfs_debug("Entering.");
@@ -1122,6 +1143,7 @@ static BOOL load_and_check_logfile(ntfs_volume *vol)
1122 /* ntfs_check_logfile() will have displayed error output. */ 1143 /* ntfs_check_logfile() will have displayed error output. */
1123 return FALSE; 1144 return FALSE;
1124 } 1145 }
1146 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1125 vol->logfile_ino = tmp_ino; 1147 vol->logfile_ino = tmp_ino;
1126 ntfs_debug("Done."); 1148 ntfs_debug("Done.");
1127 return TRUE; 1149 return TRUE;
@@ -1175,8 +1197,7 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1175 return FALSE; 1197 return FALSE;
1176 } 1198 }
1177 /* We do not care for the type of match that was found. */ 1199 /* We do not care for the type of match that was found. */
1178 if (name) 1200 kfree(name);
1179 kfree(name);
1180 /* Get the inode. */ 1201 /* Get the inode. */
1181 tmp_ino = ntfs_iget(vol->sb, MREF(mref)); 1202 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1182 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) { 1203 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
@@ -1205,10 +1226,11 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1205 */ 1226 */
1206static BOOL load_and_init_attrdef(ntfs_volume *vol) 1227static BOOL load_and_init_attrdef(ntfs_volume *vol)
1207{ 1228{
1229 loff_t i_size;
1208 struct super_block *sb = vol->sb; 1230 struct super_block *sb = vol->sb;
1209 struct inode *ino; 1231 struct inode *ino;
1210 struct page *page; 1232 struct page *page;
1211 unsigned long index, max_index; 1233 pgoff_t index, max_index;
1212 unsigned int size; 1234 unsigned int size;
1213 1235
1214 ntfs_debug("Entering."); 1236 ntfs_debug("Entering.");
@@ -1219,14 +1241,16 @@ static BOOL load_and_init_attrdef(ntfs_volume *vol)
1219 iput(ino); 1241 iput(ino);
1220 goto failed; 1242 goto failed;
1221 } 1243 }
1244 NInoSetSparseDisabled(NTFS_I(ino));
1222 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */ 1245 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1223 if (!ino->i_size || ino->i_size > 0x7fffffff) 1246 i_size = i_size_read(ino);
1247 if (i_size <= 0 || i_size > 0x7fffffff)
1224 goto iput_failed; 1248 goto iput_failed;
1225 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(ino->i_size); 1249 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1226 if (!vol->attrdef) 1250 if (!vol->attrdef)
1227 goto iput_failed; 1251 goto iput_failed;
1228 index = 0; 1252 index = 0;
1229 max_index = ino->i_size >> PAGE_CACHE_SHIFT; 1253 max_index = i_size >> PAGE_CACHE_SHIFT;
1230 size = PAGE_CACHE_SIZE; 1254 size = PAGE_CACHE_SIZE;
1231 while (index < max_index) { 1255 while (index < max_index) {
1232 /* Read the attrdef table and copy it into the linear buffer. */ 1256 /* Read the attrdef table and copy it into the linear buffer. */
@@ -1239,12 +1263,12 @@ read_partial_attrdef_page:
1239 ntfs_unmap_page(page); 1263 ntfs_unmap_page(page);
1240 }; 1264 };
1241 if (size == PAGE_CACHE_SIZE) { 1265 if (size == PAGE_CACHE_SIZE) {
1242 size = ino->i_size & ~PAGE_CACHE_MASK; 1266 size = i_size & ~PAGE_CACHE_MASK;
1243 if (size) 1267 if (size)
1244 goto read_partial_attrdef_page; 1268 goto read_partial_attrdef_page;
1245 } 1269 }
1246 vol->attrdef_size = ino->i_size; 1270 vol->attrdef_size = i_size;
1247 ntfs_debug("Read %llu bytes from $AttrDef.", ino->i_size); 1271 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1248 iput(ino); 1272 iput(ino);
1249 return TRUE; 1273 return TRUE;
1250free_iput_failed: 1274free_iput_failed:
@@ -1267,10 +1291,11 @@ failed:
1267 */ 1291 */
1268static BOOL load_and_init_upcase(ntfs_volume *vol) 1292static BOOL load_and_init_upcase(ntfs_volume *vol)
1269{ 1293{
1294 loff_t i_size;
1270 struct super_block *sb = vol->sb; 1295 struct super_block *sb = vol->sb;
1271 struct inode *ino; 1296 struct inode *ino;
1272 struct page *page; 1297 struct page *page;
1273 unsigned long index, max_index; 1298 pgoff_t index, max_index;
1274 unsigned int size; 1299 unsigned int size;
1275 int i, max; 1300 int i, max;
1276 1301
@@ -1286,14 +1311,15 @@ static BOOL load_and_init_upcase(ntfs_volume *vol)
1286 * The upcase size must not be above 64k Unicode characters, must not 1311 * The upcase size must not be above 64k Unicode characters, must not
1287 * be zero and must be a multiple of sizeof(ntfschar). 1312 * be zero and must be a multiple of sizeof(ntfschar).
1288 */ 1313 */
1289 if (!ino->i_size || ino->i_size & (sizeof(ntfschar) - 1) || 1314 i_size = i_size_read(ino);
1290 ino->i_size > 64ULL * 1024 * sizeof(ntfschar)) 1315 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1316 i_size > 64ULL * 1024 * sizeof(ntfschar))
1291 goto iput_upcase_failed; 1317 goto iput_upcase_failed;
1292 vol->upcase = (ntfschar*)ntfs_malloc_nofs(ino->i_size); 1318 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1293 if (!vol->upcase) 1319 if (!vol->upcase)
1294 goto iput_upcase_failed; 1320 goto iput_upcase_failed;
1295 index = 0; 1321 index = 0;
1296 max_index = ino->i_size >> PAGE_CACHE_SHIFT; 1322 max_index = i_size >> PAGE_CACHE_SHIFT;
1297 size = PAGE_CACHE_SIZE; 1323 size = PAGE_CACHE_SIZE;
1298 while (index < max_index) { 1324 while (index < max_index) {
1299 /* Read the upcase table and copy it into the linear buffer. */ 1325 /* Read the upcase table and copy it into the linear buffer. */
@@ -1306,13 +1332,13 @@ read_partial_upcase_page:
1306 ntfs_unmap_page(page); 1332 ntfs_unmap_page(page);
1307 }; 1333 };
1308 if (size == PAGE_CACHE_SIZE) { 1334 if (size == PAGE_CACHE_SIZE) {
1309 size = ino->i_size & ~PAGE_CACHE_MASK; 1335 size = i_size & ~PAGE_CACHE_MASK;
1310 if (size) 1336 if (size)
1311 goto read_partial_upcase_page; 1337 goto read_partial_upcase_page;
1312 } 1338 }
1313 vol->upcase_len = ino->i_size >> UCHAR_T_SIZE_BITS; 1339 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1314 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).", 1340 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1315 ino->i_size, 64 * 1024 * sizeof(ntfschar)); 1341 i_size, 64 * 1024 * sizeof(ntfschar));
1316 iput(ino); 1342 iput(ino);
1317 down(&ntfs_lock); 1343 down(&ntfs_lock);
1318 if (!default_upcase) { 1344 if (!default_upcase) {
@@ -1435,7 +1461,8 @@ static BOOL load_system_files(ntfs_volume *vol)
1435 iput(vol->lcnbmp_ino); 1461 iput(vol->lcnbmp_ino);
1436 goto bitmap_failed; 1462 goto bitmap_failed;
1437 } 1463 }
1438 if ((vol->nr_clusters + 7) >> 3 > vol->lcnbmp_ino->i_size) { 1464 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1465 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1439 iput(vol->lcnbmp_ino); 1466 iput(vol->lcnbmp_ino);
1440bitmap_failed: 1467bitmap_failed:
1441 ntfs_error(sb, "Failed to load $Bitmap."); 1468 ntfs_error(sb, "Failed to load $Bitmap.");
@@ -1486,6 +1513,12 @@ get_ctx_vol_failed:
1486 unmap_mft_record(NTFS_I(vol->vol_ino)); 1513 unmap_mft_record(NTFS_I(vol->vol_ino));
1487 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver, 1514 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1488 vol->minor_ver); 1515 vol->minor_ver);
1516 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1517 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1518 "volume version %i.%i (need at least version "
1519 "3.0).", vol->major_ver, vol->minor_ver);
1520 NVolClearSparseEnabled(vol);
1521 }
1489#ifdef NTFS_RW 1522#ifdef NTFS_RW
1490 /* Make sure that no unsupported volume flags are set. */ 1523 /* Make sure that no unsupported volume flags are set. */
1491 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 1524 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
@@ -1959,8 +1992,7 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
1959 struct address_space *mapping = vol->lcnbmp_ino->i_mapping; 1992 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
1960 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 1993 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
1961 struct page *page; 1994 struct page *page;
1962 unsigned long index, max_index; 1995 pgoff_t index, max_index;
1963 unsigned int max_size;
1964 1996
1965 ntfs_debug("Entering."); 1997 ntfs_debug("Entering.");
1966 /* Serialize accesses to the cluster bitmap. */ 1998 /* Serialize accesses to the cluster bitmap. */
@@ -1972,11 +2004,10 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
1972 */ 2004 */
1973 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> 2005 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
1974 PAGE_CACHE_SHIFT; 2006 PAGE_CACHE_SHIFT;
1975 /* Use multiples of 4 bytes. */ 2007 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
1976 max_size = PAGE_CACHE_SIZE >> 2; 2008 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.", 2009 max_index, PAGE_CACHE_SIZE / 4);
1978 max_index, max_size); 2010 for (index = 0; index < max_index; index++) {
1979 for (index = 0UL; index < max_index; index++) {
1980 unsigned int i; 2011 unsigned int i;
1981 /* 2012 /*
1982 * Read the page from page cache, getting it from backing store 2013 * Read the page from page cache, getting it from backing store
@@ -2008,7 +2039,7 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2008 * the result as all out of range bytes are set to zero by 2039 * the result as all out of range bytes are set to zero by
2009 * ntfs_readpage(). 2040 * ntfs_readpage().
2010 */ 2041 */
2011 for (i = 0; i < max_size; i++) 2042 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2012 nr_free -= (s64)hweight32(kaddr[i]); 2043 nr_free -= (s64)hweight32(kaddr[i]);
2013 kunmap_atomic(kaddr, KM_USER0); 2044 kunmap_atomic(kaddr, KM_USER0);
2014 page_cache_release(page); 2045 page_cache_release(page);
@@ -2031,6 +2062,8 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2031/** 2062/**
2032 * __get_nr_free_mft_records - return the number of free inodes on a volume 2063 * __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 2064 * @vol: ntfs volume for which to obtain free inode count
2065 * @nr_free: number of mft records in filesystem
2066 * @max_index: maximum number of pages containing set bits
2034 * 2067 *
2035 * Calculate the number of free mft records (inodes) on the mounted NTFS 2068 * 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 2069 * volume @vol. We actually calculate the number of mft records in use instead
@@ -2043,32 +2076,20 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2043 * 2076 *
2044 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing. 2077 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2045 */ 2078 */
2046static unsigned long __get_nr_free_mft_records(ntfs_volume *vol) 2079static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2080 s64 nr_free, const pgoff_t max_index)
2047{ 2081{
2048 s64 nr_free;
2049 u32 *kaddr; 2082 u32 *kaddr;
2050 struct address_space *mapping = vol->mftbmp_ino->i_mapping; 2083 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2051 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2084 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2052 struct page *page; 2085 struct page *page;
2053 unsigned long index, max_index; 2086 pgoff_t index;
2054 unsigned int max_size;
2055 2087
2056 ntfs_debug("Entering."); 2088 ntfs_debug("Entering.");
2057 /* Number of mft records in file system (at this point in time). */ 2089 /* 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 = " 2090 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2070 "0x%x.", max_index, max_size); 2091 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2071 for (index = 0UL; index < max_index; index++) { 2092 for (index = 0; index < max_index; index++) {
2072 unsigned int i; 2093 unsigned int i;
2073 /* 2094 /*
2074 * Read the page from page cache, getting it from backing store 2095 * Read the page from page cache, getting it from backing store
@@ -2100,7 +2121,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 2121 * the result as all out of range bytes are set to zero by
2101 * ntfs_readpage(). 2122 * ntfs_readpage().
2102 */ 2123 */
2103 for (i = 0; i < max_size; i++) 2124 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2104 nr_free -= (s64)hweight32(kaddr[i]); 2125 nr_free -= (s64)hweight32(kaddr[i]);
2105 kunmap_atomic(kaddr, KM_USER0); 2126 kunmap_atomic(kaddr, KM_USER0);
2106 page_cache_release(page); 2127 page_cache_release(page);
@@ -2134,8 +2155,11 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
2134 */ 2155 */
2135static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs) 2156static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2136{ 2157{
2137 ntfs_volume *vol = NTFS_SB(sb);
2138 s64 size; 2158 s64 size;
2159 ntfs_volume *vol = NTFS_SB(sb);
2160 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2161 pgoff_t max_index;
2162 unsigned long flags;
2139 2163
2140 ntfs_debug("Entering."); 2164 ntfs_debug("Entering.");
2141 /* Type of filesystem. */ 2165 /* Type of filesystem. */
@@ -2143,13 +2167,13 @@ static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2143 /* Optimal transfer block size. */ 2167 /* Optimal transfer block size. */
2144 sfs->f_bsize = PAGE_CACHE_SIZE; 2168 sfs->f_bsize = PAGE_CACHE_SIZE;
2145 /* 2169 /*
2146 * Total data blocks in file system in units of f_bsize and since 2170 * 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 2171 * inodes are also stored in data blocs ($MFT is a file) this is just
2148 * the total clusters. 2172 * the total clusters.
2149 */ 2173 */
2150 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >> 2174 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2151 PAGE_CACHE_SHIFT; 2175 PAGE_CACHE_SHIFT;
2152 /* Free data blocks in file system in units of f_bsize. */ 2176 /* Free data blocks in filesystem in units of f_bsize. */
2153 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >> 2177 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2154 PAGE_CACHE_SHIFT; 2178 PAGE_CACHE_SHIFT;
2155 if (size < 0LL) 2179 if (size < 0LL)
@@ -2158,17 +2182,27 @@ static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2158 sfs->f_bavail = sfs->f_bfree = size; 2182 sfs->f_bavail = sfs->f_bfree = size;
2159 /* Serialize accesses to the inode bitmap. */ 2183 /* Serialize accesses to the inode bitmap. */
2160 down_read(&vol->mftbmp_lock); 2184 down_read(&vol->mftbmp_lock);
2161 /* Number of inodes in file system (at this point in time). */ 2185 read_lock_irqsave(&mft_ni->size_lock, flags);
2162 sfs->f_files = vol->mft_ino->i_size >> vol->mft_record_size_bits; 2186 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2187 /*
2188 * Convert the maximum number of set bits into bytes rounded up, then
2189 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2190 * have one full and one partial page max_index = 2.
2191 */
2192 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2193 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2194 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2195 /* Number of inodes in filesystem (at this point in time). */
2196 sfs->f_files = size;
2163 /* Free inodes in fs (based on current total count). */ 2197 /* Free inodes in fs (based on current total count). */
2164 sfs->f_ffree = __get_nr_free_mft_records(vol); 2198 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2165 up_read(&vol->mftbmp_lock); 2199 up_read(&vol->mftbmp_lock);
2166 /* 2200 /*
2167 * File system id. This is extremely *nix flavour dependent and even 2201 * 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 2202 * 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 2203 * 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 2204 * 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 2205 * 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 2206 * 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 2207 * the least significant 32-bits in f_fsid[0] and the most significant
2174 * 32-bits in f_fsid[1]. 2208 * 32-bits in f_fsid[1].
@@ -2219,53 +2253,19 @@ static struct super_operations ntfs_sops = {
2219 proc. */ 2253 proc. */
2220}; 2254};
2221 2255
2222
2223/** 2256/**
2224 * Declarations for NTFS specific export operations (fs/ntfs/namei.c). 2257 * ntfs_fill_super - mount an ntfs filesystem
2225 */ 2258 * @sb: super block of ntfs filesystem to mount
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/**
2257 * ntfs_fill_super - mount an ntfs files system
2258 * @sb: super block of ntfs file system to mount
2259 * @opt: string containing the mount options 2259 * @opt: string containing the mount options
2260 * @silent: silence error output 2260 * @silent: silence error output
2261 * 2261 *
2262 * ntfs_fill_super() is called by the VFS to mount the device described by @sb 2262 * 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. 2263 * with the mount otions in @data with the NTFS filesystem.
2264 * 2264 *
2265 * If @silent is true, remain silent even if errors are detected. This is used 2265 * 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 2266 * 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 2267 * registered filesystems one after the other until one succeeds. This implies
2268 * that all file systems except the correct one will quite correctly and 2268 * 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 2269 * expectedly return an error, but nobody wants to see error messages when in
2270 * fact this is what is supposed to happen. 2270 * fact this is what is supposed to happen.
2271 * 2271 *
@@ -2292,39 +2292,25 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2292 return -ENOMEM; 2292 return -ENOMEM;
2293 } 2293 }
2294 /* Initialize ntfs_volume structure. */ 2294 /* Initialize ntfs_volume structure. */
2295 memset(vol, 0, sizeof(ntfs_volume)); 2295 *vol = (ntfs_volume) {
2296 vol->sb = sb; 2296 .sb = sb,
2297 vol->upcase = NULL; 2297 /*
2298 vol->attrdef = NULL; 2298 * Default is group and other don't have any access to files or
2299 vol->mft_ino = NULL; 2299 * directories while owner has full access. Further, files by
2300 vol->mftbmp_ino = NULL; 2300 * default are not executable but directories are of course
2301 * browseable.
2302 */
2303 .fmask = 0177,
2304 .dmask = 0077,
2305 };
2301 init_rwsem(&vol->mftbmp_lock); 2306 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); 2307 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 2308
2326 unlock_kernel(); 2309 unlock_kernel();
2327 2310
2311 /* By default, enable sparse support. */
2312 NVolSetSparseEnabled(vol);
2313
2328 /* Important to get the mount options dealt with now. */ 2314 /* Important to get the mount options dealt with now. */
2329 if (!parse_options(vol, (char*)opt)) 2315 if (!parse_options(vol, (char*)opt))
2330 goto err_out_now; 2316 goto err_out_now;
@@ -2347,7 +2333,8 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2347 } 2333 }
2348 2334
2349 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */ 2335 /* 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; 2336 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2337 NTFS_BLOCK_SIZE_BITS;
2351 2338
2352 /* Read the boot sector and return unlocked buffer head to it. */ 2339 /* Read the boot sector and return unlocked buffer head to it. */
2353 if (!(bh = read_ntfs_boot_sector(sb, silent))) { 2340 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
@@ -2581,7 +2568,7 @@ err_out_now:
2581 */ 2568 */
2582kmem_cache_t *ntfs_name_cache; 2569kmem_cache_t *ntfs_name_cache;
2583 2570
2584/* Slab caches for efficient allocation/deallocation of of inodes. */ 2571/* Slab caches for efficient allocation/deallocation of inodes. */
2585kmem_cache_t *ntfs_inode_cache; 2572kmem_cache_t *ntfs_inode_cache;
2586kmem_cache_t *ntfs_big_inode_cache; 2573kmem_cache_t *ntfs_big_inode_cache;
2587 2574
@@ -2705,7 +2692,7 @@ static int __init init_ntfs_fs(void)
2705 ntfs_debug("NTFS driver registered successfully."); 2692 ntfs_debug("NTFS driver registered successfully.");
2706 return 0; /* Success! */ 2693 return 0; /* Success! */
2707 } 2694 }
2708 printk(KERN_CRIT "NTFS: Failed to register NTFS file system driver!\n"); 2695 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
2709 2696
2710sysctl_err_out: 2697sysctl_err_out:
2711 kmem_cache_destroy(ntfs_big_inode_cache); 2698 kmem_cache_destroy(ntfs_big_inode_cache);
@@ -2719,7 +2706,7 @@ actx_err_out:
2719 kmem_cache_destroy(ntfs_index_ctx_cache); 2706 kmem_cache_destroy(ntfs_index_ctx_cache);
2720ictx_err_out: 2707ictx_err_out:
2721 if (!err) { 2708 if (!err) {
2722 printk(KERN_CRIT "NTFS: Aborting NTFS file system driver " 2709 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
2723 "registration...\n"); 2710 "registration...\n");
2724 err = -ENOMEM; 2711 err = -ENOMEM;
2725 } 2712 }
@@ -2759,7 +2746,7 @@ static void __exit exit_ntfs_fs(void)
2759} 2746}
2760 2747
2761MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>"); 2748MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
2762MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2004 Anton Altaparmakov"); 2749MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
2763MODULE_VERSION(NTFS_VERSION); 2750MODULE_VERSION(NTFS_VERSION);
2764MODULE_LICENSE("GPL"); 2751MODULE_LICENSE("GPL");
2765#ifdef DEBUG 2752#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/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/unistr.c b/fs/ntfs/unistr.c
index 560b0ea255b0..19c42e231b44 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,
diff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h
index 4b97fa8635a8..62be73ad0156 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) */
@@ -141,6 +141,7 @@ typedef enum {
141 file names in WIN32 namespace. */ 141 file names in WIN32 namespace. */
142 NV_LogFileEmpty, /* 1: $LogFile journal is empty. */ 142 NV_LogFileEmpty, /* 1: $LogFile journal is empty. */
143 NV_QuotaOutOfDate, /* 1: $Quota is out of date. */ 143 NV_QuotaOutOfDate, /* 1: $Quota is out of date. */
144 NV_SparseEnabled, /* 1: May create sparse files. */
144} ntfs_volume_flags; 145} ntfs_volume_flags;
145 146
146/* 147/*
@@ -167,5 +168,6 @@ NVOL_FNS(ShowSystemFiles)
167NVOL_FNS(CaseSensitive) 168NVOL_FNS(CaseSensitive)
168NVOL_FNS(LogFileEmpty) 169NVOL_FNS(LogFileEmpty)
169NVOL_FNS(QuotaOutOfDate) 170NVOL_FNS(QuotaOutOfDate)
171NVOL_FNS(SparseEnabled)
170 172
171#endif /* _LINUX_NTFS_VOLUME_H */ 173#endif /* _LINUX_NTFS_VOLUME_H */