ntfsplus: add iomap and address space operations
PATCH 6/11 ntfsplus: add iomap and address space operations
This adds the implementation of iomap and address space operations for ntfsplus.
--- fs/ntfsplus/aops.c | 631 +++++++++++++++++++++++++++++++++++ fs/ntfsplus/ntfs_iomap.c | 704 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 1335 insertions(+) create mode 100644 fs/ntfsplus/aops.c create mode 100644 fs/ntfsplus/ntfs_iomap.c
fs/ntfsplus/aops.c new file +631 -0
@@ -0,0 +1,631 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* NTFS kernel address space operations and page cache handling. +* +* Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2002 Richard Russon +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include <linux/writeback.h> +#include <linux/mpage.h> +#include <linux/uio.h> + +#include "aops.h" +#include "attrib.h" +#include "mft.h" +#include "ntfs.h" +#include "misc.h" +#include "ntfs_iomap.h" + +static s64 ntfs_convert_page_index_into_lcn (struct ntfs_volume *vol,struct ntfs_inode *ni, +unsigned long page_index) +{ +sector_t iblock; +s64 vcn; +s64 lcn; +unsigned char blocksize_bits = vol->sb->s_blocksize_bits; + + iblock = (s64 )page_index << (PAGE_SHIFT - blocksize_bits); + vcn = (s64 )iblock << blocksize_bits >> vol->cluster_size_bits; + +down_read (&ni->runlist.lock); + lcn =ntfs_attr_vcn_to_lcn_nolock (ni, vcn, false); +up_read (&ni->runlist.lock); + +return lcn; +} + +struct bio *ntfs_setup_bio (struct ntfs_volume *vol,unsigned int opf,s64 lcn, +unsigned int pg_ofs) +{ +struct bio *bio; + + bio =bio_alloc (vol->sb->s_bdev,1 , opf,GFP_NOIO ); +if (!bio) +return NULL ; + bio->bi_iter.bi_sector = ((lcn << vol->cluster_size_bits) + pg_ofs) >> + vol->sb->s_blocksize_bits; + +return bio; +} + +/** +* ntfs_read_folio - fill a @folio of a @file with data from the device +* @file: open file to which the folio @folio belongs or NULL +* @folio: page cache folio to fill with data +* +* For non-resident attributes, ntfs_read_folio() fills the @folio of the open +* file @file by calling the ntfs version of the generic block_read_full_folio() +* function, which in turn creates and reads in the buffers associated with +* the folio asynchronously. +* +* For resident attributes, OTOH, ntfs_read_folio() fills @folio by copying the +* data from the mft record (which at this stage is most likely in memory) and +* fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as +* even if the mft record is not cached at this point in time, we need to wait +* for it to be read in before we can do the copy. +* +* Return 0 on success and -errno on error. +*/ +static int ntfs_read_folio (struct file *file,struct folio *folio) +{ +loff_t i_size; +struct inode *vi; +struct ntfs_inode *ni; + + vi = folio->mapping->host; + i_size =i_size_read (vi); +/* Is the page fully outside i_size? (truncate in progress) */ +if (unlikely (folio->index >= (i_size +PAGE_SIZE -1 ) >> +PAGE_SHIFT )) { +folio_zero_segment (folio,0 ,PAGE_SIZE ); +ntfs_debug ("Read outside i_size - truncated?" ); +folio_mark_uptodate (folio); +folio_unlock (folio); +return 0 ; + } +/* +* This can potentially happen because we clear PageUptodate() during +* ntfs_writepage() of MstProtected() attributes. +*/ +if (folio_test_uptodate (folio)) { +folio_unlock (folio); +return 0 ; + } + ni =NTFS_I (vi); + +/* +* Only $DATA attributes can be encrypted and only unnamed $DATA +* attributes can be compressed. Index root can have the flags set but +* this means to create compressed/encrypted files, not that the +* attribute is compressed/encrypted. Note we need to check for +* AT_INDEX_ALLOCATION since this is the type of both directory and +* index inodes. +*/ +if (ni->type !=AT_INDEX_ALLOCATION ) { +/* If attribute is encrypted, deny access, just like NT4. */ +if (NInoEncrypted (ni)) { +BUG_ON (ni->type !=AT_DATA ); +folio_unlock (folio); +return -EACCES ; + } +/* Compressed data streams are handled in compress.c. */ +if (NInoNonResident (ni) &&NInoCompressed (ni)) { +BUG_ON (ni->type !=AT_DATA ); +BUG_ON (ni->name_len); +return ntfs_read_compressed_block (folio); + } + } + +return iomap_read_folio (folio, &ntfs_read_iomap_ops); +} + +static int ntfs_write_mft_block (struct ntfs_inode *ni,struct folio *folio, +struct writeback_control *wbc) +{ +struct inode *vi =VFS_I (ni); +struct ntfs_volume *vol = ni->vol; +u8 *kaddr; +struct ntfs_inode *locked_nis[PAGE_SIZE /NTFS_BLOCK_SIZE ]; +int nr_locked_nis =0 , err =0 , mft_ofs, prev_mft_ofs; +struct bio *bio =NULL ; +unsigned long mft_no; +struct ntfs_inode *tni; +s64 lcn; +unsigned int lcn_folio_off =0 ; +s64 vcn = (s64 )folio->index <<PAGE_SHIFT >> vol->cluster_size_bits; +s64 end_vcn = ni->allocated_size >> vol->cluster_size_bits; +unsigned int folio_sz; +struct runlist_element *rl; + +ntfs_debug ("Entering for inode 0x%lx, attribute type 0x%x, folio index 0x%lx." , + vi->i_ino, ni->type, folio->index); +BUG_ON (!NInoNonResident (ni)); +BUG_ON (!NInoMstProtected (ni)); + +/* +* NOTE: ntfs_write_mft_block() would be called for $MFTMirr if a page +* in its page cache were to be marked dirty. However this should +* never happen with the current driver and considering we do not +* handle this case here we do want to BUG(), at least for now. +*/ + +BUG_ON (!((S_ISREG (vi->i_mode) && !vi->i_ino) ||S_ISDIR (vi->i_mode) || + (NInoAttr (ni) && ni->type ==AT_INDEX_ALLOCATION ))); + + lcn =ntfs_convert_page_index_into_lcn (vol, ni, folio->index); +if (lcn <=LCN_HOLE ) { +folio_start_writeback (folio); +folio_unlock (folio); +folio_end_writeback (folio); +return -EIO ; + } + +if (vol->cluster_size_bits >PAGE_SHIFT ) { + lcn_folio_off = folio->index <<PAGE_SHIFT ; + lcn_folio_off &= vol->cluster_size_mask; + } + +/* Map folio so we can access its contents. */ + kaddr =kmap_local_folio (folio,0 ); +/* Clear the page uptodate flag whilst the mst fixups are applied. */ +folio_clear_uptodate (folio); + +for (mft_ofs =0 ; mft_ofs <PAGE_SIZE && vcn < end_vcn; + mft_ofs += vol->mft_record_size) { +/* Get the mft record number. */ + mft_no = (((s64 )folio->index <<PAGE_SHIFT ) + mft_ofs) >> + vol->mft_record_size_bits; +/* Check whether to write this mft record. */ + tni =NULL ; +if (ntfs_may_write_mft_record (vol, mft_no, + (struct mft_record *)(kaddr + mft_ofs), &tni)) { +unsigned int mft_record_off =0 ; +s64 vcn_off = vcn; + +/* +* The record should be written. If a locked ntfs +* inode was returned, add it to the array of locked +* ntfs inodes. +*/ +if (tni) + locked_nis[nr_locked_nis++] = tni; + +if (bio && (mft_ofs != prev_mft_ofs + vol->mft_record_size)) { +flush_bio: +flush_dcache_folio (folio); +submit_bio_wait (bio); +bio_put (bio); + bio =NULL ; + } + +if (vol->cluster_size ==NTFS_BLOCK_SIZE ) { +down_write (&ni->runlist.lock); + rl =ntfs_attr_vcn_to_rl (ni, vcn_off, &lcn); +up_write (&ni->runlist.lock); +if (IS_ERR (rl) || lcn <0 ) { + err = -EIO ; + goto unm_done; + } + } + +if (!bio) { +unsigned int off = lcn_folio_off; + +if (vol->cluster_size !=NTFS_BLOCK_SIZE ) + off += mft_ofs; + + bio =ntfs_setup_bio (vol,REQ_OP_WRITE , lcn, off); +if (!bio) { + err = -ENOMEM ; + goto unm_done; + } + } + +if (vol->cluster_size ==NTFS_BLOCK_SIZE && rl->length ==1 ) + folio_sz =NTFS_BLOCK_SIZE ; +else + folio_sz = vol->mft_record_size; +if (!bio_add_folio (bio, folio, folio_sz, + mft_ofs + mft_record_off)) { + err = -EIO ; +bio_put (bio); + goto unm_done; + } + prev_mft_ofs = mft_ofs; + mft_record_off += folio_sz; + +if (mft_record_off != vol->mft_record_size) { + vcn_off++; + goto flush_bio; + } + +if (mft_no < vol->mftmirr_size) +ntfs_sync_mft_mirror (vol, mft_no, + (struct mft_record *)(kaddr + mft_ofs)); + } + + vcn += vol->mft_record_size >> vol->cluster_size_bits; + } + +if (bio) { +flush_dcache_folio (folio); +submit_bio_wait (bio); +bio_put (bio); + } +flush_dcache_folio (folio); +unm_done: +folio_mark_uptodate (folio); +kunmap_local (kaddr); + +folio_start_writeback (folio); +folio_unlock (folio); +folio_end_writeback (folio); + +/* Unlock any locked inodes. */ +while (nr_locked_nis-- >0 ) { +struct ntfs_inode *base_tni; + + tni = locked_nis[nr_locked_nis]; +mutex_unlock (&tni->mrec_lock); + +/* Get the base inode. */ +mutex_lock (&tni->extent_lock); +if (tni->nr_extents >=0 ) + base_tni = tni; +else { + base_tni = tni->ext.base_ntfs_ino; +BUG_ON (!base_tni); + } +mutex_unlock (&tni->extent_lock); +ntfs_debug ("Unlocking %s inode 0x%lx." , + tni == base_tni ?"base" :"extent" , + tni->mft_no); +atomic_dec (&tni->count); +iput (VFS_I (base_tni)); + } + +if (unlikely (err && err != -ENOMEM )) +NVolSetErrors (vol); +if (likely (!err)) +ntfs_debug ("Done." ); +return err; +} + +/** +* ntfs_bmap - map logical file block to physical device block +* @mapping: address space mapping to which the block to be mapped belongs +* @block: logical block to map to its physical device block +* +* For regular, non-resident files (i.e. not compressed and not encrypted), map +* the logical @block belonging to the file described by the address space +* mapping @mapping to its physical device block. +* +* The size of the block is equal to the @s_blocksize field of the super block +* of the mounted file system which is guaranteed to be smaller than or equal +* to the cluster size thus the block is guaranteed to fit entirely inside the +* cluster which means we do not need to care how many contiguous bytes are +* available after the beginning of the block. +* +* Return the physical device block if the mapping succeeded or 0 if the block +* is sparse or there was an error. +* +* Note: This is a problem if someone tries to run bmap() on $Boot system file +* as that really is in block zero but there is nothing we can do. bmap() is +* just broken in that respect (just like it cannot distinguish sparse from +* not available or error). +*/ +static sector_t ntfs_bmap (struct address_space *mapping,sector_t block) +{ +s64 ofs, size; +loff_t i_size; +s64 lcn; +unsigned long blocksize, flags; +struct ntfs_inode *ni =NTFS_I (mapping->host); +struct ntfs_volume *vol = ni->vol; +unsigned int delta; +unsigned char blocksize_bits, cluster_size_shift; + +ntfs_debug ("Entering for mft_no 0x%lx, logical block 0x%llx." , + ni->mft_no, (unsigned long long )block); +if (ni->type !=AT_DATA || !NInoNonResident (ni) ||NInoEncrypted (ni)) { +ntfs_error (vol->sb,"BMAP does not make sense for %s attributes, returning 0." , + (ni->type !=AT_DATA ) ?"non-data" : + (!NInoNonResident (ni) ?"resident" : +"encrypted" )); +return 0 ; + } +/* None of these can happen. */ +BUG_ON (NInoCompressed (ni)); +BUG_ON (NInoMstProtected (ni)); + blocksize = vol->sb->s_blocksize; + blocksize_bits = vol->sb->s_blocksize_bits; + ofs = (s64 )block << blocksize_bits; +read_lock_irqsave (&ni->size_lock, flags); + size = ni->initialized_size; + i_size =i_size_read (VFS_I (ni)); +read_unlock_irqrestore (&ni->size_lock, flags); +/* +* If the offset is outside the initialized size or the block straddles +* the initialized size then pretend it is a hole unless the +* initialized size equals the file size. +*/ +if (unlikely (ofs >= size || (ofs + blocksize > size && size < i_size))) + goto hole; + cluster_size_shift = vol->cluster_size_bits; +down_read (&ni->runlist.lock); + lcn =ntfs_attr_vcn_to_lcn_nolock (ni, ofs >> cluster_size_shift, false); +up_read (&ni->runlist.lock); +if (unlikely (lcn <LCN_HOLE )) { +/* +* Step down to an integer to avoid gcc doing a long long +* comparision in the switch when we know @lcn is between +* LCN_HOLE and LCN_EIO (i.e. -1 to -5). +* +* Otherwise older gcc (at least on some architectures) will +* try to use __cmpdi2() which is of course not available in +* the kernel. +*/ +switch ((int )lcn) { +case LCN_ENOENT : +/* +* If the offset is out of bounds then pretend it is a +* hole. +*/ + goto hole; +case LCN_ENOMEM : +ntfs_error (vol->sb, +"Not enough memory to complete mapping for inode 0x%lx. Returning 0." , + ni->mft_no); +break ; +default : +ntfs_error (vol->sb, +"Failed to complete mapping for inode 0x%lx. Run chkdsk. Returning 0." , + ni->mft_no); +break ; + } +return 0 ; + } +if (lcn <0 ) { +/* It is a hole. */ +hole: +ntfs_debug ("Done (returning hole)." ); +return 0 ; + } +/* +* The block is really allocated and fullfils all our criteria. +* Convert the cluster to units of block size and return the result. +*/ + delta = ofs & vol->cluster_size_mask; +if (unlikely (sizeof (block) <sizeof (lcn))) { + block = lcn = ((lcn << cluster_size_shift) + delta) >> + blocksize_bits; +/* If the block number was truncated return 0. */ +if (unlikely (block != lcn)) { +ntfs_error (vol->sb, +"Physical block 0x%llx is too large to be returned, returning 0." , + (long long )lcn); +return 0 ; + } + }else + block = ((lcn << cluster_size_shift) + delta) >> + blocksize_bits; +ntfs_debug ("Done (returning block 0x%llx)." , (unsigned long long )lcn); +return block; +} + +static void ntfs_readahead (struct readahead_control *rac) +{ +struct address_space *mapping = rac->mapping; +struct inode *inode = mapping->host; +struct ntfs_inode *ni =NTFS_I (inode); + +if (!NInoNonResident (ni) ||NInoCompressed (ni)) { +/* No readahead for resident and compressed. */ +return ; + } + +if (NInoMstProtected (ni) && + (ni->mft_no ==FILE_MFT || ni->mft_no == FILE_MFTMirr)) +return ; + +iomap_readahead (rac, &ntfs_read_iomap_ops); +} + +static int ntfs_mft_writepage (struct folio *folio,struct writeback_control *wbc) +{ +struct address_space *mapping = folio->mapping; +struct inode *vi = mapping->host; +struct ntfs_inode *ni =NTFS_I (vi); +loff_t i_size; +int ret; + + i_size =i_size_read (vi); + +/* We have to zero every time due to mmap-at-end-of-file. */ +if (folio->index >= (i_size >>PAGE_SHIFT )) { +/* The page straddles i_size. */ +unsigned int ofs = i_size & ~PAGE_MASK ; + +folio_zero_segment (folio, ofs,PAGE_SIZE ); + } + + ret =ntfs_write_mft_block (ni, folio, wbc); +mapping_set_error (mapping, ret); +return ret; +} + +static int ntfs_writepages (struct address_space *mapping, +struct writeback_control *wbc) +{ +struct inode *inode = mapping->host; +struct ntfs_inode *ni =NTFS_I (inode); +struct iomap_writepage_ctx wpc = { + .inode = mapping->host, + .wbc = wbc, + .ops = &ntfs_writeback_ops, + }; + +if (NVolShutdown (ni->vol)) +return -EIO ; + +if (!NInoNonResident (ni)) +return 0 ; + +if (NInoMstProtected (ni) && ni->mft_no ==FILE_MFT ) { +struct folio *folio =NULL ; +int error; + +while ((folio =writeback_iter (mapping, wbc, folio, &error))) + error =ntfs_mft_writepage (folio, wbc); +return error; + } + +/* If file is encrypted, deny access, just like NT4. */ +if (NInoEncrypted (ni)) { +ntfs_debug ("Denying write access to encrypted file." ); +return -EACCES ; + } + +return iomap_writepages (&wpc); +} + +static int ntfs_swap_activate (struct swap_info_struct *sis, +struct file *swap_file,sector_t *span) +{ +return iomap_swapfile_activate (sis, swap_file, span, + &ntfs_read_iomap_ops); +} + +/** +* ntfs_normal_aops - address space operations for normal inodes and attributes +* +* Note these are not used for compressed or mst protected inodes and +* attributes. +*/ +const struct address_space_operations ntfs_normal_aops = { + .read_folio = ntfs_read_folio, + .readahead = ntfs_readahead, + .writepages = ntfs_writepages, + .direct_IO = noop_direct_IO, + .dirty_folio = iomap_dirty_folio, + .bmap = ntfs_bmap, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, + .swap_activate = ntfs_swap_activate, +}; + +/** +* ntfs_compressed_aops - address space operations for compressed inodes +*/ +const struct address_space_operations ntfs_compressed_aops = { + .read_folio = ntfs_read_folio, + .direct_IO = noop_direct_IO, + .writepages = ntfs_writepages, + .dirty_folio = iomap_dirty_folio, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, +}; + +/** +* ntfs_mst_aops - general address space operations for mst protecteed inodes +* and attributes +*/ +const struct address_space_operations ntfs_mst_aops = { + .read_folio = ntfs_read_folio,/* Fill page with data. */ + .readahead = ntfs_readahead, + .writepages = ntfs_writepages,/* Write dirty page to disk. */ + .dirty_folio = iomap_dirty_folio, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, +}; + +void mark_ntfs_record_dirty (struct folio *folio) +{ +iomap_dirty_folio (folio->mapping, folio); +} + +int ntfs_dev_read (struct super_block *sb,void *buf,loff_t start,loff_t size) +{ +pgoff_t idx, idx_end; +loff_t offset, end = start + size; +u32 from, to, buf_off =0 ; +struct folio *folio; +char *kaddr; + + idx = start >>PAGE_SHIFT ; + idx_end = end >>PAGE_SHIFT ; + from = start & ~PAGE_MASK ; + +if (idx == idx_end) + idx_end++; + +for (; idx < idx_end; idx++, from =0 ) { + folio =ntfs_read_mapping_folio (sb->s_bdev->bd_mapping, idx); +if (IS_ERR (folio)) { +ntfs_error (sb,"Unable to read %ld page" , idx); +return PTR_ERR (folio); + } + + kaddr =kmap_local_folio (folio,0 ); + offset = (loff_t )idx <<PAGE_SHIFT ; + to =min_t (u32, end - offset,PAGE_SIZE ); + +memcpy (buf + buf_off, kaddr + from, to); + buf_off += to; +kunmap_local (kaddr); +folio_put (folio); + } + +return 0 ; +} + +int ntfs_dev_write (struct super_block *sb,void *buf,loff_t start, +loff_t size,bool wait) +{ +pgoff_t idx, idx_end; +loff_t offset, end = start + size; +u32 from, to, buf_off =0 ; +struct folio *folio; +char *kaddr; + + idx = start >>PAGE_SHIFT ; + idx_end = end >>PAGE_SHIFT ; + from = start & ~PAGE_MASK ; + +if (idx == idx_end) + idx_end++; + +for (; idx < idx_end; idx++, from =0 ) { + folio =ntfs_read_mapping_folio (sb->s_bdev->bd_mapping, idx); +if (IS_ERR (folio)) { +ntfs_error (sb,"Unable to read %ld page" , idx); +return PTR_ERR (folio); + } + + kaddr =kmap_local_folio (folio,0 ); + offset = (loff_t )idx <<PAGE_SHIFT ; + to =min_t (u32, end - offset,PAGE_SIZE ); + +memcpy (kaddr + from, buf + buf_off, to); + buf_off += to; +kunmap_local (kaddr); +folio_mark_uptodate (folio); +folio_mark_dirty (folio); +if (wait) +folio_wait_stable (folio); +folio_put (folio); + } + +return 0 ; +}
fs/ntfsplus/ntfs_iomap.c new file +704 -0
704 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 7/11 ntfsplus: add attrib operatrions
This adds the implementation of attrib operatrions for ntfsplus.
--- fs/ntfsplus/attrib.c | 5373 ++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/attrlist.c | 276 +++ fs/ntfsplus/compress.c | 1565 ++++++++++++ 3 files changed, 7214 insertions(+) create mode 100644 fs/ntfsplus/attrib.c create mode 100644 fs/ntfsplus/attrlist.c create mode 100644 fs/ntfsplus/compress.c
fs/ntfsplus/attrib.c new file +5373 -0
@@ -0,0 +1,5373 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* NTFS attribute operations. Part of the Linux-NTFS project. +* +* Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc. +* Copyright (c) 2002 Richard Russon +* Copyright (c) 2025 LG Electronics Co., Ltd. +* +* Part of this file is based on code from the NTFS-3G project. +* and is copyrighted by the respective authors below: +* Copyright (c) 2000-2010 Anton Altaparmakov +* Copyright (c) 2002-2005 Richard Russon +* Copyright (c) 2002-2008 Szabolcs Szakacsits +* Copyright (c) 2004-2007 Yura Pakhuchiy +* Copyright (c) 2007-2021 Jean-Pierre Andre +* Copyright (c) 2010 Erik Larsson +*/ + +#include <linux/writeback.h> +#include <linux/iomap.h> + +#include "attrib.h" +#include "attrlist.h" +#include "lcnalloc.h" +#include "misc.h" +#include "mft.h" +#include "ntfs.h" +#include "aops.h" +#include "ntfs_iomap.h" + +__le16 AT_UNNAMED [] = {cpu_to_le16 ('\0' ) }; + +/** +* ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode +* @ni: ntfs inode for which to map (part of) a runlist +* @vcn: map runlist part containing this vcn +* @ctx: active attribute search context if present or NULL if not +* +* Map the part of a runlist containing the @vcn of the ntfs inode @ni. +* +* If @ctx is specified, it is an active search context of @ni and its base mft +* record. This is needed when ntfs_map_runlist_nolock() encounters unmapped +* runlist fragments and allows their mapping. If you do not have the mft +* record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock() +* will perform the necessary mapping and unmapping. +* +* Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and +* restores it before returning. Thus, @ctx will be left pointing to the same +* attribute on return as on entry. However, the actual pointers in @ctx may +* point to different memory locations on return, so you must remember to reset +* any cached pointers from the @ctx, i.e. after the call to +* ntfs_map_runlist_nolock(), you will probably want to do: +* m = ctx->mrec; +* a = ctx->attr; +* Assuming you cache ctx->attr in a variable @a of type attr_record * and that +* you cache ctx->mrec in a variable @m of type struct mft_record *. +*/ +int ntfs_map_runlist_nolock (struct ntfs_inode *ni,s64 vcn,struct ntfs_attr_search_ctx *ctx) +{ +s64 end_vcn; +unsigned long flags; +struct ntfs_inode *base_ni; +struct mft_record *m; +struct attr_record *a; +struct runlist_element *rl; +struct folio *put_this_folio =NULL ; +int err =0 ; +bool ctx_is_temporary, ctx_needs_reset; +struct ntfs_attr_search_ctx old_ctx = {NULL , }; +size_t new_rl_count; + +ntfs_debug ("Mapping runlist part containing vcn 0x%llx." , + (unsigned long long )vcn); +if (!NInoAttr (ni)) + base_ni = ni; +else + base_ni = ni->ext.base_ntfs_ino; +if (!ctx) { + ctx_is_temporary = ctx_needs_reset = true; + m =map_mft_record (base_ni); +if (IS_ERR (m)) +return PTR_ERR (m); + ctx =ntfs_attr_get_search_ctx (base_ni, m); +if (unlikely (!ctx)) { + err = -ENOMEM ; + goto err_out; + } + }else { +s64 allocated_size_vcn; + +BUG_ON (IS_ERR (ctx->mrec)); + a = ctx->attr; +if (!a->non_resident) { + err = -EIO ; + goto err_out; + } + ctx_is_temporary = false; + end_vcn =le64_to_cpu (a->data.non_resident.highest_vcn); +read_lock_irqsave (&ni->size_lock, flags); + allocated_size_vcn = ni->allocated_size >> + ni->vol->cluster_size_bits; +read_unlock_irqrestore (&ni->size_lock, flags); +if (!a->data.non_resident.lowest_vcn && end_vcn <=0 ) + end_vcn = allocated_size_vcn -1 ; +/* +* If we already have the attribute extent containing @vcn in +* @ctx, no need to look it up again. We slightly cheat in +* that if vcn exceeds the allocated size, we will refuse to +* map the runlist below, so there is definitely no need to get +* the right attribute extent. +*/ +if (vcn >= allocated_size_vcn || (a->type == ni->type && + a->name_length == ni->name_len && + !memcmp ((u8 *)a +le16_to_cpu (a->name_offset), + ni->name, ni->name_len) && +le64_to_cpu (a->data.non_resident.lowest_vcn) + <= vcn && end_vcn >= vcn)) + ctx_needs_reset = false; +else { +/* Save the old search context. */ + old_ctx = *ctx; +/* +* If the currently mapped (extent) inode is not the +* base inode we will unmap it when we reinitialize the +* search context which means we need to get a +* reference to the page containing the mapped mft +* record so we do not accidentally drop changes to the +* mft record when it has not been marked dirty yet. +*/ +if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino != + old_ctx.base_ntfs_ino) { + put_this_folio = old_ctx.ntfs_ino->folio; +folio_get (put_this_folio); + } +/* +* Reinitialize the search context so we can lookup the +* needed attribute extent. +*/ +ntfs_attr_reinit_search_ctx (ctx); + ctx_needs_reset = true; + } + } +if (ctx_needs_reset) { + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE , vcn,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) + err = -EIO ; + goto err_out; + } +BUG_ON (!ctx->attr->non_resident); + } + a = ctx->attr; +/* +* Only decompress the mapping pairs if @vcn is inside it. Otherwise +* we get into problems when we try to map an out of bounds vcn because +* we then try to map the already mapped runlist fragment and +* ntfs_mapping_pairs_decompress() fails. +*/ + end_vcn =le64_to_cpu (a->data.non_resident.highest_vcn) +1 ; +if (unlikely (vcn && vcn >= end_vcn)) { + err = -ENOENT ; + goto err_out; + } + rl =ntfs_mapping_pairs_decompress (ni->vol, a, &ni->runlist, &new_rl_count); +if (IS_ERR (rl)) + err =PTR_ERR (rl); +else { + ni->runlist.rl = rl; + ni->runlist.count = new_rl_count; + } +err_out: +if (ctx_is_temporary) { +if (likely (ctx)) +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); + }else if (ctx_needs_reset) { +/* +* If there is no attribute list, restoring the search context +* is accomplished simply by copying the saved context back over +* the caller supplied context. If there is an attribute list, +* things are more complicated as we need to deal with mapping +* of mft records and resulting potential changes in pointers. +*/ +if (NInoAttrList (base_ni)) { +/* +* If the currently mapped (extent) inode is not the +* one we had before, we need to unmap it and map the +* old one. +*/ +if (ctx->ntfs_ino != old_ctx.ntfs_ino) { +/* +* If the currently mapped inode is not the +* base inode, unmap it. +*/ +if (ctx->base_ntfs_ino && ctx->ntfs_ino != + ctx->base_ntfs_ino) { +unmap_extent_mft_record (ctx->ntfs_ino); + ctx->mrec = ctx->base_mrec; +BUG_ON (!ctx->mrec); + } +/* +* If the old mapped inode is not the base +* inode, map it. +*/ +if (old_ctx.base_ntfs_ino && + old_ctx.ntfs_ino != old_ctx.base_ntfs_ino) { +retry_map: + ctx->mrec =map_mft_record (old_ctx.ntfs_ino); +/* +* Something bad has happened. If out +* of memory retry till it succeeds. +* Any other errors are fatal and we +* return the error code in ctx->mrec. +* Let the caller deal with it... We +* just need to fudge things so the +* caller can reinit and/or put the +* search context safely. +*/ +if (IS_ERR (ctx->mrec)) { +if (PTR_ERR (ctx->mrec) == -ENOMEM ) { +schedule (); + goto retry_map; + }else + old_ctx.ntfs_ino = + old_ctx.base_ntfs_ino; + } + } + } +/* Update the changed pointers in the saved context. */ +if (ctx->mrec != old_ctx.mrec) { +if (!IS_ERR (ctx->mrec)) + old_ctx.attr = (struct attr_record *)( + (u8 *)ctx->mrec + + ((u8 *)old_ctx.attr - + (u8 *)old_ctx.mrec)); + old_ctx.mrec = ctx->mrec; + } + } +/* Restore the search context to the saved one. */ + *ctx = old_ctx; +/* +* We drop the reference on the page we took earlier. In the +* case that IS_ERR(ctx->mrec) is true this means we might lose +* some changes to the mft record that had been made between +* the last time it was marked dirty/written out and now. This +* at this stage is not a problem as the mapping error is fatal +* enough that the mft record cannot be written out anyway and +* the caller is very likely to shutdown the whole inode +* immediately and mark the volume dirty for chkdsk to pick up +* the pieces anyway. +*/ +if (put_this_folio) +folio_put (put_this_folio); + } +return err; +} + +/** +* ntfs_map_runlist - map (a part of) a runlist of an ntfs inode +* @ni: ntfs inode for which to map (part of) a runlist +* @vcn: map runlist part containing this vcn +* +* Map the part of a runlist containing the @vcn of the ntfs inode @ni. +*/ +int ntfs_map_runlist (struct ntfs_inode *ni,s64 vcn) +{ +int err =0 ; + +down_write (&ni->runlist.lock); +/* Make sure someone else didn't do the work while we were sleeping. */ +if (likely (ntfs_rl_vcn_to_lcn (ni->runlist.rl, vcn) <= +LCN_RL_NOT_MAPPED )) + err =ntfs_map_runlist_nolock (ni, vcn,NULL ); +up_write (&ni->runlist.lock); +return err; +} + +struct runlist_element *ntfs_attr_vcn_to_rl (struct ntfs_inode *ni,s64 vcn,s64 *lcn) +{ +struct runlist_element *rl; +int err; +bool is_retry = false; + + rl = ni->runlist.rl; +if (!rl) { + err =ntfs_attr_map_whole_runlist (ni); +if (err) +return ERR_PTR (-ENOENT ); + rl = ni->runlist.rl; + } + +remap_rl: +/* Seek to element containing target vcn. */ +while (rl->length && rl[1 ].vcn <= vcn) + rl++; + *lcn =ntfs_rl_vcn_to_lcn (rl, vcn); + +if (*lcn <=LCN_RL_NOT_MAPPED && is_retry == false) { + is_retry = true; +if (!ntfs_map_runlist_nolock (ni, vcn,NULL )) { + rl = ni->runlist.rl; + goto remap_rl; + } + } + +return rl; +} + +/** +* ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode +* @ni: ntfs inode of the attribute whose runlist to search +* @vcn: vcn to convert +* @write_locked: true if the runlist is locked for writing +* +* Find the virtual cluster number @vcn in the runlist of the ntfs attribute +* described by the ntfs inode @ni and return the corresponding logical cluster +* number (lcn). +* +* If the @vcn is not mapped yet, the attempt is made to map the attribute +* extent containing the @vcn and the vcn to lcn conversion is retried. +* +* If @write_locked is true the caller has locked the runlist for writing and +* if false for reading. +* +* Since lcns must be >= 0, we use negative return codes with special meaning: +* +* Return code Meaning / Description +* ========================================== +* LCN_HOLE Hole / not allocated on disk. +* LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds. +* LCN_ENOMEM Not enough memory to map runlist. +* LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc). +* +* Locking: - The runlist must be locked on entry and is left locked on return. +* - If @write_locked is 'false', i.e. the runlist is locked for reading, +* the lock may be dropped inside the function so you cannot rely on +* the runlist still being the same when this function returns. +*/ +s64 ntfs_attr_vcn_to_lcn_nolock (struct ntfs_inode *ni,const s64 vcn, +const bool write_locked) +{ +s64 lcn; +unsigned long flags; +bool is_retry = false; + +BUG_ON (!ni); +ntfs_debug ("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked." , + ni->mft_no, (unsigned long long )vcn, + write_locked ?"write" :"read" ); +BUG_ON (!NInoNonResident (ni)); +BUG_ON (vcn <0 ); +if (!ni->runlist.rl) { +read_lock_irqsave (&ni->size_lock, flags); +if (!ni->allocated_size) { +read_unlock_irqrestore (&ni->size_lock, flags); +return LCN_ENOENT ; + } +read_unlock_irqrestore (&ni->size_lock, flags); + } +retry_remap: +/* Convert vcn to lcn. If that fails map the runlist and retry once. */ + lcn =ntfs_rl_vcn_to_lcn (ni->runlist.rl, vcn); +if (likely (lcn >=LCN_HOLE )) { +ntfs_debug ("Done, lcn 0x%llx." , (long long )lcn); +return lcn; + } +if (lcn !=LCN_RL_NOT_MAPPED ) { +if (lcn !=LCN_ENOENT ) + lcn =LCN_EIO ; + }else if (!is_retry) { +int err; + +if (!write_locked) { +up_read (&ni->runlist.lock); +down_write (&ni->runlist.lock); +if (unlikely (ntfs_rl_vcn_to_lcn (ni->runlist.rl, vcn) != +LCN_RL_NOT_MAPPED )) { +up_write (&ni->runlist.lock); +down_read (&ni->runlist.lock); + goto retry_remap; + } + } + err =ntfs_map_runlist_nolock (ni, vcn,NULL ); +if (!write_locked) { +up_write (&ni->runlist.lock); +down_read (&ni->runlist.lock); + } +if (likely (!err)) { + is_retry = true; + goto retry_remap; + } +if (err == -ENOENT ) + lcn =LCN_ENOENT ; +else if (err == -ENOMEM ) + lcn =LCN_ENOMEM ; +else + lcn =LCN_EIO ; + } +if (lcn !=LCN_ENOENT ) +ntfs_error (ni->vol->sb,"Failed with error code %lli." , + (long long )lcn); +return lcn; +} + +struct runlist_element *__ntfs_attr_find_vcn_nolock (struct runlist *runlist,const s64 vcn) +{ +size_t lower_idx, upper_idx, idx; +struct runlist_element *run; + +if (runlist->count <=1 ) +return ERR_PTR (-ENOENT ); + + run = &runlist->rl[0 ]; +if (vcn < run->vcn) +return ERR_PTR (-ENOENT ); +else if (vcn < run->vcn + run->length) +return run; + + run = &runlist->rl[runlist->count -2 ]; +if (vcn >= run->vcn && vcn < run->vcn + run->length) +return run; +if (vcn >= run->vcn + run->length) +return ERR_PTR (-ENOENT ); + + lower_idx =1 ; + upper_idx = runlist->count -2 ; + +while (lower_idx <= upper_idx) { + idx = (lower_idx + upper_idx) >>1 ; + run = &runlist->rl[idx]; + +if (vcn < run->vcn) + upper_idx = idx -1 ; +else if (vcn >= run->vcn + run->length) + lower_idx = idx +1 ; +else +return run; + } + +return ERR_PTR (-ENOENT ); +} + +/** +* ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode +* @ni: ntfs inode describing the runlist to search +* @vcn: vcn to find +* @ctx: active attribute search context if present or NULL if not +* +* Find the virtual cluster number @vcn in the runlist described by the ntfs +* inode @ni and return the address of the runlist element containing the @vcn. +* +* If the @vcn is not mapped yet, the attempt is made to map the attribute +* extent containing the @vcn and the vcn to lcn conversion is retried. +* +* If @ctx is specified, it is an active search context of @ni and its base mft +* record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped +* runlist fragments and allows their mapping. If you do not have the mft +* record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock() +* will perform the necessary mapping and unmapping. +* +* Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and +* restores it before returning. Thus, @ctx will be left pointing to the same +* attribute on return as on entry. However, the actual pointers in @ctx may +* point to different memory locations on return, so you must remember to reset +* any cached pointers from the @ctx, i.e. after the call to +* ntfs_attr_find_vcn_nolock(), you will probably want to do: +* m = ctx->mrec; +* a = ctx->attr; +* Assuming you cache ctx->attr in a variable @a of type attr_record * and that +* you cache ctx->mrec in a variable @m of type struct mft_record *. +* Note you need to distinguish between the lcn of the returned runlist element +* being >= 0 and LCN_HOLE. In the later case you have to return zeroes on +* read and allocate clusters on write. +*/ +struct runlist_element *ntfs_attr_find_vcn_nolock (struct ntfs_inode *ni,const s64 vcn, +struct ntfs_attr_search_ctx *ctx) +{ +unsigned long flags; +struct runlist_element *rl; +int err =0 ; +bool is_retry = false; + +BUG_ON (!ni); +ntfs_debug ("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx." , + ni->mft_no, (unsigned long long )vcn, ctx ?"" :"out" ); +BUG_ON (!NInoNonResident (ni)); +BUG_ON (vcn <0 ); +if (!ni->runlist.rl) { +read_lock_irqsave (&ni->size_lock, flags); +if (!ni->allocated_size) { +read_unlock_irqrestore (&ni->size_lock, flags); +return ERR_PTR (-ENOENT ); + } +read_unlock_irqrestore (&ni->size_lock, flags); + } + +retry_remap: + rl = ni->runlist.rl; +if (likely (rl && vcn >= rl[0 ].vcn)) { + rl =__ntfs_attr_find_vcn_nolock (&ni->runlist, vcn); +if (IS_ERR (rl)) + err =PTR_ERR (rl); +else if (rl->lcn >=LCN_HOLE ) +return rl; +else if (rl->lcn <=LCN_ENOENT ) + err = -EIO ; + } +if (!err && !is_retry) { +/* +* If the search context is invalid we cannot map the unmapped +* region. +*/ +if (ctx &&IS_ERR (ctx->mrec)) + err =PTR_ERR (ctx->mrec); +else { +/* +* The @vcn is in an unmapped region, map the runlist +* and retry. +*/ + err =ntfs_map_runlist_nolock (ni, vcn, ctx); +if (likely (!err)) { + is_retry = true; + goto retry_remap; + } + } +if (err == -EINVAL ) + err = -EIO ; + }else if (!err) + err = -EIO ; +if (err != -ENOENT ) +ntfs_error (ni->vol->sb,"Failed with error code %i." , err); +return ERR_PTR (err); +} + +/** +* ntfs_attr_find - find (next) attribute in mft record +* @type: attribute type to find +* @name: attribute name to find (optional, i.e. NULL means don't care) +* @name_len: attribute name length (only needed if @name present) +* @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) +* @val: attribute value to find (optional, resident attributes only) +* @val_len: attribute value length +* @ctx: search context with mft record and attribute to search from +* +* You should not need to call this function directly. Use ntfs_attr_lookup() +* instead. +* +* ntfs_attr_find() takes a search context @ctx as parameter and searches the +* mft record specified by @ctx->mrec, beginning at @ctx->attr, for an +* attribute of @type, optionally @name and @val. +* +* If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will +* point to the found attribute. +* +* If the attribute is not found, ntfs_attr_find() returns -ENOENT and +* @ctx->attr will point to the attribute before which the attribute being +* searched for would need to be inserted if such an action were to be desired. +* +* On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is +* undefined and in particular do not rely on it not changing. +* +* If @ctx->is_first is 'true', the search begins with @ctx->attr itself. If it +* is 'false', the search begins after @ctx->attr. +* +* If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and +* @ctx->ntfs_ino must be set to the ntfs inode to which the mft record +* @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at +* the upcase table. If @ic is CASE_SENSITIVE, the comparison is case +* sensitive. When @name is present, @name_len is the @name length in Unicode +* characters. +* +* If @name is not present (NULL), we assume that the unnamed attribute is +* being searched for. +* +* Finally, the resident attribute value @val is looked for, if present. If +* @val is not present (NULL), @val_len is ignored. +* +* ntfs_attr_find() only searches the specified mft record and it ignores the +* presence of an attribute list attribute (unless it is the one being searched +* for, obviously). If you need to take attribute lists into consideration, +* use ntfs_attr_lookup() instead (see below). This also means that you cannot +* use ntfs_attr_find() to search for extent records of non-resident +* attributes, as extents with lowest_vcn != 0 are usually described by the +* attribute list attribute only. - Note that it is possible that the first +* extent is only in the attribute list while the last extent is in the base +* mft record, so do not rely on being able to find the first extent in the +* base mft record. +* +* Warning: Never use @val when looking for attribute types which can be +* non-resident as this most likely will result in a crash! +*/ +static int ntfs_attr_find (const __le32 type,const __le16 *name, +const u32 name_len,const u32 ic, +const u8 *val,const u32 val_len,struct ntfs_attr_search_ctx *ctx) +{ +struct attr_record *a; +struct ntfs_volume *vol = ctx->ntfs_ino->vol; +__le16 *upcase = vol->upcase; +u32 upcase_len = vol->upcase_len; +unsigned int space; + +/* +* Iterate over attributes in mft record starting at @ctx->attr, or the +* attribute following that, if @ctx->is_first is 'true'. +*/ +if (ctx->is_first) { + a = ctx->attr; + ctx->is_first = false; + }else + a = (struct attr_record *)((u8 *)ctx->attr + +le32_to_cpu (ctx->attr->length)); +for (;; a = (struct attr_record *)((u8 *)a +le32_to_cpu (a->length))) { +if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec + +le32_to_cpu (ctx->mrec->bytes_allocated)) +break ; + + space =le32_to_cpu (ctx->mrec->bytes_in_use) - ((u8 *)a - (u8 *)ctx->mrec); + if ((space < offsetof(struct attr_record, data.resident.reserved) + 1 || + space < le32_to_cpu(a->length)) && (space < 4 || a->type != AT_END)) +break ; + + ctx->attr = a; +if (((type !=AT_UNUSED ) && (le32_to_cpu (a->type) >le32_to_cpu (type))) || + a->type ==AT_END ) +return -ENOENT ; +if (unlikely (!a->length)) +break ; +if (type ==AT_UNUSED ) +return 0 ; +if (a->type != type) +continue ; +/* +* If @name is present, compare the two names. If @name is +* missing, assume we want an unnamed attribute. +*/ +if (!name || name ==AT_UNNAMED ) { +/* The search failed if the found attribute is named. */ +if (a->name_length) +return -ENOENT ; + }else { +if (a->name_length && ((le16_to_cpu (a->name_offset) + + a->name_length *sizeof (__le16)) > +le32_to_cpu (a->length))) { +ntfs_error (vol->sb,"Corrupt attribute name in MFT record %lld\n" , + (long long )ctx->ntfs_ino->mft_no); +break ; + } + +if (!ntfs_are_names_equal (name, name_len, + (__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length, ic, upcase, upcase_len)) { + registerint rc; + + rc =ntfs_collate_names (name, name_len, + (__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length,1 ,IGNORE_CASE , + upcase, upcase_len); +/* +* If @name collates before a->name, there is no +* matching attribute. +*/ +if (rc ==-1 ) +return -ENOENT ; +/* If the strings are not equal, continue search. */ +if (rc) +continue ; + rc =ntfs_collate_names (name, name_len, + (__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length,1 ,CASE_SENSITIVE , + upcase, upcase_len); +if (rc ==-1 ) +return -ENOENT ; +if (rc) +continue ; + } + } +/* +* The names match or @name not present and attribute is +* unnamed. If no @val specified, we have found the attribute +* and are done. +*/ +if (!val) +return 0 ; +/* @val is present; compare values. */ +else { + registerint rc; + + rc =memcmp (val, (u8 *)a +le16_to_cpu ( + a->data.resident.value_offset), +min_t (u32, val_len,le32_to_cpu ( + a->data.resident.value_length))); +/* +* If @val collates before the current attribute's +* value, there is no matching attribute. +*/ +if (!rc) { + registeru32 avl; + + avl =le32_to_cpu (a->data.resident.value_length); +if (val_len == avl) +return 0 ; +if (val_len < avl) +return -ENOENT ; + }else if (rc <0 ) +return -ENOENT ; + } + } +ntfs_error (vol->sb,"Inode is corrupt. Run chkdsk." ); +NVolSetErrors (vol); +return -EIO ; +} + +void ntfs_attr_name_free (unsigned char **name) +{ +if (*name) { +ntfs_free (*name); + *name =NULL ; + } +} + +char *ntfs_attr_name_get (const struct ntfs_volume *vol,const __le16 *uname, +const int uname_len) +{ +unsigned char *name =NULL ; +int name_len; + + name_len =ntfs_ucstonls (vol, uname, uname_len, &name,0 ); +if (name_len <0 ) { +ntfs_error (vol->sb,"ntfs_ucstonls error" ); +/* This function when returns -1, memory for name might +* be allocated. So lets free this memory. +*/ +ntfs_attr_name_free (&name); +return NULL ; + + }else if (name_len >0 ) +return name; + +ntfs_attr_name_free (&name); +return NULL ; +} + +int load_attribute_list (struct ntfs_inode *base_ni,u8 *al_start,const s64 size) +{ +struct inode *attr_vi =NULL ; +u8 *al; +struct attr_list_entry *ale; + +if (!al_start || size <=0 ) +return -EINVAL ; + + attr_vi =ntfs_attr_iget (VFS_I (base_ni),AT_ATTRIBUTE_LIST ,AT_UNNAMED ,0 ); +if (IS_ERR (attr_vi)) { +ntfs_error (base_ni->vol->sb, +"Failed to open an inode for Attribute list, mft = %ld" , + base_ni->mft_no); +return PTR_ERR (attr_vi); + } + +if (ntfs_inode_attr_pread (attr_vi,0 , size, al_start) != size) { +iput (attr_vi); +ntfs_error (base_ni->vol->sb, +"Failed to read attribute list, mft = %ld" , + base_ni->mft_no); +return -EIO ; + } +iput (attr_vi); + +for (al = al_start; al < al_start + size; al +=le16_to_cpu (ale->length)) { + ale = (struct attr_list_entry *)al; +if (ale->name_offset !=sizeof (struct attr_list_entry )) +break ; +if (le16_to_cpu (ale->length) <= ale->name_offset + ale->name_length || + al +le16_to_cpu (ale->length) > al_start + size) +break ; +if (ale->type ==AT_UNUSED ) +break ; +if (MSEQNO_LE (ale->mft_reference) ==0 ) +break ; + } +if (al != al_start + size) { +ntfs_error (base_ni->vol->sb,"Corrupt attribute list, mft = %ld" , + base_ni->mft_no); +return -EIO ; + } +return 0 ; +} + +/** +* ntfs_external_attr_find - find an attribute in the attribute list of an inode +* @type: attribute type to find +* @name: attribute name to find (optional, i.e. NULL means don't care) +* @name_len: attribute name length (only needed if @name present) +* @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) +* @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) +* @val: attribute value to find (optional, resident attributes only) +* @val_len: attribute value length +* @ctx: search context with mft record and attribute to search from +* +* You should not need to call this function directly. Use ntfs_attr_lookup() +* instead. +* +* Find an attribute by searching the attribute list for the corresponding +* attribute list entry. Having found the entry, map the mft record if the +* attribute is in a different mft record/inode, ntfs_attr_find() the attribute +* in there and return it. +* +* On first search @ctx->ntfs_ino must be the base mft record and @ctx must +* have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent +* calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is +* then the base inode). +* +* After finishing with the attribute/mft record you need to call +* ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any +* mapped inodes, etc). +* +* If the attribute is found, ntfs_external_attr_find() returns 0 and +* @ctx->attr will point to the found attribute. @ctx->mrec will point to the +* mft record in which @ctx->attr is located and @ctx->al_entry will point to +* the attribute list entry for the attribute. +* +* If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and +* @ctx->attr will point to the attribute in the base mft record before which +* the attribute being searched for would need to be inserted if such an action +* were to be desired. @ctx->mrec will point to the mft record in which +* @ctx->attr is located and @ctx->al_entry will point to the attribute list +* entry of the attribute before which the attribute being searched for would +* need to be inserted if such an action were to be desired. +* +* Thus to insert the not found attribute, one wants to add the attribute to +* @ctx->mrec (the base mft record) and if there is not enough space, the +* attribute should be placed in a newly allocated extent mft record. The +* attribute list entry for the inserted attribute should be inserted in the +* attribute list attribute at @ctx->al_entry. +* +* On actual error, ntfs_external_attr_find() returns -EIO. In this case +* @ctx->attr is undefined and in particular do not rely on it not changing. +*/ +static int ntfs_external_attr_find (const __le32 type, +const __le16 *name,const u32 name_len, +const u32 ic,const s64 lowest_vcn, +const u8 *val,const u32 val_len,struct ntfs_attr_search_ctx *ctx) +{ +struct ntfs_inode *base_ni, *ni; +struct ntfs_volume *vol; +struct attr_list_entry *al_entry, *next_al_entry; +u8 *al_start, *al_end; +struct attr_record *a; +__le16 *al_name; +u32 al_name_len; +bool is_first_search = false; +int err =0 ; +static const char *es =" Unmount and run chkdsk." ; + + ni = ctx->ntfs_ino; + base_ni = ctx->base_ntfs_ino; +ntfs_debug ("Entering for inode 0x%lx, type 0x%x." , ni->mft_no, type); +if (!base_ni) { +/* First call happens with the base mft record. */ + base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino; + ctx->base_mrec = ctx->mrec; + ctx->mapped_base_mrec = ctx->mapped_mrec; + } +if (ni == base_ni) + ctx->base_attr = ctx->attr; +if (type ==AT_END ) + goto not_found; + vol = base_ni->vol; + al_start = base_ni->attr_list; + al_end = al_start + base_ni->attr_list_size; +if (!ctx->al_entry) { + ctx->al_entry = (struct attr_list_entry *)al_start; + is_first_search = true; + } +/* +* Iterate over entries in attribute list starting at @ctx->al_entry, +* or the entry following that, if @ctx->is_first is 'true'. +*/ +if (ctx->is_first) { + al_entry = ctx->al_entry; + ctx->is_first = false; +/* +* If an enumeration and the first attribute is higher than +* the attribute list itself, need to return the attribute list +* attribute. +*/ +if ((type ==AT_UNUSED ) && is_first_search && +le32_to_cpu (al_entry->type) > +le32_to_cpu (AT_ATTRIBUTE_LIST )) + goto find_attr_list_attr; + }else { +/* Check for small entry */ +if (((al_end - (u8 *)ctx->al_entry) < + (long )offsetof(struct attr_list_entry , name)) || + (le16_to_cpu (ctx->al_entry->length) &7 ) || + (le16_to_cpu (ctx->al_entry->length) < offsetof(struct attr_list_entry , name))) + goto corrupt; + + al_entry = (struct attr_list_entry *)((u8 *)ctx->al_entry + +le16_to_cpu (ctx->al_entry->length)); + +if ((u8 *)al_entry == al_end) + goto not_found; + +/* Preliminary check for small entry */ +if ((al_end - (u8 *)al_entry) < + (long )offsetof(struct attr_list_entry , name)) + goto corrupt; + +/* +* If this is an enumeration and the attribute list attribute +* is the next one in the enumeration sequence, just return the +* attribute list attribute from the base mft record as it is +* not listed in the attribute list itself. +*/ +if ((type ==AT_UNUSED ) &&le32_to_cpu (ctx->al_entry->type) < +le32_to_cpu (AT_ATTRIBUTE_LIST ) && +le32_to_cpu (al_entry->type) > +le32_to_cpu (AT_ATTRIBUTE_LIST )) { +find_attr_list_attr: + +/* Check for bogus calls. */ +if (name || name_len || val || val_len || lowest_vcn) +return -EINVAL ; + +/* We want the base record. */ +if (ctx->ntfs_ino != base_ni) +unmap_mft_record (ctx->ntfs_ino); + ctx->ntfs_ino = base_ni; + ctx->mapped_mrec = ctx->mapped_base_mrec; + ctx->mrec = ctx->base_mrec; + ctx->is_first = true; + +/* Sanity checks are performed elsewhere. */ + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); + +/* Find the attribute list attribute. */ + err =ntfs_attr_find (AT_ATTRIBUTE_LIST ,NULL ,0 , +IGNORE_CASE ,NULL ,0 , ctx); + +/* +* Setup the search context so the correct +* attribute is returned next time round. +*/ + ctx->al_entry = al_entry; + ctx->is_first = true; + +/* Got it. Done. */ +if (!err) +return 0 ; + +/* Error! If other than not found return it. */ +if (err != -ENOENT ) +return err; + +/* Not found?!? Absurd! */ +ntfs_error (ctx->ntfs_ino->vol->sb,"Attribute list wasn't found" ); +return -EIO ; + } + } +for (;; al_entry = next_al_entry) { +/* Out of bounds check. */ +if ((u8 *)al_entry < base_ni->attr_list || + (u8 *)al_entry > al_end) +break ;/* Inode is corrupt. */ + ctx->al_entry = al_entry; +/* Catch the end of the attribute list. */ +if ((u8 *)al_entry == al_end) + goto not_found; + +if ((((u8 *)al_entry + offsetof(struct attr_list_entry , name)) > al_end) || + ((u8 *)al_entry +le16_to_cpu (al_entry->length) > al_end) || + (le16_to_cpu (al_entry->length) &7 ) || + (le16_to_cpu (al_entry->length) < + offsetof(struct attr_list_entry , name_length)) || + (al_entry->name_length && ((u8 *)al_entry + al_entry->name_offset + + al_entry->name_length *sizeof (__le16)) > al_end)) +break ;/* corrupt */ + + next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + +le16_to_cpu (al_entry->length)); +if (type !=AT_UNUSED ) { +if (le32_to_cpu (al_entry->type) >le32_to_cpu (type)) + goto not_found; +if (type != al_entry->type) +continue ; + } +/* +* If @name is present, compare the two names. If @name is +* missing, assume we want an unnamed attribute. +*/ + al_name_len = al_entry->name_length; + al_name = (__le16 *)((u8 *)al_entry + al_entry->name_offset); + +/* +* If !@type we want the attribute represented by this +* attribute list entry. +*/ +if (type ==AT_UNUSED ) + goto is_enumeration; + +if (!name || name ==AT_UNNAMED ) { +if (al_name_len) + goto not_found; + }else if (!ntfs_are_names_equal (al_name, al_name_len, name, + name_len, ic, vol->upcase, vol->upcase_len)) { + registerint rc; + + rc =ntfs_collate_names (name, name_len, al_name, + al_name_len,1 ,IGNORE_CASE , + vol->upcase, vol->upcase_len); +/* +* If @name collates before al_name, there is no +* matching attribute. +*/ +if (rc ==-1 ) + goto not_found; +/* If the strings are not equal, continue search. */ +if (rc) +continue ; + + rc =ntfs_collate_names (name, name_len, al_name, + al_name_len,1 ,CASE_SENSITIVE , + vol->upcase, vol->upcase_len); +if (rc ==-1 ) + goto not_found; +if (rc) +continue ; + } +/* +* The names match or @name not present and attribute is +* unnamed. Now check @lowest_vcn. Continue search if the +* next attribute list entry still fits @lowest_vcn. Otherwise +* we have reached the right one or the search has failed. +*/ +if (lowest_vcn && (u8 *)next_al_entry >= al_start && + (u8 *)next_al_entry +6 < al_end && + (u8 *)next_al_entry +le16_to_cpu ( + next_al_entry->length) <= al_end && +le64_to_cpu (next_al_entry->lowest_vcn) <= + lowest_vcn && + next_al_entry->type == al_entry->type && + next_al_entry->name_length == al_name_len && +ntfs_are_names_equal ((__le16 *)((u8 *) + next_al_entry + + next_al_entry->name_offset), + next_al_entry->name_length, + al_name, al_name_len,CASE_SENSITIVE , + vol->upcase, vol->upcase_len)) +continue ; + +is_enumeration: +if (MREF_LE (al_entry->mft_reference) == ni->mft_no) { +if (MSEQNO_LE (al_entry->mft_reference) != ni->seq_no) { +ntfs_error (vol->sb, +"Found stale mft reference in attribute list of base inode 0x%lx.%s" , + base_ni->mft_no, es); + err = -EIO ; +break ; + } + }else {/* Mft references do not match. */ +/* If there is a mapped record unmap it first. */ +if (ni != base_ni) +unmap_extent_mft_record (ni); +/* Do we want the base record back? */ +if (MREF_LE (al_entry->mft_reference) == + base_ni->mft_no) { + ni = ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + ctx->mapped_mrec = ctx->mapped_base_mrec; + }else { +/* We want an extent record. */ + ctx->mrec =map_extent_mft_record (base_ni, +le64_to_cpu ( + al_entry->mft_reference), &ni); +if (IS_ERR (ctx->mrec)) { +ntfs_error (vol->sb, +"Failed to map extent mft record 0x%lx of base inode 0x%lx.%s" , +MREF_LE (al_entry->mft_reference), + base_ni->mft_no, es); + err =PTR_ERR (ctx->mrec); +if (err == -ENOENT ) + err = -EIO ; +/* Cause @ctx to be sanitized below. */ + ni =NULL ; +break ; + } + ctx->ntfs_ino = ni; + ctx->mapped_mrec = true; + + } + } + a = ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); +/* +* ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the +* mft record containing the attribute represented by the +* current al_entry. +*/ +/* +* We could call into ntfs_attr_find() to find the right +* attribute in this mft record but this would be less +* efficient and not quite accurate as ntfs_attr_find() ignores +* the attribute instance numbers for example which become +* important when one plays with attribute lists. Also, +* because a proper match has been found in the attribute list +* entry above, the comparison can now be optimized. So it is +* worth re-implementing a simplified ntfs_attr_find() here. +*/ +/* +* Use a manual loop so we can still use break and continue +* with the same meanings as above. +*/ +do_next_attr_loop: +if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec + +le32_to_cpu (ctx->mrec->bytes_allocated)) +break ; +if (a->type ==AT_END ) +continue ; +if (!a->length) +break ; +if (al_entry->instance != a->instance) + goto do_next_attr; +/* +* If the type and/or the name are mismatched between the +* attribute list entry and the attribute record, there is +* corruption so we break and return error EIO. +*/ +if (al_entry->type != a->type) +break ; +if (!ntfs_are_names_equal ((__le16 *)((u8 *)a + +le16_to_cpu (a->name_offset)), a->name_length, + al_name, al_name_len,CASE_SENSITIVE , + vol->upcase, vol->upcase_len)) +break ; + ctx->attr = a; +/* +* If no @val specified or @val specified and it matches, we +* have found it! +*/ +if ((type ==AT_UNUSED ) || !val || (!a->non_resident &&le32_to_cpu ( + a->data.resident.value_length) == val_len && + !memcmp ((u8 *)a + +le16_to_cpu (a->data.resident.value_offset), + val, val_len))) { +ntfs_debug ("Done, found." ); +return 0 ; + } +do_next_attr: +/* Proceed to the next attribute in the current mft record. */ + a = (struct attr_record *)((u8 *)a +le32_to_cpu (a->length)); + goto do_next_attr_loop; + } + +corrupt: +if (ni != base_ni) { +if (ni) +unmap_extent_mft_record (ni); + ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + ctx->attr = ctx->base_attr; + ctx->mapped_mrec = ctx->mapped_base_mrec; + } + +if (!err) { +ntfs_error (vol->sb, +"Base inode 0x%lx contains corrupt attribute list attribute.%s" , + base_ni->mft_no, es); + err = -EIO ; + } + +if (err != -ENOMEM ) +NVolSetErrors (vol); +return err; +not_found: +/* +* If we were looking for AT_END, we reset the search context @ctx and +* use ntfs_attr_find() to seek to the end of the base mft record. +*/ +if (type ==AT_UNUSED || type ==AT_END ) { +ntfs_attr_reinit_search_ctx (ctx); +return ntfs_attr_find (AT_END , name, name_len, ic, val, val_len, + ctx); + } +/* +* The attribute was not found. Before we return, we want to ensure +* @ctx->mrec and @ctx->attr indicate the position at which the +* attribute should be inserted in the base mft record. Since we also +* want to preserve @ctx->al_entry we cannot reinitialize the search +* context using ntfs_attr_reinit_search_ctx() as this would set +* @ctx->al_entry to NULL. Thus we do the necessary bits manually (see +* ntfs_attr_init_search_ctx() below). Note, we _only_ preserve +* @ctx->al_entry as the remaining fields (base_*) are identical to +* their non base_ counterparts and we cannot set @ctx->base_attr +* correctly yet as we do not know what @ctx->attr will be set to by +* the call to ntfs_attr_find() below. +*/ +if (ni != base_ni) +unmap_extent_mft_record (ni); + ctx->mrec = ctx->base_mrec; + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); + ctx->is_first = true; + ctx->ntfs_ino = base_ni; + ctx->base_ntfs_ino =NULL ; + ctx->base_mrec =NULL ; + ctx->base_attr =NULL ; + ctx->mapped_mrec = ctx->mapped_base_mrec; +/* +* In case there are multiple matches in the base mft record, need to +* keep enumerating until we get an attribute not found response (or +* another error), otherwise we would keep returning the same attribute +* over and over again and all programs using us for enumeration would +* lock up in a tight loop. +*/ +do { + err =ntfs_attr_find (type, name, name_len, ic, val, val_len, + ctx); + }while (!err); +ntfs_debug ("Done, not found." ); +return err; +} + +/** +* ntfs_attr_lookup - find an attribute in an ntfs inode +* @type: attribute type to find +* @name: attribute name to find (optional, i.e. NULL means don't care) +* @name_len: attribute name length (only needed if @name present) +* @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) +* @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) +* @val: attribute value to find (optional, resident attributes only) +* @val_len: attribute value length +* @ctx: search context with mft record and attribute to search from +* +* Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must +* be the base mft record and @ctx must have been obtained from a call to +* ntfs_attr_get_search_ctx(). +* +* This function transparently handles attribute lists and @ctx is used to +* continue searches where they were left off at. +* +* After finishing with the attribute/mft record you need to call +* ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any +* mapped inodes, etc). +* +* Return 0 if the search was successful and -errno if not. +* +* When 0, @ctx->attr is the found attribute and it is in mft record +* @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is +* the attribute list entry of the found attribute. +* +* When -ENOENT, @ctx->attr is the attribute which collates just after the +* attribute being searched for, i.e. if one wants to add the attribute to the +* mft record this is the correct place to insert it into. If an attribute +* list attribute is present, @ctx->al_entry is the attribute list entry which +* collates just after the attribute list entry of the attribute being searched +* for, i.e. if one wants to add the attribute to the mft record this is the +* correct place to insert its attribute list entry into. +*/ +int ntfs_attr_lookup (const __le32 type,const __le16 *name, +const u32 name_len,const u32 ic, +const s64 lowest_vcn,const u8 *val,const u32 val_len, +struct ntfs_attr_search_ctx *ctx) +{ +struct ntfs_inode *base_ni; + +ntfs_debug ("Entering." ); +BUG_ON (IS_ERR (ctx->mrec)); +if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; +else + base_ni = ctx->ntfs_ino; +/* Sanity check, just for debugging really. */ +if (!base_ni || !NInoAttrList (base_ni) || type ==AT_ATTRIBUTE_LIST ) +return ntfs_attr_find (type, name, name_len, ic, val, val_len, + ctx); +return ntfs_external_attr_find (type, name, name_len, ic, lowest_vcn, + val, val_len, ctx); +} + +/** +* ntfs_attr_init_search_ctx - initialize an attribute search context +* @ctx: attribute search context to initialize +* @ni: ntfs inode with which to initialize the search context +* @mrec: mft record with which to initialize the search context +* +* Initialize the attribute search context @ctx with @ni and @mrec. +*/ +static bool ntfs_attr_init_search_ctx (struct ntfs_attr_search_ctx *ctx, +struct ntfs_inode *ni,struct mft_record *mrec) +{ +if (!mrec) { + mrec =map_mft_record (ni); +if (IS_ERR (mrec)) +return false; + ctx->mapped_mrec = true; + }else { + ctx->mapped_mrec = false; + } + + ctx->mrec = mrec; +/* Sanity checks are performed elsewhere. */ + ctx->attr = (struct attr_record *)((u8 *)mrec +le16_to_cpu (mrec->attrs_offset)); + ctx->is_first = true; + ctx->ntfs_ino = ni; + ctx->al_entry =NULL ; + ctx->base_ntfs_ino =NULL ; + ctx->base_mrec =NULL ; + ctx->base_attr =NULL ; + ctx->mapped_base_mrec = false; +return true; +} + +/** +* ntfs_attr_reinit_search_ctx - reinitialize an attribute search context +* @ctx: attribute search context to reinitialize +* +* Reinitialize the attribute search context @ctx, unmapping an associated +* extent mft record if present, and initialize the search context again. +* +* This is used when a search for a new attribute is being started to reset +* the search context to the beginning. +*/ +void ntfs_attr_reinit_search_ctx (struct ntfs_attr_search_ctx *ctx) +{ +bool mapped_mrec; + +if (likely (!ctx->base_ntfs_ino)) { +/* No attribute list. */ + ctx->is_first = true; +/* Sanity checks are performed elsewhere. */ + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + +le16_to_cpu (ctx->mrec->attrs_offset)); +/* +* This needs resetting due to ntfs_external_attr_find() which +* can leave it set despite having zeroed ctx->base_ntfs_ino. +*/ + ctx->al_entry =NULL ; +return ; + }/* Attribute list. */ +if (ctx->ntfs_ino != ctx->base_ntfs_ino && ctx->ntfs_ino) +unmap_extent_mft_record (ctx->ntfs_ino); + + mapped_mrec = ctx->mapped_base_mrec; +ntfs_attr_init_search_ctx (ctx, ctx->base_ntfs_ino, ctx->base_mrec); + ctx->mapped_mrec = mapped_mrec; +} + +/** +* ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context +* @ni: ntfs inode with which to initialize the search context +* @mrec: mft record with which to initialize the search context +* +* Allocate a new attribute search context, initialize it with @ni and @mrec, +* and return it. Return NULL if allocation failed. +*/ +struct ntfs_attr_search_ctx *ntfs_attr_get_search_ctx (struct ntfs_inode *ni, +struct mft_record *mrec) +{ +struct ntfs_attr_search_ctx *ctx; +bool init; + + ctx =kmem_cache_alloc (ntfs_attr_ctx_cache,GFP_NOFS ); +if (ctx) { + init =ntfs_attr_init_search_ctx (ctx, ni, mrec); +if (init == false) { +kmem_cache_free (ntfs_attr_ctx_cache, ctx); + ctx =NULL ; + } + } + +return ctx; +} + +/** +* ntfs_attr_put_search_ctx - release an attribute search context +* @ctx: attribute search context to free +* +* Release the attribute search context @ctx, unmapping an associated extent +* mft record if present. +*/ +void ntfs_attr_put_search_ctx (struct ntfs_attr_search_ctx *ctx) +{ +if (ctx->mapped_mrec) +unmap_mft_record (ctx->ntfs_ino); + +if (ctx->mapped_base_mrec && ctx->base_ntfs_ino && + ctx->ntfs_ino != ctx->base_ntfs_ino) +unmap_extent_mft_record (ctx->base_ntfs_ino); +kmem_cache_free (ntfs_attr_ctx_cache, ctx); +} + +/** +* ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to find +* +* Search for the attribute definition record corresponding to the attribute +* @type in the $AttrDef system file. +* +* Return the attribute type definition record if found and NULL if not found. +*/ +static struct attr_def *ntfs_attr_find_in_attrdef (const struct ntfs_volume *vol, +const __le32 type) +{ +struct attr_def *ad; + +BUG_ON (!vol->attrdef); +BUG_ON (!type); +for (ad = vol->attrdef; (u8 *)ad - (u8 *)vol->attrdef < + vol->attrdef_size && ad->type; ++ad) { +/* We have not found it yet, carry on searching. */ +if (likely (le32_to_cpu (ad->type) <le32_to_cpu (type))) +continue ; +/* We found the attribute; return it. */ +if (likely (ad->type == type)) +return ad; +/* We have gone too far already. No point in continuing. */ +break ; + } +/* Attribute not found. */ +ntfs_debug ("Attribute type 0x%x not found in $AttrDef." , +le32_to_cpu (type)); +return NULL ; +} + +/** +* ntfs_attr_size_bounds_check - check a size of an attribute type for validity +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to check +* @size: size which to check +* +* Check whether the @size in bytes is valid for an attribute of @type on the +* ntfs volume @vol. This information is obtained from $AttrDef system file. +*/ +int ntfs_attr_size_bounds_check (const struct ntfs_volume *vol,const __le32 type, +const s64 size) +{ +struct attr_def *ad; + +BUG_ON (size <0 ); +/* +* $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not +* listed in $AttrDef. +*/ +if (unlikely (type ==AT_ATTRIBUTE_LIST && size >256 *1024 )) +return -ERANGE ; +/* Get the $AttrDef entry for the attribute @type. */ + ad =ntfs_attr_find_in_attrdef (vol, type); +if (unlikely (!ad)) +return -ENOENT ; +/* Do the bounds check. */ +if (((le64_to_cpu (ad->min_size) >0 ) && + size <le64_to_cpu (ad->min_size)) || + ((le64_to_cpu (ad->max_size) >0 ) && size > +le64_to_cpu (ad->max_size))) +return -ERANGE ; +return 0 ; +} + +/** +* ntfs_attr_can_be_non_resident - check if an attribute can be non-resident +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to check +* +* Check whether the attribute of @type on the ntfs volume @vol is allowed to +* be non-resident. This information is obtained from $AttrDef system file. +*/ +static int ntfs_attr_can_be_non_resident (const struct ntfs_volume *vol, +const __le32 type) +{ +struct attr_def *ad; + +/* Find the attribute definition record in $AttrDef. */ + ad =ntfs_attr_find_in_attrdef (vol, type); +if (unlikely (!ad)) +return -ENOENT ; +/* Check the flags and return the result. */ +if (ad->flags &ATTR_DEF_RESIDENT ) +return -EPERM ; +return 0 ; +} + +/** +* ntfs_attr_can_be_resident - check if an attribute can be resident +* @vol: ntfs volume to which the attribute belongs +* @type: attribute type which to check +* +* Check whether the attribute of @type on the ntfs volume @vol is allowed to +* be resident. This information is derived from our ntfs knowledge and may +* not be completely accurate, especially when user defined attributes are +* present. Basically we allow everything to be resident except for index +* allocation and $EA attributes. +* +* Return 0 if the attribute is allowed to be non-resident and -EPERM if not. +* +* Warning: In the system file $MFT the attribute $Bitmap must be non-resident +* otherwise windows will not boot (blue screen of death)! We cannot +* check for this here as we do not know which inode's $Bitmap is +* being asked about so the caller needs to special case this. +*/ +int ntfs_attr_can_be_resident (const struct ntfs_volume *vol,const __le32 type) +{ +if (type ==AT_INDEX_ALLOCATION ) +return -EPERM ; +return 0 ; +} + +/** +* ntfs_attr_record_resize - resize an attribute record +* @m: mft record containing attribute record +* @a: attribute record to resize +* @new_size: new size in bytes to which to resize the attribute record @a +* +* Resize the attribute record @a, i.e. the resident part of the attribute, in +* the mft record @m to @new_size bytes. +*/ +int ntfs_attr_record_resize (struct mft_record *m,struct attr_record *a,u32 new_size) +{ +u32 old_size, alloc_size, attr_size; + + old_size =le32_to_cpu (m->bytes_in_use); + alloc_size =le32_to_cpu (m->bytes_allocated); + attr_size =le32_to_cpu (a->length); + +ntfs_debug ("Sizes: old=%u alloc=%u attr=%u new=%u\n" , + (unsigned int )old_size, (unsigned int )alloc_size, + (unsigned int )attr_size, (unsigned int )new_size); + +/* Align to 8 bytes if it is not already done. */ +if (new_size &7 ) + new_size = (new_size +7 ) & ~7 ; +/* If the actual attribute length has changed, move things around. */ +if (new_size != attr_size) { +u32 new_muse =le32_to_cpu (m->bytes_in_use) - + attr_size + new_size; +/* Not enough space in this mft record. */ +if (new_muse >le32_to_cpu (m->bytes_allocated)) +return -ENOSPC ; + +if (a->type ==AT_INDEX_ROOT && new_size > attr_size && + new_muse +120 > alloc_size && old_size +120 <= alloc_size) { +ntfs_debug ("Too big struct index_root (%u > %u)\n" , + new_muse, alloc_size); +return -ENOSPC ; + } + +/* Move attributes following @a to their new location. */ +memmove ((u8 *)a + new_size, (u8 *)a +le32_to_cpu (a->length), +le32_to_cpu (m->bytes_in_use) - ((u8 *)a - + (u8 *)m) - attr_size); +/* Adjust @m to reflect the change in used space. */ + m->bytes_in_use =cpu_to_le32 (new_muse); +/* Adjust @a to reflect the new size. */ +if (new_size >= offsetof(struct attr_record , length) +sizeof (a->length)) + a->length =cpu_to_le32 (new_size); + } +return 0 ; +} + +/** +* ntfs_resident_attr_value_resize - resize the value of a resident attribute +* @m: mft record containing attribute record +* @a: attribute record whose value to resize +* @new_size: new size in bytes to which to resize the attribute value of @a +* +* Resize the value of the attribute @a in the mft record @m to @new_size bytes. +* If the value is made bigger, the newly allocated space is cleared. +*/ +int ntfs_resident_attr_value_resize (struct mft_record *m,struct attr_record *a, +const u32 new_size) +{ +u32 old_size; + +/* Resize the resident part of the attribute record. */ +if (ntfs_attr_record_resize (m, a, +le16_to_cpu (a->data.resident.value_offset) + new_size)) +return -ENOSPC ; +/* +* The resize succeeded! If we made the attribute value bigger, clear +* the area between the old size and @new_size. +*/ + old_size =le32_to_cpu (a->data.resident.value_length); +if (new_size > old_size) +memset ((u8 *)a +le16_to_cpu (a->data.resident.value_offset) + + old_size,0 , new_size - old_size); +/* Finally update the length of the attribute value. */ + a->data.resident.value_length =cpu_to_le32 (new_size); +return 0 ; +} + +/** +* ntfs_attr_make_non_resident - convert a resident to a non-resident attribute +* @ni: ntfs inode describing the attribute to convert +* @data_size: size of the resident data to copy to the non-resident attribute +* +* Convert the resident ntfs attribute described by the ntfs inode @ni to a +* non-resident one. +* +* @data_size must be equal to the attribute value size. This is needed since +* we need to know the size before we can map the mft record and our callers +* always know it. The reason we cannot simply read the size from the vfs +* inode i_size is that this is not necessarily uptodate. This happens when +* ntfs_attr_make_non_resident() is called in the ->truncate call path(s). +*/ +int ntfs_attr_make_non_resident (struct ntfs_inode *ni,const u32 data_size) +{ +s64 new_size; +struct inode *vi =VFS_I (ni); +struct ntfs_volume *vol = ni->vol; +struct ntfs_inode *base_ni; +struct mft_record *m; +struct attr_record *a; +struct ntfs_attr_search_ctx *ctx; +struct folio *folio; +struct runlist_element *rl; +u8 *kaddr; +unsigned long flags; +int mp_size, mp_ofs, name_ofs, arec_size, err, err2; +u32 attr_size; +u8 old_res_attr_flags; + +if (NInoNonResident (ni)) { +ntfs_warning (vol->sb, +"Trying to make non-resident attribute non-resident. Aborting...\n" ); +return -EINVAL ; + } + +/* Check that the attribute is allowed to be non-resident. */ + err =ntfs_attr_can_be_non_resident (vol, ni->type); +if (unlikely (err)) { +if (err == -EPERM ) +ntfs_debug ("Attribute is not allowed to be non-resident." ); +else +ntfs_debug ("Attribute not defined on the NTFS volume!" ); +return err; + } + +BUG_ON (NInoEncrypted (ni)); + +if (!NInoAttr (ni)) + base_ni = ni; +else + base_ni = ni->ext.base_ntfs_ino; + m =map_mft_record (base_ni); +if (IS_ERR (m)) { + err =PTR_ERR (m); + m =NULL ; + ctx =NULL ; + goto err_out; + } + ctx =ntfs_attr_get_search_ctx (base_ni, m); +if (unlikely (!ctx)) { + err = -ENOMEM ; + goto err_out; + } + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (unlikely (err)) { +if (err == -ENOENT ) + err = -EIO ; + goto err_out; + } + m = ctx->mrec; + a = ctx->attr; + +/* +* The size needs to be aligned to a cluster boundary for allocation +* purposes. +*/ + new_size = (data_size + vol->cluster_size -1 ) & + ~(vol->cluster_size -1 ); +if (new_size >0 ) { +if ((a->flags &ATTR_COMPRESSION_MASK ) ==ATTR_IS_COMPRESSED ) { +/* must allocate full compression blocks */ + new_size = + ((new_size -1 ) | + ((1L << (STANDARD_COMPRESSION_UNIT + + vol->cluster_size_bits)) -1 )) +1 ; + } + +/* +* Will need folio later and since folio lock nests +* outside all ntfs locks, we need to get the folio now. +*/ + folio =__filemap_get_folio (vi->i_mapping,0 , +FGP_CREAT |FGP_LOCK , +mapping_gfp_mask (vi->i_mapping)); +if (IS_ERR (folio)) { + err = -ENOMEM ; + goto err_out; + } + +/* Start by allocating clusters to hold the attribute value. */ + rl =ntfs_cluster_alloc (vol,0 , new_size >> + vol->cluster_size_bits,-1 ,DATA_ZONE , true, + false, false); +if (IS_ERR (rl)) { + err =PTR_ERR (rl); +ntfs_debug ("Failed to allocate cluster%s, error code %i." , + (new_size >> vol->cluster_size_bits) >1 ?"s" :"" , + err); + goto folio_err_out; + } + }else { + rl =NULL ; + folio =NULL ; + } + +down_write (&ni->runlist.lock); +/* Determine the size of the mapping pairs array. */ + mp_size =ntfs_get_size_for_mapping_pairs (vol, 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 rl_err_out; + } + +if (NInoNonResident (ni) || a->non_resident) { + err = -EIO ; + goto rl_err_out; + } + +/* +* Calculate new offsets for the name and the mapping pairs array. +*/ +if (NInoSparse (ni) ||NInoCompressed (ni)) + 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_ofs = (name_ofs + a->name_length *sizeof (__le16) +7 ) & ~7 ; +/* +* Determine the size of the resident part of the now non-resident +* attribute record. +*/ + arec_size = (mp_ofs + mp_size +7 ) & ~7 ; +/* +* If the folio is not uptodate bring it uptodate by copying from the +* attribute value. +*/ + attr_size =le32_to_cpu (a->data.resident.value_length); +BUG_ON (attr_size != data_size); +if (folio && !folio_test_uptodate (folio)) { + kaddr =kmap_local_folio (folio,0 ); +memcpy (kaddr, (u8 *)a + +le16_to_cpu (a->data.resident.value_offset), + attr_size); +memset (kaddr + attr_size,0 ,PAGE_SIZE - attr_size); +kunmap_local (kaddr); +flush_dcache_folio (folio); +folio_mark_uptodate (folio); + } + +/* Backup the attribute flag. */ + old_res_attr_flags = a->data.resident.flags; +/* Resize the resident part of the attribute record. */ + err =ntfs_attr_record_resize (m, a, arec_size); +if (unlikely (err)) + goto rl_err_out; + +/* +* Convert the resident part of the attribute record to describe a +* non-resident attribute. +*/ + a->non_resident =1 ; +/* Move the attribute name if it exists and update the offset. */ +if (a->name_length) +memmove ((u8 *)a + name_ofs, (u8 *)a +le16_to_cpu (a->name_offset), + a->name_length *sizeof (__le16)); + a->name_offset =cpu_to_le16 (name_ofs); +/* Setup the fields specific to non-resident attributes. */ + a->data.non_resident.lowest_vcn =0 ; + a->data.non_resident.highest_vcn =cpu_to_le64 ((new_size -1 ) >> + vol->cluster_size_bits); + a->data.non_resident.mapping_pairs_offset =cpu_to_le16 (mp_ofs); +memset (&a->data.non_resident.reserved,0 , +sizeof (a->data.non_resident.reserved)); + a->data.non_resident.allocated_size =cpu_to_le64 (new_size); + a->data.non_resident.data_size = + a->data.non_resident.initialized_size = +cpu_to_le64 (attr_size); +if (NInoSparse (ni) ||NInoCompressed (ni)) { + a->data.non_resident.compression_unit =0 ; +if (NInoCompressed (ni) || vol->major_ver <3 ) + a->data.non_resident.compression_unit =4 ; + a->data.non_resident.compressed_size = + a->data.non_resident.allocated_size; + }else + a->data.non_resident.compression_unit =0 ; +/* Generate the mapping pairs array into the attribute record. */ + err =ntfs_mapping_pairs_build (vol, (u8 *)a + mp_ofs, + arec_size - mp_ofs, rl,0 ,-1 ,NULL ,NULL ,NULL ); +if (unlikely (err)) { +ntfs_error (vol->sb,"Failed to build mapping pairs, error code %i." , + err); + goto undo_err_out; + } + +/* Setup the in-memory attribute structure to be non-resident. */ + ni->runlist.rl = rl; +if (rl) { +for (ni->runlist.count =1 ; rl->length !=0 ; rl++) + ni->runlist.count++; + }else + ni->runlist.count =0 ; +write_lock_irqsave (&ni->size_lock, flags); + ni->allocated_size = new_size; +if (NInoSparse (ni) ||NInoCompressed (ni)) { + ni->itype.compressed.size = ni->allocated_size; +if (a->data.non_resident.compression_unit) { + ni->itype.compressed.block_size =1U << + (a->data.non_resident.compression_unit + + 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 ; + } + vi->i_blocks = ni->itype.compressed.size >>9 ; + }else + vi->i_blocks = ni->allocated_size >>9 ; +write_unlock_irqrestore (&ni->size_lock, flags); +/* +* This needs to be last since the address space operations ->read_folio +* and ->writepage can run concurrently with us as they are not +* serialized on i_mutex. Note, we are not allowed to fail once we flip +* this switch, which is another reason to do this last. +*/ +NInoSetNonResident (ni); +NInoSetFullyMapped (ni); +/* Mark the mft record dirty, so it gets written back. */ +mark_mft_record_dirty (ctx->ntfs_ino); +ntfs_attr_put_search_ctx (ctx); +unmap_mft_record (base_ni); +up_write (&ni->runlist.lock); +if (folio) { +iomap_dirty_folio (vi->i_mapping, folio); +folio_unlock (folio); +folio_put (folio); + } +ntfs_debug ("Done." ); +return 0 ; +undo_err_out: +/* Convert the attribute back into a resident attribute. */ + a->non_resident =0 ; +/* Move the attribute name if it exists and update the offset. */ + name_ofs = (offsetof(struct attr_record, data.resident.reserved) + + sizeof(a->data.resident.reserved) + 7) & ~7; +if (a->name_length) +memmove ((u8 *)a + name_ofs, (u8 *)a +le16_to_cpu (a->name_offset), + a->name_length *sizeof (__le16)); + mp_ofs = (name_ofs + a->name_length *sizeof (__le16) +7 ) & ~7 ; + a->name_offset =cpu_to_le16 (name_ofs); + arec_size = (mp_ofs + attr_size +7 ) & ~7 ; +/* Resize the resident part of the attribute record. */ + err2 =ntfs_attr_record_resize (m, a, arec_size); +if (unlikely (err2)) { +/* +* This cannot happen (well if memory corruption is at work it +* could happen in theory), but deal with it as well as we can. +* If the old size is too small, truncate the attribute, +* otherwise simply give it a larger allocated size. +*/ + arec_size =le32_to_cpu (a->length); +if ((mp_ofs + attr_size) > arec_size) { + err2 = attr_size; + attr_size = arec_size - mp_ofs; +ntfs_error (vol->sb, +"Failed to undo partial resident to non-resident attribute conversion. Truncating inode 0x%lx, attribute type 0x%x from %i bytes to %i bytes to maintain metadata consistency. THIS MEANS YOU ARE LOSING %i BYTES DATA FROM THIS %s." , + vi->i_ino, + (unsigned int )le32_to_cpu (ni->type), + err2, attr_size, err2 - attr_size, + ((ni->type ==AT_DATA ) && + !ni->name_len) ?"FILE" :"ATTRIBUTE" ); +write_lock_irqsave (&ni->size_lock, flags); + ni->initialized_size = attr_size; +i_size_write (vi, attr_size); +write_unlock_irqrestore (&ni->size_lock, flags); + } + } +/* Setup the fields specific to resident attributes. */ + a->data.resident.value_length =cpu_to_le32 (attr_size); + a->data.resident.value_offset =cpu_to_le16 (mp_ofs); + a->data.resident.flags = old_res_attr_flags; +memset (&a->data.resident.reserved,0 , +sizeof (a->data.resident.reserved)); +/* Copy the data from folio back to the attribute value. */ +if (folio) +memcpy_from_folio ((u8 *)a + mp_ofs, folio,0 , attr_size); +/* Setup the allocated size in the ntfs inode in case it changed. */ +write_lock_irqsave (&ni->size_lock, flags); + ni->allocated_size = arec_size - mp_ofs; +write_unlock_irqrestore (&ni->size_lock, flags); +/* Mark the mft record dirty, so it gets written back. */ +mark_mft_record_dirty (ctx->ntfs_ino); +rl_err_out: +up_write (&ni->runlist.lock); +if (rl) { +if (ntfs_cluster_free_from_rl (vol, rl) <0 ) { +ntfs_error (vol->sb, +"Failed to release allocated cluster(s) in error code path. Run chkdsk to recover the lost cluster(s)." ); +NVolSetErrors (vol); + } +ntfs_free (rl); +folio_err_out: +folio_unlock (folio); +folio_put (folio); + } +err_out: +if (ctx) +ntfs_attr_put_search_ctx (ctx); +if (m) +unmap_mft_record (base_ni); + ni->runlist.rl =NULL ; + +if (err == -EINVAL ) + err = -EIO ; +return err; +} + +/** +* ntfs_attr_set - fill (a part of) an attribute with a byte +* @ni: ntfs inode describing the attribute to fill +* @ofs: offset inside the attribute at which to start to fill +* @cnt: number of bytes to fill +* @val: the unsigned 8-bit value with which to fill the attribute +* +* Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at +* byte offset @ofs inside the attribute with the constant byte @val. +* +* This function is effectively like memset() applied to an ntfs attribute. +* Note thie function actually only operates on the page cache pages belonging +* to the ntfs attribute and it marks them dirty after doing the memset(). +* Thus it relies on the vm dirty page write code paths to cause the modified +* pages to be written to the mft record/disk. +*/ +int ntfs_attr_set (struct ntfs_inode *ni,s64 ofs,s64 cnt,const u8 val) +{ +struct address_space *mapping =VFS_I (ni)->i_mapping; +struct folio *folio; +pgoff_t index; +u8 *addr; +unsigned long offset; +size_t attr_len; +int ret =0 ; + + index = ofs >>PAGE_SHIFT ; +while (cnt) { + folio =ntfs_read_mapping_folio (mapping, index); +if (IS_ERR (folio)) { + ret =PTR_ERR (folio); +ntfs_error (VFS_I (ni)->i_sb,"Failed to read a page %lu for attr %#x: %ld" , + index, ni->type,PTR_ERR (folio)); +break ; + } + + offset =offset_in_folio (folio, ofs); + attr_len =min_t (size_t, (size_t )cnt,folio_size (folio) - offset); + +folio_lock (folio); + addr =kmap_local_folio (folio, offset); +memset (addr, val, attr_len); +kunmap_local (addr); + +flush_dcache_folio (folio); +folio_mark_dirty (folio); +folio_unlock (folio); +folio_put (folio); + + ofs += attr_len; + cnt -= attr_len; + index++; +cond_resched (); + } + +return ret; +} + +int ntfs_attr_set_initialized_size (struct ntfs_inode *ni,loff_t new_size) +{ +struct ntfs_attr_search_ctx *ctx; +int err =0 ; + +if (!NInoNonResident (ni)) +return -EINVAL ; + + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) +return -ENOMEM ; + + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE ,0 ,NULL ,0 , ctx); +if (err) + goto out_ctx; + + ctx->attr->data.non_resident.initialized_size =cpu_to_le64 (new_size); + ni->initialized_size = new_size; +mark_mft_record_dirty (ctx->ntfs_ino); +out_ctx: +ntfs_attr_put_search_ctx (ctx); +return err; +} + +/** +* ntfs_make_room_for_attr - make room for an attribute inside an mft record +* @m: mft record +* @pos: position at which to make space +* @size: byte size to make available at this position +* +* @pos points to the attribute in front of which we want to make space. +*/ +static int ntfs_make_room_for_attr (struct mft_record *m,u8 *pos,u32 size) +{ +u32 biu; + +ntfs_debug ("Entering for pos 0x%x, size %u.\n" , + (int )(pos - (u8 *)m), (unsigned int ) size); + +/* Make size 8-byte alignment. */ + size = (size +7 ) & ~7 ; + +/* Rigorous consistency checks. */ +if (!m || !pos || pos < (u8 *)m) { +pr_err ("%s: pos=%p m=%p" , __func__, pos, m); +return -EINVAL ; + } + +/* The -8 is for the attribute terminator. */ +if (pos - (u8 *)m > (int )le32_to_cpu (m->bytes_in_use) -8 ) +return -EINVAL ; +/* Nothing to do. */ +if (!size) +return 0 ; + + biu =le32_to_cpu (m->bytes_in_use); +/* Do we have enough space? */ +if (biu + size >le32_to_cpu (m->bytes_allocated) || + pos + size > (u8 *)m +le32_to_cpu (m->bytes_allocated)) { +ntfs_debug ("No enough space in the MFT record\n" ); +return -ENOSPC ; + } +/* Move everything after pos to pos + size. */ +memmove (pos + size, pos, biu - (pos - (u8 *)m)); +/* Update mft record. */ + m->bytes_in_use =cpu_to_le32 (biu + size); +return 0 ; +} + +/** +* ntfs_resident_attr_record_add - add resident attribute to inode +* @ni: opened ntfs inode to which MFT record add attribute +* @type: type of the new attribute +* @name: name of the new attribute +* @name_len: name length of the new attribute +* @val: value of the new attribute +* @size: size of new attribute (length of @val, if @val != NULL) +* @flags: flags of the new attribute +*/ +int ntfs_resident_attr_record_add (struct ntfs_inode *ni,__le32 type, +__le16 *name,u8 name_len,u8 *val,u32 size, +__le16 flags) +{ +struct ntfs_attr_search_ctx *ctx; +u32 length; +struct attr_record *a; +struct mft_record *m; +int err, offset; +struct ntfs_inode *base_ni; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n" , + (long long ) ni->mft_no, (unsigned int )le32_to_cpu (type), + (unsigned int )le16_to_cpu (flags)); + +if (!ni || (!name && name_len)) +return -EINVAL ; + + err =ntfs_attr_can_be_resident (ni->vol, type); +if (err) { +if (err == -EPERM ) +ntfs_debug ("Attribute can't be resident.\n" ); +else +ntfs_debug ("ntfs_attr_can_be_resident failed.\n" ); +return err; + } + +/* Locate place where record should be. */ + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) { +ntfs_error (ni->vol->sb,"%s: Failed to get search context" , + __func__); +return -ENOMEM ; + } +/* +* Use ntfs_attr_find instead of ntfs_attr_lookup to find place for +* attribute in @ni->mrec, not any extent inode in case if @ni is base +* file record. +*/ + err =ntfs_attr_find (type, name, name_len,CASE_SENSITIVE , val, size, ctx); +if (!err) { + err = -EEXIST ; +ntfs_debug ("Attribute already present.\n" ); + goto put_err_out; + } +if (err != -ENOENT ) { + err = -EIO ; + goto put_err_out; + } + a = ctx->attr; + m = ctx->mrec; + +/* Make room for attribute. */ + length = offsetof(struct attr_record, data.resident.reserved) + +sizeof (a->data.resident.reserved) + + ((name_len *sizeof (__le16) +7 ) & ~7 ) + + ((size +7 ) & ~7 ); + err =ntfs_make_room_for_attr (ctx->mrec, (u8 *) ctx->attr, length); +if (err) { +ntfs_debug ("Failed to make room for attribute.\n" ); + goto put_err_out; + } + +/* Setup record fields. */ + offset = ((u8 *)a - (u8 *)m); + a->type = type; + a->length =cpu_to_le32 (length); + a->non_resident =0 ; + a->name_length = name_len; + a->name_offset = + name_len ? cpu_to_le16((offsetof(struct attr_record, data.resident.reserved) + + sizeof(a->data.resident.reserved))) : cpu_to_le16(0); + + a->flags = flags; + a->instance = m->next_attr_instance; + a->data.resident.value_length =cpu_to_le32 (size); + a->data.resident.value_offset =cpu_to_le16 (length - ((size +7 ) & ~7 )); +if (val) +memcpy ((u8 *)a +le16_to_cpu (a->data.resident.value_offset), val, size); +else +memset ((u8 *)a +le16_to_cpu (a->data.resident.value_offset),0 , size); +if (type ==AT_FILE_NAME ) + a->data.resident.flags =RESIDENT_ATTR_IS_INDEXED ; +else + a->data.resident.flags =0 ; +if (name_len) +memcpy ((u8 *)a +le16_to_cpu (a->name_offset), + name,sizeof (__le16) * name_len); + m->next_attr_instance = +cpu_to_le16 ((le16_to_cpu (m->next_attr_instance) +1 ) &0xffff ); +if (ni->nr_extents ==-1 ) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; +if (type !=AT_ATTRIBUTE_LIST &&NInoAttrList (base_ni)) { + err =ntfs_attrlist_entry_add (ni, a); +if (err) { +ntfs_attr_record_resize (m, a,0 ); +mark_mft_record_dirty (ctx->ntfs_ino); +ntfs_debug ("Failed add attribute entry to ATTRIBUTE_LIST.\n" ); + goto put_err_out; + } + } +mark_mft_record_dirty (ni); +ntfs_attr_put_search_ctx (ctx); +return offset; +put_err_out: +ntfs_attr_put_search_ctx (ctx); +return -EIO ; +} + +/** +* ntfs_non_resident_attr_record_add - add extent of non-resident attribute +* @ni: opened ntfs inode to which MFT record add attribute +* @type: type of the new attribute extent +* @name: name of the new attribute extent +* @name_len: name length of the new attribute extent +* @lowest_vcn: lowest vcn of the new attribute extent +* @dataruns_size: dataruns size of the new attribute extent +* @flags: flags of the new attribute extent +*/ +static int ntfs_non_resident_attr_record_add (struct ntfs_inode *ni,__le32 type, +__le16 *name,u8 name_len,s64 lowest_vcn,int dataruns_size, +__le16 flags) +{ +struct ntfs_attr_search_ctx *ctx; +u32 length; +struct attr_record *a; +struct mft_record *m; +struct ntfs_inode *base_ni; +int err, offset; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n" , + (long long ) ni->mft_no, (unsigned int )le32_to_cpu (type), + (long long ) lowest_vcn, dataruns_size, + (unsigned int )le16_to_cpu (flags)); + +if (!ni || dataruns_size <=0 || (!name && name_len)) +return -EINVAL ; + + err =ntfs_attr_can_be_non_resident (ni->vol, type); +if (err) { +if (err == -EPERM ) +pr_err ("Attribute can't be non resident" ); +else +pr_err ("ntfs_attr_can_be_non_resident failed" ); +return err; + } + +/* Locate place where record should be. */ + ctx =ntfs_attr_get_search_ctx (ni,NULL ); +if (!ctx) { +pr_err ("%s: Failed to get search context" , __func__); +return -ENOMEM ; + } +/* +* Use ntfs_attr_find instead of ntfs_attr_lookup to find place for +* attribute in @ni->mrec, not any extent inode in case if @ni is base +* file record. +*/ + err =ntfs_attr_find (type, name, name_len,CASE_SENSITIVE ,NULL ,0 , ctx); +if (!err) { + err = -EEXIST ; +pr_err ("Attribute 0x%x already present" , type); + goto put_err_out; + } +if (err != -ENOENT ) { +pr_err ("ntfs_attr_find failed" ); + err = -EIO ; + goto put_err_out; + } + a = ctx->attr; + m = ctx->mrec; + +/* Make room for attribute. */ + dataruns_size = (dataruns_size +7 ) & ~7 ; + length = offsetof(struct attr_record, data.non_resident.compressed_size) + + ((sizeof (__le16) * name_len +7 ) & ~7 ) + dataruns_size + + ((flags & (ATTR_IS_COMPRESSED |ATTR_IS_SPARSE )) ? +sizeof (a->data.non_resident.compressed_size) :0 ); + err =ntfs_make_room_for_attr (ctx->mrec, (u8 *) ctx->attr, length); +if (err) { +pr_err ("Failed to make room for attribute" ); + goto put_err_out; + } + +/* Setup record fields. */ + a->type = type; + a->length =cpu_to_le32 (length); + a->non_resident =1 ; + a->name_length = name_len; + a->name_offset =cpu_to_le16 (offsetof(struct attr_record , + data.non_resident.compressed_size) + + ((flags & (ATTR_IS_COMPRESSED |ATTR_IS_SPARSE )) ? + sizeof(a->data.non_resident.compressed_size) : 0)); + a->flags = flags; + a->instance = m->next_attr_instance; + a->data.non_resident.lowest_vcn =cpu_to_le64 (lowest_vcn); + a->data.non_resident.mapping_pairs_offset =cpu_to_le16 (length - dataruns_size); + a->data.non_resident.compression_unit = + (flags &ATTR_IS_COMPRESSED ) ?STANDARD_COMPRESSION_UNIT :0 ; +/* If @lowest_vcn == 0, than setup empty attribute. */ +if (!lowest_vcn) { + a->data.non_resident.highest_vcn =cpu_to_le64 (-1 ); + a->data.non_resident.allocated_size =0 ; + a->data.non_resident.data_size =0 ; + a->data.non_resident.initialized_size =0 ; +/* Set empty mapping pairs. */ + *((u8 *)a +le16_to_cpu (a->data.non_resident.mapping_pairs_offset)) =0 ; + } +if (name_len) +memcpy ((u8 *)a +le16_to_cpu (a->name_offset), + name,sizeof (__le16) * name_len); + m->next_attr_instance = +cpu_to_le16 ((le16_to_cpu (m->next_attr_instance) +1 ) &0xffff ); +if (ni->nr_extents ==-1 ) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; +if (type !=AT_ATTRIBUTE_LIST &&NInoAttrList (base_ni)) { + err =ntfs_attrlist_entry_add (ni, a); +if (err) { +pr_err ("Failed add attr entry to attrlist" ); +ntfs_attr_record_resize (m, a,0 ); + goto put_err_out; + } + } +mark_mft_record_dirty (ni); +/* +* Locate offset from start of the MFT record where new attribute is +* placed. We need relookup it, because record maybe moved during +* update of attribute list. +*/ +ntfs_attr_reinit_search_ctx (ctx); + err =ntfs_attr_lookup (type, name, name_len,CASE_SENSITIVE , + lowest_vcn,NULL ,0 , ctx); +if (err) { +pr_err ("%s: attribute lookup failed" , __func__); +ntfs_attr_put_search_ctx (ctx); +return err; + + } + offset = (u8 *)ctx->attr - (u8 *)ctx->mrec; +ntfs_attr_put_search_ctx (ctx); +return offset; +put_err_out: +ntfs_attr_put_search_ctx (ctx); +return -1 ; +} + +/** +* ntfs_attr_record_rm - remove attribute extent +* @ctx: search context describing the attribute which should be removed +* +* If this function succeed, user should reinit search context if he/she wants +* use it anymore. +*/ +int ntfs_attr_record_rm (struct ntfs_attr_search_ctx *ctx) +{ +struct ntfs_inode *base_ni, *ni; +__le32 type; +int err; + +if (!ctx || !ctx->ntfs_ino || !ctx->mrec || !ctx->attr) +return -EINVAL ; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x.\n" , + (long long ) ctx->ntfs_ino->mft_no, + (unsigned int )le32_to_cpu (ctx->attr->type)); + type = ctx->attr->type; + ni = ctx->ntfs_ino; +if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; +else + base_ni = ctx->ntfs_ino; + +/* Remove attribute itself. */ +if (ntfs_attr_record_resize (ctx->mrec, ctx->attr,0 )) { +ntfs_debug ("Couldn't remove attribute record. Bug or damaged MFT record.\n" ); +return -EIO ; + } +mark_mft_record_dirty (ni); + +/* +* Remove record from $ATTRIBUTE_LIST if present and we don't want +* delete $ATTRIBUTE_LIST itself. +*/ +if (NInoAttrList (base_ni) && type !=AT_ATTRIBUTE_LIST ) { + err =ntfs_attrlist_entry_rm (ctx); +if (err) { +ntfs_debug ("Couldn't delete record from $ATTRIBUTE_LIST.\n" ); +return err; + } + } + +/* Post $ATTRIBUTE_LIST delete setup. */ +if (type ==AT_ATTRIBUTE_LIST ) { +if (NInoAttrList (base_ni) && base_ni->attr_list) +ntfs_free (base_ni->attr_list); + base_ni->attr_list =NULL ; +NInoClearAttrList (base_ni); + } + +/* Free MFT record, if it doesn't contain attributes. */ +if (le32_to_cpu (ctx->mrec->bytes_in_use) - +le16_to_cpu (ctx->mrec->attrs_offset) ==8 ) { +if (ntfs_mft_record_free (ni->vol, ni)) { +ntfs_debug ("Couldn't free MFT record.\n" ); +return -EIO ; + } +/* Remove done if we freed base inode. */ +if (ni == base_ni) +return 0 ; +ntfs_inode_close (ni); + ctx->ntfs_ino = ni =NULL ; + } + +if (type ==AT_ATTRIBUTE_LIST || !NInoAttrList (base_ni)) +return 0 ; + +/* Remove attribute list if we don't need it any more. */ +if (!ntfs_attrlist_need (base_ni)) { +struct ntfs_attr na; +struct inode *attr_vi; + +ntfs_attr_reinit_search_ctx (ctx); +if (ntfs_attr_lookup (AT_ATTRIBUTE_LIST ,NULL ,0 ,CASE_SENSITIVE , +0 ,NULL ,0 , ctx)) { +ntfs_debug ("Couldn't find attribute list. Succeed anyway.\n" ); +return 0 ; + } +/* Deallocate clusters. */ +if (ctx->attr->non_resident) { +struct runlist_element *al_rl; +size_t new_rl_count; + + al_rl =ntfs_mapping_pairs_decompress (base_ni->vol, + ctx->attr,NULL , &new_rl_count); +if (IS_ERR (al_rl)) { +ntfs_debug ("Couldn't decompress attribute list runlist. Succeed anyway.\n" ); +return 0 ; + } +if (ntfs_cluster_free_from_rl (base_ni->vol, al_rl)) +ntfs_debug ("Leaking clusters! Run chkdsk. Couldn't free clusters from attribute list runlist.\n" ); +ntfs_free (al_rl); + } +/* Remove attribute record itself. */ +if (ntfs_attr_record_rm (ctx)) { +ntfs_debug ("Couldn't remove attribute list. Succeed anyway.\n" ); +return 0 ; + } + + na.mft_no =VFS_I (base_ni)->i_ino; + na.type =AT_ATTRIBUTE_LIST ; + na.name =NULL ; + na.name_len =0 ; + + attr_vi =ilookup5 (VFS_I (base_ni)->i_sb,VFS_I (base_ni)->i_ino, + ntfs_test_inode, &na); +if (attr_vi) { +clear_nlink (attr_vi); +iput (attr_vi); + } + + } +return 0 ; +} + +/** +* ntfs_attr_add - add attribute to inode +* @ni: opened ntfs inode to which add attribute +* @type: type of the new attribute +* @name: name in unicode of the new attribute +* @name_len: name length in unicode characters of the new attribute +* @val: value of new attribute +* @size: size of the new attribute / length of @val (if specified) +* +* @val should always be specified for always resident attributes (eg. FILE_NAME +* attribute), for attributes that can become non-resident @val can be NULL +* (eg. DATA attribute). @size can be specified even if @val is NULL, in this +* case data size will be equal to @size and initialized size will be equal +* to 0. +* +* If inode haven't got enough space to add attribute, add attribute to one of +* it extents, if no extents present or no one of them have enough space, than +* allocate new extent and add attribute to it. +* +* If on one of this steps attribute list is needed but not present, than it is +* added transparently to caller. So, this function should not be called with +* @type == AT_ATTRIBUTE_LIST, if you really need to add attribute list call +* ntfs_inode_add_attrlist instead. +* +* On success return 0. On error return -1 with errno set to the error code. +*/ +int ntfs_attr_add (struct ntfs_inode *ni,__le32 type, +__le16 *name,u8 name_len,u8 *val,s64 size) +{ +struct super_block *sb; +u32 attr_rec_size; +int err, i, offset; +bool is_resident; +bool can_be_non_resident = false; +struct ntfs_inode *attr_ni; +struct inode *attr_vi; +struct mft_record *ni_mrec; + +if (!ni || size <0 || type ==AT_ATTRIBUTE_LIST ) +return -EINVAL ; + +ntfs_debug ("Entering for inode 0x%llx, attr %x, size %lld.\n" , + (long long ) ni->mft_no, type, size); + +if (ni->nr_extents ==-1 ) + ni = ni->ext.base_ntfs_ino; + +/* Check the attribute type and the size. */ + err =ntfs_attr_size_bounds_check (ni->vol, type, size); +if (err) { +if (err == -ENOENT ) + err = -EIO ; +return err; + } + + sb = ni->vol->sb; +/* Sanity checks for always resident attributes. */ + err =ntfs_attr_can_be_non_resident (ni->vol, type); +if (err) { +if (err != -EPERM ) { +ntfs_error (sb,"ntfs_attr_can_be_non_resident failed" ); + goto err_out; + } +/* @val is mandatory. */ +if (!val) { +ntfs_error (sb, +"val is mandatory for always resident attributes" ); +return -EINVAL ; + } +if (size > ni->vol->mft_record_size) { +ntfs_error (sb,"Attribute is too big" ); +return -ERANGE ; + } + }else + can_be_non_resident = true; + +/* +* Determine resident or not will be new attribute. We add 8 to size in +* non resident case for mapping pairs. +*/ + err =ntfs_attr_can_be_resident (ni->vol, type); +if (!err) { + is_resident = true; + }else { +if (err != -EPERM ) { +ntfs_error (sb,"ntfs_attr_can_be_resident failed" ); + goto err_out; + } + is_resident = false; + } + +/* Calculate attribute record size. */ +if (is_resident) + attr_rec_size = offsetof(struct attr_record, data.resident.reserved) + +1 + + ((name_len *sizeof (__le16) +7 ) & ~7 ) + + ((size +7 ) & ~7 ); +else + attr_rec_size = offsetof(struct attr_record, data.non_resident.compressed_size) + + ((name_len *sizeof (__le16) +7 ) & ~7 ) +8 ; + +/* +* If we have enough free space for the new attribute in the base MFT +* record, then add attribute to it. +*/ +retry: + ni_mrec =map_mft_record (ni); +if (IS_ERR (ni_mrec)) { + err = -EIO ; + goto err_out; + } + +if (le32_to_cpu (ni_mrec->bytes_allocated) - +le32_to_cpu (ni_mrec->bytes_in_use) >= attr_rec_size) { + attr_ni = ni; +unmap_mft_record (ni); + goto add_attr_record; + } +unmap_mft_record (ni); + +/* Try to add to extent inodes. */ + err =ntfs_inode_attach_all_extents (ni); +if (err) { +ntfs_error (sb,"Failed to attach all extents to inode" ); + goto err_out; + } + +for (i =0 ; i < ni->nr_extents; i++) { + attr_ni = ni->ext.extent_ntfs_inos[i]; + ni_mrec =map_mft_record (attr_ni); +if (IS_ERR (ni_mrec)) { + err = -EIO ; + goto err_out; + } + +if (le32_to_cpu (ni_mrec->bytes_allocated) - +le32_to_cpu (ni_mrec->bytes_in_use) >= + attr_rec_size) { +unmap_mft_record (attr_ni); + goto add_attr_record; + } +unmap_mft_record (attr_ni); + } + +/* There is no extent that contain enough space for new attribute. */ +if (!NInoAttrList (ni)) { +/* Add attribute list not present, add it and retry. */ + err =ntfs_inode_add_attrlist (ni); +if (err) { +ntfs_error (sb,"Failed to add attribute list" ); + goto err_out; + } + goto retry; + } + + attr_ni =NULL ; +/* Allocate new extent. */ + err =ntfs_mft_record_alloc (ni->vol,0 , &attr_ni, ni,NULL ); +if (err) { +ntfs_error (sb,"Failed to allocate extent record" ); + goto err_out; + } +unmap_mft_record (attr_ni); + +add_attr_record: +if (is_resident) { +/* Add resident attribute. */ + offset =ntfs_resident_attr_record_add (attr_ni, type, name, + name_len, val, size,0 ); +if (offset <0 ) { +if (offset == -ENOSPC && can_be_non_resident) + goto add_non_resident; + err = offset; +ntfs_error (sb,"Failed to add resident attribute" ); + goto free_err_out; + } +return 0 ; + } + +add_non_resident: +/* Add non resident attribute. */ + offset =ntfs_non_resident_attr_record_add (attr_ni, type, name, + name_len,0 ,8 ,0 ); +if (offset <0 ) { + err = offset; +ntfs_error (sb,"Failed to add non resident attribute" ); + goto free_err_out; + } + +/* If @size == 0, we are done. */ +if (!size) +return 0 ; + +/* Open new attribute and resize it. */ + attr_vi =ntfs_attr_iget (VFS_I (ni), type, name, name_len); +if (IS_ERR (attr_vi)) { +ntfs_error (sb,"Failed to open just added attribute" ); + goto rm_attr_err_out; + } + attr_ni =NTFS_I (attr_vi); + +/* Resize and set attribute value. */ +if (ntfs_attr_truncate (attr_ni, size) || + (val && (ntfs_inode_attr_pwrite (attr_vi,0 , size, val, false) != size))) { + err = -EIO ; +ntfs_error (sb,"Failed to initialize just added attribute" ); +if (ntfs_attr_rm (attr_ni)) +ntfs_error (sb,"Failed to remove just added attribute" ); +iput (attr_vi); + goto err_out; + } +iput (attr_vi); +return 0 ; + +rm_attr_err_out: +/* Remove just added attribute. */ + ni_mrec =map_mft_record (attr_ni); +if (!IS_ERR (ni_mrec)) { +if (ntfs_attr_record_resize (ni_mrec, + (struct attr_record *)((u8 *)ni_mrec + offset),0 )) +ntfs_error (sb,"Failed to remove just added attribute #2" ); +unmap_mft_record (attr_ni); + }else +pr_err ("EIO when try to remove new added attr\n" ); + +free_err_out: +/* Free MFT record, if it doesn't contain attributes. */ + ni_mrec =map_mft_record (attr_ni); +if (!IS_ERR (ni_mrec)) { +int attr_size; + + attr_size =le32_to_cpu (ni_mrec->bytes_in_use) - +le16_to_cpu (ni_mrec->attrs_offset); +unmap_mft_record (attr_ni); +if (attr_size ==8 ) { +if (ntfs_mft_record_free (attr_ni->vol, attr_ni)) +ntfs_error (sb,"Failed to free MFT record" ); +if (attr_ni->nr_extents <0 ) +ntfs_inode_close (attr_ni); + } + }else +pr_err ("EIO when testing mft record is free-able\n" ); + +err_out: +return err; +} + +/** +* __ntfs_attr_init - primary initialization of an ntfs attribute structure +* @ni: ntfs attribute inode to initialize +* @ni: ntfs inode with which to initialize the ntfs attribute +* @type: attribute type +* @name: attribute name in little endian Unicode or NULL +* @name_len: length of attribute @name in Unicode characters (if @name given) +* +* Initialize the ntfs attribute @na with @ni, @type, @name, and @name_len. +*/ +static void __ntfs_attr_init (struct ntfs_inode *ni, +const __le32 type,__le16 *name,const u32 name_len) +{ + ni->runlist.rl =NULL ; + ni->type = type; + ni->name = name; +if (name) + ni->name_len = name_len; +else + ni->name_len =0 ; +} + +/** +* ntfs_attr_init - initialize an ntfs_attr with data sizes and status +* Final initialization for an ntfs attribute. +*/ +static void ntfs_attr_init (struct ntfs_inode *ni,const bool non_resident, +const bool compressed,const bool encrypted,const bool sparse, +const s64 allocated_size,const s64 data_size, +const s64 initialized_size,const s64 compressed_size, +const u8 compression_unit) +{ +if (non_resident) +NInoSetNonResident (ni); +if (compressed) { +NInoSetCompressed (ni); + ni->flags |=FILE_ATTR_COMPRESSED ; + } +if (encrypted) { +NInoSetEncrypted (ni); + ni->flags |=FILE_ATTR_ENCRYPTED ; + } +if (sparse) { +NInoSetSparse (ni); + ni->flags |=FILE_ATTR_SPARSE_FILE ; + } + ni->allocated_size = allocated_size; + ni->data_size = data_size; + ni->initialized_size = initialized_size; +if (compressed || sparse) { +struct ntfs_volume *vol = ni->vol; + + ni->itype.compressed.size = compressed_size; + ni->itype.compressed.block_clusters =1 << compression_unit; + ni->itype.compressed.block_size =1 << (compression_unit + + vol->cluster_size_bits); + ni->itype.compressed.block_size_bits =ffs ( + ni->itype.compressed.block_size) -1 ; + } +} + +/** +* ntfs_attr_open - open an ntfs attribute for access +* @ni: open ntfs inode in which the ntfs attribute resides +* @type: attribute type +* @name: attribute name in little endian Unicode or AT_UNNAMED or NULL +* @name_len: length of attribute @name in Unicode characters (if @name given) +*/ +int ntfs_attr_open (struct ntfs_inode *ni,const __le32 type, +__le16 *name,u32 name_len) +{ +struct ntfs_attr_search_ctx *ctx; +__le16 *newname =NULL ; +struct attr_record *a; +bool cs; +struct ntfs_inode *base_ni; +int err; + +ntfs_debug ("Entering for inode %lld, attr 0x%x.\n" , + (unsigned long long )ni->mft_no, type); + +if (!ni || !ni->vol) +return -EINVAL ; + +if (NInoAttr (ni)) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; + +if (name && name !=AT_UNNAMED && name !=I30 ) { + name =ntfs_ucsndup (name, name_len); +if (!name) { + err = -ENOMEM ; + goto err_out; + } + newname = name; + } + + ctx =ntfs_attr_get_search_ctx (base_ni,NULL ); +if (!ctx) { + err = -ENOMEM ; +pr_err ("%s: Failed to get search context" , __func__); + goto err_out; + } + + err =ntfs_attr_lookup (type, name, name_len,0 ,0 ,NULL ,0 , ctx); +if (err) + goto put_err_out; + + a = ctx->attr; + +if (!name) { +if (a->name_length) { + name =ntfs_ucsndup ((__le16 *)((u8 *)a +le16_to_cpu (a->name_offset)), + a->name_length); +if (!name) + goto put_err_out; + newname = name; + name_len = a->name_length; + }else { + name =AT_UNNAMED ; + name_len =0 ; + } + } + +__ntfs_attr_init (ni, type, name, name_len); + +/* +* Wipe the flags in case they are not zero for an attribute list +* attribute. Windows does not complain about invalid flags and chkdsk +* does not detect or fix them so we need to cope with it, too. +*/ +if (type ==AT_ATTRIBUTE_LIST ) + a->flags =0 ; + +if ((type ==AT_DATA ) && + (a->non_resident ? !a->data.non_resident.initialized_size : + !a->data.resident.value_length)) { +/* +* Define/redefine the compression state if stream is +* empty, based on the compression mark on parent +* directory (for unnamed data streams) or on current +* inode (for named data streams). The compression mark +* may change any time, the compression state can only +* change when stream is wiped out. +* +* Also prevent compression on NTFS version < 3.0 +* or cluster size > 4K or compression is disabled +*/ + a->flags &= ~ATTR_COMPRESSION_MASK ; +if (NInoCompressed (ni) + && (ni->vol->major_ver >=3 ) + &&NVolCompression (ni->vol) + && (ni->vol->cluster_size <=MAX_COMPRESSION_CLUSTER_SIZE )) + a->flags |=ATTR_IS_COMPRESSED ; + } + + cs = a->flags & (ATTR_IS_COMPRESSED |ATTR_IS_SPARSE ); + +if (ni->type ==AT_DATA && ni->name ==AT_UNNAMED && + ((!(a->flags &ATTR_IS_COMPRESSED ) != !NInoCompressed (ni)) || + (!(a->flags &ATTR_IS_SPARSE ) != !NInoSparse (ni)) || + (!(a->flags &ATTR_IS_ENCRYPTED ) != !NInoEncrypted (ni)))) { + err = -EIO ; +pr_err ("Inode %lld has corrupt attribute flags (0x%x <> 0x%x)\n" , + (unsigned long long )ni->mft_no, + a->flags, ni->flags); + goto put_err_out; + } + +if (a->non_resident) { +if (((a->flags &ATTR_COMPRESSION_MASK ) || a->data.non_resident.compression_unit) && + (ni->vol->major_ver <3 )) { + err = -EIO ; +pr_err ("Compressed inode %lld not allowed on NTFS %d.%d\n" , + (unsigned long long )ni->mft_no, + ni->vol->major_ver, + ni->vol->major_ver); + goto put_err_out; + } + +if ((a->flags &ATTR_IS_COMPRESSED ) && !a->data.non_resident.compression_unit) { + err = -EIO ; +pr_err ("Compressed inode %lld attr 0x%x has no compression unit\n" , + (unsigned long long )ni->mft_no, type); + goto put_err_out; + } +if ((a->flags &ATTR_COMPRESSION_MASK ) && + (a->data.non_resident.compression_unit !=STANDARD_COMPRESSION_UNIT )) { + err = -EIO ; +pr_err ("Compressed inode %lld attr 0x%lx has an unsupported compression unit %d\n" , + (unsigned long long )ni->mft_no, + (long )le32_to_cpu (type), + (int )a->data.non_resident.compression_unit); + goto put_err_out; + } +ntfs_attr_init (ni, true, a->flags &ATTR_IS_COMPRESSED , + a->flags &ATTR_IS_ENCRYPTED , + a->flags &ATTR_IS_SPARSE , +le64_to_cpu (a->data.non_resident.allocated_size), +le64_to_cpu (a->data.non_resident.data_size), +le64_to_cpu (a->data.non_resident.initialized_size), + cs ?le64_to_cpu (a->data.non_resident.compressed_size) :0 , + cs ? a->data.non_resident.compression_unit :0 ); + }else { +s64 l =le32_to_cpu (a->data.resident.value_length); + +ntfs_attr_init (ni, false, a->flags &ATTR_IS_COMPRESSED , + a->flags &ATTR_IS_ENCRYPTED , + a->flags &ATTR_IS_SPARSE , (l +7 ) & ~7 , l, l, + cs ? (l +7 ) & ~7 :0 ,0 ); + } +ntfs_attr_put_search_ctx (ctx); +out: +ntfs_debug ("\n" ); +return err; + +put_err_out: +ntfs_attr_put_search_ctx (ctx); +err_out: +ntfs_free (newname); + goto out; +} + +/** +* ntfs_attr_close - free an ntfs attribute structure +* @ni: ntfs inode to free +* +* Release all memory associated with the ntfs attribute @na and then release +* @na itself. +*/ +void ntfs_attr_close (struct ntfs_inode *ni) +{ +if (NInoNonResident (ni) && ni->runlist.rl) +ntfs_free (ni->runlist.rl); +/* Don't release if using an internal constant. */ +if (ni->name !=AT_UNNAMED && ni->name !=I30 ) +ntfs_free (ni->name); +} + +/** +* ntfs_attr_map_whole_runlist - map the whole runlist of an ntfs attribute +* @ni: ntfs inode for which to map the runlist +* +* Map the whole runlist of the ntfs attribute @na. For an attribute made up +* of only one attribute extent this is the same as calling +* ntfs_map_runlist(ni, 0) but for an attribute with multiple extents this +* will map the runlist fragments from each of the extents thus giving access +* to the entirety of the disk allocation of an attribute. +*/ +int ntfs_attr_map_whole_runlist (struct ntfs_inode *ni) +{ +s64 next_vcn, last_vcn, highest_vcn; +struct ntfs_attr_search_ctx *ctx; +struct ntfs_volume *vol = ni->vol; +struct super_block *sb = vol->sb; +struct attr_record *a; +int err; +struct ntfs_inode *base_ni; +int not_mapped; +size_t new_rl_count; + +ntfs_debug ("Entering for inode 0x%llx, attr 0x%x.\n" , + (unsigned long long )ni->mft_no, ni->type); + +if (NInoFullyMapped (ni) && ni->runlist.rl) +return 0 ; + +if (NInoAttr (ni)) + base_ni = ni->ext.base_ntfs_ino; +else + base_ni = ni; + + ctx =ntfs_attr_get_search_ctx (base_ni,NULL ); +if (!ctx) { +ntfs_error (sb,"%s: Failed to get search context" , __func__); +return -ENOMEM ; + } + +/* Map all attribute extents one by one. */ + next_vcn = last_vcn = highest_vcn =0 ; + a =NULL ; +while (1 ) { +struct runlist_element *rl; + + not_mapped =0 ; +if (ntfs_rl_vcn_to_lcn (ni->runlist.rl, next_vcn) ==LCN_RL_NOT_MAPPED ) + not_mapped =1 ; + + err =ntfs_attr_lookup (ni->type, ni->name, ni->name_len, +CASE_SENSITIVE , next_vcn,NULL ,0 , ctx); +if (err) +break ; + + a = ctx->attr; + +if (not_mapped) { +/* Decode the runlist. */ + rl =ntfs_mapping_pairs_decompress (ni->vol, a, &ni->runlist, + &new_rl_count); +if (IS_ERR (rl)) { + err =PTR_ERR (rl); + goto err_out; + } + ni->runlist.rl = rl; + ni->runlist.count = new_rl_count; + } + +/* Are we in the first extent? */ +if (!next_vcn) { +if (a->data.non_resident.lowest_vcn) { + err = -EIO ; +ntfs_error (sb, +"First extent of inode %llu attribute has non-zero lowest_vcn" , + (unsigned long long )ni->mft_no); + goto err_out;
Lines of this file are not shown. View the raw message.
fs/ntfsplus/attrlist.c new file +276 -0
276 lines not on this page · raw message
fs/ntfsplus/compress.c new file +1565 -0
1,565 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH 8/11 ntfsplus: add runlist handling and cluster allocator
In reply to Namjae JeonThis adds the implementation of runlist handling and cluster allocator for ntfsplus.
--- fs/ntfsplus/bitmap.c | 193 ++++ fs/ntfsplus/lcnalloc.c | 993 ++++++++++++++++++++ fs/ntfsplus/runlist.c | 1995 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 3181 insertions(+) create mode 100644 fs/ntfsplus/bitmap.c create mode 100644 fs/ntfsplus/lcnalloc.c create mode 100644 fs/ntfsplus/runlist.c
fs/ntfsplus/bitmap.c new file +193 -0
@@ -0,0 +1,193 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* NTFS kernel bitmap handling. Part of the Linux-NTFS project. +* +* Copyright (c) 2004-2005 Anton Altaparmakov +* Copyright (c) 2025 LG Electronics Co., Ltd. +*/ + +#include "bitmap.h" +#include "aops.h" +#include "ntfs.h" + +/** +* __ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value +* @vi: vfs inode describing the bitmap +* @start_bit: first bit to set +* @count: number of bits to set +* @value: value to set the bits to (i.e. 0 or 1) +* @is_rollback: if 'true' this is a rollback operation +* +* Set @count bits starting at bit @start_bit in the bitmap described by the +* vfs inode @vi to @value, where @value is either 0 or 1. +* +* @is_rollback should always be 'false', it is for internal use to rollback +* errors. You probably want to use ntfs_bitmap_set_bits_in_run() instead. +*/ +int __ntfs_bitmap_set_bits_in_run (struct inode *vi,const s64 start_bit, +const s64 count,const u8 value,const bool is_rollback) +{ +s64 cnt = count; +pgoff_t index, end_index; +struct address_space *mapping; +struct folio *folio; +u8 *kaddr; +int pos, len; +u8 bit; +struct ntfs_inode *ni =NTFS_I (vi); +struct ntfs_volume *vol = ni->vol; + +BUG_ON (!vi); +ntfs_debug ("Entering for i_ino 0x%lx, start_bit 0x%llx, count 0x%llx, value %u.%s" , + vi->i_ino, (unsigned long long )start_bit, + (unsigned long long )cnt, (unsigned int )value, + is_rollback ?" (rollback)" :"" ); +BUG_ON (start_bit <0 ); +BUG_ON (cnt <0 ); +BUG_ON (value >1 ); +/* +* Calculate the indices for the pages containing the first and last +* bits, i.e. @start_bit and @start_bit + @cnt - 1, respectively. +*/ + index = start_bit >> (3 +PAGE_SHIFT ); + end_index = (start_bit + cnt -1 ) >> (3 +PAGE_SHIFT ); + +/* Get the page containing the first bit (@start_bit). */ + mapping = vi->i_mapping; + folio =ntfs_read_mapping_folio (mapping, index); +if (IS_ERR (folio)) { +if (!is_rollback) +ntfs_error (vi->i_sb, +"Failed to map first page (error %li), aborting." , +PTR_ERR (folio)); +return PTR_ERR (folio); + } + +folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); + +/* Set @pos to the position of the byte containing @start_bit. */ + pos = (start_bit >>3 ) & ~PAGE_MASK ; + +/* Calculate the position of @start_bit in the first byte. */ + bit = start_bit &7 ; + +/* If the first byte is partial, modify the appropriate bits in it. */ +if (bit) { +u8 *byte = kaddr + pos; + +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value,min_t (s64,8 - bit, cnt)); +while ((bit &7 ) && cnt) { + cnt--; +if (value) + *byte |=1 << bit++; +else + *byte &= ~(1 << bit++); + } +/* If we are done, unmap the page and return success. */ +if (!cnt) + goto done; + +/* Update @pos to the new position. */ + pos++; + } +/* +* Depending on @value, modify all remaining whole bytes in the page up +* to @cnt. +*/ + len =min_t (s64, cnt >>3 ,PAGE_SIZE - pos); +memset (kaddr + pos, value ?0xff :0 , len); + cnt -= len <<3 ; +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value, len <<3 ); + +/* Update @len to point to the first not-done byte in the page. */ +if (cnt <8 ) + len += pos; + +/* If we are not in the last page, deal with all subsequent pages. */ +while (index < end_index) { +BUG_ON (cnt <=0 ); + +/* Update @index and get the next folio. */ +flush_dcache_folio (folio); +folio_mark_dirty (folio); +folio_unlock (folio); +ntfs_unmap_folio (folio, kaddr); + folio =ntfs_read_mapping_folio (mapping, ++index); +if (IS_ERR (folio)) { +ntfs_error (vi->i_sb, +"Failed to map subsequent page (error %li), aborting." , +PTR_ERR (folio)); + goto rollback; + } + +folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); +/* +* Depending on @value, modify all remaining whole bytes in the +* page up to @cnt. +*/ + len =min_t (s64, cnt >>3 ,PAGE_SIZE ); +memset (kaddr, value ?0xff :0 , len); + cnt -= len <<3 ; +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value, len <<3 ); + } +/* +* The currently mapped page is the last one. If the last byte is +* partial, modify the appropriate bits in it. Note, @len is the +* position of the last byte inside the page. +*/ +if (cnt) { +u8 *byte; + +BUG_ON (cnt >7 ); + + bit = cnt; + byte = kaddr + len; +if (ni->mft_no == FILE_Bitmap) +ntfs_set_lcn_empty_bits (vol, index, value, bit); +while (bit--) { +if (value) + *byte |=1 << bit; +else + *byte &= ~(1 << bit); + } + } +done: +/* We are done. Unmap the folio and return success. */ +flush_dcache_folio (folio); +folio_mark_dirty (folio); +folio_unlock (folio); +ntfs_unmap_folio (folio, kaddr); +ntfs_debug ("Done." ); +return 0 ; +rollback: +/* +* Current state: +* - no pages are mapped +* - @count - @cnt is the number of bits that have been modified +*/ +if (is_rollback) +return PTR_ERR (folio); +if (count != cnt) + pos =__ntfs_bitmap_set_bits_in_run (vi, start_bit, count - cnt, + value ?0 :1 , true); +else + pos =0 ; +if (!pos) { +/* Rollback was successful. */ +ntfs_error (vi->i_sb, +"Failed to map subsequent page (error %li), aborting." , +PTR_ERR (folio)); + }else { +/* Rollback failed. */ +ntfs_error (vi->i_sb, +"Failed to map subsequent page (error %li) and rollback failed (error %i). Aborting and leaving inconsistent metadata. Unmount and run chkdsk." , +PTR_ERR (folio), pos); +NVolSetErrors (NTFS_SB (vi->i_sb)); + } +return PTR_ERR (folio); +}
fs/ntfsplus/lcnalloc.c new file +993 -0
993 lines not on this page · raw message
fs/ntfsplus/runlist.c new file +1995 -0
1,814 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH 9/11 ntfsplus: add reparse and ea operations
In reply to Namjae JeonThis adds the implementation of reparse and ea operations for ntfsplus.
--- fs/ntfsplus/ea.c | 712 ++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/reparse.c | 550 ++++++++++++++++++++++++++++++++ 2 files changed, 1262 insertions(+) create mode 100644 fs/ntfsplus/ea.c create mode 100644 fs/ntfsplus/reparse.c
fs/ntfsplus/ea.c new file +712 -0
@@ -0,0 +1,712 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** +* Pocessing of EA's +* +* Part of this file is based on code from the NTFS-3G project. +* +* 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" +#include "misc.h" + +static int ntfs_write_ea (struct ntfs_inode *ni,int type,char *value,s64 ea_off, +s64 ea_size) +{ +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 +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; +unsigned short int ea_value_len, ea_info_qlen; +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 )) { +ntfs_free (p_ea_info); +return -ENODATA ; + } + + ea_info_qlen =le16_to_cpu (p_ea_info->ea_query_length); +ntfs_free (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) { +ntfs_free (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); +ntfs_free (ea_buf); +return ea_value_len; + } + + err = -ENODATA ; +free_ea_buf: +ntfs_free (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; +unsigned short int ea_info_qsize; +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 ; +ntfs_free (p_ea_info); + goto create_ea_info; + } + + ea_info_qsize =le16_to_cpu (p_ea_info->ea_query_length); + }else { +create_ea_info: + p_ea_info =ntfs_malloc_nofs (sizeof (struct ea_information )); +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; +memset (ea_buf + ea_info_qsize,0 , ea_size); + p_ea_info->ea_query_length =cpu_to_le16 (ea_info_qsize); + + err =ntfs_write_ea (ni,AT_EA_INFORMATION , (char *)p_ea_info,0 , +sizeof (struct ea_information )); +if (err) + goto out; + + err =ntfs_write_ea (ni,AT_EA , ea_buf,0 , all_ea_size); +if (err) + goto out; + +if ((flags &XATTR_REPLACE ) && !val_size) { +/* Remove xattr. */ + goto out; + } + }else { +if (flags &XATTR_REPLACE ) { + err = -ENODATA ; + goto out; + } + } +ntfs_free (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); +if (err) + goto out; + } + + err =ntfs_write_ea (ni,AT_EA_INFORMATION , (char *)p_ea_info,0 , +sizeof (struct ea_information )); +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); + +ntfs_free (ea_buf); +ntfs_free (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; +unsigned int 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 =le16_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); +ntfs_free (ea_info); +ntfs_free (ea_buf); + +return err ? err : ret; +} + +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; + +mutex_lock (&ni->mrec_lock); + err =ntfs_get_ea (inode, name,strlen (name), buffer, size); +mutex_unlock (&ni->mrec_lock); + +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; + +mutex_lock (&ni->mrec_lock); + err =ntfs_set_ea (inode, name,strlen (name), value, size, flags,NULL ); +mutex_unlock (&ni->mrec_lock); + +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_NTFSPLUS_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) { +set_cached_acl (inode, type, acl); + inode->i_mode = mode; +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/ntfsplus/reparse.c new file +550 -0
550 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 10/11 ntfsplus: add misc operations
In reply to Namjae JeonThis adds the implementation of misc operations for ntfsplus.
--- fs/ntfsplus/collate.c | 173 ++++++++++ fs/ntfsplus/logfile.c | 773 ++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/misc.c | 221 ++++++++++++ fs/ntfsplus/unistr.c | 471 +++++++++++++++++++++++++ fs/ntfsplus/upcase.c | 73 ++++ 5 files changed, 1711 insertions(+) create mode 100644 fs/ntfsplus/collate.c create mode 100644 fs/ntfsplus/logfile.c create mode 100644 fs/ntfsplus/misc.c create mode 100644 fs/ntfsplus/unistr.c create mode 100644 fs/ntfsplus/upcase.c
fs/ntfsplus/collate.c new file +173 -0
@@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* NTFS kernel collation handling. Part of the Linux-NTFS project. +* +* Copyright (c) 2004 Anton Altaparmakov +* +* Part of this file is based on code from the NTFS-3G project. +* and is copyrighted by the respective authors below: +* Copyright (c) 2004 Anton Altaparmakov +* Copyright (c) 2005 Yura Pakhuchiy +*/ + +#include "collate.h" +#include "misc.h" +#include "ntfs.h" + +static int ntfs_collate_binary (struct ntfs_volume *vol, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int rc; + +ntfs_debug ("Entering." ); + rc =memcmp (data1, data2,min (data1_len, data2_len)); +if (!rc && (data1_len != data2_len)) { +if (data1_len < data2_len) + rc =-1 ; +else + rc =1 ; + } +ntfs_debug ("Done, returning %i" , rc); +return rc; +} + +static int ntfs_collate_ntofs_ulong (struct ntfs_volume *vol, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int rc; +u32 d1, d2; + +ntfs_debug ("Entering." ); +BUG_ON (data1_len != data2_len); +BUG_ON (data1_len !=4 ); + d1 =le32_to_cpup (data1); + d2 =le32_to_cpup (data2); +if (d1 < d2) + rc =-1 ; +else { +if (d1 == d2) + rc =0 ; +else + rc =1 ; + } +ntfs_debug ("Done, returning %i" , rc); +return rc; +} + +/** +* ntfs_collate_ntofs_ulongs - Which of two le32 arrays should be listed first +* +* Returns: -1, 0 or 1 depending of how the arrays compare +*/ +static int ntfs_collate_ntofs_ulongs (struct ntfs_volume *vol, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int rc; +int len; +const __le32 *p1, *p2; +u32 d1, d2; + +ntfs_debug ("Entering." ); +if ((data1_len != data2_len) || (data1_len <=0 ) || (data1_len &3 )) { +ntfs_error (vol->sb,"data1_len or data2_len not valid\n" ); +return -1 ; + } + + p1 = (const __le32 *)data1; + p2 = (const __le32 *)data2; + len = data1_len; +do { + d1 =le32_to_cpup (p1); + p1++; + d2 =le32_to_cpup (p2); + p2++; + }while ((d1 == d2) && ((len -=4 ) >0 )); +if (d1 < d2) + rc =-1 ; +else { +if (d1 == d2) + rc =0 ; +else + rc =1 ; + } +ntfs_debug ("Done, returning %i." , rc); +return rc; +} + +/** +* ntfs_collate_file_name - Which of two filenames should be listed first +*/ +static int ntfs_collate_file_name (struct ntfs_volume *vol, +const void *data1,const int __always_unused data1_len, +const void *data2,const int __always_unused data2_len) +{ +int rc; + +ntfs_debug ("Entering.\n" ); + rc =ntfs_file_compare_values (data1, data2,-2 , +IGNORE_CASE , vol->upcase, vol->upcase_len); +if (!rc) + rc =ntfs_file_compare_values (data1, data2, +-2 ,CASE_SENSITIVE , vol->upcase, vol->upcase_len); +ntfs_debug ("Done, returning %i.\n" , rc); +return rc; +} + +typedef int (*ntfs_collate_func_t )(struct ntfs_volume *,const void *,const int , +const void *,const int ); + +static ntfs_collate_func_t ntfs_do_collate0x0[3 ] = { + ntfs_collate_binary, + ntfs_collate_file_name, +NULL /*ntfs_collate_unicode_string*/ , +}; + +static ntfs_collate_func_t ntfs_do_collate0x1[4 ] = { + ntfs_collate_ntofs_ulong, +NULL /*ntfs_collate_ntofs_sid*/ , +NULL /*ntfs_collate_ntofs_security_hash*/ , + ntfs_collate_ntofs_ulongs, +}; + +/** +* ntfs_collate - collate two data items using a specified collation rule +* @vol: ntfs volume to which the data items belong +* @cr: collation rule to use when comparing the items +* @data1: first data item to collate +* @data1_len: length in bytes of @data1 +* @data2: second data item to collate +* @data2_len: length in bytes of @data2 +* +* Collate the two data items @data1 and @data2 using the collation rule @cr +* and return -1, 0, ir 1 if @data1 is found, respectively, to collate before, +* to match, or to collate after @data2. +* +* For speed we use the collation rule @cr as an index into two tables of +* function pointers to call the appropriate collation function. +*/ +int ntfs_collate (struct ntfs_volume *vol,__le32 cr, +const void *data1,const int data1_len, +const void *data2,const int data2_len) +{ +int i; + +ntfs_debug ("Entering." ); + +BUG_ON (cr !=COLLATION_BINARY && cr !=COLLATION_NTOFS_ULONG && + cr !=COLLATION_FILE_NAME && cr !=COLLATION_NTOFS_ULONGS ); + i =le32_to_cpu (cr); +BUG_ON (i <0 ); +if (i <=0x02 ) +return ntfs_do_collate0x0[i](vol, data1, data1_len, + data2, data2_len); +BUG_ON (i <0x10 ); + i -=0x10 ; +if (likely (i <=3 )) +return ntfs_do_collate0x1[i](vol, data1, data1_len, + data2, data2_len); +BUG (); +return 0 ; +}
fs/ntfsplus/logfile.c new file +773 -0
773 lines not on this page · raw message
fs/ntfsplus/misc.c new file +221 -0
221 lines not on this page · raw message
fs/ntfsplus/unistr.c new file +471 -0
471 lines not on this page · raw message
fs/ntfsplus/upcase.c new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 11/11 ntfsplus: add Kconfig and Makefile
In reply to Namjae JeonThis adds the Kconfig and Makefile for ntfsplus.
--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/ntfsplus/Kconfig | 45 ++++++++++++++++++++++++++++++++++++++++++++ fs/ntfsplus/Makefile | 18 ++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 fs/ntfsplus/Kconfig create mode 100644 fs/ntfsplus/Makefile
fs/Kconfig +1 -0
@@ -153,6 +153,7 @@ menu "DOS/FAT/EXFAT/NT Filesystems" source "fs/fat/Kconfig" source "fs/exfat/Kconfig" source "fs/ntfs3/Kconfig" +source "fs/ntfsplus/Kconfig" endmenu endif # BLOCK
fs/Makefile +1 -0
@@ -91,6 +91,7 @@ obj-y += unicode/ obj-$(CONFIG_SMBFS) += smb/ obj-$(CONFIG_HPFS_FS) += hpfs/ obj-$(CONFIG_NTFS3_FS) += ntfs3/ +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus/ obj-$(CONFIG_UFS_FS) += ufs/ obj-$(CONFIG_EFS_FS) += efs/ obj-$(CONFIG_JFFS2_FS) += jffs2/
fs/ntfsplus/Kconfig new file +45 -0
45 lines not on this page · raw message
fs/ntfsplus/Makefile new file +18 -0
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for the ntfsplus filesystem support. +# + +# to check robot warnings +ccflags-y += -Wint-to-pointer-cast \ + $(call cc-option,-Wunused-but-set-variable,-Wunused-const-variable) \ + $(call cc-option,-Wold-style-declaration,-Wout-of-line-declaration) + +obj-$(CONFIG_NTFSPLUS_FS) += ntfsplus.o + +ntfsplus-y := aops.o attrib.o collate.o misc.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 \ + ntfs_iomap.o + +ccflags-$(CONFIG_NTFSPLUS_DEBUG) += -DDEBUG
Signature · 1 line
2.34.1