Skip to content

Commit bfab36e

Browse files
Anton AltaparmakovLinus Torvalds
authored andcommitted
NTFS: Fix a mount time deadlock.
Big thanks go to Mathias Kolehmainen for reporting the bug, providing debug output and testing the patches I sent him to get it working. The fix was to stop calling ntfs_attr_set() at mount time as that causes balance_dirty_pages_ratelimited() to be called which on systems with little memory actually tries to go and balance the dirty pages which tries to take the s_umount semaphore but because we are still in fill_super() across which the VFS holds s_umount for writing this results in a deadlock. We now do the dirty work by hand by submitting individual buffers. This has the annoying "feature" that mounting can take a few seconds if the journal is large as we have clear it all. One day someone should improve on this by deferring the journal clearing to a helper kernel thread so it can be done in the background but I don't have time for this at the moment and the current solution works fine so I am leaving it like this for now. Signed-off-by: Anton Altaparmakov <aia21@cantab.net> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f26e51f commit bfab36e

9 files changed

Lines changed: 181 additions & 53 deletions

File tree

‎Documentation/filesystems/ntfs.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ raiddev /dev/md0
407407
device /dev/hda5
408408
raid-disk 0
409409
device /dev/hdb1
410-
raid-disl 1
410+
raid-disk 1
411411

412412
For linear raid, just change the raid-level above to "raid-level linear", for
413413
mirrors, change it to "raid-level 1", and for stripe sets with parity, change
@@ -457,6 +457,8 @@ ChangeLog
457457

458458
Note, a technical ChangeLog aimed at kernel hackers is in fs/ntfs/ChangeLog.
459459

460+
2.1.29:
461+
- Fix a deadlock when mounting read-write.
460462
2.1.28:
461463
- Fix a deadlock.
462464
2.1.27:

‎fs/ntfs/ChangeLog‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ ToDo/Notes:
1717
happen is unclear however so it is worth waiting until someone hits
1818
the problem.
1919

20+
2.1.29 - Fix a deadlock at mount time.
21+
22+
- During mount the VFS holds s_umount lock on the superblock. So when
23+
we try to empty the journal $LogFile contents by calling
24+
ntfs_attr_set() when the machine does not have much memory and the
25+
journal is large ntfs_attr_set() results in the VM trying to balance
26+
dirty pages which in turn tries to that the s_umount lock and thus we
27+
get a deadlock. The solution is to not use ntfs_attr_set() and
28+
instead do the zeroing by hand at the block level rather than page
29+
cache level.
30+
- Fix sparse warnings.
31+
2032
2.1.28 - Fix a deadlock.
2133

2234
- Fix deadlock in fs/ntfs/inode.c::ntfs_put_inode(). Thanks to Sergey

‎fs/ntfs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ntfs-objs := aops.o attrib.o collate.o compress.o debug.o dir.o file.o \
66
index.o inode.o mft.o mst.o namei.o runlist.o super.o sysctl.o \
77
unistr.o upcase.o
88

9-
EXTRA_CFLAGS = -DNTFS_VERSION=\"2.1.28\"
9+
EXTRA_CFLAGS = -DNTFS_VERSION=\"2.1.29\"
1010

1111
ifeq ($(CONFIG_NTFS_DEBUG),y)
1212
EXTRA_CFLAGS += -DDEBUG

