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