18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/** 38c2ecf20Sopenharmony_ci * inode.c - NTFS kernel inode handling. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/buffer_head.h> 98c2ecf20Sopenharmony_ci#include <linux/fs.h> 108c2ecf20Sopenharmony_ci#include <linux/mm.h> 118c2ecf20Sopenharmony_ci#include <linux/mount.h> 128c2ecf20Sopenharmony_ci#include <linux/mutex.h> 138c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 148c2ecf20Sopenharmony_ci#include <linux/quotaops.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/log2.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include "aops.h" 198c2ecf20Sopenharmony_ci#include "attrib.h" 208c2ecf20Sopenharmony_ci#include "bitmap.h" 218c2ecf20Sopenharmony_ci#include "dir.h" 228c2ecf20Sopenharmony_ci#include "debug.h" 238c2ecf20Sopenharmony_ci#include "inode.h" 248c2ecf20Sopenharmony_ci#include "lcnalloc.h" 258c2ecf20Sopenharmony_ci#include "malloc.h" 268c2ecf20Sopenharmony_ci#include "mft.h" 278c2ecf20Sopenharmony_ci#include "time.h" 288c2ecf20Sopenharmony_ci#include "ntfs.h" 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/** 318c2ecf20Sopenharmony_ci * ntfs_test_inode - compare two (possibly fake) inodes for equality 328c2ecf20Sopenharmony_ci * @vi: vfs inode which to test 338c2ecf20Sopenharmony_ci * @data: data which is being tested with 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci * Compare the ntfs attribute embedded in the ntfs specific part of the vfs 368c2ecf20Sopenharmony_ci * inode @vi for equality with the ntfs attribute @data. 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * If searching for the normal file/directory inode, set @na->type to AT_UNUSED. 398c2ecf20Sopenharmony_ci * @na->name and @na->name_len are then ignored. 408c2ecf20Sopenharmony_ci * 418c2ecf20Sopenharmony_ci * Return 1 if the attributes match and 0 if not. 428c2ecf20Sopenharmony_ci * 438c2ecf20Sopenharmony_ci * NOTE: This function runs with the inode_hash_lock spin lock held so it is not 448c2ecf20Sopenharmony_ci * allowed to sleep. 458c2ecf20Sopenharmony_ci */ 468c2ecf20Sopenharmony_ciint ntfs_test_inode(struct inode *vi, void *data) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci ntfs_attr *na = (ntfs_attr *)data; 498c2ecf20Sopenharmony_ci ntfs_inode *ni; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci if (vi->i_ino != na->mft_no) 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci ni = NTFS_I(vi); 548c2ecf20Sopenharmony_ci /* If !NInoAttr(ni), @vi is a normal file or directory inode. */ 558c2ecf20Sopenharmony_ci if (likely(!NInoAttr(ni))) { 568c2ecf20Sopenharmony_ci /* If not looking for a normal inode this is a mismatch. */ 578c2ecf20Sopenharmony_ci if (unlikely(na->type != AT_UNUSED)) 588c2ecf20Sopenharmony_ci return 0; 598c2ecf20Sopenharmony_ci } else { 608c2ecf20Sopenharmony_ci /* A fake inode describing an attribute. */ 618c2ecf20Sopenharmony_ci if (ni->type != na->type) 628c2ecf20Sopenharmony_ci return 0; 638c2ecf20Sopenharmony_ci if (ni->name_len != na->name_len) 648c2ecf20Sopenharmony_ci return 0; 658c2ecf20Sopenharmony_ci if (na->name_len && memcmp(ni->name, na->name, 668c2ecf20Sopenharmony_ci na->name_len * sizeof(ntfschar))) 678c2ecf20Sopenharmony_ci return 0; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci /* Match! */ 708c2ecf20Sopenharmony_ci return 1; 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/** 748c2ecf20Sopenharmony_ci * ntfs_init_locked_inode - initialize an inode 758c2ecf20Sopenharmony_ci * @vi: vfs inode to initialize 768c2ecf20Sopenharmony_ci * @data: data which to initialize @vi to 778c2ecf20Sopenharmony_ci * 788c2ecf20Sopenharmony_ci * Initialize the vfs inode @vi with the values from the ntfs attribute @data in 798c2ecf20Sopenharmony_ci * order to enable ntfs_test_inode() to do its work. 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * If initializing the normal file/directory inode, set @na->type to AT_UNUSED. 828c2ecf20Sopenharmony_ci * In that case, @na->name and @na->name_len should be set to NULL and 0, 838c2ecf20Sopenharmony_ci * respectively. Although that is not strictly necessary as 848c2ecf20Sopenharmony_ci * ntfs_read_locked_inode() will fill them in later. 858c2ecf20Sopenharmony_ci * 868c2ecf20Sopenharmony_ci * Return 0 on success and -errno on error. 878c2ecf20Sopenharmony_ci * 888c2ecf20Sopenharmony_ci * NOTE: This function runs with the inode->i_lock spin lock held so it is not 898c2ecf20Sopenharmony_ci * allowed to sleep. (Hence the GFP_ATOMIC allocation.) 908c2ecf20Sopenharmony_ci */ 918c2ecf20Sopenharmony_cistatic int ntfs_init_locked_inode(struct inode *vi, void *data) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci ntfs_attr *na = (ntfs_attr *)data; 948c2ecf20Sopenharmony_ci ntfs_inode *ni = NTFS_I(vi); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci vi->i_ino = na->mft_no; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci ni->type = na->type; 998c2ecf20Sopenharmony_ci if (na->type == AT_INDEX_ALLOCATION) 1008c2ecf20Sopenharmony_ci NInoSetMstProtected(ni); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci ni->name = na->name; 1038c2ecf20Sopenharmony_ci ni->name_len = na->name_len; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* If initializing a normal inode, we are done. */ 1068c2ecf20Sopenharmony_ci if (likely(na->type == AT_UNUSED)) { 1078c2ecf20Sopenharmony_ci BUG_ON(na->name); 1088c2ecf20Sopenharmony_ci BUG_ON(na->name_len); 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci /* It is a fake inode. */ 1138c2ecf20Sopenharmony_ci NInoSetAttr(ni); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* 1168c2ecf20Sopenharmony_ci * We have I30 global constant as an optimization as it is the name 1178c2ecf20Sopenharmony_ci * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC 1188c2ecf20Sopenharmony_ci * allocation but that is ok. And most attributes are unnamed anyway, 1198c2ecf20Sopenharmony_ci * thus the fraction of named attributes with name != I30 is actually 1208c2ecf20Sopenharmony_ci * absolutely tiny. 1218c2ecf20Sopenharmony_ci */ 1228c2ecf20Sopenharmony_ci if (na->name_len && na->name != I30) { 1238c2ecf20Sopenharmony_ci unsigned int i; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci BUG_ON(!na->name); 1268c2ecf20Sopenharmony_ci i = na->name_len * sizeof(ntfschar); 1278c2ecf20Sopenharmony_ci ni->name = kmalloc(i + sizeof(ntfschar), GFP_ATOMIC); 1288c2ecf20Sopenharmony_ci if (!ni->name) 1298c2ecf20Sopenharmony_ci return -ENOMEM; 1308c2ecf20Sopenharmony_ci memcpy(ni->name, na->name, i); 1318c2ecf20Sopenharmony_ci ni->name[na->name_len] = 0; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci return 0; 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic int ntfs_read_locked_inode(struct inode *vi); 1378c2ecf20Sopenharmony_cistatic int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi); 1388c2ecf20Sopenharmony_cistatic int ntfs_read_locked_index_inode(struct inode *base_vi, 1398c2ecf20Sopenharmony_ci struct inode *vi); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/** 1428c2ecf20Sopenharmony_ci * ntfs_iget - obtain a struct inode corresponding to a specific normal inode 1438c2ecf20Sopenharmony_ci * @sb: super block of mounted volume 1448c2ecf20Sopenharmony_ci * @mft_no: mft record number / inode number to obtain 1458c2ecf20Sopenharmony_ci * 1468c2ecf20Sopenharmony_ci * Obtain the struct inode corresponding to a specific normal inode (i.e. a 1478c2ecf20Sopenharmony_ci * file or directory). 1488c2ecf20Sopenharmony_ci * 1498c2ecf20Sopenharmony_ci * If the inode is in the cache, it is just returned with an increased 1508c2ecf20Sopenharmony_ci * reference count. Otherwise, a new struct inode is allocated and initialized, 1518c2ecf20Sopenharmony_ci * and finally ntfs_read_locked_inode() is called to read in the inode and 1528c2ecf20Sopenharmony_ci * fill in the remainder of the inode structure. 1538c2ecf20Sopenharmony_ci * 1548c2ecf20Sopenharmony_ci * Return the struct inode on success. Check the return value with IS_ERR() and 1558c2ecf20Sopenharmony_ci * if true, the function failed and the error code is obtained from PTR_ERR(). 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_cistruct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci struct inode *vi; 1608c2ecf20Sopenharmony_ci int err; 1618c2ecf20Sopenharmony_ci ntfs_attr na; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci na.mft_no = mft_no; 1648c2ecf20Sopenharmony_ci na.type = AT_UNUSED; 1658c2ecf20Sopenharmony_ci na.name = NULL; 1668c2ecf20Sopenharmony_ci na.name_len = 0; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci vi = iget5_locked(sb, mft_no, ntfs_test_inode, 1698c2ecf20Sopenharmony_ci ntfs_init_locked_inode, &na); 1708c2ecf20Sopenharmony_ci if (unlikely(!vi)) 1718c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci err = 0; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci /* If this is a freshly allocated inode, need to read it now. */ 1768c2ecf20Sopenharmony_ci if (vi->i_state & I_NEW) { 1778c2ecf20Sopenharmony_ci err = ntfs_read_locked_inode(vi); 1788c2ecf20Sopenharmony_ci unlock_new_inode(vi); 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci /* 1818c2ecf20Sopenharmony_ci * There is no point in keeping bad inodes around if the failure was 1828c2ecf20Sopenharmony_ci * due to ENOMEM. We want to be able to retry again later. 1838c2ecf20Sopenharmony_ci */ 1848c2ecf20Sopenharmony_ci if (unlikely(err == -ENOMEM)) { 1858c2ecf20Sopenharmony_ci iput(vi); 1868c2ecf20Sopenharmony_ci vi = ERR_PTR(err); 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci return vi; 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci/** 1928c2ecf20Sopenharmony_ci * ntfs_attr_iget - obtain a struct inode corresponding to an attribute 1938c2ecf20Sopenharmony_ci * @base_vi: vfs base inode containing the attribute 1948c2ecf20Sopenharmony_ci * @type: attribute type 1958c2ecf20Sopenharmony_ci * @name: Unicode name of the attribute (NULL if unnamed) 1968c2ecf20Sopenharmony_ci * @name_len: length of @name in Unicode characters (0 if unnamed) 1978c2ecf20Sopenharmony_ci * 1988c2ecf20Sopenharmony_ci * Obtain the (fake) struct inode corresponding to the attribute specified by 1998c2ecf20Sopenharmony_ci * @type, @name, and @name_len, which is present in the base mft record 2008c2ecf20Sopenharmony_ci * specified by the vfs inode @base_vi. 2018c2ecf20Sopenharmony_ci * 2028c2ecf20Sopenharmony_ci * If the attribute inode is in the cache, it is just returned with an 2038c2ecf20Sopenharmony_ci * increased reference count. Otherwise, a new struct inode is allocated and 2048c2ecf20Sopenharmony_ci * initialized, and finally ntfs_read_locked_attr_inode() is called to read the 2058c2ecf20Sopenharmony_ci * attribute and fill in the inode structure. 2068c2ecf20Sopenharmony_ci * 2078c2ecf20Sopenharmony_ci * Note, for index allocation attributes, you need to use ntfs_index_iget() 2088c2ecf20Sopenharmony_ci * instead of ntfs_attr_iget() as working with indices is a lot more complex. 2098c2ecf20Sopenharmony_ci * 2108c2ecf20Sopenharmony_ci * Return the struct inode of the attribute inode on success. Check the return 2118c2ecf20Sopenharmony_ci * value with IS_ERR() and if true, the function failed and the error code is 2128c2ecf20Sopenharmony_ci * obtained from PTR_ERR(). 2138c2ecf20Sopenharmony_ci */ 2148c2ecf20Sopenharmony_cistruct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type, 2158c2ecf20Sopenharmony_ci ntfschar *name, u32 name_len) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci struct inode *vi; 2188c2ecf20Sopenharmony_ci int err; 2198c2ecf20Sopenharmony_ci ntfs_attr na; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* Make sure no one calls ntfs_attr_iget() for indices. */ 2228c2ecf20Sopenharmony_ci BUG_ON(type == AT_INDEX_ALLOCATION); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci na.mft_no = base_vi->i_ino; 2258c2ecf20Sopenharmony_ci na.type = type; 2268c2ecf20Sopenharmony_ci na.name = name; 2278c2ecf20Sopenharmony_ci na.name_len = name_len; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode, 2308c2ecf20Sopenharmony_ci ntfs_init_locked_inode, &na); 2318c2ecf20Sopenharmony_ci if (unlikely(!vi)) 2328c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci err = 0; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* If this is a freshly allocated inode, need to read it now. */ 2378c2ecf20Sopenharmony_ci if (vi->i_state & I_NEW) { 2388c2ecf20Sopenharmony_ci err = ntfs_read_locked_attr_inode(base_vi, vi); 2398c2ecf20Sopenharmony_ci unlock_new_inode(vi); 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci /* 2428c2ecf20Sopenharmony_ci * There is no point in keeping bad attribute inodes around. This also 2438c2ecf20Sopenharmony_ci * simplifies things in that we never need to check for bad attribute 2448c2ecf20Sopenharmony_ci * inodes elsewhere. 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_ci if (unlikely(err)) { 2478c2ecf20Sopenharmony_ci iput(vi); 2488c2ecf20Sopenharmony_ci vi = ERR_PTR(err); 2498c2ecf20Sopenharmony_ci } 2508c2ecf20Sopenharmony_ci return vi; 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/** 2548c2ecf20Sopenharmony_ci * ntfs_index_iget - obtain a struct inode corresponding to an index 2558c2ecf20Sopenharmony_ci * @base_vi: vfs base inode containing the index related attributes 2568c2ecf20Sopenharmony_ci * @name: Unicode name of the index 2578c2ecf20Sopenharmony_ci * @name_len: length of @name in Unicode characters 2588c2ecf20Sopenharmony_ci * 2598c2ecf20Sopenharmony_ci * Obtain the (fake) struct inode corresponding to the index specified by @name 2608c2ecf20Sopenharmony_ci * and @name_len, which is present in the base mft record specified by the vfs 2618c2ecf20Sopenharmony_ci * inode @base_vi. 2628c2ecf20Sopenharmony_ci * 2638c2ecf20Sopenharmony_ci * If the index inode is in the cache, it is just returned with an increased 2648c2ecf20Sopenharmony_ci * reference count. Otherwise, a new struct inode is allocated and 2658c2ecf20Sopenharmony_ci * initialized, and finally ntfs_read_locked_index_inode() is called to read 2668c2ecf20Sopenharmony_ci * the index related attributes and fill in the inode structure. 2678c2ecf20Sopenharmony_ci * 2688c2ecf20Sopenharmony_ci * Return the struct inode of the index inode on success. Check the return 2698c2ecf20Sopenharmony_ci * value with IS_ERR() and if true, the function failed and the error code is 2708c2ecf20Sopenharmony_ci * obtained from PTR_ERR(). 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_cistruct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name, 2738c2ecf20Sopenharmony_ci u32 name_len) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci struct inode *vi; 2768c2ecf20Sopenharmony_ci int err; 2778c2ecf20Sopenharmony_ci ntfs_attr na; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci na.mft_no = base_vi->i_ino; 2808c2ecf20Sopenharmony_ci na.type = AT_INDEX_ALLOCATION; 2818c2ecf20Sopenharmony_ci na.name = name; 2828c2ecf20Sopenharmony_ci na.name_len = name_len; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode, 2858c2ecf20Sopenharmony_ci ntfs_init_locked_inode, &na); 2868c2ecf20Sopenharmony_ci if (unlikely(!vi)) 2878c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci err = 0; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci /* If this is a freshly allocated inode, need to read it now. */ 2928c2ecf20Sopenharmony_ci if (vi->i_state & I_NEW) { 2938c2ecf20Sopenharmony_ci err = ntfs_read_locked_index_inode(base_vi, vi); 2948c2ecf20Sopenharmony_ci unlock_new_inode(vi); 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci /* 2978c2ecf20Sopenharmony_ci * There is no point in keeping bad index inodes around. This also 2988c2ecf20Sopenharmony_ci * simplifies things in that we never need to check for bad index 2998c2ecf20Sopenharmony_ci * inodes elsewhere. 3008c2ecf20Sopenharmony_ci */ 3018c2ecf20Sopenharmony_ci if (unlikely(err)) { 3028c2ecf20Sopenharmony_ci iput(vi); 3038c2ecf20Sopenharmony_ci vi = ERR_PTR(err); 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci return vi; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistruct inode *ntfs_alloc_big_inode(struct super_block *sb) 3098c2ecf20Sopenharmony_ci{ 3108c2ecf20Sopenharmony_ci ntfs_inode *ni; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci ntfs_debug("Entering."); 3138c2ecf20Sopenharmony_ci ni = kmem_cache_alloc(ntfs_big_inode_cache, GFP_NOFS); 3148c2ecf20Sopenharmony_ci if (likely(ni != NULL)) { 3158c2ecf20Sopenharmony_ci ni->state = 0; 3168c2ecf20Sopenharmony_ci return VFS_I(ni); 3178c2ecf20Sopenharmony_ci } 3188c2ecf20Sopenharmony_ci ntfs_error(sb, "Allocation of NTFS big inode structure failed."); 3198c2ecf20Sopenharmony_ci return NULL; 3208c2ecf20Sopenharmony_ci} 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_civoid ntfs_free_big_inode(struct inode *inode) 3238c2ecf20Sopenharmony_ci{ 3248c2ecf20Sopenharmony_ci kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode)); 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic inline ntfs_inode *ntfs_alloc_extent_inode(void) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci ntfs_inode *ni; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci ntfs_debug("Entering."); 3328c2ecf20Sopenharmony_ci ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS); 3338c2ecf20Sopenharmony_ci if (likely(ni != NULL)) { 3348c2ecf20Sopenharmony_ci ni->state = 0; 3358c2ecf20Sopenharmony_ci return ni; 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci ntfs_error(NULL, "Allocation of NTFS inode structure failed."); 3388c2ecf20Sopenharmony_ci return NULL; 3398c2ecf20Sopenharmony_ci} 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_cistatic void ntfs_destroy_extent_inode(ntfs_inode *ni) 3428c2ecf20Sopenharmony_ci{ 3438c2ecf20Sopenharmony_ci ntfs_debug("Entering."); 3448c2ecf20Sopenharmony_ci BUG_ON(ni->page); 3458c2ecf20Sopenharmony_ci if (!atomic_dec_and_test(&ni->count)) 3468c2ecf20Sopenharmony_ci BUG(); 3478c2ecf20Sopenharmony_ci kmem_cache_free(ntfs_inode_cache, ni); 3488c2ecf20Sopenharmony_ci} 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci/* 3518c2ecf20Sopenharmony_ci * The attribute runlist lock has separate locking rules from the 3528c2ecf20Sopenharmony_ci * normal runlist lock, so split the two lock-classes: 3538c2ecf20Sopenharmony_ci */ 3548c2ecf20Sopenharmony_cistatic struct lock_class_key attr_list_rl_lock_class; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci/** 3578c2ecf20Sopenharmony_ci * __ntfs_init_inode - initialize ntfs specific part of an inode 3588c2ecf20Sopenharmony_ci * @sb: super block of mounted volume 3598c2ecf20Sopenharmony_ci * @ni: freshly allocated ntfs inode which to initialize 3608c2ecf20Sopenharmony_ci * 3618c2ecf20Sopenharmony_ci * Initialize an ntfs inode to defaults. 3628c2ecf20Sopenharmony_ci * 3638c2ecf20Sopenharmony_ci * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left 3648c2ecf20Sopenharmony_ci * untouched. Make sure to initialize them elsewhere. 3658c2ecf20Sopenharmony_ci * 3668c2ecf20Sopenharmony_ci * Return zero on success and -ENOMEM on error. 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_civoid __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni) 3698c2ecf20Sopenharmony_ci{ 3708c2ecf20Sopenharmony_ci ntfs_debug("Entering."); 3718c2ecf20Sopenharmony_ci rwlock_init(&ni->size_lock); 3728c2ecf20Sopenharmony_ci ni->initialized_size = ni->allocated_size = 0; 3738c2ecf20Sopenharmony_ci ni->seq_no = 0; 3748c2ecf20Sopenharmony_ci atomic_set(&ni->count, 1); 3758c2ecf20Sopenharmony_ci ni->vol = NTFS_SB(sb); 3768c2ecf20Sopenharmony_ci ntfs_init_runlist(&ni->runlist); 3778c2ecf20Sopenharmony_ci mutex_init(&ni->mrec_lock); 3788c2ecf20Sopenharmony_ci ni->page = NULL; 3798c2ecf20Sopenharmony_ci ni->page_ofs = 0; 3808c2ecf20Sopenharmony_ci ni->attr_list_size = 0; 3818c2ecf20Sopenharmony_ci ni->attr_list = NULL; 3828c2ecf20Sopenharmony_ci ntfs_init_runlist(&ni->attr_list_rl); 3838c2ecf20Sopenharmony_ci lockdep_set_class(&ni->attr_list_rl.lock, 3848c2ecf20Sopenharmony_ci &attr_list_rl_lock_class); 3858c2ecf20Sopenharmony_ci ni->itype.index.block_size = 0; 3868c2ecf20Sopenharmony_ci ni->itype.index.vcn_size = 0; 3878c2ecf20Sopenharmony_ci ni->itype.index.collation_rule = 0; 3888c2ecf20Sopenharmony_ci ni->itype.index.block_size_bits = 0; 3898c2ecf20Sopenharmony_ci ni->itype.index.vcn_size_bits = 0; 3908c2ecf20Sopenharmony_ci mutex_init(&ni->extent_lock); 3918c2ecf20Sopenharmony_ci ni->nr_extents = 0; 3928c2ecf20Sopenharmony_ci ni->ext.base_ntfs_ino = NULL; 3938c2ecf20Sopenharmony_ci} 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci/* 3968c2ecf20Sopenharmony_ci * Extent inodes get MFT-mapped in a nested way, while the base inode 3978c2ecf20Sopenharmony_ci * is still mapped. Teach this nesting to the lock validator by creating 3988c2ecf20Sopenharmony_ci * a separate class for nested inode's mrec_lock's: 3998c2ecf20Sopenharmony_ci */ 4008c2ecf20Sopenharmony_cistatic struct lock_class_key extent_inode_mrec_lock_key; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ciinline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, 4038c2ecf20Sopenharmony_ci unsigned long mft_no) 4048c2ecf20Sopenharmony_ci{ 4058c2ecf20Sopenharmony_ci ntfs_inode *ni = ntfs_alloc_extent_inode(); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci ntfs_debug("Entering."); 4088c2ecf20Sopenharmony_ci if (likely(ni != NULL)) { 4098c2ecf20Sopenharmony_ci __ntfs_init_inode(sb, ni); 4108c2ecf20Sopenharmony_ci lockdep_set_class(&ni->mrec_lock, &extent_inode_mrec_lock_key); 4118c2ecf20Sopenharmony_ci ni->mft_no = mft_no; 4128c2ecf20Sopenharmony_ci ni->type = AT_UNUSED; 4138c2ecf20Sopenharmony_ci ni->name = NULL; 4148c2ecf20Sopenharmony_ci ni->name_len = 0; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci return ni; 4178c2ecf20Sopenharmony_ci} 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci/** 4208c2ecf20Sopenharmony_ci * ntfs_is_extended_system_file - check if a file is in the $Extend directory 4218c2ecf20Sopenharmony_ci * @ctx: initialized attribute search context 4228c2ecf20Sopenharmony_ci * 4238c2ecf20Sopenharmony_ci * Search all file name attributes in the inode described by the attribute 4248c2ecf20Sopenharmony_ci * search context @ctx and check if any of the names are in the $Extend system 4258c2ecf20Sopenharmony_ci * directory. 4268c2ecf20Sopenharmony_ci * 4278c2ecf20Sopenharmony_ci * Return values: 4288c2ecf20Sopenharmony_ci * 1: file is in $Extend directory 4298c2ecf20Sopenharmony_ci * 0: file is not in $Extend directory 4308c2ecf20Sopenharmony_ci * -errno: failed to determine if the file is in the $Extend directory 4318c2ecf20Sopenharmony_ci */ 4328c2ecf20Sopenharmony_cistatic int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx) 4338c2ecf20Sopenharmony_ci{ 4348c2ecf20Sopenharmony_ci int nr_links, err; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci /* Restart search. */ 4378c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* Get number of hard links. */ 4408c2ecf20Sopenharmony_ci nr_links = le16_to_cpu(ctx->mrec->link_count); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci /* Loop through all hard links. */ 4438c2ecf20Sopenharmony_ci while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, 4448c2ecf20Sopenharmony_ci ctx))) { 4458c2ecf20Sopenharmony_ci FILE_NAME_ATTR *file_name_attr; 4468c2ecf20Sopenharmony_ci ATTR_RECORD *attr = ctx->attr; 4478c2ecf20Sopenharmony_ci u8 *p, *p2; 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci nr_links--; 4508c2ecf20Sopenharmony_ci /* 4518c2ecf20Sopenharmony_ci * Maximum sanity checking as we are called on an inode that 4528c2ecf20Sopenharmony_ci * we suspect might be corrupt. 4538c2ecf20Sopenharmony_ci */ 4548c2ecf20Sopenharmony_ci p = (u8*)attr + le32_to_cpu(attr->length); 4558c2ecf20Sopenharmony_ci if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec + 4568c2ecf20Sopenharmony_ci le32_to_cpu(ctx->mrec->bytes_in_use)) { 4578c2ecf20Sopenharmony_cierr_corrupt_attr: 4588c2ecf20Sopenharmony_ci ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name " 4598c2ecf20Sopenharmony_ci "attribute. You should run chkdsk."); 4608c2ecf20Sopenharmony_ci return -EIO; 4618c2ecf20Sopenharmony_ci } 4628c2ecf20Sopenharmony_ci if (attr->non_resident) { 4638c2ecf20Sopenharmony_ci ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file " 4648c2ecf20Sopenharmony_ci "name. You should run chkdsk."); 4658c2ecf20Sopenharmony_ci return -EIO; 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci if (attr->flags) { 4688c2ecf20Sopenharmony_ci ntfs_error(ctx->ntfs_ino->vol->sb, "File name with " 4698c2ecf20Sopenharmony_ci "invalid flags. You should run " 4708c2ecf20Sopenharmony_ci "chkdsk."); 4718c2ecf20Sopenharmony_ci return -EIO; 4728c2ecf20Sopenharmony_ci } 4738c2ecf20Sopenharmony_ci if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) { 4748c2ecf20Sopenharmony_ci ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file " 4758c2ecf20Sopenharmony_ci "name. You should run chkdsk."); 4768c2ecf20Sopenharmony_ci return -EIO; 4778c2ecf20Sopenharmony_ci } 4788c2ecf20Sopenharmony_ci file_name_attr = (FILE_NAME_ATTR*)((u8*)attr + 4798c2ecf20Sopenharmony_ci le16_to_cpu(attr->data.resident.value_offset)); 4808c2ecf20Sopenharmony_ci p2 = (u8 *)file_name_attr + le32_to_cpu(attr->data.resident.value_length); 4818c2ecf20Sopenharmony_ci if (p2 < (u8*)attr || p2 > p) 4828c2ecf20Sopenharmony_ci goto err_corrupt_attr; 4838c2ecf20Sopenharmony_ci /* This attribute is ok, but is it in the $Extend directory? */ 4848c2ecf20Sopenharmony_ci if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend) 4858c2ecf20Sopenharmony_ci return 1; /* YES, it's an extended system file. */ 4868c2ecf20Sopenharmony_ci } 4878c2ecf20Sopenharmony_ci if (unlikely(err != -ENOENT)) 4888c2ecf20Sopenharmony_ci return err; 4898c2ecf20Sopenharmony_ci if (unlikely(nr_links)) { 4908c2ecf20Sopenharmony_ci ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count " 4918c2ecf20Sopenharmony_ci "doesn't match number of name attributes. You " 4928c2ecf20Sopenharmony_ci "should run chkdsk."); 4938c2ecf20Sopenharmony_ci return -EIO; 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci return 0; /* NO, it is not an extended system file. */ 4968c2ecf20Sopenharmony_ci} 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci/** 4998c2ecf20Sopenharmony_ci * ntfs_read_locked_inode - read an inode from its device 5008c2ecf20Sopenharmony_ci * @vi: inode to read 5018c2ecf20Sopenharmony_ci * 5028c2ecf20Sopenharmony_ci * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode 5038c2ecf20Sopenharmony_ci * described by @vi into memory from the device. 5048c2ecf20Sopenharmony_ci * 5058c2ecf20Sopenharmony_ci * The only fields in @vi that we need to/can look at when the function is 5068c2ecf20Sopenharmony_ci * called are i_sb, pointing to the mounted device's super block, and i_ino, 5078c2ecf20Sopenharmony_ci * the number of the inode to load. 5088c2ecf20Sopenharmony_ci * 5098c2ecf20Sopenharmony_ci * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino 5108c2ecf20Sopenharmony_ci * for reading and sets up the necessary @vi fields as well as initializing 5118c2ecf20Sopenharmony_ci * the ntfs inode. 5128c2ecf20Sopenharmony_ci * 5138c2ecf20Sopenharmony_ci * Q: What locks are held when the function is called? 5148c2ecf20Sopenharmony_ci * A: i_state has I_NEW set, hence the inode is locked, also 5158c2ecf20Sopenharmony_ci * i_count is set to 1, so it is not going to go away 5168c2ecf20Sopenharmony_ci * i_flags is set to 0 and we have no business touching it. Only an ioctl() 5178c2ecf20Sopenharmony_ci * is allowed to write to them. We should of course be honouring them but 5188c2ecf20Sopenharmony_ci * we need to do that using the IS_* macros defined in include/linux/fs.h. 5198c2ecf20Sopenharmony_ci * In any case ntfs_read_locked_inode() has nothing to do with i_flags. 5208c2ecf20Sopenharmony_ci * 5218c2ecf20Sopenharmony_ci * Return 0 on success and -errno on error. In the error case, the inode will 5228c2ecf20Sopenharmony_ci * have had make_bad_inode() executed on it. 5238c2ecf20Sopenharmony_ci */ 5248c2ecf20Sopenharmony_cistatic int ntfs_read_locked_inode(struct inode *vi) 5258c2ecf20Sopenharmony_ci{ 5268c2ecf20Sopenharmony_ci ntfs_volume *vol = NTFS_SB(vi->i_sb); 5278c2ecf20Sopenharmony_ci ntfs_inode *ni; 5288c2ecf20Sopenharmony_ci struct inode *bvi; 5298c2ecf20Sopenharmony_ci MFT_RECORD *m; 5308c2ecf20Sopenharmony_ci ATTR_RECORD *a; 5318c2ecf20Sopenharmony_ci STANDARD_INFORMATION *si; 5328c2ecf20Sopenharmony_ci ntfs_attr_search_ctx *ctx; 5338c2ecf20Sopenharmony_ci int err = 0; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci /* Setup the generic vfs inode parts now. */ 5388c2ecf20Sopenharmony_ci vi->i_uid = vol->uid; 5398c2ecf20Sopenharmony_ci vi->i_gid = vol->gid; 5408c2ecf20Sopenharmony_ci vi->i_mode = 0; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci /* 5438c2ecf20Sopenharmony_ci * Initialize the ntfs specific part of @vi special casing 5448c2ecf20Sopenharmony_ci * FILE_MFT which we need to do at mount time. 5458c2ecf20Sopenharmony_ci */ 5468c2ecf20Sopenharmony_ci if (vi->i_ino != FILE_MFT) 5478c2ecf20Sopenharmony_ci ntfs_init_big_inode(vi); 5488c2ecf20Sopenharmony_ci ni = NTFS_I(vi); 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci m = map_mft_record(ni); 5518c2ecf20Sopenharmony_ci if (IS_ERR(m)) { 5528c2ecf20Sopenharmony_ci err = PTR_ERR(m); 5538c2ecf20Sopenharmony_ci goto err_out; 5548c2ecf20Sopenharmony_ci } 5558c2ecf20Sopenharmony_ci ctx = ntfs_attr_get_search_ctx(ni, m); 5568c2ecf20Sopenharmony_ci if (!ctx) { 5578c2ecf20Sopenharmony_ci err = -ENOMEM; 5588c2ecf20Sopenharmony_ci goto unm_err_out; 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci if (!(m->flags & MFT_RECORD_IN_USE)) { 5628c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Inode is not in use!"); 5638c2ecf20Sopenharmony_ci goto unm_err_out; 5648c2ecf20Sopenharmony_ci } 5658c2ecf20Sopenharmony_ci if (m->base_mft_record) { 5668c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Inode is an extent inode!"); 5678c2ecf20Sopenharmony_ci goto unm_err_out; 5688c2ecf20Sopenharmony_ci } 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci /* Transfer information from mft record into vfs and ntfs inodes. */ 5718c2ecf20Sopenharmony_ci vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci /* 5748c2ecf20Sopenharmony_ci * FIXME: Keep in mind that link_count is two for files which have both 5758c2ecf20Sopenharmony_ci * a long file name and a short file name as separate entries, so if 5768c2ecf20Sopenharmony_ci * we are hiding short file names this will be too high. Either we need 5778c2ecf20Sopenharmony_ci * to account for the short file names by subtracting them or we need 5788c2ecf20Sopenharmony_ci * to make sure we delete files even though i_nlink is not zero which 5798c2ecf20Sopenharmony_ci * might be tricky due to vfs interactions. Need to think about this 5808c2ecf20Sopenharmony_ci * some more when implementing the unlink command. 5818c2ecf20Sopenharmony_ci */ 5828c2ecf20Sopenharmony_ci set_nlink(vi, le16_to_cpu(m->link_count)); 5838c2ecf20Sopenharmony_ci /* 5848c2ecf20Sopenharmony_ci * FIXME: Reparse points can have the directory bit set even though 5858c2ecf20Sopenharmony_ci * they would be S_IFLNK. Need to deal with this further below when we 5868c2ecf20Sopenharmony_ci * implement reparse points / symbolic links but it will do for now. 5878c2ecf20Sopenharmony_ci * Also if not a directory, it could be something else, rather than 5888c2ecf20Sopenharmony_ci * a regular file. But again, will do for now. 5898c2ecf20Sopenharmony_ci */ 5908c2ecf20Sopenharmony_ci /* Everyone gets all permissions. */ 5918c2ecf20Sopenharmony_ci vi->i_mode |= S_IRWXUGO; 5928c2ecf20Sopenharmony_ci /* If read-only, no one gets write permissions. */ 5938c2ecf20Sopenharmony_ci if (IS_RDONLY(vi)) 5948c2ecf20Sopenharmony_ci vi->i_mode &= ~S_IWUGO; 5958c2ecf20Sopenharmony_ci if (m->flags & MFT_RECORD_IS_DIRECTORY) { 5968c2ecf20Sopenharmony_ci vi->i_mode |= S_IFDIR; 5978c2ecf20Sopenharmony_ci /* 5988c2ecf20Sopenharmony_ci * Apply the directory permissions mask set in the mount 5998c2ecf20Sopenharmony_ci * options. 6008c2ecf20Sopenharmony_ci */ 6018c2ecf20Sopenharmony_ci vi->i_mode &= ~vol->dmask; 6028c2ecf20Sopenharmony_ci /* Things break without this kludge! */ 6038c2ecf20Sopenharmony_ci if (vi->i_nlink > 1) 6048c2ecf20Sopenharmony_ci set_nlink(vi, 1); 6058c2ecf20Sopenharmony_ci } else { 6068c2ecf20Sopenharmony_ci vi->i_mode |= S_IFREG; 6078c2ecf20Sopenharmony_ci /* Apply the file permissions mask set in the mount options. */ 6088c2ecf20Sopenharmony_ci vi->i_mode &= ~vol->fmask; 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci /* 6118c2ecf20Sopenharmony_ci * Find the standard information attribute in the mft record. At this 6128c2ecf20Sopenharmony_ci * stage we haven't setup the attribute list stuff yet, so this could 6138c2ecf20Sopenharmony_ci * in fact fail if the standard information is in an extent record, but 6148c2ecf20Sopenharmony_ci * I don't think this actually ever happens. 6158c2ecf20Sopenharmony_ci */ 6168c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0, 6178c2ecf20Sopenharmony_ci ctx); 6188c2ecf20Sopenharmony_ci if (unlikely(err)) { 6198c2ecf20Sopenharmony_ci if (err == -ENOENT) { 6208c2ecf20Sopenharmony_ci /* 6218c2ecf20Sopenharmony_ci * TODO: We should be performing a hot fix here (if the 6228c2ecf20Sopenharmony_ci * recover mount option is set) by creating a new 6238c2ecf20Sopenharmony_ci * attribute. 6248c2ecf20Sopenharmony_ci */ 6258c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute " 6268c2ecf20Sopenharmony_ci "is missing."); 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci goto unm_err_out; 6298c2ecf20Sopenharmony_ci } 6308c2ecf20Sopenharmony_ci a = ctx->attr; 6318c2ecf20Sopenharmony_ci /* Get the standard information attribute value. */ 6328c2ecf20Sopenharmony_ci if ((u8 *)a + le16_to_cpu(a->data.resident.value_offset) 6338c2ecf20Sopenharmony_ci + le32_to_cpu(a->data.resident.value_length) > 6348c2ecf20Sopenharmony_ci (u8 *)ctx->mrec + vol->mft_record_size) { 6358c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Corrupt standard information attribute in inode."); 6368c2ecf20Sopenharmony_ci goto unm_err_out; 6378c2ecf20Sopenharmony_ci } 6388c2ecf20Sopenharmony_ci si = (STANDARD_INFORMATION*)((u8*)a + 6398c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset)); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci /* Transfer information from the standard information into vi. */ 6428c2ecf20Sopenharmony_ci /* 6438c2ecf20Sopenharmony_ci * Note: The i_?times do not quite map perfectly onto the NTFS times, 6448c2ecf20Sopenharmony_ci * but they are close enough, and in the end it doesn't really matter 6458c2ecf20Sopenharmony_ci * that much... 6468c2ecf20Sopenharmony_ci */ 6478c2ecf20Sopenharmony_ci /* 6488c2ecf20Sopenharmony_ci * mtime is the last change of the data within the file. Not changed 6498c2ecf20Sopenharmony_ci * when only metadata is changed, e.g. a rename doesn't affect mtime. 6508c2ecf20Sopenharmony_ci */ 6518c2ecf20Sopenharmony_ci vi->i_mtime = ntfs2utc(si->last_data_change_time); 6528c2ecf20Sopenharmony_ci /* 6538c2ecf20Sopenharmony_ci * ctime is the last change of the metadata of the file. This obviously 6548c2ecf20Sopenharmony_ci * always changes, when mtime is changed. ctime can be changed on its 6558c2ecf20Sopenharmony_ci * own, mtime is then not changed, e.g. when a file is renamed. 6568c2ecf20Sopenharmony_ci */ 6578c2ecf20Sopenharmony_ci vi->i_ctime = ntfs2utc(si->last_mft_change_time); 6588c2ecf20Sopenharmony_ci /* 6598c2ecf20Sopenharmony_ci * Last access to the data within the file. Not changed during a rename 6608c2ecf20Sopenharmony_ci * for example but changed whenever the file is written to. 6618c2ecf20Sopenharmony_ci */ 6628c2ecf20Sopenharmony_ci vi->i_atime = ntfs2utc(si->last_access_time); 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci /* Find the attribute list attribute if present. */ 6658c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 6668c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 6678c2ecf20Sopenharmony_ci if (err) { 6688c2ecf20Sopenharmony_ci if (unlikely(err != -ENOENT)) { 6698c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to lookup attribute list " 6708c2ecf20Sopenharmony_ci "attribute."); 6718c2ecf20Sopenharmony_ci goto unm_err_out; 6728c2ecf20Sopenharmony_ci } 6738c2ecf20Sopenharmony_ci } else /* if (!err) */ { 6748c2ecf20Sopenharmony_ci if (vi->i_ino == FILE_MFT) 6758c2ecf20Sopenharmony_ci goto skip_attr_list_load; 6768c2ecf20Sopenharmony_ci ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino); 6778c2ecf20Sopenharmony_ci NInoSetAttrList(ni); 6788c2ecf20Sopenharmony_ci a = ctx->attr; 6798c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 6808c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Attribute list attribute is " 6818c2ecf20Sopenharmony_ci "compressed."); 6828c2ecf20Sopenharmony_ci goto unm_err_out; 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED || 6858c2ecf20Sopenharmony_ci a->flags & ATTR_IS_SPARSE) { 6868c2ecf20Sopenharmony_ci if (a->non_resident) { 6878c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Non-resident attribute " 6888c2ecf20Sopenharmony_ci "list attribute is encrypted/" 6898c2ecf20Sopenharmony_ci "sparse."); 6908c2ecf20Sopenharmony_ci goto unm_err_out; 6918c2ecf20Sopenharmony_ci } 6928c2ecf20Sopenharmony_ci ntfs_warning(vi->i_sb, "Resident attribute list " 6938c2ecf20Sopenharmony_ci "attribute in inode 0x%lx is marked " 6948c2ecf20Sopenharmony_ci "encrypted/sparse which is not true. " 6958c2ecf20Sopenharmony_ci "However, Windows allows this and " 6968c2ecf20Sopenharmony_ci "chkdsk does not detect or correct it " 6978c2ecf20Sopenharmony_ci "so we will just ignore the invalid " 6988c2ecf20Sopenharmony_ci "flags and pretend they are not set.", 6998c2ecf20Sopenharmony_ci vi->i_ino); 7008c2ecf20Sopenharmony_ci } 7018c2ecf20Sopenharmony_ci /* Now allocate memory for the attribute list. */ 7028c2ecf20Sopenharmony_ci ni->attr_list_size = (u32)ntfs_attr_size(a); 7038c2ecf20Sopenharmony_ci ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 7048c2ecf20Sopenharmony_ci if (!ni->attr_list) { 7058c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Not enough memory to allocate " 7068c2ecf20Sopenharmony_ci "buffer for attribute list."); 7078c2ecf20Sopenharmony_ci err = -ENOMEM; 7088c2ecf20Sopenharmony_ci goto unm_err_out; 7098c2ecf20Sopenharmony_ci } 7108c2ecf20Sopenharmony_ci if (a->non_resident) { 7118c2ecf20Sopenharmony_ci NInoSetAttrListNonResident(ni); 7128c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 7138c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Attribute list has non " 7148c2ecf20Sopenharmony_ci "zero lowest_vcn."); 7158c2ecf20Sopenharmony_ci goto unm_err_out; 7168c2ecf20Sopenharmony_ci } 7178c2ecf20Sopenharmony_ci /* 7188c2ecf20Sopenharmony_ci * Setup the runlist. No need for locking as we have 7198c2ecf20Sopenharmony_ci * exclusive access to the inode at this time. 7208c2ecf20Sopenharmony_ci */ 7218c2ecf20Sopenharmony_ci ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 7228c2ecf20Sopenharmony_ci a, NULL); 7238c2ecf20Sopenharmony_ci if (IS_ERR(ni->attr_list_rl.rl)) { 7248c2ecf20Sopenharmony_ci err = PTR_ERR(ni->attr_list_rl.rl); 7258c2ecf20Sopenharmony_ci ni->attr_list_rl.rl = NULL; 7268c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Mapping pairs " 7278c2ecf20Sopenharmony_ci "decompression failed."); 7288c2ecf20Sopenharmony_ci goto unm_err_out; 7298c2ecf20Sopenharmony_ci } 7308c2ecf20Sopenharmony_ci /* Now load the attribute list. */ 7318c2ecf20Sopenharmony_ci if ((err = load_attribute_list(vol, &ni->attr_list_rl, 7328c2ecf20Sopenharmony_ci ni->attr_list, ni->attr_list_size, 7338c2ecf20Sopenharmony_ci sle64_to_cpu(a->data.non_resident. 7348c2ecf20Sopenharmony_ci initialized_size)))) { 7358c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to load " 7368c2ecf20Sopenharmony_ci "attribute list attribute."); 7378c2ecf20Sopenharmony_ci goto unm_err_out; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci } else /* if (!a->non_resident) */ { 7408c2ecf20Sopenharmony_ci if ((u8*)a + le16_to_cpu(a->data.resident.value_offset) 7418c2ecf20Sopenharmony_ci + le32_to_cpu( 7428c2ecf20Sopenharmony_ci a->data.resident.value_length) > 7438c2ecf20Sopenharmony_ci (u8*)ctx->mrec + vol->mft_record_size) { 7448c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Corrupt attribute list " 7458c2ecf20Sopenharmony_ci "in inode."); 7468c2ecf20Sopenharmony_ci goto unm_err_out; 7478c2ecf20Sopenharmony_ci } 7488c2ecf20Sopenharmony_ci /* Now copy the attribute list. */ 7498c2ecf20Sopenharmony_ci memcpy(ni->attr_list, (u8*)a + le16_to_cpu( 7508c2ecf20Sopenharmony_ci a->data.resident.value_offset), 7518c2ecf20Sopenharmony_ci le32_to_cpu( 7528c2ecf20Sopenharmony_ci a->data.resident.value_length)); 7538c2ecf20Sopenharmony_ci } 7548c2ecf20Sopenharmony_ci } 7558c2ecf20Sopenharmony_ciskip_attr_list_load: 7568c2ecf20Sopenharmony_ci /* 7578c2ecf20Sopenharmony_ci * If an attribute list is present we now have the attribute list value 7588c2ecf20Sopenharmony_ci * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes. 7598c2ecf20Sopenharmony_ci */ 7608c2ecf20Sopenharmony_ci if (S_ISDIR(vi->i_mode)) { 7618c2ecf20Sopenharmony_ci loff_t bvi_size; 7628c2ecf20Sopenharmony_ci ntfs_inode *bni; 7638c2ecf20Sopenharmony_ci INDEX_ROOT *ir; 7648c2ecf20Sopenharmony_ci u8 *ir_end, *index_end; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci /* It is a directory, find index root attribute. */ 7678c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 7688c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 7698c2ecf20Sopenharmony_ci 0, NULL, 0, ctx); 7708c2ecf20Sopenharmony_ci if (unlikely(err)) { 7718c2ecf20Sopenharmony_ci if (err == -ENOENT) { 7728c2ecf20Sopenharmony_ci // FIXME: File is corrupt! Hot-fix with empty 7738c2ecf20Sopenharmony_ci // index root attribute if recovery option is 7748c2ecf20Sopenharmony_ci // set. 7758c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ROOT attribute " 7768c2ecf20Sopenharmony_ci "is missing."); 7778c2ecf20Sopenharmony_ci } 7788c2ecf20Sopenharmony_ci goto unm_err_out; 7798c2ecf20Sopenharmony_ci } 7808c2ecf20Sopenharmony_ci a = ctx->attr; 7818c2ecf20Sopenharmony_ci /* Set up the state. */ 7828c2ecf20Sopenharmony_ci if (unlikely(a->non_resident)) { 7838c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "$INDEX_ROOT attribute is not " 7848c2ecf20Sopenharmony_ci "resident."); 7858c2ecf20Sopenharmony_ci goto unm_err_out; 7868c2ecf20Sopenharmony_ci } 7878c2ecf20Sopenharmony_ci /* Ensure the attribute name is placed before the value. */ 7888c2ecf20Sopenharmony_ci if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 7898c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset)))) { 7908c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "$INDEX_ROOT attribute name is " 7918c2ecf20Sopenharmony_ci "placed after the attribute value."); 7928c2ecf20Sopenharmony_ci goto unm_err_out; 7938c2ecf20Sopenharmony_ci } 7948c2ecf20Sopenharmony_ci /* 7958c2ecf20Sopenharmony_ci * Compressed/encrypted index root just means that the newly 7968c2ecf20Sopenharmony_ci * created files in that directory should be created compressed/ 7978c2ecf20Sopenharmony_ci * encrypted. However index root cannot be both compressed and 7988c2ecf20Sopenharmony_ci * encrypted. 7998c2ecf20Sopenharmony_ci */ 8008c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) 8018c2ecf20Sopenharmony_ci NInoSetCompressed(ni); 8028c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED) { 8038c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 8048c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found encrypted and " 8058c2ecf20Sopenharmony_ci "compressed attribute."); 8068c2ecf20Sopenharmony_ci goto unm_err_out; 8078c2ecf20Sopenharmony_ci } 8088c2ecf20Sopenharmony_ci NInoSetEncrypted(ni); 8098c2ecf20Sopenharmony_ci } 8108c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_SPARSE) 8118c2ecf20Sopenharmony_ci NInoSetSparse(ni); 8128c2ecf20Sopenharmony_ci ir = (INDEX_ROOT*)((u8*)a + 8138c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset)); 8148c2ecf20Sopenharmony_ci ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length); 8158c2ecf20Sopenharmony_ci if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) { 8168c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is " 8178c2ecf20Sopenharmony_ci "corrupt."); 8188c2ecf20Sopenharmony_ci goto unm_err_out; 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci index_end = (u8*)&ir->index + 8218c2ecf20Sopenharmony_ci le32_to_cpu(ir->index.index_length); 8228c2ecf20Sopenharmony_ci if (index_end > ir_end) { 8238c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Directory index is corrupt."); 8248c2ecf20Sopenharmony_ci goto unm_err_out; 8258c2ecf20Sopenharmony_ci } 8268c2ecf20Sopenharmony_ci if (ir->type != AT_FILE_NAME) { 8278c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Indexed attribute is not " 8288c2ecf20Sopenharmony_ci "$FILE_NAME."); 8298c2ecf20Sopenharmony_ci goto unm_err_out; 8308c2ecf20Sopenharmony_ci } 8318c2ecf20Sopenharmony_ci if (ir->collation_rule != COLLATION_FILE_NAME) { 8328c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index collation rule is not " 8338c2ecf20Sopenharmony_ci "COLLATION_FILE_NAME."); 8348c2ecf20Sopenharmony_ci goto unm_err_out; 8358c2ecf20Sopenharmony_ci } 8368c2ecf20Sopenharmony_ci ni->itype.index.collation_rule = ir->collation_rule; 8378c2ecf20Sopenharmony_ci ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 8388c2ecf20Sopenharmony_ci if (ni->itype.index.block_size & 8398c2ecf20Sopenharmony_ci (ni->itype.index.block_size - 1)) { 8408c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index block size (%u) is not a " 8418c2ecf20Sopenharmony_ci "power of two.", 8428c2ecf20Sopenharmony_ci ni->itype.index.block_size); 8438c2ecf20Sopenharmony_ci goto unm_err_out; 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci if (ni->itype.index.block_size > PAGE_SIZE) { 8468c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index block size (%u) > " 8478c2ecf20Sopenharmony_ci "PAGE_SIZE (%ld) is not " 8488c2ecf20Sopenharmony_ci "supported. Sorry.", 8498c2ecf20Sopenharmony_ci ni->itype.index.block_size, 8508c2ecf20Sopenharmony_ci PAGE_SIZE); 8518c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 8528c2ecf20Sopenharmony_ci goto unm_err_out; 8538c2ecf20Sopenharmony_ci } 8548c2ecf20Sopenharmony_ci if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 8558c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index block size (%u) < " 8568c2ecf20Sopenharmony_ci "NTFS_BLOCK_SIZE (%i) is not " 8578c2ecf20Sopenharmony_ci "supported. Sorry.", 8588c2ecf20Sopenharmony_ci ni->itype.index.block_size, 8598c2ecf20Sopenharmony_ci NTFS_BLOCK_SIZE); 8608c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 8618c2ecf20Sopenharmony_ci goto unm_err_out; 8628c2ecf20Sopenharmony_ci } 8638c2ecf20Sopenharmony_ci ni->itype.index.block_size_bits = 8648c2ecf20Sopenharmony_ci ffs(ni->itype.index.block_size) - 1; 8658c2ecf20Sopenharmony_ci /* Determine the size of a vcn in the directory index. */ 8668c2ecf20Sopenharmony_ci if (vol->cluster_size <= ni->itype.index.block_size) { 8678c2ecf20Sopenharmony_ci ni->itype.index.vcn_size = vol->cluster_size; 8688c2ecf20Sopenharmony_ci ni->itype.index.vcn_size_bits = vol->cluster_size_bits; 8698c2ecf20Sopenharmony_ci } else { 8708c2ecf20Sopenharmony_ci ni->itype.index.vcn_size = vol->sector_size; 8718c2ecf20Sopenharmony_ci ni->itype.index.vcn_size_bits = vol->sector_size_bits; 8728c2ecf20Sopenharmony_ci } 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci /* Setup the index allocation attribute, even if not present. */ 8758c2ecf20Sopenharmony_ci NInoSetMstProtected(ni); 8768c2ecf20Sopenharmony_ci ni->type = AT_INDEX_ALLOCATION; 8778c2ecf20Sopenharmony_ci ni->name = I30; 8788c2ecf20Sopenharmony_ci ni->name_len = 4; 8798c2ecf20Sopenharmony_ci 8808c2ecf20Sopenharmony_ci if (!(ir->index.flags & LARGE_INDEX)) { 8818c2ecf20Sopenharmony_ci /* No index allocation. */ 8828c2ecf20Sopenharmony_ci vi->i_size = ni->initialized_size = 8838c2ecf20Sopenharmony_ci ni->allocated_size = 0; 8848c2ecf20Sopenharmony_ci /* We are done with the mft record, so we release it. */ 8858c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 8868c2ecf20Sopenharmony_ci unmap_mft_record(ni); 8878c2ecf20Sopenharmony_ci m = NULL; 8888c2ecf20Sopenharmony_ci ctx = NULL; 8898c2ecf20Sopenharmony_ci goto skip_large_dir_stuff; 8908c2ecf20Sopenharmony_ci } /* LARGE_INDEX: Index allocation present. Setup state. */ 8918c2ecf20Sopenharmony_ci NInoSetIndexAllocPresent(ni); 8928c2ecf20Sopenharmony_ci /* Find index allocation attribute. */ 8938c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 8948c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4, 8958c2ecf20Sopenharmony_ci CASE_SENSITIVE, 0, NULL, 0, ctx); 8968c2ecf20Sopenharmony_ci if (unlikely(err)) { 8978c2ecf20Sopenharmony_ci if (err == -ENOENT) 8988c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION " 8998c2ecf20Sopenharmony_ci "attribute is not present but " 9008c2ecf20Sopenharmony_ci "$INDEX_ROOT indicated it is."); 9018c2ecf20Sopenharmony_ci else 9028c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to lookup " 9038c2ecf20Sopenharmony_ci "$INDEX_ALLOCATION " 9048c2ecf20Sopenharmony_ci "attribute."); 9058c2ecf20Sopenharmony_ci goto unm_err_out; 9068c2ecf20Sopenharmony_ci } 9078c2ecf20Sopenharmony_ci a = ctx->attr; 9088c2ecf20Sopenharmony_ci if (!a->non_resident) { 9098c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 9108c2ecf20Sopenharmony_ci "is resident."); 9118c2ecf20Sopenharmony_ci goto unm_err_out; 9128c2ecf20Sopenharmony_ci } 9138c2ecf20Sopenharmony_ci /* 9148c2ecf20Sopenharmony_ci * Ensure the attribute name is placed before the mapping pairs 9158c2ecf20Sopenharmony_ci * array. 9168c2ecf20Sopenharmony_ci */ 9178c2ecf20Sopenharmony_ci if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 9188c2ecf20Sopenharmony_ci le16_to_cpu( 9198c2ecf20Sopenharmony_ci a->data.non_resident.mapping_pairs_offset)))) { 9208c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name " 9218c2ecf20Sopenharmony_ci "is placed after the mapping pairs " 9228c2ecf20Sopenharmony_ci "array."); 9238c2ecf20Sopenharmony_ci goto unm_err_out; 9248c2ecf20Sopenharmony_ci } 9258c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED) { 9268c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 9278c2ecf20Sopenharmony_ci "is encrypted."); 9288c2ecf20Sopenharmony_ci goto unm_err_out; 9298c2ecf20Sopenharmony_ci } 9308c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_SPARSE) { 9318c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 9328c2ecf20Sopenharmony_ci "is sparse."); 9338c2ecf20Sopenharmony_ci goto unm_err_out; 9348c2ecf20Sopenharmony_ci } 9358c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 9368c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 9378c2ecf20Sopenharmony_ci "is compressed."); 9388c2ecf20Sopenharmony_ci goto unm_err_out; 9398c2ecf20Sopenharmony_ci } 9408c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 9418c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "First extent of " 9428c2ecf20Sopenharmony_ci "$INDEX_ALLOCATION attribute has non " 9438c2ecf20Sopenharmony_ci "zero lowest_vcn."); 9448c2ecf20Sopenharmony_ci goto unm_err_out; 9458c2ecf20Sopenharmony_ci } 9468c2ecf20Sopenharmony_ci vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); 9478c2ecf20Sopenharmony_ci ni->initialized_size = sle64_to_cpu( 9488c2ecf20Sopenharmony_ci a->data.non_resident.initialized_size); 9498c2ecf20Sopenharmony_ci ni->allocated_size = sle64_to_cpu( 9508c2ecf20Sopenharmony_ci a->data.non_resident.allocated_size); 9518c2ecf20Sopenharmony_ci /* 9528c2ecf20Sopenharmony_ci * We are done with the mft record, so we release it. Otherwise 9538c2ecf20Sopenharmony_ci * we would deadlock in ntfs_attr_iget(). 9548c2ecf20Sopenharmony_ci */ 9558c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 9568c2ecf20Sopenharmony_ci unmap_mft_record(ni); 9578c2ecf20Sopenharmony_ci m = NULL; 9588c2ecf20Sopenharmony_ci ctx = NULL; 9598c2ecf20Sopenharmony_ci /* Get the index bitmap attribute inode. */ 9608c2ecf20Sopenharmony_ci bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4); 9618c2ecf20Sopenharmony_ci if (IS_ERR(bvi)) { 9628c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to get bitmap attribute."); 9638c2ecf20Sopenharmony_ci err = PTR_ERR(bvi); 9648c2ecf20Sopenharmony_ci goto unm_err_out; 9658c2ecf20Sopenharmony_ci } 9668c2ecf20Sopenharmony_ci bni = NTFS_I(bvi); 9678c2ecf20Sopenharmony_ci if (NInoCompressed(bni) || NInoEncrypted(bni) || 9688c2ecf20Sopenharmony_ci NInoSparse(bni)) { 9698c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$BITMAP attribute is compressed " 9708c2ecf20Sopenharmony_ci "and/or encrypted and/or sparse."); 9718c2ecf20Sopenharmony_ci goto iput_unm_err_out; 9728c2ecf20Sopenharmony_ci } 9738c2ecf20Sopenharmony_ci /* Consistency check bitmap size vs. index allocation size. */ 9748c2ecf20Sopenharmony_ci bvi_size = i_size_read(bvi); 9758c2ecf20Sopenharmony_ci if ((bvi_size << 3) < (vi->i_size >> 9768c2ecf20Sopenharmony_ci ni->itype.index.block_size_bits)) { 9778c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) " 9788c2ecf20Sopenharmony_ci "for index allocation (0x%llx).", 9798c2ecf20Sopenharmony_ci bvi_size << 3, vi->i_size); 9808c2ecf20Sopenharmony_ci goto iput_unm_err_out; 9818c2ecf20Sopenharmony_ci } 9828c2ecf20Sopenharmony_ci /* No longer need the bitmap attribute inode. */ 9838c2ecf20Sopenharmony_ci iput(bvi); 9848c2ecf20Sopenharmony_ciskip_large_dir_stuff: 9858c2ecf20Sopenharmony_ci /* Setup the operations for this inode. */ 9868c2ecf20Sopenharmony_ci vi->i_op = &ntfs_dir_inode_ops; 9878c2ecf20Sopenharmony_ci vi->i_fop = &ntfs_dir_ops; 9888c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_mst_aops; 9898c2ecf20Sopenharmony_ci } else { 9908c2ecf20Sopenharmony_ci /* It is a file. */ 9918c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci /* Setup the data attribute, even if not present. */ 9948c2ecf20Sopenharmony_ci ni->type = AT_DATA; 9958c2ecf20Sopenharmony_ci ni->name = NULL; 9968c2ecf20Sopenharmony_ci ni->name_len = 0; 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_ci /* Find first extent of the unnamed data attribute. */ 9998c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx); 10008c2ecf20Sopenharmony_ci if (unlikely(err)) { 10018c2ecf20Sopenharmony_ci vi->i_size = ni->initialized_size = 10028c2ecf20Sopenharmony_ci ni->allocated_size = 0; 10038c2ecf20Sopenharmony_ci if (err != -ENOENT) { 10048c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to lookup $DATA " 10058c2ecf20Sopenharmony_ci "attribute."); 10068c2ecf20Sopenharmony_ci goto unm_err_out; 10078c2ecf20Sopenharmony_ci } 10088c2ecf20Sopenharmony_ci /* 10098c2ecf20Sopenharmony_ci * FILE_Secure does not have an unnamed $DATA 10108c2ecf20Sopenharmony_ci * attribute, so we special case it here. 10118c2ecf20Sopenharmony_ci */ 10128c2ecf20Sopenharmony_ci if (vi->i_ino == FILE_Secure) 10138c2ecf20Sopenharmony_ci goto no_data_attr_special_case; 10148c2ecf20Sopenharmony_ci /* 10158c2ecf20Sopenharmony_ci * Most if not all the system files in the $Extend 10168c2ecf20Sopenharmony_ci * system directory do not have unnamed data 10178c2ecf20Sopenharmony_ci * attributes so we need to check if the parent 10188c2ecf20Sopenharmony_ci * directory of the file is FILE_Extend and if it is 10198c2ecf20Sopenharmony_ci * ignore this error. To do this we need to get the 10208c2ecf20Sopenharmony_ci * name of this inode from the mft record as the name 10218c2ecf20Sopenharmony_ci * contains the back reference to the parent directory. 10228c2ecf20Sopenharmony_ci */ 10238c2ecf20Sopenharmony_ci if (ntfs_is_extended_system_file(ctx) > 0) 10248c2ecf20Sopenharmony_ci goto no_data_attr_special_case; 10258c2ecf20Sopenharmony_ci // FIXME: File is corrupt! Hot-fix with empty data 10268c2ecf20Sopenharmony_ci // attribute if recovery option is set. 10278c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$DATA attribute is missing."); 10288c2ecf20Sopenharmony_ci goto unm_err_out; 10298c2ecf20Sopenharmony_ci } 10308c2ecf20Sopenharmony_ci a = ctx->attr; 10318c2ecf20Sopenharmony_ci /* Setup the state. */ 10328c2ecf20Sopenharmony_ci if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 10338c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 10348c2ecf20Sopenharmony_ci NInoSetCompressed(ni); 10358c2ecf20Sopenharmony_ci if (vol->cluster_size > 4096) { 10368c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found " 10378c2ecf20Sopenharmony_ci "compressed data but " 10388c2ecf20Sopenharmony_ci "compression is " 10398c2ecf20Sopenharmony_ci "disabled due to " 10408c2ecf20Sopenharmony_ci "cluster size (%i) > " 10418c2ecf20Sopenharmony_ci "4kiB.", 10428c2ecf20Sopenharmony_ci vol->cluster_size); 10438c2ecf20Sopenharmony_ci goto unm_err_out; 10448c2ecf20Sopenharmony_ci } 10458c2ecf20Sopenharmony_ci if ((a->flags & ATTR_COMPRESSION_MASK) 10468c2ecf20Sopenharmony_ci != ATTR_IS_COMPRESSED) { 10478c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found unknown " 10488c2ecf20Sopenharmony_ci "compression method " 10498c2ecf20Sopenharmony_ci "or corrupt file."); 10508c2ecf20Sopenharmony_ci goto unm_err_out; 10518c2ecf20Sopenharmony_ci } 10528c2ecf20Sopenharmony_ci } 10538c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_SPARSE) 10548c2ecf20Sopenharmony_ci NInoSetSparse(ni); 10558c2ecf20Sopenharmony_ci } 10568c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED) { 10578c2ecf20Sopenharmony_ci if (NInoCompressed(ni)) { 10588c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found encrypted and " 10598c2ecf20Sopenharmony_ci "compressed data."); 10608c2ecf20Sopenharmony_ci goto unm_err_out; 10618c2ecf20Sopenharmony_ci } 10628c2ecf20Sopenharmony_ci NInoSetEncrypted(ni); 10638c2ecf20Sopenharmony_ci } 10648c2ecf20Sopenharmony_ci if (a->non_resident) { 10658c2ecf20Sopenharmony_ci NInoSetNonResident(ni); 10668c2ecf20Sopenharmony_ci if (NInoCompressed(ni) || NInoSparse(ni)) { 10678c2ecf20Sopenharmony_ci if (NInoCompressed(ni) && a->data.non_resident. 10688c2ecf20Sopenharmony_ci compression_unit != 4) { 10698c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found " 10708c2ecf20Sopenharmony_ci "non-standard " 10718c2ecf20Sopenharmony_ci "compression unit (%u " 10728c2ecf20Sopenharmony_ci "instead of 4). " 10738c2ecf20Sopenharmony_ci "Cannot handle this.", 10748c2ecf20Sopenharmony_ci a->data.non_resident. 10758c2ecf20Sopenharmony_ci compression_unit); 10768c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 10778c2ecf20Sopenharmony_ci goto unm_err_out; 10788c2ecf20Sopenharmony_ci } 10798c2ecf20Sopenharmony_ci if (a->data.non_resident.compression_unit) { 10808c2ecf20Sopenharmony_ci ni->itype.compressed.block_size = 1U << 10818c2ecf20Sopenharmony_ci (a->data.non_resident. 10828c2ecf20Sopenharmony_ci compression_unit + 10838c2ecf20Sopenharmony_ci vol->cluster_size_bits); 10848c2ecf20Sopenharmony_ci ni->itype.compressed.block_size_bits = 10858c2ecf20Sopenharmony_ci ffs(ni->itype. 10868c2ecf20Sopenharmony_ci compressed. 10878c2ecf20Sopenharmony_ci block_size) - 1; 10888c2ecf20Sopenharmony_ci ni->itype.compressed.block_clusters = 10898c2ecf20Sopenharmony_ci 1U << a->data. 10908c2ecf20Sopenharmony_ci non_resident. 10918c2ecf20Sopenharmony_ci compression_unit; 10928c2ecf20Sopenharmony_ci } else { 10938c2ecf20Sopenharmony_ci ni->itype.compressed.block_size = 0; 10948c2ecf20Sopenharmony_ci ni->itype.compressed.block_size_bits = 10958c2ecf20Sopenharmony_ci 0; 10968c2ecf20Sopenharmony_ci ni->itype.compressed.block_clusters = 10978c2ecf20Sopenharmony_ci 0; 10988c2ecf20Sopenharmony_ci } 10998c2ecf20Sopenharmony_ci ni->itype.compressed.size = sle64_to_cpu( 11008c2ecf20Sopenharmony_ci a->data.non_resident. 11018c2ecf20Sopenharmony_ci compressed_size); 11028c2ecf20Sopenharmony_ci } 11038c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 11048c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "First extent of $DATA " 11058c2ecf20Sopenharmony_ci "attribute has non zero " 11068c2ecf20Sopenharmony_ci "lowest_vcn."); 11078c2ecf20Sopenharmony_ci goto unm_err_out; 11088c2ecf20Sopenharmony_ci } 11098c2ecf20Sopenharmony_ci vi->i_size = sle64_to_cpu( 11108c2ecf20Sopenharmony_ci a->data.non_resident.data_size); 11118c2ecf20Sopenharmony_ci ni->initialized_size = sle64_to_cpu( 11128c2ecf20Sopenharmony_ci a->data.non_resident.initialized_size); 11138c2ecf20Sopenharmony_ci ni->allocated_size = sle64_to_cpu( 11148c2ecf20Sopenharmony_ci a->data.non_resident.allocated_size); 11158c2ecf20Sopenharmony_ci } else { /* Resident attribute. */ 11168c2ecf20Sopenharmony_ci vi->i_size = ni->initialized_size = le32_to_cpu( 11178c2ecf20Sopenharmony_ci a->data.resident.value_length); 11188c2ecf20Sopenharmony_ci ni->allocated_size = le32_to_cpu(a->length) - 11198c2ecf20Sopenharmony_ci le16_to_cpu( 11208c2ecf20Sopenharmony_ci a->data.resident.value_offset); 11218c2ecf20Sopenharmony_ci if (vi->i_size > ni->allocated_size) { 11228c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Resident data attribute " 11238c2ecf20Sopenharmony_ci "is corrupt (size exceeds " 11248c2ecf20Sopenharmony_ci "allocation)."); 11258c2ecf20Sopenharmony_ci goto unm_err_out; 11268c2ecf20Sopenharmony_ci } 11278c2ecf20Sopenharmony_ci } 11288c2ecf20Sopenharmony_cino_data_attr_special_case: 11298c2ecf20Sopenharmony_ci /* We are done with the mft record, so we release it. */ 11308c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 11318c2ecf20Sopenharmony_ci unmap_mft_record(ni); 11328c2ecf20Sopenharmony_ci m = NULL; 11338c2ecf20Sopenharmony_ci ctx = NULL; 11348c2ecf20Sopenharmony_ci /* Setup the operations for this inode. */ 11358c2ecf20Sopenharmony_ci vi->i_op = &ntfs_file_inode_ops; 11368c2ecf20Sopenharmony_ci vi->i_fop = &ntfs_file_ops; 11378c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_normal_aops; 11388c2ecf20Sopenharmony_ci if (NInoMstProtected(ni)) 11398c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_mst_aops; 11408c2ecf20Sopenharmony_ci else if (NInoCompressed(ni)) 11418c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_compressed_aops; 11428c2ecf20Sopenharmony_ci } 11438c2ecf20Sopenharmony_ci /* 11448c2ecf20Sopenharmony_ci * The number of 512-byte blocks used on disk (for stat). This is in so 11458c2ecf20Sopenharmony_ci * far inaccurate as it doesn't account for any named streams or other 11468c2ecf20Sopenharmony_ci * special non-resident attributes, but that is how Windows works, too, 11478c2ecf20Sopenharmony_ci * so we are at least consistent with Windows, if not entirely 11488c2ecf20Sopenharmony_ci * consistent with the Linux Way. Doing it the Linux Way would cause a 11498c2ecf20Sopenharmony_ci * significant slowdown as it would involve iterating over all 11508c2ecf20Sopenharmony_ci * attributes in the mft record and adding the allocated/compressed 11518c2ecf20Sopenharmony_ci * sizes of all non-resident attributes present to give us the Linux 11528c2ecf20Sopenharmony_ci * correct size that should go into i_blocks (after division by 512). 11538c2ecf20Sopenharmony_ci */ 11548c2ecf20Sopenharmony_ci if (S_ISREG(vi->i_mode) && (NInoCompressed(ni) || NInoSparse(ni))) 11558c2ecf20Sopenharmony_ci vi->i_blocks = ni->itype.compressed.size >> 9; 11568c2ecf20Sopenharmony_ci else 11578c2ecf20Sopenharmony_ci vi->i_blocks = ni->allocated_size >> 9; 11588c2ecf20Sopenharmony_ci ntfs_debug("Done."); 11598c2ecf20Sopenharmony_ci return 0; 11608c2ecf20Sopenharmony_ciiput_unm_err_out: 11618c2ecf20Sopenharmony_ci iput(bvi); 11628c2ecf20Sopenharmony_ciunm_err_out: 11638c2ecf20Sopenharmony_ci if (!err) 11648c2ecf20Sopenharmony_ci err = -EIO; 11658c2ecf20Sopenharmony_ci if (ctx) 11668c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 11678c2ecf20Sopenharmony_ci if (m) 11688c2ecf20Sopenharmony_ci unmap_mft_record(ni); 11698c2ecf20Sopenharmony_cierr_out: 11708c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Failed with error code %i. Marking corrupt " 11718c2ecf20Sopenharmony_ci "inode 0x%lx as bad. Run chkdsk.", err, vi->i_ino); 11728c2ecf20Sopenharmony_ci make_bad_inode(vi); 11738c2ecf20Sopenharmony_ci if (err != -EOPNOTSUPP && err != -ENOMEM) 11748c2ecf20Sopenharmony_ci NVolSetErrors(vol); 11758c2ecf20Sopenharmony_ci return err; 11768c2ecf20Sopenharmony_ci} 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci/** 11798c2ecf20Sopenharmony_ci * ntfs_read_locked_attr_inode - read an attribute inode from its base inode 11808c2ecf20Sopenharmony_ci * @base_vi: base inode 11818c2ecf20Sopenharmony_ci * @vi: attribute inode to read 11828c2ecf20Sopenharmony_ci * 11838c2ecf20Sopenharmony_ci * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the 11848c2ecf20Sopenharmony_ci * attribute inode described by @vi into memory from the base mft record 11858c2ecf20Sopenharmony_ci * described by @base_ni. 11868c2ecf20Sopenharmony_ci * 11878c2ecf20Sopenharmony_ci * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for 11888c2ecf20Sopenharmony_ci * reading and looks up the attribute described by @vi before setting up the 11898c2ecf20Sopenharmony_ci * necessary fields in @vi as well as initializing the ntfs inode. 11908c2ecf20Sopenharmony_ci * 11918c2ecf20Sopenharmony_ci * Q: What locks are held when the function is called? 11928c2ecf20Sopenharmony_ci * A: i_state has I_NEW set, hence the inode is locked, also 11938c2ecf20Sopenharmony_ci * i_count is set to 1, so it is not going to go away 11948c2ecf20Sopenharmony_ci * 11958c2ecf20Sopenharmony_ci * Return 0 on success and -errno on error. In the error case, the inode will 11968c2ecf20Sopenharmony_ci * have had make_bad_inode() executed on it. 11978c2ecf20Sopenharmony_ci * 11988c2ecf20Sopenharmony_ci * Note this cannot be called for AT_INDEX_ALLOCATION. 11998c2ecf20Sopenharmony_ci */ 12008c2ecf20Sopenharmony_cistatic int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi) 12018c2ecf20Sopenharmony_ci{ 12028c2ecf20Sopenharmony_ci ntfs_volume *vol = NTFS_SB(vi->i_sb); 12038c2ecf20Sopenharmony_ci ntfs_inode *ni, *base_ni; 12048c2ecf20Sopenharmony_ci MFT_RECORD *m; 12058c2ecf20Sopenharmony_ci ATTR_RECORD *a; 12068c2ecf20Sopenharmony_ci ntfs_attr_search_ctx *ctx; 12078c2ecf20Sopenharmony_ci int err = 0; 12088c2ecf20Sopenharmony_ci 12098c2ecf20Sopenharmony_ci ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); 12108c2ecf20Sopenharmony_ci 12118c2ecf20Sopenharmony_ci ntfs_init_big_inode(vi); 12128c2ecf20Sopenharmony_ci 12138c2ecf20Sopenharmony_ci ni = NTFS_I(vi); 12148c2ecf20Sopenharmony_ci base_ni = NTFS_I(base_vi); 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ci /* Just mirror the values from the base inode. */ 12178c2ecf20Sopenharmony_ci vi->i_uid = base_vi->i_uid; 12188c2ecf20Sopenharmony_ci vi->i_gid = base_vi->i_gid; 12198c2ecf20Sopenharmony_ci set_nlink(vi, base_vi->i_nlink); 12208c2ecf20Sopenharmony_ci vi->i_mtime = base_vi->i_mtime; 12218c2ecf20Sopenharmony_ci vi->i_ctime = base_vi->i_ctime; 12228c2ecf20Sopenharmony_ci vi->i_atime = base_vi->i_atime; 12238c2ecf20Sopenharmony_ci vi->i_generation = ni->seq_no = base_ni->seq_no; 12248c2ecf20Sopenharmony_ci 12258c2ecf20Sopenharmony_ci /* Set inode type to zero but preserve permissions. */ 12268c2ecf20Sopenharmony_ci vi->i_mode = base_vi->i_mode & ~S_IFMT; 12278c2ecf20Sopenharmony_ci 12288c2ecf20Sopenharmony_ci m = map_mft_record(base_ni); 12298c2ecf20Sopenharmony_ci if (IS_ERR(m)) { 12308c2ecf20Sopenharmony_ci err = PTR_ERR(m); 12318c2ecf20Sopenharmony_ci goto err_out; 12328c2ecf20Sopenharmony_ci } 12338c2ecf20Sopenharmony_ci ctx = ntfs_attr_get_search_ctx(base_ni, m); 12348c2ecf20Sopenharmony_ci if (!ctx) { 12358c2ecf20Sopenharmony_ci err = -ENOMEM; 12368c2ecf20Sopenharmony_ci goto unm_err_out; 12378c2ecf20Sopenharmony_ci } 12388c2ecf20Sopenharmony_ci /* Find the attribute. */ 12398c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 12408c2ecf20Sopenharmony_ci CASE_SENSITIVE, 0, NULL, 0, ctx); 12418c2ecf20Sopenharmony_ci if (unlikely(err)) 12428c2ecf20Sopenharmony_ci goto unm_err_out; 12438c2ecf20Sopenharmony_ci a = ctx->attr; 12448c2ecf20Sopenharmony_ci if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 12458c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 12468c2ecf20Sopenharmony_ci NInoSetCompressed(ni); 12478c2ecf20Sopenharmony_ci if ((ni->type != AT_DATA) || (ni->type == AT_DATA && 12488c2ecf20Sopenharmony_ci ni->name_len)) { 12498c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found compressed " 12508c2ecf20Sopenharmony_ci "non-data or named data " 12518c2ecf20Sopenharmony_ci "attribute. Please report " 12528c2ecf20Sopenharmony_ci "you saw this message to " 12538c2ecf20Sopenharmony_ci "linux-ntfs-dev@lists." 12548c2ecf20Sopenharmony_ci "sourceforge.net"); 12558c2ecf20Sopenharmony_ci goto unm_err_out; 12568c2ecf20Sopenharmony_ci } 12578c2ecf20Sopenharmony_ci if (vol->cluster_size > 4096) { 12588c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found compressed " 12598c2ecf20Sopenharmony_ci "attribute but compression is " 12608c2ecf20Sopenharmony_ci "disabled due to cluster size " 12618c2ecf20Sopenharmony_ci "(%i) > 4kiB.", 12628c2ecf20Sopenharmony_ci vol->cluster_size); 12638c2ecf20Sopenharmony_ci goto unm_err_out; 12648c2ecf20Sopenharmony_ci } 12658c2ecf20Sopenharmony_ci if ((a->flags & ATTR_COMPRESSION_MASK) != 12668c2ecf20Sopenharmony_ci ATTR_IS_COMPRESSED) { 12678c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found unknown " 12688c2ecf20Sopenharmony_ci "compression method."); 12698c2ecf20Sopenharmony_ci goto unm_err_out; 12708c2ecf20Sopenharmony_ci } 12718c2ecf20Sopenharmony_ci } 12728c2ecf20Sopenharmony_ci /* 12738c2ecf20Sopenharmony_ci * The compressed/sparse flag set in an index root just means 12748c2ecf20Sopenharmony_ci * to compress all files. 12758c2ecf20Sopenharmony_ci */ 12768c2ecf20Sopenharmony_ci if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 12778c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found mst protected attribute " 12788c2ecf20Sopenharmony_ci "but the attribute is %s. Please " 12798c2ecf20Sopenharmony_ci "report you saw this message to " 12808c2ecf20Sopenharmony_ci "linux-ntfs-dev@lists.sourceforge.net", 12818c2ecf20Sopenharmony_ci NInoCompressed(ni) ? "compressed" : 12828c2ecf20Sopenharmony_ci "sparse"); 12838c2ecf20Sopenharmony_ci goto unm_err_out; 12848c2ecf20Sopenharmony_ci } 12858c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_SPARSE) 12868c2ecf20Sopenharmony_ci NInoSetSparse(ni); 12878c2ecf20Sopenharmony_ci } 12888c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED) { 12898c2ecf20Sopenharmony_ci if (NInoCompressed(ni)) { 12908c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found encrypted and compressed " 12918c2ecf20Sopenharmony_ci "data."); 12928c2ecf20Sopenharmony_ci goto unm_err_out; 12938c2ecf20Sopenharmony_ci } 12948c2ecf20Sopenharmony_ci /* 12958c2ecf20Sopenharmony_ci * The encryption flag set in an index root just means to 12968c2ecf20Sopenharmony_ci * encrypt all files. 12978c2ecf20Sopenharmony_ci */ 12988c2ecf20Sopenharmony_ci if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 12998c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found mst protected attribute " 13008c2ecf20Sopenharmony_ci "but the attribute is encrypted. " 13018c2ecf20Sopenharmony_ci "Please report you saw this message " 13028c2ecf20Sopenharmony_ci "to linux-ntfs-dev@lists.sourceforge." 13038c2ecf20Sopenharmony_ci "net"); 13048c2ecf20Sopenharmony_ci goto unm_err_out; 13058c2ecf20Sopenharmony_ci } 13068c2ecf20Sopenharmony_ci if (ni->type != AT_DATA) { 13078c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found encrypted non-data " 13088c2ecf20Sopenharmony_ci "attribute."); 13098c2ecf20Sopenharmony_ci goto unm_err_out; 13108c2ecf20Sopenharmony_ci } 13118c2ecf20Sopenharmony_ci NInoSetEncrypted(ni); 13128c2ecf20Sopenharmony_ci } 13138c2ecf20Sopenharmony_ci if (!a->non_resident) { 13148c2ecf20Sopenharmony_ci /* Ensure the attribute name is placed before the value. */ 13158c2ecf20Sopenharmony_ci if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 13168c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset)))) { 13178c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Attribute name is placed after " 13188c2ecf20Sopenharmony_ci "the attribute value."); 13198c2ecf20Sopenharmony_ci goto unm_err_out; 13208c2ecf20Sopenharmony_ci } 13218c2ecf20Sopenharmony_ci if (NInoMstProtected(ni)) { 13228c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found mst protected attribute " 13238c2ecf20Sopenharmony_ci "but the attribute is resident. " 13248c2ecf20Sopenharmony_ci "Please report you saw this message to " 13258c2ecf20Sopenharmony_ci "linux-ntfs-dev@lists.sourceforge.net"); 13268c2ecf20Sopenharmony_ci goto unm_err_out; 13278c2ecf20Sopenharmony_ci } 13288c2ecf20Sopenharmony_ci vi->i_size = ni->initialized_size = le32_to_cpu( 13298c2ecf20Sopenharmony_ci a->data.resident.value_length); 13308c2ecf20Sopenharmony_ci ni->allocated_size = le32_to_cpu(a->length) - 13318c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset); 13328c2ecf20Sopenharmony_ci if (vi->i_size > ni->allocated_size) { 13338c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Resident attribute is corrupt " 13348c2ecf20Sopenharmony_ci "(size exceeds allocation)."); 13358c2ecf20Sopenharmony_ci goto unm_err_out; 13368c2ecf20Sopenharmony_ci } 13378c2ecf20Sopenharmony_ci } else { 13388c2ecf20Sopenharmony_ci NInoSetNonResident(ni); 13398c2ecf20Sopenharmony_ci /* 13408c2ecf20Sopenharmony_ci * Ensure the attribute name is placed before the mapping pairs 13418c2ecf20Sopenharmony_ci * array. 13428c2ecf20Sopenharmony_ci */ 13438c2ecf20Sopenharmony_ci if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 13448c2ecf20Sopenharmony_ci le16_to_cpu( 13458c2ecf20Sopenharmony_ci a->data.non_resident.mapping_pairs_offset)))) { 13468c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Attribute name is placed after " 13478c2ecf20Sopenharmony_ci "the mapping pairs array."); 13488c2ecf20Sopenharmony_ci goto unm_err_out; 13498c2ecf20Sopenharmony_ci } 13508c2ecf20Sopenharmony_ci if (NInoCompressed(ni) || NInoSparse(ni)) { 13518c2ecf20Sopenharmony_ci if (NInoCompressed(ni) && a->data.non_resident. 13528c2ecf20Sopenharmony_ci compression_unit != 4) { 13538c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found non-standard " 13548c2ecf20Sopenharmony_ci "compression unit (%u instead " 13558c2ecf20Sopenharmony_ci "of 4). Cannot handle this.", 13568c2ecf20Sopenharmony_ci a->data.non_resident. 13578c2ecf20Sopenharmony_ci compression_unit); 13588c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 13598c2ecf20Sopenharmony_ci goto unm_err_out; 13608c2ecf20Sopenharmony_ci } 13618c2ecf20Sopenharmony_ci if (a->data.non_resident.compression_unit) { 13628c2ecf20Sopenharmony_ci ni->itype.compressed.block_size = 1U << 13638c2ecf20Sopenharmony_ci (a->data.non_resident. 13648c2ecf20Sopenharmony_ci compression_unit + 13658c2ecf20Sopenharmony_ci vol->cluster_size_bits); 13668c2ecf20Sopenharmony_ci ni->itype.compressed.block_size_bits = 13678c2ecf20Sopenharmony_ci ffs(ni->itype.compressed. 13688c2ecf20Sopenharmony_ci block_size) - 1; 13698c2ecf20Sopenharmony_ci ni->itype.compressed.block_clusters = 1U << 13708c2ecf20Sopenharmony_ci a->data.non_resident. 13718c2ecf20Sopenharmony_ci compression_unit; 13728c2ecf20Sopenharmony_ci } else { 13738c2ecf20Sopenharmony_ci ni->itype.compressed.block_size = 0; 13748c2ecf20Sopenharmony_ci ni->itype.compressed.block_size_bits = 0; 13758c2ecf20Sopenharmony_ci ni->itype.compressed.block_clusters = 0; 13768c2ecf20Sopenharmony_ci } 13778c2ecf20Sopenharmony_ci ni->itype.compressed.size = sle64_to_cpu( 13788c2ecf20Sopenharmony_ci a->data.non_resident.compressed_size); 13798c2ecf20Sopenharmony_ci } 13808c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 13818c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "First extent of attribute has " 13828c2ecf20Sopenharmony_ci "non-zero lowest_vcn."); 13838c2ecf20Sopenharmony_ci goto unm_err_out; 13848c2ecf20Sopenharmony_ci } 13858c2ecf20Sopenharmony_ci vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); 13868c2ecf20Sopenharmony_ci ni->initialized_size = sle64_to_cpu( 13878c2ecf20Sopenharmony_ci a->data.non_resident.initialized_size); 13888c2ecf20Sopenharmony_ci ni->allocated_size = sle64_to_cpu( 13898c2ecf20Sopenharmony_ci a->data.non_resident.allocated_size); 13908c2ecf20Sopenharmony_ci } 13918c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_normal_aops; 13928c2ecf20Sopenharmony_ci if (NInoMstProtected(ni)) 13938c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_mst_aops; 13948c2ecf20Sopenharmony_ci else if (NInoCompressed(ni)) 13958c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_compressed_aops; 13968c2ecf20Sopenharmony_ci if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT) 13978c2ecf20Sopenharmony_ci vi->i_blocks = ni->itype.compressed.size >> 9; 13988c2ecf20Sopenharmony_ci else 13998c2ecf20Sopenharmony_ci vi->i_blocks = ni->allocated_size >> 9; 14008c2ecf20Sopenharmony_ci /* 14018c2ecf20Sopenharmony_ci * Make sure the base inode does not go away and attach it to the 14028c2ecf20Sopenharmony_ci * attribute inode. 14038c2ecf20Sopenharmony_ci */ 14048c2ecf20Sopenharmony_ci igrab(base_vi); 14058c2ecf20Sopenharmony_ci ni->ext.base_ntfs_ino = base_ni; 14068c2ecf20Sopenharmony_ci ni->nr_extents = -1; 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 14098c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 14108c2ecf20Sopenharmony_ci 14118c2ecf20Sopenharmony_ci ntfs_debug("Done."); 14128c2ecf20Sopenharmony_ci return 0; 14138c2ecf20Sopenharmony_ci 14148c2ecf20Sopenharmony_ciunm_err_out: 14158c2ecf20Sopenharmony_ci if (!err) 14168c2ecf20Sopenharmony_ci err = -EIO; 14178c2ecf20Sopenharmony_ci if (ctx) 14188c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 14198c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 14208c2ecf20Sopenharmony_cierr_out: 14218c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Failed with error code %i while reading attribute " 14228c2ecf20Sopenharmony_ci "inode (mft_no 0x%lx, type 0x%x, name_len %i). " 14238c2ecf20Sopenharmony_ci "Marking corrupt inode and base inode 0x%lx as bad. " 14248c2ecf20Sopenharmony_ci "Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len, 14258c2ecf20Sopenharmony_ci base_vi->i_ino); 14268c2ecf20Sopenharmony_ci make_bad_inode(vi); 14278c2ecf20Sopenharmony_ci if (err != -ENOMEM) 14288c2ecf20Sopenharmony_ci NVolSetErrors(vol); 14298c2ecf20Sopenharmony_ci return err; 14308c2ecf20Sopenharmony_ci} 14318c2ecf20Sopenharmony_ci 14328c2ecf20Sopenharmony_ci/** 14338c2ecf20Sopenharmony_ci * ntfs_read_locked_index_inode - read an index inode from its base inode 14348c2ecf20Sopenharmony_ci * @base_vi: base inode 14358c2ecf20Sopenharmony_ci * @vi: index inode to read 14368c2ecf20Sopenharmony_ci * 14378c2ecf20Sopenharmony_ci * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the 14388c2ecf20Sopenharmony_ci * index inode described by @vi into memory from the base mft record described 14398c2ecf20Sopenharmony_ci * by @base_ni. 14408c2ecf20Sopenharmony_ci * 14418c2ecf20Sopenharmony_ci * ntfs_read_locked_index_inode() maps, pins and locks the base inode for 14428c2ecf20Sopenharmony_ci * reading and looks up the attributes relating to the index described by @vi 14438c2ecf20Sopenharmony_ci * before setting up the necessary fields in @vi as well as initializing the 14448c2ecf20Sopenharmony_ci * ntfs inode. 14458c2ecf20Sopenharmony_ci * 14468c2ecf20Sopenharmony_ci * Note, index inodes are essentially attribute inodes (NInoAttr() is true) 14478c2ecf20Sopenharmony_ci * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they 14488c2ecf20Sopenharmony_ci * are setup like directory inodes since directories are a special case of 14498c2ecf20Sopenharmony_ci * indices ao they need to be treated in much the same way. Most importantly, 14508c2ecf20Sopenharmony_ci * for small indices the index allocation attribute might not actually exist. 14518c2ecf20Sopenharmony_ci * However, the index root attribute always exists but this does not need to 14528c2ecf20Sopenharmony_ci * have an inode associated with it and this is why we define a new inode type 14538c2ecf20Sopenharmony_ci * index. Also, like for directories, we need to have an attribute inode for 14548c2ecf20Sopenharmony_ci * the bitmap attribute corresponding to the index allocation attribute and we 14558c2ecf20Sopenharmony_ci * can store this in the appropriate field of the inode, just like we do for 14568c2ecf20Sopenharmony_ci * normal directory inodes. 14578c2ecf20Sopenharmony_ci * 14588c2ecf20Sopenharmony_ci * Q: What locks are held when the function is called? 14598c2ecf20Sopenharmony_ci * A: i_state has I_NEW set, hence the inode is locked, also 14608c2ecf20Sopenharmony_ci * i_count is set to 1, so it is not going to go away 14618c2ecf20Sopenharmony_ci * 14628c2ecf20Sopenharmony_ci * Return 0 on success and -errno on error. In the error case, the inode will 14638c2ecf20Sopenharmony_ci * have had make_bad_inode() executed on it. 14648c2ecf20Sopenharmony_ci */ 14658c2ecf20Sopenharmony_cistatic int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi) 14668c2ecf20Sopenharmony_ci{ 14678c2ecf20Sopenharmony_ci loff_t bvi_size; 14688c2ecf20Sopenharmony_ci ntfs_volume *vol = NTFS_SB(vi->i_sb); 14698c2ecf20Sopenharmony_ci ntfs_inode *ni, *base_ni, *bni; 14708c2ecf20Sopenharmony_ci struct inode *bvi; 14718c2ecf20Sopenharmony_ci MFT_RECORD *m; 14728c2ecf20Sopenharmony_ci ATTR_RECORD *a; 14738c2ecf20Sopenharmony_ci ntfs_attr_search_ctx *ctx; 14748c2ecf20Sopenharmony_ci INDEX_ROOT *ir; 14758c2ecf20Sopenharmony_ci u8 *ir_end, *index_end; 14768c2ecf20Sopenharmony_ci int err = 0; 14778c2ecf20Sopenharmony_ci 14788c2ecf20Sopenharmony_ci ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); 14798c2ecf20Sopenharmony_ci ntfs_init_big_inode(vi); 14808c2ecf20Sopenharmony_ci ni = NTFS_I(vi); 14818c2ecf20Sopenharmony_ci base_ni = NTFS_I(base_vi); 14828c2ecf20Sopenharmony_ci /* Just mirror the values from the base inode. */ 14838c2ecf20Sopenharmony_ci vi->i_uid = base_vi->i_uid; 14848c2ecf20Sopenharmony_ci vi->i_gid = base_vi->i_gid; 14858c2ecf20Sopenharmony_ci set_nlink(vi, base_vi->i_nlink); 14868c2ecf20Sopenharmony_ci vi->i_mtime = base_vi->i_mtime; 14878c2ecf20Sopenharmony_ci vi->i_ctime = base_vi->i_ctime; 14888c2ecf20Sopenharmony_ci vi->i_atime = base_vi->i_atime; 14898c2ecf20Sopenharmony_ci vi->i_generation = ni->seq_no = base_ni->seq_no; 14908c2ecf20Sopenharmony_ci /* Set inode type to zero but preserve permissions. */ 14918c2ecf20Sopenharmony_ci vi->i_mode = base_vi->i_mode & ~S_IFMT; 14928c2ecf20Sopenharmony_ci /* Map the mft record for the base inode. */ 14938c2ecf20Sopenharmony_ci m = map_mft_record(base_ni); 14948c2ecf20Sopenharmony_ci if (IS_ERR(m)) { 14958c2ecf20Sopenharmony_ci err = PTR_ERR(m); 14968c2ecf20Sopenharmony_ci goto err_out; 14978c2ecf20Sopenharmony_ci } 14988c2ecf20Sopenharmony_ci ctx = ntfs_attr_get_search_ctx(base_ni, m); 14998c2ecf20Sopenharmony_ci if (!ctx) { 15008c2ecf20Sopenharmony_ci err = -ENOMEM; 15018c2ecf20Sopenharmony_ci goto unm_err_out; 15028c2ecf20Sopenharmony_ci } 15038c2ecf20Sopenharmony_ci /* Find the index root attribute. */ 15048c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len, 15058c2ecf20Sopenharmony_ci CASE_SENSITIVE, 0, NULL, 0, ctx); 15068c2ecf20Sopenharmony_ci if (unlikely(err)) { 15078c2ecf20Sopenharmony_ci if (err == -ENOENT) 15088c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is " 15098c2ecf20Sopenharmony_ci "missing."); 15108c2ecf20Sopenharmony_ci goto unm_err_out; 15118c2ecf20Sopenharmony_ci } 15128c2ecf20Sopenharmony_ci a = ctx->attr; 15138c2ecf20Sopenharmony_ci /* Set up the state. */ 15148c2ecf20Sopenharmony_ci if (unlikely(a->non_resident)) { 15158c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident."); 15168c2ecf20Sopenharmony_ci goto unm_err_out; 15178c2ecf20Sopenharmony_ci } 15188c2ecf20Sopenharmony_ci /* Ensure the attribute name is placed before the value. */ 15198c2ecf20Sopenharmony_ci if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 15208c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset)))) { 15218c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed " 15228c2ecf20Sopenharmony_ci "after the attribute value."); 15238c2ecf20Sopenharmony_ci goto unm_err_out; 15248c2ecf20Sopenharmony_ci } 15258c2ecf20Sopenharmony_ci /* 15268c2ecf20Sopenharmony_ci * Compressed/encrypted/sparse index root is not allowed, except for 15278c2ecf20Sopenharmony_ci * directories of course but those are not dealt with here. 15288c2ecf20Sopenharmony_ci */ 15298c2ecf20Sopenharmony_ci if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED | 15308c2ecf20Sopenharmony_ci ATTR_IS_SPARSE)) { 15318c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index " 15328c2ecf20Sopenharmony_ci "root attribute."); 15338c2ecf20Sopenharmony_ci goto unm_err_out; 15348c2ecf20Sopenharmony_ci } 15358c2ecf20Sopenharmony_ci ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset)); 15368c2ecf20Sopenharmony_ci ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length); 15378c2ecf20Sopenharmony_ci if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) { 15388c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt."); 15398c2ecf20Sopenharmony_ci goto unm_err_out; 15408c2ecf20Sopenharmony_ci } 15418c2ecf20Sopenharmony_ci index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); 15428c2ecf20Sopenharmony_ci if (index_end > ir_end) { 15438c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index is corrupt."); 15448c2ecf20Sopenharmony_ci goto unm_err_out; 15458c2ecf20Sopenharmony_ci } 15468c2ecf20Sopenharmony_ci if (ir->type) { 15478c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).", 15488c2ecf20Sopenharmony_ci le32_to_cpu(ir->type)); 15498c2ecf20Sopenharmony_ci goto unm_err_out; 15508c2ecf20Sopenharmony_ci } 15518c2ecf20Sopenharmony_ci ni->itype.index.collation_rule = ir->collation_rule; 15528c2ecf20Sopenharmony_ci ntfs_debug("Index collation rule is 0x%x.", 15538c2ecf20Sopenharmony_ci le32_to_cpu(ir->collation_rule)); 15548c2ecf20Sopenharmony_ci ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 15558c2ecf20Sopenharmony_ci if (!is_power_of_2(ni->itype.index.block_size)) { 15568c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index block size (%u) is not a power of " 15578c2ecf20Sopenharmony_ci "two.", ni->itype.index.block_size); 15588c2ecf20Sopenharmony_ci goto unm_err_out; 15598c2ecf20Sopenharmony_ci } 15608c2ecf20Sopenharmony_ci if (ni->itype.index.block_size > PAGE_SIZE) { 15618c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE " 15628c2ecf20Sopenharmony_ci "(%ld) is not supported. Sorry.", 15638c2ecf20Sopenharmony_ci ni->itype.index.block_size, PAGE_SIZE); 15648c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 15658c2ecf20Sopenharmony_ci goto unm_err_out; 15668c2ecf20Sopenharmony_ci } 15678c2ecf20Sopenharmony_ci if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 15688c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE " 15698c2ecf20Sopenharmony_ci "(%i) is not supported. Sorry.", 15708c2ecf20Sopenharmony_ci ni->itype.index.block_size, NTFS_BLOCK_SIZE); 15718c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 15728c2ecf20Sopenharmony_ci goto unm_err_out; 15738c2ecf20Sopenharmony_ci } 15748c2ecf20Sopenharmony_ci ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1; 15758c2ecf20Sopenharmony_ci /* Determine the size of a vcn in the index. */ 15768c2ecf20Sopenharmony_ci if (vol->cluster_size <= ni->itype.index.block_size) { 15778c2ecf20Sopenharmony_ci ni->itype.index.vcn_size = vol->cluster_size; 15788c2ecf20Sopenharmony_ci ni->itype.index.vcn_size_bits = vol->cluster_size_bits; 15798c2ecf20Sopenharmony_ci } else { 15808c2ecf20Sopenharmony_ci ni->itype.index.vcn_size = vol->sector_size; 15818c2ecf20Sopenharmony_ci ni->itype.index.vcn_size_bits = vol->sector_size_bits; 15828c2ecf20Sopenharmony_ci } 15838c2ecf20Sopenharmony_ci /* Check for presence of index allocation attribute. */ 15848c2ecf20Sopenharmony_ci if (!(ir->index.flags & LARGE_INDEX)) { 15858c2ecf20Sopenharmony_ci /* No index allocation. */ 15868c2ecf20Sopenharmony_ci vi->i_size = ni->initialized_size = ni->allocated_size = 0; 15878c2ecf20Sopenharmony_ci /* We are done with the mft record, so we release it. */ 15888c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 15898c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 15908c2ecf20Sopenharmony_ci m = NULL; 15918c2ecf20Sopenharmony_ci ctx = NULL; 15928c2ecf20Sopenharmony_ci goto skip_large_index_stuff; 15938c2ecf20Sopenharmony_ci } /* LARGE_INDEX: Index allocation present. Setup state. */ 15948c2ecf20Sopenharmony_ci NInoSetIndexAllocPresent(ni); 15958c2ecf20Sopenharmony_ci /* Find index allocation attribute. */ 15968c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 15978c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len, 15988c2ecf20Sopenharmony_ci CASE_SENSITIVE, 0, NULL, 0, ctx); 15998c2ecf20Sopenharmony_ci if (unlikely(err)) { 16008c2ecf20Sopenharmony_ci if (err == -ENOENT) 16018c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 16028c2ecf20Sopenharmony_ci "not present but $INDEX_ROOT " 16038c2ecf20Sopenharmony_ci "indicated it is."); 16048c2ecf20Sopenharmony_ci else 16058c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to lookup " 16068c2ecf20Sopenharmony_ci "$INDEX_ALLOCATION attribute."); 16078c2ecf20Sopenharmony_ci goto unm_err_out; 16088c2ecf20Sopenharmony_ci } 16098c2ecf20Sopenharmony_ci a = ctx->attr; 16108c2ecf20Sopenharmony_ci if (!a->non_resident) { 16118c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 16128c2ecf20Sopenharmony_ci "resident."); 16138c2ecf20Sopenharmony_ci goto unm_err_out; 16148c2ecf20Sopenharmony_ci } 16158c2ecf20Sopenharmony_ci /* 16168c2ecf20Sopenharmony_ci * Ensure the attribute name is placed before the mapping pairs array. 16178c2ecf20Sopenharmony_ci */ 16188c2ecf20Sopenharmony_ci if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 16198c2ecf20Sopenharmony_ci le16_to_cpu( 16208c2ecf20Sopenharmony_ci a->data.non_resident.mapping_pairs_offset)))) { 16218c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is " 16228c2ecf20Sopenharmony_ci "placed after the mapping pairs array."); 16238c2ecf20Sopenharmony_ci goto unm_err_out; 16248c2ecf20Sopenharmony_ci } 16258c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED) { 16268c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 16278c2ecf20Sopenharmony_ci "encrypted."); 16288c2ecf20Sopenharmony_ci goto unm_err_out; 16298c2ecf20Sopenharmony_ci } 16308c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_SPARSE) { 16318c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse."); 16328c2ecf20Sopenharmony_ci goto unm_err_out; 16338c2ecf20Sopenharmony_ci } 16348c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 16358c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 16368c2ecf20Sopenharmony_ci "compressed."); 16378c2ecf20Sopenharmony_ci goto unm_err_out; 16388c2ecf20Sopenharmony_ci } 16398c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 16408c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION " 16418c2ecf20Sopenharmony_ci "attribute has non zero lowest_vcn."); 16428c2ecf20Sopenharmony_ci goto unm_err_out; 16438c2ecf20Sopenharmony_ci } 16448c2ecf20Sopenharmony_ci vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); 16458c2ecf20Sopenharmony_ci ni->initialized_size = sle64_to_cpu( 16468c2ecf20Sopenharmony_ci a->data.non_resident.initialized_size); 16478c2ecf20Sopenharmony_ci ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size); 16488c2ecf20Sopenharmony_ci /* 16498c2ecf20Sopenharmony_ci * We are done with the mft record, so we release it. Otherwise 16508c2ecf20Sopenharmony_ci * we would deadlock in ntfs_attr_iget(). 16518c2ecf20Sopenharmony_ci */ 16528c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 16538c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 16548c2ecf20Sopenharmony_ci m = NULL; 16558c2ecf20Sopenharmony_ci ctx = NULL; 16568c2ecf20Sopenharmony_ci /* Get the index bitmap attribute inode. */ 16578c2ecf20Sopenharmony_ci bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len); 16588c2ecf20Sopenharmony_ci if (IS_ERR(bvi)) { 16598c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to get bitmap attribute."); 16608c2ecf20Sopenharmony_ci err = PTR_ERR(bvi); 16618c2ecf20Sopenharmony_ci goto unm_err_out; 16628c2ecf20Sopenharmony_ci } 16638c2ecf20Sopenharmony_ci bni = NTFS_I(bvi); 16648c2ecf20Sopenharmony_ci if (NInoCompressed(bni) || NInoEncrypted(bni) || 16658c2ecf20Sopenharmony_ci NInoSparse(bni)) { 16668c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or " 16678c2ecf20Sopenharmony_ci "encrypted and/or sparse."); 16688c2ecf20Sopenharmony_ci goto iput_unm_err_out; 16698c2ecf20Sopenharmony_ci } 16708c2ecf20Sopenharmony_ci /* Consistency check bitmap size vs. index allocation size. */ 16718c2ecf20Sopenharmony_ci bvi_size = i_size_read(bvi); 16728c2ecf20Sopenharmony_ci if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) { 16738c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for " 16748c2ecf20Sopenharmony_ci "index allocation (0x%llx).", bvi_size << 3, 16758c2ecf20Sopenharmony_ci vi->i_size); 16768c2ecf20Sopenharmony_ci goto iput_unm_err_out; 16778c2ecf20Sopenharmony_ci } 16788c2ecf20Sopenharmony_ci iput(bvi); 16798c2ecf20Sopenharmony_ciskip_large_index_stuff: 16808c2ecf20Sopenharmony_ci /* Setup the operations for this index inode. */ 16818c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_mst_aops; 16828c2ecf20Sopenharmony_ci vi->i_blocks = ni->allocated_size >> 9; 16838c2ecf20Sopenharmony_ci /* 16848c2ecf20Sopenharmony_ci * Make sure the base inode doesn't go away and attach it to the 16858c2ecf20Sopenharmony_ci * index inode. 16868c2ecf20Sopenharmony_ci */ 16878c2ecf20Sopenharmony_ci igrab(base_vi); 16888c2ecf20Sopenharmony_ci ni->ext.base_ntfs_ino = base_ni; 16898c2ecf20Sopenharmony_ci ni->nr_extents = -1; 16908c2ecf20Sopenharmony_ci 16918c2ecf20Sopenharmony_ci ntfs_debug("Done."); 16928c2ecf20Sopenharmony_ci return 0; 16938c2ecf20Sopenharmony_ciiput_unm_err_out: 16948c2ecf20Sopenharmony_ci iput(bvi); 16958c2ecf20Sopenharmony_ciunm_err_out: 16968c2ecf20Sopenharmony_ci if (!err) 16978c2ecf20Sopenharmony_ci err = -EIO; 16988c2ecf20Sopenharmony_ci if (ctx) 16998c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 17008c2ecf20Sopenharmony_ci if (m) 17018c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 17028c2ecf20Sopenharmony_cierr_out: 17038c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed with error code %i while reading index " 17048c2ecf20Sopenharmony_ci "inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino, 17058c2ecf20Sopenharmony_ci ni->name_len); 17068c2ecf20Sopenharmony_ci make_bad_inode(vi); 17078c2ecf20Sopenharmony_ci if (err != -EOPNOTSUPP && err != -ENOMEM) 17088c2ecf20Sopenharmony_ci NVolSetErrors(vol); 17098c2ecf20Sopenharmony_ci return err; 17108c2ecf20Sopenharmony_ci} 17118c2ecf20Sopenharmony_ci 17128c2ecf20Sopenharmony_ci/* 17138c2ecf20Sopenharmony_ci * The MFT inode has special locking, so teach the lock validator 17148c2ecf20Sopenharmony_ci * about this by splitting off the locking rules of the MFT from 17158c2ecf20Sopenharmony_ci * the locking rules of other inodes. The MFT inode can never be 17168c2ecf20Sopenharmony_ci * accessed from the VFS side (or even internally), only by the 17178c2ecf20Sopenharmony_ci * map_mft functions. 17188c2ecf20Sopenharmony_ci */ 17198c2ecf20Sopenharmony_cistatic struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key; 17208c2ecf20Sopenharmony_ci 17218c2ecf20Sopenharmony_ci/** 17228c2ecf20Sopenharmony_ci * ntfs_read_inode_mount - special read_inode for mount time use only 17238c2ecf20Sopenharmony_ci * @vi: inode to read 17248c2ecf20Sopenharmony_ci * 17258c2ecf20Sopenharmony_ci * Read inode FILE_MFT at mount time, only called with super_block lock 17268c2ecf20Sopenharmony_ci * held from within the read_super() code path. 17278c2ecf20Sopenharmony_ci * 17288c2ecf20Sopenharmony_ci * This function exists because when it is called the page cache for $MFT/$DATA 17298c2ecf20Sopenharmony_ci * is not initialized and hence we cannot get at the contents of mft records 17308c2ecf20Sopenharmony_ci * by calling map_mft_record*(). 17318c2ecf20Sopenharmony_ci * 17328c2ecf20Sopenharmony_ci * Further it needs to cope with the circular references problem, i.e. cannot 17338c2ecf20Sopenharmony_ci * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because 17348c2ecf20Sopenharmony_ci * we do not know where the other extent mft records are yet and again, because 17358c2ecf20Sopenharmony_ci * we cannot call map_mft_record*() yet. Obviously this applies only when an 17368c2ecf20Sopenharmony_ci * attribute list is actually present in $MFT inode. 17378c2ecf20Sopenharmony_ci * 17388c2ecf20Sopenharmony_ci * We solve these problems by starting with the $DATA attribute before anything 17398c2ecf20Sopenharmony_ci * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each 17408c2ecf20Sopenharmony_ci * extent is found, we ntfs_mapping_pairs_decompress() including the implied 17418c2ecf20Sopenharmony_ci * ntfs_runlists_merge(). Each step of the iteration necessarily provides 17428c2ecf20Sopenharmony_ci * sufficient information for the next step to complete. 17438c2ecf20Sopenharmony_ci * 17448c2ecf20Sopenharmony_ci * This should work but there are two possible pit falls (see inline comments 17458c2ecf20Sopenharmony_ci * below), but only time will tell if they are real pits or just smoke... 17468c2ecf20Sopenharmony_ci */ 17478c2ecf20Sopenharmony_ciint ntfs_read_inode_mount(struct inode *vi) 17488c2ecf20Sopenharmony_ci{ 17498c2ecf20Sopenharmony_ci VCN next_vcn, last_vcn, highest_vcn; 17508c2ecf20Sopenharmony_ci s64 block; 17518c2ecf20Sopenharmony_ci struct super_block *sb = vi->i_sb; 17528c2ecf20Sopenharmony_ci ntfs_volume *vol = NTFS_SB(sb); 17538c2ecf20Sopenharmony_ci struct buffer_head *bh; 17548c2ecf20Sopenharmony_ci ntfs_inode *ni; 17558c2ecf20Sopenharmony_ci MFT_RECORD *m = NULL; 17568c2ecf20Sopenharmony_ci ATTR_RECORD *a; 17578c2ecf20Sopenharmony_ci ntfs_attr_search_ctx *ctx; 17588c2ecf20Sopenharmony_ci unsigned int i, nr_blocks; 17598c2ecf20Sopenharmony_ci int err; 17608c2ecf20Sopenharmony_ci 17618c2ecf20Sopenharmony_ci ntfs_debug("Entering."); 17628c2ecf20Sopenharmony_ci 17638c2ecf20Sopenharmony_ci /* Initialize the ntfs specific part of @vi. */ 17648c2ecf20Sopenharmony_ci ntfs_init_big_inode(vi); 17658c2ecf20Sopenharmony_ci 17668c2ecf20Sopenharmony_ci ni = NTFS_I(vi); 17678c2ecf20Sopenharmony_ci 17688c2ecf20Sopenharmony_ci /* Setup the data attribute. It is special as it is mst protected. */ 17698c2ecf20Sopenharmony_ci NInoSetNonResident(ni); 17708c2ecf20Sopenharmony_ci NInoSetMstProtected(ni); 17718c2ecf20Sopenharmony_ci NInoSetSparseDisabled(ni); 17728c2ecf20Sopenharmony_ci ni->type = AT_DATA; 17738c2ecf20Sopenharmony_ci ni->name = NULL; 17748c2ecf20Sopenharmony_ci ni->name_len = 0; 17758c2ecf20Sopenharmony_ci /* 17768c2ecf20Sopenharmony_ci * This sets up our little cheat allowing us to reuse the async read io 17778c2ecf20Sopenharmony_ci * completion handler for directories. 17788c2ecf20Sopenharmony_ci */ 17798c2ecf20Sopenharmony_ci ni->itype.index.block_size = vol->mft_record_size; 17808c2ecf20Sopenharmony_ci ni->itype.index.block_size_bits = vol->mft_record_size_bits; 17818c2ecf20Sopenharmony_ci 17828c2ecf20Sopenharmony_ci /* Very important! Needed to be able to call map_mft_record*(). */ 17838c2ecf20Sopenharmony_ci vol->mft_ino = vi; 17848c2ecf20Sopenharmony_ci 17858c2ecf20Sopenharmony_ci /* Allocate enough memory to read the first mft record. */ 17868c2ecf20Sopenharmony_ci if (vol->mft_record_size > 64 * 1024) { 17878c2ecf20Sopenharmony_ci ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).", 17888c2ecf20Sopenharmony_ci vol->mft_record_size); 17898c2ecf20Sopenharmony_ci goto err_out; 17908c2ecf20Sopenharmony_ci } 17918c2ecf20Sopenharmony_ci i = vol->mft_record_size; 17928c2ecf20Sopenharmony_ci if (i < sb->s_blocksize) 17938c2ecf20Sopenharmony_ci i = sb->s_blocksize; 17948c2ecf20Sopenharmony_ci m = (MFT_RECORD*)ntfs_malloc_nofs(i); 17958c2ecf20Sopenharmony_ci if (!m) { 17968c2ecf20Sopenharmony_ci ntfs_error(sb, "Failed to allocate buffer for $MFT record 0."); 17978c2ecf20Sopenharmony_ci goto err_out; 17988c2ecf20Sopenharmony_ci } 17998c2ecf20Sopenharmony_ci 18008c2ecf20Sopenharmony_ci /* Determine the first block of the $MFT/$DATA attribute. */ 18018c2ecf20Sopenharmony_ci block = vol->mft_lcn << vol->cluster_size_bits >> 18028c2ecf20Sopenharmony_ci sb->s_blocksize_bits; 18038c2ecf20Sopenharmony_ci nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits; 18048c2ecf20Sopenharmony_ci if (!nr_blocks) 18058c2ecf20Sopenharmony_ci nr_blocks = 1; 18068c2ecf20Sopenharmony_ci 18078c2ecf20Sopenharmony_ci /* Load $MFT/$DATA's first mft record. */ 18088c2ecf20Sopenharmony_ci for (i = 0; i < nr_blocks; i++) { 18098c2ecf20Sopenharmony_ci bh = sb_bread(sb, block++); 18108c2ecf20Sopenharmony_ci if (!bh) { 18118c2ecf20Sopenharmony_ci ntfs_error(sb, "Device read failed."); 18128c2ecf20Sopenharmony_ci goto err_out; 18138c2ecf20Sopenharmony_ci } 18148c2ecf20Sopenharmony_ci memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data, 18158c2ecf20Sopenharmony_ci sb->s_blocksize); 18168c2ecf20Sopenharmony_ci brelse(bh); 18178c2ecf20Sopenharmony_ci } 18188c2ecf20Sopenharmony_ci 18198c2ecf20Sopenharmony_ci if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) { 18208c2ecf20Sopenharmony_ci ntfs_error(sb, "Incorrect mft record size %u in superblock, should be %u.", 18218c2ecf20Sopenharmony_ci le32_to_cpu(m->bytes_allocated), vol->mft_record_size); 18228c2ecf20Sopenharmony_ci goto err_out; 18238c2ecf20Sopenharmony_ci } 18248c2ecf20Sopenharmony_ci 18258c2ecf20Sopenharmony_ci /* Apply the mst fixups. */ 18268c2ecf20Sopenharmony_ci if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) { 18278c2ecf20Sopenharmony_ci /* FIXME: Try to use the $MFTMirr now. */ 18288c2ecf20Sopenharmony_ci ntfs_error(sb, "MST fixup failed. $MFT is corrupt."); 18298c2ecf20Sopenharmony_ci goto err_out; 18308c2ecf20Sopenharmony_ci } 18318c2ecf20Sopenharmony_ci 18328c2ecf20Sopenharmony_ci /* Sanity check offset to the first attribute */ 18338c2ecf20Sopenharmony_ci if (le16_to_cpu(m->attrs_offset) >= le32_to_cpu(m->bytes_allocated)) { 18348c2ecf20Sopenharmony_ci ntfs_error(sb, "Incorrect mft offset to the first attribute %u in superblock.", 18358c2ecf20Sopenharmony_ci le16_to_cpu(m->attrs_offset)); 18368c2ecf20Sopenharmony_ci goto err_out; 18378c2ecf20Sopenharmony_ci } 18388c2ecf20Sopenharmony_ci 18398c2ecf20Sopenharmony_ci /* Need this to sanity check attribute list references to $MFT. */ 18408c2ecf20Sopenharmony_ci vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 18418c2ecf20Sopenharmony_ci 18428c2ecf20Sopenharmony_ci /* Provides readpage() for map_mft_record(). */ 18438c2ecf20Sopenharmony_ci vi->i_mapping->a_ops = &ntfs_mst_aops; 18448c2ecf20Sopenharmony_ci 18458c2ecf20Sopenharmony_ci ctx = ntfs_attr_get_search_ctx(ni, m); 18468c2ecf20Sopenharmony_ci if (!ctx) { 18478c2ecf20Sopenharmony_ci err = -ENOMEM; 18488c2ecf20Sopenharmony_ci goto err_out; 18498c2ecf20Sopenharmony_ci } 18508c2ecf20Sopenharmony_ci 18518c2ecf20Sopenharmony_ci /* Find the attribute list attribute if present. */ 18528c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 18538c2ecf20Sopenharmony_ci if (err) { 18548c2ecf20Sopenharmony_ci if (unlikely(err != -ENOENT)) { 18558c2ecf20Sopenharmony_ci ntfs_error(sb, "Failed to lookup attribute list " 18568c2ecf20Sopenharmony_ci "attribute. You should run chkdsk."); 18578c2ecf20Sopenharmony_ci goto put_err_out; 18588c2ecf20Sopenharmony_ci } 18598c2ecf20Sopenharmony_ci } else /* if (!err) */ { 18608c2ecf20Sopenharmony_ci ATTR_LIST_ENTRY *al_entry, *next_al_entry; 18618c2ecf20Sopenharmony_ci u8 *al_end; 18628c2ecf20Sopenharmony_ci static const char *es = " Not allowed. $MFT is corrupt. " 18638c2ecf20Sopenharmony_ci "You should run chkdsk."; 18648c2ecf20Sopenharmony_ci 18658c2ecf20Sopenharmony_ci ntfs_debug("Attribute list attribute found in $MFT."); 18668c2ecf20Sopenharmony_ci NInoSetAttrList(ni); 18678c2ecf20Sopenharmony_ci a = ctx->attr; 18688c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK) { 18698c2ecf20Sopenharmony_ci ntfs_error(sb, "Attribute list attribute is " 18708c2ecf20Sopenharmony_ci "compressed.%s", es); 18718c2ecf20Sopenharmony_ci goto put_err_out; 18728c2ecf20Sopenharmony_ci } 18738c2ecf20Sopenharmony_ci if (a->flags & ATTR_IS_ENCRYPTED || 18748c2ecf20Sopenharmony_ci a->flags & ATTR_IS_SPARSE) { 18758c2ecf20Sopenharmony_ci if (a->non_resident) { 18768c2ecf20Sopenharmony_ci ntfs_error(sb, "Non-resident attribute list " 18778c2ecf20Sopenharmony_ci "attribute is encrypted/" 18788c2ecf20Sopenharmony_ci "sparse.%s", es); 18798c2ecf20Sopenharmony_ci goto put_err_out; 18808c2ecf20Sopenharmony_ci } 18818c2ecf20Sopenharmony_ci ntfs_warning(sb, "Resident attribute list attribute " 18828c2ecf20Sopenharmony_ci "in $MFT system file is marked " 18838c2ecf20Sopenharmony_ci "encrypted/sparse which is not true. " 18848c2ecf20Sopenharmony_ci "However, Windows allows this and " 18858c2ecf20Sopenharmony_ci "chkdsk does not detect or correct it " 18868c2ecf20Sopenharmony_ci "so we will just ignore the invalid " 18878c2ecf20Sopenharmony_ci "flags and pretend they are not set."); 18888c2ecf20Sopenharmony_ci } 18898c2ecf20Sopenharmony_ci /* Now allocate memory for the attribute list. */ 18908c2ecf20Sopenharmony_ci ni->attr_list_size = (u32)ntfs_attr_size(a); 18918c2ecf20Sopenharmony_ci if (!ni->attr_list_size) { 18928c2ecf20Sopenharmony_ci ntfs_error(sb, "Attr_list_size is zero"); 18938c2ecf20Sopenharmony_ci goto put_err_out; 18948c2ecf20Sopenharmony_ci } 18958c2ecf20Sopenharmony_ci ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 18968c2ecf20Sopenharmony_ci if (!ni->attr_list) { 18978c2ecf20Sopenharmony_ci ntfs_error(sb, "Not enough memory to allocate buffer " 18988c2ecf20Sopenharmony_ci "for attribute list."); 18998c2ecf20Sopenharmony_ci goto put_err_out; 19008c2ecf20Sopenharmony_ci } 19018c2ecf20Sopenharmony_ci if (a->non_resident) { 19028c2ecf20Sopenharmony_ci NInoSetAttrListNonResident(ni); 19038c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 19048c2ecf20Sopenharmony_ci ntfs_error(sb, "Attribute list has non zero " 19058c2ecf20Sopenharmony_ci "lowest_vcn. $MFT is corrupt. " 19068c2ecf20Sopenharmony_ci "You should run chkdsk."); 19078c2ecf20Sopenharmony_ci goto put_err_out; 19088c2ecf20Sopenharmony_ci } 19098c2ecf20Sopenharmony_ci /* Setup the runlist. */ 19108c2ecf20Sopenharmony_ci ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 19118c2ecf20Sopenharmony_ci a, NULL); 19128c2ecf20Sopenharmony_ci if (IS_ERR(ni->attr_list_rl.rl)) { 19138c2ecf20Sopenharmony_ci err = PTR_ERR(ni->attr_list_rl.rl); 19148c2ecf20Sopenharmony_ci ni->attr_list_rl.rl = NULL; 19158c2ecf20Sopenharmony_ci ntfs_error(sb, "Mapping pairs decompression " 19168c2ecf20Sopenharmony_ci "failed with error code %i.", 19178c2ecf20Sopenharmony_ci -err); 19188c2ecf20Sopenharmony_ci goto put_err_out; 19198c2ecf20Sopenharmony_ci } 19208c2ecf20Sopenharmony_ci /* Now load the attribute list. */ 19218c2ecf20Sopenharmony_ci if ((err = load_attribute_list(vol, &ni->attr_list_rl, 19228c2ecf20Sopenharmony_ci ni->attr_list, ni->attr_list_size, 19238c2ecf20Sopenharmony_ci sle64_to_cpu(a->data. 19248c2ecf20Sopenharmony_ci non_resident.initialized_size)))) { 19258c2ecf20Sopenharmony_ci ntfs_error(sb, "Failed to load attribute list " 19268c2ecf20Sopenharmony_ci "attribute with error code %i.", 19278c2ecf20Sopenharmony_ci -err); 19288c2ecf20Sopenharmony_ci goto put_err_out; 19298c2ecf20Sopenharmony_ci } 19308c2ecf20Sopenharmony_ci } else /* if (!ctx.attr->non_resident) */ { 19318c2ecf20Sopenharmony_ci if ((u8*)a + le16_to_cpu( 19328c2ecf20Sopenharmony_ci a->data.resident.value_offset) + 19338c2ecf20Sopenharmony_ci le32_to_cpu( 19348c2ecf20Sopenharmony_ci a->data.resident.value_length) > 19358c2ecf20Sopenharmony_ci (u8*)ctx->mrec + vol->mft_record_size) { 19368c2ecf20Sopenharmony_ci ntfs_error(sb, "Corrupt attribute list " 19378c2ecf20Sopenharmony_ci "attribute."); 19388c2ecf20Sopenharmony_ci goto put_err_out; 19398c2ecf20Sopenharmony_ci } 19408c2ecf20Sopenharmony_ci /* Now copy the attribute list. */ 19418c2ecf20Sopenharmony_ci memcpy(ni->attr_list, (u8*)a + le16_to_cpu( 19428c2ecf20Sopenharmony_ci a->data.resident.value_offset), 19438c2ecf20Sopenharmony_ci le32_to_cpu( 19448c2ecf20Sopenharmony_ci a->data.resident.value_length)); 19458c2ecf20Sopenharmony_ci } 19468c2ecf20Sopenharmony_ci /* The attribute list is now setup in memory. */ 19478c2ecf20Sopenharmony_ci /* 19488c2ecf20Sopenharmony_ci * FIXME: I don't know if this case is actually possible. 19498c2ecf20Sopenharmony_ci * According to logic it is not possible but I have seen too 19508c2ecf20Sopenharmony_ci * many weird things in MS software to rely on logic... Thus we 19518c2ecf20Sopenharmony_ci * perform a manual search and make sure the first $MFT/$DATA 19528c2ecf20Sopenharmony_ci * extent is in the base inode. If it is not we abort with an 19538c2ecf20Sopenharmony_ci * error and if we ever see a report of this error we will need 19548c2ecf20Sopenharmony_ci * to do some magic in order to have the necessary mft record 19558c2ecf20Sopenharmony_ci * loaded and in the right place in the page cache. But 19568c2ecf20Sopenharmony_ci * hopefully logic will prevail and this never happens... 19578c2ecf20Sopenharmony_ci */ 19588c2ecf20Sopenharmony_ci al_entry = (ATTR_LIST_ENTRY*)ni->attr_list; 19598c2ecf20Sopenharmony_ci al_end = (u8*)al_entry + ni->attr_list_size; 19608c2ecf20Sopenharmony_ci for (;; al_entry = next_al_entry) { 19618c2ecf20Sopenharmony_ci /* Out of bounds check. */ 19628c2ecf20Sopenharmony_ci if ((u8*)al_entry < ni->attr_list || 19638c2ecf20Sopenharmony_ci (u8*)al_entry > al_end) 19648c2ecf20Sopenharmony_ci goto em_put_err_out; 19658c2ecf20Sopenharmony_ci /* Catch the end of the attribute list. */ 19668c2ecf20Sopenharmony_ci if ((u8*)al_entry == al_end) 19678c2ecf20Sopenharmony_ci goto em_put_err_out; 19688c2ecf20Sopenharmony_ci if (!al_entry->length) 19698c2ecf20Sopenharmony_ci goto em_put_err_out; 19708c2ecf20Sopenharmony_ci if ((u8*)al_entry + 6 > al_end || (u8*)al_entry + 19718c2ecf20Sopenharmony_ci le16_to_cpu(al_entry->length) > al_end) 19728c2ecf20Sopenharmony_ci goto em_put_err_out; 19738c2ecf20Sopenharmony_ci next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry + 19748c2ecf20Sopenharmony_ci le16_to_cpu(al_entry->length)); 19758c2ecf20Sopenharmony_ci if (le32_to_cpu(al_entry->type) > le32_to_cpu(AT_DATA)) 19768c2ecf20Sopenharmony_ci goto em_put_err_out; 19778c2ecf20Sopenharmony_ci if (AT_DATA != al_entry->type) 19788c2ecf20Sopenharmony_ci continue; 19798c2ecf20Sopenharmony_ci /* We want an unnamed attribute. */ 19808c2ecf20Sopenharmony_ci if (al_entry->name_length) 19818c2ecf20Sopenharmony_ci goto em_put_err_out; 19828c2ecf20Sopenharmony_ci /* Want the first entry, i.e. lowest_vcn == 0. */ 19838c2ecf20Sopenharmony_ci if (al_entry->lowest_vcn) 19848c2ecf20Sopenharmony_ci goto em_put_err_out; 19858c2ecf20Sopenharmony_ci /* First entry has to be in the base mft record. */ 19868c2ecf20Sopenharmony_ci if (MREF_LE(al_entry->mft_reference) != vi->i_ino) { 19878c2ecf20Sopenharmony_ci /* MFT references do not match, logic fails. */ 19888c2ecf20Sopenharmony_ci ntfs_error(sb, "BUG: The first $DATA extent " 19898c2ecf20Sopenharmony_ci "of $MFT is not in the base " 19908c2ecf20Sopenharmony_ci "mft record. Please report " 19918c2ecf20Sopenharmony_ci "you saw this message to " 19928c2ecf20Sopenharmony_ci "linux-ntfs-dev@lists." 19938c2ecf20Sopenharmony_ci "sourceforge.net"); 19948c2ecf20Sopenharmony_ci goto put_err_out; 19958c2ecf20Sopenharmony_ci } else { 19968c2ecf20Sopenharmony_ci /* Sequence numbers must match. */ 19978c2ecf20Sopenharmony_ci if (MSEQNO_LE(al_entry->mft_reference) != 19988c2ecf20Sopenharmony_ci ni->seq_no) 19998c2ecf20Sopenharmony_ci goto em_put_err_out; 20008c2ecf20Sopenharmony_ci /* Got it. All is ok. We can stop now. */ 20018c2ecf20Sopenharmony_ci break; 20028c2ecf20Sopenharmony_ci } 20038c2ecf20Sopenharmony_ci } 20048c2ecf20Sopenharmony_ci } 20058c2ecf20Sopenharmony_ci 20068c2ecf20Sopenharmony_ci ntfs_attr_reinit_search_ctx(ctx); 20078c2ecf20Sopenharmony_ci 20088c2ecf20Sopenharmony_ci /* Now load all attribute extents. */ 20098c2ecf20Sopenharmony_ci a = NULL; 20108c2ecf20Sopenharmony_ci next_vcn = last_vcn = highest_vcn = 0; 20118c2ecf20Sopenharmony_ci while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, 20128c2ecf20Sopenharmony_ci ctx))) { 20138c2ecf20Sopenharmony_ci runlist_element *nrl; 20148c2ecf20Sopenharmony_ci 20158c2ecf20Sopenharmony_ci /* Cache the current attribute. */ 20168c2ecf20Sopenharmony_ci a = ctx->attr; 20178c2ecf20Sopenharmony_ci /* $MFT must be non-resident. */ 20188c2ecf20Sopenharmony_ci if (!a->non_resident) { 20198c2ecf20Sopenharmony_ci ntfs_error(sb, "$MFT must be non-resident but a " 20208c2ecf20Sopenharmony_ci "resident extent was found. $MFT is " 20218c2ecf20Sopenharmony_ci "corrupt. Run chkdsk."); 20228c2ecf20Sopenharmony_ci goto put_err_out; 20238c2ecf20Sopenharmony_ci } 20248c2ecf20Sopenharmony_ci /* $MFT must be uncompressed and unencrypted. */ 20258c2ecf20Sopenharmony_ci if (a->flags & ATTR_COMPRESSION_MASK || 20268c2ecf20Sopenharmony_ci a->flags & ATTR_IS_ENCRYPTED || 20278c2ecf20Sopenharmony_ci a->flags & ATTR_IS_SPARSE) { 20288c2ecf20Sopenharmony_ci ntfs_error(sb, "$MFT must be uncompressed, " 20298c2ecf20Sopenharmony_ci "non-sparse, and unencrypted but a " 20308c2ecf20Sopenharmony_ci "compressed/sparse/encrypted extent " 20318c2ecf20Sopenharmony_ci "was found. $MFT is corrupt. Run " 20328c2ecf20Sopenharmony_ci "chkdsk."); 20338c2ecf20Sopenharmony_ci goto put_err_out; 20348c2ecf20Sopenharmony_ci } 20358c2ecf20Sopenharmony_ci /* 20368c2ecf20Sopenharmony_ci * Decompress the mapping pairs array of this extent and merge 20378c2ecf20Sopenharmony_ci * the result into the existing runlist. No need for locking 20388c2ecf20Sopenharmony_ci * as we have exclusive access to the inode at this time and we 20398c2ecf20Sopenharmony_ci * are a mount in progress task, too. 20408c2ecf20Sopenharmony_ci */ 20418c2ecf20Sopenharmony_ci nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl); 20428c2ecf20Sopenharmony_ci if (IS_ERR(nrl)) { 20438c2ecf20Sopenharmony_ci ntfs_error(sb, "ntfs_mapping_pairs_decompress() " 20448c2ecf20Sopenharmony_ci "failed with error code %ld. $MFT is " 20458c2ecf20Sopenharmony_ci "corrupt.", PTR_ERR(nrl)); 20468c2ecf20Sopenharmony_ci goto put_err_out; 20478c2ecf20Sopenharmony_ci } 20488c2ecf20Sopenharmony_ci ni->runlist.rl = nrl; 20498c2ecf20Sopenharmony_ci 20508c2ecf20Sopenharmony_ci /* Are we in the first extent? */ 20518c2ecf20Sopenharmony_ci if (!next_vcn) { 20528c2ecf20Sopenharmony_ci if (a->data.non_resident.lowest_vcn) { 20538c2ecf20Sopenharmony_ci ntfs_error(sb, "First extent of $DATA " 20548c2ecf20Sopenharmony_ci "attribute has non zero " 20558c2ecf20Sopenharmony_ci "lowest_vcn. $MFT is corrupt. " 20568c2ecf20Sopenharmony_ci "You should run chkdsk."); 20578c2ecf20Sopenharmony_ci goto put_err_out; 20588c2ecf20Sopenharmony_ci } 20598c2ecf20Sopenharmony_ci /* Get the last vcn in the $DATA attribute. */ 20608c2ecf20Sopenharmony_ci last_vcn = sle64_to_cpu( 20618c2ecf20Sopenharmony_ci a->data.non_resident.allocated_size) 20628c2ecf20Sopenharmony_ci >> vol->cluster_size_bits; 20638c2ecf20Sopenharmony_ci /* Fill in the inode size. */ 20648c2ecf20Sopenharmony_ci vi->i_size = sle64_to_cpu( 20658c2ecf20Sopenharmony_ci a->data.non_resident.data_size); 20668c2ecf20Sopenharmony_ci ni->initialized_size = sle64_to_cpu( 20678c2ecf20Sopenharmony_ci a->data.non_resident.initialized_size); 20688c2ecf20Sopenharmony_ci ni->allocated_size = sle64_to_cpu( 20698c2ecf20Sopenharmony_ci a->data.non_resident.allocated_size); 20708c2ecf20Sopenharmony_ci /* 20718c2ecf20Sopenharmony_ci * Verify the number of mft records does not exceed 20728c2ecf20Sopenharmony_ci * 2^32 - 1. 20738c2ecf20Sopenharmony_ci */ 20748c2ecf20Sopenharmony_ci if ((vi->i_size >> vol->mft_record_size_bits) >= 20758c2ecf20Sopenharmony_ci (1ULL << 32)) { 20768c2ecf20Sopenharmony_ci ntfs_error(sb, "$MFT is too big! Aborting."); 20778c2ecf20Sopenharmony_ci goto put_err_out; 20788c2ecf20Sopenharmony_ci } 20798c2ecf20Sopenharmony_ci /* 20808c2ecf20Sopenharmony_ci * We have got the first extent of the runlist for 20818c2ecf20Sopenharmony_ci * $MFT which means it is now relatively safe to call 20828c2ecf20Sopenharmony_ci * the normal ntfs_read_inode() function. 20838c2ecf20Sopenharmony_ci * Complete reading the inode, this will actually 20848c2ecf20Sopenharmony_ci * re-read the mft record for $MFT, this time entering 20858c2ecf20Sopenharmony_ci * it into the page cache with which we complete the 20868c2ecf20Sopenharmony_ci * kick start of the volume. It should be safe to do 20878c2ecf20Sopenharmony_ci * this now as the first extent of $MFT/$DATA is 20888c2ecf20Sopenharmony_ci * already known and we would hope that we don't need 20898c2ecf20Sopenharmony_ci * further extents in order to find the other 20908c2ecf20Sopenharmony_ci * attributes belonging to $MFT. Only time will tell if 20918c2ecf20Sopenharmony_ci * this is really the case. If not we will have to play 20928c2ecf20Sopenharmony_ci * magic at this point, possibly duplicating a lot of 20938c2ecf20Sopenharmony_ci * ntfs_read_inode() at this point. We will need to 20948c2ecf20Sopenharmony_ci * ensure we do enough of its work to be able to call 20958c2ecf20Sopenharmony_ci * ntfs_read_inode() on extents of $MFT/$DATA. But lets 20968c2ecf20Sopenharmony_ci * hope this never happens... 20978c2ecf20Sopenharmony_ci */ 20988c2ecf20Sopenharmony_ci ntfs_read_locked_inode(vi); 20998c2ecf20Sopenharmony_ci if (is_bad_inode(vi)) { 21008c2ecf20Sopenharmony_ci ntfs_error(sb, "ntfs_read_inode() of $MFT " 21018c2ecf20Sopenharmony_ci "failed. BUG or corrupt $MFT. " 21028c2ecf20Sopenharmony_ci "Run chkdsk and if no errors " 21038c2ecf20Sopenharmony_ci "are found, please report you " 21048c2ecf20Sopenharmony_ci "saw this message to " 21058c2ecf20Sopenharmony_ci "linux-ntfs-dev@lists." 21068c2ecf20Sopenharmony_ci "sourceforge.net"); 21078c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 21088c2ecf20Sopenharmony_ci /* Revert to the safe super operations. */ 21098c2ecf20Sopenharmony_ci ntfs_free(m); 21108c2ecf20Sopenharmony_ci return -1; 21118c2ecf20Sopenharmony_ci } 21128c2ecf20Sopenharmony_ci /* 21138c2ecf20Sopenharmony_ci * Re-initialize some specifics about $MFT's inode as 21148c2ecf20Sopenharmony_ci * ntfs_read_inode() will have set up the default ones. 21158c2ecf20Sopenharmony_ci */ 21168c2ecf20Sopenharmony_ci /* Set uid and gid to root. */ 21178c2ecf20Sopenharmony_ci vi->i_uid = GLOBAL_ROOT_UID; 21188c2ecf20Sopenharmony_ci vi->i_gid = GLOBAL_ROOT_GID; 21198c2ecf20Sopenharmony_ci /* Regular file. No access for anyone. */ 21208c2ecf20Sopenharmony_ci vi->i_mode = S_IFREG; 21218c2ecf20Sopenharmony_ci /* No VFS initiated operations allowed for $MFT. */ 21228c2ecf20Sopenharmony_ci vi->i_op = &ntfs_empty_inode_ops; 21238c2ecf20Sopenharmony_ci vi->i_fop = &ntfs_empty_file_ops; 21248c2ecf20Sopenharmony_ci } 21258c2ecf20Sopenharmony_ci 21268c2ecf20Sopenharmony_ci /* Get the lowest vcn for the next extent. */ 21278c2ecf20Sopenharmony_ci highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn); 21288c2ecf20Sopenharmony_ci next_vcn = highest_vcn + 1; 21298c2ecf20Sopenharmony_ci 21308c2ecf20Sopenharmony_ci /* Only one extent or error, which we catch below. */ 21318c2ecf20Sopenharmony_ci if (next_vcn <= 0) 21328c2ecf20Sopenharmony_ci break; 21338c2ecf20Sopenharmony_ci 21348c2ecf20Sopenharmony_ci /* Avoid endless loops due to corruption. */ 21358c2ecf20Sopenharmony_ci if (next_vcn < sle64_to_cpu( 21368c2ecf20Sopenharmony_ci a->data.non_resident.lowest_vcn)) { 21378c2ecf20Sopenharmony_ci ntfs_error(sb, "$MFT has corrupt attribute list " 21388c2ecf20Sopenharmony_ci "attribute. Run chkdsk."); 21398c2ecf20Sopenharmony_ci goto put_err_out; 21408c2ecf20Sopenharmony_ci } 21418c2ecf20Sopenharmony_ci } 21428c2ecf20Sopenharmony_ci if (err != -ENOENT) { 21438c2ecf20Sopenharmony_ci ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. " 21448c2ecf20Sopenharmony_ci "$MFT is corrupt. Run chkdsk."); 21458c2ecf20Sopenharmony_ci goto put_err_out; 21468c2ecf20Sopenharmony_ci } 21478c2ecf20Sopenharmony_ci if (!a) { 21488c2ecf20Sopenharmony_ci ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is " 21498c2ecf20Sopenharmony_ci "corrupt. Run chkdsk."); 21508c2ecf20Sopenharmony_ci goto put_err_out; 21518c2ecf20Sopenharmony_ci } 21528c2ecf20Sopenharmony_ci if (highest_vcn && highest_vcn != last_vcn - 1) { 21538c2ecf20Sopenharmony_ci ntfs_error(sb, "Failed to load the complete runlist for " 21548c2ecf20Sopenharmony_ci "$MFT/$DATA. Driver bug or corrupt $MFT. " 21558c2ecf20Sopenharmony_ci "Run chkdsk."); 21568c2ecf20Sopenharmony_ci ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx", 21578c2ecf20Sopenharmony_ci (unsigned long long)highest_vcn, 21588c2ecf20Sopenharmony_ci (unsigned long long)last_vcn - 1); 21598c2ecf20Sopenharmony_ci goto put_err_out; 21608c2ecf20Sopenharmony_ci } 21618c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 21628c2ecf20Sopenharmony_ci ntfs_debug("Done."); 21638c2ecf20Sopenharmony_ci ntfs_free(m); 21648c2ecf20Sopenharmony_ci 21658c2ecf20Sopenharmony_ci /* 21668c2ecf20Sopenharmony_ci * Split the locking rules of the MFT inode from the 21678c2ecf20Sopenharmony_ci * locking rules of other inodes: 21688c2ecf20Sopenharmony_ci */ 21698c2ecf20Sopenharmony_ci lockdep_set_class(&ni->runlist.lock, &mft_ni_runlist_lock_key); 21708c2ecf20Sopenharmony_ci lockdep_set_class(&ni->mrec_lock, &mft_ni_mrec_lock_key); 21718c2ecf20Sopenharmony_ci 21728c2ecf20Sopenharmony_ci return 0; 21738c2ecf20Sopenharmony_ci 21748c2ecf20Sopenharmony_ciem_put_err_out: 21758c2ecf20Sopenharmony_ci ntfs_error(sb, "Couldn't find first extent of $DATA attribute in " 21768c2ecf20Sopenharmony_ci "attribute list. $MFT is corrupt. Run chkdsk."); 21778c2ecf20Sopenharmony_ciput_err_out: 21788c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 21798c2ecf20Sopenharmony_cierr_out: 21808c2ecf20Sopenharmony_ci ntfs_error(sb, "Failed. Marking inode as bad."); 21818c2ecf20Sopenharmony_ci make_bad_inode(vi); 21828c2ecf20Sopenharmony_ci ntfs_free(m); 21838c2ecf20Sopenharmony_ci return -1; 21848c2ecf20Sopenharmony_ci} 21858c2ecf20Sopenharmony_ci 21868c2ecf20Sopenharmony_cistatic void __ntfs_clear_inode(ntfs_inode *ni) 21878c2ecf20Sopenharmony_ci{ 21888c2ecf20Sopenharmony_ci /* Free all alocated memory. */ 21898c2ecf20Sopenharmony_ci down_write(&ni->runlist.lock); 21908c2ecf20Sopenharmony_ci if (ni->runlist.rl) { 21918c2ecf20Sopenharmony_ci ntfs_free(ni->runlist.rl); 21928c2ecf20Sopenharmony_ci ni->runlist.rl = NULL; 21938c2ecf20Sopenharmony_ci } 21948c2ecf20Sopenharmony_ci up_write(&ni->runlist.lock); 21958c2ecf20Sopenharmony_ci 21968c2ecf20Sopenharmony_ci if (ni->attr_list) { 21978c2ecf20Sopenharmony_ci ntfs_free(ni->attr_list); 21988c2ecf20Sopenharmony_ci ni->attr_list = NULL; 21998c2ecf20Sopenharmony_ci } 22008c2ecf20Sopenharmony_ci 22018c2ecf20Sopenharmony_ci down_write(&ni->attr_list_rl.lock); 22028c2ecf20Sopenharmony_ci if (ni->attr_list_rl.rl) { 22038c2ecf20Sopenharmony_ci ntfs_free(ni->attr_list_rl.rl); 22048c2ecf20Sopenharmony_ci ni->attr_list_rl.rl = NULL; 22058c2ecf20Sopenharmony_ci } 22068c2ecf20Sopenharmony_ci up_write(&ni->attr_list_rl.lock); 22078c2ecf20Sopenharmony_ci 22088c2ecf20Sopenharmony_ci if (ni->name_len && ni->name != I30) { 22098c2ecf20Sopenharmony_ci /* Catch bugs... */ 22108c2ecf20Sopenharmony_ci BUG_ON(!ni->name); 22118c2ecf20Sopenharmony_ci kfree(ni->name); 22128c2ecf20Sopenharmony_ci } 22138c2ecf20Sopenharmony_ci} 22148c2ecf20Sopenharmony_ci 22158c2ecf20Sopenharmony_civoid ntfs_clear_extent_inode(ntfs_inode *ni) 22168c2ecf20Sopenharmony_ci{ 22178c2ecf20Sopenharmony_ci ntfs_debug("Entering for inode 0x%lx.", ni->mft_no); 22188c2ecf20Sopenharmony_ci 22198c2ecf20Sopenharmony_ci BUG_ON(NInoAttr(ni)); 22208c2ecf20Sopenharmony_ci BUG_ON(ni->nr_extents != -1); 22218c2ecf20Sopenharmony_ci 22228c2ecf20Sopenharmony_ci#ifdef NTFS_RW 22238c2ecf20Sopenharmony_ci if (NInoDirty(ni)) { 22248c2ecf20Sopenharmony_ci if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino))) 22258c2ecf20Sopenharmony_ci ntfs_error(ni->vol->sb, "Clearing dirty extent inode! " 22268c2ecf20Sopenharmony_ci "Losing data! This is a BUG!!!"); 22278c2ecf20Sopenharmony_ci // FIXME: Do something!!! 22288c2ecf20Sopenharmony_ci } 22298c2ecf20Sopenharmony_ci#endif /* NTFS_RW */ 22308c2ecf20Sopenharmony_ci 22318c2ecf20Sopenharmony_ci __ntfs_clear_inode(ni); 22328c2ecf20Sopenharmony_ci 22338c2ecf20Sopenharmony_ci /* Bye, bye... */ 22348c2ecf20Sopenharmony_ci ntfs_destroy_extent_inode(ni); 22358c2ecf20Sopenharmony_ci} 22368c2ecf20Sopenharmony_ci 22378c2ecf20Sopenharmony_ci/** 22388c2ecf20Sopenharmony_ci * ntfs_evict_big_inode - clean up the ntfs specific part of an inode 22398c2ecf20Sopenharmony_ci * @vi: vfs inode pending annihilation 22408c2ecf20Sopenharmony_ci * 22418c2ecf20Sopenharmony_ci * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode() 22428c2ecf20Sopenharmony_ci * is called, which deallocates all memory belonging to the NTFS specific part 22438c2ecf20Sopenharmony_ci * of the inode and returns. 22448c2ecf20Sopenharmony_ci * 22458c2ecf20Sopenharmony_ci * If the MFT record is dirty, we commit it before doing anything else. 22468c2ecf20Sopenharmony_ci */ 22478c2ecf20Sopenharmony_civoid ntfs_evict_big_inode(struct inode *vi) 22488c2ecf20Sopenharmony_ci{ 22498c2ecf20Sopenharmony_ci ntfs_inode *ni = NTFS_I(vi); 22508c2ecf20Sopenharmony_ci 22518c2ecf20Sopenharmony_ci truncate_inode_pages_final(&vi->i_data); 22528c2ecf20Sopenharmony_ci clear_inode(vi); 22538c2ecf20Sopenharmony_ci 22548c2ecf20Sopenharmony_ci#ifdef NTFS_RW 22558c2ecf20Sopenharmony_ci if (NInoDirty(ni)) { 22568c2ecf20Sopenharmony_ci bool was_bad = (is_bad_inode(vi)); 22578c2ecf20Sopenharmony_ci 22588c2ecf20Sopenharmony_ci /* Committing the inode also commits all extent inodes. */ 22598c2ecf20Sopenharmony_ci ntfs_commit_inode(vi); 22608c2ecf20Sopenharmony_ci 22618c2ecf20Sopenharmony_ci if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) { 22628c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to commit dirty inode " 22638c2ecf20Sopenharmony_ci "0x%lx. Losing data!", vi->i_ino); 22648c2ecf20Sopenharmony_ci // FIXME: Do something!!! 22658c2ecf20Sopenharmony_ci } 22668c2ecf20Sopenharmony_ci } 22678c2ecf20Sopenharmony_ci#endif /* NTFS_RW */ 22688c2ecf20Sopenharmony_ci 22698c2ecf20Sopenharmony_ci /* No need to lock at this stage as no one else has a reference. */ 22708c2ecf20Sopenharmony_ci if (ni->nr_extents > 0) { 22718c2ecf20Sopenharmony_ci int i; 22728c2ecf20Sopenharmony_ci 22738c2ecf20Sopenharmony_ci for (i = 0; i < ni->nr_extents; i++) 22748c2ecf20Sopenharmony_ci ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]); 22758c2ecf20Sopenharmony_ci kfree(ni->ext.extent_ntfs_inos); 22768c2ecf20Sopenharmony_ci } 22778c2ecf20Sopenharmony_ci 22788c2ecf20Sopenharmony_ci __ntfs_clear_inode(ni); 22798c2ecf20Sopenharmony_ci 22808c2ecf20Sopenharmony_ci if (NInoAttr(ni)) { 22818c2ecf20Sopenharmony_ci /* Release the base inode if we are holding it. */ 22828c2ecf20Sopenharmony_ci if (ni->nr_extents == -1) { 22838c2ecf20Sopenharmony_ci iput(VFS_I(ni->ext.base_ntfs_ino)); 22848c2ecf20Sopenharmony_ci ni->nr_extents = 0; 22858c2ecf20Sopenharmony_ci ni->ext.base_ntfs_ino = NULL; 22868c2ecf20Sopenharmony_ci } 22878c2ecf20Sopenharmony_ci } 22888c2ecf20Sopenharmony_ci BUG_ON(ni->page); 22898c2ecf20Sopenharmony_ci if (!atomic_dec_and_test(&ni->count)) 22908c2ecf20Sopenharmony_ci BUG(); 22918c2ecf20Sopenharmony_ci return; 22928c2ecf20Sopenharmony_ci} 22938c2ecf20Sopenharmony_ci 22948c2ecf20Sopenharmony_ci/** 22958c2ecf20Sopenharmony_ci * ntfs_show_options - show mount options in /proc/mounts 22968c2ecf20Sopenharmony_ci * @sf: seq_file in which to write our mount options 22978c2ecf20Sopenharmony_ci * @root: root of the mounted tree whose mount options to display 22988c2ecf20Sopenharmony_ci * 22998c2ecf20Sopenharmony_ci * Called by the VFS once for each mounted ntfs volume when someone reads 23008c2ecf20Sopenharmony_ci * /proc/mounts in order to display the NTFS specific mount options of each 23018c2ecf20Sopenharmony_ci * mount. The mount options of fs specified by @root are written to the seq file 23028c2ecf20Sopenharmony_ci * @sf and success is returned. 23038c2ecf20Sopenharmony_ci */ 23048c2ecf20Sopenharmony_ciint ntfs_show_options(struct seq_file *sf, struct dentry *root) 23058c2ecf20Sopenharmony_ci{ 23068c2ecf20Sopenharmony_ci ntfs_volume *vol = NTFS_SB(root->d_sb); 23078c2ecf20Sopenharmony_ci int i; 23088c2ecf20Sopenharmony_ci 23098c2ecf20Sopenharmony_ci seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid)); 23108c2ecf20Sopenharmony_ci seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid)); 23118c2ecf20Sopenharmony_ci if (vol->fmask == vol->dmask) 23128c2ecf20Sopenharmony_ci seq_printf(sf, ",umask=0%o", vol->fmask); 23138c2ecf20Sopenharmony_ci else { 23148c2ecf20Sopenharmony_ci seq_printf(sf, ",fmask=0%o", vol->fmask); 23158c2ecf20Sopenharmony_ci seq_printf(sf, ",dmask=0%o", vol->dmask); 23168c2ecf20Sopenharmony_ci } 23178c2ecf20Sopenharmony_ci seq_printf(sf, ",nls=%s", vol->nls_map->charset); 23188c2ecf20Sopenharmony_ci if (NVolCaseSensitive(vol)) 23198c2ecf20Sopenharmony_ci seq_printf(sf, ",case_sensitive"); 23208c2ecf20Sopenharmony_ci if (NVolShowSystemFiles(vol)) 23218c2ecf20Sopenharmony_ci seq_printf(sf, ",show_sys_files"); 23228c2ecf20Sopenharmony_ci if (!NVolSparseEnabled(vol)) 23238c2ecf20Sopenharmony_ci seq_printf(sf, ",disable_sparse"); 23248c2ecf20Sopenharmony_ci for (i = 0; on_errors_arr[i].val; i++) { 23258c2ecf20Sopenharmony_ci if (on_errors_arr[i].val & vol->on_errors) 23268c2ecf20Sopenharmony_ci seq_printf(sf, ",errors=%s", on_errors_arr[i].str); 23278c2ecf20Sopenharmony_ci } 23288c2ecf20Sopenharmony_ci seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier); 23298c2ecf20Sopenharmony_ci return 0; 23308c2ecf20Sopenharmony_ci} 23318c2ecf20Sopenharmony_ci 23328c2ecf20Sopenharmony_ci#ifdef NTFS_RW 23338c2ecf20Sopenharmony_ci 23348c2ecf20Sopenharmony_cistatic const char *es = " Leaving inconsistent metadata. Unmount and run " 23358c2ecf20Sopenharmony_ci "chkdsk."; 23368c2ecf20Sopenharmony_ci 23378c2ecf20Sopenharmony_ci/** 23388c2ecf20Sopenharmony_ci * ntfs_truncate - called when the i_size of an ntfs inode is changed 23398c2ecf20Sopenharmony_ci * @vi: inode for which the i_size was changed 23408c2ecf20Sopenharmony_ci * 23418c2ecf20Sopenharmony_ci * We only support i_size changes for normal files at present, i.e. not 23428c2ecf20Sopenharmony_ci * compressed and not encrypted. This is enforced in ntfs_setattr(), see 23438c2ecf20Sopenharmony_ci * below. 23448c2ecf20Sopenharmony_ci * 23458c2ecf20Sopenharmony_ci * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and 23468c2ecf20Sopenharmony_ci * that the change is allowed. 23478c2ecf20Sopenharmony_ci * 23488c2ecf20Sopenharmony_ci * This implies for us that @vi is a file inode rather than a directory, index, 23498c2ecf20Sopenharmony_ci * or attribute inode as well as that @vi is a base inode. 23508c2ecf20Sopenharmony_ci * 23518c2ecf20Sopenharmony_ci * Returns 0 on success or -errno on error. 23528c2ecf20Sopenharmony_ci * 23538c2ecf20Sopenharmony_ci * Called with ->i_mutex held. 23548c2ecf20Sopenharmony_ci */ 23558c2ecf20Sopenharmony_ciint ntfs_truncate(struct inode *vi) 23568c2ecf20Sopenharmony_ci{ 23578c2ecf20Sopenharmony_ci s64 new_size, old_size, nr_freed, new_alloc_size, old_alloc_size; 23588c2ecf20Sopenharmony_ci VCN highest_vcn; 23598c2ecf20Sopenharmony_ci unsigned long flags; 23608c2ecf20Sopenharmony_ci ntfs_inode *base_ni, *ni = NTFS_I(vi); 23618c2ecf20Sopenharmony_ci ntfs_volume *vol = ni->vol; 23628c2ecf20Sopenharmony_ci ntfs_attr_search_ctx *ctx; 23638c2ecf20Sopenharmony_ci MFT_RECORD *m; 23648c2ecf20Sopenharmony_ci ATTR_RECORD *a; 23658c2ecf20Sopenharmony_ci const char *te = " Leaving file length out of sync with i_size."; 23668c2ecf20Sopenharmony_ci int err, mp_size, size_change, alloc_change; 23678c2ecf20Sopenharmony_ci u32 attr_len; 23688c2ecf20Sopenharmony_ci 23698c2ecf20Sopenharmony_ci ntfs_debug("Entering for inode 0x%lx.", vi->i_ino); 23708c2ecf20Sopenharmony_ci BUG_ON(NInoAttr(ni)); 23718c2ecf20Sopenharmony_ci BUG_ON(S_ISDIR(vi->i_mode)); 23728c2ecf20Sopenharmony_ci BUG_ON(NInoMstProtected(ni)); 23738c2ecf20Sopenharmony_ci BUG_ON(ni->nr_extents < 0); 23748c2ecf20Sopenharmony_ciretry_truncate: 23758c2ecf20Sopenharmony_ci /* 23768c2ecf20Sopenharmony_ci * Lock the runlist for writing and map the mft record to ensure it is 23778c2ecf20Sopenharmony_ci * safe to mess with the attribute runlist and sizes. 23788c2ecf20Sopenharmony_ci */ 23798c2ecf20Sopenharmony_ci down_write(&ni->runlist.lock); 23808c2ecf20Sopenharmony_ci if (!NInoAttr(ni)) 23818c2ecf20Sopenharmony_ci base_ni = ni; 23828c2ecf20Sopenharmony_ci else 23838c2ecf20Sopenharmony_ci base_ni = ni->ext.base_ntfs_ino; 23848c2ecf20Sopenharmony_ci m = map_mft_record(base_ni); 23858c2ecf20Sopenharmony_ci if (IS_ERR(m)) { 23868c2ecf20Sopenharmony_ci err = PTR_ERR(m); 23878c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx " 23888c2ecf20Sopenharmony_ci "(error code %d).%s", vi->i_ino, err, te); 23898c2ecf20Sopenharmony_ci ctx = NULL; 23908c2ecf20Sopenharmony_ci m = NULL; 23918c2ecf20Sopenharmony_ci goto old_bad_out; 23928c2ecf20Sopenharmony_ci } 23938c2ecf20Sopenharmony_ci ctx = ntfs_attr_get_search_ctx(base_ni, m); 23948c2ecf20Sopenharmony_ci if (unlikely(!ctx)) { 23958c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to allocate a search context for " 23968c2ecf20Sopenharmony_ci "inode 0x%lx (not enough memory).%s", 23978c2ecf20Sopenharmony_ci vi->i_ino, te); 23988c2ecf20Sopenharmony_ci err = -ENOMEM; 23998c2ecf20Sopenharmony_ci goto old_bad_out; 24008c2ecf20Sopenharmony_ci } 24018c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 24028c2ecf20Sopenharmony_ci CASE_SENSITIVE, 0, NULL, 0, ctx); 24038c2ecf20Sopenharmony_ci if (unlikely(err)) { 24048c2ecf20Sopenharmony_ci if (err == -ENOENT) { 24058c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Open attribute is missing from " 24068c2ecf20Sopenharmony_ci "mft record. Inode 0x%lx is corrupt. " 24078c2ecf20Sopenharmony_ci "Run chkdsk.%s", vi->i_ino, te); 24088c2ecf20Sopenharmony_ci err = -EIO; 24098c2ecf20Sopenharmony_ci } else 24108c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed to lookup attribute in " 24118c2ecf20Sopenharmony_ci "inode 0x%lx (error code %d).%s", 24128c2ecf20Sopenharmony_ci vi->i_ino, err, te); 24138c2ecf20Sopenharmony_ci goto old_bad_out; 24148c2ecf20Sopenharmony_ci } 24158c2ecf20Sopenharmony_ci m = ctx->mrec; 24168c2ecf20Sopenharmony_ci a = ctx->attr; 24178c2ecf20Sopenharmony_ci /* 24188c2ecf20Sopenharmony_ci * The i_size of the vfs inode is the new size for the attribute value. 24198c2ecf20Sopenharmony_ci */ 24208c2ecf20Sopenharmony_ci new_size = i_size_read(vi); 24218c2ecf20Sopenharmony_ci /* The current size of the attribute value is the old size. */ 24228c2ecf20Sopenharmony_ci old_size = ntfs_attr_size(a); 24238c2ecf20Sopenharmony_ci /* Calculate the new allocated size. */ 24248c2ecf20Sopenharmony_ci if (NInoNonResident(ni)) 24258c2ecf20Sopenharmony_ci new_alloc_size = (new_size + vol->cluster_size - 1) & 24268c2ecf20Sopenharmony_ci ~(s64)vol->cluster_size_mask; 24278c2ecf20Sopenharmony_ci else 24288c2ecf20Sopenharmony_ci new_alloc_size = (new_size + 7) & ~7; 24298c2ecf20Sopenharmony_ci /* The current allocated size is the old allocated size. */ 24308c2ecf20Sopenharmony_ci read_lock_irqsave(&ni->size_lock, flags); 24318c2ecf20Sopenharmony_ci old_alloc_size = ni->allocated_size; 24328c2ecf20Sopenharmony_ci read_unlock_irqrestore(&ni->size_lock, flags); 24338c2ecf20Sopenharmony_ci /* 24348c2ecf20Sopenharmony_ci * The change in the file size. This will be 0 if no change, >0 if the 24358c2ecf20Sopenharmony_ci * size is growing, and <0 if the size is shrinking. 24368c2ecf20Sopenharmony_ci */ 24378c2ecf20Sopenharmony_ci size_change = -1; 24388c2ecf20Sopenharmony_ci if (new_size - old_size >= 0) { 24398c2ecf20Sopenharmony_ci size_change = 1; 24408c2ecf20Sopenharmony_ci if (new_size == old_size) 24418c2ecf20Sopenharmony_ci size_change = 0; 24428c2ecf20Sopenharmony_ci } 24438c2ecf20Sopenharmony_ci /* As above for the allocated size. */ 24448c2ecf20Sopenharmony_ci alloc_change = -1; 24458c2ecf20Sopenharmony_ci if (new_alloc_size - old_alloc_size >= 0) { 24468c2ecf20Sopenharmony_ci alloc_change = 1; 24478c2ecf20Sopenharmony_ci if (new_alloc_size == old_alloc_size) 24488c2ecf20Sopenharmony_ci alloc_change = 0; 24498c2ecf20Sopenharmony_ci } 24508c2ecf20Sopenharmony_ci /* 24518c2ecf20Sopenharmony_ci * If neither the size nor the allocation are being changed there is 24528c2ecf20Sopenharmony_ci * nothing to do. 24538c2ecf20Sopenharmony_ci */ 24548c2ecf20Sopenharmony_ci if (!size_change && !alloc_change) 24558c2ecf20Sopenharmony_ci goto unm_done; 24568c2ecf20Sopenharmony_ci /* If the size is changing, check if new size is allowed in $AttrDef. */ 24578c2ecf20Sopenharmony_ci if (size_change) { 24588c2ecf20Sopenharmony_ci err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); 24598c2ecf20Sopenharmony_ci if (unlikely(err)) { 24608c2ecf20Sopenharmony_ci if (err == -ERANGE) { 24618c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Truncate would cause the " 24628c2ecf20Sopenharmony_ci "inode 0x%lx to %simum size " 24638c2ecf20Sopenharmony_ci "for its attribute type " 24648c2ecf20Sopenharmony_ci "(0x%x). Aborting truncate.", 24658c2ecf20Sopenharmony_ci vi->i_ino, 24668c2ecf20Sopenharmony_ci new_size > old_size ? "exceed " 24678c2ecf20Sopenharmony_ci "the max" : "go under the min", 24688c2ecf20Sopenharmony_ci le32_to_cpu(ni->type)); 24698c2ecf20Sopenharmony_ci err = -EFBIG; 24708c2ecf20Sopenharmony_ci } else { 24718c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Inode 0x%lx has unknown " 24728c2ecf20Sopenharmony_ci "attribute type 0x%x. " 24738c2ecf20Sopenharmony_ci "Aborting truncate.", 24748c2ecf20Sopenharmony_ci vi->i_ino, 24758c2ecf20Sopenharmony_ci le32_to_cpu(ni->type)); 24768c2ecf20Sopenharmony_ci err = -EIO; 24778c2ecf20Sopenharmony_ci } 24788c2ecf20Sopenharmony_ci /* Reset the vfs inode size to the old size. */ 24798c2ecf20Sopenharmony_ci i_size_write(vi, old_size); 24808c2ecf20Sopenharmony_ci goto err_out; 24818c2ecf20Sopenharmony_ci } 24828c2ecf20Sopenharmony_ci } 24838c2ecf20Sopenharmony_ci if (NInoCompressed(ni) || NInoEncrypted(ni)) { 24848c2ecf20Sopenharmony_ci ntfs_warning(vi->i_sb, "Changes in inode size are not " 24858c2ecf20Sopenharmony_ci "supported yet for %s files, ignoring.", 24868c2ecf20Sopenharmony_ci NInoCompressed(ni) ? "compressed" : 24878c2ecf20Sopenharmony_ci "encrypted"); 24888c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 24898c2ecf20Sopenharmony_ci goto bad_out; 24908c2ecf20Sopenharmony_ci } 24918c2ecf20Sopenharmony_ci if (a->non_resident) 24928c2ecf20Sopenharmony_ci goto do_non_resident_truncate; 24938c2ecf20Sopenharmony_ci BUG_ON(NInoNonResident(ni)); 24948c2ecf20Sopenharmony_ci /* Resize the attribute record to best fit the new attribute size. */ 24958c2ecf20Sopenharmony_ci if (new_size < vol->mft_record_size && 24968c2ecf20Sopenharmony_ci !ntfs_resident_attr_value_resize(m, a, new_size)) { 24978c2ecf20Sopenharmony_ci /* The resize succeeded! */ 24988c2ecf20Sopenharmony_ci flush_dcache_mft_record_page(ctx->ntfs_ino); 24998c2ecf20Sopenharmony_ci mark_mft_record_dirty(ctx->ntfs_ino); 25008c2ecf20Sopenharmony_ci write_lock_irqsave(&ni->size_lock, flags); 25018c2ecf20Sopenharmony_ci /* Update the sizes in the ntfs inode and all is done. */ 25028c2ecf20Sopenharmony_ci ni->allocated_size = le32_to_cpu(a->length) - 25038c2ecf20Sopenharmony_ci le16_to_cpu(a->data.resident.value_offset); 25048c2ecf20Sopenharmony_ci /* 25058c2ecf20Sopenharmony_ci * Note ntfs_resident_attr_value_resize() has already done any 25068c2ecf20Sopenharmony_ci * necessary data clearing in the attribute record. When the 25078c2ecf20Sopenharmony_ci * file is being shrunk vmtruncate() will already have cleared 25088c2ecf20Sopenharmony_ci * the top part of the last partial page, i.e. since this is 25098c2ecf20Sopenharmony_ci * the resident case this is the page with index 0. However, 25108c2ecf20Sopenharmony_ci * when the file is being expanded, the page cache page data 25118c2ecf20Sopenharmony_ci * between the old data_size, i.e. old_size, and the new_size 25128c2ecf20Sopenharmony_ci * has not been zeroed. Fortunately, we do not need to zero it 25138c2ecf20Sopenharmony_ci * either since on one hand it will either already be zero due 25148c2ecf20Sopenharmony_ci * to both readpage and writepage clearing partial page data 25158c2ecf20Sopenharmony_ci * beyond i_size in which case there is nothing to do or in the 25168c2ecf20Sopenharmony_ci * case of the file being mmap()ped at the same time, POSIX 25178c2ecf20Sopenharmony_ci * specifies that the behaviour is unspecified thus we do not 25188c2ecf20Sopenharmony_ci * have to do anything. This means that in our implementation 25198c2ecf20Sopenharmony_ci * in the rare case that the file is mmap()ped and a write 25208c2ecf20Sopenharmony_ci * occurred into the mmap()ped region just beyond the file size 25218c2ecf20Sopenharmony_ci * and writepage has not yet been called to write out the page 25228c2ecf20Sopenharmony_ci * (which would clear the area beyond the file size) and we now 25238c2ecf20Sopenharmony_ci * extend the file size to incorporate this dirty region 25248c2ecf20Sopenharmony_ci * outside the file size, a write of the page would result in 25258c2ecf20Sopenharmony_ci * this data being written to disk instead of being cleared. 25268c2ecf20Sopenharmony_ci * Given both POSIX and the Linux mmap(2) man page specify that 25278c2ecf20Sopenharmony_ci * this corner case is undefined, we choose to leave it like 25288c2ecf20Sopenharmony_ci * that as this is much simpler for us as we cannot lock the 25298c2ecf20Sopenharmony_ci * relevant page now since we are holding too many ntfs locks 25308c2ecf20Sopenharmony_ci * which would result in a lock reversal deadlock. 25318c2ecf20Sopenharmony_ci */ 25328c2ecf20Sopenharmony_ci ni->initialized_size = new_size; 25338c2ecf20Sopenharmony_ci write_unlock_irqrestore(&ni->size_lock, flags); 25348c2ecf20Sopenharmony_ci goto unm_done; 25358c2ecf20Sopenharmony_ci } 25368c2ecf20Sopenharmony_ci /* If the above resize failed, this must be an attribute extension. */ 25378c2ecf20Sopenharmony_ci BUG_ON(size_change < 0); 25388c2ecf20Sopenharmony_ci /* 25398c2ecf20Sopenharmony_ci * We have to drop all the locks so we can call 25408c2ecf20Sopenharmony_ci * ntfs_attr_make_non_resident(). This could be optimised by try- 25418c2ecf20Sopenharmony_ci * locking the first page cache page and only if that fails dropping 25428c2ecf20Sopenharmony_ci * the locks, locking the page, and redoing all the locking and 25438c2ecf20Sopenharmony_ci * lookups. While this would be a huge optimisation, it is not worth 25448c2ecf20Sopenharmony_ci * it as this is definitely a slow code path as it only ever can happen 25458c2ecf20Sopenharmony_ci * once for any given file. 25468c2ecf20Sopenharmony_ci */ 25478c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 25488c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 25498c2ecf20Sopenharmony_ci up_write(&ni->runlist.lock); 25508c2ecf20Sopenharmony_ci /* 25518c2ecf20Sopenharmony_ci * Not enough space in the mft record, try to make the attribute 25528c2ecf20Sopenharmony_ci * non-resident and if successful restart the truncation process. 25538c2ecf20Sopenharmony_ci */ 25548c2ecf20Sopenharmony_ci err = ntfs_attr_make_non_resident(ni, old_size); 25558c2ecf20Sopenharmony_ci if (likely(!err)) 25568c2ecf20Sopenharmony_ci goto retry_truncate; 25578c2ecf20Sopenharmony_ci /* 25588c2ecf20Sopenharmony_ci * Could not make non-resident. If this is due to this not being 25598c2ecf20Sopenharmony_ci * permitted for this attribute type or there not being enough space, 25608c2ecf20Sopenharmony_ci * try to make other attributes non-resident. Otherwise fail. 25618c2ecf20Sopenharmony_ci */ 25628c2ecf20Sopenharmony_ci if (unlikely(err != -EPERM && err != -ENOSPC)) { 25638c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, attribute " 25648c2ecf20Sopenharmony_ci "type 0x%x, because the conversion from " 25658c2ecf20Sopenharmony_ci "resident to non-resident attribute failed " 25668c2ecf20Sopenharmony_ci "with error code %i.", vi->i_ino, 25678c2ecf20Sopenharmony_ci (unsigned)le32_to_cpu(ni->type), err); 25688c2ecf20Sopenharmony_ci if (err != -ENOMEM) 25698c2ecf20Sopenharmony_ci err = -EIO; 25708c2ecf20Sopenharmony_ci goto conv_err_out; 25718c2ecf20Sopenharmony_ci } 25728c2ecf20Sopenharmony_ci /* TODO: Not implemented from here, abort. */ 25738c2ecf20Sopenharmony_ci if (err == -ENOSPC) 25748c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Not enough space in the mft record/on " 25758c2ecf20Sopenharmony_ci "disk for the non-resident attribute value. " 25768c2ecf20Sopenharmony_ci "This case is not implemented yet."); 25778c2ecf20Sopenharmony_ci else /* if (err == -EPERM) */ 25788c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "This attribute type may not be " 25798c2ecf20Sopenharmony_ci "non-resident. This case is not implemented " 25808c2ecf20Sopenharmony_ci "yet."); 25818c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 25828c2ecf20Sopenharmony_ci goto conv_err_out; 25838c2ecf20Sopenharmony_ci#if 0 25848c2ecf20Sopenharmony_ci // TODO: Attempt to make other attributes non-resident. 25858c2ecf20Sopenharmony_ci if (!err) 25868c2ecf20Sopenharmony_ci goto do_resident_extend; 25878c2ecf20Sopenharmony_ci /* 25888c2ecf20Sopenharmony_ci * Both the attribute list attribute and the standard information 25898c2ecf20Sopenharmony_ci * attribute must remain in the base inode. Thus, if this is one of 25908c2ecf20Sopenharmony_ci * these attributes, we have to try to move other attributes out into 25918c2ecf20Sopenharmony_ci * extent mft records instead. 25928c2ecf20Sopenharmony_ci */ 25938c2ecf20Sopenharmony_ci if (ni->type == AT_ATTRIBUTE_LIST || 25948c2ecf20Sopenharmony_ci ni->type == AT_STANDARD_INFORMATION) { 25958c2ecf20Sopenharmony_ci // TODO: Attempt to move other attributes into extent mft 25968c2ecf20Sopenharmony_ci // records. 25978c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 25988c2ecf20Sopenharmony_ci if (!err) 25998c2ecf20Sopenharmony_ci goto do_resident_extend; 26008c2ecf20Sopenharmony_ci goto err_out; 26018c2ecf20Sopenharmony_ci } 26028c2ecf20Sopenharmony_ci // TODO: Attempt to move this attribute to an extent mft record, but 26038c2ecf20Sopenharmony_ci // only if it is not already the only attribute in an mft record in 26048c2ecf20Sopenharmony_ci // which case there would be nothing to gain. 26058c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 26068c2ecf20Sopenharmony_ci if (!err) 26078c2ecf20Sopenharmony_ci goto do_resident_extend; 26088c2ecf20Sopenharmony_ci /* There is nothing we can do to make enough space. )-: */ 26098c2ecf20Sopenharmony_ci goto err_out; 26108c2ecf20Sopenharmony_ci#endif 26118c2ecf20Sopenharmony_cido_non_resident_truncate: 26128c2ecf20Sopenharmony_ci BUG_ON(!NInoNonResident(ni)); 26138c2ecf20Sopenharmony_ci if (alloc_change < 0) { 26148c2ecf20Sopenharmony_ci highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn); 26158c2ecf20Sopenharmony_ci if (highest_vcn > 0 && 26168c2ecf20Sopenharmony_ci old_alloc_size >> vol->cluster_size_bits > 26178c2ecf20Sopenharmony_ci highest_vcn + 1) { 26188c2ecf20Sopenharmony_ci /* 26198c2ecf20Sopenharmony_ci * This attribute has multiple extents. Not yet 26208c2ecf20Sopenharmony_ci * supported. 26218c2ecf20Sopenharmony_ci */ 26228c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, " 26238c2ecf20Sopenharmony_ci "attribute type 0x%x, because the " 26248c2ecf20Sopenharmony_ci "attribute is highly fragmented (it " 26258c2ecf20Sopenharmony_ci "consists of multiple extents) and " 26268c2ecf20Sopenharmony_ci "this case is not implemented yet.", 26278c2ecf20Sopenharmony_ci vi->i_ino, 26288c2ecf20Sopenharmony_ci (unsigned)le32_to_cpu(ni->type)); 26298c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 26308c2ecf20Sopenharmony_ci goto bad_out; 26318c2ecf20Sopenharmony_ci } 26328c2ecf20Sopenharmony_ci } 26338c2ecf20Sopenharmony_ci /* 26348c2ecf20Sopenharmony_ci * If the size is shrinking, need to reduce the initialized_size and 26358c2ecf20Sopenharmony_ci * the data_size before reducing the allocation. 26368c2ecf20Sopenharmony_ci */ 26378c2ecf20Sopenharmony_ci if (size_change < 0) { 26388c2ecf20Sopenharmony_ci /* 26398c2ecf20Sopenharmony_ci * Make the valid size smaller (i_size is already up-to-date). 26408c2ecf20Sopenharmony_ci */ 26418c2ecf20Sopenharmony_ci write_lock_irqsave(&ni->size_lock, flags); 26428c2ecf20Sopenharmony_ci if (new_size < ni->initialized_size) { 26438c2ecf20Sopenharmony_ci ni->initialized_size = new_size; 26448c2ecf20Sopenharmony_ci a->data.non_resident.initialized_size = 26458c2ecf20Sopenharmony_ci cpu_to_sle64(new_size); 26468c2ecf20Sopenharmony_ci } 26478c2ecf20Sopenharmony_ci a->data.non_resident.data_size = cpu_to_sle64(new_size); 26488c2ecf20Sopenharmony_ci write_unlock_irqrestore(&ni->size_lock, flags); 26498c2ecf20Sopenharmony_ci flush_dcache_mft_record_page(ctx->ntfs_ino); 26508c2ecf20Sopenharmony_ci mark_mft_record_dirty(ctx->ntfs_ino); 26518c2ecf20Sopenharmony_ci /* If the allocated size is not changing, we are done. */ 26528c2ecf20Sopenharmony_ci if (!alloc_change) 26538c2ecf20Sopenharmony_ci goto unm_done; 26548c2ecf20Sopenharmony_ci /* 26558c2ecf20Sopenharmony_ci * If the size is shrinking it makes no sense for the 26568c2ecf20Sopenharmony_ci * allocation to be growing. 26578c2ecf20Sopenharmony_ci */ 26588c2ecf20Sopenharmony_ci BUG_ON(alloc_change > 0); 26598c2ecf20Sopenharmony_ci } else /* if (size_change >= 0) */ { 26608c2ecf20Sopenharmony_ci /* 26618c2ecf20Sopenharmony_ci * The file size is growing or staying the same but the 26628c2ecf20Sopenharmony_ci * allocation can be shrinking, growing or staying the same. 26638c2ecf20Sopenharmony_ci */ 26648c2ecf20Sopenharmony_ci if (alloc_change > 0) { 26658c2ecf20Sopenharmony_ci /* 26668c2ecf20Sopenharmony_ci * We need to extend the allocation and possibly update 26678c2ecf20Sopenharmony_ci * the data size. If we are updating the data size, 26688c2ecf20Sopenharmony_ci * since we are not touching the initialized_size we do 26698c2ecf20Sopenharmony_ci * not need to worry about the actual data on disk. 26708c2ecf20Sopenharmony_ci * And as far as the page cache is concerned, there 26718c2ecf20Sopenharmony_ci * will be no pages beyond the old data size and any 26728c2ecf20Sopenharmony_ci * partial region in the last page between the old and 26738c2ecf20Sopenharmony_ci * new data size (or the end of the page if the new 26748c2ecf20Sopenharmony_ci * data size is outside the page) does not need to be 26758c2ecf20Sopenharmony_ci * modified as explained above for the resident 26768c2ecf20Sopenharmony_ci * attribute truncate case. To do this, we simply drop 26778c2ecf20Sopenharmony_ci * the locks we hold and leave all the work to our 26788c2ecf20Sopenharmony_ci * friendly helper ntfs_attr_extend_allocation(). 26798c2ecf20Sopenharmony_ci */ 26808c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 26818c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 26828c2ecf20Sopenharmony_ci up_write(&ni->runlist.lock); 26838c2ecf20Sopenharmony_ci err = ntfs_attr_extend_allocation(ni, new_size, 26848c2ecf20Sopenharmony_ci size_change > 0 ? new_size : -1, -1); 26858c2ecf20Sopenharmony_ci /* 26868c2ecf20Sopenharmony_ci * ntfs_attr_extend_allocation() will have done error 26878c2ecf20Sopenharmony_ci * output already. 26888c2ecf20Sopenharmony_ci */ 26898c2ecf20Sopenharmony_ci goto done; 26908c2ecf20Sopenharmony_ci } 26918c2ecf20Sopenharmony_ci if (!alloc_change) 26928c2ecf20Sopenharmony_ci goto alloc_done; 26938c2ecf20Sopenharmony_ci } 26948c2ecf20Sopenharmony_ci /* alloc_change < 0 */ 26958c2ecf20Sopenharmony_ci /* Free the clusters. */ 26968c2ecf20Sopenharmony_ci nr_freed = ntfs_cluster_free(ni, new_alloc_size >> 26978c2ecf20Sopenharmony_ci vol->cluster_size_bits, -1, ctx); 26988c2ecf20Sopenharmony_ci m = ctx->mrec; 26998c2ecf20Sopenharmony_ci a = ctx->attr; 27008c2ecf20Sopenharmony_ci if (unlikely(nr_freed < 0)) { 27018c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Failed to release cluster(s) (error code " 27028c2ecf20Sopenharmony_ci "%lli). Unmount and run chkdsk to recover " 27038c2ecf20Sopenharmony_ci "the lost cluster(s).", (long long)nr_freed); 27048c2ecf20Sopenharmony_ci NVolSetErrors(vol); 27058c2ecf20Sopenharmony_ci nr_freed = 0; 27068c2ecf20Sopenharmony_ci } 27078c2ecf20Sopenharmony_ci /* Truncate the runlist. */ 27088c2ecf20Sopenharmony_ci err = ntfs_rl_truncate_nolock(vol, &ni->runlist, 27098c2ecf20Sopenharmony_ci new_alloc_size >> vol->cluster_size_bits); 27108c2ecf20Sopenharmony_ci /* 27118c2ecf20Sopenharmony_ci * If the runlist truncation failed and/or the search context is no 27128c2ecf20Sopenharmony_ci * longer valid, we cannot resize the attribute record or build the 27138c2ecf20Sopenharmony_ci * mapping pairs array thus we mark the inode bad so that no access to 27148c2ecf20Sopenharmony_ci * the freed clusters can happen. 27158c2ecf20Sopenharmony_ci */ 27168c2ecf20Sopenharmony_ci if (unlikely(err || IS_ERR(m))) { 27178c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Failed to %s (error code %li).%s", 27188c2ecf20Sopenharmony_ci IS_ERR(m) ? 27198c2ecf20Sopenharmony_ci "restore attribute search context" : 27208c2ecf20Sopenharmony_ci "truncate attribute runlist", 27218c2ecf20Sopenharmony_ci IS_ERR(m) ? PTR_ERR(m) : err, es); 27228c2ecf20Sopenharmony_ci err = -EIO; 27238c2ecf20Sopenharmony_ci goto bad_out; 27248c2ecf20Sopenharmony_ci } 27258c2ecf20Sopenharmony_ci /* Get the size for the shrunk mapping pairs array for the runlist. */ 27268c2ecf20Sopenharmony_ci mp_size = ntfs_get_size_for_mapping_pairs(vol, ni->runlist.rl, 0, -1); 27278c2ecf20Sopenharmony_ci if (unlikely(mp_size <= 0)) { 27288c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, " 27298c2ecf20Sopenharmony_ci "attribute type 0x%x, because determining the " 27308c2ecf20Sopenharmony_ci "size for the mapping pairs failed with error " 27318c2ecf20Sopenharmony_ci "code %i.%s", vi->i_ino, 27328c2ecf20Sopenharmony_ci (unsigned)le32_to_cpu(ni->type), mp_size, es); 27338c2ecf20Sopenharmony_ci err = -EIO; 27348c2ecf20Sopenharmony_ci goto bad_out; 27358c2ecf20Sopenharmony_ci } 27368c2ecf20Sopenharmony_ci /* 27378c2ecf20Sopenharmony_ci * Shrink the attribute record for the new mapping pairs array. Note, 27388c2ecf20Sopenharmony_ci * this cannot fail since we are making the attribute smaller thus by 27398c2ecf20Sopenharmony_ci * definition there is enough space to do so. 27408c2ecf20Sopenharmony_ci */ 27418c2ecf20Sopenharmony_ci attr_len = le32_to_cpu(a->length); 27428c2ecf20Sopenharmony_ci err = ntfs_attr_record_resize(m, a, mp_size + 27438c2ecf20Sopenharmony_ci le16_to_cpu(a->data.non_resident.mapping_pairs_offset)); 27448c2ecf20Sopenharmony_ci BUG_ON(err); 27458c2ecf20Sopenharmony_ci /* 27468c2ecf20Sopenharmony_ci * Generate the mapping pairs array directly into the attribute record. 27478c2ecf20Sopenharmony_ci */ 27488c2ecf20Sopenharmony_ci err = ntfs_mapping_pairs_build(vol, (u8*)a + 27498c2ecf20Sopenharmony_ci le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 27508c2ecf20Sopenharmony_ci mp_size, ni->runlist.rl, 0, -1, NULL); 27518c2ecf20Sopenharmony_ci if (unlikely(err)) { 27528c2ecf20Sopenharmony_ci ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, " 27538c2ecf20Sopenharmony_ci "attribute type 0x%x, because building the " 27548c2ecf20Sopenharmony_ci "mapping pairs failed with error code %i.%s", 27558c2ecf20Sopenharmony_ci vi->i_ino, (unsigned)le32_to_cpu(ni->type), 27568c2ecf20Sopenharmony_ci err, es); 27578c2ecf20Sopenharmony_ci err = -EIO; 27588c2ecf20Sopenharmony_ci goto bad_out; 27598c2ecf20Sopenharmony_ci } 27608c2ecf20Sopenharmony_ci /* Update the allocated/compressed size as well as the highest vcn. */ 27618c2ecf20Sopenharmony_ci a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >> 27628c2ecf20Sopenharmony_ci vol->cluster_size_bits) - 1); 27638c2ecf20Sopenharmony_ci write_lock_irqsave(&ni->size_lock, flags); 27648c2ecf20Sopenharmony_ci ni->allocated_size = new_alloc_size; 27658c2ecf20Sopenharmony_ci a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size); 27668c2ecf20Sopenharmony_ci if (NInoSparse(ni) || NInoCompressed(ni)) { 27678c2ecf20Sopenharmony_ci if (nr_freed) { 27688c2ecf20Sopenharmony_ci ni->itype.compressed.size -= nr_freed << 27698c2ecf20Sopenharmony_ci vol->cluster_size_bits; 27708c2ecf20Sopenharmony_ci BUG_ON(ni->itype.compressed.size < 0); 27718c2ecf20Sopenharmony_ci a->data.non_resident.compressed_size = cpu_to_sle64( 27728c2ecf20Sopenharmony_ci ni->itype.compressed.size); 27738c2ecf20Sopenharmony_ci vi->i_blocks = ni->itype.compressed.size >> 9; 27748c2ecf20Sopenharmony_ci } 27758c2ecf20Sopenharmony_ci } else 27768c2ecf20Sopenharmony_ci vi->i_blocks = new_alloc_size >> 9; 27778c2ecf20Sopenharmony_ci write_unlock_irqrestore(&ni->size_lock, flags); 27788c2ecf20Sopenharmony_ci /* 27798c2ecf20Sopenharmony_ci * We have shrunk the allocation. If this is a shrinking truncate we 27808c2ecf20Sopenharmony_ci * have already dealt with the initialized_size and the data_size above 27818c2ecf20Sopenharmony_ci * and we are done. If the truncate is only changing the allocation 27828c2ecf20Sopenharmony_ci * and not the data_size, we are also done. If this is an extending 27838c2ecf20Sopenharmony_ci * truncate, need to extend the data_size now which is ensured by the 27848c2ecf20Sopenharmony_ci * fact that @size_change is positive. 27858c2ecf20Sopenharmony_ci */ 27868c2ecf20Sopenharmony_cialloc_done: 27878c2ecf20Sopenharmony_ci /* 27888c2ecf20Sopenharmony_ci * If the size is growing, need to update it now. If it is shrinking, 27898c2ecf20Sopenharmony_ci * we have already updated it above (before the allocation change). 27908c2ecf20Sopenharmony_ci */ 27918c2ecf20Sopenharmony_ci if (size_change > 0) 27928c2ecf20Sopenharmony_ci a->data.non_resident.data_size = cpu_to_sle64(new_size); 27938c2ecf20Sopenharmony_ci /* Ensure the modified mft record is written out. */ 27948c2ecf20Sopenharmony_ci flush_dcache_mft_record_page(ctx->ntfs_ino); 27958c2ecf20Sopenharmony_ci mark_mft_record_dirty(ctx->ntfs_ino); 27968c2ecf20Sopenharmony_ciunm_done: 27978c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 27988c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 27998c2ecf20Sopenharmony_ci up_write(&ni->runlist.lock); 28008c2ecf20Sopenharmony_cidone: 28018c2ecf20Sopenharmony_ci /* Update the mtime and ctime on the base inode. */ 28028c2ecf20Sopenharmony_ci /* normally ->truncate shouldn't update ctime or mtime, 28038c2ecf20Sopenharmony_ci * but ntfs did before so it got a copy & paste version 28048c2ecf20Sopenharmony_ci * of file_update_time. one day someone should fix this 28058c2ecf20Sopenharmony_ci * for real. 28068c2ecf20Sopenharmony_ci */ 28078c2ecf20Sopenharmony_ci if (!IS_NOCMTIME(VFS_I(base_ni)) && !IS_RDONLY(VFS_I(base_ni))) { 28088c2ecf20Sopenharmony_ci struct timespec64 now = current_time(VFS_I(base_ni)); 28098c2ecf20Sopenharmony_ci int sync_it = 0; 28108c2ecf20Sopenharmony_ci 28118c2ecf20Sopenharmony_ci if (!timespec64_equal(&VFS_I(base_ni)->i_mtime, &now) || 28128c2ecf20Sopenharmony_ci !timespec64_equal(&VFS_I(base_ni)->i_ctime, &now)) 28138c2ecf20Sopenharmony_ci sync_it = 1; 28148c2ecf20Sopenharmony_ci VFS_I(base_ni)->i_mtime = now; 28158c2ecf20Sopenharmony_ci VFS_I(base_ni)->i_ctime = now; 28168c2ecf20Sopenharmony_ci 28178c2ecf20Sopenharmony_ci if (sync_it) 28188c2ecf20Sopenharmony_ci mark_inode_dirty_sync(VFS_I(base_ni)); 28198c2ecf20Sopenharmony_ci } 28208c2ecf20Sopenharmony_ci 28218c2ecf20Sopenharmony_ci if (likely(!err)) { 28228c2ecf20Sopenharmony_ci NInoClearTruncateFailed(ni); 28238c2ecf20Sopenharmony_ci ntfs_debug("Done."); 28248c2ecf20Sopenharmony_ci } 28258c2ecf20Sopenharmony_ci return err; 28268c2ecf20Sopenharmony_ciold_bad_out: 28278c2ecf20Sopenharmony_ci old_size = -1; 28288c2ecf20Sopenharmony_cibad_out: 28298c2ecf20Sopenharmony_ci if (err != -ENOMEM && err != -EOPNOTSUPP) 28308c2ecf20Sopenharmony_ci NVolSetErrors(vol); 28318c2ecf20Sopenharmony_ci if (err != -EOPNOTSUPP) 28328c2ecf20Sopenharmony_ci NInoSetTruncateFailed(ni); 28338c2ecf20Sopenharmony_ci else if (old_size >= 0) 28348c2ecf20Sopenharmony_ci i_size_write(vi, old_size); 28358c2ecf20Sopenharmony_cierr_out: 28368c2ecf20Sopenharmony_ci if (ctx) 28378c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 28388c2ecf20Sopenharmony_ci if (m) 28398c2ecf20Sopenharmony_ci unmap_mft_record(base_ni); 28408c2ecf20Sopenharmony_ci up_write(&ni->runlist.lock); 28418c2ecf20Sopenharmony_ciout: 28428c2ecf20Sopenharmony_ci ntfs_debug("Failed. Returning error code %i.", err); 28438c2ecf20Sopenharmony_ci return err; 28448c2ecf20Sopenharmony_ciconv_err_out: 28458c2ecf20Sopenharmony_ci if (err != -ENOMEM && err != -EOPNOTSUPP) 28468c2ecf20Sopenharmony_ci NVolSetErrors(vol); 28478c2ecf20Sopenharmony_ci if (err != -EOPNOTSUPP) 28488c2ecf20Sopenharmony_ci NInoSetTruncateFailed(ni); 28498c2ecf20Sopenharmony_ci else 28508c2ecf20Sopenharmony_ci i_size_write(vi, old_size); 28518c2ecf20Sopenharmony_ci goto out; 28528c2ecf20Sopenharmony_ci} 28538c2ecf20Sopenharmony_ci 28548c2ecf20Sopenharmony_ci/** 28558c2ecf20Sopenharmony_ci * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value 28568c2ecf20Sopenharmony_ci * @vi: inode for which the i_size was changed 28578c2ecf20Sopenharmony_ci * 28588c2ecf20Sopenharmony_ci * Wrapper for ntfs_truncate() that has no return value. 28598c2ecf20Sopenharmony_ci * 28608c2ecf20Sopenharmony_ci * See ntfs_truncate() description above for details. 28618c2ecf20Sopenharmony_ci */ 28628c2ecf20Sopenharmony_ci#ifdef NTFS_RW 28638c2ecf20Sopenharmony_civoid ntfs_truncate_vfs(struct inode *vi) { 28648c2ecf20Sopenharmony_ci ntfs_truncate(vi); 28658c2ecf20Sopenharmony_ci} 28668c2ecf20Sopenharmony_ci#endif 28678c2ecf20Sopenharmony_ci 28688c2ecf20Sopenharmony_ci/** 28698c2ecf20Sopenharmony_ci * ntfs_setattr - called from notify_change() when an attribute is being changed 28708c2ecf20Sopenharmony_ci * @dentry: dentry whose attributes to change 28718c2ecf20Sopenharmony_ci * @attr: structure describing the attributes and the changes 28728c2ecf20Sopenharmony_ci * 28738c2ecf20Sopenharmony_ci * We have to trap VFS attempts to truncate the file described by @dentry as 28748c2ecf20Sopenharmony_ci * soon as possible, because we do not implement changes in i_size yet. So we 28758c2ecf20Sopenharmony_ci * abort all i_size changes here. 28768c2ecf20Sopenharmony_ci * 28778c2ecf20Sopenharmony_ci * We also abort all changes of user, group, and mode as we do not implement 28788c2ecf20Sopenharmony_ci * the NTFS ACLs yet. 28798c2ecf20Sopenharmony_ci * 28808c2ecf20Sopenharmony_ci * Called with ->i_mutex held. 28818c2ecf20Sopenharmony_ci */ 28828c2ecf20Sopenharmony_ciint ntfs_setattr(struct dentry *dentry, struct iattr *attr) 28838c2ecf20Sopenharmony_ci{ 28848c2ecf20Sopenharmony_ci struct inode *vi = d_inode(dentry); 28858c2ecf20Sopenharmony_ci int err; 28868c2ecf20Sopenharmony_ci unsigned int ia_valid = attr->ia_valid; 28878c2ecf20Sopenharmony_ci 28888c2ecf20Sopenharmony_ci err = setattr_prepare(dentry, attr); 28898c2ecf20Sopenharmony_ci if (err) 28908c2ecf20Sopenharmony_ci goto out; 28918c2ecf20Sopenharmony_ci /* We do not support NTFS ACLs yet. */ 28928c2ecf20Sopenharmony_ci if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) { 28938c2ecf20Sopenharmony_ci ntfs_warning(vi->i_sb, "Changes in user/group/mode are not " 28948c2ecf20Sopenharmony_ci "supported yet, ignoring."); 28958c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 28968c2ecf20Sopenharmony_ci goto out; 28978c2ecf20Sopenharmony_ci } 28988c2ecf20Sopenharmony_ci if (ia_valid & ATTR_SIZE) { 28998c2ecf20Sopenharmony_ci if (attr->ia_size != i_size_read(vi)) { 29008c2ecf20Sopenharmony_ci ntfs_inode *ni = NTFS_I(vi); 29018c2ecf20Sopenharmony_ci /* 29028c2ecf20Sopenharmony_ci * FIXME: For now we do not support resizing of 29038c2ecf20Sopenharmony_ci * compressed or encrypted files yet. 29048c2ecf20Sopenharmony_ci */ 29058c2ecf20Sopenharmony_ci if (NInoCompressed(ni) || NInoEncrypted(ni)) { 29068c2ecf20Sopenharmony_ci ntfs_warning(vi->i_sb, "Changes in inode size " 29078c2ecf20Sopenharmony_ci "are not supported yet for " 29088c2ecf20Sopenharmony_ci "%s files, ignoring.", 29098c2ecf20Sopenharmony_ci NInoCompressed(ni) ? 29108c2ecf20Sopenharmony_ci "compressed" : "encrypted"); 29118c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 29128c2ecf20Sopenharmony_ci } else { 29138c2ecf20Sopenharmony_ci truncate_setsize(vi, attr->ia_size); 29148c2ecf20Sopenharmony_ci ntfs_truncate_vfs(vi); 29158c2ecf20Sopenharmony_ci } 29168c2ecf20Sopenharmony_ci if (err || ia_valid == ATTR_SIZE) 29178c2ecf20Sopenharmony_ci goto out; 29188c2ecf20Sopenharmony_ci } else { 29198c2ecf20Sopenharmony_ci /* 29208c2ecf20Sopenharmony_ci * We skipped the truncate but must still update 29218c2ecf20Sopenharmony_ci * timestamps. 29228c2ecf20Sopenharmony_ci */ 29238c2ecf20Sopenharmony_ci ia_valid |= ATTR_MTIME | ATTR_CTIME; 29248c2ecf20Sopenharmony_ci } 29258c2ecf20Sopenharmony_ci } 29268c2ecf20Sopenharmony_ci if (ia_valid & ATTR_ATIME) 29278c2ecf20Sopenharmony_ci vi->i_atime = attr->ia_atime; 29288c2ecf20Sopenharmony_ci if (ia_valid & ATTR_MTIME) 29298c2ecf20Sopenharmony_ci vi->i_mtime = attr->ia_mtime; 29308c2ecf20Sopenharmony_ci if (ia_valid & ATTR_CTIME) 29318c2ecf20Sopenharmony_ci vi->i_ctime = attr->ia_ctime; 29328c2ecf20Sopenharmony_ci mark_inode_dirty(vi); 29338c2ecf20Sopenharmony_ciout: 29348c2ecf20Sopenharmony_ci return err; 29358c2ecf20Sopenharmony_ci} 29368c2ecf20Sopenharmony_ci 29378c2ecf20Sopenharmony_ci/** 29388c2ecf20Sopenharmony_ci * ntfs_write_inode - write out a dirty inode 29398c2ecf20Sopenharmony_ci * @vi: inode to write out 29408c2ecf20Sopenharmony_ci * @sync: if true, write out synchronously 29418c2ecf20Sopenharmony_ci * 29428c2ecf20Sopenharmony_ci * Write out a dirty inode to disk including any extent inodes if present. 29438c2ecf20Sopenharmony_ci * 29448c2ecf20Sopenharmony_ci * If @sync is true, commit the inode to disk and wait for io completion. This 29458c2ecf20Sopenharmony_ci * is done using write_mft_record(). 29468c2ecf20Sopenharmony_ci * 29478c2ecf20Sopenharmony_ci * If @sync is false, just schedule the write to happen but do not wait for i/o 29488c2ecf20Sopenharmony_ci * completion. In 2.6 kernels, scheduling usually happens just by virtue of 29498c2ecf20Sopenharmony_ci * marking the page (and in this case mft record) dirty but we do not implement 29508c2ecf20Sopenharmony_ci * this yet as write_mft_record() largely ignores the @sync parameter and 29518c2ecf20Sopenharmony_ci * always performs synchronous writes. 29528c2ecf20Sopenharmony_ci * 29538c2ecf20Sopenharmony_ci * Return 0 on success and -errno on error. 29548c2ecf20Sopenharmony_ci */ 29558c2ecf20Sopenharmony_ciint __ntfs_write_inode(struct inode *vi, int sync) 29568c2ecf20Sopenharmony_ci{ 29578c2ecf20Sopenharmony_ci sle64 nt; 29588c2ecf20Sopenharmony_ci ntfs_inode *ni = NTFS_I(vi); 29598c2ecf20Sopenharmony_ci ntfs_attr_search_ctx *ctx; 29608c2ecf20Sopenharmony_ci MFT_RECORD *m; 29618c2ecf20Sopenharmony_ci STANDARD_INFORMATION *si; 29628c2ecf20Sopenharmony_ci int err = 0; 29638c2ecf20Sopenharmony_ci bool modified = false; 29648c2ecf20Sopenharmony_ci 29658c2ecf20Sopenharmony_ci ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "", 29668c2ecf20Sopenharmony_ci vi->i_ino); 29678c2ecf20Sopenharmony_ci /* 29688c2ecf20Sopenharmony_ci * Dirty attribute inodes are written via their real inodes so just 29698c2ecf20Sopenharmony_ci * clean them here. Access time updates are taken care off when the 29708c2ecf20Sopenharmony_ci * real inode is written. 29718c2ecf20Sopenharmony_ci */ 29728c2ecf20Sopenharmony_ci if (NInoAttr(ni)) { 29738c2ecf20Sopenharmony_ci NInoClearDirty(ni); 29748c2ecf20Sopenharmony_ci ntfs_debug("Done."); 29758c2ecf20Sopenharmony_ci return 0; 29768c2ecf20Sopenharmony_ci } 29778c2ecf20Sopenharmony_ci /* Map, pin, and lock the mft record belonging to the inode. */ 29788c2ecf20Sopenharmony_ci m = map_mft_record(ni); 29798c2ecf20Sopenharmony_ci if (IS_ERR(m)) { 29808c2ecf20Sopenharmony_ci err = PTR_ERR(m); 29818c2ecf20Sopenharmony_ci goto err_out; 29828c2ecf20Sopenharmony_ci } 29838c2ecf20Sopenharmony_ci /* Update the access times in the standard information attribute. */ 29848c2ecf20Sopenharmony_ci ctx = ntfs_attr_get_search_ctx(ni, m); 29858c2ecf20Sopenharmony_ci if (unlikely(!ctx)) { 29868c2ecf20Sopenharmony_ci err = -ENOMEM; 29878c2ecf20Sopenharmony_ci goto unm_err_out; 29888c2ecf20Sopenharmony_ci } 29898c2ecf20Sopenharmony_ci err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 29908c2ecf20Sopenharmony_ci CASE_SENSITIVE, 0, NULL, 0, ctx); 29918c2ecf20Sopenharmony_ci if (unlikely(err)) { 29928c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 29938c2ecf20Sopenharmony_ci goto unm_err_out; 29948c2ecf20Sopenharmony_ci } 29958c2ecf20Sopenharmony_ci si = (STANDARD_INFORMATION*)((u8*)ctx->attr + 29968c2ecf20Sopenharmony_ci le16_to_cpu(ctx->attr->data.resident.value_offset)); 29978c2ecf20Sopenharmony_ci /* Update the access times if they have changed. */ 29988c2ecf20Sopenharmony_ci nt = utc2ntfs(vi->i_mtime); 29998c2ecf20Sopenharmony_ci if (si->last_data_change_time != nt) { 30008c2ecf20Sopenharmony_ci ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, " 30018c2ecf20Sopenharmony_ci "new = 0x%llx", vi->i_ino, (long long) 30028c2ecf20Sopenharmony_ci sle64_to_cpu(si->last_data_change_time), 30038c2ecf20Sopenharmony_ci (long long)sle64_to_cpu(nt)); 30048c2ecf20Sopenharmony_ci si->last_data_change_time = nt; 30058c2ecf20Sopenharmony_ci modified = true; 30068c2ecf20Sopenharmony_ci } 30078c2ecf20Sopenharmony_ci nt = utc2ntfs(vi->i_ctime); 30088c2ecf20Sopenharmony_ci if (si->last_mft_change_time != nt) { 30098c2ecf20Sopenharmony_ci ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, " 30108c2ecf20Sopenharmony_ci "new = 0x%llx", vi->i_ino, (long long) 30118c2ecf20Sopenharmony_ci sle64_to_cpu(si->last_mft_change_time), 30128c2ecf20Sopenharmony_ci (long long)sle64_to_cpu(nt)); 30138c2ecf20Sopenharmony_ci si->last_mft_change_time = nt; 30148c2ecf20Sopenharmony_ci modified = true; 30158c2ecf20Sopenharmony_ci } 30168c2ecf20Sopenharmony_ci nt = utc2ntfs(vi->i_atime); 30178c2ecf20Sopenharmony_ci if (si->last_access_time != nt) { 30188c2ecf20Sopenharmony_ci ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, " 30198c2ecf20Sopenharmony_ci "new = 0x%llx", vi->i_ino, 30208c2ecf20Sopenharmony_ci (long long)sle64_to_cpu(si->last_access_time), 30218c2ecf20Sopenharmony_ci (long long)sle64_to_cpu(nt)); 30228c2ecf20Sopenharmony_ci si->last_access_time = nt; 30238c2ecf20Sopenharmony_ci modified = true; 30248c2ecf20Sopenharmony_ci } 30258c2ecf20Sopenharmony_ci /* 30268c2ecf20Sopenharmony_ci * If we just modified the standard information attribute we need to 30278c2ecf20Sopenharmony_ci * mark the mft record it is in dirty. We do this manually so that 30288c2ecf20Sopenharmony_ci * mark_inode_dirty() is not called which would redirty the inode and 30298c2ecf20Sopenharmony_ci * hence result in an infinite loop of trying to write the inode. 30308c2ecf20Sopenharmony_ci * There is no need to mark the base inode nor the base mft record 30318c2ecf20Sopenharmony_ci * dirty, since we are going to write this mft record below in any case 30328c2ecf20Sopenharmony_ci * and the base mft record may actually not have been modified so it 30338c2ecf20Sopenharmony_ci * might not need to be written out. 30348c2ecf20Sopenharmony_ci * NOTE: It is not a problem when the inode for $MFT itself is being 30358c2ecf20Sopenharmony_ci * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES 30368c2ecf20Sopenharmony_ci * on the $MFT inode and hence ntfs_write_inode() will not be 30378c2ecf20Sopenharmony_ci * re-invoked because of it which in turn is ok since the dirtied mft 30388c2ecf20Sopenharmony_ci * record will be cleaned and written out to disk below, i.e. before 30398c2ecf20Sopenharmony_ci * this function returns. 30408c2ecf20Sopenharmony_ci */ 30418c2ecf20Sopenharmony_ci if (modified) { 30428c2ecf20Sopenharmony_ci flush_dcache_mft_record_page(ctx->ntfs_ino); 30438c2ecf20Sopenharmony_ci if (!NInoTestSetDirty(ctx->ntfs_ino)) 30448c2ecf20Sopenharmony_ci mark_ntfs_record_dirty(ctx->ntfs_ino->page, 30458c2ecf20Sopenharmony_ci ctx->ntfs_ino->page_ofs); 30468c2ecf20Sopenharmony_ci } 30478c2ecf20Sopenharmony_ci ntfs_attr_put_search_ctx(ctx); 30488c2ecf20Sopenharmony_ci /* Now the access times are updated, write the base mft record. */ 30498c2ecf20Sopenharmony_ci if (NInoDirty(ni)) 30508c2ecf20Sopenharmony_ci err = write_mft_record(ni, m, sync); 30518c2ecf20Sopenharmony_ci /* Write all attached extent mft records. */ 30528c2ecf20Sopenharmony_ci mutex_lock(&ni->extent_lock); 30538c2ecf20Sopenharmony_ci if (ni->nr_extents > 0) { 30548c2ecf20Sopenharmony_ci ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos; 30558c2ecf20Sopenharmony_ci int i; 30568c2ecf20Sopenharmony_ci 30578c2ecf20Sopenharmony_ci ntfs_debug("Writing %i extent inodes.", ni->nr_extents); 30588c2ecf20Sopenharmony_ci for (i = 0; i < ni->nr_extents; i++) { 30598c2ecf20Sopenharmony_ci ntfs_inode *tni = extent_nis[i]; 30608c2ecf20Sopenharmony_ci 30618c2ecf20Sopenharmony_ci if (NInoDirty(tni)) { 30628c2ecf20Sopenharmony_ci MFT_RECORD *tm = map_mft_record(tni); 30638c2ecf20Sopenharmony_ci int ret; 30648c2ecf20Sopenharmony_ci 30658c2ecf20Sopenharmony_ci if (IS_ERR(tm)) { 30668c2ecf20Sopenharmony_ci if (!err || err == -ENOMEM) 30678c2ecf20Sopenharmony_ci err = PTR_ERR(tm); 30688c2ecf20Sopenharmony_ci continue; 30698c2ecf20Sopenharmony_ci } 30708c2ecf20Sopenharmony_ci ret = write_mft_record(tni, tm, sync); 30718c2ecf20Sopenharmony_ci unmap_mft_record(tni); 30728c2ecf20Sopenharmony_ci if (unlikely(ret)) { 30738c2ecf20Sopenharmony_ci if (!err || err == -ENOMEM) 30748c2ecf20Sopenharmony_ci err = ret; 30758c2ecf20Sopenharmony_ci } 30768c2ecf20Sopenharmony_ci } 30778c2ecf20Sopenharmony_ci } 30788c2ecf20Sopenharmony_ci } 30798c2ecf20Sopenharmony_ci mutex_unlock(&ni->extent_lock); 30808c2ecf20Sopenharmony_ci unmap_mft_record(ni); 30818c2ecf20Sopenharmony_ci if (unlikely(err)) 30828c2ecf20Sopenharmony_ci goto err_out; 30838c2ecf20Sopenharmony_ci ntfs_debug("Done."); 30848c2ecf20Sopenharmony_ci return 0; 30858c2ecf20Sopenharmony_ciunm_err_out: 30868c2ecf20Sopenharmony_ci unmap_mft_record(ni); 30878c2ecf20Sopenharmony_cierr_out: 30888c2ecf20Sopenharmony_ci if (err == -ENOMEM) { 30898c2ecf20Sopenharmony_ci ntfs_warning(vi->i_sb, "Not enough memory to write inode. " 30908c2ecf20Sopenharmony_ci "Marking the inode dirty again, so the VFS " 30918c2ecf20Sopenharmony_ci "retries later."); 30928c2ecf20Sopenharmony_ci mark_inode_dirty(vi); 30938c2ecf20Sopenharmony_ci } else { 30948c2ecf20Sopenharmony_ci ntfs_error(vi->i_sb, "Failed (error %i): Run chkdsk.", -err); 30958c2ecf20Sopenharmony_ci NVolSetErrors(ni->vol); 30968c2ecf20Sopenharmony_ci } 30978c2ecf20Sopenharmony_ci return err; 30988c2ecf20Sopenharmony_ci} 30998c2ecf20Sopenharmony_ci 31008c2ecf20Sopenharmony_ci#endif /* NTFS_RW */ 3101