2021-08-13 17:21:29 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/nls.h>
|
2022-09-23 12:42:18 +03:00
|
|
|
#include <linux/ctype.h>
|
2022-10-07 19:53:30 +03:00
|
|
|
#include <linux/posix_acl.h>
|
2021-08-13 17:21:29 +03:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "ntfs.h"
|
|
|
|
#include "ntfs_fs.h"
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* fill_name_de - Format NTFS_DE in @buf.
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
|
|
|
int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
|
|
|
|
const struct cpu_str *uni)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct NTFS_DE *e = buf;
|
|
|
|
u16 data_size;
|
|
|
|
struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
|
|
|
|
|
|
|
|
#ifndef CONFIG_NTFS3_64BIT_CLUSTER
|
|
|
|
e->ref.high = fname->home.high = 0;
|
|
|
|
#endif
|
|
|
|
if (uni) {
|
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
int ulen = uni->len;
|
|
|
|
__le16 *uname = fname->name;
|
|
|
|
const u16 *name_cpu = uni->name;
|
|
|
|
|
|
|
|
while (ulen--)
|
|
|
|
*uname++ = cpu_to_le16(*name_cpu++);
|
|
|
|
#else
|
|
|
|
memcpy(fname->name, uni->name, uni->len * sizeof(u16));
|
|
|
|
#endif
|
|
|
|
fname->name_len = uni->len;
|
|
|
|
|
|
|
|
} else {
|
2021-08-03 14:57:09 +03:00
|
|
|
/* Convert input string to unicode. */
|
2021-08-13 17:21:29 +03:00
|
|
|
err = ntfs_nls_to_utf16(sbi, name->name, name->len,
|
|
|
|
(struct cpu_str *)&fname->name_len,
|
|
|
|
NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
fname->type = FILE_NAME_POSIX;
|
|
|
|
data_size = fname_full_size(fname);
|
|
|
|
|
fs/ntfs3: Use kernel ALIGN macros over driver specific
The static checkers (Smatch) were complaining because QuadAlign() was
buggy. If you try to align something higher than UINT_MAX it got
truncated to a u32.
Smatch warning was:
fs/ntfs3/attrib.c:383 attr_set_size_res()
warn: was expecting a 64 bit value instead of '~7'
So that this will not happen again we will change all these macros to
kernel made ones. This can also help some other static analyzing tools
to give us better warnings.
Patch was generated with Coccinelle script and after that some style
issue was hand fixed.
Coccinelle script:
virtual patch
@alloc depends on patch@
expression x;
@@
(
- #define QuadAlign(n) (((n) + 7u) & (~7u))
|
- QuadAlign(x)
+ ALIGN(x, 8)
|
- #define IsQuadAligned(n) (!((size_t)(n)&7u))
|
- IsQuadAligned(x)
+ IS_ALIGNED(x, 8)
|
- #define Quad2Align(n) (((n) + 15u) & (~15u))
|
- Quad2Align(x)
+ ALIGN(x, 16)
|
- #define IsQuad2Aligned(n) (!((size_t)(n)&15u))
|
- IsQuad2Aligned(x)
+ IS_ALIGNED(x, 16)
|
- #define Quad4Align(n) (((n) + 31u) & (~31u))
|
- Quad4Align(x)
+ ALIGN(x, 32)
|
- #define IsSizeTAligned(n) (!((size_t)(n) & (sizeof(size_t) - 1)))
|
- IsSizeTAligned(x)
+ IS_ALIGNED(x, sizeof(size_t))
|
- #define DwordAlign(n) (((n) + 3u) & (~3u))
|
- DwordAlign(x)
+ ALIGN(x, 4)
|
- #define IsDwordAligned(n) (!((size_t)(n)&3u))
|
- IsDwordAligned(x)
+ IS_ALIGNED(x, 4)
|
- #define WordAlign(n) (((n) + 1u) & (~1u))
|
- WordAlign(x)
+ ALIGN(x, 2)
|
- #define IsWordAligned(n) (!((size_t)(n)&1u))
|
- IsWordAligned(x)
+ IS_ALIGNED(x, 2)
|
)
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
2021-08-26 11:56:29 +03:00
|
|
|
e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
|
2021-08-13 17:21:29 +03:00
|
|
|
e->key_size = cpu_to_le16(data_size);
|
|
|
|
e->flags = 0;
|
|
|
|
e->res = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_lookup - inode_operations::lookup
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
|
|
|
static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
|
|
|
|
u32 flags)
|
|
|
|
{
|
|
|
|
struct ntfs_inode *ni = ntfs_i(dir);
|
|
|
|
struct cpu_str *uni = __getname();
|
|
|
|
struct inode *inode;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!uni)
|
|
|
|
inode = ERR_PTR(-ENOMEM);
|
|
|
|
else {
|
|
|
|
err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
|
|
|
|
dentry->d_name.len, uni, NTFS_NAME_LEN,
|
|
|
|
UTF16_HOST_ENDIAN);
|
|
|
|
if (err < 0)
|
|
|
|
inode = ERR_PTR(err);
|
|
|
|
else {
|
2024-08-28 11:55:53 +03:00
|
|
|
ni_lock_dir(ni);
|
2021-08-13 17:21:29 +03:00
|
|
|
inode = dir_search_u(dir, uni, NULL);
|
|
|
|
ni_unlock(ni);
|
|
|
|
}
|
|
|
|
__putname(uni);
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:21:59 +00:00
|
|
|
/*
|
|
|
|
* Check for a null pointer
|
|
|
|
* If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
|
|
|
|
* This causes null pointer dereference in d_splice_alias().
|
|
|
|
*/
|
2022-12-30 14:58:25 +04:00
|
|
|
if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
|
2022-11-25 10:21:59 +00:00
|
|
|
iput(inode);
|
|
|
|
inode = ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
return d_splice_alias(inode, dentry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_create - inode_operations::create
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
2023-01-13 12:49:13 +01:00
|
|
|
static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
|
2021-08-13 17:21:29 +03:00
|
|
|
struct dentry *dentry, umode_t mode, bool excl)
|
|
|
|
{
|
2024-04-16 09:52:47 +03:00
|
|
|
return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
|
|
|
|
NULL, 0, NULL);
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-06-26 15:26:18 +03:00
|
|
|
* ntfs_mknod - inode_operations::mknod
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
2023-01-13 12:49:16 +01:00
|
|
|
static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
|
2021-08-13 17:21:29 +03:00
|
|
|
struct dentry *dentry, umode_t mode, dev_t rdev)
|
|
|
|
{
|
2024-04-16 09:52:47 +03:00
|
|
|
return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
|
|
|
|
NULL);
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_link - inode_operations::link
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
|
|
|
static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct inode *inode = d_inode(ode);
|
|
|
|
struct ntfs_inode *ni = ntfs_i(inode);
|
|
|
|
|
|
|
|
if (S_ISDIR(inode->i_mode))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (inode->i_nlink >= NTFS_LINK_MAX)
|
|
|
|
return -EMLINK;
|
|
|
|
|
|
|
|
ni_lock_dir(ntfs_i(dir));
|
|
|
|
if (inode != dir)
|
|
|
|
ni_lock(ni);
|
|
|
|
|
|
|
|
inc_nlink(inode);
|
|
|
|
ihold(inode);
|
|
|
|
|
|
|
|
err = ntfs_link_inode(inode, de);
|
2021-08-31 18:52:39 +03:00
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
if (!err) {
|
2023-10-04 14:52:40 -04:00
|
|
|
inode_set_ctime_current(inode);
|
|
|
|
inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
|
2021-08-13 17:21:29 +03:00
|
|
|
mark_inode_dirty(inode);
|
|
|
|
mark_inode_dirty(dir);
|
|
|
|
d_instantiate(de, inode);
|
|
|
|
} else {
|
|
|
|
drop_nlink(inode);
|
|
|
|
iput(inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inode != dir)
|
|
|
|
ni_unlock(ni);
|
|
|
|
ni_unlock(ntfs_i(dir));
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_unlink - inode_operations::unlink
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
|
|
|
static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ntfs_inode *ni = ntfs_i(dir);
|
|
|
|
int err;
|
|
|
|
|
2025-06-24 16:35:39 +03:00
|
|
|
/* Avoid any operation if inode is bad. */
|
|
|
|
if (unlikely(is_bad_ni(ni)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2023-11-24 12:19:37 +03:00
|
|
|
if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
|
|
|
|
return -EIO;
|
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
ni_lock_dir(ni);
|
|
|
|
|
|
|
|
err = ntfs_unlink_inode(dir, dentry);
|
|
|
|
|
|
|
|
ni_unlock(ni);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_symlink - inode_operations::symlink
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
2023-01-13 12:49:14 +01:00
|
|
|
static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
|
2021-08-13 17:21:29 +03:00
|
|
|
struct dentry *dentry, const char *symname)
|
|
|
|
{
|
|
|
|
u32 size = strlen(symname);
|
|
|
|
|
2025-06-24 16:35:39 +03:00
|
|
|
/* Avoid any operation if inode is bad. */
|
|
|
|
if (unlikely(is_bad_ni(ntfs_i(dir))))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2023-11-24 12:19:37 +03:00
|
|
|
if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
|
|
|
|
return -EIO;
|
|
|
|
|
2024-04-16 09:52:47 +03:00
|
|
|
return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
|
|
|
|
symname, size, NULL);
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_mkdir- inode_operations::mkdir
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
Change inode_operations.mkdir to return struct dentry *
Some filesystems, such as NFS, cifs, ceph, and fuse, do not have
complete control of sequencing on the actual filesystem (e.g. on a
different server) and may find that the inode created for a mkdir
request already exists in the icache and dcache by the time the mkdir
request returns. For example, if the filesystem is mounted twice the
directory could be visible on the other mount before it is on the
original mount, and a pair of name_to_handle_at(), open_by_handle_at()
calls could instantiate the directory inode with an IS_ROOT() dentry
before the first mkdir returns.
This means that the dentry passed to ->mkdir() may not be the one that
is associated with the inode after the ->mkdir() completes. Some
callers need to interact with the inode after the ->mkdir completes and
they currently need to perform a lookup in the (rare) case that the
dentry is no longer hashed.
This lookup-after-mkdir requires that the directory remains locked to
avoid races. Planned future patches to lock the dentry rather than the
directory will mean that this lookup cannot be performed atomically with
the mkdir.
To remove this barrier, this patch changes ->mkdir to return the
resulting dentry if it is different from the one passed in.
Possible returns are:
NULL - the directory was created and no other dentry was used
ERR_PTR() - an error occurred
non-NULL - this other dentry was spliced in
This patch only changes file-systems to return "ERR_PTR(err)" instead of
"err" or equivalent transformations. Subsequent patches will make
further changes to some file-systems to return a correct dentry.
Not all filesystems reliably result in a positive hashed dentry:
- NFS, cifs, hostfs will sometimes need to perform a lookup of
the name to get inode information. Races could result in this
returning something different. Note that this lookup is
non-atomic which is what we are trying to avoid. Placing the
lookup in filesystem code means it only happens when the filesystem
has no other option.
- kernfs and tracefs leave the dentry negative and the ->revalidate
operation ensures that lookup will be called to correctly populate
the dentry. This could be fixed but I don't think it is important
to any of the users of vfs_mkdir() which look at the dentry.
The recommendation to use
d_drop();d_splice_alias()
is ugly but fits with current practice. A planned future patch will
change this.
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: NeilBrown <neilb@suse.de>
Link: https://lore.kernel.org/r/20250227013949.536172-2-neilb@suse.de
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-02-27 12:32:53 +11:00
|
|
|
static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
|
|
|
|
struct dentry *dentry, umode_t mode)
|
2021-08-13 17:21:29 +03:00
|
|
|
{
|
Change inode_operations.mkdir to return struct dentry *
Some filesystems, such as NFS, cifs, ceph, and fuse, do not have
complete control of sequencing on the actual filesystem (e.g. on a
different server) and may find that the inode created for a mkdir
request already exists in the icache and dcache by the time the mkdir
request returns. For example, if the filesystem is mounted twice the
directory could be visible on the other mount before it is on the
original mount, and a pair of name_to_handle_at(), open_by_handle_at()
calls could instantiate the directory inode with an IS_ROOT() dentry
before the first mkdir returns.
This means that the dentry passed to ->mkdir() may not be the one that
is associated with the inode after the ->mkdir() completes. Some
callers need to interact with the inode after the ->mkdir completes and
they currently need to perform a lookup in the (rare) case that the
dentry is no longer hashed.
This lookup-after-mkdir requires that the directory remains locked to
avoid races. Planned future patches to lock the dentry rather than the
directory will mean that this lookup cannot be performed atomically with
the mkdir.
To remove this barrier, this patch changes ->mkdir to return the
resulting dentry if it is different from the one passed in.
Possible returns are:
NULL - the directory was created and no other dentry was used
ERR_PTR() - an error occurred
non-NULL - this other dentry was spliced in
This patch only changes file-systems to return "ERR_PTR(err)" instead of
"err" or equivalent transformations. Subsequent patches will make
further changes to some file-systems to return a correct dentry.
Not all filesystems reliably result in a positive hashed dentry:
- NFS, cifs, hostfs will sometimes need to perform a lookup of
the name to get inode information. Races could result in this
returning something different. Note that this lookup is
non-atomic which is what we are trying to avoid. Placing the
lookup in filesystem code means it only happens when the filesystem
has no other option.
- kernfs and tracefs leave the dentry negative and the ->revalidate
operation ensures that lookup will be called to correctly populate
the dentry. This could be fixed but I don't think it is important
to any of the users of vfs_mkdir() which look at the dentry.
The recommendation to use
d_drop();d_splice_alias()
is ugly but fits with current practice. A planned future patch will
change this.
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: NeilBrown <neilb@suse.de>
Link: https://lore.kernel.org/r/20250227013949.536172-2-neilb@suse.de
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-02-27 12:32:53 +11:00
|
|
|
return ERR_PTR(ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
|
|
|
|
NULL, 0, NULL));
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-06-30 18:49:24 +03:00
|
|
|
* ntfs_rmdir - inode_operations::rmdir
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
|
|
|
static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ntfs_inode *ni = ntfs_i(dir);
|
|
|
|
int err;
|
|
|
|
|
2025-06-24 16:35:39 +03:00
|
|
|
/* Avoid any operation if inode is bad. */
|
|
|
|
if (unlikely(is_bad_ni(ni)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2023-11-24 12:19:37 +03:00
|
|
|
if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
|
|
|
|
return -EIO;
|
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
ni_lock_dir(ni);
|
|
|
|
|
|
|
|
err = ntfs_unlink_inode(dir, dentry);
|
|
|
|
|
|
|
|
ni_unlock(ni);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-08-03 14:57:09 +03:00
|
|
|
* ntfs_rename - inode_operations::rename
|
2021-08-13 17:21:29 +03:00
|
|
|
*/
|
2023-01-13 12:49:17 +01:00
|
|
|
static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
|
2021-08-31 18:52:39 +03:00
|
|
|
struct dentry *dentry, struct inode *new_dir,
|
2021-08-13 17:21:29 +03:00
|
|
|
struct dentry *new_dentry, u32 flags)
|
|
|
|
{
|
|
|
|
int err;
|
2021-08-31 18:52:39 +03:00
|
|
|
struct super_block *sb = dir->i_sb;
|
2021-08-13 17:21:29 +03:00
|
|
|
struct ntfs_sb_info *sbi = sb->s_fs_info;
|
2021-08-31 18:52:39 +03:00
|
|
|
struct ntfs_inode *dir_ni = ntfs_i(dir);
|
2021-08-13 17:21:29 +03:00
|
|
|
struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
|
2021-08-31 18:52:39 +03:00
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
struct ntfs_inode *ni = ntfs_i(inode);
|
|
|
|
struct inode *new_inode = d_inode(new_dentry);
|
|
|
|
struct NTFS_DE *de, *new_de;
|
2025-06-18 15:31:57 +08:00
|
|
|
bool is_same;
|
2021-08-31 18:52:39 +03:00
|
|
|
/*
|
|
|
|
* de - memory of PATH_MAX bytes:
|
|
|
|
* [0-1024) - original name (dentry->d_name)
|
|
|
|
* [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
|
|
|
|
* [2048-3072) - new name (new_dentry->d_name)
|
|
|
|
*/
|
2021-08-13 17:21:29 +03:00
|
|
|
static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
|
|
|
|
static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
|
|
|
|
1024);
|
|
|
|
static_assert(PATH_MAX >= 4 * 1024);
|
|
|
|
|
2025-06-24 16:35:39 +03:00
|
|
|
/* Avoid any operation if inode is bad. */
|
|
|
|
if (unlikely(is_bad_ni(ni)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2023-11-24 12:19:37 +03:00
|
|
|
if (unlikely(ntfs3_forced_shutdown(sb)))
|
|
|
|
return -EIO;
|
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
if (flags & ~RENAME_NOREPLACE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
is_same = dentry->d_name.len == new_dentry->d_name.len &&
|
|
|
|
!memcmp(dentry->d_name.name, new_dentry->d_name.name,
|
|
|
|
dentry->d_name.len);
|
2021-08-13 17:21:29 +03:00
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
if (is_same && dir == new_dir) {
|
2021-08-03 14:57:09 +03:00
|
|
|
/* Nothing to do. */
|
2021-08-31 18:52:39 +03:00
|
|
|
return 0;
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
if (ntfs_is_meta_file(sbi, inode->i_ino)) {
|
|
|
|
/* Should we print an error? */
|
|
|
|
return -EINVAL;
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_inode) {
|
2021-08-03 14:57:09 +03:00
|
|
|
/* Target name exists. Unlink it. */
|
2021-08-13 17:21:29 +03:00
|
|
|
dget(new_dentry);
|
|
|
|
ni_lock_dir(new_dir_ni);
|
|
|
|
err = ntfs_unlink_inode(new_dir, new_dentry);
|
|
|
|
ni_unlock(new_dir_ni);
|
|
|
|
dput(new_dentry);
|
|
|
|
if (err)
|
2021-08-31 18:52:39 +03:00
|
|
|
return err;
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:57:09 +03:00
|
|
|
/* Allocate PATH_MAX bytes. */
|
2021-08-31 18:52:39 +03:00
|
|
|
de = __getname();
|
|
|
|
if (!de)
|
|
|
|
return -ENOMEM;
|
2021-08-13 17:21:29 +03:00
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
/* Translate dentry->d_name into unicode form. */
|
|
|
|
err = fill_name_de(sbi, de, &dentry->d_name, NULL);
|
2021-08-13 17:21:29 +03:00
|
|
|
if (err < 0)
|
2021-08-31 18:52:39 +03:00
|
|
|
goto out;
|
2021-08-13 17:21:29 +03:00
|
|
|
|
|
|
|
if (is_same) {
|
2021-08-31 18:52:39 +03:00
|
|
|
/* Reuse 'de'. */
|
|
|
|
new_de = de;
|
2021-08-13 17:21:29 +03:00
|
|
|
} else {
|
2021-08-31 18:52:39 +03:00
|
|
|
/* Translate new_dentry->d_name into unicode form. */
|
|
|
|
new_de = Add2Ptr(de, 2048);
|
2021-08-13 17:21:29 +03:00
|
|
|
err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
|
|
|
|
if (err < 0)
|
2021-08-31 18:52:39 +03:00
|
|
|
goto out;
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
ni_lock_dir(dir_ni);
|
|
|
|
ni_lock(ni);
|
2022-10-07 20:16:15 +03:00
|
|
|
if (dir_ni != new_dir_ni)
|
|
|
|
ni_lock_dir2(new_dir_ni);
|
2021-08-31 18:52:39 +03:00
|
|
|
|
2025-06-18 15:31:57 +08:00
|
|
|
err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de);
|
|
|
|
if (!err) {
|
2023-07-05 15:00:38 -04:00
|
|
|
simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
|
2021-08-31 18:52:39 +03:00
|
|
|
mark_inode_dirty(inode);
|
|
|
|
mark_inode_dirty(dir);
|
2023-07-05 15:00:38 -04:00
|
|
|
if (dir != new_dir)
|
2021-08-31 18:52:39 +03:00
|
|
|
mark_inode_dirty(new_dir);
|
2021-08-13 17:21:29 +03:00
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
if (IS_DIRSYNC(dir))
|
|
|
|
ntfs_sync_inode(dir);
|
2021-08-13 17:21:29 +03:00
|
|
|
|
2021-08-31 18:52:39 +03:00
|
|
|
if (IS_DIRSYNC(new_dir))
|
|
|
|
ntfs_sync_inode(inode);
|
2021-08-13 17:21:29 +03:00
|
|
|
}
|
|
|
|
|
2022-10-07 20:16:15 +03:00
|
|
|
if (dir_ni != new_dir_ni)
|
|
|
|
ni_unlock(new_dir_ni);
|
2021-08-31 18:52:39 +03:00
|
|
|
ni_unlock(ni);
|
|
|
|
ni_unlock(dir_ni);
|
2021-08-13 17:21:29 +03:00
|
|
|
out:
|
2021-08-31 18:52:39 +03:00
|
|
|
__putname(de);
|
2021-08-13 17:21:29 +03:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dentry *ntfs3_get_parent(struct dentry *child)
|
|
|
|
{
|
|
|
|
struct inode *inode = d_inode(child);
|
|
|
|
struct ntfs_inode *ni = ntfs_i(inode);
|
|
|
|
|
|
|
|
struct ATTR_LIST_ENTRY *le = NULL;
|
|
|
|
struct ATTRIB *attr = NULL;
|
|
|
|
struct ATTR_FILE_NAME *fname;
|
|
|
|
|
|
|
|
while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
|
|
|
|
NULL))) {
|
|
|
|
fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
|
|
|
|
if (!fname)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return d_obtain_alias(
|
|
|
|
ntfs_iget5(inode->i_sb, &fname->home, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
}
|
|
|
|
|
2022-09-23 12:42:18 +03:00
|
|
|
/*
|
|
|
|
* dentry_operations::d_hash
|
|
|
|
*/
|
|
|
|
static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
|
|
|
|
{
|
|
|
|
struct ntfs_sb_info *sbi;
|
|
|
|
const char *n = name->name;
|
|
|
|
unsigned int len = name->len;
|
|
|
|
unsigned long hash;
|
|
|
|
struct cpu_str *uni;
|
|
|
|
unsigned int c;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* First try fast implementation. */
|
|
|
|
hash = init_name_hash(dentry);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!len--) {
|
|
|
|
name->hash = end_name_hash(hash);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = *n++;
|
|
|
|
if (c >= 0x80)
|
|
|
|
break;
|
|
|
|
|
|
|
|
hash = partial_name_hash(toupper(c), hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try slow way with current upcase table
|
|
|
|
*/
|
2024-09-02 14:19:32 -03:00
|
|
|
uni = kmem_cache_alloc(names_cachep, GFP_NOWAIT);
|
2022-09-23 12:42:18 +03:00
|
|
|
if (!uni)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
sbi = dentry->d_sb->s_fs_info;
|
|
|
|
|
|
|
|
err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
|
|
|
|
UTF16_HOST_ENDIAN);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!err) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
|
|
|
|
init_name_hash(dentry));
|
|
|
|
name->hash = end_name_hash(hash);
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out:
|
2024-09-02 14:19:32 -03:00
|
|
|
kmem_cache_free(names_cachep, uni);
|
2022-09-23 12:42:18 +03:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dentry_operations::d_compare
|
|
|
|
*/
|
|
|
|
static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
|
|
|
|
const char *str, const struct qstr *name)
|
|
|
|
{
|
|
|
|
struct ntfs_sb_info *sbi;
|
|
|
|
int ret;
|
|
|
|
const char *n1 = str;
|
|
|
|
const char *n2 = name->name;
|
|
|
|
unsigned int len2 = name->len;
|
|
|
|
unsigned int lm = min(len1, len2);
|
|
|
|
unsigned char c1, c2;
|
2022-10-06 19:58:11 +03:00
|
|
|
struct cpu_str *uni1;
|
|
|
|
struct le_str *uni2;
|
2022-09-23 12:42:18 +03:00
|
|
|
|
|
|
|
/* First try fast implementation. */
|
|
|
|
for (;;) {
|
2022-10-04 07:41:45 -07:00
|
|
|
if (!lm--)
|
2022-10-04 16:23:59 -07:00
|
|
|
return len1 != len2;
|
2022-09-23 12:42:18 +03:00
|
|
|
|
|
|
|
if ((c1 = *n1++) == (c2 = *n2++))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c1 >= 0x80 || c2 >= 0x80)
|
|
|
|
break;
|
|
|
|
|
2022-10-04 07:41:45 -07:00
|
|
|
if (toupper(c1) != toupper(c2))
|
|
|
|
return 1;
|
2022-09-23 12:42:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try slow way with current upcase table
|
|
|
|
*/
|
|
|
|
sbi = dentry->d_sb->s_fs_info;
|
|
|
|
uni1 = __getname();
|
|
|
|
if (!uni1)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
|
|
|
|
UTF16_HOST_ENDIAN);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
uni2 = Add2Ptr(uni1, 2048);
|
|
|
|
|
2022-10-06 19:58:11 +03:00
|
|
|
ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
|
|
|
|
(struct cpu_str *)uni2, NTFS_NAME_LEN,
|
|
|
|
UTF16_LITTLE_ENDIAN);
|
2022-09-23 12:42:18 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:58:11 +03:00
|
|
|
ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
|
2022-09-23 12:42:18 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
__putname(uni1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
// clang-format off
|
|
|
|
const struct inode_operations ntfs_dir_inode_operations = {
|
|
|
|
.lookup = ntfs_lookup,
|
|
|
|
.create = ntfs_create,
|
|
|
|
.link = ntfs_link,
|
|
|
|
.unlink = ntfs_unlink,
|
|
|
|
.symlink = ntfs_symlink,
|
|
|
|
.mkdir = ntfs_mkdir,
|
|
|
|
.rmdir = ntfs_rmdir,
|
|
|
|
.mknod = ntfs_mknod,
|
|
|
|
.rename = ntfs_rename,
|
2023-02-14 15:06:27 +04:00
|
|
|
.get_acl = ntfs_get_acl,
|
2021-08-13 17:21:29 +03:00
|
|
|
.set_acl = ntfs_set_acl,
|
2024-07-18 17:41:08 +03:00
|
|
|
.setattr = ntfs_setattr,
|
2021-08-13 17:21:29 +03:00
|
|
|
.getattr = ntfs_getattr,
|
|
|
|
.listxattr = ntfs_listxattr,
|
|
|
|
.fiemap = ntfs_fiemap,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct inode_operations ntfs_special_inode_operations = {
|
2024-07-18 17:41:08 +03:00
|
|
|
.setattr = ntfs_setattr,
|
2021-08-13 17:21:29 +03:00
|
|
|
.getattr = ntfs_getattr,
|
|
|
|
.listxattr = ntfs_listxattr,
|
2023-02-14 15:06:27 +04:00
|
|
|
.get_acl = ntfs_get_acl,
|
2021-08-13 17:21:29 +03:00
|
|
|
.set_acl = ntfs_set_acl,
|
|
|
|
};
|
2022-09-23 12:42:18 +03:00
|
|
|
|
|
|
|
const struct dentry_operations ntfs_dentry_ops = {
|
|
|
|
.d_hash = ntfs_d_hash,
|
|
|
|
.d_compare = ntfs_d_compare,
|
|
|
|
};
|
|
|
|
|
2021-08-13 17:21:29 +03:00
|
|
|
// clang-format on
|