Skip to content

Commit 1842fbc

Browse files
fs/ntfs3: Fix ntfs_create_inode()
Previous variant creates an inode that requires update the parent directory (ea_packed_size). Operations in ntfs_create_inode have been rearranged so we insert new directory entry with correct ea_packed_size and new created inode does not require update it's parent directory. Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
1 parent 267a36b commit 1842fbc

4 files changed

Lines changed: 58 additions & 49 deletions

File tree

fs/ntfs3/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ int ntfs3_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
703703
}
704704

705705
if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
706-
ntfs_save_wsl_perm(inode);
706+
ntfs_save_wsl_perm(inode, NULL);
707707
mark_inode_dirty(inode);
708708
out:
709709
return err;

fs/ntfs3/inode.c

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,8 +1320,7 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap,
13201320
inode_init_owner(idmap, inode, dir, mode);
13211321
mode = inode->i_mode;
13221322

1323-
inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1324-
current_time(inode);
1323+
ni->i_crtime = current_time(inode);
13251324

13261325
rec = ni->mi.mrec;
13271326
rec->hard_links = cpu_to_le16(1);
@@ -1362,10 +1361,9 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap,
13621361
attr->res.data_size = cpu_to_le32(dsize);
13631362

13641363
std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1365-
kernel2nt(&inode->i_atime);
1364+
kernel2nt(&ni->i_crtime);
13661365

1367-
ni->std_fa = fa;
1368-
std5->fa = fa;
1366+
std5->fa = ni->std_fa = fa;
13691367

13701368
attr = Add2Ptr(attr, asize);
13711369

@@ -1564,11 +1562,15 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap,
15641562
}
15651563

15661564
asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1565+
/* Write non resident data. */
1566+
err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp,
1567+
nsize, 0);
1568+
if (err)
1569+
goto out5;
15671570
} else {
15681571
attr->res.data_off = SIZEOF_RESIDENT_LE;
15691572
attr->res.data_size = cpu_to_le32(nsize);
15701573
memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1571-
nsize = 0;
15721574
}
15731575
/* Size of symlink equals the length of input string. */
15741576
inode->i_size = size;
@@ -1589,19 +1591,8 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap,
15891591
rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
15901592
rec->next_attr_id = cpu_to_le16(aid);
15911593

1592-
/* Step 2: Add new name in index. */
1593-
err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1594-
if (err)
1595-
goto out6;
1596-
1597-
/* Unlock parent directory before ntfs_init_acl. */
1598-
if (!fnd)
1599-
ni_unlock(dir_ni);
1600-
16011594
inode->i_generation = le16_to_cpu(rec->seq);
16021595

1603-
dir->i_mtime = dir->i_ctime = inode->i_atime;
1604-
16051596
if (S_ISDIR(mode)) {
16061597
inode->i_op = &ntfs_dir_inode_operations;
16071598
inode->i_fop = &ntfs_dir_operations;
@@ -1626,41 +1617,58 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap,
16261617
if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
16271618
err = ntfs_init_acl(idmap, inode, dir);
16281619
if (err)
1629-
goto out7;
1620+
goto out5;
16301621
} else
16311622
#endif
16321623
{
16331624
inode->i_flags |= S_NOSEC;
16341625
}
16351626

1636-
/* Write non resident data. */
1637-
if (nsize) {
1638-
err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1639-
if (err)
1640-
goto out7;
1627+
/*
1628+
* ntfs_init_acl and ntfs_save_wsl_perm update extended attribute.
1629+
* The packed size of extended attribute is stored in direntry too.
1630+
* 'fname' here points to inside new_de.
1631+
*/
1632+
ntfs_save_wsl_perm(inode, &fname->dup.ea_size);
1633+
1634+
/*
1635+
* update ea_size in file_name attribute too.
1636+
* Use ni_find_attr cause layout of MFT record may be changed
1637+
* in ntfs_init_acl and ntfs_save_wsl_perm.
1638+
*/
1639+
attr = ni_find_attr(ni, NULL, NULL, ATTR_NAME, NULL, 0, NULL, NULL);
1640+
if (attr) {
1641+
struct ATTR_FILE_NAME *fn;
1642+
1643+
fn = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1644+
if (fn)
1645+
fn->dup.ea_size = fname->dup.ea_size;
16411646
}
16421647

1648+
/* We do not need to update parent directory later */
1649+
ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
1650+
1651+
/* Step 2: Add new name in index. */
1652+
err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1653+
if (err)
1654+
goto out6;
1655+
16431656
/*
16441657
* Call 'd_instantiate' after inode->i_op is set
16451658
* but before finish_open.
16461659
*/
16471660
d_instantiate(dentry, inode);
16481661

1649-
ntfs_save_wsl_perm(inode);
1662+
/* Set original time. inode times (i_ctime) may be changed in ntfs_init_acl. */
1663+
inode->i_atime = inode->i_mtime = inode->i_ctime = dir->i_mtime =
1664+
dir->i_ctime = ni->i_crtime;
1665+
16501666
mark_inode_dirty(dir);
16511667
mark_inode_dirty(inode);
16521668

16531669
/* Normal exit. */
16541670
goto out2;
16551671

1656-
out7:
1657-
1658-
/* Undo 'indx_insert_entry'. */
1659-
if (!fnd)
1660-
ni_lock_dir(dir_ni);
1661-
indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1662-
le16_to_cpu(new_de->key_size), sbi);
1663-
/* ni_unlock(dir_ni); will be called later. */
16641672
out6:
16651673
if (rp_inserted)
16661674
ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
@@ -1682,11 +1690,11 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap,
16821690
kfree(rp);
16831691

