ntfs filesystem remake
Subsystems (4)
Namjae Jeon
maintainerRe: [PATCH v6 11/16] ntfs: update runlist handling and cluster allocator
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
Suggested commit message:
Updates runlist handling and cluster allocation to support contiguous allocations and filesystem trimming.
Improve the runlist API to handle allocation failures and introduces discard support.
Okay, I will use it.
Quoted 11 lines
fs/ntfs/lcnalloc.c switch_to_data1_zone: search_zone = 2; · line 739
Quoted 7 lines
fs/ntfs/lcnalloc.c +4
-0+if (is_dealloc == true) +ntfs_release_dirty_clusters (vol, rl->length);up_write (&vol->lcnbmp_lock); +memalloc_nofs_restore (memalloc_flags);ntfs_debug ("Done." ); +return rl ==NULL ?ERR_PTR (-EIO ) : rl;In general you want the
memalloc_nofs_restoreto be after goto labels in a single place, as otherwise debugging is really hard. In doubt a separate wrapper doing it my be even better.
Okay, I will change it as you pointed out.
Thanks for your review!
Quoted 2 lines
Namjae Jeon
maintainerPATCH v6 12/16 ntfs: add reparse and ea operations
In reply to Namjae JeonThis patch implements support for Extended Attributes and Reparse Points, enabling file operations, Access Control Lists, and compatibility with Windows Subsystem for Linux (WSL) metadata.
- Implement xattr handlers for get/set/list operations.
- Support POSIX ACLs.
- Implement handling for WSL attributes to map Linux permissions and ownership to NTFS EAs.
- Implement handling for NTFS reparse points, including creating and reading symbolic links.
- Support special files via WSL reparse tags.
--- fs/ntfs/ea.c | 947 ++++++++++++++++++++++++++++++++++++++++++++++ fs/ntfs/reparse.c | 548 +++++++++++++++++++++++++++ 2 files changed, 1495 insertions(+) create mode 100644 fs/ntfs/ea.c create mode 100644 fs/ntfs/reparse.c
fs/ntfs/ea.c new file +947 -0
@@ -0,0 +1,947 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Pocessing of EA's +* +* Part of this file is based on code from the NTFS-3G. +* +* Copyright (c) 2014-2021 Jean-Pierre Andre +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/fs.h> +#include <linux/posix_acl.h> +#include <linux/posix_acl_xattr.h> +#include <linux/xattr.h> + +#include "layout.h" +#include "attrib.h" +#include "index.h" +#include "dir.h" +#include "ea.h" + +static int ntfs_write_ea (struct ntfs_inode *ni,__le32 type,char *value,s64 ea_off, +s64 ea_size,bool need_truncate) +{ +struct inode *ea_vi; +int err =0 ; +s64 written; + + ea_vi =ntfs_attr_iget (VFS_I (ni), type,AT_UNNAMED ,0 ); +if (IS_ERR (ea_vi)) +return PTR_ERR (ea_vi); + + written =ntfs_inode_attr_pwrite (ea_vi, ea_off, ea_size, value, false); +if (written != ea_size) + err = -EIO ; +else { +struct ntfs_inode *ea_ni =NTFS_I (ea_vi); + +if (need_truncate && ea_ni->data_size > ea_off + ea_size) +ntfs_attr_truncate (ea_ni, ea_off + ea_size); +mark_mft_record_dirty (ni); + } + +iput (ea_vi); +return err; +} + +static int ntfs_ea_lookup (char *ea_buf,s64 ea_buf_size,const char *name, +int name_len,s64 *ea_offset,s64 *ea_size) +{ +const struct ea_attr *p_ea; +s64 offset; +unsigned int next; + +if (ea_buf_size <sizeof (struct ea_attr )) + goto out; + + offset =0 ; +do { + p_ea = (const struct ea_attr *)&ea_buf[offset]; + next =le32_to_cpu (p_ea->next_entry_offset); + +if (offset + next > ea_buf_size || + ((1 + p_ea->ea_name_length) > (ea_buf_size - offset))) +break ; + +if (p_ea->ea_name_length == name_len && + !memcmp (p_ea->ea_name, name, name_len)) { + *ea_offset = offset; +if (next) + *ea_size = next; +else { +unsigned int ea_len =1 + p_ea->ea_name_length + +le16_to_cpu (p_ea->ea_value_length); + +if ((ea_buf_size - offset) < ea_len) + goto out; + + *ea_size =ALIGN (struct_size (p_ea, ea_name, +1 + p_ea->ea_name_length + +le16_to_cpu (p_ea->ea_value_length)),4 ); + } + +if (ea_buf_size < *ea_offset + *ea_size) + goto out; + +return 0 ; + } + offset += next; + }while (next >0 && offset < ea_buf_size && +sizeof (struct ea_attr ) < (ea_buf_size - offset)); + +out: +return -ENOENT ; +} + +/* +* Return the existing EA +* +* The EA_INFORMATION is not examined and the consistency of the +* existing EA is not checked. +* +* If successful, the full attribute is returned unchanged +* and its size is returned. +* If the designated buffer is too small, the needed size is +* returned, and the buffer is left unchanged. +* If there is an error, a negative value is returned and errno +* is set according to the error. +*/ +static int ntfs_get_ea (struct inode *inode,const char *name,size_t name_len, +void *buffer,size_t size) +{ +struct ntfs_inode *ni =NTFS_I (inode); +const struct ea_attr *p_ea; +char *ea_buf; +s64 ea_off, ea_size, all_ea_size, ea_info_size; +int err; +u32 ea_info_qlen; +u16 ea_value_len; +struct ea_information *p_ea_info; + +if (!NInoHasEA (ni)) +return -ENODATA ; + + p_ea_info =ntfs_attr_readall (ni,AT_EA_INFORMATION ,NULL ,0 , + &ea_info_size); +if (!p_ea_info || ea_info_size !=sizeof (struct ea_information )) { +kvfree (p_ea_info); +return -ENODATA ; + } + + ea_info_qlen =le32_to_cpu (p_ea_info->ea_query_length); +kvfree (p_ea_info); + + ea_buf =ntfs_attr_readall (ni,AT_EA ,NULL ,0 , &all_ea_size); +if (!ea_buf) +return -ENODATA ; + + err =ntfs_ea_lookup (ea_buf, ea_info_qlen, name, name_len, &ea_off, + &ea_size); +if (!err) { + p_ea = (struct ea_attr *)&ea_buf[ea_off]; + ea_value_len =le16_to_cpu (p_ea->ea_value_length); +if (!buffer) { +kvfree (ea_buf); +return ea_value_len; + } + +if (ea_value_len > size) { + err = -ERANGE ; + goto free_ea_buf; + } + +memcpy (buffer, &p_ea->ea_name[p_ea->ea_name_length +1 ], + ea_value_len); +kvfree (ea_buf); +return ea_value_len; + } + + err = -ENODATA ; +free_ea_buf: +kvfree (ea_buf); +return err; +} + +static inline int ea_packed_size (const struct ea_attr *p_ea) +{ +/* +* 4 bytes for header (flags and lengths) + name length + 1 + +* value length. +*/ +return 5 + p_ea->ea_name_length +le16_to_cpu (p_ea->ea_value_length); +} + +/* +* Set a new EA, and set EA_INFORMATION accordingly +* +* This is roughly the same as ZwSetEaFile() on Windows, however +* the "offset to next" of the last EA should not be cleared. +* +* Consistency of the new EA is first checked. +* +* EA_INFORMATION is set first, and it is restored to its former +* state if setting EA fails. +*/ +static int ntfs_set_ea (struct inode *inode,const char *name,size_t name_len, +const void *value,size_t val_size,int flags, +__le16 *packed_ea_size) +{ +struct ntfs_inode *ni =NTFS_I (inode); +struct ea_information *p_ea_info =NULL ; +int ea_packed, err =0 ; +struct ea_attr *p_ea; +u32 ea_info_qsize =0 ; +char *ea_buf =NULL ; +size_t new_ea_size =ALIGN (struct_size (p_ea, ea_name,1 + name_len + val_size),4 ); +s64 ea_off, ea_info_size, all_ea_size, ea_size; + +if (name_len >255 ) +return -ENAMETOOLONG ; + +if (ntfs_attr_exist (ni,AT_EA_INFORMATION ,AT_UNNAMED ,0 )) { + p_ea_info =ntfs_attr_readall (ni,AT_EA_INFORMATION ,NULL ,0 , + &ea_info_size); +if (!p_ea_info || ea_info_size !=sizeof (struct ea_information )) + goto out; + + ea_buf =ntfs_attr_readall (ni,AT_EA ,NULL ,0 , &all_ea_size); +if (!ea_buf) { + ea_info_qsize =0 ; +kvfree (p_ea_info); + goto create_ea_info; + } + + ea_info_qsize =le32_to_cpu (p_ea_info->ea_query_length); + }else { +create_ea_info: + p_ea_info =kzalloc (sizeof (struct ea_information ),GFP_NOFS ); +if (!p_ea_info) +return -ENOMEM ; + + ea_info_qsize =0 ; + err =ntfs_attr_add (ni,AT_EA_INFORMATION ,AT_UNNAMED ,0 , + (char *)p_ea_info,sizeof (struct ea_information )); +if (err) + goto out; + +if (ntfs_attr_exist (ni,AT_EA ,AT_UNNAMED ,0 )) { + err =ntfs_attr_remove (ni,AT_EA ,AT_UNNAMED ,0 ); +if (err) + goto out; + } + + goto alloc_new_ea; + } + +if (ea_info_qsize > all_ea_size) { + err = -EIO ; + goto out; + } + + err =ntfs_ea_lookup (ea_buf, ea_info_qsize, name, name_len, &ea_off, + &ea_size); +if (ea_info_qsize && !err) { +if (flags &XATTR_CREATE ) { + err = -EEXIST ; + goto out; + } + + p_ea = (struct ea_attr *)(ea_buf + ea_off); + +if (val_size && +le16_to_cpu (p_ea->ea_value_length) == val_size && + !memcmp (p_ea->ea_name + p_ea->ea_name_length +1 , value, + val_size)) + goto out; + +le16_add_cpu (&p_ea_info->ea_length,0 -ea_packed_size (p_ea)); + +if (p_ea->flags &NEED_EA ) +le16_add_cpu (&p_ea_info->need_ea_count,-1 ); + +memmove ((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size)); + ea_info_qsize -= ea_size; + p_ea_info->ea_query_length =cpu_to_le32 (ea_info_qsize); + + err =ntfs_write_ea (ni,AT_EA_INFORMATION , (char *)p_ea_info,0 , +sizeof (struct ea_information ), false); +if (err) + goto out; + + err =ntfs_write_ea (ni,AT_EA , ea_buf,0 , ea_info_qsize, true); +if (err) + goto out; + +if ((flags &XATTR_REPLACE ) && !val_size) { +/* Remove xattr. */ + goto out; + } + }else { +if (flags &XATTR_REPLACE ) { + err = -ENODATA ; + goto out; + } + } +kvfree (ea_buf); + +alloc_new_ea: + ea_buf =kzalloc (new_ea_size,GFP_NOFS ); +if (!ea_buf) { + err = -ENOMEM ; + goto out; + } + +/* +* EA and REPARSE_POINT compatibility not checked any more, +* required by Windows 10, but having both may lead to +* problems with earlier versions. +*/ + p_ea = (struct ea_attr *)ea_buf; +memcpy (p_ea->ea_name, name, name_len); + p_ea->ea_name_length = name_len; + p_ea->ea_name[name_len] =0 ; +memcpy (p_ea->ea_name + name_len +1 , value, val_size); + p_ea->ea_value_length =cpu_to_le16 (val_size); + p_ea->next_entry_offset =cpu_to_le32 (new_ea_size); + + ea_packed =le16_to_cpu (p_ea_info->ea_length) +ea_packed_size (p_ea); + p_ea_info->ea_length =cpu_to_le16 (ea_packed); + p_ea_info->ea_query_length =cpu_to_le32 (ea_info_qsize + new_ea_size); + +if (ea_packed >0xffff || +ntfs_attr_size_bounds_check (ni->vol,AT_EA , new_ea_size)) { + err = -EFBIG ; + goto out; + } + +/* +* no EA or EA_INFORMATION : add them +*/ +if (!ntfs_attr_exist (ni,AT_EA ,AT_UNNAMED ,0 )) { + err =ntfs_attr_add (ni,AT_EA ,AT_UNNAMED ,0 , (char *)p_ea, + new_ea_size); +if (err) + goto out; + }else { + err =ntfs_write_ea (ni,AT_EA , (char *)p_ea, ea_info_qsize, + new_ea_size, false); +if (err) + goto out; + } + + err =ntfs_write_ea (ni,AT_EA_INFORMATION , (char *)p_ea_info,0 , +sizeof (struct ea_information ), false); +if (err) + goto out; + +if (packed_ea_size) + *packed_ea_size = p_ea_info->ea_length; +mark_mft_record_dirty (ni); +out: +if (ea_info_qsize >0 ) +NInoSetHasEA (ni); +else +NInoClearHasEA (ni); + +kvfree (ea_buf); +kvfree (p_ea_info); + +return err; +} + +/* +* Check for the presence of an EA "$LXDEV" (used by WSL) +* and return its value as a device address +*/ +int ntfs_ea_get_wsl_inode (struct inode *inode,dev_t *rdevp,unsigned int flags) +{ +int err; +__le32 v; + +if (!(flags &NTFS_VOL_UID )) { +/* Load uid to lxuid EA */ + err =ntfs_get_ea (inode,"$LXUID" ,sizeof ("$LXUID" ) -1 , &v, +sizeof (v)); +if (err <0 ) +return err; +i_uid_write (inode,le32_to_cpu (v)); + } + +if (!(flags &NTFS_VOL_UID )) { +/* Load gid to lxgid EA */ + err =ntfs_get_ea (inode,"$LXGID" ,sizeof ("$LXGID" ) -1 , &v, +sizeof (v)); +if (err <0 ) +return err; +i_gid_write (inode,le32_to_cpu (v)); + } + +/* Load mode to lxmod EA */ + err =ntfs_get_ea (inode,"$LXMOD" ,sizeof ("$LXMOD" ) -1 , &v,sizeof (v)); +if (err >0 ) { + inode->i_mode =le32_to_cpu (v); + }else { +/* Everyone gets all permissions. */ + inode->i_mode |=0777 ; + } + +/* Load mode to lxdev EA */ + err =ntfs_get_ea (inode,"$LXDEV" ,sizeof ("$LXDEV" ) -1 , &v,sizeof (v)); +if (err >0 ) + *rdevp =le32_to_cpu (v); + err =0 ; + +return err; +} + +int ntfs_ea_set_wsl_inode (struct inode *inode,dev_t rdev,__le16 *ea_size, +unsigned int flags) +{ +__le32 v; +int err; + +if (flags &NTFS_EA_UID ) { +/* Store uid to lxuid EA */ + v =cpu_to_le32 (i_uid_read (inode)); + err =ntfs_set_ea (inode,"$LXUID" ,sizeof ("$LXUID" ) -1 , &v, +sizeof (v),0 , ea_size); +if (err) +return err; + } + +if (flags &NTFS_EA_GID ) { +/* Store gid to lxgid EA */ + v =cpu_to_le32 (i_gid_read (inode)); + err =ntfs_set_ea (inode,"$LXGID" ,sizeof ("$LXGID" ) -1 , &v, +sizeof (v),0 , ea_size); +if (err) +return err; + } + +if (flags &NTFS_EA_MODE ) { +/* Store mode to lxmod EA */ + v =cpu_to_le32 (inode->i_mode); + err =ntfs_set_ea (inode,"$LXMOD" ,sizeof ("$LXMOD" ) -1 , &v, +sizeof (v),0 , ea_size); +if (err) +return err; + } + +if (rdev) { + v =cpu_to_le32 (rdev); + err =ntfs_set_ea (inode,"$LXDEV" ,sizeof ("$LXDEV" ) -1 , &v,sizeof (v), +0 , ea_size); + } + +return err; +} + +ssize_t ntfs_listxattr (struct dentry *dentry,char *buffer,size_t size) +{ +struct inode *inode =d_inode (dentry); +struct ntfs_inode *ni =NTFS_I (inode); +const struct ea_attr *p_ea; +s64 offset, ea_buf_size, ea_info_size; +int next, err =0 , ea_size; +u32 ea_info_qsize; +char *ea_buf =NULL ; +ssize_t ret =0 ; +struct ea_information *ea_info; + +if (!NInoHasEA (ni)) +return 0 ; + +mutex_lock (&NTFS_I (inode)->mrec_lock); + ea_info =ntfs_attr_readall (ni,AT_EA_INFORMATION ,NULL ,0 , + &ea_info_size); +if (!ea_info || ea_info_size !=sizeof (struct ea_information )) + goto out; + + ea_info_qsize =le32_to_cpu (ea_info->ea_query_length); + + ea_buf =ntfs_attr_readall (ni,AT_EA ,NULL ,0 , &ea_buf_size); +if (!ea_buf) + goto out; + +if (ea_info_qsize > ea_buf_size) + goto out; + +if (ea_buf_size <sizeof (struct ea_attr )) + goto out; + + offset =0 ; +do { + p_ea = (const struct ea_attr *)&ea_buf[offset]; + next =le32_to_cpu (p_ea->next_entry_offset); +if (next) + ea_size = next; +else + ea_size =ALIGN (struct_size (p_ea, ea_name, +1 + p_ea->ea_name_length + +le16_to_cpu (p_ea->ea_value_length)), +4 ); +if (buffer) { +if (offset + ea_size > ea_info_qsize) +break ; + +if (ret + p_ea->ea_name_length +1 > size) { + err = -ERANGE ; + goto out; + } + +if (p_ea->ea_name_length +1 > (ea_info_qsize - offset)) +break ; + +memcpy (buffer + ret, p_ea->ea_name, p_ea->ea_name_length); + buffer[ret + p_ea->ea_name_length] =0 ; + } + + ret += p_ea->ea_name_length +1 ; + offset += ea_size; + }while (next >0 && offset < ea_info_qsize && +sizeof (struct ea_attr ) < (ea_info_qsize - offset)); + +out: +mutex_unlock (&NTFS_I (inode)->mrec_lock); +kvfree (ea_info); +kvfree (ea_buf); + +return err ? err : ret; +} + +// clang-format off +#define SYSTEM_DOS_ATTRIB "system.dos_attrib" +#define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" +#define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" +// clang-format on + +static int ntfs_getxattr (const struct xattr_handler *handler, +struct dentry *unused,struct inode *inode,const char *name, +void *buffer,size_t size) +{ +struct ntfs_inode *ni =NTFS_I (inode); +int err; + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (!strcmp (name,SYSTEM_DOS_ATTRIB )) { +if (!buffer) { + err =sizeof (u8); + }else if (size <sizeof (u8)) { + err = -ENODATA ; + }else { + err =sizeof (u8); + *(u8 *)buffer = (u8)(le32_to_cpu (ni->flags) &0x3F ); + } + goto out; + } + +if (!strcmp (name,SYSTEM_NTFS_ATTRIB ) || + !strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) { +if (!buffer) { + err =sizeof (u32); + }else if (size <sizeof (u32)) { + err = -ENODATA ; + }else { + err =sizeof (u32); + *(u32 *)buffer =le32_to_cpu (ni->flags); +if (!strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) + *(__be32 *)buffer =cpu_to_be32 (*(u32 *)buffer); + } + goto out; + } + +mutex_lock (&ni->mrec_lock); + err =ntfs_get_ea (inode, name,strlen (name), buffer, size); +mutex_unlock (&ni->mrec_lock); + +out: +return err; +} + +static int ntfs_new_attr_flags (struct ntfs_inode *ni,__le32 fattr) +{ +struct ntfs_attr_search_ctx *ctx; +struct mft_record *m; +struct attr_record *a; +__le16 new_aflags; +int mp_size, mp_ofs, name_ofs, arec_size, err; + + m =map_mft_record (ni); +if (IS_ERR (m)) +return PTR_ERR (m); + + ctx =ntfs_attr_get_search_ctx (ni, m); +if (!ctx) { + err = -ENOMEM ; + goto err_out; + } + + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (err) { + err = -EINVAL ; + goto err_out; + } + + a = ctx->attr; + new_aflags = ctx->attr->flags; + +if (fattr &FILE_ATTR_SPARSE_FILE ) + new_aflags |=ATTR_IS_SPARSE ; +else + new_aflags &= ~ATTR_IS_SPARSE ; + +if (fattr &FILE_ATTR_COMPRESSED ) + new_aflags |=ATTR_IS_COMPRESSED ; +else + new_aflags &= ~ATTR_IS_COMPRESSED ; + +if (new_aflags == a->flags) +return 0 ; + +if ((new_aflags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) == + (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) { +pr_err ("file can't be sparsed and compressed\n" ); + err = -EOPNOTSUPP ; + goto err_out; + } + +if (!a->non_resident) + goto out; + +if (a->data.non_resident.data_size) { +pr_err ("Can't change sparsed/compressed for non-empty file" ); + err = -EOPNOTSUPP ; + goto err_out; + } + +if (new_aflags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) + name_ofs = (offsetof(struct attr_record , + data.non_resident.compressed_size) + + sizeof(a->data.non_resident.compressed_size) + 7) & ~7; +else + name_ofs = (offsetof(struct attr_record , + data.non_resident.compressed_size) + 7) & ~7; + + mp_size =ntfs_get_size_for_mapping_pairs (ni->vol, ni->runlist.rl,0 ,-1 ,-1 ); +if (unlikely (mp_size <0 )) { + err = mp_size; +ntfs_debug ("Failed to get size for mapping pairs array, error code %i.\n" , err); + goto err_out; + } + + mp_ofs = (name_ofs + a->name_length *sizeof (__le16) +7 ) & ~7 ; + arec_size = (mp_ofs + mp_size +7 ) & ~7 ; + + err =ntfs_attr_record_resize (m, a, arec_size); +if (unlikely (err)) + goto err_out; + +if (new_aflags & (ATTR_IS_SPARSE |ATTR_IS_COMPRESSED )) { + a->data.non_resident.compression_unit =0 ; +if (new_aflags &ATTR_IS_COMPRESSED || ni->vol->major_ver <3 ) + a->data.non_resident.compression_unit =4 ; + a->data.non_resident.compressed_size =0 ; + ni->itype.compressed.size =0 ; +if (a->data.non_resident.compression_unit) { + ni->itype.compressed.block_size =1U << + (a->data.non_resident.compression_unit + + ni->vol->cluster_size_bits); + ni->itype.compressed.block_size_bits = +ffs (ni->itype.compressed.block_size) - +1 ; + ni->itype.compressed.block_clusters =1U << + a->data.non_resident.compression_unit; + }else { + ni->itype.compressed.block_size =0 ; + ni->itype.compressed.block_size_bits =0 ; + ni->itype.compressed.block_clusters =0 ; + } + +if (new_aflags &ATTR_IS_SPARSE ) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + +if (new_aflags &ATTR_IS_COMPRESSED ) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; + } + }else { + ni->flags &= ~(FILE_ATTR_SPARSE_FILE |FILE_ATTR_COMPRESSED ); + a->data.non_resident.compression_unit =0 ; +NInoClearSparse (ni); +NInoClearCompressed (ni); + } + + a->name_offset =cpu_to_le16 (name_ofs); + a->data.non_resident.mapping_pairs_offset =cpu_to_le16 (mp_ofs); + +out: + a->flags = new_aflags; +mark_mft_record_dirty (ctx->ntfs_ino); +err_out: +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (ni); +return err; +} + +static int ntfs_setxattr (const struct xattr_handler *handler, +struct mnt_idmap *idmap,struct dentry *unused, +struct inode *inode,const char *name,const void *value, +size_t size,int flags) +{ +struct ntfs_inode *ni =NTFS_I (inode); +int err; +__le32 fattr; + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (!strcmp (name,SYSTEM_DOS_ATTRIB )) { +if (sizeof (u8) != size) { + err = -EINVAL ; + goto out; + } + fattr =cpu_to_le32 (*(u8 *)value); + goto set_fattr; + } + +if (!strcmp (name,SYSTEM_NTFS_ATTRIB ) || + !strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) { +if (size !=sizeof (u32)) { + err = -EINVAL ; + goto out; + } +if (!strcmp (name,SYSTEM_NTFS_ATTRIB_BE )) + fattr =cpu_to_le32 (be32_to_cpu (*(__be32 *)value)); +else + fattr =cpu_to_le32 (*(u32 *)value); + +if (S_ISREG (inode->i_mode)) { +mutex_lock (&ni->mrec_lock); + err =ntfs_new_attr_flags (ni, fattr); +mutex_unlock (&ni->mrec_lock); +if (err) + goto out; + } + +set_fattr: +if (S_ISDIR (inode->i_mode)) + fattr |=FILE_ATTR_DIRECTORY ; +else + fattr &= ~FILE_ATTR_DIRECTORY ; + +if (ni->flags != fattr) { + ni->flags = fattr; +if (fattr &FILE_ATTR_READONLY ) + inode->i_mode &= ~0222 ; +else + inode->i_mode |=0222 ; +NInoSetFileNameDirty (ni); +mark_inode_dirty (inode); + } + err =0 ; + goto out; + } + +mutex_lock (&ni->mrec_lock); + err =ntfs_set_ea (inode, name,strlen (name), value, size, flags,NULL ); +mutex_unlock (&ni->mrec_lock); + +out: +inode_set_ctime_current (inode); +mark_inode_dirty (inode); +return err; +} + +static bool ntfs_xattr_user_list (struct dentry *dentry) +{ +return true; +} + +// clang-format off +static const struct xattr_handler ntfs_other_xattr_handler = { + .prefix ="" , + .get = ntfs_getxattr, + .set = ntfs_setxattr, + .list = ntfs_xattr_user_list, +}; + +const struct xattr_handler *const ntfs_xattr_handlers[] = { + &ntfs_other_xattr_handler, +NULL , +}; +// clang-format on + +#ifdef CONFIG_NTFS_FS_POSIX_ACL +struct posix_acl *ntfs_get_acl (struct mnt_idmap *idmap,struct dentry *dentry, +int type) +{ +struct inode *inode =d_inode (dentry); +struct ntfs_inode *ni =NTFS_I (inode); +const char *name; +size_t name_len; +struct posix_acl *acl; +int err; +void *buf; + +/* Allocate PATH_MAX bytes. */ + buf =__getname (); +if (!buf) +return ERR_PTR (-ENOMEM ); + +/* Possible values of 'type' was already checked above. */ +if (type ==ACL_TYPE_ACCESS ) { + name =XATTR_NAME_POSIX_ACL_ACCESS ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_ACCESS ) -1 ; + }else { + name =XATTR_NAME_POSIX_ACL_DEFAULT ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_DEFAULT ) -1 ; + } + +mutex_lock (&ni->mrec_lock); + err =ntfs_get_ea (inode, name, name_len, buf,PATH_MAX ); +mutex_unlock (&ni->mrec_lock); + +/* Translate extended attribute to acl. */ +if (err >=0 ) + acl =posix_acl_from_xattr (&init_user_ns, buf, err); +else if (err == -ENODATA ) + acl =NULL ; +else + acl =ERR_PTR (err); + +if (!IS_ERR (acl)) +set_cached_acl (inode, type, acl); + +__putname (buf); + +return acl; +} + +static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap, +struct inode *inode,struct posix_acl *acl, +int type,bool init_acl) +{ +const char *name; +size_t size, name_len; +void *value; +int err; +int flags; +umode_t mode; + +if (S_ISLNK (inode->i_mode)) +return -EOPNOTSUPP ; + + mode = inode->i_mode; +switch (type) { +case ACL_TYPE_ACCESS : +/* Do not change i_mode if we are in init_acl */ +if (acl && !init_acl) { + err =posix_acl_update_mode (idmap, inode, &mode, &acl); +if (err) +return err; + } + name =XATTR_NAME_POSIX_ACL_ACCESS ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_ACCESS ) -1 ; +break ; + +case ACL_TYPE_DEFAULT : +if (!S_ISDIR (inode->i_mode)) +return acl ? -EACCES :0 ; + name =XATTR_NAME_POSIX_ACL_DEFAULT ; + name_len =sizeof (XATTR_NAME_POSIX_ACL_DEFAULT ) -1 ; +break ; + +default : +return -EINVAL ; + } + +if (!acl) { +/* Remove xattr if it can be presented via mode. */ + size =0 ; + value =NULL ; + flags =XATTR_REPLACE ; + }else { + size =posix_acl_xattr_size (acl->a_count); + value =kmalloc (size,GFP_NOFS ); +if (!value) +return -ENOMEM ; + err =posix_acl_to_xattr (&init_user_ns, acl, value, size); +if (err <0 ) + goto out; + flags =0 ; + } + +mutex_lock (&NTFS_I (inode)->mrec_lock); + err =ntfs_set_ea (inode, name, name_len, value, size, flags,NULL ); +mutex_unlock (&NTFS_I (inode)->mrec_lock); +if (err == -ENODATA && !size) + err =0 ;/* Removing non existed xattr. */ +if (!err) { +__le16 ea_size =0 ; +umode_t old_mode = inode->i_mode; + + inode->i_mode = mode; +mutex_lock (&NTFS_I (inode)->mrec_lock); + err =ntfs_ea_set_wsl_inode (inode,0 , &ea_size,NTFS_EA_MODE ); +if (err) { +ntfs_set_ea (inode, name, name_len,NULL ,0 , +XATTR_REPLACE ,NULL ); +mutex_unlock (&NTFS_I (inode)->mrec_lock); + inode->i_mode = old_mode; + goto out; + } +mutex_unlock (&NTFS_I (inode)->mrec_lock); + +set_cached_acl (inode, type, acl); +inode_set_ctime_current (inode); +mark_inode_dirty (inode); + } + +out: +kfree (value); + +return err; +} + +int ntfs_set_acl (struct mnt_idmap *idmap,struct dentry *dentry, +struct posix_acl *acl,int type) +{ +return ntfs_set_acl_ex (idmap,d_inode (dentry), acl, type, false); +} + +int ntfs_init_acl (struct mnt_idmap *idmap,struct inode *inode, +struct inode *dir) +{ +struct posix_acl *default_acl, *acl; +int err; + + err =posix_acl_create (dir, &inode->i_mode, &default_acl, &acl); +if (err) +return err; + +if (default_acl) { + err =ntfs_set_acl_ex (idmap, inode, default_acl, +ACL_TYPE_DEFAULT , true); +posix_acl_release (default_acl); + }else { + inode->i_default_acl =NULL ; + } + +if (acl) { +if (!err) + err =ntfs_set_acl_ex (idmap, inode, acl, +ACL_TYPE_ACCESS , true); +posix_acl_release (acl); + }else { + inode->i_acl =NULL ; + } + +return err; +} +#endif
fs/ntfs/reparse.c new file +548 -0
548 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v6 12/16] ntfs: add reparse and ea operations
Suggested commit message:
Implement support for Extended Attributes and Reparse Points, enabling Posix ACL support and, and compatibility with Windows Subsystem for Linux (WSL) metadata.
fs/ntfs/reparse.c line 27
Quoted 6 lines
fs/ntfs/reparse.c new file +6
-0+struct WSL_LINK_REPARSE_DATA { +__le32 type; +char link[]; +}; + +struct REPARSE_INDEX {/* index entry in $Extend/$Reparse */
Why are these using all upper case names unlike the rest of the code?
fs/ntfs/reparse.c line 56
Quoted 6 lines
fs/ntfs/reparse.c new file +6
-0+ ok = ni && reparse_attr && (size >=sizeof (struct reparse_point )) && + (reparse_attr->reparse_tag !=IO_REPARSE_TAG_RESERVED_ZERO ) && + (((size_t )le16_to_cpu (reparse_attr->reparse_data_length) + +sizeof (struct reparse_point ) + + ((reparse_attr->reparse_tag &IO_REPARSE_TAG_IS_MICROSOFT ) ? +0 :sizeof (struct guid ))) == size);
A bunch of superflous braces. But in general decomposing such complex operations into an inline helper using multiple if statements and adding comments improves the readability a lot.
fs/ntfs/reparse.c line 57
Quoted 1 line
fs/ntfs/reparse.c new file +1
-0+if (ok) {
... and just return here for !ok and reduce the indentation for the rest of the function?
Quoted 7 lines
fs/ntfs/reparse.c new file +7
-0+switch (reparse_attr->reparse_tag) { +case IO_REPARSE_TAG_LX_SYMLINK : + wsl_reparse_data = (const struct WSL_LINK_REPARSE_DATA *) + reparse_attr->reparse_data; +if ((le16_to_cpu (reparse_attr->reparse_data_length) <= +sizeof (wsl_reparse_data->type)) || + (wsl_reparse_data->type !=cpu_to_le32 (2 )))
fs/ntfs/reparse.c line 74
Quoted 10 lines
fs/ntfs/reparse.c new file +10
-0+ ok = false; +break ; +case IO_REPARSE_TAG_AF_UNIX : +case IO_REPARSE_TAG_LX_FIFO : +case IO_REPARSE_TAG_LX_CHR : +case IO_REPARSE_TAG_LX_BLK : +if (reparse_attr->reparse_data_length || + !(ni->flags &FILE_ATTRIBUTE_RECALL_ON_OPEN )) + ok = false; +break ;
... and then directly return from inside the switch as well?
Namjae Jeon
maintainerRe: [PATCH v6 12/16] ntfs: add reparse and ea operations
Quoted 6 lines · Christoph Hellwig
Suggested commit message:
Implement support for Extended Attributes and Reparse Points, enabling Posix ACL support and, and compatibility with Windows Subsystem for Linux (WSL) metadata.
Okay, I will use it.
Quoted 10 lines
fs/ntfs/reparse.c line 27
Quoted 7 lines
fs/ntfs/reparse.c new file +6
-0+struct WSL_LINK_REPARSE_DATA { +__le32 type; +char link[]; +}; + +struct REPARSE_INDEX {/* index entry in $Extend/$Reparse */ Why are these using all upper case names unlike the rest of the code?
Right, I will change it.
Quoted 11 lines
fs/ntfs/reparse.c line 56
Quoted 7 lines
fs/ntfs/reparse.c new file +6
-0+ ok = ni && reparse_attr && (size >=sizeof (struct reparse_point )) && + (reparse_attr->reparse_tag !=IO_REPARSE_TAG_RESERVED_ZERO ) && + (((size_t )le16_to_cpu (reparse_attr->reparse_data_length) + +sizeof (struct reparse_point ) + + ((reparse_attr->reparse_tag &IO_REPARSE_TAG_IS_MICROSOFT ) ? +0 :sizeof (struct guid ))) == size);A bunch of superflous braces. But in general decomposing such complex operations into an inline helper using multiple if statements and adding comments improves the readability a lot.
Okay, I will update and add the comments.
Quoted 5 lines
Quoted 2 lines
+ if (ok) {
... and just return here for !ok and reduce the indentation for the rest of the function?
Right, I will change it like this.
Quoted 19 lines
Quoted 8 lines
fs/ntfs/reparse.c new file +7
-0+switch (reparse_attr->reparse_tag) { +case IO_REPARSE_TAG_LX_SYMLINK : + wsl_reparse_data = (const struct WSL_LINK_REPARSE_DATA *) + reparse_attr->reparse_data; +if ((le16_to_cpu (reparse_attr->reparse_data_length) <= +sizeof (wsl_reparse_data->type)) || + (wsl_reparse_data->type !=cpu_to_le32 (2 )))fs/ntfs/reparse.c line 74
Quoted 10 lines
fs/ntfs/reparse.c new file +10
-0+ ok = false; +break ; +case IO_REPARSE_TAG_AF_UNIX : +case IO_REPARSE_TAG_LX_FIFO : +case IO_REPARSE_TAG_LX_CHR : +case IO_REPARSE_TAG_LX_BLK : +if (reparse_attr->reparse_data_length || + !(ni->flags &FILE_ATTRIBUTE_RECALL_ON_OPEN )) + ok = false; +break ;
Quoted 1 line
... and then directly return from inside the switch as well?
Right, I will change it.
Thanks for the review!
Quoted 2 lines
Namjae Jeon
maintainerPATCH v6 14/16 ntfs3: remove legacy ntfs driver support
In reply to Namjae JeonThis patch reverts the following commits that introduced legacy ntfs driver alias and related support code:
74871791ffa9 ntfs3: serve as alias for the legacy ntfs driver 1ff2e956608c fs/ntfs3: Redesign legacy ntfs support 9b872cc50daa ntfs3: add legacy ntfs file operations d55f90e9b243 ntfs3: enforce read-only when used as legacy ntfs driver
The legacy ntfs driver has been remade as a new implementation, so the alias and related codes in ntfs3 are no longer needed.
--- fs/ntfs3/Kconfig | 9 ------- fs/ntfs3/dir.c | 9 ------- fs/ntfs3/file.c | 10 -------- fs/ntfs3/inode.c | 16 ++++--------- fs/ntfs3/ntfs_fs.h | 11 --------- fs/ntfs3/super.c | 59 +--------------------------------------------- 6 files changed, 5 insertions(+), 109 deletions(-)
fs/ntfs3/Kconfig +0 -9
@@ -46,12 +46,3 @@ config NTFS3_FS_POSIX_ACL NOTE: this is linux only feature. Windows will ignore these ACLs. If you don't know what Access Control Lists are, say N. - -config NTFS_FS - tristate "NTFS file system support" - select NTFS3_FS - select BUFFER_HEAD - select NLS - help - This config option is here only for backward compatibility. NTFS - filesystem is now handled by the NTFS3 driver.
fs/ntfs3/dir.c +0 -9
@@ -631,13 +631,4 @@ const struct file_operations ntfs_dir_operations = { .compat_ioctl = ntfs_compat_ioctl,#endif }; - -#if IS_ENABLED (CONFIG_NTFS_FS ) -const struct file_operations ntfs_legacy_dir_operations = { - .llseek = generic_file_llseek, - .read = generic_read_dir, - .iterate_shared = ntfs_readdir, - .open = ntfs_file_open, -}; -#endif // clang-format on
fs/ntfs3/file.c +0 -10
@@ -1478,14 +1478,4 @@ const struct file_operations ntfs_file_operations = { .fallocate = ntfs_fallocate, .release = ntfs_file_release, }; - -#if IS_ENABLED (CONFIG_NTFS_FS ) -const struct file_operations ntfs_legacy_file_operations = { - .llseek = generic_file_llseek, - .read_iter = ntfs_file_read_iter, - .splice_read = ntfs_file_splice_read, - .open = ntfs_file_open, - .release = ntfs_file_release, -}; -#endif // clang-format on
fs/ntfs3/inode.c +4 -12
@@ -444,9 +444,7 @@ static struct inode *ntfs_read_mft(struct inode *inode,* Usually a hard links to directories are disabled. */ inode->i_op = &ntfs_dir_inode_operations; - inode->i_fop =unlikely (is_legacy_ntfs (sb)) ? - &ntfs_legacy_dir_operations : - &ntfs_dir_operations; + inode->i_fop = &ntfs_dir_operations; ni->i_valid =0 ; }else if (S_ISLNK (mode)) { ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY ;
@@ -456,9 +454,7 @@ static struct inode *ntfs_read_mft(struct inode *inode, }else if (S_ISREG (mode)) { ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY ; inode->i_op = &ntfs_file_inode_operations; - inode->i_fop =unlikely (is_legacy_ntfs (sb)) ? - &ntfs_legacy_file_operations : - &ntfs_file_operations; + inode->i_fop = &ntfs_file_operations; inode->i_mapping->a_ops =is_compressed (ni) ? &ntfs_aops_cmpr : &ntfs_aops;if (ino !=MFT_REC_MFT )
@@ -1590,9 +1586,7 @@ int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,if (S_ISDIR (mode)) { inode->i_op = &ntfs_dir_inode_operations; - inode->i_fop =unlikely (is_legacy_ntfs (sb)) ? - &ntfs_legacy_dir_operations : - &ntfs_dir_operations; + inode->i_fop = &ntfs_dir_operations; }else if (S_ISLNK (mode)) { inode->i_op = &ntfs_link_inode_operations; inode->i_fop =NULL ;
@@ -1601,9 +1595,7 @@ int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,inode_nohighmem (inode); }else if (S_ISREG (mode)) { inode->i_op = &ntfs_file_inode_operations; - inode->i_fop =unlikely (is_legacy_ntfs (sb)) ? - &ntfs_legacy_file_operations : - &ntfs_file_operations; + inode->i_fop = &ntfs_file_operations; inode->i_mapping->a_ops =is_compressed (ni) ? &ntfs_aops_cmpr : &ntfs_aops;init_rwsem (&ni->file.run_lock);
fs/ntfs3/ntfs_fs.h +0 -11
@@ -501,7 +501,6 @@ struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni, struct ntfs_fnd *fnd);bool dir_is_empty (struct inode *dir);extern const struct file_operations ntfs_dir_operations; -extern const struct file_operations ntfs_legacy_dir_operations;/* Globals from file.c */ int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
@@ -516,7 +515,6 @@ long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg);extern const struct inode_operations ntfs_special_inode_operations;extern const struct inode_operations ntfs_file_inode_operations;extern const struct file_operations ntfs_file_operations; -extern const struct file_operations ntfs_legacy_file_operations;/* Globals from frecord.c */ void ni_remove_mi (struct ntfs_inode *ni,struct mft_inode *mi);
@@ -1160,13 +1158,4 @@ static inline void le64_sub_cpu(__le64 *var, u64 val) *var = cpu_to_le64(le64_to_cpu(*var) - val); } -#if IS_ENABLED (CONFIG_NTFS_FS ) -bool is_legacy_ntfs (struct super_block *sb); -#else -static inline bool is_legacy_ntfs (struct super_block *sb) -{ -return false; -} -#endif -#endif /* _LINUX_NTFS3_NTFS_FS_H */
fs/ntfs3/super.c +1 -58
107 lines not on this page · raw message
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH v6 15/16 ntfs: add Kconfig and Makefile
In reply to Namjae JeonThis introduce Kconfig and Makefile for remade ntfs.
And this patch make ntfs and ntfs3 mutually exclusive so only one can be built-in(y), while both can still be built as modules(m).
--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfs/Kconfig | 47 +++++++++++++++++++++++++++++++++++++++++++++++ fs/ntfs/Makefile | 10 ++++++++++ fs/ntfs3/Kconfig | 1 + 5 files changed, 60 insertions(+) create mode 100644 fs/ntfs/Kconfig create mode 100644 fs/ntfs/Makefile
fs/Kconfig +1 -0
@@ -152,6 +152,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" +source "fs/ntfs/Kconfig" source "fs/ntfs3/Kconfig" endmenu
fs/Makefile +1 -0
@@ -90,6 +90,7 @@ obj-$(CONFIG_NLS) += nls/ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ +obj-$(CONFIG_NTFS_FS) += ntfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ obj-$(CONFIG_UFS_FS) += ufs/ obj-$(CONFIG_EFS_FS) += efs/
fs/ntfs/Kconfig new file +47 -0
47 lines not on this page · raw message
fs/ntfs/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_NTFS_FS) += ntfs.o + +ntfs-y := aops.o attrib.o collate.o dir.o file.o index.o inode.o \ + mft.o mst.o namei.o runlist.o super.o unistr.o attrlist.o ea.o \ + upcase.o bitmap.o lcnalloc.o logfile.o reparse.o compress.o \ + iomap.o debug.o sysctl.o quota.o object_id.o bdev-io.o + +ccflags-$(CONFIG_NTFS_DEBUG) += -DDEBUG
fs/ntfs3/Kconfig +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only config NTFS3_FS tristate "NTFS Read-Write file system support" + depends on !NTFS_FS || m select BUFFER_HEAD select NLS select LEGACY_DIRECT_IO
Signature · 1 line
2.25.1
Re: [PATCH v6 15/16] ntfs: add Kconfig and Makefile
Looks good:
Namjae Jeon
maintainerPATCH v6 16/16 MAINTAINERS: update ntfs filesystem entry
In reply to Namjae JeonAdd myself and Hyunchul Lee as ntfs maintainer.
Since Anton is already listed in CREDITS, only his outdated information is updated here. the web address in the W: field in his entry is no longer accessible. Update his CREDITS with the web and email address found in the ntfs filesystem entry.
--- CREDITS | 4 ++-- MAINTAINERS | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-)
CREDITS +2 -2
@@ -80,8 +80,8 @@ S: B-2610 Wilrijk-Antwerpen S: Belgium N: Anton Altaparmakov -E: [email protected] -W: http://www-stu.christs.cam.ac.uk/~aia21/ +E: [email protected] +W: http://www.tuxera.com/ D: Author of new NTFS driver, various other kernel hacks. S: Christ's College S: Cambridge CB2 3BU
MAINTAINERS +5 -6
@@ -18647,12 +18647,11 @@ T: git https://github.com/davejiang/linux.git F: drivers/ntb/hw/intel/ NTFS FILESYSTEM -M: Anton Altaparmakov <[email protected]> -R: Namjae Jeon <[email protected]> -L: [email protected] -S: Supported -W: http://www.tuxera.com/ -T: git git://git.kernel.org/pub/scm/linux/kernel/git/aia21/ntfs.git +M: Namjae Jeon <[email protected]> +M: Hyunchul Lee <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git F: Documentation/filesystems/ntfs.rst F: fs/ntfs/
Signature · 1 line
2.25.1