Skip to content

Commit 807c453

Browse files
NTFS: Fix handling of sparse attributes in ntfs_attr_make_non_resident().
Also, add BUG() checks to ntfs_attr_make_non_resident() and ntfs_attr_set() to ensure that these functions are never called for compressed or encrypted attributes. Signed-off-by: Anton Altaparmakov <aia21@cantab.net>
1 parent 2983d1b commit 807c453

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

‎fs/ntfs/ChangeLog‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ ToDo/Notes:
6666
return LCN_ENOENT when there is no runlist and the allocated size is
6767
zero.
6868
- Fix load_attribute_list() to handle the case of a NULL runlist.
69+
- Fix handling of sparse attributes in ntfs_attr_make_non_resident().
70+
- Add BUG() checks to ntfs_attr_make_non_resident() and ntfs_attr_set()
71+
to ensure that these functions are never called for compressed or
72+
encrypted attributes.
6973

7074
2.1.23 - Implement extension of resident files and make writing safe as well as
7175
many bug fixes, cleanups, and enhancements...

‎fs/ntfs/attrib.c‎

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,12 @@ int ntfs_attr_make_non_resident(ntfs_inode *ni)
13711371
"volume!");
13721372
return err;
13731373
}
1374+
/*
1375+
* FIXME: Compressed and encrypted attributes are not supported when
1376+
* writing and we should never have gotten here for them.
1377+
*/
1378+
BUG_ON(NInoCompressed(ni));
1379+
BUG_ON(NInoEncrypted(ni));
13741380
/*
13751381
* The size needs to be aligned to a cluster boundary for allocation
13761382
* purposes.
@@ -1447,10 +1453,15 @@ int ntfs_attr_make_non_resident(ntfs_inode *ni)
14471453
BUG_ON(a->non_resident);
14481454
/*
14491455
* Calculate new offsets for the name and the mapping pairs array.
1450-
* We assume the attribute is not compressed or sparse.
14511456
*/
1452-
name_ofs = (offsetof(ATTR_REC,
1453-
data.non_resident.compressed_size) + 7) & ~7;
1457+
if (NInoSparse(ni) || NInoCompressed(ni))
1458+
name_ofs = (offsetof(ATTR_REC,
1459+
data.non_resident.compressed_size) +
1460+
sizeof(a->data.non_resident.compressed_size) +
1461+
7) & ~7;
1462+
else
1463+
name_ofs = (offsetof(ATTR_REC,
1464+
data.non_resident.compressed_size) + 7) & ~7;
14541465
mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
14551466
/*
14561467
* Determine the size of the resident part of the now non-resident
@@ -1489,24 +1500,23 @@ int ntfs_attr_make_non_resident(ntfs_inode *ni)
14891500
memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
14901501
a->name_length * sizeof(ntfschar));
14911502
a->name_offset = cpu_to_le16(name_ofs);
1492-
/*
1493-
* FIXME: For now just clear all of these as we do not support them
1494-
* when writing.
1495-
*/
1496-
a->flags &= cpu_to_le16(0xffff & ~le16_to_cpu(ATTR_IS_SPARSE |
1497-
ATTR_IS_ENCRYPTED | ATTR_COMPRESSION_MASK));
14981503
/* Setup the fields specific to non-resident attributes. */
14991504
a->data.non_resident.lowest_vcn = 0;
15001505
a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
15011506
vol->cluster_size_bits);
15021507
a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1503-
a->data.non_resident.compression_unit = 0;
15041508
memset(&a->data.non_resident.reserved, 0,
15051509
sizeof(a->data.non_resident.reserved));
15061510
a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
15071511
a->data.non_resident.data_size =
15081512
a->data.non_resident.initialized_size =
15091513
cpu_to_sle64(attr_size);
1514+
if (NInoSparse(ni) || NInoCompressed(ni)) {
1515+
a->data.non_resident.compression_unit = 4;
1516+
a->data.non_resident.compressed_size =
1517+
a->data.non_resident.allocated_size;
1518+
} else
1519+
a->data.non_resident.compression_unit = 0;
15101520
/* Generate the mapping pairs array into the attribute record. */
15111521
err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
15121522
arec_size - mp_ofs, rl, 0, -1, NULL);
@@ -1516,16 +1526,19 @@ int ntfs_attr_make_non_resident(ntfs_inode *ni)
15161526
goto undo_err_out;
15171527
}
15181528
/* Setup the in-memory attribute structure to be non-resident. */
1519-
/*
1520-
* FIXME: For now just clear all of these as we do not support them
1521-
* when writing.
1522-
*/
1523-
NInoClearSparse(ni);
1524-
NInoClearEncrypted(ni);
1525-
NInoClearCompressed(ni);
15261529
ni->runlist.rl = rl;
15271530
write_lock_irqsave(&ni->size_lock, flags);
15281531
ni->allocated_size = new_size;
1532+
if (NInoSparse(ni) || NInoCompressed(ni)) {
1533+
ni->itype.compressed.size = ni->allocated_size;
1534+
ni->itype.compressed.block_size = 1U <<
1535+
(a->data.non_resident.compression_unit +
1536+
vol->cluster_size_bits);
1537+
ni->itype.compressed.block_size_bits =
1538+
ffs(ni->itype.compressed.block_size) - 1;
1539+
ni->itype.compressed.block_clusters = 1U <<
1540+
a->data.non_resident.compression_unit;
1541+
}
15291542
write_unlock_irqrestore(&ni->size_lock, flags);
15301543
/*
15311544
* This needs to be last since the address space operations ->readpage
@@ -1673,6 +1686,12 @@ int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
16731686
BUG_ON(cnt < 0);
16741687
if (!cnt)
16751688
goto done;
1689+
/*
1690+
* FIXME: Compressed and encrypted attributes are not supported when
1691+
* writing and we should never have gotten here for them.
1692+
*/
1693+
BUG_ON(NInoCompressed(ni));
1694+
BUG_ON(NInoEncrypted(ni));
16761695
mapping = VFS_I(ni)->i_mapping;
16771696
/* Work out the starting index and page offset. */
16781697
idx = ofs >> PAGE_CACHE_SHIFT;

0 commit comments

Comments
 (0)