Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * inode.c - NTFS kernel inode handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "aops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "attrib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "bitmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "lcnalloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "malloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "mft.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "time.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "ntfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * ntfs_test_inode - compare two (possibly fake) inodes for equality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * @vi:		vfs inode which to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * @data:	data which is being tested with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * Compare the ntfs attribute embedded in the ntfs specific part of the vfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * inode @vi for equality with the ntfs attribute @data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * If searching for the normal file/directory inode, set @na->type to AT_UNUSED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * @na->name and @na->name_len are then ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * Return 1 if the attributes match and 0 if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * NOTE: This function runs with the inode_hash_lock spin lock held so it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * allowed to sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) int ntfs_test_inode(struct inode *vi, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	ntfs_attr *na = (ntfs_attr *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	ntfs_inode *ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	if (vi->i_ino != na->mft_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	/* If !NInoAttr(ni), @vi is a normal file or directory inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	if (likely(!NInoAttr(ni))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		/* If not looking for a normal inode this is a mismatch. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		if (unlikely(na->type != AT_UNUSED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		/* A fake inode describing an attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		if (ni->type != na->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		if (ni->name_len != na->name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		if (na->name_len && memcmp(ni->name, na->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 				na->name_len * sizeof(ntfschar)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	/* Match! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * ntfs_init_locked_inode - initialize an inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * @vi:		vfs inode to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * @data:	data which to initialize @vi to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * Initialize the vfs inode @vi with the values from the ntfs attribute @data in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * order to enable ntfs_test_inode() to do its work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * If initializing the normal file/directory inode, set @na->type to AT_UNUSED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * In that case, @na->name and @na->name_len should be set to NULL and 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * respectively. Although that is not strictly necessary as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * ntfs_read_locked_inode() will fill them in later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * Return 0 on success and -errno on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * NOTE: This function runs with the inode->i_lock spin lock held so it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * allowed to sleep. (Hence the GFP_ATOMIC allocation.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static int ntfs_init_locked_inode(struct inode *vi, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	ntfs_attr *na = (ntfs_attr *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	ntfs_inode *ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	vi->i_ino = na->mft_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	ni->type = na->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	if (na->type == AT_INDEX_ALLOCATION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		NInoSetMstProtected(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	ni->name = na->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	ni->name_len = na->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	/* If initializing a normal inode, we are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	if (likely(na->type == AT_UNUSED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		BUG_ON(na->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		BUG_ON(na->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	/* It is a fake inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	NInoSetAttr(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	 * We have I30 global constant as an optimization as it is the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	 * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	 * allocation but that is ok. And most attributes are unnamed anyway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	 * thus the fraction of named attributes with name != I30 is actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	 * absolutely tiny.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	if (na->name_len && na->name != I30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		BUG_ON(!na->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		i = na->name_len * sizeof(ntfschar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		ni->name = kmalloc(i + sizeof(ntfschar), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		if (!ni->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		memcpy(ni->name, na->name, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		ni->name[na->name_len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static int ntfs_read_locked_inode(struct inode *vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) static int ntfs_read_locked_index_inode(struct inode *base_vi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		struct inode *vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  * ntfs_iget - obtain a struct inode corresponding to a specific normal inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  * @sb:		super block of mounted volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  * @mft_no:	mft record number / inode number to obtain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * Obtain the struct inode corresponding to a specific normal inode (i.e. a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * file or directory).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * If the inode is in the cache, it is just returned with an increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * reference count. Otherwise, a new struct inode is allocated and initialized,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * and finally ntfs_read_locked_inode() is called to read in the inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * fill in the remainder of the inode structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * Return the struct inode on success. Check the return value with IS_ERR() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * if true, the function failed and the error code is obtained from PTR_ERR().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct inode *vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	ntfs_attr na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	na.mft_no = mft_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	na.type = AT_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	na.name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	na.name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	vi = iget5_locked(sb, mft_no, ntfs_test_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			ntfs_init_locked_inode, &na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	if (unlikely(!vi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	/* If this is a freshly allocated inode, need to read it now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	if (vi->i_state & I_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		err = ntfs_read_locked_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		unlock_new_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	 * There is no point in keeping bad inodes around if the failure was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	 * due to ENOMEM. We want to be able to retry again later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (unlikely(err == -ENOMEM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		iput(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		vi = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	return vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  * ntfs_attr_iget - obtain a struct inode corresponding to an attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * @base_vi:	vfs base inode containing the attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * @type:	attribute type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  * @name:	Unicode name of the attribute (NULL if unnamed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * @name_len:	length of @name in Unicode characters (0 if unnamed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * Obtain the (fake) struct inode corresponding to the attribute specified by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * @type, @name, and @name_len, which is present in the base mft record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * specified by the vfs inode @base_vi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * If the attribute inode is in the cache, it is just returned with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * increased reference count. Otherwise, a new struct inode is allocated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * initialized, and finally ntfs_read_locked_attr_inode() is called to read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * attribute and fill in the inode structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * Note, for index allocation attributes, you need to use ntfs_index_iget()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * instead of ntfs_attr_iget() as working with indices is a lot more complex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * Return the struct inode of the attribute inode on success. Check the return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * value with IS_ERR() and if true, the function failed and the error code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * obtained from PTR_ERR().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		ntfschar *name, u32 name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	struct inode *vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	ntfs_attr na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* Make sure no one calls ntfs_attr_iget() for indices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	BUG_ON(type == AT_INDEX_ALLOCATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	na.mft_no = base_vi->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	na.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	na.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	na.name_len = name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			ntfs_init_locked_inode, &na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (unlikely(!vi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	/* If this is a freshly allocated inode, need to read it now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (vi->i_state & I_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		err = ntfs_read_locked_attr_inode(base_vi, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		unlock_new_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	 * There is no point in keeping bad attribute inodes around. This also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	 * simplifies things in that we never need to check for bad attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	 * inodes elsewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		iput(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		vi = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	return vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  * ntfs_index_iget - obtain a struct inode corresponding to an index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  * @base_vi:	vfs base inode containing the index related attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * @name:	Unicode name of the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  * @name_len:	length of @name in Unicode characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  * Obtain the (fake) struct inode corresponding to the index specified by @name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  * and @name_len, which is present in the base mft record specified by the vfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * inode @base_vi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263)  * If the index inode is in the cache, it is just returned with an increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  * reference count.  Otherwise, a new struct inode is allocated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * initialized, and finally ntfs_read_locked_index_inode() is called to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * the index related attributes and fill in the inode structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268)  * Return the struct inode of the index inode on success. Check the return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  * value with IS_ERR() and if true, the function failed and the error code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  * obtained from PTR_ERR().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		u32 name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	struct inode *vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	ntfs_attr na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	na.mft_no = base_vi->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	na.type = AT_INDEX_ALLOCATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	na.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	na.name_len = name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			ntfs_init_locked_inode, &na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	if (unlikely(!vi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	/* If this is a freshly allocated inode, need to read it now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	if (vi->i_state & I_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		err = ntfs_read_locked_index_inode(base_vi, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		unlock_new_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 * There is no point in keeping bad index inodes around.  This also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	 * simplifies things in that we never need to check for bad index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	 * inodes elsewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		iput(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		vi = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	return vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) struct inode *ntfs_alloc_big_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	ntfs_inode *ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	ntfs_debug("Entering.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	ni = kmem_cache_alloc(ntfs_big_inode_cache, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	if (likely(ni != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		ni->state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		return VFS_I(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	ntfs_error(sb, "Allocation of NTFS big inode structure failed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) void ntfs_free_big_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static inline ntfs_inode *ntfs_alloc_extent_inode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	ntfs_inode *ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	ntfs_debug("Entering.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	if (likely(ni != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		ni->state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		return ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ntfs_error(NULL, "Allocation of NTFS inode structure failed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) static void ntfs_destroy_extent_inode(ntfs_inode *ni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	ntfs_debug("Entering.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	BUG_ON(ni->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	if (!atomic_dec_and_test(&ni->count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	kmem_cache_free(ntfs_inode_cache, ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351)  * The attribute runlist lock has separate locking rules from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352)  * normal runlist lock, so split the two lock-classes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) static struct lock_class_key attr_list_rl_lock_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * __ntfs_init_inode - initialize ntfs specific part of an inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * @sb:		super block of mounted volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  * @ni:		freshly allocated ntfs inode which to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  * Initialize an ntfs inode to defaults.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  * untouched. Make sure to initialize them elsewhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366)  * Return zero on success and -ENOMEM on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	ntfs_debug("Entering.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	rwlock_init(&ni->size_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	ni->initialized_size = ni->allocated_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	ni->seq_no = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	atomic_set(&ni->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	ni->vol = NTFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	ntfs_init_runlist(&ni->runlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	mutex_init(&ni->mrec_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	ni->page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	ni->page_ofs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	ni->attr_list_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	ni->attr_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	ntfs_init_runlist(&ni->attr_list_rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	lockdep_set_class(&ni->attr_list_rl.lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 				&attr_list_rl_lock_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	ni->itype.index.block_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	ni->itype.index.vcn_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	ni->itype.index.collation_rule = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	ni->itype.index.block_size_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	ni->itype.index.vcn_size_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	mutex_init(&ni->extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	ni->nr_extents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	ni->ext.base_ntfs_ino = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * Extent inodes get MFT-mapped in a nested way, while the base inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * is still mapped. Teach this nesting to the lock validator by creating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * a separate class for nested inode's mrec_lock's:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) static struct lock_class_key extent_inode_mrec_lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		unsigned long mft_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	ntfs_inode *ni = ntfs_alloc_extent_inode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	ntfs_debug("Entering.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (likely(ni != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		__ntfs_init_inode(sb, ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		lockdep_set_class(&ni->mrec_lock, &extent_inode_mrec_lock_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		ni->mft_no = mft_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		ni->type = AT_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		ni->name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		ni->name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	return ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * ntfs_is_extended_system_file - check if a file is in the $Extend directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * @ctx:	initialized attribute search context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  * Search all file name attributes in the inode described by the attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * search context @ctx and check if any of the names are in the $Extend system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  * directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  * Return values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  *	   1: file is in $Extend directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  *	   0: file is not in $Extend directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  *    -errno: failed to determine if the file is in the $Extend directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) static int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	int nr_links, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	/* Restart search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	/* Get number of hard links. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	nr_links = le16_to_cpu(ctx->mrec->link_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	/* Loop through all hard links. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			ctx))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		FILE_NAME_ATTR *file_name_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		ATTR_RECORD *attr = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		u8 *p, *p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		nr_links--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		 * Maximum sanity checking as we are called on an inode that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		 * we suspect might be corrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		p = (u8*)attr + le32_to_cpu(attr->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 				le32_to_cpu(ctx->mrec->bytes_in_use)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) err_corrupt_attr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 					"attribute. You should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		if (attr->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 					"name. You should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		if (attr->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			ntfs_error(ctx->ntfs_ino->vol->sb, "File name with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 					"invalid flags. You should run "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 					"chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 					"name. You should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		file_name_attr = (FILE_NAME_ATTR*)((u8*)attr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 				le16_to_cpu(attr->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		p2 = (u8 *)file_name_attr + le32_to_cpu(attr->data.resident.value_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		if (p2 < (u8*)attr || p2 > p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			goto err_corrupt_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		/* This attribute is ok, but is it in the $Extend directory? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			return 1;	/* YES, it's an extended system file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (unlikely(err != -ENOENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (unlikely(nr_links)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 				"doesn't match number of name attributes. You "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 				"should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	return 0;	/* NO, it is not an extended system file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499)  * ntfs_read_locked_inode - read an inode from its device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  * @vi:		inode to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502)  * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503)  * described by @vi into memory from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505)  * The only fields in @vi that we need to/can look at when the function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506)  * called are i_sb, pointing to the mounted device's super block, and i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507)  * the number of the inode to load.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  * for reading and sets up the necessary @vi fields as well as initializing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  * the ntfs inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513)  * Q: What locks are held when the function is called?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  * A: i_state has I_NEW set, hence the inode is locked, also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515)  *    i_count is set to 1, so it is not going to go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  *    i_flags is set to 0 and we have no business touching it.  Only an ioctl()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  *    is allowed to write to them. We should of course be honouring them but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  *    we need to do that using the IS_* macros defined in include/linux/fs.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  *    In any case ntfs_read_locked_inode() has nothing to do with i_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  * Return 0 on success and -errno on error.  In the error case, the inode will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  * have had make_bad_inode() executed on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) static int ntfs_read_locked_inode(struct inode *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	ntfs_volume *vol = NTFS_SB(vi->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	ntfs_inode *ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct inode *bvi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	ATTR_RECORD *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	STANDARD_INFORMATION *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	/* Setup the generic vfs inode parts now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	vi->i_uid = vol->uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	vi->i_gid = vol->gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	vi->i_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 * Initialize the ntfs specific part of @vi special casing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * FILE_MFT which we need to do at mount time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (vi->i_ino != FILE_MFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		ntfs_init_big_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	m = map_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		err = PTR_ERR(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	ctx = ntfs_attr_get_search_ctx(ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (!(m->flags & MFT_RECORD_IN_USE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		ntfs_error(vi->i_sb, "Inode is not in use!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (m->base_mft_record) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		ntfs_error(vi->i_sb, "Inode is an extent inode!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	/* Transfer information from mft record into vfs and ntfs inodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	 * FIXME: Keep in mind that link_count is two for files which have both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	 * a long file name and a short file name as separate entries, so if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	 * we are hiding short file names this will be too high. Either we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	 * to account for the short file names by subtracting them or we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	 * to make sure we delete files even though i_nlink is not zero which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	 * might be tricky due to vfs interactions. Need to think about this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	 * some more when implementing the unlink command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	set_nlink(vi, le16_to_cpu(m->link_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	 * FIXME: Reparse points can have the directory bit set even though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	 * they would be S_IFLNK. Need to deal with this further below when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	 * implement reparse points / symbolic links but it will do for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	 * Also if not a directory, it could be something else, rather than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	 * a regular file. But again, will do for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	/* Everyone gets all permissions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	vi->i_mode |= S_IRWXUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	/* If read-only, no one gets write permissions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	if (IS_RDONLY(vi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		vi->i_mode &= ~S_IWUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	if (m->flags & MFT_RECORD_IS_DIRECTORY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		vi->i_mode |= S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		 * Apply the directory permissions mask set in the mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		 * options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		vi->i_mode &= ~vol->dmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		/* Things break without this kludge! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		if (vi->i_nlink > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			set_nlink(vi, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		vi->i_mode |= S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		/* Apply the file permissions mask set in the mount options. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		vi->i_mode &= ~vol->fmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	 * Find the standard information attribute in the mft record. At this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	 * stage we haven't setup the attribute list stuff yet, so this could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	 * in fact fail if the standard information is in an extent record, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	 * I don't think this actually ever happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 			 * TODO: We should be performing a hot fix here (if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			 * recover mount option is set) by creating a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			 * attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 					"is missing.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	/* Get the standard information attribute value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	if ((u8 *)a + le16_to_cpu(a->data.resident.value_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			+ le32_to_cpu(a->data.resident.value_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			(u8 *)ctx->mrec + vol->mft_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		ntfs_error(vi->i_sb, "Corrupt standard information attribute in inode.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	si = (STANDARD_INFORMATION*)((u8*)a +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			le16_to_cpu(a->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	/* Transfer information from the standard information into vi. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	 * Note: The i_?times do not quite map perfectly onto the NTFS times,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	 * but they are close enough, and in the end it doesn't really matter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	 * that much...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	 * mtime is the last change of the data within the file. Not changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	 * when only metadata is changed, e.g. a rename doesn't affect mtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	vi->i_mtime = ntfs2utc(si->last_data_change_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	 * ctime is the last change of the metadata of the file. This obviously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	 * always changes, when mtime is changed. ctime can be changed on its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	 * own, mtime is then not changed, e.g. when a file is renamed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	vi->i_ctime = ntfs2utc(si->last_mft_change_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	 * Last access to the data within the file. Not changed during a rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	 * for example but changed whenever the file is written to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	vi->i_atime = ntfs2utc(si->last_access_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	/* Find the attribute list attribute if present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		if (unlikely(err != -ENOENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			ntfs_error(vi->i_sb, "Failed to lookup attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 					"attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	} else /* if (!err) */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		if (vi->i_ino == FILE_MFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			goto skip_attr_list_load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		NInoSetAttrList(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			ntfs_error(vi->i_sb, "Attribute list attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 					"compressed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		if (a->flags & ATTR_IS_ENCRYPTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 				a->flags & ATTR_IS_SPARSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			if (a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 				ntfs_error(vi->i_sb, "Non-resident attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 						"list attribute is encrypted/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 						"sparse.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 			ntfs_warning(vi->i_sb, "Resident attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 					"attribute in inode 0x%lx is marked "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 					"encrypted/sparse which is not true.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 					"However, Windows allows this and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 					"chkdsk does not detect or correct it "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 					"so we will just ignore the invalid "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 					"flags and pretend they are not set.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 					vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		/* Now allocate memory for the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		ni->attr_list_size = (u32)ntfs_attr_size(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		if (!ni->attr_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 			ntfs_error(vi->i_sb, "Not enough memory to allocate "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 					"buffer for attribute list.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		if (a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			NInoSetAttrListNonResident(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 				ntfs_error(vi->i_sb, "Attribute list has non "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 						"zero lowest_vcn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			 * Setup the runlist. No need for locking as we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			 * exclusive access to the inode at this time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 			ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 					a, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			if (IS_ERR(ni->attr_list_rl.rl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 				err = PTR_ERR(ni->attr_list_rl.rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 				ni->attr_list_rl.rl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 				ntfs_error(vi->i_sb, "Mapping pairs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 						"decompression failed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 			/* Now load the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			if ((err = load_attribute_list(vol, &ni->attr_list_rl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 					ni->attr_list, ni->attr_list_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 					sle64_to_cpu(a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 					initialized_size)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 				ntfs_error(vi->i_sb, "Failed to load "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 						"attribute list attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		} else /* if (!a->non_resident) */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 			if ((u8*)a + le16_to_cpu(a->data.resident.value_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 					+ le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 					a->data.resident.value_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 					(u8*)ctx->mrec + vol->mft_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 				ntfs_error(vi->i_sb, "Corrupt attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 						"in inode.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			/* Now copy the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 					a->data.resident.value_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 					le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 					a->data.resident.value_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) skip_attr_list_load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	 * If an attribute list is present we now have the attribute list value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	 * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (S_ISDIR(vi->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		loff_t bvi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		ntfs_inode *bni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		INDEX_ROOT *ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		u8 *ir_end, *index_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		/* It is a directory, find index root attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				// FIXME: File is corrupt! Hot-fix with empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 				// index root attribute if recovery option is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				// set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				ntfs_error(vi->i_sb, "$INDEX_ROOT attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 						"is missing.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		/* Set up the state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		if (unlikely(a->non_resident)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			ntfs_error(vol->sb, "$INDEX_ROOT attribute is not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 					"resident.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		/* Ensure the attribute name is placed before the value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 				le16_to_cpu(a->data.resident.value_offset)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			ntfs_error(vol->sb, "$INDEX_ROOT attribute name is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 					"placed after the attribute value.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		 * Compressed/encrypted index root just means that the newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		 * created files in that directory should be created compressed/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		 * encrypted. However index root cannot be both compressed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		 * encrypted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		if (a->flags & ATTR_COMPRESSION_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			NInoSetCompressed(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		if (a->flags & ATTR_IS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 				ntfs_error(vi->i_sb, "Found encrypted and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 						"compressed attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			NInoSetEncrypted(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		if (a->flags & ATTR_IS_SPARSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			NInoSetSparse(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		ir = (INDEX_ROOT*)((u8*)a +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 				le16_to_cpu(a->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 					"corrupt.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		index_end = (u8*)&ir->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 				le32_to_cpu(ir->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		if (index_end > ir_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			ntfs_error(vi->i_sb, "Directory index is corrupt.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		if (ir->type != AT_FILE_NAME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			ntfs_error(vi->i_sb, "Indexed attribute is not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 					"$FILE_NAME.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		if (ir->collation_rule != COLLATION_FILE_NAME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			ntfs_error(vi->i_sb, "Index collation rule is not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 					"COLLATION_FILE_NAME.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		ni->itype.index.collation_rule = ir->collation_rule;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		if (ni->itype.index.block_size &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 				(ni->itype.index.block_size - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			ntfs_error(vi->i_sb, "Index block size (%u) is not a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 					"power of two.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 					ni->itype.index.block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		if (ni->itype.index.block_size > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			ntfs_error(vi->i_sb, "Index block size (%u) > "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 					"PAGE_SIZE (%ld) is not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 					"supported.  Sorry.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 					ni->itype.index.block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 					PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			ntfs_error(vi->i_sb, "Index block size (%u) < "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 					"NTFS_BLOCK_SIZE (%i) is not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 					"supported.  Sorry.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 					ni->itype.index.block_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 					NTFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		ni->itype.index.block_size_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 				ffs(ni->itype.index.block_size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		/* Determine the size of a vcn in the directory index. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		if (vol->cluster_size <= ni->itype.index.block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			ni->itype.index.vcn_size = vol->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			ni->itype.index.vcn_size = vol->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			ni->itype.index.vcn_size_bits = vol->sector_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		/* Setup the index allocation attribute, even if not present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		NInoSetMstProtected(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		ni->type = AT_INDEX_ALLOCATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		ni->name = I30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		ni->name_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (!(ir->index.flags & LARGE_INDEX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			/* No index allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			vi->i_size = ni->initialized_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 					ni->allocated_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			/* We are done with the mft record, so we release it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			unmap_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			goto skip_large_dir_stuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		} /* LARGE_INDEX: Index allocation present. Setup state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		NInoSetIndexAllocPresent(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		/* Find index allocation attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				CASE_SENSITIVE, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 				ntfs_error(vi->i_sb, "$INDEX_ALLOCATION "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 						"attribute is not present but "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 						"$INDEX_ROOT indicated it is.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 				ntfs_error(vi->i_sb, "Failed to lookup "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 						"$INDEX_ALLOCATION "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 						"attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		if (!a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 					"is resident.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		 * Ensure the attribute name is placed before the mapping pairs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		 * array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 				a->data.non_resident.mapping_pairs_offset)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 					"is placed after the mapping pairs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 					"array.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		if (a->flags & ATTR_IS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 					"is encrypted.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		if (a->flags & ATTR_IS_SPARSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 					"is sparse.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 					"is compressed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 			ntfs_error(vi->i_sb, "First extent of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 					"$INDEX_ALLOCATION attribute has non "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 					"zero lowest_vcn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		ni->initialized_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 				a->data.non_resident.initialized_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		ni->allocated_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				a->data.non_resident.allocated_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		 * We are done with the mft record, so we release it. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		 * we would deadlock in ntfs_attr_iget().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		unmap_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		/* Get the index bitmap attribute inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		if (IS_ERR(bvi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			err = PTR_ERR(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		bni = NTFS_I(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (NInoCompressed(bni) || NInoEncrypted(bni) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 				NInoSparse(bni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 					"and/or encrypted and/or sparse.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			goto iput_unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		/* Consistency check bitmap size vs. index allocation size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		bvi_size = i_size_read(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		if ((bvi_size << 3) < (vi->i_size >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 				ni->itype.index.block_size_bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 					"for index allocation (0x%llx).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 					bvi_size << 3, vi->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			goto iput_unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		/* No longer need the bitmap attribute inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		iput(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) skip_large_dir_stuff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		/* Setup the operations for this inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		vi->i_op = &ntfs_dir_inode_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		vi->i_fop = &ntfs_dir_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		vi->i_mapping->a_ops = &ntfs_mst_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		/* It is a file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		/* Setup the data attribute, even if not present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		ni->type = AT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		ni->name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		ni->name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		/* Find first extent of the unnamed data attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			vi->i_size = ni->initialized_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 					ni->allocated_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			if (err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 				ntfs_error(vi->i_sb, "Failed to lookup $DATA "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 						"attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			 * FILE_Secure does not have an unnamed $DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			 * attribute, so we special case it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			if (vi->i_ino == FILE_Secure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 				goto no_data_attr_special_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			 * Most if not all the system files in the $Extend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			 * system directory do not have unnamed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			 * attributes so we need to check if the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			 * directory of the file is FILE_Extend and if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			 * ignore this error. To do this we need to get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			 * name of this inode from the mft record as the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			 * contains the back reference to the parent directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			if (ntfs_is_extended_system_file(ctx) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 				goto no_data_attr_special_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 			// FIXME: File is corrupt! Hot-fix with empty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			// attribute if recovery option is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			ntfs_error(vi->i_sb, "$DATA attribute is missing.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		/* Setup the state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 				NInoSetCompressed(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 				if (vol->cluster_size > 4096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 					ntfs_error(vi->i_sb, "Found "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 							"compressed data but "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 							"compression is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 							"disabled due to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 							"cluster size (%i) > "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 							"4kiB.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 							vol->cluster_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 					goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 				if ((a->flags & ATTR_COMPRESSION_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 						!= ATTR_IS_COMPRESSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 					ntfs_error(vi->i_sb, "Found unknown "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 							"compression method "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 							"or corrupt file.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 					goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			if (a->flags & ATTR_IS_SPARSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				NInoSetSparse(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		if (a->flags & ATTR_IS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			if (NInoCompressed(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 				ntfs_error(vi->i_sb, "Found encrypted and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 						"compressed data.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			NInoSetEncrypted(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		if (a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			NInoSetNonResident(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			if (NInoCompressed(ni) || NInoSparse(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 				if (NInoCompressed(ni) && a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 						compression_unit != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 					ntfs_error(vi->i_sb, "Found "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 							"non-standard "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 							"compression unit (%u "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 							"instead of 4).  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 							"Cannot handle this.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 							a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 							compression_unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 					err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 					goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 				if (a->data.non_resident.compression_unit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 					ni->itype.compressed.block_size = 1U <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 							(a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 							compression_unit +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 							vol->cluster_size_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 					ni->itype.compressed.block_size_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 							ffs(ni->itype.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 							compressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 							block_size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 					ni->itype.compressed.block_clusters =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 							1U << a->data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 							non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 							compression_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 					ni->itype.compressed.block_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 					ni->itype.compressed.block_size_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 							0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 					ni->itype.compressed.block_clusters =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 							0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 				ni->itype.compressed.size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 						a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 						compressed_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 				ntfs_error(vi->i_sb, "First extent of $DATA "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 						"attribute has non zero "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 						"lowest_vcn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			vi->i_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 					a->data.non_resident.data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			ni->initialized_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 					a->data.non_resident.initialized_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			ni->allocated_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 					a->data.non_resident.allocated_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		} else { /* Resident attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 			vi->i_size = ni->initialized_size = le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 					a->data.resident.value_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			ni->allocated_size = le32_to_cpu(a->length) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 					le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 					a->data.resident.value_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			if (vi->i_size > ni->allocated_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				ntfs_error(vi->i_sb, "Resident data attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 						"is corrupt (size exceeds "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 						"allocation).");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) no_data_attr_special_case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		/* We are done with the mft record, so we release it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		unmap_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		/* Setup the operations for this inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		vi->i_op = &ntfs_file_inode_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		vi->i_fop = &ntfs_file_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		vi->i_mapping->a_ops = &ntfs_normal_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		if (NInoMstProtected(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			vi->i_mapping->a_ops = &ntfs_mst_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		else if (NInoCompressed(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			vi->i_mapping->a_ops = &ntfs_compressed_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	 * The number of 512-byte blocks used on disk (for stat). This is in so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	 * far inaccurate as it doesn't account for any named streams or other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	 * special non-resident attributes, but that is how Windows works, too,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	 * so we are at least consistent with Windows, if not entirely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	 * consistent with the Linux Way. Doing it the Linux Way would cause a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	 * significant slowdown as it would involve iterating over all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	 * attributes in the mft record and adding the allocated/compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	 * sizes of all non-resident attributes present to give us the Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	 * correct size that should go into i_blocks (after division by 512).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	if (S_ISREG(vi->i_mode) && (NInoCompressed(ni) || NInoSparse(ni)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		vi->i_blocks = ni->itype.compressed.size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		vi->i_blocks = ni->allocated_size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) iput_unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	iput(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		unmap_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	ntfs_error(vol->sb, "Failed with error code %i.  Marking corrupt "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			"inode 0x%lx as bad.  Run chkdsk.", err, vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	make_bad_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	if (err != -EOPNOTSUPP && err != -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		NVolSetErrors(vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * ntfs_read_locked_attr_inode - read an attribute inode from its base inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * @base_vi:	base inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  * @vi:		attribute inode to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)  * attribute inode described by @vi into memory from the base mft record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)  * described by @base_ni.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)  * reading and looks up the attribute described by @vi before setting up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)  * necessary fields in @vi as well as initializing the ntfs inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)  * Q: What locks are held when the function is called?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)  * A: i_state has I_NEW set, hence the inode is locked, also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)  *    i_count is set to 1, so it is not going to go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)  * Return 0 on success and -errno on error.  In the error case, the inode will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)  * have had make_bad_inode() executed on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)  * Note this cannot be called for AT_INDEX_ALLOCATION.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	ntfs_volume *vol = NTFS_SB(vi->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	ntfs_inode *ni, *base_ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	ATTR_RECORD *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	ntfs_init_big_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	ni	= NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	base_ni = NTFS_I(base_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	/* Just mirror the values from the base inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	vi->i_uid	= base_vi->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	vi->i_gid	= base_vi->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	set_nlink(vi, base_vi->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	vi->i_mtime	= base_vi->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	vi->i_ctime	= base_vi->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	vi->i_atime	= base_vi->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	vi->i_generation = ni->seq_no = base_ni->seq_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	/* Set inode type to zero but preserve permissions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	vi->i_mode	= base_vi->i_mode & ~S_IFMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	m = map_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		err = PTR_ERR(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	ctx = ntfs_attr_get_search_ctx(base_ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	/* Find the attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			CASE_SENSITIVE, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			NInoSetCompressed(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 					ni->name_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 				ntfs_error(vi->i_sb, "Found compressed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 						"non-data or named data "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 						"attribute.  Please report "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 						"you saw this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 						"linux-ntfs-dev@lists."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 						"sourceforge.net");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			if (vol->cluster_size > 4096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				ntfs_error(vi->i_sb, "Found compressed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 						"attribute but compression is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 						"disabled due to cluster size "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 						"(%i) > 4kiB.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 						vol->cluster_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			if ((a->flags & ATTR_COMPRESSION_MASK) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 					ATTR_IS_COMPRESSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 				ntfs_error(vi->i_sb, "Found unknown "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 						"compression method.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		 * The compressed/sparse flag set in an index root just means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		 * to compress all files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			ntfs_error(vi->i_sb, "Found mst protected attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 					"but the attribute is %s.  Please "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 					"report you saw this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 					"linux-ntfs-dev@lists.sourceforge.net",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 					NInoCompressed(ni) ? "compressed" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 					"sparse");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		if (a->flags & ATTR_IS_SPARSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			NInoSetSparse(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	if (a->flags & ATTR_IS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		if (NInoCompressed(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			ntfs_error(vi->i_sb, "Found encrypted and compressed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 					"data.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		 * The encryption flag set in an index root just means to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		 * encrypt all files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 			ntfs_error(vi->i_sb, "Found mst protected attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 					"but the attribute is encrypted.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 					"Please report you saw this message "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 					"to linux-ntfs-dev@lists.sourceforge."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 					"net");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		if (ni->type != AT_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			ntfs_error(vi->i_sb, "Found encrypted non-data "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 					"attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		NInoSetEncrypted(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	if (!a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		/* Ensure the attribute name is placed before the value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 				le16_to_cpu(a->data.resident.value_offset)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 			ntfs_error(vol->sb, "Attribute name is placed after "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 					"the attribute value.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		if (NInoMstProtected(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			ntfs_error(vi->i_sb, "Found mst protected attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 					"but the attribute is resident.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 					"Please report you saw this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 					"linux-ntfs-dev@lists.sourceforge.net");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		vi->i_size = ni->initialized_size = le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 				a->data.resident.value_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		ni->allocated_size = le32_to_cpu(a->length) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 				le16_to_cpu(a->data.resident.value_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		if (vi->i_size > ni->allocated_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			ntfs_error(vi->i_sb, "Resident attribute is corrupt "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 					"(size exceeds allocation).");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		NInoSetNonResident(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		 * Ensure the attribute name is placed before the mapping pairs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		 * array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 				le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 				a->data.non_resident.mapping_pairs_offset)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			ntfs_error(vol->sb, "Attribute name is placed after "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 					"the mapping pairs array.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		if (NInoCompressed(ni) || NInoSparse(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			if (NInoCompressed(ni) && a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 					compression_unit != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 				ntfs_error(vi->i_sb, "Found non-standard "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 						"compression unit (%u instead "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 						"of 4).  Cannot handle this.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 						a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 						compression_unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 				err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			if (a->data.non_resident.compression_unit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 				ni->itype.compressed.block_size = 1U <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 						(a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 						compression_unit +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 						vol->cluster_size_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 				ni->itype.compressed.block_size_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 						ffs(ni->itype.compressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 						block_size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 				ni->itype.compressed.block_clusters = 1U <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 						a->data.non_resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 						compression_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				ni->itype.compressed.block_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				ni->itype.compressed.block_size_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 				ni->itype.compressed.block_clusters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			ni->itype.compressed.size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 					a->data.non_resident.compressed_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			ntfs_error(vi->i_sb, "First extent of attribute has "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 					"non-zero lowest_vcn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		ni->initialized_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 				a->data.non_resident.initialized_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		ni->allocated_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 				a->data.non_resident.allocated_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	vi->i_mapping->a_ops = &ntfs_normal_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	if (NInoMstProtected(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		vi->i_mapping->a_ops = &ntfs_mst_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	else if (NInoCompressed(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		vi->i_mapping->a_ops = &ntfs_compressed_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		vi->i_blocks = ni->itype.compressed.size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		vi->i_blocks = ni->allocated_size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	 * Make sure the base inode does not go away and attach it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	 * attribute inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	igrab(base_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	ni->ext.base_ntfs_ino = base_ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	ni->nr_extents = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	ntfs_error(vol->sb, "Failed with error code %i while reading attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			"inode (mft_no 0x%lx, type 0x%x, name_len %i).  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			"Marking corrupt inode and base inode 0x%lx as bad.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			"Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			base_vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	make_bad_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	if (err != -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		NVolSetErrors(vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)  * ntfs_read_locked_index_inode - read an index inode from its base inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)  * @base_vi:	base inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)  * @vi:		index inode to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)  * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)  * index inode described by @vi into memory from the base mft record described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)  * by @base_ni.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)  * ntfs_read_locked_index_inode() maps, pins and locks the base inode for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)  * reading and looks up the attributes relating to the index described by @vi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)  * before setting up the necessary fields in @vi as well as initializing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)  * ntfs inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446)  * Note, index inodes are essentially attribute inodes (NInoAttr() is true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)  * with the attribute type set to AT_INDEX_ALLOCATION.  Apart from that, they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)  * are setup like directory inodes since directories are a special case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)  * indices ao they need to be treated in much the same way.  Most importantly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)  * for small indices the index allocation attribute might not actually exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)  * However, the index root attribute always exists but this does not need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)  * have an inode associated with it and this is why we define a new inode type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)  * index.  Also, like for directories, we need to have an attribute inode for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)  * the bitmap attribute corresponding to the index allocation attribute and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)  * can store this in the appropriate field of the inode, just like we do for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)  * normal directory inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)  * Q: What locks are held when the function is called?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)  * A: i_state has I_NEW set, hence the inode is locked, also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)  *    i_count is set to 1, so it is not going to go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)  * Return 0 on success and -errno on error.  In the error case, the inode will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463)  * have had make_bad_inode() executed on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	loff_t bvi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	ntfs_volume *vol = NTFS_SB(vi->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	ntfs_inode *ni, *base_ni, *bni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	struct inode *bvi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	ATTR_RECORD *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	INDEX_ROOT *ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	u8 *ir_end, *index_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	ntfs_init_big_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	ni	= NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	base_ni = NTFS_I(base_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	/* Just mirror the values from the base inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	vi->i_uid	= base_vi->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	vi->i_gid	= base_vi->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	set_nlink(vi, base_vi->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	vi->i_mtime	= base_vi->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	vi->i_ctime	= base_vi->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	vi->i_atime	= base_vi->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	vi->i_generation = ni->seq_no = base_ni->seq_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	/* Set inode type to zero but preserve permissions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	vi->i_mode	= base_vi->i_mode & ~S_IFMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	/* Map the mft record for the base inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	m = map_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		err = PTR_ERR(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	ctx = ntfs_attr_get_search_ctx(base_ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	/* Find the index root attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			CASE_SENSITIVE, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 			ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 					"missing.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	/* Set up the state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	if (unlikely(a->non_resident)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	/* Ensure the attribute name is placed before the value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 			le16_to_cpu(a->data.resident.value_offset)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 				"after the attribute value.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	 * Compressed/encrypted/sparse index root is not allowed, except for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	 * directories of course but those are not dealt with here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 			ATTR_IS_SPARSE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 				"root attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	if (index_end > ir_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		ntfs_error(vi->i_sb, "Index is corrupt.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	if (ir->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				le32_to_cpu(ir->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	ni->itype.index.collation_rule = ir->collation_rule;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	ntfs_debug("Index collation rule is 0x%x.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			le32_to_cpu(ir->collation_rule));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	if (!is_power_of_2(ni->itype.index.block_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		ntfs_error(vi->i_sb, "Index block size (%u) is not a power of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 				"two.", ni->itype.index.block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (ni->itype.index.block_size > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 				"(%ld) is not supported.  Sorry.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 				ni->itype.index.block_size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 				"(%i) is not supported.  Sorry.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 				ni->itype.index.block_size, NTFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	/* Determine the size of a vcn in the index. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	if (vol->cluster_size <= ni->itype.index.block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		ni->itype.index.vcn_size = vol->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		ni->itype.index.vcn_size = vol->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		ni->itype.index.vcn_size_bits = vol->sector_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	/* Check for presence of index allocation attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	if (!(ir->index.flags & LARGE_INDEX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		/* No index allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		vi->i_size = ni->initialized_size = ni->allocated_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		/* We are done with the mft record, so we release it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		goto skip_large_index_stuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	} /* LARGE_INDEX:  Index allocation present.  Setup state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	NInoSetIndexAllocPresent(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	/* Find index allocation attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 			CASE_SENSITIVE, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 			ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 					"not present but $INDEX_ROOT "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 					"indicated it is.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 			ntfs_error(vi->i_sb, "Failed to lookup "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 					"$INDEX_ALLOCATION attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	if (!a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 				"resident.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	 * Ensure the attribute name is placed before the mapping pairs array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			a->data.non_resident.mapping_pairs_offset)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 				"placed after the mapping pairs array.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (a->flags & ATTR_IS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 				"encrypted.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	if (a->flags & ATTR_IS_SPARSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 				"compressed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 				"attribute has non zero lowest_vcn.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	ni->initialized_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 			a->data.non_resident.initialized_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	 * We are done with the mft record, so we release it.  Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	 * we would deadlock in ntfs_attr_iget().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	/* Get the index bitmap attribute inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (IS_ERR(bvi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		err = PTR_ERR(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	bni = NTFS_I(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	if (NInoCompressed(bni) || NInoEncrypted(bni) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			NInoSparse(bni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 				"encrypted and/or sparse.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		goto iput_unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	/* Consistency check bitmap size vs. index allocation size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	bvi_size = i_size_read(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 				"index allocation (0x%llx).", bvi_size << 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 				vi->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		goto iput_unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	iput(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) skip_large_index_stuff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	/* Setup the operations for this index inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	vi->i_mapping->a_ops = &ntfs_mst_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	vi->i_blocks = ni->allocated_size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	 * Make sure the base inode doesn't go away and attach it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	 * index inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	igrab(base_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	ni->ext.base_ntfs_ino = base_ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	ni->nr_extents = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) iput_unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	iput(bvi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 			"inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 			ni->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	make_bad_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	if (err != -EOPNOTSUPP && err != -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		NVolSetErrors(vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)  * The MFT inode has special locking, so teach the lock validator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)  * about this by splitting off the locking rules of the MFT from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)  * the locking rules of other inodes. The MFT inode can never be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)  * accessed from the VFS side (or even internally), only by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)  * map_mft functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)  * ntfs_read_inode_mount - special read_inode for mount time use only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)  * @vi:		inode to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725)  * Read inode FILE_MFT at mount time, only called with super_block lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)  * held from within the read_super() code path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)  * This function exists because when it is called the page cache for $MFT/$DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)  * is not initialized and hence we cannot get at the contents of mft records
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730)  * by calling map_mft_record*().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)  * Further it needs to cope with the circular references problem, i.e. cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733)  * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)  * we do not know where the other extent mft records are yet and again, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)  * we cannot call map_mft_record*() yet.  Obviously this applies only when an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)  * attribute list is actually present in $MFT inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)  * We solve these problems by starting with the $DATA attribute before anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)  * else and iterating using ntfs_attr_lookup($DATA) over all extents.  As each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)  * extent is found, we ntfs_mapping_pairs_decompress() including the implied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)  * ntfs_runlists_merge().  Each step of the iteration necessarily provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742)  * sufficient information for the next step to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)  * This should work but there are two possible pit falls (see inline comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)  * below), but only time will tell if they are real pits or just smoke...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) int ntfs_read_inode_mount(struct inode *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	VCN next_vcn, last_vcn, highest_vcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	s64 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	struct super_block *sb = vi->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	ntfs_volume *vol = NTFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	ntfs_inode *ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	MFT_RECORD *m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	ATTR_RECORD *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	unsigned int i, nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	ntfs_debug("Entering.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	/* Initialize the ntfs specific part of @vi. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	ntfs_init_big_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	/* Setup the data attribute. It is special as it is mst protected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	NInoSetNonResident(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	NInoSetMstProtected(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	NInoSetSparseDisabled(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	ni->type = AT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	ni->name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	ni->name_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	 * This sets up our little cheat allowing us to reuse the async read io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	 * completion handler for directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	ni->itype.index.block_size = vol->mft_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	ni->itype.index.block_size_bits = vol->mft_record_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	/* Very important! Needed to be able to call map_mft_record*(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	vol->mft_ino = vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	/* Allocate enough memory to read the first mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	if (vol->mft_record_size > 64 * 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 				vol->mft_record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	i = vol->mft_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	if (i < sb->s_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		i = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	m = (MFT_RECORD*)ntfs_malloc_nofs(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	if (!m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		ntfs_error(sb, "Failed to allocate buffer for $MFT record 0.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	/* Determine the first block of the $MFT/$DATA attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	block = vol->mft_lcn << vol->cluster_size_bits >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 			sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	if (!nr_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		nr_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	/* Load $MFT/$DATA's first mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	for (i = 0; i < nr_blocks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		bh = sb_bread(sb, block++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			ntfs_error(sb, "Device read failed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 				sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		ntfs_error(sb, "Incorrect mft record size %u in superblock, should be %u.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 				le32_to_cpu(m->bytes_allocated), vol->mft_record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	/* Apply the mst fixups. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		/* FIXME: Try to use the $MFTMirr now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		ntfs_error(sb, "MST fixup failed. $MFT is corrupt.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	/* Need this to sanity check attribute list references to $MFT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	/* Provides readpage() for map_mft_record(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	vi->i_mapping->a_ops = &ntfs_mst_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	ctx = ntfs_attr_get_search_ctx(ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	/* Find the attribute list attribute if present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		if (unlikely(err != -ENOENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 			ntfs_error(sb, "Failed to lookup attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 					"attribute. You should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	} else /* if (!err) */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		ATTR_LIST_ENTRY *al_entry, *next_al_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		u8 *al_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		static const char *es = "  Not allowed.  $MFT is corrupt.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 				"You should run chkdsk.";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		ntfs_debug("Attribute list attribute found in $MFT.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		NInoSetAttrList(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		if (a->flags & ATTR_COMPRESSION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			ntfs_error(sb, "Attribute list attribute is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 					"compressed.%s", es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		if (a->flags & ATTR_IS_ENCRYPTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 				a->flags & ATTR_IS_SPARSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 			if (a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 				ntfs_error(sb, "Non-resident attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 						"attribute is encrypted/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 						"sparse.%s", es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 			ntfs_warning(sb, "Resident attribute list attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 					"in $MFT system file is marked "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 					"encrypted/sparse which is not true.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 					"However, Windows allows this and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 					"chkdsk does not detect or correct it "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 					"so we will just ignore the invalid "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 					"flags and pretend they are not set.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		/* Now allocate memory for the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		ni->attr_list_size = (u32)ntfs_attr_size(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		if (!ni->attr_list_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 			ntfs_error(sb, "Attr_list_size is zero");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		if (!ni->attr_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			ntfs_error(sb, "Not enough memory to allocate buffer "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 					"for attribute list.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		if (a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 			NInoSetAttrListNonResident(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 			if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 				ntfs_error(sb, "Attribute list has non zero "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 						"lowest_vcn. $MFT is corrupt. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 						"You should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 			/* Setup the runlist. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 			ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 					a, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 			if (IS_ERR(ni->attr_list_rl.rl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 				err = PTR_ERR(ni->attr_list_rl.rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 				ni->attr_list_rl.rl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 				ntfs_error(sb, "Mapping pairs decompression "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 						"failed with error code %i.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 						-err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 			/* Now load the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 			if ((err = load_attribute_list(vol, &ni->attr_list_rl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 					ni->attr_list, ni->attr_list_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 					sle64_to_cpu(a->data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 					non_resident.initialized_size)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 				ntfs_error(sb, "Failed to load attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 						"attribute with error code %i.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 						-err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		} else /* if (!ctx.attr->non_resident) */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 			if ((u8*)a + le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 					a->data.resident.value_offset) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 					le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 					a->data.resident.value_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 					(u8*)ctx->mrec + vol->mft_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 				ntfs_error(sb, "Corrupt attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 						"attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 			/* Now copy the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 			memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 					a->data.resident.value_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 					le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 					a->data.resident.value_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		/* The attribute list is now setup in memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		 * FIXME: I don't know if this case is actually possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		 * According to logic it is not possible but I have seen too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		 * many weird things in MS software to rely on logic... Thus we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		 * perform a manual search and make sure the first $MFT/$DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		 * extent is in the base inode. If it is not we abort with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		 * error and if we ever see a report of this error we will need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		 * to do some magic in order to have the necessary mft record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		 * loaded and in the right place in the page cache. But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		 * hopefully logic will prevail and this never happens...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		al_entry = (ATTR_LIST_ENTRY*)ni->attr_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		al_end = (u8*)al_entry + ni->attr_list_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		for (;; al_entry = next_al_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 			/* Out of bounds check. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 			if ((u8*)al_entry < ni->attr_list ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 					(u8*)al_entry > al_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 			/* Catch the end of the attribute list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 			if ((u8*)al_entry == al_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 			if (!al_entry->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 			if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 					le16_to_cpu(al_entry->length) > al_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 			next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 					le16_to_cpu(al_entry->length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 			if (le32_to_cpu(al_entry->type) > le32_to_cpu(AT_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 			if (AT_DATA != al_entry->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 			/* We want an unnamed attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 			if (al_entry->name_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 			/* Want the first entry, i.e. lowest_vcn == 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 			if (al_entry->lowest_vcn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 				goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 			/* First entry has to be in the base mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 			if (MREF_LE(al_entry->mft_reference) != vi->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 				/* MFT references do not match, logic fails. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 				ntfs_error(sb, "BUG: The first $DATA extent "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 						"of $MFT is not in the base "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 						"mft record. Please report "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 						"you saw this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 						"linux-ntfs-dev@lists."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 						"sourceforge.net");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 				/* Sequence numbers must match. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 				if (MSEQNO_LE(al_entry->mft_reference) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 						ni->seq_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 					goto em_put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 				/* Got it. All is ok. We can stop now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	ntfs_attr_reinit_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	/* Now load all attribute extents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	a = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	next_vcn = last_vcn = highest_vcn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 			ctx))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		runlist_element *nrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		/* Cache the current attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		/* $MFT must be non-resident. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		if (!a->non_resident) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			ntfs_error(sb, "$MFT must be non-resident but a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 					"resident extent was found. $MFT is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 					"corrupt. Run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		/* $MFT must be uncompressed and unencrypted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		if (a->flags & ATTR_COMPRESSION_MASK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 				a->flags & ATTR_IS_ENCRYPTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 				a->flags & ATTR_IS_SPARSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 			ntfs_error(sb, "$MFT must be uncompressed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 					"non-sparse, and unencrypted but a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 					"compressed/sparse/encrypted extent "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 					"was found. $MFT is corrupt. Run "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 					"chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		 * Decompress the mapping pairs array of this extent and merge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		 * the result into the existing runlist. No need for locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		 * as we have exclusive access to the inode at this time and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		 * are a mount in progress task, too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		if (IS_ERR(nrl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 					"failed with error code %ld.  $MFT is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 					"corrupt.", PTR_ERR(nrl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		ni->runlist.rl = nrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		/* Are we in the first extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		if (!next_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			if (a->data.non_resident.lowest_vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 				ntfs_error(sb, "First extent of $DATA "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 						"attribute has non zero "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 						"lowest_vcn. $MFT is corrupt. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 						"You should run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			/* Get the last vcn in the $DATA attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			last_vcn = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 					a->data.non_resident.allocated_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 					>> vol->cluster_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 			/* Fill in the inode size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 			vi->i_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 					a->data.non_resident.data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 			ni->initialized_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 					a->data.non_resident.initialized_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 			ni->allocated_size = sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 					a->data.non_resident.allocated_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 			 * Verify the number of mft records does not exceed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			 * 2^32 - 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 			if ((vi->i_size >> vol->mft_record_size_bits) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 					(1ULL << 32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 				ntfs_error(sb, "$MFT is too big! Aborting.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 				goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 			 * We have got the first extent of the runlist for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 			 * $MFT which means it is now relatively safe to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			 * the normal ntfs_read_inode() function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			 * Complete reading the inode, this will actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			 * re-read the mft record for $MFT, this time entering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			 * it into the page cache with which we complete the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			 * kick start of the volume. It should be safe to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 			 * this now as the first extent of $MFT/$DATA is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 			 * already known and we would hope that we don't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 			 * further extents in order to find the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			 * attributes belonging to $MFT. Only time will tell if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 			 * this is really the case. If not we will have to play
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 			 * magic at this point, possibly duplicating a lot of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 			 * ntfs_read_inode() at this point. We will need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 			 * ensure we do enough of its work to be able to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 			 * ntfs_read_inode() on extents of $MFT/$DATA. But lets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			 * hope this never happens...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			ntfs_read_locked_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			if (is_bad_inode(vi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 				ntfs_error(sb, "ntfs_read_inode() of $MFT "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 						"failed. BUG or corrupt $MFT. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 						"Run chkdsk and if no errors "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 						"are found, please report you "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 						"saw this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 						"linux-ntfs-dev@lists."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 						"sourceforge.net");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 				ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 				/* Revert to the safe super operations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 				ntfs_free(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			 * Re-initialize some specifics about $MFT's inode as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			 * ntfs_read_inode() will have set up the default ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 			/* Set uid and gid to root. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 			vi->i_uid = GLOBAL_ROOT_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 			vi->i_gid = GLOBAL_ROOT_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 			/* Regular file. No access for anyone. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 			vi->i_mode = S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 			/* No VFS initiated operations allowed for $MFT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			vi->i_op = &ntfs_empty_inode_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 			vi->i_fop = &ntfs_empty_file_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		/* Get the lowest vcn for the next extent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		next_vcn = highest_vcn + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		/* Only one extent or error, which we catch below. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 		if (next_vcn <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 		/* Avoid endless loops due to corruption. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		if (next_vcn < sle64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 				a->data.non_resident.lowest_vcn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 			ntfs_error(sb, "$MFT has corrupt attribute list "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 					"attribute. Run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 			goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	if (err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 		ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 				"$MFT is corrupt. Run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	if (!a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 				"corrupt. Run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	if (highest_vcn && highest_vcn != last_vcn - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 		ntfs_error(sb, "Failed to load the complete runlist for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 				"$MFT/$DATA. Driver bug or corrupt $MFT. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 				"Run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 				(unsigned long long)highest_vcn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 				(unsigned long long)last_vcn - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		goto put_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	ntfs_free(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	 * Split the locking rules of the MFT inode from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	 * locking rules of other inodes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	lockdep_set_class(&ni->runlist.lock, &mft_ni_runlist_lock_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	lockdep_set_class(&ni->mrec_lock, &mft_ni_mrec_lock_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) em_put_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	ntfs_error(sb, "Couldn't find first extent of $DATA attribute in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 			"attribute list. $MFT is corrupt. Run chkdsk.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) put_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	ntfs_error(sb, "Failed. Marking inode as bad.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	make_bad_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	ntfs_free(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) static void __ntfs_clear_inode(ntfs_inode *ni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	/* Free all alocated memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	down_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	if (ni->runlist.rl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		ntfs_free(ni->runlist.rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		ni->runlist.rl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	up_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	if (ni->attr_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 		ntfs_free(ni->attr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		ni->attr_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	down_write(&ni->attr_list_rl.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	if (ni->attr_list_rl.rl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		ntfs_free(ni->attr_list_rl.rl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		ni->attr_list_rl.rl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	up_write(&ni->attr_list_rl.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	if (ni->name_len && ni->name != I30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		/* Catch bugs... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		BUG_ON(!ni->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		kfree(ni->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) void ntfs_clear_extent_inode(ntfs_inode *ni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	BUG_ON(NInoAttr(ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	BUG_ON(ni->nr_extents != -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) #ifdef NTFS_RW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	if (NInoDirty(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 			ntfs_error(ni->vol->sb, "Clearing dirty extent inode!  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 					"Losing data!  This is a BUG!!!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		// FIXME:  Do something!!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) #endif /* NTFS_RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	__ntfs_clear_inode(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	/* Bye, bye... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	ntfs_destroy_extent_inode(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231)  * ntfs_evict_big_inode - clean up the ntfs specific part of an inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232)  * @vi:		vfs inode pending annihilation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234)  * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235)  * is called, which deallocates all memory belonging to the NTFS specific part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236)  * of the inode and returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238)  * If the MFT record is dirty, we commit it before doing anything else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) void ntfs_evict_big_inode(struct inode *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	ntfs_inode *ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	truncate_inode_pages_final(&vi->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	clear_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) #ifdef NTFS_RW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	if (NInoDirty(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		bool was_bad = (is_bad_inode(vi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		/* Committing the inode also commits all extent inodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		ntfs_commit_inode(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 		if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 			ntfs_error(vi->i_sb, "Failed to commit dirty inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 					"0x%lx.  Losing data!", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 			// FIXME:  Do something!!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) #endif /* NTFS_RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	/* No need to lock at this stage as no one else has a reference. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	if (ni->nr_extents > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		for (i = 0; i < ni->nr_extents; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 			ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		kfree(ni->ext.extent_ntfs_inos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	__ntfs_clear_inode(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	if (NInoAttr(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		/* Release the base inode if we are holding it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		if (ni->nr_extents == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 			iput(VFS_I(ni->ext.base_ntfs_ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 			ni->nr_extents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			ni->ext.base_ntfs_ino = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	BUG_ON(ni->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	if (!atomic_dec_and_test(&ni->count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288)  * ntfs_show_options - show mount options in /proc/mounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289)  * @sf:		seq_file in which to write our mount options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290)  * @root:	root of the mounted tree whose mount options to display
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292)  * Called by the VFS once for each mounted ntfs volume when someone reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293)  * /proc/mounts in order to display the NTFS specific mount options of each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)  * mount. The mount options of fs specified by @root are written to the seq file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295)  * @sf and success is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) int ntfs_show_options(struct seq_file *sf, struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	ntfs_volume *vol = NTFS_SB(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	if (vol->fmask == vol->dmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		seq_printf(sf, ",umask=0%o", vol->fmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		seq_printf(sf, ",fmask=0%o", vol->fmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 		seq_printf(sf, ",dmask=0%o", vol->dmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	seq_printf(sf, ",nls=%s", vol->nls_map->charset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	if (NVolCaseSensitive(vol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 		seq_printf(sf, ",case_sensitive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	if (NVolShowSystemFiles(vol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 		seq_printf(sf, ",show_sys_files");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	if (!NVolSparseEnabled(vol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 		seq_printf(sf, ",disable_sparse");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	for (i = 0; on_errors_arr[i].val; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 		if (on_errors_arr[i].val & vol->on_errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 			seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) #ifdef NTFS_RW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) static const char *es = "  Leaving inconsistent metadata.  Unmount and run "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 		"chkdsk.";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331)  * ntfs_truncate - called when the i_size of an ntfs inode is changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)  * @vi:		inode for which the i_size was changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334)  * We only support i_size changes for normal files at present, i.e. not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)  * compressed and not encrypted.  This is enforced in ntfs_setattr(), see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  * below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)  * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339)  * that the change is allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)  * This implies for us that @vi is a file inode rather than a directory, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)  * or attribute inode as well as that @vi is a base inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344)  * Returns 0 on success or -errno on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346)  * Called with ->i_mutex held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) int ntfs_truncate(struct inode *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	s64 new_size, old_size, nr_freed, new_alloc_size, old_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	VCN highest_vcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	ntfs_inode *base_ni, *ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	ntfs_volume *vol = ni->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	ATTR_RECORD *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	const char *te = "  Leaving file length out of sync with i_size.";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	int err, mp_size, size_change, alloc_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	u32 attr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	BUG_ON(NInoAttr(ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	BUG_ON(S_ISDIR(vi->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	BUG_ON(NInoMstProtected(ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	BUG_ON(ni->nr_extents < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) retry_truncate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	 * Lock the runlist for writing and map the mft record to ensure it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	 * safe to mess with the attribute runlist and sizes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	down_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	if (!NInoAttr(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		base_ni = ni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		base_ni = ni->ext.base_ntfs_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	m = map_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 		err = PTR_ERR(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 		ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 				"(error code %d).%s", vi->i_ino, err, te);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		goto old_bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	ctx = ntfs_attr_get_search_ctx(base_ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	if (unlikely(!ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 		ntfs_error(vi->i_sb, "Failed to allocate a search context for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 				"inode 0x%lx (not enough memory).%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 				vi->i_ino, te);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		goto old_bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 			CASE_SENSITIVE, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 			ntfs_error(vi->i_sb, "Open attribute is missing from "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 					"mft record.  Inode 0x%lx is corrupt.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 					"Run chkdsk.%s", vi->i_ino, te);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 			ntfs_error(vi->i_sb, "Failed to lookup attribute in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 					"inode 0x%lx (error code %d).%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 					vi->i_ino, err, te);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 		goto old_bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	m = ctx->mrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	 * The i_size of the vfs inode is the new size for the attribute value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	new_size = i_size_read(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	/* The current size of the attribute value is the old size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	old_size = ntfs_attr_size(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	/* Calculate the new allocated size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	if (NInoNonResident(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 		new_alloc_size = (new_size + vol->cluster_size - 1) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 				~(s64)vol->cluster_size_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		new_alloc_size = (new_size + 7) & ~7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	/* The current allocated size is the old allocated size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	read_lock_irqsave(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	old_alloc_size = ni->allocated_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	read_unlock_irqrestore(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	 * The change in the file size.  This will be 0 if no change, >0 if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	 * size is growing, and <0 if the size is shrinking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	size_change = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	if (new_size - old_size >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 		size_change = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		if (new_size == old_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 			size_change = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	/* As above for the allocated size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	alloc_change = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	if (new_alloc_size - old_alloc_size >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		alloc_change = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 		if (new_alloc_size == old_alloc_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 			alloc_change = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	 * If neither the size nor the allocation are being changed there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	 * nothing to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	if (!size_change && !alloc_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		goto unm_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	/* If the size is changing, check if new size is allowed in $AttrDef. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	if (size_change) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 		err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 			if (err == -ERANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 				ntfs_error(vol->sb, "Truncate would cause the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 						"inode 0x%lx to %simum size "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 						"for its attribute type "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 						"(0x%x).  Aborting truncate.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 						vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 						new_size > old_size ? "exceed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 						"the max" : "go under the min",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 						le32_to_cpu(ni->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 				err = -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 				ntfs_error(vol->sb, "Inode 0x%lx has unknown "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 						"attribute type 0x%x.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 						"Aborting truncate.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 						vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 						le32_to_cpu(ni->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 				err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 			/* Reset the vfs inode size to the old size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 			i_size_write(vi, old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	if (NInoCompressed(ni) || NInoEncrypted(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		ntfs_warning(vi->i_sb, "Changes in inode size are not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 				"supported yet for %s files, ignoring.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 				NInoCompressed(ni) ? "compressed" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 				"encrypted");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 		goto bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	if (a->non_resident)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 		goto do_non_resident_truncate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	BUG_ON(NInoNonResident(ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	/* Resize the attribute record to best fit the new attribute size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	if (new_size < vol->mft_record_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 			!ntfs_resident_attr_value_resize(m, a, new_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 		/* The resize succeeded! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		mark_mft_record_dirty(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		write_lock_irqsave(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 		/* Update the sizes in the ntfs inode and all is done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		ni->allocated_size = le32_to_cpu(a->length) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 				le16_to_cpu(a->data.resident.value_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		 * Note ntfs_resident_attr_value_resize() has already done any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		 * necessary data clearing in the attribute record.  When the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		 * file is being shrunk vmtruncate() will already have cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		 * the top part of the last partial page, i.e. since this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		 * the resident case this is the page with index 0.  However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 		 * when the file is being expanded, the page cache page data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		 * between the old data_size, i.e. old_size, and the new_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 		 * has not been zeroed.  Fortunately, we do not need to zero it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		 * either since on one hand it will either already be zero due
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 		 * to both readpage and writepage clearing partial page data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 		 * beyond i_size in which case there is nothing to do or in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 		 * case of the file being mmap()ped at the same time, POSIX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		 * specifies that the behaviour is unspecified thus we do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 		 * have to do anything.  This means that in our implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 		 * in the rare case that the file is mmap()ped and a write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		 * occurred into the mmap()ped region just beyond the file size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		 * and writepage has not yet been called to write out the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 		 * (which would clear the area beyond the file size) and we now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 		 * extend the file size to incorporate this dirty region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		 * outside the file size, a write of the page would result in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		 * this data being written to disk instead of being cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		 * Given both POSIX and the Linux mmap(2) man page specify that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		 * this corner case is undefined, we choose to leave it like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		 * that as this is much simpler for us as we cannot lock the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 		 * relevant page now since we are holding too many ntfs locks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 		 * which would result in a lock reversal deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		ni->initialized_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 		write_unlock_irqrestore(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 		goto unm_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	/* If the above resize failed, this must be an attribute extension. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	BUG_ON(size_change < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	 * We have to drop all the locks so we can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	 * ntfs_attr_make_non_resident().  This could be optimised by try-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	 * locking the first page cache page and only if that fails dropping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	 * the locks, locking the page, and redoing all the locking and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	 * lookups.  While this would be a huge optimisation, it is not worth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	 * it as this is definitely a slow code path as it only ever can happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	 * once for any given file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	up_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	 * Not enough space in the mft record, try to make the attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	 * non-resident and if successful restart the truncation process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	err = ntfs_attr_make_non_resident(ni, old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	if (likely(!err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 		goto retry_truncate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	 * Could not make non-resident.  If this is due to this not being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	 * permitted for this attribute type or there not being enough space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	 * try to make other attributes non-resident.  Otherwise fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	if (unlikely(err != -EPERM && err != -ENOSPC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 		ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, attribute "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 				"type 0x%x, because the conversion from "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 				"resident to non-resident attribute failed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 				"with error code %i.", vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 				(unsigned)le32_to_cpu(ni->type), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 		if (err != -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 		goto conv_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	/* TODO: Not implemented from here, abort. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	if (err == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 		ntfs_error(vol->sb, "Not enough space in the mft record/on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 				"disk for the non-resident attribute value.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 				"This case is not implemented yet.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	else /* if (err == -EPERM) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 		ntfs_error(vol->sb, "This attribute type may not be "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 				"non-resident.  This case is not implemented "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 				"yet.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	goto conv_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	// TODO: Attempt to make other attributes non-resident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 		goto do_resident_extend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	 * Both the attribute list attribute and the standard information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 	 * attribute must remain in the base inode.  Thus, if this is one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	 * these attributes, we have to try to move other attributes out into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	 * extent mft records instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 	if (ni->type == AT_ATTRIBUTE_LIST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			ni->type == AT_STANDARD_INFORMATION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 		// TODO: Attempt to move other attributes into extent mft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 		// records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 			goto do_resident_extend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	// TODO: Attempt to move this attribute to an extent mft record, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	// only if it is not already the only attribute in an mft record in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	// which case there would be nothing to gain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 		goto do_resident_extend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	/* There is nothing we can do to make enough space. )-: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) do_non_resident_truncate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	BUG_ON(!NInoNonResident(ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	if (alloc_change < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 		highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 		if (highest_vcn > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 				old_alloc_size >> vol->cluster_size_bits >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 				highest_vcn + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			 * This attribute has multiple extents.  Not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 			 * supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 			ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 					"attribute type 0x%x, because the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 					"attribute is highly fragmented (it "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 					"consists of multiple extents) and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 					"this case is not implemented yet.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 					vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 					(unsigned)le32_to_cpu(ni->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 			err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 			goto bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	 * If the size is shrinking, need to reduce the initialized_size and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	 * the data_size before reducing the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	if (size_change < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		 * Make the valid size smaller (i_size is already up-to-date).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		write_lock_irqsave(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		if (new_size < ni->initialized_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 			ni->initialized_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 			a->data.non_resident.initialized_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 					cpu_to_sle64(new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		a->data.non_resident.data_size = cpu_to_sle64(new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 		write_unlock_irqrestore(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 		mark_mft_record_dirty(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		/* If the allocated size is not changing, we are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		if (!alloc_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 			goto unm_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 		 * If the size is shrinking it makes no sense for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		 * allocation to be growing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		BUG_ON(alloc_change > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	} else /* if (size_change >= 0) */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		 * The file size is growing or staying the same but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 		 * allocation can be shrinking, growing or staying the same.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		if (alloc_change > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 			 * We need to extend the allocation and possibly update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 			 * the data size.  If we are updating the data size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 			 * since we are not touching the initialized_size we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 			 * not need to worry about the actual data on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 			 * And as far as the page cache is concerned, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 			 * will be no pages beyond the old data size and any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 			 * partial region in the last page between the old and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 			 * new data size (or the end of the page if the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 			 * data size is outside the page) does not need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 			 * modified as explained above for the resident
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 			 * attribute truncate case.  To do this, we simply drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 			 * the locks we hold and leave all the work to our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 			 * friendly helper ntfs_attr_extend_allocation().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 			ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 			unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 			up_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 			err = ntfs_attr_extend_allocation(ni, new_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 					size_change > 0 ? new_size : -1, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 			 * ntfs_attr_extend_allocation() will have done error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 			 * output already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 		if (!alloc_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 			goto alloc_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	/* alloc_change < 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	/* Free the clusters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	nr_freed = ntfs_cluster_free(ni, new_alloc_size >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 			vol->cluster_size_bits, -1, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	m = ctx->mrec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	a = ctx->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	if (unlikely(nr_freed < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 		ntfs_error(vol->sb, "Failed to release cluster(s) (error code "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 				"%lli).  Unmount and run chkdsk to recover "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 				"the lost cluster(s).", (long long)nr_freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 		NVolSetErrors(vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 		nr_freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	/* Truncate the runlist. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	err = ntfs_rl_truncate_nolock(vol, &ni->runlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 			new_alloc_size >> vol->cluster_size_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	 * If the runlist truncation failed and/or the search context is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	 * longer valid, we cannot resize the attribute record or build the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	 * mapping pairs array thus we mark the inode bad so that no access to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	 * the freed clusters can happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	if (unlikely(err || IS_ERR(m))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 		ntfs_error(vol->sb, "Failed to %s (error code %li).%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 				IS_ERR(m) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 				"restore attribute search context" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 				"truncate attribute runlist",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 				IS_ERR(m) ? PTR_ERR(m) : err, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 		goto bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	/* Get the size for the shrunk mapping pairs array for the runlist. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	mp_size = ntfs_get_size_for_mapping_pairs(vol, ni->runlist.rl, 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	if (unlikely(mp_size <= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 				"attribute type 0x%x, because determining the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 				"size for the mapping pairs failed with error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 				"code %i.%s", vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 				(unsigned)le32_to_cpu(ni->type), mp_size, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 		goto bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	 * Shrink the attribute record for the new mapping pairs array.  Note,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	 * this cannot fail since we are making the attribute smaller thus by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	 * definition there is enough space to do so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	attr_len = le32_to_cpu(a->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	err = ntfs_attr_record_resize(m, a, mp_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	BUG_ON(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	 * Generate the mapping pairs array directly into the attribute record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	err = ntfs_mapping_pairs_build(vol, (u8*)a +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 			mp_size, ni->runlist.rl, 0, -1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 		ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 				"attribute type 0x%x, because building the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 				"mapping pairs failed with error code %i.%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 				vi->i_ino, (unsigned)le32_to_cpu(ni->type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 				err, es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 		goto bad_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	/* Update the allocated/compressed size as well as the highest vcn. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 			vol->cluster_size_bits) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	write_lock_irqsave(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	ni->allocated_size = new_alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	if (NInoSparse(ni) || NInoCompressed(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 		if (nr_freed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 			ni->itype.compressed.size -= nr_freed <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 					vol->cluster_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 			BUG_ON(ni->itype.compressed.size < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 			a->data.non_resident.compressed_size = cpu_to_sle64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 					ni->itype.compressed.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 			vi->i_blocks = ni->itype.compressed.size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 		vi->i_blocks = new_alloc_size >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	write_unlock_irqrestore(&ni->size_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	 * We have shrunk the allocation.  If this is a shrinking truncate we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	 * have already dealt with the initialized_size and the data_size above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	 * and we are done.  If the truncate is only changing the allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	 * and not the data_size, we are also done.  If this is an extending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	 * truncate, need to extend the data_size now which is ensured by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	 * fact that @size_change is positive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) alloc_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	 * If the size is growing, need to update it now.  If it is shrinking,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	 * we have already updated it above (before the allocation change).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	if (size_change > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 		a->data.non_resident.data_size = cpu_to_sle64(new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	/* Ensure the modified mft record is written out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	flush_dcache_mft_record_page(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	mark_mft_record_dirty(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) unm_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	up_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	/* Update the mtime and ctime on the base inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	/* normally ->truncate shouldn't update ctime or mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	 * but ntfs did before so it got a copy & paste version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	 * of file_update_time.  one day someone should fix this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	 * for real.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	if (!IS_NOCMTIME(VFS_I(base_ni)) && !IS_RDONLY(VFS_I(base_ni))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 		struct timespec64 now = current_time(VFS_I(base_ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 		int sync_it = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 		if (!timespec64_equal(&VFS_I(base_ni)->i_mtime, &now) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		    !timespec64_equal(&VFS_I(base_ni)->i_ctime, &now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 			sync_it = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 		VFS_I(base_ni)->i_mtime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 		VFS_I(base_ni)->i_ctime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 		if (sync_it)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 			mark_inode_dirty_sync(VFS_I(base_ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	if (likely(!err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 		NInoClearTruncateFailed(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 		ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) old_bad_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	old_size = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) bad_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	if (err != -ENOMEM && err != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 		NVolSetErrors(vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 	if (err != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 		NInoSetTruncateFailed(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	else if (old_size >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 		i_size_write(vi, old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		unmap_mft_record(base_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	up_write(&ni->runlist.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	ntfs_debug("Failed.  Returning error code %i.", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) conv_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 	if (err != -ENOMEM && err != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 		NVolSetErrors(vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	if (err != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 		NInoSetTruncateFailed(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 		i_size_write(vi, old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848)  * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849)  * @vi:		inode for which the i_size was changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851)  * Wrapper for ntfs_truncate() that has no return value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853)  * See ntfs_truncate() description above for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) #ifdef NTFS_RW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) void ntfs_truncate_vfs(struct inode *vi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	ntfs_truncate(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862)  * ntfs_setattr - called from notify_change() when an attribute is being changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863)  * @dentry:	dentry whose attributes to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864)  * @attr:	structure describing the attributes and the changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866)  * We have to trap VFS attempts to truncate the file described by @dentry as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867)  * soon as possible, because we do not implement changes in i_size yet.  So we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868)  * abort all i_size changes here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870)  * We also abort all changes of user, group, and mode as we do not implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871)  * the NTFS ACLs yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873)  * Called with ->i_mutex held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 	struct inode *vi = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	unsigned int ia_valid = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	err = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	/* We do not support NTFS ACLs yet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 	if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 		ntfs_warning(vi->i_sb, "Changes in user/group/mode are not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 				"supported yet, ignoring.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	if (ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		if (attr->ia_size != i_size_read(vi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 			ntfs_inode *ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 			 * FIXME: For now we do not support resizing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 			 * compressed or encrypted files yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 			if (NInoCompressed(ni) || NInoEncrypted(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 				ntfs_warning(vi->i_sb, "Changes in inode size "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 						"are not supported yet for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 						"%s files, ignoring.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 						NInoCompressed(ni) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 						"compressed" : "encrypted");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 				err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 				truncate_setsize(vi, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 				ntfs_truncate_vfs(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 			if (err || ia_valid == ATTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 			 * We skipped the truncate but must still update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 			 * timestamps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 			ia_valid |= ATTR_MTIME | ATTR_CTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	if (ia_valid & ATTR_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		vi->i_atime = attr->ia_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	if (ia_valid & ATTR_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		vi->i_mtime = attr->ia_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 	if (ia_valid & ATTR_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		vi->i_ctime = attr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 	mark_inode_dirty(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931)  * ntfs_write_inode - write out a dirty inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932)  * @vi:		inode to write out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933)  * @sync:	if true, write out synchronously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935)  * Write out a dirty inode to disk including any extent inodes if present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937)  * If @sync is true, commit the inode to disk and wait for io completion.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938)  * is done using write_mft_record().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940)  * If @sync is false, just schedule the write to happen but do not wait for i/o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941)  * completion.  In 2.6 kernels, scheduling usually happens just by virtue of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942)  * marking the page (and in this case mft record) dirty but we do not implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943)  * this yet as write_mft_record() largely ignores the @sync parameter and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944)  * always performs synchronous writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946)  * Return 0 on success and -errno on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) int __ntfs_write_inode(struct inode *vi, int sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	sle64 nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	ntfs_inode *ni = NTFS_I(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 	STANDARD_INFORMATION *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	bool modified = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 			vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	 * Dirty attribute inodes are written via their real inodes so just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	 * clean them here.  Access time updates are taken care off when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	 * real inode is written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	if (NInoAttr(ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		NInoClearDirty(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 		ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	/* Map, pin, and lock the mft record belonging to the inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	m = map_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 		err = PTR_ERR(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 	/* Update the access times in the standard information attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 	ctx = ntfs_attr_get_search_ctx(ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	if (unlikely(!ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 			CASE_SENSITIVE, 0, NULL, 0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	si = (STANDARD_INFORMATION*)((u8*)ctx->attr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 			le16_to_cpu(ctx->attr->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	/* Update the access times if they have changed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 	nt = utc2ntfs(vi->i_mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	if (si->last_data_change_time != nt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 		ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 				"new = 0x%llx", vi->i_ino, (long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 				sle64_to_cpu(si->last_data_change_time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 				(long long)sle64_to_cpu(nt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		si->last_data_change_time = nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 		modified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 	nt = utc2ntfs(vi->i_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 	if (si->last_mft_change_time != nt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 		ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 				"new = 0x%llx", vi->i_ino, (long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 				sle64_to_cpu(si->last_mft_change_time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 				(long long)sle64_to_cpu(nt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 		si->last_mft_change_time = nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 		modified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	nt = utc2ntfs(vi->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	if (si->last_access_time != nt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 		ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 				"new = 0x%llx", vi->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 				(long long)sle64_to_cpu(si->last_access_time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 				(long long)sle64_to_cpu(nt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 		si->last_access_time = nt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 		modified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	 * If we just modified the standard information attribute we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	 * mark the mft record it is in dirty.  We do this manually so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 	 * mark_inode_dirty() is not called which would redirty the inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	 * hence result in an infinite loop of trying to write the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	 * There is no need to mark the base inode nor the base mft record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	 * dirty, since we are going to write this mft record below in any case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 	 * and the base mft record may actually not have been modified so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 	 * might not need to be written out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	 * NOTE: It is not a problem when the inode for $MFT itself is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	 * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 	 * on the $MFT inode and hence ntfs_write_inode() will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	 * re-invoked because of it which in turn is ok since the dirtied mft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	 * record will be cleaned and written out to disk below, i.e. before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	 * this function returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	if (modified) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 		if (!NInoTestSetDirty(ctx->ntfs_ino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 			mark_ntfs_record_dirty(ctx->ntfs_ino->page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 					ctx->ntfs_ino->page_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	/* Now the access times are updated, write the base mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	if (NInoDirty(ni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 		err = write_mft_record(ni, m, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	/* Write all attached extent mft records. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 	mutex_lock(&ni->extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	if (ni->nr_extents > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 		ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 		ntfs_debug("Writing %i extent inodes.", ni->nr_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 		for (i = 0; i < ni->nr_extents; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 			ntfs_inode *tni = extent_nis[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 			if (NInoDirty(tni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 				MFT_RECORD *tm = map_mft_record(tni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 				int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 				if (IS_ERR(tm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 					if (!err || err == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 						err = PTR_ERR(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 				ret = write_mft_record(tni, tm, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 				unmap_mft_record(tni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 				if (unlikely(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 					if (!err || err == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 						err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	mutex_unlock(&ni->extent_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	unmap_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	unmap_mft_record(ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	if (err == -ENOMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 		ntfs_warning(vi->i_sb, "Not enough memory to write inode.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 				"Marking the inode dirty again, so the VFS "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 				"retries later.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 		mark_inode_dirty(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 		ntfs_error(vi->i_sb, "Failed (error %i):  Run chkdsk.", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 		NVolSetErrors(ni->vol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) #endif /* NTFS_RW */