xref: /OK3568_Linux_fs/kernel/fs/ntfs/dir.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun  * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2001-2007 Anton Altaparmakov
6*4882a593Smuzhiyun  * Copyright (c) 2002 Richard Russon
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/buffer_head.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/blkdev.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "dir.h"
14*4882a593Smuzhiyun #include "aops.h"
15*4882a593Smuzhiyun #include "attrib.h"
16*4882a593Smuzhiyun #include "mft.h"
17*4882a593Smuzhiyun #include "debug.h"
18*4882a593Smuzhiyun #include "ntfs.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun  * The little endian Unicode string $I30 as a global constant.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
24*4882a593Smuzhiyun 		cpu_to_le16('3'),	cpu_to_le16('0'), 0 };
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun  * ntfs_lookup_inode_by_name - find an inode in a directory given its name
28*4882a593Smuzhiyun  * @dir_ni:	ntfs inode of the directory in which to search for the name
29*4882a593Smuzhiyun  * @uname:	Unicode name for which to search in the directory
30*4882a593Smuzhiyun  * @uname_len:	length of the name @uname in Unicode characters
31*4882a593Smuzhiyun  * @res:	return the found file name if necessary (see below)
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Look for an inode with name @uname in the directory with inode @dir_ni.
34*4882a593Smuzhiyun  * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
35*4882a593Smuzhiyun  * the Unicode name. If the name is found in the directory, the corresponding
36*4882a593Smuzhiyun  * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
37*4882a593Smuzhiyun  * is a 64-bit number containing the sequence number.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * On error, a negative value is returned corresponding to the error code. In
40*4882a593Smuzhiyun  * particular if the inode is not found -ENOENT is returned. Note that you
41*4882a593Smuzhiyun  * can't just check the return value for being negative, you have to check the
42*4882a593Smuzhiyun  * inode number for being negative which you can extract using MREC(return
43*4882a593Smuzhiyun  * value).
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * Note, @uname_len does not include the (optional) terminating NULL character.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * Note, we look for a case sensitive match first but we also look for a case
48*4882a593Smuzhiyun  * insensitive match at the same time. If we find a case insensitive match, we
49*4882a593Smuzhiyun  * save that for the case that we don't find an exact match, where we return
50*4882a593Smuzhiyun  * the case insensitive match and setup @res (which we allocate!) with the mft
51*4882a593Smuzhiyun  * reference, the file name type, length and with a copy of the little endian
52*4882a593Smuzhiyun  * Unicode file name itself. If we match a file name which is in the DOS name
53*4882a593Smuzhiyun  * space, we only return the mft reference and file name type in @res.
54*4882a593Smuzhiyun  * ntfs_lookup() then uses this to find the long file name in the inode itself.
55*4882a593Smuzhiyun  * This is to avoid polluting the dcache with short file names. We want them to
56*4882a593Smuzhiyun  * work but we don't care for how quickly one can access them. This also fixes
57*4882a593Smuzhiyun  * the dcache aliasing issues.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Locking:  - Caller must hold i_mutex on the directory.
60*4882a593Smuzhiyun  *	     - Each page cache page in the index allocation mapping must be
61*4882a593Smuzhiyun  *	       locked whilst being accessed otherwise we may find a corrupt
62*4882a593Smuzhiyun  *	       page due to it being under ->writepage at the moment which
63*4882a593Smuzhiyun  *	       applies the mst protection fixups before writing out and then
64*4882a593Smuzhiyun  *	       removes them again after the write is complete after which it
65*4882a593Smuzhiyun  *	       unlocks the page.
66*4882a593Smuzhiyun  */
ntfs_lookup_inode_by_name(ntfs_inode * dir_ni,const ntfschar * uname,const int uname_len,ntfs_name ** res)67*4882a593Smuzhiyun MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
68*4882a593Smuzhiyun 		const int uname_len, ntfs_name **res)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	ntfs_volume *vol = dir_ni->vol;
71*4882a593Smuzhiyun 	struct super_block *sb = vol->sb;
72*4882a593Smuzhiyun 	MFT_RECORD *m;
73*4882a593Smuzhiyun 	INDEX_ROOT *ir;
74*4882a593Smuzhiyun 	INDEX_ENTRY *ie;
75*4882a593Smuzhiyun 	INDEX_ALLOCATION *ia;
76*4882a593Smuzhiyun 	u8 *index_end;
77*4882a593Smuzhiyun 	u64 mref;
78*4882a593Smuzhiyun 	ntfs_attr_search_ctx *ctx;
79*4882a593Smuzhiyun 	int err, rc;
80*4882a593Smuzhiyun 	VCN vcn, old_vcn;
81*4882a593Smuzhiyun 	struct address_space *ia_mapping;
82*4882a593Smuzhiyun 	struct page *page;
83*4882a593Smuzhiyun 	u8 *kaddr;
84*4882a593Smuzhiyun 	ntfs_name *name = NULL;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
87*4882a593Smuzhiyun 	BUG_ON(NInoAttr(dir_ni));
88*4882a593Smuzhiyun 	/* Get hold of the mft record for the directory. */
89*4882a593Smuzhiyun 	m = map_mft_record(dir_ni);
90*4882a593Smuzhiyun 	if (IS_ERR(m)) {
91*4882a593Smuzhiyun 		ntfs_error(sb, "map_mft_record() failed with error code %ld.",
92*4882a593Smuzhiyun 				-PTR_ERR(m));
93*4882a593Smuzhiyun 		return ERR_MREF(PTR_ERR(m));
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 	ctx = ntfs_attr_get_search_ctx(dir_ni, m);
96*4882a593Smuzhiyun 	if (unlikely(!ctx)) {
97*4882a593Smuzhiyun 		err = -ENOMEM;
98*4882a593Smuzhiyun 		goto err_out;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	/* Find the index root attribute in the mft record. */
101*4882a593Smuzhiyun 	err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
102*4882a593Smuzhiyun 			0, ctx);
103*4882a593Smuzhiyun 	if (unlikely(err)) {
104*4882a593Smuzhiyun 		if (err == -ENOENT) {
105*4882a593Smuzhiyun 			ntfs_error(sb, "Index root attribute missing in "
106*4882a593Smuzhiyun 					"directory inode 0x%lx.",
107*4882a593Smuzhiyun 					dir_ni->mft_no);
108*4882a593Smuzhiyun 			err = -EIO;
109*4882a593Smuzhiyun 		}
110*4882a593Smuzhiyun 		goto err_out;
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 	/* Get to the index root value (it's been verified in read_inode). */
113*4882a593Smuzhiyun 	ir = (INDEX_ROOT*)((u8*)ctx->attr +
114*4882a593Smuzhiyun 			le16_to_cpu(ctx->attr->data.resident.value_offset));
115*4882a593Smuzhiyun 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
116*4882a593Smuzhiyun 	/* The first index entry. */
117*4882a593Smuzhiyun 	ie = (INDEX_ENTRY*)((u8*)&ir->index +
118*4882a593Smuzhiyun 			le32_to_cpu(ir->index.entries_offset));
119*4882a593Smuzhiyun 	/*
120*4882a593Smuzhiyun 	 * Loop until we exceed valid memory (corruption case) or until we
121*4882a593Smuzhiyun 	 * reach the last entry.
122*4882a593Smuzhiyun 	 */
123*4882a593Smuzhiyun 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
124*4882a593Smuzhiyun 		/* Bounds checks. */
125*4882a593Smuzhiyun 		if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
126*4882a593Smuzhiyun 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
127*4882a593Smuzhiyun 				(u8*)ie + le16_to_cpu(ie->key_length) >
128*4882a593Smuzhiyun 				index_end)
129*4882a593Smuzhiyun 			goto dir_err_out;
130*4882a593Smuzhiyun 		/*
131*4882a593Smuzhiyun 		 * The last entry cannot contain a name. It can however contain
132*4882a593Smuzhiyun 		 * a pointer to a child node in the B+tree so we just break out.
133*4882a593Smuzhiyun 		 */
134*4882a593Smuzhiyun 		if (ie->flags & INDEX_ENTRY_END)
135*4882a593Smuzhiyun 			break;
136*4882a593Smuzhiyun 		/*
137*4882a593Smuzhiyun 		 * We perform a case sensitive comparison and if that matches
138*4882a593Smuzhiyun 		 * we are done and return the mft reference of the inode (i.e.
139*4882a593Smuzhiyun 		 * the inode number together with the sequence number for
140*4882a593Smuzhiyun 		 * consistency checking). We convert it to cpu format before
141*4882a593Smuzhiyun 		 * returning.
142*4882a593Smuzhiyun 		 */
143*4882a593Smuzhiyun 		if (ntfs_are_names_equal(uname, uname_len,
144*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
145*4882a593Smuzhiyun 				ie->key.file_name.file_name_length,
146*4882a593Smuzhiyun 				CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
147*4882a593Smuzhiyun found_it:
148*4882a593Smuzhiyun 			/*
149*4882a593Smuzhiyun 			 * We have a perfect match, so we don't need to care
150*4882a593Smuzhiyun 			 * about having matched imperfectly before, so we can
151*4882a593Smuzhiyun 			 * free name and set *res to NULL.
152*4882a593Smuzhiyun 			 * However, if the perfect match is a short file name,
153*4882a593Smuzhiyun 			 * we need to signal this through *res, so that
154*4882a593Smuzhiyun 			 * ntfs_lookup() can fix dcache aliasing issues.
155*4882a593Smuzhiyun 			 * As an optimization we just reuse an existing
156*4882a593Smuzhiyun 			 * allocation of *res.
157*4882a593Smuzhiyun 			 */
158*4882a593Smuzhiyun 			if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
159*4882a593Smuzhiyun 				if (!name) {
160*4882a593Smuzhiyun 					name = kmalloc(sizeof(ntfs_name),
161*4882a593Smuzhiyun 							GFP_NOFS);
162*4882a593Smuzhiyun 					if (!name) {
163*4882a593Smuzhiyun 						err = -ENOMEM;
164*4882a593Smuzhiyun 						goto err_out;
165*4882a593Smuzhiyun 					}
166*4882a593Smuzhiyun 				}
167*4882a593Smuzhiyun 				name->mref = le64_to_cpu(
168*4882a593Smuzhiyun 						ie->data.dir.indexed_file);
169*4882a593Smuzhiyun 				name->type = FILE_NAME_DOS;
170*4882a593Smuzhiyun 				name->len = 0;
171*4882a593Smuzhiyun 				*res = name;
172*4882a593Smuzhiyun 			} else {
173*4882a593Smuzhiyun 				kfree(name);
174*4882a593Smuzhiyun 				*res = NULL;
175*4882a593Smuzhiyun 			}
176*4882a593Smuzhiyun 			mref = le64_to_cpu(ie->data.dir.indexed_file);
177*4882a593Smuzhiyun 			ntfs_attr_put_search_ctx(ctx);
178*4882a593Smuzhiyun 			unmap_mft_record(dir_ni);
179*4882a593Smuzhiyun 			return mref;
180*4882a593Smuzhiyun 		}
181*4882a593Smuzhiyun 		/*
182*4882a593Smuzhiyun 		 * For a case insensitive mount, we also perform a case
183*4882a593Smuzhiyun 		 * insensitive comparison (provided the file name is not in the
184*4882a593Smuzhiyun 		 * POSIX namespace). If the comparison matches, and the name is
185*4882a593Smuzhiyun 		 * in the WIN32 namespace, we cache the filename in *res so
186*4882a593Smuzhiyun 		 * that the caller, ntfs_lookup(), can work on it. If the
187*4882a593Smuzhiyun 		 * comparison matches, and the name is in the DOS namespace, we
188*4882a593Smuzhiyun 		 * only cache the mft reference and the file name type (we set
189*4882a593Smuzhiyun 		 * the name length to zero for simplicity).
190*4882a593Smuzhiyun 		 */
191*4882a593Smuzhiyun 		if (!NVolCaseSensitive(vol) &&
192*4882a593Smuzhiyun 				ie->key.file_name.file_name_type &&
193*4882a593Smuzhiyun 				ntfs_are_names_equal(uname, uname_len,
194*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
195*4882a593Smuzhiyun 				ie->key.file_name.file_name_length,
196*4882a593Smuzhiyun 				IGNORE_CASE, vol->upcase, vol->upcase_len)) {
197*4882a593Smuzhiyun 			int name_size = sizeof(ntfs_name);
198*4882a593Smuzhiyun 			u8 type = ie->key.file_name.file_name_type;
199*4882a593Smuzhiyun 			u8 len = ie->key.file_name.file_name_length;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 			/* Only one case insensitive matching name allowed. */
202*4882a593Smuzhiyun 			if (name) {
203*4882a593Smuzhiyun 				ntfs_error(sb, "Found already allocated name "
204*4882a593Smuzhiyun 						"in phase 1. Please run chkdsk "
205*4882a593Smuzhiyun 						"and if that doesn't find any "
206*4882a593Smuzhiyun 						"errors please report you saw "
207*4882a593Smuzhiyun 						"this message to "
208*4882a593Smuzhiyun 						"linux-ntfs-dev@lists."
209*4882a593Smuzhiyun 						"sourceforge.net.");
210*4882a593Smuzhiyun 				goto dir_err_out;
211*4882a593Smuzhiyun 			}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 			if (type != FILE_NAME_DOS)
214*4882a593Smuzhiyun 				name_size += len * sizeof(ntfschar);
215*4882a593Smuzhiyun 			name = kmalloc(name_size, GFP_NOFS);
216*4882a593Smuzhiyun 			if (!name) {
217*4882a593Smuzhiyun 				err = -ENOMEM;
218*4882a593Smuzhiyun 				goto err_out;
219*4882a593Smuzhiyun 			}
220*4882a593Smuzhiyun 			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
221*4882a593Smuzhiyun 			name->type = type;
222*4882a593Smuzhiyun 			if (type != FILE_NAME_DOS) {
223*4882a593Smuzhiyun 				name->len = len;
224*4882a593Smuzhiyun 				memcpy(name->name, ie->key.file_name.file_name,
225*4882a593Smuzhiyun 						len * sizeof(ntfschar));
226*4882a593Smuzhiyun 			} else
227*4882a593Smuzhiyun 				name->len = 0;
228*4882a593Smuzhiyun 			*res = name;
229*4882a593Smuzhiyun 		}
230*4882a593Smuzhiyun 		/*
231*4882a593Smuzhiyun 		 * Not a perfect match, need to do full blown collation so we
232*4882a593Smuzhiyun 		 * know which way in the B+tree we have to go.
233*4882a593Smuzhiyun 		 */
234*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
235*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
236*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
237*4882a593Smuzhiyun 				IGNORE_CASE, vol->upcase, vol->upcase_len);
238*4882a593Smuzhiyun 		/*
239*4882a593Smuzhiyun 		 * If uname collates before the name of the current entry, there
240*4882a593Smuzhiyun 		 * is definitely no such name in this index but we might need to
241*4882a593Smuzhiyun 		 * descend into the B+tree so we just break out of the loop.
242*4882a593Smuzhiyun 		 */
243*4882a593Smuzhiyun 		if (rc == -1)
244*4882a593Smuzhiyun 			break;
245*4882a593Smuzhiyun 		/* The names are not equal, continue the search. */
246*4882a593Smuzhiyun 		if (rc)
247*4882a593Smuzhiyun 			continue;
248*4882a593Smuzhiyun 		/*
249*4882a593Smuzhiyun 		 * Names match with case insensitive comparison, now try the
250*4882a593Smuzhiyun 		 * case sensitive comparison, which is required for proper
251*4882a593Smuzhiyun 		 * collation.
252*4882a593Smuzhiyun 		 */
253*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
254*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
255*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
256*4882a593Smuzhiyun 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
257*4882a593Smuzhiyun 		if (rc == -1)
258*4882a593Smuzhiyun 			break;
259*4882a593Smuzhiyun 		if (rc)
260*4882a593Smuzhiyun 			continue;
261*4882a593Smuzhiyun 		/*
262*4882a593Smuzhiyun 		 * Perfect match, this will never happen as the
263*4882a593Smuzhiyun 		 * ntfs_are_names_equal() call will have gotten a match but we
264*4882a593Smuzhiyun 		 * still treat it correctly.
265*4882a593Smuzhiyun 		 */
266*4882a593Smuzhiyun 		goto found_it;
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 	/*
269*4882a593Smuzhiyun 	 * We have finished with this index without success. Check for the
270*4882a593Smuzhiyun 	 * presence of a child node and if not present return -ENOENT, unless
271*4882a593Smuzhiyun 	 * we have got a matching name cached in name in which case return the
272*4882a593Smuzhiyun 	 * mft reference associated with it.
273*4882a593Smuzhiyun 	 */
274*4882a593Smuzhiyun 	if (!(ie->flags & INDEX_ENTRY_NODE)) {
275*4882a593Smuzhiyun 		if (name) {
276*4882a593Smuzhiyun 			ntfs_attr_put_search_ctx(ctx);
277*4882a593Smuzhiyun 			unmap_mft_record(dir_ni);
278*4882a593Smuzhiyun 			return name->mref;
279*4882a593Smuzhiyun 		}
280*4882a593Smuzhiyun 		ntfs_debug("Entry not found.");
281*4882a593Smuzhiyun 		err = -ENOENT;
282*4882a593Smuzhiyun 		goto err_out;
283*4882a593Smuzhiyun 	} /* Child node present, descend into it. */
284*4882a593Smuzhiyun 	/* Consistency check: Verify that an index allocation exists. */
285*4882a593Smuzhiyun 	if (!NInoIndexAllocPresent(dir_ni)) {
286*4882a593Smuzhiyun 		ntfs_error(sb, "No index allocation attribute but index entry "
287*4882a593Smuzhiyun 				"requires one. Directory inode 0x%lx is "
288*4882a593Smuzhiyun 				"corrupt or driver bug.", dir_ni->mft_no);
289*4882a593Smuzhiyun 		goto err_out;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 	/* Get the starting vcn of the index_block holding the child node. */
292*4882a593Smuzhiyun 	vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
293*4882a593Smuzhiyun 	ia_mapping = VFS_I(dir_ni)->i_mapping;
294*4882a593Smuzhiyun 	/*
295*4882a593Smuzhiyun 	 * We are done with the index root and the mft record. Release them,
296*4882a593Smuzhiyun 	 * otherwise we deadlock with ntfs_map_page().
297*4882a593Smuzhiyun 	 */
298*4882a593Smuzhiyun 	ntfs_attr_put_search_ctx(ctx);
299*4882a593Smuzhiyun 	unmap_mft_record(dir_ni);
300*4882a593Smuzhiyun 	m = NULL;
301*4882a593Smuzhiyun 	ctx = NULL;
302*4882a593Smuzhiyun descend_into_child_node:
303*4882a593Smuzhiyun 	/*
304*4882a593Smuzhiyun 	 * Convert vcn to index into the index allocation attribute in units
305*4882a593Smuzhiyun 	 * of PAGE_SIZE and map the page cache page, reading it from
306*4882a593Smuzhiyun 	 * disk if necessary.
307*4882a593Smuzhiyun 	 */
308*4882a593Smuzhiyun 	page = ntfs_map_page(ia_mapping, vcn <<
309*4882a593Smuzhiyun 			dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
310*4882a593Smuzhiyun 	if (IS_ERR(page)) {
311*4882a593Smuzhiyun 		ntfs_error(sb, "Failed to map directory index page, error %ld.",
312*4882a593Smuzhiyun 				-PTR_ERR(page));
313*4882a593Smuzhiyun 		err = PTR_ERR(page);
314*4882a593Smuzhiyun 		goto err_out;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 	lock_page(page);
317*4882a593Smuzhiyun 	kaddr = (u8*)page_address(page);
318*4882a593Smuzhiyun fast_descend_into_child_node:
319*4882a593Smuzhiyun 	/* Get to the index allocation block. */
320*4882a593Smuzhiyun 	ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
321*4882a593Smuzhiyun 			dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
322*4882a593Smuzhiyun 	/* Bounds checks. */
323*4882a593Smuzhiyun 	if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
324*4882a593Smuzhiyun 		ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
325*4882a593Smuzhiyun 				"inode 0x%lx or driver bug.", dir_ni->mft_no);
326*4882a593Smuzhiyun 		goto unm_err_out;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 	/* Catch multi sector transfer fixup errors. */
329*4882a593Smuzhiyun 	if (unlikely(!ntfs_is_indx_record(ia->magic))) {
330*4882a593Smuzhiyun 		ntfs_error(sb, "Directory index record with vcn 0x%llx is "
331*4882a593Smuzhiyun 				"corrupt.  Corrupt inode 0x%lx.  Run chkdsk.",
332*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no);
333*4882a593Smuzhiyun 		goto unm_err_out;
334*4882a593Smuzhiyun 	}
335*4882a593Smuzhiyun 	if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
336*4882a593Smuzhiyun 		ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
337*4882a593Smuzhiyun 				"different from expected VCN (0x%llx). "
338*4882a593Smuzhiyun 				"Directory inode 0x%lx is corrupt or driver "
339*4882a593Smuzhiyun 				"bug.", (unsigned long long)
340*4882a593Smuzhiyun 				sle64_to_cpu(ia->index_block_vcn),
341*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no);
342*4882a593Smuzhiyun 		goto unm_err_out;
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 	if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
345*4882a593Smuzhiyun 			dir_ni->itype.index.block_size) {
346*4882a593Smuzhiyun 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
347*4882a593Smuzhiyun 				"0x%lx has a size (%u) differing from the "
348*4882a593Smuzhiyun 				"directory specified size (%u). Directory "
349*4882a593Smuzhiyun 				"inode is corrupt or driver bug.",
350*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no,
351*4882a593Smuzhiyun 				le32_to_cpu(ia->index.allocated_size) + 0x18,
352*4882a593Smuzhiyun 				dir_ni->itype.index.block_size);
353*4882a593Smuzhiyun 		goto unm_err_out;
354*4882a593Smuzhiyun 	}
355*4882a593Smuzhiyun 	index_end = (u8*)ia + dir_ni->itype.index.block_size;
356*4882a593Smuzhiyun 	if (index_end > kaddr + PAGE_SIZE) {
357*4882a593Smuzhiyun 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
358*4882a593Smuzhiyun 				"0x%lx crosses page boundary. Impossible! "
359*4882a593Smuzhiyun 				"Cannot access! This is probably a bug in the "
360*4882a593Smuzhiyun 				"driver.", (unsigned long long)vcn,
361*4882a593Smuzhiyun 				dir_ni->mft_no);
362*4882a593Smuzhiyun 		goto unm_err_out;
363*4882a593Smuzhiyun 	}
364*4882a593Smuzhiyun 	index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
365*4882a593Smuzhiyun 	if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
366*4882a593Smuzhiyun 		ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
367*4882a593Smuzhiyun 				"inode 0x%lx exceeds maximum size.",
368*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no);
369*4882a593Smuzhiyun 		goto unm_err_out;
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 	/* The first index entry. */
372*4882a593Smuzhiyun 	ie = (INDEX_ENTRY*)((u8*)&ia->index +
373*4882a593Smuzhiyun 			le32_to_cpu(ia->index.entries_offset));
374*4882a593Smuzhiyun 	/*
375*4882a593Smuzhiyun 	 * Iterate similar to above big loop but applied to index buffer, thus
376*4882a593Smuzhiyun 	 * loop until we exceed valid memory (corruption case) or until we
377*4882a593Smuzhiyun 	 * reach the last entry.
378*4882a593Smuzhiyun 	 */
379*4882a593Smuzhiyun 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
380*4882a593Smuzhiyun 		/* Bounds check. */
381*4882a593Smuzhiyun 		if ((u8*)ie < (u8*)ia || (u8*)ie +
382*4882a593Smuzhiyun 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
383*4882a593Smuzhiyun 				(u8*)ie + le16_to_cpu(ie->key_length) >
384*4882a593Smuzhiyun 				index_end) {
385*4882a593Smuzhiyun 			ntfs_error(sb, "Index entry out of bounds in "
386*4882a593Smuzhiyun 					"directory inode 0x%lx.",
387*4882a593Smuzhiyun 					dir_ni->mft_no);
388*4882a593Smuzhiyun 			goto unm_err_out;
389*4882a593Smuzhiyun 		}
390*4882a593Smuzhiyun 		/*
391*4882a593Smuzhiyun 		 * The last entry cannot contain a name. It can however contain
392*4882a593Smuzhiyun 		 * a pointer to a child node in the B+tree so we just break out.
393*4882a593Smuzhiyun 		 */
394*4882a593Smuzhiyun 		if (ie->flags & INDEX_ENTRY_END)
395*4882a593Smuzhiyun 			break;
396*4882a593Smuzhiyun 		/*
397*4882a593Smuzhiyun 		 * We perform a case sensitive comparison and if that matches
398*4882a593Smuzhiyun 		 * we are done and return the mft reference of the inode (i.e.
399*4882a593Smuzhiyun 		 * the inode number together with the sequence number for
400*4882a593Smuzhiyun 		 * consistency checking). We convert it to cpu format before
401*4882a593Smuzhiyun 		 * returning.
402*4882a593Smuzhiyun 		 */
403*4882a593Smuzhiyun 		if (ntfs_are_names_equal(uname, uname_len,
404*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
405*4882a593Smuzhiyun 				ie->key.file_name.file_name_length,
406*4882a593Smuzhiyun 				CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
407*4882a593Smuzhiyun found_it2:
408*4882a593Smuzhiyun 			/*
409*4882a593Smuzhiyun 			 * We have a perfect match, so we don't need to care
410*4882a593Smuzhiyun 			 * about having matched imperfectly before, so we can
411*4882a593Smuzhiyun 			 * free name and set *res to NULL.
412*4882a593Smuzhiyun 			 * However, if the perfect match is a short file name,
413*4882a593Smuzhiyun 			 * we need to signal this through *res, so that
414*4882a593Smuzhiyun 			 * ntfs_lookup() can fix dcache aliasing issues.
415*4882a593Smuzhiyun 			 * As an optimization we just reuse an existing
416*4882a593Smuzhiyun 			 * allocation of *res.
417*4882a593Smuzhiyun 			 */
418*4882a593Smuzhiyun 			if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
419*4882a593Smuzhiyun 				if (!name) {
420*4882a593Smuzhiyun 					name = kmalloc(sizeof(ntfs_name),
421*4882a593Smuzhiyun 							GFP_NOFS);
422*4882a593Smuzhiyun 					if (!name) {
423*4882a593Smuzhiyun 						err = -ENOMEM;
424*4882a593Smuzhiyun 						goto unm_err_out;
425*4882a593Smuzhiyun 					}
426*4882a593Smuzhiyun 				}
427*4882a593Smuzhiyun 				name->mref = le64_to_cpu(
428*4882a593Smuzhiyun 						ie->data.dir.indexed_file);
429*4882a593Smuzhiyun 				name->type = FILE_NAME_DOS;
430*4882a593Smuzhiyun 				name->len = 0;
431*4882a593Smuzhiyun 				*res = name;
432*4882a593Smuzhiyun 			} else {
433*4882a593Smuzhiyun 				kfree(name);
434*4882a593Smuzhiyun 				*res = NULL;
435*4882a593Smuzhiyun 			}
436*4882a593Smuzhiyun 			mref = le64_to_cpu(ie->data.dir.indexed_file);
437*4882a593Smuzhiyun 			unlock_page(page);
438*4882a593Smuzhiyun 			ntfs_unmap_page(page);
439*4882a593Smuzhiyun 			return mref;
440*4882a593Smuzhiyun 		}
441*4882a593Smuzhiyun 		/*
442*4882a593Smuzhiyun 		 * For a case insensitive mount, we also perform a case
443*4882a593Smuzhiyun 		 * insensitive comparison (provided the file name is not in the
444*4882a593Smuzhiyun 		 * POSIX namespace). If the comparison matches, and the name is
445*4882a593Smuzhiyun 		 * in the WIN32 namespace, we cache the filename in *res so
446*4882a593Smuzhiyun 		 * that the caller, ntfs_lookup(), can work on it. If the
447*4882a593Smuzhiyun 		 * comparison matches, and the name is in the DOS namespace, we
448*4882a593Smuzhiyun 		 * only cache the mft reference and the file name type (we set
449*4882a593Smuzhiyun 		 * the name length to zero for simplicity).
450*4882a593Smuzhiyun 		 */
451*4882a593Smuzhiyun 		if (!NVolCaseSensitive(vol) &&
452*4882a593Smuzhiyun 				ie->key.file_name.file_name_type &&
453*4882a593Smuzhiyun 				ntfs_are_names_equal(uname, uname_len,
454*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
455*4882a593Smuzhiyun 				ie->key.file_name.file_name_length,
456*4882a593Smuzhiyun 				IGNORE_CASE, vol->upcase, vol->upcase_len)) {
457*4882a593Smuzhiyun 			int name_size = sizeof(ntfs_name);
458*4882a593Smuzhiyun 			u8 type = ie->key.file_name.file_name_type;
459*4882a593Smuzhiyun 			u8 len = ie->key.file_name.file_name_length;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 			/* Only one case insensitive matching name allowed. */
462*4882a593Smuzhiyun 			if (name) {
463*4882a593Smuzhiyun 				ntfs_error(sb, "Found already allocated name "
464*4882a593Smuzhiyun 						"in phase 2. Please run chkdsk "
465*4882a593Smuzhiyun 						"and if that doesn't find any "
466*4882a593Smuzhiyun 						"errors please report you saw "
467*4882a593Smuzhiyun 						"this message to "
468*4882a593Smuzhiyun 						"linux-ntfs-dev@lists."
469*4882a593Smuzhiyun 						"sourceforge.net.");
470*4882a593Smuzhiyun 				unlock_page(page);
471*4882a593Smuzhiyun 				ntfs_unmap_page(page);
472*4882a593Smuzhiyun 				goto dir_err_out;
473*4882a593Smuzhiyun 			}
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 			if (type != FILE_NAME_DOS)
476*4882a593Smuzhiyun 				name_size += len * sizeof(ntfschar);
477*4882a593Smuzhiyun 			name = kmalloc(name_size, GFP_NOFS);
478*4882a593Smuzhiyun 			if (!name) {
479*4882a593Smuzhiyun 				err = -ENOMEM;
480*4882a593Smuzhiyun 				goto unm_err_out;
481*4882a593Smuzhiyun 			}
482*4882a593Smuzhiyun 			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
483*4882a593Smuzhiyun 			name->type = type;
484*4882a593Smuzhiyun 			if (type != FILE_NAME_DOS) {
485*4882a593Smuzhiyun 				name->len = len;
486*4882a593Smuzhiyun 				memcpy(name->name, ie->key.file_name.file_name,
487*4882a593Smuzhiyun 						len * sizeof(ntfschar));
488*4882a593Smuzhiyun 			} else
489*4882a593Smuzhiyun 				name->len = 0;
490*4882a593Smuzhiyun 			*res = name;
491*4882a593Smuzhiyun 		}
492*4882a593Smuzhiyun 		/*
493*4882a593Smuzhiyun 		 * Not a perfect match, need to do full blown collation so we
494*4882a593Smuzhiyun 		 * know which way in the B+tree we have to go.
495*4882a593Smuzhiyun 		 */
496*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
497*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
498*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
499*4882a593Smuzhiyun 				IGNORE_CASE, vol->upcase, vol->upcase_len);
500*4882a593Smuzhiyun 		/*
501*4882a593Smuzhiyun 		 * If uname collates before the name of the current entry, there
502*4882a593Smuzhiyun 		 * is definitely no such name in this index but we might need to
503*4882a593Smuzhiyun 		 * descend into the B+tree so we just break out of the loop.
504*4882a593Smuzhiyun 		 */
505*4882a593Smuzhiyun 		if (rc == -1)
506*4882a593Smuzhiyun 			break;
507*4882a593Smuzhiyun 		/* The names are not equal, continue the search. */
508*4882a593Smuzhiyun 		if (rc)
509*4882a593Smuzhiyun 			continue;
510*4882a593Smuzhiyun 		/*
511*4882a593Smuzhiyun 		 * Names match with case insensitive comparison, now try the
512*4882a593Smuzhiyun 		 * case sensitive comparison, which is required for proper
513*4882a593Smuzhiyun 		 * collation.
514*4882a593Smuzhiyun 		 */
515*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
516*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
517*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
518*4882a593Smuzhiyun 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
519*4882a593Smuzhiyun 		if (rc == -1)
520*4882a593Smuzhiyun 			break;
521*4882a593Smuzhiyun 		if (rc)
522*4882a593Smuzhiyun 			continue;
523*4882a593Smuzhiyun 		/*
524*4882a593Smuzhiyun 		 * Perfect match, this will never happen as the
525*4882a593Smuzhiyun 		 * ntfs_are_names_equal() call will have gotten a match but we
526*4882a593Smuzhiyun 		 * still treat it correctly.
527*4882a593Smuzhiyun 		 */
528*4882a593Smuzhiyun 		goto found_it2;
529*4882a593Smuzhiyun 	}
530*4882a593Smuzhiyun 	/*
531*4882a593Smuzhiyun 	 * We have finished with this index buffer without success. Check for
532*4882a593Smuzhiyun 	 * the presence of a child node.
533*4882a593Smuzhiyun 	 */
534*4882a593Smuzhiyun 	if (ie->flags & INDEX_ENTRY_NODE) {
535*4882a593Smuzhiyun 		if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
536*4882a593Smuzhiyun 			ntfs_error(sb, "Index entry with child node found in "
537*4882a593Smuzhiyun 					"a leaf node in directory inode 0x%lx.",
538*4882a593Smuzhiyun 					dir_ni->mft_no);
539*4882a593Smuzhiyun 			goto unm_err_out;
540*4882a593Smuzhiyun 		}
541*4882a593Smuzhiyun 		/* Child node present, descend into it. */
542*4882a593Smuzhiyun 		old_vcn = vcn;
543*4882a593Smuzhiyun 		vcn = sle64_to_cpup((sle64*)((u8*)ie +
544*4882a593Smuzhiyun 				le16_to_cpu(ie->length) - 8));
545*4882a593Smuzhiyun 		if (vcn >= 0) {
546*4882a593Smuzhiyun 			/* If vcn is in the same page cache page as old_vcn we
547*4882a593Smuzhiyun 			 * recycle the mapped page. */
548*4882a593Smuzhiyun 			if (old_vcn << vol->cluster_size_bits >>
549*4882a593Smuzhiyun 					PAGE_SHIFT == vcn <<
550*4882a593Smuzhiyun 					vol->cluster_size_bits >>
551*4882a593Smuzhiyun 					PAGE_SHIFT)
552*4882a593Smuzhiyun 				goto fast_descend_into_child_node;
553*4882a593Smuzhiyun 			unlock_page(page);
554*4882a593Smuzhiyun 			ntfs_unmap_page(page);
555*4882a593Smuzhiyun 			goto descend_into_child_node;
556*4882a593Smuzhiyun 		}
557*4882a593Smuzhiyun 		ntfs_error(sb, "Negative child node vcn in directory inode "
558*4882a593Smuzhiyun 				"0x%lx.", dir_ni->mft_no);
559*4882a593Smuzhiyun 		goto unm_err_out;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 	/*
562*4882a593Smuzhiyun 	 * No child node present, return -ENOENT, unless we have got a matching
563*4882a593Smuzhiyun 	 * name cached in name in which case return the mft reference
564*4882a593Smuzhiyun 	 * associated with it.
565*4882a593Smuzhiyun 	 */
566*4882a593Smuzhiyun 	if (name) {
567*4882a593Smuzhiyun 		unlock_page(page);
568*4882a593Smuzhiyun 		ntfs_unmap_page(page);
569*4882a593Smuzhiyun 		return name->mref;
570*4882a593Smuzhiyun 	}
571*4882a593Smuzhiyun 	ntfs_debug("Entry not found.");
572*4882a593Smuzhiyun 	err = -ENOENT;
573*4882a593Smuzhiyun unm_err_out:
574*4882a593Smuzhiyun 	unlock_page(page);
575*4882a593Smuzhiyun 	ntfs_unmap_page(page);
576*4882a593Smuzhiyun err_out:
577*4882a593Smuzhiyun 	if (!err)
578*4882a593Smuzhiyun 		err = -EIO;
579*4882a593Smuzhiyun 	if (ctx)
580*4882a593Smuzhiyun 		ntfs_attr_put_search_ctx(ctx);
581*4882a593Smuzhiyun 	if (m)
582*4882a593Smuzhiyun 		unmap_mft_record(dir_ni);
583*4882a593Smuzhiyun 	if (name) {
584*4882a593Smuzhiyun 		kfree(name);
585*4882a593Smuzhiyun 		*res = NULL;
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 	return ERR_MREF(err);
588*4882a593Smuzhiyun dir_err_out:
589*4882a593Smuzhiyun 	ntfs_error(sb, "Corrupt directory.  Aborting lookup.");
590*4882a593Smuzhiyun 	goto err_out;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun #if 0
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun // TODO: (AIA)
596*4882a593Smuzhiyun // The algorithm embedded in this code will be required for the time when we
597*4882a593Smuzhiyun // want to support adding of entries to directories, where we require correct
598*4882a593Smuzhiyun // collation of file names in order not to cause corruption of the filesystem.
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun /**
601*4882a593Smuzhiyun  * ntfs_lookup_inode_by_name - find an inode in a directory given its name
602*4882a593Smuzhiyun  * @dir_ni:	ntfs inode of the directory in which to search for the name
603*4882a593Smuzhiyun  * @uname:	Unicode name for which to search in the directory
604*4882a593Smuzhiyun  * @uname_len:	length of the name @uname in Unicode characters
605*4882a593Smuzhiyun  *
606*4882a593Smuzhiyun  * Look for an inode with name @uname in the directory with inode @dir_ni.
607*4882a593Smuzhiyun  * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
608*4882a593Smuzhiyun  * the Unicode name. If the name is found in the directory, the corresponding
609*4882a593Smuzhiyun  * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
610*4882a593Smuzhiyun  * is a 64-bit number containing the sequence number.
611*4882a593Smuzhiyun  *
612*4882a593Smuzhiyun  * On error, a negative value is returned corresponding to the error code. In
613*4882a593Smuzhiyun  * particular if the inode is not found -ENOENT is returned. Note that you
614*4882a593Smuzhiyun  * can't just check the return value for being negative, you have to check the
615*4882a593Smuzhiyun  * inode number for being negative which you can extract using MREC(return
616*4882a593Smuzhiyun  * value).
617*4882a593Smuzhiyun  *
618*4882a593Smuzhiyun  * Note, @uname_len does not include the (optional) terminating NULL character.
619*4882a593Smuzhiyun  */
620*4882a593Smuzhiyun u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
621*4882a593Smuzhiyun 		const int uname_len)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun 	ntfs_volume *vol = dir_ni->vol;
624*4882a593Smuzhiyun 	struct super_block *sb = vol->sb;
625*4882a593Smuzhiyun 	MFT_RECORD *m;
626*4882a593Smuzhiyun 	INDEX_ROOT *ir;
627*4882a593Smuzhiyun 	INDEX_ENTRY *ie;
628*4882a593Smuzhiyun 	INDEX_ALLOCATION *ia;
629*4882a593Smuzhiyun 	u8 *index_end;
630*4882a593Smuzhiyun 	u64 mref;
631*4882a593Smuzhiyun 	ntfs_attr_search_ctx *ctx;
632*4882a593Smuzhiyun 	int err, rc;
633*4882a593Smuzhiyun 	IGNORE_CASE_BOOL ic;
634*4882a593Smuzhiyun 	VCN vcn, old_vcn;
635*4882a593Smuzhiyun 	struct address_space *ia_mapping;
636*4882a593Smuzhiyun 	struct page *page;
637*4882a593Smuzhiyun 	u8 *kaddr;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	/* Get hold of the mft record for the directory. */
640*4882a593Smuzhiyun 	m = map_mft_record(dir_ni);
641*4882a593Smuzhiyun 	if (IS_ERR(m)) {
642*4882a593Smuzhiyun 		ntfs_error(sb, "map_mft_record() failed with error code %ld.",
643*4882a593Smuzhiyun 				-PTR_ERR(m));
644*4882a593Smuzhiyun 		return ERR_MREF(PTR_ERR(m));
645*4882a593Smuzhiyun 	}
646*4882a593Smuzhiyun 	ctx = ntfs_attr_get_search_ctx(dir_ni, m);
647*4882a593Smuzhiyun 	if (!ctx) {
648*4882a593Smuzhiyun 		err = -ENOMEM;
649*4882a593Smuzhiyun 		goto err_out;
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun 	/* Find the index root attribute in the mft record. */
652*4882a593Smuzhiyun 	err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
653*4882a593Smuzhiyun 			0, ctx);
654*4882a593Smuzhiyun 	if (unlikely(err)) {
655*4882a593Smuzhiyun 		if (err == -ENOENT) {
656*4882a593Smuzhiyun 			ntfs_error(sb, "Index root attribute missing in "
657*4882a593Smuzhiyun 					"directory inode 0x%lx.",
658*4882a593Smuzhiyun 					dir_ni->mft_no);
659*4882a593Smuzhiyun 			err = -EIO;
660*4882a593Smuzhiyun 		}
661*4882a593Smuzhiyun 		goto err_out;
662*4882a593Smuzhiyun 	}
663*4882a593Smuzhiyun 	/* Get to the index root value (it's been verified in read_inode). */
664*4882a593Smuzhiyun 	ir = (INDEX_ROOT*)((u8*)ctx->attr +
665*4882a593Smuzhiyun 			le16_to_cpu(ctx->attr->data.resident.value_offset));
666*4882a593Smuzhiyun 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
667*4882a593Smuzhiyun 	/* The first index entry. */
668*4882a593Smuzhiyun 	ie = (INDEX_ENTRY*)((u8*)&ir->index +
669*4882a593Smuzhiyun 			le32_to_cpu(ir->index.entries_offset));
670*4882a593Smuzhiyun 	/*
671*4882a593Smuzhiyun 	 * Loop until we exceed valid memory (corruption case) or until we
672*4882a593Smuzhiyun 	 * reach the last entry.
673*4882a593Smuzhiyun 	 */
674*4882a593Smuzhiyun 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
675*4882a593Smuzhiyun 		/* Bounds checks. */
676*4882a593Smuzhiyun 		if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
677*4882a593Smuzhiyun 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
678*4882a593Smuzhiyun 				(u8*)ie + le16_to_cpu(ie->key_length) >
679*4882a593Smuzhiyun 				index_end)
680*4882a593Smuzhiyun 			goto dir_err_out;
681*4882a593Smuzhiyun 		/*
682*4882a593Smuzhiyun 		 * The last entry cannot contain a name. It can however contain
683*4882a593Smuzhiyun 		 * a pointer to a child node in the B+tree so we just break out.
684*4882a593Smuzhiyun 		 */
685*4882a593Smuzhiyun 		if (ie->flags & INDEX_ENTRY_END)
686*4882a593Smuzhiyun 			break;
687*4882a593Smuzhiyun 		/*
688*4882a593Smuzhiyun 		 * If the current entry has a name type of POSIX, the name is
689*4882a593Smuzhiyun 		 * case sensitive and not otherwise. This has the effect of us
690*4882a593Smuzhiyun 		 * not being able to access any POSIX file names which collate
691*4882a593Smuzhiyun 		 * after the non-POSIX one when they only differ in case, but
692*4882a593Smuzhiyun 		 * anyone doing screwy stuff like that deserves to burn in
693*4882a593Smuzhiyun 		 * hell... Doing that kind of stuff on NT4 actually causes
694*4882a593Smuzhiyun 		 * corruption on the partition even when using SP6a and Linux
695*4882a593Smuzhiyun 		 * is not involved at all.
696*4882a593Smuzhiyun 		 */
697*4882a593Smuzhiyun 		ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
698*4882a593Smuzhiyun 				CASE_SENSITIVE;
699*4882a593Smuzhiyun 		/*
700*4882a593Smuzhiyun 		 * If the names match perfectly, we are done and return the
701*4882a593Smuzhiyun 		 * mft reference of the inode (i.e. the inode number together
702*4882a593Smuzhiyun 		 * with the sequence number for consistency checking. We
703*4882a593Smuzhiyun 		 * convert it to cpu format before returning.
704*4882a593Smuzhiyun 		 */
705*4882a593Smuzhiyun 		if (ntfs_are_names_equal(uname, uname_len,
706*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
707*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, ic,
708*4882a593Smuzhiyun 				vol->upcase, vol->upcase_len)) {
709*4882a593Smuzhiyun found_it:
710*4882a593Smuzhiyun 			mref = le64_to_cpu(ie->data.dir.indexed_file);
711*4882a593Smuzhiyun 			ntfs_attr_put_search_ctx(ctx);
712*4882a593Smuzhiyun 			unmap_mft_record(dir_ni);
713*4882a593Smuzhiyun 			return mref;
714*4882a593Smuzhiyun 		}
715*4882a593Smuzhiyun 		/*
716*4882a593Smuzhiyun 		 * Not a perfect match, need to do full blown collation so we
717*4882a593Smuzhiyun 		 * know which way in the B+tree we have to go.
718*4882a593Smuzhiyun 		 */
719*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
720*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
721*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
722*4882a593Smuzhiyun 				IGNORE_CASE, vol->upcase, vol->upcase_len);
723*4882a593Smuzhiyun 		/*
724*4882a593Smuzhiyun 		 * If uname collates before the name of the current entry, there
725*4882a593Smuzhiyun 		 * is definitely no such name in this index but we might need to
726*4882a593Smuzhiyun 		 * descend into the B+tree so we just break out of the loop.
727*4882a593Smuzhiyun 		 */
728*4882a593Smuzhiyun 		if (rc == -1)
729*4882a593Smuzhiyun 			break;
730*4882a593Smuzhiyun 		/* The names are not equal, continue the search. */
731*4882a593Smuzhiyun 		if (rc)
732*4882a593Smuzhiyun 			continue;
733*4882a593Smuzhiyun 		/*
734*4882a593Smuzhiyun 		 * Names match with case insensitive comparison, now try the
735*4882a593Smuzhiyun 		 * case sensitive comparison, which is required for proper
736*4882a593Smuzhiyun 		 * collation.
737*4882a593Smuzhiyun 		 */
738*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
739*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
740*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
741*4882a593Smuzhiyun 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
742*4882a593Smuzhiyun 		if (rc == -1)
743*4882a593Smuzhiyun 			break;
744*4882a593Smuzhiyun 		if (rc)
745*4882a593Smuzhiyun 			continue;
746*4882a593Smuzhiyun 		/*
747*4882a593Smuzhiyun 		 * Perfect match, this will never happen as the
748*4882a593Smuzhiyun 		 * ntfs_are_names_equal() call will have gotten a match but we
749*4882a593Smuzhiyun 		 * still treat it correctly.
750*4882a593Smuzhiyun 		 */
751*4882a593Smuzhiyun 		goto found_it;
752*4882a593Smuzhiyun 	}
753*4882a593Smuzhiyun 	/*
754*4882a593Smuzhiyun 	 * We have finished with this index without success. Check for the
755*4882a593Smuzhiyun 	 * presence of a child node.
756*4882a593Smuzhiyun 	 */
757*4882a593Smuzhiyun 	if (!(ie->flags & INDEX_ENTRY_NODE)) {
758*4882a593Smuzhiyun 		/* No child node, return -ENOENT. */
759*4882a593Smuzhiyun 		err = -ENOENT;
760*4882a593Smuzhiyun 		goto err_out;
761*4882a593Smuzhiyun 	} /* Child node present, descend into it. */
762*4882a593Smuzhiyun 	/* Consistency check: Verify that an index allocation exists. */
763*4882a593Smuzhiyun 	if (!NInoIndexAllocPresent(dir_ni)) {
764*4882a593Smuzhiyun 		ntfs_error(sb, "No index allocation attribute but index entry "
765*4882a593Smuzhiyun 				"requires one. Directory inode 0x%lx is "
766*4882a593Smuzhiyun 				"corrupt or driver bug.", dir_ni->mft_no);
767*4882a593Smuzhiyun 		goto err_out;
768*4882a593Smuzhiyun 	}
769*4882a593Smuzhiyun 	/* Get the starting vcn of the index_block holding the child node. */
770*4882a593Smuzhiyun 	vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
771*4882a593Smuzhiyun 	ia_mapping = VFS_I(dir_ni)->i_mapping;
772*4882a593Smuzhiyun 	/*
773*4882a593Smuzhiyun 	 * We are done with the index root and the mft record. Release them,
774*4882a593Smuzhiyun 	 * otherwise we deadlock with ntfs_map_page().
775*4882a593Smuzhiyun 	 */
776*4882a593Smuzhiyun 	ntfs_attr_put_search_ctx(ctx);
777*4882a593Smuzhiyun 	unmap_mft_record(dir_ni);
778*4882a593Smuzhiyun 	m = NULL;
779*4882a593Smuzhiyun 	ctx = NULL;
780*4882a593Smuzhiyun descend_into_child_node:
781*4882a593Smuzhiyun 	/*
782*4882a593Smuzhiyun 	 * Convert vcn to index into the index allocation attribute in units
783*4882a593Smuzhiyun 	 * of PAGE_SIZE and map the page cache page, reading it from
784*4882a593Smuzhiyun 	 * disk if necessary.
785*4882a593Smuzhiyun 	 */
786*4882a593Smuzhiyun 	page = ntfs_map_page(ia_mapping, vcn <<
787*4882a593Smuzhiyun 			dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
788*4882a593Smuzhiyun 	if (IS_ERR(page)) {
789*4882a593Smuzhiyun 		ntfs_error(sb, "Failed to map directory index page, error %ld.",
790*4882a593Smuzhiyun 				-PTR_ERR(page));
791*4882a593Smuzhiyun 		err = PTR_ERR(page);
792*4882a593Smuzhiyun 		goto err_out;
793*4882a593Smuzhiyun 	}
794*4882a593Smuzhiyun 	lock_page(page);
795*4882a593Smuzhiyun 	kaddr = (u8*)page_address(page);
796*4882a593Smuzhiyun fast_descend_into_child_node:
797*4882a593Smuzhiyun 	/* Get to the index allocation block. */
798*4882a593Smuzhiyun 	ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
799*4882a593Smuzhiyun 			dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
800*4882a593Smuzhiyun 	/* Bounds checks. */
801*4882a593Smuzhiyun 	if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
802*4882a593Smuzhiyun 		ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
803*4882a593Smuzhiyun 				"inode 0x%lx or driver bug.", dir_ni->mft_no);
804*4882a593Smuzhiyun 		goto unm_err_out;
805*4882a593Smuzhiyun 	}
806*4882a593Smuzhiyun 	/* Catch multi sector transfer fixup errors. */
807*4882a593Smuzhiyun 	if (unlikely(!ntfs_is_indx_record(ia->magic))) {
808*4882a593Smuzhiyun 		ntfs_error(sb, "Directory index record with vcn 0x%llx is "
809*4882a593Smuzhiyun 				"corrupt.  Corrupt inode 0x%lx.  Run chkdsk.",
810*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no);
811*4882a593Smuzhiyun 		goto unm_err_out;
812*4882a593Smuzhiyun 	}
813*4882a593Smuzhiyun 	if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
814*4882a593Smuzhiyun 		ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
815*4882a593Smuzhiyun 				"different from expected VCN (0x%llx). "
816*4882a593Smuzhiyun 				"Directory inode 0x%lx is corrupt or driver "
817*4882a593Smuzhiyun 				"bug.", (unsigned long long)
818*4882a593Smuzhiyun 				sle64_to_cpu(ia->index_block_vcn),
819*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no);
820*4882a593Smuzhiyun 		goto unm_err_out;
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 	if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
823*4882a593Smuzhiyun 			dir_ni->itype.index.block_size) {
824*4882a593Smuzhiyun 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
825*4882a593Smuzhiyun 				"0x%lx has a size (%u) differing from the "
826*4882a593Smuzhiyun 				"directory specified size (%u). Directory "
827*4882a593Smuzhiyun 				"inode is corrupt or driver bug.",
828*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no,
829*4882a593Smuzhiyun 				le32_to_cpu(ia->index.allocated_size) + 0x18,
830*4882a593Smuzhiyun 				dir_ni->itype.index.block_size);
831*4882a593Smuzhiyun 		goto unm_err_out;
832*4882a593Smuzhiyun 	}
833*4882a593Smuzhiyun 	index_end = (u8*)ia + dir_ni->itype.index.block_size;
834*4882a593Smuzhiyun 	if (index_end > kaddr + PAGE_SIZE) {
835*4882a593Smuzhiyun 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
836*4882a593Smuzhiyun 				"0x%lx crosses page boundary. Impossible! "
837*4882a593Smuzhiyun 				"Cannot access! This is probably a bug in the "
838*4882a593Smuzhiyun 				"driver.", (unsigned long long)vcn,
839*4882a593Smuzhiyun 				dir_ni->mft_no);
840*4882a593Smuzhiyun 		goto unm_err_out;
841*4882a593Smuzhiyun 	}
842*4882a593Smuzhiyun 	index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
843*4882a593Smuzhiyun 	if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
844*4882a593Smuzhiyun 		ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
845*4882a593Smuzhiyun 				"inode 0x%lx exceeds maximum size.",
846*4882a593Smuzhiyun 				(unsigned long long)vcn, dir_ni->mft_no);
847*4882a593Smuzhiyun 		goto unm_err_out;
848*4882a593Smuzhiyun 	}
849*4882a593Smuzhiyun 	/* The first index entry. */
850*4882a593Smuzhiyun 	ie = (INDEX_ENTRY*)((u8*)&ia->index +
851*4882a593Smuzhiyun 			le32_to_cpu(ia->index.entries_offset));
852*4882a593Smuzhiyun 	/*
853*4882a593Smuzhiyun 	 * Iterate similar to above big loop but applied to index buffer, thus
854*4882a593Smuzhiyun 	 * loop until we exceed valid memory (corruption case) or until we
855*4882a593Smuzhiyun 	 * reach the last entry.
856*4882a593Smuzhiyun 	 */
857*4882a593Smuzhiyun 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
858*4882a593Smuzhiyun 		/* Bounds check. */
859*4882a593Smuzhiyun 		if ((u8*)ie < (u8*)ia || (u8*)ie +
860*4882a593Smuzhiyun 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
861*4882a593Smuzhiyun 				(u8*)ie + le16_to_cpu(ie->key_length) >
862*4882a593Smuzhiyun 				index_end) {
863*4882a593Smuzhiyun 			ntfs_error(sb, "Index entry out of bounds in "
864*4882a593Smuzhiyun 					"directory inode 0x%lx.",
865*4882a593Smuzhiyun 					dir_ni->mft_no);
866*4882a593Smuzhiyun 			goto unm_err_out;
867*4882a593Smuzhiyun 		}
868*4882a593Smuzhiyun 		/*
869*4882a593Smuzhiyun 		 * The last entry cannot contain a name. It can however contain
870*4882a593Smuzhiyun 		 * a pointer to a child node in the B+tree so we just break out.
871*4882a593Smuzhiyun 		 */
872*4882a593Smuzhiyun 		if (ie->flags & INDEX_ENTRY_END)
873*4882a593Smuzhiyun 			break;
874*4882a593Smuzhiyun 		/*
875*4882a593Smuzhiyun 		 * If the current entry has a name type of POSIX, the name is
876*4882a593Smuzhiyun 		 * case sensitive and not otherwise. This has the effect of us
877*4882a593Smuzhiyun 		 * not being able to access any POSIX file names which collate
878*4882a593Smuzhiyun 		 * after the non-POSIX one when they only differ in case, but
879*4882a593Smuzhiyun 		 * anyone doing screwy stuff like that deserves to burn in
880*4882a593Smuzhiyun 		 * hell... Doing that kind of stuff on NT4 actually causes
881*4882a593Smuzhiyun 		 * corruption on the partition even when using SP6a and Linux
882*4882a593Smuzhiyun 		 * is not involved at all.
883*4882a593Smuzhiyun 		 */
884*4882a593Smuzhiyun 		ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
885*4882a593Smuzhiyun 				CASE_SENSITIVE;
886*4882a593Smuzhiyun 		/*
887*4882a593Smuzhiyun 		 * If the names match perfectly, we are done and return the
888*4882a593Smuzhiyun 		 * mft reference of the inode (i.e. the inode number together
889*4882a593Smuzhiyun 		 * with the sequence number for consistency checking. We
890*4882a593Smuzhiyun 		 * convert it to cpu format before returning.
891*4882a593Smuzhiyun 		 */
892*4882a593Smuzhiyun 		if (ntfs_are_names_equal(uname, uname_len,
893*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
894*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, ic,
895*4882a593Smuzhiyun 				vol->upcase, vol->upcase_len)) {
896*4882a593Smuzhiyun found_it2:
897*4882a593Smuzhiyun 			mref = le64_to_cpu(ie->data.dir.indexed_file);
898*4882a593Smuzhiyun 			unlock_page(page);
899*4882a593Smuzhiyun 			ntfs_unmap_page(page);
900*4882a593Smuzhiyun 			return mref;
901*4882a593Smuzhiyun 		}
902*4882a593Smuzhiyun 		/*
903*4882a593Smuzhiyun 		 * Not a perfect match, need to do full blown collation so we
904*4882a593Smuzhiyun 		 * know which way in the B+tree we have to go.
905*4882a593Smuzhiyun 		 */
906*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
907*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
908*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
909*4882a593Smuzhiyun 				IGNORE_CASE, vol->upcase, vol->upcase_len);
910*4882a593Smuzhiyun 		/*
911*4882a593Smuzhiyun 		 * If uname collates before the name of the current entry, there
912*4882a593Smuzhiyun 		 * is definitely no such name in this index but we might need to
913*4882a593Smuzhiyun 		 * descend into the B+tree so we just break out of the loop.
914*4882a593Smuzhiyun 		 */
915*4882a593Smuzhiyun 		if (rc == -1)
916*4882a593Smuzhiyun 			break;
917*4882a593Smuzhiyun 		/* The names are not equal, continue the search. */
918*4882a593Smuzhiyun 		if (rc)
919*4882a593Smuzhiyun 			continue;
920*4882a593Smuzhiyun 		/*
921*4882a593Smuzhiyun 		 * Names match with case insensitive comparison, now try the
922*4882a593Smuzhiyun 		 * case sensitive comparison, which is required for proper
923*4882a593Smuzhiyun 		 * collation.
924*4882a593Smuzhiyun 		 */
925*4882a593Smuzhiyun 		rc = ntfs_collate_names(uname, uname_len,
926*4882a593Smuzhiyun 				(ntfschar*)&ie->key.file_name.file_name,
927*4882a593Smuzhiyun 				ie->key.file_name.file_name_length, 1,
928*4882a593Smuzhiyun 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
929*4882a593Smuzhiyun 		if (rc == -1)
930*4882a593Smuzhiyun 			break;
931*4882a593Smuzhiyun 		if (rc)
932*4882a593Smuzhiyun 			continue;
933*4882a593Smuzhiyun 		/*
934*4882a593Smuzhiyun 		 * Perfect match, this will never happen as the
935*4882a593Smuzhiyun 		 * ntfs_are_names_equal() call will have gotten a match but we
936*4882a593Smuzhiyun 		 * still treat it correctly.
937*4882a593Smuzhiyun 		 */
938*4882a593Smuzhiyun 		goto found_it2;
939*4882a593Smuzhiyun 	}
940*4882a593Smuzhiyun 	/*
941*4882a593Smuzhiyun 	 * We have finished with this index buffer without success. Check for
942*4882a593Smuzhiyun 	 * the presence of a child node.
943*4882a593Smuzhiyun 	 */
944*4882a593Smuzhiyun 	if (ie->flags & INDEX_ENTRY_NODE) {
945*4882a593Smuzhiyun 		if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
946*4882a593Smuzhiyun 			ntfs_error(sb, "Index entry with child node found in "
947*4882a593Smuzhiyun 					"a leaf node in directory inode 0x%lx.",
948*4882a593Smuzhiyun 					dir_ni->mft_no);
949*4882a593Smuzhiyun 			goto unm_err_out;
950*4882a593Smuzhiyun 		}
951*4882a593Smuzhiyun 		/* Child node present, descend into it. */
952*4882a593Smuzhiyun 		old_vcn = vcn;
953*4882a593Smuzhiyun 		vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
954*4882a593Smuzhiyun 		if (vcn >= 0) {
955*4882a593Smuzhiyun 			/* If vcn is in the same page cache page as old_vcn we
956*4882a593Smuzhiyun 			 * recycle the mapped page. */
957*4882a593Smuzhiyun 			if (old_vcn << vol->cluster_size_bits >>
958*4882a593Smuzhiyun 					PAGE_SHIFT == vcn <<
959*4882a593Smuzhiyun 					vol->cluster_size_bits >>
960*4882a593Smuzhiyun 					PAGE_SHIFT)
961*4882a593Smuzhiyun 				goto fast_descend_into_child_node;
962*4882a593Smuzhiyun 			unlock_page(page);
963*4882a593Smuzhiyun 			ntfs_unmap_page(page);
964*4882a593Smuzhiyun 			goto descend_into_child_node;
965*4882a593Smuzhiyun 		}
966*4882a593Smuzhiyun 		ntfs_error(sb, "Negative child node vcn in directory inode "
967*4882a593Smuzhiyun 				"0x%lx.", dir_ni->mft_no);
968*4882a593Smuzhiyun 		goto unm_err_out;
969*4882a593Smuzhiyun 	}
970*4882a593Smuzhiyun 	/* No child node, return -ENOENT. */
971*4882a593Smuzhiyun 	ntfs_debug("Entry not found.");
972*4882a593Smuzhiyun 	err = -ENOENT;
973*4882a593Smuzhiyun unm_err_out:
974*4882a593Smuzhiyun 	unlock_page(page);
975*4882a593Smuzhiyun 	ntfs_unmap_page(page);
976*4882a593Smuzhiyun err_out:
977*4882a593Smuzhiyun 	if (!err)
978*4882a593Smuzhiyun 		err = -EIO;
979*4882a593Smuzhiyun 	if (ctx)
980*4882a593Smuzhiyun 		ntfs_attr_put_search_ctx(ctx);
981*4882a593Smuzhiyun 	if (m)
982*4882a593Smuzhiyun 		unmap_mft_record(dir_ni);
983*4882a593Smuzhiyun 	return ERR_MREF(err);
984*4882a593Smuzhiyun dir_err_out:
985*4882a593Smuzhiyun 	ntfs_error(sb, "Corrupt directory. Aborting lookup.");
986*4882a593Smuzhiyun 	goto err_out;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun #endif
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun /**
992*4882a593Smuzhiyun  * ntfs_filldir - ntfs specific filldir method
993*4882a593Smuzhiyun  * @vol:	current ntfs volume
994*4882a593Smuzhiyun  * @ndir:	ntfs inode of current directory
995*4882a593Smuzhiyun  * @ia_page:	page in which the index allocation buffer @ie is in resides
996*4882a593Smuzhiyun  * @ie:		current index entry
997*4882a593Smuzhiyun  * @name:	buffer to use for the converted name
998*4882a593Smuzhiyun  * @actor:	what to feed the entries to
999*4882a593Smuzhiyun  *
1000*4882a593Smuzhiyun  * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
1001*4882a593Smuzhiyun  * callback.
1002*4882a593Smuzhiyun  *
1003*4882a593Smuzhiyun  * If @ia_page is not NULL it is the locked page containing the index
1004*4882a593Smuzhiyun  * allocation block containing the index entry @ie.
1005*4882a593Smuzhiyun  *
1006*4882a593Smuzhiyun  * Note, we drop (and then reacquire) the page lock on @ia_page across the
1007*4882a593Smuzhiyun  * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
1008*4882a593Smuzhiyun  * since ntfs_lookup() will lock the same page.  As an optimization, we do not
1009*4882a593Smuzhiyun  * retake the lock if we are returning a non-zero value as ntfs_readdir()
1010*4882a593Smuzhiyun  * would need to drop the lock immediately anyway.
1011*4882a593Smuzhiyun  */
ntfs_filldir(ntfs_volume * vol,ntfs_inode * ndir,struct page * ia_page,INDEX_ENTRY * ie,u8 * name,struct dir_context * actor)1012*4882a593Smuzhiyun static inline int ntfs_filldir(ntfs_volume *vol,
1013*4882a593Smuzhiyun 		ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie,
1014*4882a593Smuzhiyun 		u8 *name, struct dir_context *actor)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	unsigned long mref;
1017*4882a593Smuzhiyun 	int name_len;
1018*4882a593Smuzhiyun 	unsigned dt_type;
1019*4882a593Smuzhiyun 	FILE_NAME_TYPE_FLAGS name_type;
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	name_type = ie->key.file_name.file_name_type;
1022*4882a593Smuzhiyun 	if (name_type == FILE_NAME_DOS) {
1023*4882a593Smuzhiyun 		ntfs_debug("Skipping DOS name space entry.");
1024*4882a593Smuzhiyun 		return 0;
1025*4882a593Smuzhiyun 	}
1026*4882a593Smuzhiyun 	if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
1027*4882a593Smuzhiyun 		ntfs_debug("Skipping root directory self reference entry.");
1028*4882a593Smuzhiyun 		return 0;
1029*4882a593Smuzhiyun 	}
1030*4882a593Smuzhiyun 	if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
1031*4882a593Smuzhiyun 			!NVolShowSystemFiles(vol)) {
1032*4882a593Smuzhiyun 		ntfs_debug("Skipping system file.");
1033*4882a593Smuzhiyun 		return 0;
1034*4882a593Smuzhiyun 	}
1035*4882a593Smuzhiyun 	name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
1036*4882a593Smuzhiyun 			ie->key.file_name.file_name_length, &name,
1037*4882a593Smuzhiyun 			NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
1038*4882a593Smuzhiyun 	if (name_len <= 0) {
1039*4882a593Smuzhiyun 		ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
1040*4882a593Smuzhiyun 				(long long)MREF_LE(ie->data.dir.indexed_file));
1041*4882a593Smuzhiyun 		return 0;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 	if (ie->key.file_name.file_attributes &
1044*4882a593Smuzhiyun 			FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
1045*4882a593Smuzhiyun 		dt_type = DT_DIR;
1046*4882a593Smuzhiyun 	else
1047*4882a593Smuzhiyun 		dt_type = DT_REG;
1048*4882a593Smuzhiyun 	mref = MREF_LE(ie->data.dir.indexed_file);
1049*4882a593Smuzhiyun 	/*
1050*4882a593Smuzhiyun 	 * Drop the page lock otherwise we deadlock with NFS when it calls
1051*4882a593Smuzhiyun 	 * ->lookup since ntfs_lookup() will lock the same page.
1052*4882a593Smuzhiyun 	 */
1053*4882a593Smuzhiyun 	if (ia_page)
1054*4882a593Smuzhiyun 		unlock_page(ia_page);
1055*4882a593Smuzhiyun 	ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
1056*4882a593Smuzhiyun 			"0x%lx, DT_%s.", name, name_len, actor->pos, mref,
1057*4882a593Smuzhiyun 			dt_type == DT_DIR ? "DIR" : "REG");
1058*4882a593Smuzhiyun 	if (!dir_emit(actor, name, name_len, mref, dt_type))
1059*4882a593Smuzhiyun 		return 1;
1060*4882a593Smuzhiyun 	/* Relock the page but not if we are aborting ->readdir. */
1061*4882a593Smuzhiyun 	if (ia_page)
1062*4882a593Smuzhiyun 		lock_page(ia_page);
1063*4882a593Smuzhiyun 	return 0;
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun /*
1067*4882a593Smuzhiyun  * We use the same basic approach as the old NTFS driver, i.e. we parse the
1068*4882a593Smuzhiyun  * index root entries and then the index allocation entries that are marked
1069*4882a593Smuzhiyun  * as in use in the index bitmap.
1070*4882a593Smuzhiyun  *
1071*4882a593Smuzhiyun  * While this will return the names in random order this doesn't matter for
1072*4882a593Smuzhiyun  * ->readdir but OTOH results in a faster ->readdir.
1073*4882a593Smuzhiyun  *
1074*4882a593Smuzhiyun  * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
1075*4882a593Smuzhiyun  * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1076*4882a593Smuzhiyun  * modifications).
1077*4882a593Smuzhiyun  *
1078*4882a593Smuzhiyun  * Locking:  - Caller must hold i_mutex on the directory.
1079*4882a593Smuzhiyun  *	     - Each page cache page in the index allocation mapping must be
1080*4882a593Smuzhiyun  *	       locked whilst being accessed otherwise we may find a corrupt
1081*4882a593Smuzhiyun  *	       page due to it being under ->writepage at the moment which
1082*4882a593Smuzhiyun  *	       applies the mst protection fixups before writing out and then
1083*4882a593Smuzhiyun  *	       removes them again after the write is complete after which it
1084*4882a593Smuzhiyun  *	       unlocks the page.
1085*4882a593Smuzhiyun  */
ntfs_readdir(struct file * file,struct dir_context * actor)1086*4882a593Smuzhiyun static int ntfs_readdir(struct file *file, struct dir_context *actor)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun 	s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1089*4882a593Smuzhiyun 	loff_t i_size;
1090*4882a593Smuzhiyun 	struct inode *bmp_vi, *vdir = file_inode(file);
1091*4882a593Smuzhiyun 	struct super_block *sb = vdir->i_sb;
1092*4882a593Smuzhiyun 	ntfs_inode *ndir = NTFS_I(vdir);
1093*4882a593Smuzhiyun 	ntfs_volume *vol = NTFS_SB(sb);
1094*4882a593Smuzhiyun 	MFT_RECORD *m;
1095*4882a593Smuzhiyun 	INDEX_ROOT *ir = NULL;
1096*4882a593Smuzhiyun 	INDEX_ENTRY *ie;
1097*4882a593Smuzhiyun 	INDEX_ALLOCATION *ia;
1098*4882a593Smuzhiyun 	u8 *name = NULL;
1099*4882a593Smuzhiyun 	int rc, err, ir_pos, cur_bmp_pos;
1100*4882a593Smuzhiyun 	struct address_space *ia_mapping, *bmp_mapping;
1101*4882a593Smuzhiyun 	struct page *bmp_page = NULL, *ia_page = NULL;
1102*4882a593Smuzhiyun 	u8 *kaddr, *bmp, *index_end;
1103*4882a593Smuzhiyun 	ntfs_attr_search_ctx *ctx;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
1106*4882a593Smuzhiyun 			vdir->i_ino, actor->pos);
1107*4882a593Smuzhiyun 	rc = err = 0;
1108*4882a593Smuzhiyun 	/* Are we at end of dir yet? */
1109*4882a593Smuzhiyun 	i_size = i_size_read(vdir);
1110*4882a593Smuzhiyun 	if (actor->pos >= i_size + vol->mft_record_size)
1111*4882a593Smuzhiyun 		return 0;
1112*4882a593Smuzhiyun 	/* Emulate . and .. for all directories. */
1113*4882a593Smuzhiyun 	if (!dir_emit_dots(file, actor))
1114*4882a593Smuzhiyun 		return 0;
1115*4882a593Smuzhiyun 	m = NULL;
1116*4882a593Smuzhiyun 	ctx = NULL;
1117*4882a593Smuzhiyun 	/*
1118*4882a593Smuzhiyun 	 * Allocate a buffer to store the current name being processed
1119*4882a593Smuzhiyun 	 * converted to format determined by current NLS.
1120*4882a593Smuzhiyun 	 */
1121*4882a593Smuzhiyun 	name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
1122*4882a593Smuzhiyun 	if (unlikely(!name)) {
1123*4882a593Smuzhiyun 		err = -ENOMEM;
1124*4882a593Smuzhiyun 		goto err_out;
1125*4882a593Smuzhiyun 	}
1126*4882a593Smuzhiyun 	/* Are we jumping straight into the index allocation attribute? */
1127*4882a593Smuzhiyun 	if (actor->pos >= vol->mft_record_size)
1128*4882a593Smuzhiyun 		goto skip_index_root;
1129*4882a593Smuzhiyun 	/* Get hold of the mft record for the directory. */
1130*4882a593Smuzhiyun 	m = map_mft_record(ndir);
1131*4882a593Smuzhiyun 	if (IS_ERR(m)) {
1132*4882a593Smuzhiyun 		err = PTR_ERR(m);
1133*4882a593Smuzhiyun 		m = NULL;
1134*4882a593Smuzhiyun 		goto err_out;
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun 	ctx = ntfs_attr_get_search_ctx(ndir, m);
1137*4882a593Smuzhiyun 	if (unlikely(!ctx)) {
1138*4882a593Smuzhiyun 		err = -ENOMEM;
1139*4882a593Smuzhiyun 		goto err_out;
1140*4882a593Smuzhiyun 	}
1141*4882a593Smuzhiyun 	/* Get the offset into the index root attribute. */
1142*4882a593Smuzhiyun 	ir_pos = (s64)actor->pos;
1143*4882a593Smuzhiyun 	/* Find the index root attribute in the mft record. */
1144*4882a593Smuzhiyun 	err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
1145*4882a593Smuzhiyun 			0, ctx);
1146*4882a593Smuzhiyun 	if (unlikely(err)) {
1147*4882a593Smuzhiyun 		ntfs_error(sb, "Index root attribute missing in directory "
1148*4882a593Smuzhiyun 				"inode 0x%lx.", vdir->i_ino);
1149*4882a593Smuzhiyun 		goto err_out;
1150*4882a593Smuzhiyun 	}
1151*4882a593Smuzhiyun 	/*
1152*4882a593Smuzhiyun 	 * Copy the index root attribute value to a buffer so that we can put
1153*4882a593Smuzhiyun 	 * the search context and unmap the mft record before calling the
1154*4882a593Smuzhiyun 	 * filldir() callback.  We need to do this because of NFSd which calls
1155*4882a593Smuzhiyun 	 * ->lookup() from its filldir callback() and this causes NTFS to
1156*4882a593Smuzhiyun 	 * deadlock as ntfs_lookup() maps the mft record of the directory and
1157*4882a593Smuzhiyun 	 * we have got it mapped here already.  The only solution is for us to
1158*4882a593Smuzhiyun 	 * unmap the mft record here so that a call to ntfs_lookup() is able to
1159*4882a593Smuzhiyun 	 * map the mft record without deadlocking.
1160*4882a593Smuzhiyun 	 */
1161*4882a593Smuzhiyun 	rc = le32_to_cpu(ctx->attr->data.resident.value_length);
1162*4882a593Smuzhiyun 	ir = kmalloc(rc, GFP_NOFS);
1163*4882a593Smuzhiyun 	if (unlikely(!ir)) {
1164*4882a593Smuzhiyun 		err = -ENOMEM;
1165*4882a593Smuzhiyun 		goto err_out;
1166*4882a593Smuzhiyun 	}
1167*4882a593Smuzhiyun 	/* Copy the index root value (it has been verified in read_inode). */
1168*4882a593Smuzhiyun 	memcpy(ir, (u8*)ctx->attr +
1169*4882a593Smuzhiyun 			le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
1170*4882a593Smuzhiyun 	ntfs_attr_put_search_ctx(ctx);
1171*4882a593Smuzhiyun 	unmap_mft_record(ndir);
1172*4882a593Smuzhiyun 	ctx = NULL;
1173*4882a593Smuzhiyun 	m = NULL;
1174*4882a593Smuzhiyun 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1175*4882a593Smuzhiyun 	/* The first index entry. */
1176*4882a593Smuzhiyun 	ie = (INDEX_ENTRY*)((u8*)&ir->index +
1177*4882a593Smuzhiyun 			le32_to_cpu(ir->index.entries_offset));
1178*4882a593Smuzhiyun 	/*
1179*4882a593Smuzhiyun 	 * Loop until we exceed valid memory (corruption case) or until we
1180*4882a593Smuzhiyun 	 * reach the last entry or until filldir tells us it has had enough
1181*4882a593Smuzhiyun 	 * or signals an error (both covered by the rc test).
1182*4882a593Smuzhiyun 	 */
1183*4882a593Smuzhiyun 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1184*4882a593Smuzhiyun 		ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir);
1185*4882a593Smuzhiyun 		/* Bounds checks. */
1186*4882a593Smuzhiyun 		if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
1187*4882a593Smuzhiyun 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
1188*4882a593Smuzhiyun 				(u8*)ie + le16_to_cpu(ie->key_length) >
1189*4882a593Smuzhiyun 				index_end))
1190*4882a593Smuzhiyun 			goto err_out;
1191*4882a593Smuzhiyun 		/* The last entry cannot contain a name. */
1192*4882a593Smuzhiyun 		if (ie->flags & INDEX_ENTRY_END)
1193*4882a593Smuzhiyun 			break;
1194*4882a593Smuzhiyun 		/* Skip index root entry if continuing previous readdir. */
1195*4882a593Smuzhiyun 		if (ir_pos > (u8*)ie - (u8*)ir)
1196*4882a593Smuzhiyun 			continue;
1197*4882a593Smuzhiyun 		/* Advance the position even if going to skip the entry. */
1198*4882a593Smuzhiyun 		actor->pos = (u8*)ie - (u8*)ir;
1199*4882a593Smuzhiyun 		/* Submit the name to the filldir callback. */
1200*4882a593Smuzhiyun 		rc = ntfs_filldir(vol, ndir, NULL, ie, name, actor);
1201*4882a593Smuzhiyun 		if (rc) {
1202*4882a593Smuzhiyun 			kfree(ir);
1203*4882a593Smuzhiyun 			goto abort;
1204*4882a593Smuzhiyun 		}
1205*4882a593Smuzhiyun 	}
1206*4882a593Smuzhiyun 	/* We are done with the index root and can free the buffer. */
1207*4882a593Smuzhiyun 	kfree(ir);
1208*4882a593Smuzhiyun 	ir = NULL;
1209*4882a593Smuzhiyun 	/* If there is no index allocation attribute we are finished. */
1210*4882a593Smuzhiyun 	if (!NInoIndexAllocPresent(ndir))
1211*4882a593Smuzhiyun 		goto EOD;
1212*4882a593Smuzhiyun 	/* Advance fpos to the beginning of the index allocation. */
1213*4882a593Smuzhiyun 	actor->pos = vol->mft_record_size;
1214*4882a593Smuzhiyun skip_index_root:
1215*4882a593Smuzhiyun 	kaddr = NULL;
1216*4882a593Smuzhiyun 	prev_ia_pos = -1LL;
1217*4882a593Smuzhiyun 	/* Get the offset into the index allocation attribute. */
1218*4882a593Smuzhiyun 	ia_pos = (s64)actor->pos - vol->mft_record_size;
1219*4882a593Smuzhiyun 	ia_mapping = vdir->i_mapping;
1220*4882a593Smuzhiyun 	ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino);
1221*4882a593Smuzhiyun 	bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
1222*4882a593Smuzhiyun 	if (IS_ERR(bmp_vi)) {
1223*4882a593Smuzhiyun 		ntfs_error(sb, "Failed to get bitmap attribute.");
1224*4882a593Smuzhiyun 		err = PTR_ERR(bmp_vi);
1225*4882a593Smuzhiyun 		goto err_out;
1226*4882a593Smuzhiyun 	}
1227*4882a593Smuzhiyun 	bmp_mapping = bmp_vi->i_mapping;
1228*4882a593Smuzhiyun 	/* Get the starting bitmap bit position and sanity check it. */
1229*4882a593Smuzhiyun 	bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1230*4882a593Smuzhiyun 	if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
1231*4882a593Smuzhiyun 		ntfs_error(sb, "Current index allocation position exceeds "
1232*4882a593Smuzhiyun 				"index bitmap size.");
1233*4882a593Smuzhiyun 		goto iput_err_out;
1234*4882a593Smuzhiyun 	}
1235*4882a593Smuzhiyun 	/* Get the starting bit position in the current bitmap page. */
1236*4882a593Smuzhiyun 	cur_bmp_pos = bmp_pos & ((PAGE_SIZE * 8) - 1);
1237*4882a593Smuzhiyun 	bmp_pos &= ~(u64)((PAGE_SIZE * 8) - 1);
1238*4882a593Smuzhiyun get_next_bmp_page:
1239*4882a593Smuzhiyun 	ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1240*4882a593Smuzhiyun 			(unsigned long long)bmp_pos >> (3 + PAGE_SHIFT),
1241*4882a593Smuzhiyun 			(unsigned long long)bmp_pos &
1242*4882a593Smuzhiyun 			(unsigned long long)((PAGE_SIZE * 8) - 1));
1243*4882a593Smuzhiyun 	bmp_page = ntfs_map_page(bmp_mapping,
1244*4882a593Smuzhiyun 			bmp_pos >> (3 + PAGE_SHIFT));
1245*4882a593Smuzhiyun 	if (IS_ERR(bmp_page)) {
1246*4882a593Smuzhiyun 		ntfs_error(sb, "Reading index bitmap failed.");
1247*4882a593Smuzhiyun 		err = PTR_ERR(bmp_page);
1248*4882a593Smuzhiyun 		bmp_page = NULL;
1249*4882a593Smuzhiyun 		goto iput_err_out;
1250*4882a593Smuzhiyun 	}
1251*4882a593Smuzhiyun 	bmp = (u8*)page_address(bmp_page);
1252*4882a593Smuzhiyun 	/* Find next index block in use. */
1253*4882a593Smuzhiyun 	while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
1254*4882a593Smuzhiyun find_next_index_buffer:
1255*4882a593Smuzhiyun 		cur_bmp_pos++;
1256*4882a593Smuzhiyun 		/*
1257*4882a593Smuzhiyun 		 * If we have reached the end of the bitmap page, get the next
1258*4882a593Smuzhiyun 		 * page, and put away the old one.
1259*4882a593Smuzhiyun 		 */
1260*4882a593Smuzhiyun 		if (unlikely((cur_bmp_pos >> 3) >= PAGE_SIZE)) {
1261*4882a593Smuzhiyun 			ntfs_unmap_page(bmp_page);
1262*4882a593Smuzhiyun 			bmp_pos += PAGE_SIZE * 8;
1263*4882a593Smuzhiyun 			cur_bmp_pos = 0;
1264*4882a593Smuzhiyun 			goto get_next_bmp_page;
1265*4882a593Smuzhiyun 		}
1266*4882a593Smuzhiyun 		/* If we have reached the end of the bitmap, we are done. */
1267*4882a593Smuzhiyun 		if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
1268*4882a593Smuzhiyun 			goto unm_EOD;
1269*4882a593Smuzhiyun 		ia_pos = (bmp_pos + cur_bmp_pos) <<
1270*4882a593Smuzhiyun 				ndir->itype.index.block_size_bits;
1271*4882a593Smuzhiyun 	}
1272*4882a593Smuzhiyun 	ntfs_debug("Handling index buffer 0x%llx.",
1273*4882a593Smuzhiyun 			(unsigned long long)bmp_pos + cur_bmp_pos);
1274*4882a593Smuzhiyun 	/* If the current index buffer is in the same page we reuse the page. */
1275*4882a593Smuzhiyun 	if ((prev_ia_pos & (s64)PAGE_MASK) !=
1276*4882a593Smuzhiyun 			(ia_pos & (s64)PAGE_MASK)) {
1277*4882a593Smuzhiyun 		prev_ia_pos = ia_pos;
1278*4882a593Smuzhiyun 		if (likely(ia_page != NULL)) {
1279*4882a593Smuzhiyun 			unlock_page(ia_page);
1280*4882a593Smuzhiyun 			ntfs_unmap_page(ia_page);
1281*4882a593Smuzhiyun 		}
1282*4882a593Smuzhiyun 		/*
1283*4882a593Smuzhiyun 		 * Map the page cache page containing the current ia_pos,
1284*4882a593Smuzhiyun 		 * reading it from disk if necessary.
1285*4882a593Smuzhiyun 		 */
1286*4882a593Smuzhiyun 		ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_SHIFT);
1287*4882a593Smuzhiyun 		if (IS_ERR(ia_page)) {
1288*4882a593Smuzhiyun 			ntfs_error(sb, "Reading index allocation data failed.");
1289*4882a593Smuzhiyun 			err = PTR_ERR(ia_page);
1290*4882a593Smuzhiyun 			ia_page = NULL;
1291*4882a593Smuzhiyun 			goto err_out;
1292*4882a593Smuzhiyun 		}
1293*4882a593Smuzhiyun 		lock_page(ia_page);
1294*4882a593Smuzhiyun 		kaddr = (u8*)page_address(ia_page);
1295*4882a593Smuzhiyun 	}
1296*4882a593Smuzhiyun 	/* Get the current index buffer. */
1297*4882a593Smuzhiyun 	ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_MASK &
1298*4882a593Smuzhiyun 					  ~(s64)(ndir->itype.index.block_size - 1)));
1299*4882a593Smuzhiyun 	/* Bounds checks. */
1300*4882a593Smuzhiyun 	if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE)) {
1301*4882a593Smuzhiyun 		ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
1302*4882a593Smuzhiyun 				"inode 0x%lx or driver bug.", vdir->i_ino);
1303*4882a593Smuzhiyun 		goto err_out;
1304*4882a593Smuzhiyun 	}
1305*4882a593Smuzhiyun 	/* Catch multi sector transfer fixup errors. */
1306*4882a593Smuzhiyun 	if (unlikely(!ntfs_is_indx_record(ia->magic))) {
1307*4882a593Smuzhiyun 		ntfs_error(sb, "Directory index record with vcn 0x%llx is "
1308*4882a593Smuzhiyun 				"corrupt.  Corrupt inode 0x%lx.  Run chkdsk.",
1309*4882a593Smuzhiyun 				(unsigned long long)ia_pos >>
1310*4882a593Smuzhiyun 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
1311*4882a593Smuzhiyun 		goto err_out;
1312*4882a593Smuzhiyun 	}
1313*4882a593Smuzhiyun 	if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
1314*4882a593Smuzhiyun 			~(s64)(ndir->itype.index.block_size - 1)) >>
1315*4882a593Smuzhiyun 			ndir->itype.index.vcn_size_bits)) {
1316*4882a593Smuzhiyun 		ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
1317*4882a593Smuzhiyun 				"different from expected VCN (0x%llx). "
1318*4882a593Smuzhiyun 				"Directory inode 0x%lx is corrupt or driver "
1319*4882a593Smuzhiyun 				"bug. ", (unsigned long long)
1320*4882a593Smuzhiyun 				sle64_to_cpu(ia->index_block_vcn),
1321*4882a593Smuzhiyun 				(unsigned long long)ia_pos >>
1322*4882a593Smuzhiyun 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
1323*4882a593Smuzhiyun 		goto err_out;
1324*4882a593Smuzhiyun 	}
1325*4882a593Smuzhiyun 	if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
1326*4882a593Smuzhiyun 			ndir->itype.index.block_size)) {
1327*4882a593Smuzhiyun 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1328*4882a593Smuzhiyun 				"0x%lx has a size (%u) differing from the "
1329*4882a593Smuzhiyun 				"directory specified size (%u). Directory "
1330*4882a593Smuzhiyun 				"inode is corrupt or driver bug.",
1331*4882a593Smuzhiyun 				(unsigned long long)ia_pos >>
1332*4882a593Smuzhiyun 				ndir->itype.index.vcn_size_bits, vdir->i_ino,
1333*4882a593Smuzhiyun 				le32_to_cpu(ia->index.allocated_size) + 0x18,
1334*4882a593Smuzhiyun 				ndir->itype.index.block_size);
1335*4882a593Smuzhiyun 		goto err_out;
1336*4882a593Smuzhiyun 	}
1337*4882a593Smuzhiyun 	index_end = (u8*)ia + ndir->itype.index.block_size;
1338*4882a593Smuzhiyun 	if (unlikely(index_end > kaddr + PAGE_SIZE)) {
1339*4882a593Smuzhiyun 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1340*4882a593Smuzhiyun 				"0x%lx crosses page boundary. Impossible! "
1341*4882a593Smuzhiyun 				"Cannot access! This is probably a bug in the "
1342*4882a593Smuzhiyun 				"driver.", (unsigned long long)ia_pos >>
1343*4882a593Smuzhiyun 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
1344*4882a593Smuzhiyun 		goto err_out;
1345*4882a593Smuzhiyun 	}
1346*4882a593Smuzhiyun 	ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
1347*4882a593Smuzhiyun 	index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
1348*4882a593Smuzhiyun 	if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
1349*4882a593Smuzhiyun 		ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
1350*4882a593Smuzhiyun 				"inode 0x%lx exceeds maximum size.",
1351*4882a593Smuzhiyun 				(unsigned long long)ia_pos >>
1352*4882a593Smuzhiyun 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
1353*4882a593Smuzhiyun 		goto err_out;
1354*4882a593Smuzhiyun 	}
1355*4882a593Smuzhiyun 	/* The first index entry in this index buffer. */
1356*4882a593Smuzhiyun 	ie = (INDEX_ENTRY*)((u8*)&ia->index +
1357*4882a593Smuzhiyun 			le32_to_cpu(ia->index.entries_offset));
1358*4882a593Smuzhiyun 	/*
1359*4882a593Smuzhiyun 	 * Loop until we exceed valid memory (corruption case) or until we
1360*4882a593Smuzhiyun 	 * reach the last entry or until filldir tells us it has had enough
1361*4882a593Smuzhiyun 	 * or signals an error (both covered by the rc test).
1362*4882a593Smuzhiyun 	 */
1363*4882a593Smuzhiyun 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1364*4882a593Smuzhiyun 		ntfs_debug("In index allocation, offset 0x%llx.",
1365*4882a593Smuzhiyun 				(unsigned long long)ia_start +
1366*4882a593Smuzhiyun 				(unsigned long long)((u8*)ie - (u8*)ia));
1367*4882a593Smuzhiyun 		/* Bounds checks. */
1368*4882a593Smuzhiyun 		if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
1369*4882a593Smuzhiyun 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
1370*4882a593Smuzhiyun 				(u8*)ie + le16_to_cpu(ie->key_length) >
1371*4882a593Smuzhiyun 				index_end))
1372*4882a593Smuzhiyun 			goto err_out;
1373*4882a593Smuzhiyun 		/* The last entry cannot contain a name. */
1374*4882a593Smuzhiyun 		if (ie->flags & INDEX_ENTRY_END)
1375*4882a593Smuzhiyun 			break;
1376*4882a593Smuzhiyun 		/* Skip index block entry if continuing previous readdir. */
1377*4882a593Smuzhiyun 		if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
1378*4882a593Smuzhiyun 			continue;
1379*4882a593Smuzhiyun 		/* Advance the position even if going to skip the entry. */
1380*4882a593Smuzhiyun 		actor->pos = (u8*)ie - (u8*)ia +
1381*4882a593Smuzhiyun 				(sle64_to_cpu(ia->index_block_vcn) <<
1382*4882a593Smuzhiyun 				ndir->itype.index.vcn_size_bits) +
1383*4882a593Smuzhiyun 				vol->mft_record_size;
1384*4882a593Smuzhiyun 		/*
1385*4882a593Smuzhiyun 		 * Submit the name to the @filldir callback.  Note,
1386*4882a593Smuzhiyun 		 * ntfs_filldir() drops the lock on @ia_page but it retakes it
1387*4882a593Smuzhiyun 		 * before returning, unless a non-zero value is returned in
1388*4882a593Smuzhiyun 		 * which case the page is left unlocked.
1389*4882a593Smuzhiyun 		 */
1390*4882a593Smuzhiyun 		rc = ntfs_filldir(vol, ndir, ia_page, ie, name, actor);
1391*4882a593Smuzhiyun 		if (rc) {
1392*4882a593Smuzhiyun 			/* @ia_page is already unlocked in this case. */
1393*4882a593Smuzhiyun 			ntfs_unmap_page(ia_page);
1394*4882a593Smuzhiyun 			ntfs_unmap_page(bmp_page);
1395*4882a593Smuzhiyun 			iput(bmp_vi);
1396*4882a593Smuzhiyun 			goto abort;
1397*4882a593Smuzhiyun 		}
1398*4882a593Smuzhiyun 	}
1399*4882a593Smuzhiyun 	goto find_next_index_buffer;
1400*4882a593Smuzhiyun unm_EOD:
1401*4882a593Smuzhiyun 	if (ia_page) {
1402*4882a593Smuzhiyun 		unlock_page(ia_page);
1403*4882a593Smuzhiyun 		ntfs_unmap_page(ia_page);
1404*4882a593Smuzhiyun 	}
1405*4882a593Smuzhiyun 	ntfs_unmap_page(bmp_page);
1406*4882a593Smuzhiyun 	iput(bmp_vi);
1407*4882a593Smuzhiyun EOD:
1408*4882a593Smuzhiyun 	/* We are finished, set fpos to EOD. */
1409*4882a593Smuzhiyun 	actor->pos = i_size + vol->mft_record_size;
1410*4882a593Smuzhiyun abort:
1411*4882a593Smuzhiyun 	kfree(name);
1412*4882a593Smuzhiyun 	return 0;
1413*4882a593Smuzhiyun err_out:
1414*4882a593Smuzhiyun 	if (bmp_page) {
1415*4882a593Smuzhiyun 		ntfs_unmap_page(bmp_page);
1416*4882a593Smuzhiyun iput_err_out:
1417*4882a593Smuzhiyun 		iput(bmp_vi);
1418*4882a593Smuzhiyun 	}
1419*4882a593Smuzhiyun 	if (ia_page) {
1420*4882a593Smuzhiyun 		unlock_page(ia_page);
1421*4882a593Smuzhiyun 		ntfs_unmap_page(ia_page);
1422*4882a593Smuzhiyun 	}
1423*4882a593Smuzhiyun 	kfree(ir);
1424*4882a593Smuzhiyun 	kfree(name);
1425*4882a593Smuzhiyun 	if (ctx)
1426*4882a593Smuzhiyun 		ntfs_attr_put_search_ctx(ctx);
1427*4882a593Smuzhiyun 	if (m)
1428*4882a593Smuzhiyun 		unmap_mft_record(ndir);
1429*4882a593Smuzhiyun 	if (!err)
1430*4882a593Smuzhiyun 		err = -EIO;
1431*4882a593Smuzhiyun 	ntfs_debug("Failed. Returning error code %i.", -err);
1432*4882a593Smuzhiyun 	return err;
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun /**
1436*4882a593Smuzhiyun  * ntfs_dir_open - called when an inode is about to be opened
1437*4882a593Smuzhiyun  * @vi:		inode to be opened
1438*4882a593Smuzhiyun  * @filp:	file structure describing the inode
1439*4882a593Smuzhiyun  *
1440*4882a593Smuzhiyun  * Limit directory size to the page cache limit on architectures where unsigned
1441*4882a593Smuzhiyun  * long is 32-bits. This is the most we can do for now without overflowing the
1442*4882a593Smuzhiyun  * page cache page index. Doing it this way means we don't run into problems
1443*4882a593Smuzhiyun  * because of existing too large directories. It would be better to allow the
1444*4882a593Smuzhiyun  * user to read the accessible part of the directory but I doubt very much
1445*4882a593Smuzhiyun  * anyone is going to hit this check on a 32-bit architecture, so there is no
1446*4882a593Smuzhiyun  * point in adding the extra complexity required to support this.
1447*4882a593Smuzhiyun  *
1448*4882a593Smuzhiyun  * On 64-bit architectures, the check is hopefully optimized away by the
1449*4882a593Smuzhiyun  * compiler.
1450*4882a593Smuzhiyun  */
ntfs_dir_open(struct inode * vi,struct file * filp)1451*4882a593Smuzhiyun static int ntfs_dir_open(struct inode *vi, struct file *filp)
1452*4882a593Smuzhiyun {
1453*4882a593Smuzhiyun 	if (sizeof(unsigned long) < 8) {
1454*4882a593Smuzhiyun 		if (i_size_read(vi) > MAX_LFS_FILESIZE)
1455*4882a593Smuzhiyun 			return -EFBIG;
1456*4882a593Smuzhiyun 	}
1457*4882a593Smuzhiyun 	return 0;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun #ifdef NTFS_RW
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun /**
1463*4882a593Smuzhiyun  * ntfs_dir_fsync - sync a directory to disk
1464*4882a593Smuzhiyun  * @filp:	directory to be synced
1465*4882a593Smuzhiyun  * @dentry:	dentry describing the directory to sync
1466*4882a593Smuzhiyun  * @datasync:	if non-zero only flush user data and not metadata
1467*4882a593Smuzhiyun  *
1468*4882a593Smuzhiyun  * Data integrity sync of a directory to disk.  Used for fsync, fdatasync, and
1469*4882a593Smuzhiyun  * msync system calls.  This function is based on file.c::ntfs_file_fsync().
1470*4882a593Smuzhiyun  *
1471*4882a593Smuzhiyun  * Write the mft record and all associated extent mft records as well as the
1472*4882a593Smuzhiyun  * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
1473*4882a593Smuzhiyun  *
1474*4882a593Smuzhiyun  * If @datasync is true, we do not wait on the inode(s) to be written out
1475*4882a593Smuzhiyun  * but we always wait on the page cache pages to be written out.
1476*4882a593Smuzhiyun  *
1477*4882a593Smuzhiyun  * Note: In the past @filp could be NULL so we ignore it as we don't need it
1478*4882a593Smuzhiyun  * anyway.
1479*4882a593Smuzhiyun  *
1480*4882a593Smuzhiyun  * Locking: Caller must hold i_mutex on the inode.
1481*4882a593Smuzhiyun  *
1482*4882a593Smuzhiyun  * TODO: We should probably also write all attribute/index inodes associated
1483*4882a593Smuzhiyun  * with this inode but since we have no simple way of getting to them we ignore
1484*4882a593Smuzhiyun  * this problem for now.  We do write the $BITMAP attribute if it is present
1485*4882a593Smuzhiyun  * which is the important one for a directory so things are not too bad.
1486*4882a593Smuzhiyun  */
ntfs_dir_fsync(struct file * filp,loff_t start,loff_t end,int datasync)1487*4882a593Smuzhiyun static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
1488*4882a593Smuzhiyun 			  int datasync)
1489*4882a593Smuzhiyun {
1490*4882a593Smuzhiyun 	struct inode *bmp_vi, *vi = filp->f_mapping->host;
1491*4882a593Smuzhiyun 	int err, ret;
1492*4882a593Smuzhiyun 	ntfs_attr na;
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	err = file_write_and_wait_range(filp, start, end);
1497*4882a593Smuzhiyun 	if (err)
1498*4882a593Smuzhiyun 		return err;
1499*4882a593Smuzhiyun 	inode_lock(vi);
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	BUG_ON(!S_ISDIR(vi->i_mode));
1502*4882a593Smuzhiyun 	/* If the bitmap attribute inode is in memory sync it, too. */
1503*4882a593Smuzhiyun 	na.mft_no = vi->i_ino;
1504*4882a593Smuzhiyun 	na.type = AT_BITMAP;
1505*4882a593Smuzhiyun 	na.name = I30;
1506*4882a593Smuzhiyun 	na.name_len = 4;
1507*4882a593Smuzhiyun 	bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na);
1508*4882a593Smuzhiyun 	if (bmp_vi) {
1509*4882a593Smuzhiyun  		write_inode_now(bmp_vi, !datasync);
1510*4882a593Smuzhiyun 		iput(bmp_vi);
1511*4882a593Smuzhiyun 	}
1512*4882a593Smuzhiyun 	ret = __ntfs_write_inode(vi, 1);
1513*4882a593Smuzhiyun 	write_inode_now(vi, !datasync);
1514*4882a593Smuzhiyun 	err = sync_blockdev(vi->i_sb->s_bdev);
1515*4882a593Smuzhiyun 	if (unlikely(err && !ret))
1516*4882a593Smuzhiyun 		ret = err;
1517*4882a593Smuzhiyun 	if (likely(!ret))
1518*4882a593Smuzhiyun 		ntfs_debug("Done.");
1519*4882a593Smuzhiyun 	else
1520*4882a593Smuzhiyun 		ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx.  Error "
1521*4882a593Smuzhiyun 				"%u.", datasync ? "data" : "", vi->i_ino, -ret);
1522*4882a593Smuzhiyun 	inode_unlock(vi);
1523*4882a593Smuzhiyun 	return ret;
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun #endif /* NTFS_RW */
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun const struct file_operations ntfs_dir_ops = {
1529*4882a593Smuzhiyun 	.llseek		= generic_file_llseek,	/* Seek inside directory. */
1530*4882a593Smuzhiyun 	.read		= generic_read_dir,	/* Return -EISDIR. */
1531*4882a593Smuzhiyun 	.iterate	= ntfs_readdir,		/* Read directory contents. */
1532*4882a593Smuzhiyun #ifdef NTFS_RW
1533*4882a593Smuzhiyun 	.fsync		= ntfs_dir_fsync,	/* Sync a directory to disk. */
1534*4882a593Smuzhiyun #endif /* NTFS_RW */
1535*4882a593Smuzhiyun 	/*.ioctl	= ,*/			/* Perform function on the
1536*4882a593Smuzhiyun 						   mounted filesystem. */
1537*4882a593Smuzhiyun 	.open		= ntfs_dir_open,	/* Open directory. */
1538*4882a593Smuzhiyun };
1539