‎fs/ntfs/aops.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* aops.c - NTFS kernel address space operations and page cache handling.
33
* Part of the Linux-NTFS project.
44
*
5-
* Copyright (c) 2001-2006 Anton Altaparmakov
5+
* Copyright (c) 2001-2007 Anton Altaparmakov
66
* Copyright (c) 2002 Richard Russon
77
*
88
* This program/include file is free software; you can redistribute it and/or
@@ -396,7 +396,7 @@ static int ntfs_readpage(struct file *file, struct page *page)
396396
loff_t i_size;
397397
struct inode *vi;
398398
ntfs_inode *ni, *base_ni;
399-
u8 *kaddr;
399+
u8 *addr;
400400
ntfs_attr_search_ctx *ctx;
401401
MFT_RECORD *mrec;
402402
unsigned long flags;
@@ -491,15 +491,15 @@ static int ntfs_readpage(struct file *file, struct page *page)
491491
/* Race with shrinking truncate. */
492492
attr_len = i_size;
493493
}
494-
kaddr = kmap_atomic(page, KM_USER0);
494+
addr = kmap_atomic(page, KM_USER0);
495495
/* Copy the data to the page. */
496-
memcpy(kaddr, (u8*)ctx->attr +
496+
memcpy(addr, (u8*)ctx->attr +
497497
le16_to_cpu(ctx->attr->data.resident.value_offset),
498498
attr_len);
499499
/* Zero the remainder of the page. */
500-
memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
500+
memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
501501
flush_dcache_page(page);
502-
kunmap_atomic(kaddr, KM_USER0);
502+
kunmap_atomic(addr, KM_USER0);
503503
put_unm_err_out:
504504
ntfs_attr_put_search_ctx(ctx);
505505
unm_err_out:
@@ -1344,7 +1344,7 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
13441344
loff_t i_size;
13451345
struct inode *vi = page->mapping->host;
13461346
ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1347-
char *kaddr;
1347+
char *addr;
13481348
ntfs_attr_search_ctx *ctx = NULL;
13491349
MFT_RECORD *m = NULL;
13501350
u32 attr_len;
@@ -1484,14 +1484,14 @@ static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
14841484
/* Shrinking cannot fail. */
14851485
BUG_ON(err);
14861486
}
1487-
kaddr = kmap_atomic(page, KM_USER0);
1487+
addr = kmap_atomic(page, KM_USER0);
14881488
/* Copy the data from the page to the mft record. */
14891489
memcpy((u8*)ctx->attr +
14901490
le16_to_cpu(ctx->attr->data.resident.value_offset),
1491-
kaddr, attr_len);
1491+
addr, attr_len);
14921492
/* Zero out of bounds area in the page cache page. */
1493-
memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1494-
kunmap_atomic(kaddr, KM_USER0);
1493+
memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1494+
kunmap_atomic(addr, KM_USER0);
14951495
flush_dcache_page(page);
14961496
flush_dcache_mft_record_page(ctx->ntfs_ino);
14971497
/* We are done with the page. */

‎fs/ntfs/attrib.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
33
*
4-
* Copyright (c) 2001-2006 Anton Altaparmakov
4+
* Copyright (c) 2001-2007 Anton Altaparmakov
55
* Copyright (c) 2002 Richard Russon
66
*
77
* This program/include file is free software; you can redistribute it and/or
@@ -2500,7 +2500,7 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
25002500
struct page *page;
25012501
u8 *kaddr;
25022502
pgoff_t idx, end;
2503-
unsigned int start_ofs, end_ofs, size;
2503+
unsigned start_ofs, end_ofs, size;
25042504

25052505
ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
25062506
(long long)ofs, (long long)cnt, val);
@@ -2548,6 +2548,8 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
25482548
kunmap_atomic(kaddr, KM_USER0);
25492549
set_page_dirty(page);
25502550
page_cache_release(page);
2551+
balance_dirty_pages_ratelimited(mapping);
2552+
cond_resched();
25512553
if (idx == end)
25522554
goto done;
25532555
idx++;
@@ -2604,6 +2606,8 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
26042606
kunmap_atomic(kaddr, KM_USER0);
26052607
set_page_dirty(page);
26062608
page_cache_release(page);
2609+
balance_dirty_pages_ratelimited(mapping);
2610+
cond_resched();
26072611
}
26082612
done:
26092613
ntfs_debug("Done.");

‎fs/ntfs/file.c‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
33
*
4-
* Copyright (c) 2001-2006 Anton Altaparmakov
4+
* Copyright (c) 2001-2007 Anton Altaparmakov
55
*
66
* This program/include file is free software; you can redistribute it and/or
77
* modify it under the terms of the GNU General Public License as published
@@ -26,7 +26,6 @@
2626
#include <linux/swap.h>
2727
#include <linux/uio.h>
2828
#include <linux/writeback.h>
29-
#include <linux/sched.h>
3029

