Commit a5ea1066 authored by Anton Altaparmakov's avatar Anton Altaparmakov

NTFS: 2.0.20 - Support non-resident directory index bitmaps, fix page leak in readdir.

- Move the directory index bitmap to use an attribute inode instead of
  having special fields for it inside the ntfs inode structure. This
  means that the index bitmaps now use the page cache for i/o, too,
  and also as a side effect we get support for non-resident index
  bitmaps for free.
- Simplify/cleanup error handling in fs/ntfs/dir.c::ntfs_readdir() and
  fix a page leak that manifested itself in some cases.
- Add fs/ntfs/inode.c::ntfs_put_inode(), which we need to release the
  index bitmap inode on the final iput().
parent 1138bf4c
...@@ -247,6 +247,12 @@ ChangeLog ...@@ -247,6 +247,12 @@ ChangeLog
Note, a technical ChangeLog aimed at kernel hackers is in fs/ntfs/ChangeLog. Note, a technical ChangeLog aimed at kernel hackers is in fs/ntfs/ChangeLog.
2.0.20:
- Support non-resident directory index bitmaps. This means we now cope
with huge directories without problems.
- Fix a page leak that manifested itself in some cases when reading
directory contents.
- Internal cleanups.
2.0.19: 2.0.19:
- Fix race condition and improvements in block i/o interface. - Fix race condition and improvements in block i/o interface.
- Optimization when reading compressed files. - Optimization when reading compressed files.
......
...@@ -7,10 +7,20 @@ ToDo: ...@@ -7,10 +7,20 @@ ToDo:
truncate the visible i_size? Will the user just get -E2BIG (or truncate the visible i_size? Will the user just get -E2BIG (or
whatever) on open()? Or will (s)he be able to open() but lseek() and whatever) on open()? Or will (s)he be able to open() but lseek() and
read() will fail when s_maxbytes is reached? -> Investigate this. read() will fail when s_maxbytes is reached? -> Investigate this.
- Implement/allow non-resident index bitmaps in dir.c::ntfs_readdir()
and then also consider initialized_size w.r.t. the bitmaps, etc.
- Enable NFS exporting of NTFS. - Enable NFS exporting of NTFS.
2.0.20 - Support non-resident directory index bitmaps, fix page leak in readdir.
- Move the directory index bitmap to use an attribute inode instead of
having special fields for it inside the ntfs inode structure. This
means that the index bitmaps now use the page cache for i/o, too,
and also as a side effect we get support for non-resident index
bitmaps for free.
- Simplify/cleanup error handling in fs/ntfs/dir.c::ntfs_readdir() and
fix a page leak that manifested itself in some cases.
- Add fs/ntfs/inode.c::ntfs_put_inode(), which we need to release the
index bitmap inode on the final iput().
2.0.19 - Fix race condition, improvements, and optimizations in i/o interface. 2.0.19 - Fix race condition, improvements, and optimizations in i/o interface.
- Apply block optimization added to fs/ntfs/aops.c::ntfs_read_block() - Apply block optimization added to fs/ntfs/aops.c::ntfs_read_block()
......
...@@ -5,7 +5,7 @@ obj-$(CONFIG_NTFS_FS) += ntfs.o ...@@ -5,7 +5,7 @@ obj-$(CONFIG_NTFS_FS) += ntfs.o
ntfs-objs := aops.o attrib.o compress.o debug.o dir.o file.o inode.o mft.o \ ntfs-objs := aops.o attrib.o compress.o debug.o dir.o file.o inode.o mft.o \
mst.o namei.o super.o sysctl.o time.o unistr.o upcase.o mst.o namei.o super.o sysctl.o time.o unistr.o upcase.o
EXTRA_CFLAGS = -DNTFS_VERSION=\"2.0.19\" EXTRA_CFLAGS = -DNTFS_VERSION=\"2.0.20\"
ifeq ($(CONFIG_NTFS_DEBUG),y) ifeq ($(CONFIG_NTFS_DEBUG),y)
EXTRA_CFLAGS += -DDEBUG EXTRA_CFLAGS += -DDEBUG
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project. * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
* *
* Copyright (c) 2001,2002 Anton Altaparmakov. * Copyright (c) 2001,2002 Anton Altaparmakov.
* Copyright (C) 2002 Richard Russon. * Copyright (c) 2002 Richard Russon.
* *
* This program/include file is free software; you can redistribute it and/or * This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published * modify it under the terms of the GNU General Public License as published
...@@ -1052,8 +1052,8 @@ static inline int ntfs_filldir(ntfs_volume *vol, struct file *filp, ...@@ -1052,8 +1052,8 @@ static inline int ntfs_filldir(ntfs_volume *vol, struct file *filp,
*/ */
static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
{ {
s64 ia_pos, ia_start, prev_ia_pos; s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
struct inode *vdir = filp->f_dentry->d_inode; struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode;
struct super_block *sb = vdir->i_sb; struct super_block *sb = vdir->i_sb;
ntfs_inode *ndir = NTFS_I(vdir); ntfs_inode *ndir = NTFS_I(vdir);
ntfs_volume *vol = NTFS_SB(sb); ntfs_volume *vol = NTFS_SB(sb);
...@@ -1061,10 +1061,10 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1061,10 +1061,10 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
INDEX_ROOT *ir; INDEX_ROOT *ir;
INDEX_ENTRY *ie; INDEX_ENTRY *ie;
INDEX_ALLOCATION *ia; INDEX_ALLOCATION *ia;
u8 *name; u8 *name = NULL;
int rc, err, ir_pos, bmp_pos; int rc, err, ir_pos, cur_bmp_pos;
struct address_space *ia_mapping; struct address_space *ia_mapping, *bmp_mapping;
struct page *page; struct page *bmp_page = NULL, *ia_page = NULL;
u8 *kaddr, *bmp, *index_end; u8 *kaddr, *bmp, *index_end;
attr_search_context *ctx; attr_search_context *ctx;
...@@ -1093,17 +1093,20 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1093,17 +1093,20 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
goto done; goto done;
filp->f_pos++; filp->f_pos++;
} }
/* Get hold of the mft record for the directory. */ /* Get hold of the mft record for the directory. */
m = map_mft_record(READ, ndir); m = map_mft_record(READ, ndir);
if (IS_ERR(m)) { if (unlikely(IS_ERR(m))) {
err = PTR_ERR(m); err = PTR_ERR(m);
m = NULL;
ctx = NULL;
goto err_out; goto err_out;
} }
ctx = get_attr_search_ctx(ndir, m); ctx = get_attr_search_ctx(ndir, m);
if (!ctx) { if (unlikely(!ctx)) {
err = -ENOMEM; err = -ENOMEM;
goto unm_err_out; goto err_out;
} }
/* /*
...@@ -1112,9 +1115,9 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1112,9 +1115,9 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
*/ */
name = (u8*)kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, name = (u8*)kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1,
GFP_NOFS); GFP_NOFS);
if (!name) { if (unlikely(!name)) {
err = -ENOMEM; err = -ENOMEM;
goto put_unm_err_out; goto err_out;
} }
/* Are we jumping straight into the index allocation attribute? */ /* Are we jumping straight into the index allocation attribute? */
if (filp->f_pos >= vol->mft_record_size) if (filp->f_pos >= vol->mft_record_size)
...@@ -1122,12 +1125,11 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1122,12 +1125,11 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
/* Get the offset into the index root attribute. */ /* Get the offset into the index root attribute. */
ir_pos = (s64)filp->f_pos; ir_pos = (s64)filp->f_pos;
/* Find the index root attribute in the mft record. */ /* Find the index root attribute in the mft record. */
if (!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0, if (unlikely(!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0,
ctx)) { NULL, 0, ctx))) {
ntfs_error(sb, "Index root attribute missing in directory " ntfs_error(sb, "Index root attribute missing in directory "
"inode 0x%lx.", vdir->i_ino); "inode 0x%lx.", vdir->i_ino);
err = -EIO; goto err_out;
goto kf_unm_err_out;
} }
/* Get to the index root value (it's been verified in read_inode). */ /* Get to the index root value (it's been verified in read_inode). */
ir = (INDEX_ROOT*)((u8*)ctx->attr + ir = (INDEX_ROOT*)((u8*)ctx->attr +
...@@ -1144,11 +1146,11 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1144,11 +1146,11 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->_IEH(length)))) { for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->_IEH(length)))) {
ntfs_debug("In index root, offset 0x%x.", (u8*)ie - (u8*)ir); ntfs_debug("In index root, offset 0x%x.", (u8*)ie - (u8*)ir);
/* Bounds checks. */ /* Bounds checks. */
if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + if (unlikely((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
sizeof(INDEX_ENTRY_HEADER) > index_end || sizeof(INDEX_ENTRY_HEADER) > index_end ||
(u8*)ie + le16_to_cpu(ie->_IEH(key_length)) > (u8*)ie + le16_to_cpu(ie->_IEH(key_length)) >
index_end) index_end))
goto dir_err_out; goto err_out;
/* The last entry cannot contain a name. */ /* The last entry cannot contain a name. */
if (ie->_IEH(flags) & INDEX_ENTRY_END) if (ie->_IEH(flags) & INDEX_ENTRY_END)
break; break;
...@@ -1166,80 +1168,99 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1166,80 +1168,99 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
goto EOD; goto EOD;
/* Advance f_pos to the beginning of the index allocation. */ /* Advance f_pos to the beginning of the index allocation. */
filp->f_pos = vol->mft_record_size; filp->f_pos = vol->mft_record_size;
/* Reinitialize the search context. */
reinit_attr_search_ctx(ctx);
skip_index_root: skip_index_root:
if (NInoBmpNonResident(ndir)) { kaddr = NULL;
/* prev_ia_pos = -1LL;
* Read the page of the bitmap that contains the current index
* block.
*/
// TODO: FIXME: Implement this!
ntfs_error(sb, "Index bitmap is non-resident, which is not "
"supported yet. Pretending that end of "
"directory has been reached.\n");
goto EOD;
} else {
/* Find the index bitmap attribute in the mft record. */
if (!lookup_attr(AT_BITMAP, I30, 4, CASE_SENSITIVE, 0, NULL, 0,
ctx)) {
ntfs_error(sb, "Index bitmap attribute missing in "
"directory inode 0x%lx.", vdir->i_ino);
err = -EIO;
goto kf_unm_err_out;
}
bmp = (u8*)ctx->attr +
le16_to_cpu(ctx->attr->_ARA(value_offset));
}
/* Get the offset into the index allocation attribute. */ /* Get the offset into the index allocation attribute. */
ia_pos = (s64)filp->f_pos - vol->mft_record_size; ia_pos = (s64)filp->f_pos - vol->mft_record_size;
ia_mapping = vdir->i_mapping; ia_mapping = vdir->i_mapping;
/* If the index block is not in use find the next one that is. */ bmp_vi = ndir->_IDM(bmp_ino);
if (unlikely(!bmp_vi)) {
ntfs_debug("Inode %lu, regetting index bitmap.", vdir->i_ino);
bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
if (unlikely(IS_ERR(bmp_vi))) {
ntfs_error(sb, "Failed to get bitmap attribute.");
err = PTR_ERR(bmp_vi);
goto err_out;
}
ndir->_IDM(bmp_ino) = bmp_vi;
}
bmp_mapping = bmp_vi->i_mapping;
/* Get the starting bitmap bit position and sanity check it. */
bmp_pos = ia_pos >> ndir->_IDM(index_block_size_bits); bmp_pos = ia_pos >> ndir->_IDM(index_block_size_bits);
page = NULL; if (unlikely(bmp_pos >> 3 >= bmp_vi->i_size)) {
kaddr = NULL;
prev_ia_pos = -1LL;
if (bmp_pos >> 3 >= ndir->_IDM(bmp_size)) {
ntfs_error(sb, "Current index allocation position exceeds " ntfs_error(sb, "Current index allocation position exceeds "
"index bitmap size."); "index bitmap size.");
goto kf_unm_err_out; goto err_out;
} }
while (!(bmp[bmp_pos >> 3] & (1 << (bmp_pos & 7)))) { /* Get the starting bit position in the current bitmap page. */
cur_bmp_pos = bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1);
bmp_pos &= ~((PAGE_CACHE_SIZE * 8) - 1);
get_next_bmp_page:
ntfs_debug("Reading bitmap with page index 0x%Lx, bit ofs 0x%Lx",
(long long)bmp_pos >> (3 + PAGE_CACHE_SHIFT),
(long long)bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1));
bmp_page = ntfs_map_page(bmp_mapping,
bmp_pos >> (3 + PAGE_CACHE_SHIFT));
if (unlikely(IS_ERR(bmp_page))) {
ntfs_error(sb, "Reading index bitmap failed.");
err = PTR_ERR(bmp_page);
bmp_page = NULL;
goto err_out;
}
bmp = (u8*)page_address(bmp_page);
/* Find next index block in use. */
while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
find_next_index_buffer: find_next_index_buffer:
bmp_pos++; cur_bmp_pos++;
/*
* If we have reached the end of the bitmap page, get the next
* page, and put away the old one.
*/
if (unlikely((cur_bmp_pos >> 3) >= PAGE_CACHE_SIZE)) {
ntfs_unmap_page(bmp_page);
bmp_pos += PAGE_CACHE_SIZE * 8;
cur_bmp_pos = 0;
goto get_next_bmp_page;
}
/* If we have reached the end of the bitmap, we are done. */ /* If we have reached the end of the bitmap, we are done. */
if (bmp_pos >> 3 >= ndir->_IDM(bmp_size)) if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= vdir->i_size))
goto EOD; goto unm_EOD;
ia_pos = (s64)bmp_pos << ndir->_IDM(index_block_size_bits); ia_pos = (bmp_pos + cur_bmp_pos) <<
ndir->_IDM(index_block_size_bits);
} }
ntfs_debug("Handling index buffer 0x%x.", bmp_pos); ntfs_debug("Handling index buffer 0x%Lx.",
(long long)bmp_pos + cur_bmp_pos);
/* If the current index buffer is in the same page we reuse the page. */ /* If the current index buffer is in the same page we reuse the page. */
if ((prev_ia_pos & PAGE_CACHE_MASK) != (ia_pos & PAGE_CACHE_MASK)) { if ((prev_ia_pos & PAGE_CACHE_MASK) != (ia_pos & PAGE_CACHE_MASK)) {
prev_ia_pos = ia_pos; prev_ia_pos = ia_pos;
if (page) if (likely(ia_page != NULL))
ntfs_unmap_page(page); ntfs_unmap_page(ia_page);
/* /*
* Map the page cache page containing the current ia_pos, * Map the page cache page containing the current ia_pos,
* reading it from disk if necessary. * reading it from disk if necessary.
*/ */
page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_CACHE_SHIFT); ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_CACHE_SHIFT);
if (IS_ERR(page)) if (unlikely(IS_ERR(ia_page))) {
goto map_page_err_out; ntfs_error(sb, "Reading index allocation data failed.");
kaddr = (u8*)page_address(page); err = PTR_ERR(ia_page);
ia_page = NULL;
goto err_out;
}
kaddr = (u8*)page_address(ia_page);
} }
/* Get the current index buffer. */ /* Get the current index buffer. */
ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_CACHE_MASK & ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_CACHE_MASK &
~(s64)(ndir->_IDM(index_block_size) - 1))); ~(s64)(ndir->_IDM(index_block_size) - 1)));
/* Bounds checks. */ /* Bounds checks. */
if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) { if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE)) {
ntfs_error(sb, "Out of bounds check failed. Corrupt directory " ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
"inode 0x%lx or driver bug.", vdir->i_ino); "inode 0x%lx or driver bug.", vdir->i_ino);
err = -EIO; goto err_out;
goto unm_dir_err_out;
} }
if (sle64_to_cpu(ia->index_block_vcn) != (ia_pos & if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
~(s64)(ndir->_IDM(index_block_size) - 1)) >> ~(s64)(ndir->_IDM(index_block_size) - 1)) >>
ndir->_IDM(index_vcn_size_bits)) { ndir->_IDM(index_vcn_size_bits))) {
ntfs_error(sb, "Actual VCN (0x%Lx) of index buffer is " ntfs_error(sb, "Actual VCN (0x%Lx) of index buffer is "
"different from expected VCN (0x%Lx). " "different from expected VCN (0x%Lx). "
"Directory inode 0x%lx is corrupt or driver " "Directory inode 0x%lx is corrupt or driver "
...@@ -1247,11 +1268,10 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1247,11 +1268,10 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
(long long)sle64_to_cpu(ia->index_block_vcn), (long long)sle64_to_cpu(ia->index_block_vcn),
(long long)ia_pos >> (long long)ia_pos >>
ndir->_IDM(index_vcn_size_bits), vdir->i_ino); ndir->_IDM(index_vcn_size_bits), vdir->i_ino);
err = -EIO; goto err_out;
goto unm_dir_err_out;
} }
if (le32_to_cpu(ia->index.allocated_size) + 0x18 != if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
ndir->_IDM(index_block_size)) { ndir->_IDM(index_block_size))) {
ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode " ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
"0x%lx has a size (%u) differing from the " "0x%lx has a size (%u) differing from the "
"directory specified size (%u). Directory " "directory specified size (%u). Directory "
...@@ -1260,28 +1280,25 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1260,28 +1280,25 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
ndir->_IDM(index_vcn_size_bits), vdir->i_ino, ndir->_IDM(index_vcn_size_bits), vdir->i_ino,
le32_to_cpu(ia->index.allocated_size) + 0x18, le32_to_cpu(ia->index.allocated_size) + 0x18,
ndir->_IDM(index_block_size)); ndir->_IDM(index_block_size));
err = -EIO; goto err_out;
goto unm_dir_err_out;
} }
index_end = (u8*)ia + ndir->_IDM(index_block_size); index_end = (u8*)ia + ndir->_IDM(index_block_size);
if (index_end > kaddr + PAGE_CACHE_SIZE) { if (unlikely(index_end > kaddr + PAGE_CACHE_SIZE)) {
ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode " ntfs_error(sb, "Index buffer (VCN 0x%Lx) of directory inode "
"0x%lx crosses page boundary. Impossible! " "0x%lx crosses page boundary. Impossible! "
"Cannot access! This is probably a bug in the " "Cannot access! This is probably a bug in the "
"driver.", (long long)ia_pos >> "driver.", (long long)ia_pos >>
ndir->_IDM(index_vcn_size_bits), vdir->i_ino); ndir->_IDM(index_vcn_size_bits), vdir->i_ino);
err = -EIO; goto err_out;
goto unm_dir_err_out;
} }
ia_start = ia_pos & ~(s64)(ndir->_IDM(index_block_size) - 1); ia_start = ia_pos & ~(s64)(ndir->_IDM(index_block_size) - 1);
index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
if (index_end > (u8*)ia + ndir->_IDM(index_block_size)) { if (unlikely(index_end > (u8*)ia + ndir->_IDM(index_block_size))) {
ntfs_error(sb, "Size of index buffer (VCN 0x%Lx) of directory " ntfs_error(sb, "Size of index buffer (VCN 0x%Lx) of directory "
"inode 0x%lx exceeds maximum size.", "inode 0x%lx exceeds maximum size.",
(long long)ia_pos >> (long long)ia_pos >>
ndir->_IDM(index_vcn_size_bits), vdir->i_ino); ndir->_IDM(index_vcn_size_bits), vdir->i_ino);
err = -EIO; goto err_out;
goto unm_dir_err_out;
} }
/* The first index entry in this index buffer. */ /* The first index entry in this index buffer. */
ie = (INDEX_ENTRY*)((u8*)&ia->index + ie = (INDEX_ENTRY*)((u8*)&ia->index +
...@@ -1295,11 +1312,11 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1295,11 +1312,11 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
ntfs_debug("In index allocation, offset 0x%Lx.", ntfs_debug("In index allocation, offset 0x%Lx.",
(long long)ia_start + ((u8*)ie - (u8*)ia)); (long long)ia_start + ((u8*)ie - (u8*)ia));
/* Bounds checks. */ /* Bounds checks. */
if ((u8*)ie < (u8*)ia || (u8*)ie + if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
sizeof(INDEX_ENTRY_HEADER) > index_end || sizeof(INDEX_ENTRY_HEADER) > index_end ||
(u8*)ie + le16_to_cpu(ie->_IEH(key_length)) > (u8*)ie + le16_to_cpu(ie->_IEH(key_length)) >
index_end) index_end))
goto unm_dir_err_out; goto err_out;
/* The last entry cannot contain a name. */ /* The last entry cannot contain a name. */
if (ie->_IEH(flags) & INDEX_ENTRY_END) if (ie->_IEH(flags) & INDEX_ENTRY_END)
break; break;
...@@ -1310,11 +1327,16 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1310,11 +1327,16 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
rc = ntfs_filldir(vol, filp, ndir, INDEX_TYPE_ALLOCATION, ia, rc = ntfs_filldir(vol, filp, ndir, INDEX_TYPE_ALLOCATION, ia,
ie, name, dirent, filldir); ie, name, dirent, filldir);
if (rc) { if (rc) {
ntfs_unmap_page(page); ntfs_unmap_page(ia_page);
ntfs_unmap_page(bmp_page);
goto abort; goto abort;
} }
} }
goto find_next_index_buffer; goto find_next_index_buffer;
unm_EOD:
if (ia_page)
ntfs_unmap_page(ia_page);
ntfs_unmap_page(bmp_page);
EOD: EOD:
/* We are finished, set f_pos to EOD. */ /* We are finished, set f_pos to EOD. */
filp->f_pos = vdir->i_size + vol->mft_record_size; filp->f_pos = vdir->i_size + vol->mft_record_size;
...@@ -1331,24 +1353,21 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir) ...@@ -1331,24 +1353,21 @@ static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
rc, filp->f_pos); rc, filp->f_pos);
#endif #endif
return 0; return 0;
map_page_err_out:
ntfs_error(sb, "Reading index allocation data failed.");
err = PTR_ERR(page);
kf_unm_err_out:
kfree(name);
put_unm_err_out:
put_attr_search_ctx(ctx);
unm_err_out:
unmap_mft_record(READ, ndir);
err_out: err_out:
if (bmp_page)
ntfs_unmap_page(bmp_page);
if (ia_page)
ntfs_unmap_page(ia_page);
if (name)
kfree(name);
if (ctx)
put_attr_search_ctx(ctx);
if (m)
unmap_mft_record(READ, ndir);
if (!err)
err = -EIO;
ntfs_debug("Failed. Returning error code %i.", -err); ntfs_debug("Failed. Returning error code %i.", -err);
return err; return err;
unm_dir_err_out:
ntfs_unmap_page(page);
dir_err_out:
ntfs_error(sb, "Corrupt directory. Aborting. You should run chkdsk.");
err = -EIO;
goto kf_unm_err_out;
} }
struct file_operations ntfs_dir_ops = { struct file_operations ntfs_dir_ops = {
......
...@@ -329,12 +329,9 @@ static void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni) ...@@ -329,12 +329,9 @@ static void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
ni->attr_list_size = 0; ni->attr_list_size = 0;
ni->attr_list = NULL; ni->attr_list = NULL;
init_run_list(&ni->attr_list_rl); init_run_list(&ni->attr_list_rl);
ni->_IDM(bmp_ino) = NULL;
ni->_IDM(index_block_size) = 0; ni->_IDM(index_block_size) = 0;
ni->_IDM(index_vcn_size) = 0; ni->_IDM(index_vcn_size) = 0;
ni->_IDM(bmp_size) = 0;
ni->_IDM(bmp_initialized_size) = 0;
ni->_IDM(bmp_allocated_size) = 0;
init_run_list(&ni->_IDM(bmp_rl));
ni->_IDM(index_block_size_bits) = 0; ni->_IDM(index_block_size_bits) = 0;
ni->_IDM(index_vcn_size_bits) = 0; ni->_IDM(index_vcn_size_bits) = 0;
init_MUTEX(&ni->extent_lock); init_MUTEX(&ni->extent_lock);
...@@ -680,6 +677,8 @@ static int ntfs_read_locked_inode(struct inode *vi) ...@@ -680,6 +677,8 @@ static int ntfs_read_locked_inode(struct inode *vi)
* in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes. * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
*/ */
if (S_ISDIR(vi->i_mode)) { if (S_ISDIR(vi->i_mode)) {
struct inode *bvi;
ntfs_inode *bni;
INDEX_ROOT *ir; INDEX_ROOT *ir;
char *ir_end, *index_end; char *ir_end, *index_end;
...@@ -787,7 +786,8 @@ static int ntfs_read_locked_inode(struct inode *vi) ...@@ -787,7 +786,8 @@ static int ntfs_read_locked_inode(struct inode *vi)
if (!(ir->index.flags & LARGE_INDEX)) { if (!(ir->index.flags & LARGE_INDEX)) {
/* No index allocation. */ /* No index allocation. */
vi->i_size = ni->initialized_size = 0; vi->i_size = ni->initialized_size =
ni->allocated_size = 0;
goto skip_large_dir_stuff; goto skip_large_dir_stuff;
} /* LARGE_INDEX: Index allocation present. Setup state. */ } /* LARGE_INDEX: Index allocation present. Setup state. */
NInoSetIndexAllocPresent(ni); NInoSetIndexAllocPresent(ni);
...@@ -832,63 +832,31 @@ static int ntfs_read_locked_inode(struct inode *vi) ...@@ -832,63 +832,31 @@ static int ntfs_read_locked_inode(struct inode *vi)
ctx->attr->_ANR(initialized_size)); ctx->attr->_ANR(initialized_size));
ni->allocated_size = sle64_to_cpu( ni->allocated_size = sle64_to_cpu(
ctx->attr->_ANR(allocated_size)); ctx->attr->_ANR(allocated_size));
/* Find bitmap attribute. */
reinit_attr_search_ctx(ctx); /* Get the index bitmap attribute inode. */
if (!lookup_attr(AT_BITMAP, I30, 4, CASE_SENSITIVE, 0, NULL, 0, bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
ctx)) { if (unlikely(IS_ERR(bvi))) {
ntfs_error(vi->i_sb, "$BITMAP attribute is not " ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
"present but it must be."); err = PTR_ERR(bvi);
goto unm_err_out; goto unm_err_out;
} }
if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ni->_IDM(bmp_ino) = bvi;
ATTR_IS_ENCRYPTED | ATTR_IS_SPARSE)) { bni = NTFS_I(bvi);
if (NInoCompressed(bni) || NInoEncrypted(bni) ||
NInoSparse(bni)) {
ntfs_error(vi->i_sb, "$BITMAP attribute is compressed " ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
"and/or encrypted and/or sparse."); "and/or encrypted and/or sparse.");
goto unm_err_out; goto unm_err_out;
} }
if (ctx->attr->non_resident) {
NInoSetBmpNonResident(ni);
if (ctx->attr->_ANR(lowest_vcn)) {
ntfs_error(vi->i_sb, "First extent of $BITMAP "
"attribute has non zero "
"lowest_vcn. Inode is corrupt. "
"You should run chkdsk.");
goto unm_err_out;
}
ni->_IDM(bmp_size) = sle64_to_cpu(
ctx->attr->_ANR(data_size));
ni->_IDM(bmp_initialized_size) = sle64_to_cpu(
ctx->attr->_ANR(initialized_size));
ni->_IDM(bmp_allocated_size) = sle64_to_cpu(
ctx->attr->_ANR(allocated_size));
/*
* Setup the run list. No need for locking as we have
* exclusive access to the inode at this time.
*/
ni->_IDM(bmp_rl).rl = decompress_mapping_pairs(vol,
ctx->attr, NULL);
if (IS_ERR(ni->_IDM(bmp_rl).rl)) {
err = PTR_ERR(ni->_IDM(bmp_rl).rl);
ni->_IDM(bmp_rl).rl = NULL;
ntfs_error(vi->i_sb, "Mapping pairs "
"decompression failed with "
"error code %i.", -err);
goto unm_err_out;
}
} else
ni->_IDM(bmp_size) = ni->_IDM(bmp_initialized_size) =
ni->_IDM(bmp_allocated_size) =
le32_to_cpu(
ctx->attr->_ARA(value_length));
/* Consistency check bitmap size vs. index allocation size. */ /* Consistency check bitmap size vs. index allocation size. */
if (ni->_IDM(bmp_size) << 3 < vi->i_size >> if ((bvi->i_size << 3) < (vi->i_size >>
ni->_IDM(index_block_size_bits)) { ni->_IDM(index_block_size_bits))) {
ntfs_error(vi->i_sb, "$I30 bitmap too small (0x%Lx) " ntfs_error(vi->i_sb, "Index bitmap too small (0x%Lx) "
"for index allocation (0x%Lx).", "for index allocation (0x%Lx).",
(long long)ni->_IDM(bmp_size) << 3, bvi->i_size << 3, vi->i_size);
vi->i_size);
goto unm_err_out; goto unm_err_out;
} }
skip_large_dir_stuff: skip_large_dir_stuff:
/* Everyone gets read and scan permissions. */ /* Everyone gets read and scan permissions. */
vi->i_mode |= S_IRUGO | S_IXUGO; vi->i_mode |= S_IRUGO | S_IXUGO;
...@@ -1271,7 +1239,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi) ...@@ -1271,7 +1239,7 @@ static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
"will probably cause problems " "will probably cause problems "
"when trying to access the " "when trying to access the "
"file. Please notify " "file. Please notify "
"linux-ntfs-dev@ lists.sf.net " "linux-ntfs-dev@lists.sf.net "
"that you saw this message."); "that you saw this message.");
} }
} }
...@@ -1785,6 +1753,32 @@ int ntfs_commit_inode(ntfs_inode *ni) ...@@ -1785,6 +1753,32 @@ int ntfs_commit_inode(ntfs_inode *ni)
return 0; return 0;
} }
/**
* ntfs_put_inode - handler for when the inode reference count is decremented
* @vi: vfs inode
*
* The VFS calls ntfs_put_inode() every time the inode reference count (i_count)
* is about to be decremented (but before the decrement itself.
*
* If the inode @vi is a directory with a single reference, we need to put the
* attribute inode for the directory index bitmap, if it is present, otherwise
* the directory inode would remain pinned for ever (or rather until umount()
* time.
*/
void ntfs_put_inode(struct inode *vi)
{
if (S_ISDIR(vi->i_mode) && (atomic_read(&vi->i_count) == 2)) {
ntfs_inode *ni;
ni = NTFS_I(vi);
if (NInoIndexAllocPresent(ni) && ni->_IDM(bmp_ino)) {
iput(ni->_IDM(bmp_ino));
ni->_IDM(bmp_ino) = NULL;
}
}
return;
}
void __ntfs_clear_inode(ntfs_inode *ni) void __ntfs_clear_inode(ntfs_inode *ni)
{ {
int err; int err;
...@@ -1866,12 +1860,7 @@ void ntfs_clear_big_inode(struct inode *vi) ...@@ -1866,12 +1860,7 @@ void ntfs_clear_big_inode(struct inode *vi)
__ntfs_clear_inode(ni); __ntfs_clear_inode(ni);
if (S_ISDIR(vi->i_mode)) { if (NInoAttr(ni)) {
down_write(&ni->_IDM(bmp_rl).lock);
if (ni->_IDM(bmp_rl).rl)
ntfs_free(ni->_IDM(bmp_rl).rl);
up_write(&ni->_IDM(bmp_rl).lock);
} else if (NInoAttr(ni)) {
/* Release the base inode if we are holding it. */ /* Release the base inode if we are holding it. */
if (ni->nr_extents == -1) { if (ni->nr_extents == -1) {
iput(VFS_I(ni->_INE(base_ntfs_ino))); iput(VFS_I(ni->_INE(base_ntfs_ino)));
......
...@@ -92,14 +92,11 @@ struct _ntfs_inode { ...@@ -92,14 +92,11 @@ struct _ntfs_inode {
run_list attr_list_rl; /* Run list for the attribute list value. */ run_list attr_list_rl; /* Run list for the attribute list value. */
union { union {
struct { /* It is a directory or $MFT. */ struct { /* It is a directory or $MFT. */
struct inode *bmp_ino; /* Attribute inode for the
directory index $BITMAP. */
u32 index_block_size; /* Size of an index block. */ u32 index_block_size; /* Size of an index block. */
u32 index_vcn_size; /* Size of a vcn in this u32 index_vcn_size; /* Size of a vcn in this
directory index. */ directory index. */
s64 bmp_size; /* Size of the $I30 bitmap. */
s64 bmp_initialized_size; /* Copy from $I30 bitmap. */
s64 bmp_allocated_size; /* Copy from $I30 bitmap. */
run_list bmp_rl; /* Run list for the $I30 bitmap
if it is non-resident. */
u8 index_block_size_bits; /* Log2 of the above. */ u8 index_block_size_bits; /* Log2 of the above. */
u8 index_vcn_size_bits; /* Log2 of the above. */ u8 index_vcn_size_bits; /* Log2 of the above. */
} SN(idm); } SN(idm);
...@@ -165,7 +162,6 @@ typedef enum { ...@@ -165,7 +162,6 @@ typedef enum {
NI_Sparse, /* 1: Unnamed data attr is sparse (f). NI_Sparse, /* 1: Unnamed data attr is sparse (f).
1: Create sparse files by default (d). 1: Create sparse files by default (d).
1: Attribute is sparse (a). */ 1: Attribute is sparse (a). */
NI_BmpNonResident, /* 1: $I30 bitmap attr is non resident (d). */
} ntfs_inode_state_bits; } ntfs_inode_state_bits;
/* /*
...@@ -203,7 +199,6 @@ NINO_FNS(IndexAllocPresent) ...@@ -203,7 +199,6 @@ NINO_FNS(IndexAllocPresent)
NINO_FNS(Compressed) NINO_FNS(Compressed)
NINO_FNS(Encrypted) NINO_FNS(Encrypted)
NINO_FNS(Sparse) NINO_FNS(Sparse)
NINO_FNS(BmpNonResident)
/* /*
* The full structure containing a ntfs_inode and a vfs struct inode. Used for * The full structure containing a ntfs_inode and a vfs struct inode. Used for
...@@ -247,6 +242,8 @@ extern void ntfs_read_inode_mount(struct inode *vi); ...@@ -247,6 +242,8 @@ extern void ntfs_read_inode_mount(struct inode *vi);
extern void ntfs_dirty_inode(struct inode *vi); extern void ntfs_dirty_inode(struct inode *vi);
extern void ntfs_put_inode(struct inode *vi);
extern int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt); extern int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt);
#endif /* _LINUX_NTFS_FS_INODE_H */ #endif /* _LINUX_NTFS_FS_INODE_H */
......
...@@ -1263,10 +1263,8 @@ struct super_operations ntfs_sops = { ...@@ -1263,10 +1263,8 @@ struct super_operations ntfs_sops = {
dirty_inode: ntfs_dirty_inode, /* VFS: Called from dirty_inode: ntfs_dirty_inode, /* VFS: Called from
__mark_inode_dirty(). */ __mark_inode_dirty(). */
//write_inode: NULL, /* VFS: Write dirty inode to disk. */ //write_inode: NULL, /* VFS: Write dirty inode to disk. */
//put_inode: NULL, /* VFS: Called whenever the reference put_inode: ntfs_put_inode, /* VFS: Called just before the inode
// count (i_count) of the inode is reference count is decreased. */
// going to be decreased but before the
// actual decrease. */
//delete_inode: NULL, /* VFS: Delete inode from disk. Called //delete_inode: NULL, /* VFS: Delete inode from disk. Called
// when i_count becomes 0 and i_nlink is // when i_count becomes 0 and i_nlink is
// also 0. */ // also 0. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment