| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /** |
|---|
| 2 | 3 | * inode.c - NTFS kernel inode handling. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program/include file is free software; you can redistribute it and/or |
|---|
| 7 | | - * modify it under the terms of the GNU General Public License as published |
|---|
| 8 | | - * by the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | | - * (at your option) any later version. |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program/include file is distributed in the hope that it will be |
|---|
| 12 | | - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
|---|
| 13 | | - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | | - * GNU General Public License for more details. |
|---|
| 15 | | - * |
|---|
| 16 | | - * You should have received a copy of the GNU General Public License |
|---|
| 17 | | - * along with this program (in the main directory of the Linux-NTFS |
|---|
| 18 | | - * distribution in the file COPYING); if not, write to the Free Software |
|---|
| 19 | | - * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | 6 | */ |
|---|
| 21 | 7 | |
|---|
| 22 | 8 | #include <linux/buffer_head.h> |
|---|
| .. | .. |
|---|
| 44 | 30 | /** |
|---|
| 45 | 31 | * ntfs_test_inode - compare two (possibly fake) inodes for equality |
|---|
| 46 | 32 | * @vi: vfs inode which to test |
|---|
| 47 | | - * @na: ntfs attribute which is being tested with |
|---|
| 33 | + * @data: data which is being tested with |
|---|
| 48 | 34 | * |
|---|
| 49 | 35 | * Compare the ntfs attribute embedded in the ntfs specific part of the vfs |
|---|
| 50 | | - * inode @vi for equality with the ntfs attribute @na. |
|---|
| 36 | + * inode @vi for equality with the ntfs attribute @data. |
|---|
| 51 | 37 | * |
|---|
| 52 | 38 | * If searching for the normal file/directory inode, set @na->type to AT_UNUSED. |
|---|
| 53 | 39 | * @na->name and @na->name_len are then ignored. |
|---|
| .. | .. |
|---|
| 57 | 43 | * NOTE: This function runs with the inode_hash_lock spin lock held so it is not |
|---|
| 58 | 44 | * allowed to sleep. |
|---|
| 59 | 45 | */ |
|---|
| 60 | | -int ntfs_test_inode(struct inode *vi, ntfs_attr *na) |
|---|
| 46 | +int ntfs_test_inode(struct inode *vi, void *data) |
|---|
| 61 | 47 | { |
|---|
| 48 | + ntfs_attr *na = (ntfs_attr *)data; |
|---|
| 62 | 49 | ntfs_inode *ni; |
|---|
| 63 | 50 | |
|---|
| 64 | 51 | if (vi->i_ino != na->mft_no) |
|---|
| .. | .. |
|---|
| 86 | 73 | /** |
|---|
| 87 | 74 | * ntfs_init_locked_inode - initialize an inode |
|---|
| 88 | 75 | * @vi: vfs inode to initialize |
|---|
| 89 | | - * @na: ntfs attribute which to initialize @vi to |
|---|
| 76 | + * @data: data which to initialize @vi to |
|---|
| 90 | 77 | * |
|---|
| 91 | | - * Initialize the vfs inode @vi with the values from the ntfs attribute @na in |
|---|
| 78 | + * Initialize the vfs inode @vi with the values from the ntfs attribute @data in |
|---|
| 92 | 79 | * order to enable ntfs_test_inode() to do its work. |
|---|
| 93 | 80 | * |
|---|
| 94 | 81 | * If initializing the normal file/directory inode, set @na->type to AT_UNUSED. |
|---|
| .. | .. |
|---|
| 101 | 88 | * NOTE: This function runs with the inode->i_lock spin lock held so it is not |
|---|
| 102 | 89 | * allowed to sleep. (Hence the GFP_ATOMIC allocation.) |
|---|
| 103 | 90 | */ |
|---|
| 104 | | -static int ntfs_init_locked_inode(struct inode *vi, ntfs_attr *na) |
|---|
| 91 | +static int ntfs_init_locked_inode(struct inode *vi, void *data) |
|---|
| 105 | 92 | { |
|---|
| 93 | + ntfs_attr *na = (ntfs_attr *)data; |
|---|
| 106 | 94 | ntfs_inode *ni = NTFS_I(vi); |
|---|
| 107 | 95 | |
|---|
| 108 | 96 | vi->i_ino = na->mft_no; |
|---|
| .. | .. |
|---|
| 145 | 133 | return 0; |
|---|
| 146 | 134 | } |
|---|
| 147 | 135 | |
|---|
| 148 | | -typedef int (*set_t)(struct inode *, void *); |
|---|
| 149 | 136 | static int ntfs_read_locked_inode(struct inode *vi); |
|---|
| 150 | 137 | static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi); |
|---|
| 151 | 138 | static int ntfs_read_locked_index_inode(struct inode *base_vi, |
|---|
| .. | .. |
|---|
| 178 | 165 | na.name = NULL; |
|---|
| 179 | 166 | na.name_len = 0; |
|---|
| 180 | 167 | |
|---|
| 181 | | - vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode, |
|---|
| 182 | | - (set_t)ntfs_init_locked_inode, &na); |
|---|
| 168 | + vi = iget5_locked(sb, mft_no, ntfs_test_inode, |
|---|
| 169 | + ntfs_init_locked_inode, &na); |
|---|
| 183 | 170 | if (unlikely(!vi)) |
|---|
| 184 | 171 | return ERR_PTR(-ENOMEM); |
|---|
| 185 | 172 | |
|---|
| .. | .. |
|---|
| 239 | 226 | na.name = name; |
|---|
| 240 | 227 | na.name_len = name_len; |
|---|
| 241 | 228 | |
|---|
| 242 | | - vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode, |
|---|
| 243 | | - (set_t)ntfs_init_locked_inode, &na); |
|---|
| 229 | + vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode, |
|---|
| 230 | + ntfs_init_locked_inode, &na); |
|---|
| 244 | 231 | if (unlikely(!vi)) |
|---|
| 245 | 232 | return ERR_PTR(-ENOMEM); |
|---|
| 246 | 233 | |
|---|
| .. | .. |
|---|
| 294 | 281 | na.name = name; |
|---|
| 295 | 282 | na.name_len = name_len; |
|---|
| 296 | 283 | |
|---|
| 297 | | - vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode, |
|---|
| 298 | | - (set_t)ntfs_init_locked_inode, &na); |
|---|
| 284 | + vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode, |
|---|
| 285 | + ntfs_init_locked_inode, &na); |
|---|
| 299 | 286 | if (unlikely(!vi)) |
|---|
| 300 | 287 | return ERR_PTR(-ENOMEM); |
|---|
| 301 | 288 | |
|---|
| .. | .. |
|---|
| 332 | 319 | return NULL; |
|---|
| 333 | 320 | } |
|---|
| 334 | 321 | |
|---|
| 335 | | -static void ntfs_i_callback(struct rcu_head *head) |
|---|
| 322 | +void ntfs_free_big_inode(struct inode *inode) |
|---|
| 336 | 323 | { |
|---|
| 337 | | - struct inode *inode = container_of(head, struct inode, i_rcu); |
|---|
| 338 | 324 | kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode)); |
|---|
| 339 | | -} |
|---|
| 340 | | - |
|---|
| 341 | | -void ntfs_destroy_big_inode(struct inode *inode) |
|---|
| 342 | | -{ |
|---|
| 343 | | - ntfs_inode *ni = NTFS_I(inode); |
|---|
| 344 | | - |
|---|
| 345 | | - ntfs_debug("Entering."); |
|---|
| 346 | | - BUG_ON(ni->page); |
|---|
| 347 | | - if (!atomic_dec_and_test(&ni->count)) |
|---|
| 348 | | - BUG(); |
|---|
| 349 | | - call_rcu(&inode->i_rcu, ntfs_i_callback); |
|---|
| 350 | 325 | } |
|---|
| 351 | 326 | |
|---|
| 352 | 327 | static inline ntfs_inode *ntfs_alloc_extent_inode(void) |
|---|
| .. | .. |
|---|
| 1854 | 1829 | goto err_out; |
|---|
| 1855 | 1830 | } |
|---|
| 1856 | 1831 | |
|---|
| 1832 | + /* Sanity check offset to the first attribute */ |
|---|
| 1833 | + if (le16_to_cpu(m->attrs_offset) >= le32_to_cpu(m->bytes_allocated)) { |
|---|
| 1834 | + ntfs_error(sb, "Incorrect mft offset to the first attribute %u in superblock.", |
|---|
| 1835 | + le16_to_cpu(m->attrs_offset)); |
|---|
| 1836 | + goto err_out; |
|---|
| 1837 | + } |
|---|
| 1838 | + |
|---|
| 1857 | 1839 | /* Need this to sanity check attribute list references to $MFT. */ |
|---|
| 1858 | 1840 | vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); |
|---|
| 1859 | 1841 | |
|---|
| .. | .. |
|---|
| 1906 | 1888 | } |
|---|
| 1907 | 1889 | /* Now allocate memory for the attribute list. */ |
|---|
| 1908 | 1890 | ni->attr_list_size = (u32)ntfs_attr_size(a); |
|---|
| 1891 | + if (!ni->attr_list_size) { |
|---|
| 1892 | + ntfs_error(sb, "Attr_list_size is zero"); |
|---|
| 1893 | + goto put_err_out; |
|---|
| 1894 | + } |
|---|
| 1909 | 1895 | ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); |
|---|
| 1910 | 1896 | if (!ni->attr_list) { |
|---|
| 1911 | 1897 | ntfs_error(sb, "Not enough memory to allocate buffer " |
|---|
| .. | .. |
|---|
| 2299 | 2285 | ni->ext.base_ntfs_ino = NULL; |
|---|
| 2300 | 2286 | } |
|---|
| 2301 | 2287 | } |
|---|
| 2288 | + BUG_ON(ni->page); |
|---|
| 2289 | + if (!atomic_dec_and_test(&ni->count)) |
|---|
| 2290 | + BUG(); |
|---|
| 2302 | 2291 | return; |
|---|
| 2303 | 2292 | } |
|---|
| 2304 | 2293 | |
|---|
| .. | .. |
|---|
| 2935 | 2924 | } |
|---|
| 2936 | 2925 | } |
|---|
| 2937 | 2926 | if (ia_valid & ATTR_ATIME) |
|---|
| 2938 | | - vi->i_atime = timespec64_trunc(attr->ia_atime, |
|---|
| 2939 | | - vi->i_sb->s_time_gran); |
|---|
| 2927 | + vi->i_atime = attr->ia_atime; |
|---|
| 2940 | 2928 | if (ia_valid & ATTR_MTIME) |
|---|
| 2941 | | - vi->i_mtime = timespec64_trunc(attr->ia_mtime, |
|---|
| 2942 | | - vi->i_sb->s_time_gran); |
|---|
| 2929 | + vi->i_mtime = attr->ia_mtime; |
|---|
| 2943 | 2930 | if (ia_valid & ATTR_CTIME) |
|---|
| 2944 | | - vi->i_ctime = timespec64_trunc(attr->ia_ctime, |
|---|
| 2945 | | - vi->i_sb->s_time_gran); |
|---|
| 2931 | + vi->i_ctime = attr->ia_ctime; |
|---|
| 2946 | 2932 | mark_inode_dirty(vi); |
|---|
| 2947 | 2933 | out: |
|---|
| 2948 | 2934 | return err; |
|---|