16841692
out1:
1685-
if (err) {
1686-
if (!fnd)
1687-
ni_unlock(dir_ni);
1693+
if (!fnd)
1694+
ni_unlock(dir_ni);
1695+
1696+
if (err)
16881697
return ERR_PTR(err);
1689-
}
16901698

16911699
unlock_new_inode(inode);
16921700

@@ -1783,9 +1791,6 @@ void ntfs_evict_inode(struct inode *inode)
17831791
{
17841792
truncate_inode_pages_final(&inode->i_data);
17851793

1786-
if (inode->i_nlink)
1787-
_ni_write_inode(inode, inode_needs_sync(inode));
1788-
17891794
invalidate_inode_buffers(inode);
17901795
clear_inode(inode);
17911796

fs/ntfs3/ntfs_fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry);
872872
ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
873873
extern const struct xattr_handler *ntfs_xattr_handlers[];
874874

875-
int ntfs_save_wsl_perm(struct inode *inode);
875+
int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size);
876876
void ntfs_get_wsl_perm(struct inode *inode);
877877

878878
/* globals from lznt.c */

fs/ntfs3/xattr.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
296296

297297
static noinline int ntfs_set_ea(struct inode *inode, const char *name,
298298
size_t name_len, const void *value,
299-
size_t val_size, int flags, bool locked)
299+
size_t val_size, int flags, bool locked,
300+
__le16 *ea_size)
300301
{
301302
struct ntfs_inode *ni = ntfs_i(inode);
302303
struct ntfs_sb_info *sbi = ni->mi.sbi;
@@ -504,6 +505,8 @@ static noinline int ntfs_set_ea(struct inode *inode, const char *name,
504505

505506
if (ea_info.size_pack != size_pack)
506507
ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
508+
if (ea_size)
509+
*ea_size = ea_info.size_pack;
507510
mark_inode_dirty(&ni->vfs_inode);
508511

509512
out:
@@ -633,7 +636,7 @@ static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
633636
flags = 0;
634637
}
635638

636-
err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0);
639+
err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0, NULL);
637640
if (err == -ENODATA && !size)
638641
err = 0; /* Removing non existed xattr. */
639642
if (!err) {
@@ -923,7 +926,8 @@ static noinline int ntfs_setxattr(const struct xattr_handler *handler,
923926
}
924927

925928
/* Deal with NTFS extended attribute. */
926-
err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0);
929+
err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0,
930+
NULL);
927931

928932
out:
929933
inode->i_ctime = current_time(inode);
@@ -937,7 +941,7 @@ static noinline int ntfs_setxattr(const struct xattr_handler *handler,
937941
*
938942
* save uid/gid/mode in xattr
939943
*/
940-
int ntfs_save_wsl_perm(struct inode *inode)
944+
int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size)
941945
{
942946
int err;
943947
__le32 value;
@@ -946,26 +950,26 @@ int ntfs_save_wsl_perm(struct inode *inode)
946950
ni_lock(ni);
947951
value = cpu_to_le32(i_uid_read(inode));
948952
err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
949-
sizeof(value), 0, true); /* true == already locked. */
953+
sizeof(value), 0, true, ea_size);
950954
if (err)
951955
goto out;
952956

953957
value = cpu_to_le32(i_gid_read(inode));
954958
err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
955-
sizeof(value), 0, true);
959+
sizeof(value), 0, true, ea_size);
956960
if (err)
957961
goto out;
958962

959963
value = cpu_to_le32(inode->i_mode);
960964
err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
961-
sizeof(value), 0, true);
965+
sizeof(value), 0, true, ea_size);
962966
if (err)
963967
goto out;
964968

965969
if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
966970
value = cpu_to_le32(inode->i_rdev);
967971
err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
968-
sizeof(value), 0, true);
972+
sizeof(value), 0, true, ea_size);
969973
if (err)
970974
goto out;
971975
}

0 commit comments

Comments
 (0)