3130
#include <asm/page.h>
3231
#include <asm/uaccess.h>
@@ -362,7 +361,7 @@ static inline void ntfs_fault_in_pages_readable(const char __user *uaddr,
362361
volatile char c;
363362

364363
/* Set @end to the first byte outside the last page we care about. */
365-
end = (const char __user*)PAGE_ALIGN((ptrdiff_t __user)uaddr + bytes);
364+
end = (const char __user*)PAGE_ALIGN((unsigned long)uaddr + bytes);
366365

367366
while (!__get_user(c, uaddr) && (uaddr += PAGE_SIZE, uaddr < end))
368367
;
@@ -532,7 +531,8 @@ static int ntfs_prepare_pages_for_non_resident_write(struct page **pages,
532531
blocksize_bits = vol->sb->s_blocksize_bits;
533532
u = 0;
534533
do {
535-
struct page *page = pages[u];
534+
page = pages[u];
535+
BUG_ON(!page);
536536
/*
537537
* create_empty_buffers() will create uptodate/dirty buffers if
538538
* the page is uptodate/dirty.
@@ -1291,7 +1291,7 @@ static inline size_t ntfs_copy_from_user(struct page **pages,
12911291
size_t bytes)
12921292
{
12931293
struct page **last_page = pages + nr_pages;
1294-
char *kaddr;
1294+
char *addr;
12951295
size_t total = 0;
12961296
unsigned len;
12971297
int left;
@@ -1300,13 +1300,13 @@ static inline size_t ntfs_copy_from_user(struct page **pages,
13001300
len = PAGE_CACHE_SIZE - ofs;
13011301
if (len > bytes)
13021302
len = bytes;
1303-
kaddr = kmap_atomic(*pages, KM_USER0);
1304-
left = __copy_from_user_inatomic(kaddr + ofs, buf, len);
1305-
kunmap_atomic(kaddr, KM_USER0);
1303+
addr = kmap_atomic(*pages, KM_USER0);
1304+
left = __copy_from_user_inatomic(addr + ofs, buf, len);
1305+
kunmap_atomic(addr, KM_USER0);
13061306
if (unlikely(left)) {
13071307
/* Do it the slow way. */
1308-
kaddr = kmap(*pages);
1309-
left = __copy_from_user(kaddr + ofs, buf, len);
1308+
addr = kmap(*pages);
1309+
left = __copy_from_user(addr + ofs, buf, len);
13101310
kunmap(*pages);
13111311
if (unlikely(left))
13121312
goto err_out;
@@ -1408,26 +1408,26 @@ static inline size_t ntfs_copy_from_user_iovec(struct page **pages,
14081408
size_t *iov_ofs, size_t bytes)
14091409
{
14101410
struct page **last_page = pages + nr_pages;
1411-
char *kaddr;
1411+
char *addr;
14121412
size_t copied, len, total = 0;
14131413

14141414
do {
14151415
len = PAGE_CACHE_SIZE - ofs;
14161416
if (len > bytes)
14171417
len = bytes;
1418-
kaddr = kmap_atomic(*pages, KM_USER0);
1419-
copied = __ntfs_copy_from_user_iovec_inatomic(kaddr + ofs,
1418+
addr = kmap_atomic(*pages, KM_USER0);
1419+
copied = __ntfs_copy_from_user_iovec_inatomic(addr + ofs,
14201420
*iov, *iov_ofs, len);
1421-
kunmap_atomic(kaddr, KM_USER0);
1421+
kunmap_atomic(addr, KM_USER0);
14221422
if (unlikely(copied != len)) {
14231423
/* Do it the slow way. */
1424-
kaddr = kmap(*pages);
1425-
copied = __ntfs_copy_from_user_iovec_inatomic(kaddr + ofs,
1424+
addr = kmap(*pages);
1425+
copied = __ntfs_copy_from_user_iovec_inatomic(addr + ofs,
14261426
*iov, *iov_ofs, len);
14271427
/*
14281428
* Zero the rest of the target like __copy_from_user().
14291429
*/
1430-
memset(kaddr + ofs + copied, 0, len - copied);
1430+
memset(addr + ofs + copied, 0, len - copied);
14311431
kunmap(*pages);
14321432
if (unlikely(copied != len))
14331433
goto err_out;
@@ -1735,8 +1735,6 @@ static int ntfs_commit_pages_after_write(struct page **pages,
17351735
read_unlock_irqrestore(&ni->size_lock, flags);
17361736
BUG_ON(initialized_size != i_size);
17371737
if (end > initialized_size) {
1738-
unsigned long flags;
1739-
17401738
write_lock_irqsave(&ni->size_lock, flags);
17411739
ni->initialized_size = end;
17421740
i_size_write(vi, end);

‎fs/ntfs/inode.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "dir.h"
3535
#include "debug.h"
3636
#include "inode.h"
37-
#include "attrib.h"
3837
#include "lcnalloc.h"
3938
#include "malloc.h"
4039
#include "mft.h"
@@ -2500,8 +2499,6 @@ int ntfs_truncate(struct inode *vi)
25002499
/* Resize the attribute record to best fit the new attribute size. */
25012500
if (new_size < vol->mft_record_size &&
25022501
!ntfs_resident_attr_value_resize(m, a, new_size)) {
2503-
unsigned long flags;
2504-
25052502
/* The resize succeeded! */
25062503
flush_dcache_mft_record_page(ctx->ntfs_ino);
25072504
mark_mft_record_dirty(ctx->ntfs_ino);

0 commit comments

Comments
 (0)