2 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2003 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/smp_lock.h>
28 * The little endian Unicode string $I30 as a global constant.
30 uchar_t I30
[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('I'),
31 const_cpu_to_le16('3'), const_cpu_to_le16('0'),
32 const_cpu_to_le16(0) };
35 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
36 * @dir_ni: ntfs inode of the directory in which to search for the name
37 * @uname: Unicode name for which to search in the directory
38 * @uname_len: length of the name @uname in Unicode characters
39 * @res: return the found file name if necessary (see below)
41 * Look for an inode with name @uname in the directory with inode @dir_ni.
42 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
43 * the Unicode name. If the name is found in the directory, the corresponding
44 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
45 * is a 64-bit number containing the sequence number.
47 * On error, a negative value is returned corresponding to the error code. In
48 * particular if the inode is not found -ENOENT is returned. Note that you
49 * can't just check the return value for being negative, you have to check the
50 * inode number for being negative which you can extract using MREC(return
53 * Note, @uname_len does not include the (optional) terminating NULL character.
55 * Note, we look for a case sensitive match first but we also look for a case
56 * insensitive match at the same time. If we find a case insensitive match, we
57 * save that for the case that we don't find an exact match, where we return
58 * the case insensitive match and setup @res (which we allocate!) with the mft
59 * reference, the file name type, length and with a copy of the little endian
60 * Unicode file name itself. If we match a file name which is in the DOS name
61 * space, we only return the mft reference and file name type in @res.
62 * ntfs_lookup() then uses this to find the long file name in the inode itself.
63 * This is to avoid polluting the dcache with short file names. We want them to
64 * work but we don't care for how quickly one can access them. This also fixes
65 * the dcache aliasing issues.
67 MFT_REF
ntfs_lookup_inode_by_name(ntfs_inode
*dir_ni
, const uchar_t
*uname
,
68 const int uname_len
, ntfs_name
**res
)
70 ntfs_volume
*vol
= dir_ni
->vol
;
71 struct super_block
*sb
= vol
->sb
;
78 attr_search_context
*ctx
;
81 struct address_space
*ia_mapping
;
84 ntfs_name
*name
= NULL
;
86 /* Get hold of the mft record for the directory. */
87 m
= map_mft_record(dir_ni
);
88 if (unlikely(IS_ERR(m
))) {
89 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
91 return ERR_MREF(PTR_ERR(m
));
93 ctx
= get_attr_search_ctx(dir_ni
, m
);
98 /* Find the index root attribute in the mft record. */
99 if (!lookup_attr(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
, 0,
101 ntfs_error(sb
, "Index root attribute missing in directory "
102 "inode 0x%lx.", dir_ni
->mft_no
);
106 /* Get to the index root value (it's been verified in read_inode). */
107 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
108 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
109 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
110 /* The first index entry. */
111 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
112 le32_to_cpu(ir
->index
.entries_offset
));
114 * Loop until we exceed valid memory (corruption case) or until we
115 * reach the last entry.
117 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
119 if ((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
120 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
121 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
125 * The last entry cannot contain a name. It can however contain
126 * a pointer to a child node in the B+tree so we just break out.
128 if (ie
->flags
& INDEX_ENTRY_END
)
131 * We perform a case sensitive comparison and if that matches
132 * we are done and return the mft reference of the inode (i.e.
133 * the inode number together with the sequence number for
134 * consistency checking). We convert it to cpu format before
137 if (ntfs_are_names_equal(uname
, uname_len
,
138 (uchar_t
*)&ie
->key
.file_name
.file_name
,
139 ie
->key
.file_name
.file_name_length
,
140 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
)) {
143 * We have a perfect match, so we don't need to care
144 * about having matched imperfectly before, so we can
145 * free name and set *res to NULL.
146 * However, if the perfect match is a short file name,
147 * we need to signal this through *res, so that
148 * ntfs_lookup() can fix dcache aliasing issues.
149 * As an optimization we just reuse an existing
150 * allocation of *res.
152 if (ie
->key
.file_name
.file_name_type
== FILE_NAME_DOS
) {
154 name
= kmalloc(sizeof(ntfs_name
),
161 name
->mref
= le64_to_cpu(
162 ie
->data
.dir
.indexed_file
);
163 name
->type
= FILE_NAME_DOS
;
171 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
172 put_attr_search_ctx(ctx
);
173 unmap_mft_record(dir_ni
);
177 * For a case insensitive mount, we also perform a case
178 * insensitive comparison (provided the file name is not in the
179 * POSIX namespace). If the comparison matches, and the name is
180 * in the WIN32 namespace, we cache the filename in *res so
181 * that the caller, ntfs_lookup(), can work on it. If the
182 * comparison matches, and the name is in the DOS namespace, we
183 * only cache the mft reference and the file name type (we set
184 * the name length to zero for simplicity).
186 if (!NVolCaseSensitive(vol
) &&
187 ie
->key
.file_name
.file_name_type
&&
188 ntfs_are_names_equal(uname
, uname_len
,
189 (uchar_t
*)&ie
->key
.file_name
.file_name
,
190 ie
->key
.file_name
.file_name_length
,
191 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
)) {
192 int name_size
= sizeof(ntfs_name
);
193 u8 type
= ie
->key
.file_name
.file_name_type
;
194 u8 len
= ie
->key
.file_name
.file_name_length
;
196 /* Only one case insensitive matching name allowed. */
198 ntfs_error(sb
, "Found already allocated name "
199 "in phase 1. Please run chkdsk "
200 "and if that doesn't find any "
201 "errors please report you saw "
203 "linux-ntfs-dev@lists.sf.net.");
207 if (type
!= FILE_NAME_DOS
)
208 name_size
+= len
* sizeof(uchar_t
);
209 name
= kmalloc(name_size
, GFP_NOFS
);
214 name
->mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
216 if (type
!= FILE_NAME_DOS
) {
218 memcpy(name
->name
, ie
->key
.file_name
.file_name
,
219 len
* sizeof(uchar_t
));
225 * Not a perfect match, need to do full blown collation so we
226 * know which way in the B+tree we have to go.
228 rc
= ntfs_collate_names(uname
, uname_len
,
229 (uchar_t
*)&ie
->key
.file_name
.file_name
,
230 ie
->key
.file_name
.file_name_length
, 1,
231 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
233 * If uname collates before the name of the current entry, there
234 * is definitely no such name in this index but we might need to
235 * descend into the B+tree so we just break out of the loop.
239 /* The names are not equal, continue the search. */
243 * Names match with case insensitive comparison, now try the
244 * case sensitive comparison, which is required for proper
247 rc
= ntfs_collate_names(uname
, uname_len
,
248 (uchar_t
*)&ie
->key
.file_name
.file_name
,
249 ie
->key
.file_name
.file_name_length
, 1,
250 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
256 * Perfect match, this will never happen as the
257 * ntfs_are_names_equal() call will have gotten a match but we
258 * still treat it correctly.
263 * We have finished with this index without success. Check for the
264 * presence of a child node and if not present return -ENOENT, unless
265 * we have got a matching name cached in name in which case return the
266 * mft reference associated with it.
268 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
270 put_attr_search_ctx(ctx
);
271 unmap_mft_record(dir_ni
);
274 ntfs_debug("Entry not found.");
277 } /* Child node present, descend into it. */
278 /* Consistency check: Verify that an index allocation exists. */
279 if (!NInoIndexAllocPresent(dir_ni
)) {
280 ntfs_error(sb
, "No index allocation attribute but index entry "
281 "requires one. Directory inode 0x%lx is "
282 "corrupt or driver bug.", dir_ni
->mft_no
);
286 /* Get the starting vcn of the index_block holding the child node. */
287 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
288 ia_mapping
= VFS_I(dir_ni
)->i_mapping
;
290 * We are done with the index root and the mft record. Release them,
291 * otherwise we deadlock with ntfs_map_page().
293 put_attr_search_ctx(ctx
);
294 unmap_mft_record(dir_ni
);
297 descend_into_child_node
:
299 * Convert vcn to index into the index allocation attribute in units
300 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
303 page
= ntfs_map_page(ia_mapping
, vcn
<<
304 dir_ni
->itype
.index
.vcn_size_bits
>> PAGE_CACHE_SHIFT
);
306 ntfs_error(sb
, "Failed to map directory index page, error %ld.",
311 kaddr
= (u8
*)page_address(page
);
312 fast_descend_into_child_node
:
313 /* Get to the index allocation block. */
314 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
315 dir_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_CACHE_MASK
));
317 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_CACHE_SIZE
) {
318 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
319 "inode 0x%lx or driver bug.", dir_ni
->mft_no
);
323 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
324 ntfs_error(sb
, "Actual VCN (0x%Lx) of index buffer is "
325 "different from expected VCN (0x%Lx). "
326 "Directory inode 0x%lx is corrupt or driver "
328 (long long)sle64_to_cpu(ia
->index_block_vcn
),
329 (long long)vcn
, dir_ni
->mft_no
);
333 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
334 dir_ni
->itype
.index
.block_size
) {
335 ntfs_error(sb
, "Index buffer (VCN 0x%Lx) of directory inode "
336 "0x%lx has a size (%u) differing from the "
337 "directory specified size (%u). Directory "
338 "inode is corrupt or driver bug.",
339 (long long)vcn
, dir_ni
->mft_no
,
340 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
341 dir_ni
->itype
.index
.block_size
);
345 index_end
= (u8
*)ia
+ dir_ni
->itype
.index
.block_size
;
346 if (index_end
> kaddr
+ PAGE_CACHE_SIZE
) {
347 ntfs_error(sb
, "Index buffer (VCN 0x%Lx) of directory inode "
348 "0x%lx crosses page boundary. Impossible! "
349 "Cannot access! This is probably a bug in the "
350 "driver.", (long long)vcn
, dir_ni
->mft_no
);
354 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
355 if (index_end
> (u8
*)ia
+ dir_ni
->itype
.index
.block_size
) {
356 ntfs_error(sb
, "Size of index buffer (VCN 0x%Lx) of directory "
357 "inode 0x%lx exceeds maximum size.",
358 (long long)vcn
, dir_ni
->mft_no
);
362 /* The first index entry. */
363 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
364 le32_to_cpu(ia
->index
.entries_offset
));
366 * Iterate similar to above big loop but applied to index buffer, thus
367 * loop until we exceed valid memory (corruption case) or until we
368 * reach the last entry.
370 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
372 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
373 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
374 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
376 ntfs_error(sb
, "Index entry out of bounds in "
377 "directory inode 0x%lx.",
383 * The last entry cannot contain a name. It can however contain
384 * a pointer to a child node in the B+tree so we just break out.
386 if (ie
->flags
& INDEX_ENTRY_END
)
389 * We perform a case sensitive comparison and if that matches
390 * we are done and return the mft reference of the inode (i.e.
391 * the inode number together with the sequence number for
392 * consistency checking). We convert it to cpu format before
395 if (ntfs_are_names_equal(uname
, uname_len
,
396 (uchar_t
*)&ie
->key
.file_name
.file_name
,
397 ie
->key
.file_name
.file_name_length
,
398 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
)) {
401 * We have a perfect match, so we don't need to care
402 * about having matched imperfectly before, so we can
403 * free name and set *res to NULL.
404 * However, if the perfect match is a short file name,
405 * we need to signal this through *res, so that
406 * ntfs_lookup() can fix dcache aliasing issues.
407 * As an optimization we just reuse an existing
408 * allocation of *res.
410 if (ie
->key
.file_name
.file_name_type
== FILE_NAME_DOS
) {
412 name
= kmalloc(sizeof(ntfs_name
),
419 name
->mref
= le64_to_cpu(
420 ie
->data
.dir
.indexed_file
);
421 name
->type
= FILE_NAME_DOS
;
429 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
430 ntfs_unmap_page(page
);
434 * For a case insensitive mount, we also perform a case
435 * insensitive comparison (provided the file name is not in the
436 * POSIX namespace). If the comparison matches, and the name is
437 * in the WIN32 namespace, we cache the filename in *res so
438 * that the caller, ntfs_lookup(), can work on it. If the
439 * comparison matches, and the name is in the DOS namespace, we
440 * only cache the mft reference and the file name type (we set
441 * the name length to zero for simplicity).
443 if (!NVolCaseSensitive(vol
) &&
444 ie
->key
.file_name
.file_name_type
&&
445 ntfs_are_names_equal(uname
, uname_len
,
446 (uchar_t
*)&ie
->key
.file_name
.file_name
,
447 ie
->key
.file_name
.file_name_length
,
448 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
)) {
449 int name_size
= sizeof(ntfs_name
);
450 u8 type
= ie
->key
.file_name
.file_name_type
;
451 u8 len
= ie
->key
.file_name
.file_name_length
;
453 /* Only one case insensitive matching name allowed. */
455 ntfs_error(sb
, "Found already allocated name "
456 "in phase 2. Please run chkdsk "
457 "and if that doesn't find any "
458 "errors please report you saw "
460 "linux-ntfs-dev@lists.sf.net.");
461 ntfs_unmap_page(page
);
465 if (type
!= FILE_NAME_DOS
)
466 name_size
+= len
* sizeof(uchar_t
);
467 name
= kmalloc(name_size
, GFP_NOFS
);
472 name
->mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
474 if (type
!= FILE_NAME_DOS
) {
476 memcpy(name
->name
, ie
->key
.file_name
.file_name
,
477 len
* sizeof(uchar_t
));
483 * Not a perfect match, need to do full blown collation so we
484 * know which way in the B+tree we have to go.
486 rc
= ntfs_collate_names(uname
, uname_len
,
487 (uchar_t
*)&ie
->key
.file_name
.file_name
,
488 ie
->key
.file_name
.file_name_length
, 1,
489 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
491 * If uname collates before the name of the current entry, there
492 * is definitely no such name in this index but we might need to
493 * descend into the B+tree so we just break out of the loop.
497 /* The names are not equal, continue the search. */
501 * Names match with case insensitive comparison, now try the
502 * case sensitive comparison, which is required for proper
505 rc
= ntfs_collate_names(uname
, uname_len
,
506 (uchar_t
*)&ie
->key
.file_name
.file_name
,
507 ie
->key
.file_name
.file_name_length
, 1,
508 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
514 * Perfect match, this will never happen as the
515 * ntfs_are_names_equal() call will have gotten a match but we
516 * still treat it correctly.
521 * We have finished with this index buffer without success. Check for
522 * the presence of a child node.
524 if (ie
->flags
& INDEX_ENTRY_NODE
) {
525 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
526 ntfs_error(sb
, "Index entry with child node found in "
527 "a leaf node in directory inode 0x%lx.",
532 /* Child node present, descend into it. */
534 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
536 /* If vcn is in the same page cache page as old_vcn we
537 * recycle the mapped page. */
538 if (old_vcn
<< vol
->cluster_size_bits
>>
539 PAGE_CACHE_SHIFT
== vcn
<<
540 vol
->cluster_size_bits
>>
542 goto fast_descend_into_child_node
;
543 ntfs_unmap_page(page
);
544 goto descend_into_child_node
;
546 ntfs_error(sb
, "Negative child node vcn in directory inode "
547 "0x%lx.", dir_ni
->mft_no
);
552 * No child node present, return -ENOENT, unless we have got a matching
553 * name cached in name in which case return the mft reference
554 * associated with it.
557 ntfs_unmap_page(page
);
560 ntfs_debug("Entry not found.");
563 ntfs_unmap_page(page
);
566 put_attr_search_ctx(ctx
);
568 unmap_mft_record(dir_ni
);
573 return ERR_MREF(err
);
575 ntfs_error(sb
, "Corrupt directory. Aborting lookup.");
583 // The algorithm embedded in this code will be required for the time when we
584 // want to support adding of entries to directories, where we require correct
585 // collation of file names in order not to cause corruption of the file system.
588 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
589 * @dir_ni: ntfs inode of the directory in which to search for the name
590 * @uname: Unicode name for which to search in the directory
591 * @uname_len: length of the name @uname in Unicode characters
593 * Look for an inode with name @uname in the directory with inode @dir_ni.
594 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
595 * the Unicode name. If the name is found in the directory, the corresponding
596 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
597 * is a 64-bit number containing the sequence number.
599 * On error, a negative value is returned corresponding to the error code. In
600 * particular if the inode is not found -ENOENT is returned. Note that you
601 * can't just check the return value for being negative, you have to check the
602 * inode number for being negative which you can extract using MREC(return
605 * Note, @uname_len does not include the (optional) terminating NULL character.
607 u64
ntfs_lookup_inode_by_name(ntfs_inode
*dir_ni
, const uchar_t
*uname
,
610 ntfs_volume
*vol
= dir_ni
->vol
;
611 struct super_block
*sb
= vol
->sb
;
615 INDEX_ALLOCATION
*ia
;
618 attr_search_context
*ctx
;
622 struct address_space
*ia_mapping
;
626 /* Get hold of the mft record for the directory. */
627 m
= map_mft_record(dir_ni
);
629 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
631 return ERR_MREF(PTR_ERR(m
));
633 ctx
= get_attr_search_ctx(dir_ni
, m
);
638 /* Find the index root attribute in the mft record. */
639 if (!lookup_attr(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
, 0,
641 ntfs_error(sb
, "Index root attribute missing in directory "
642 "inode 0x%lx.", dir_ni
->mft_no
);
646 /* Get to the index root value (it's been verified in read_inode). */
647 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
648 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
649 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
650 /* The first index entry. */
651 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
652 le32_to_cpu(ir
->index
.entries_offset
));
654 * Loop until we exceed valid memory (corruption case) or until we
655 * reach the last entry.
657 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
659 if ((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
660 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
661 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
665 * The last entry cannot contain a name. It can however contain
666 * a pointer to a child node in the B+tree so we just break out.
668 if (ie
->flags
& INDEX_ENTRY_END
)
671 * If the current entry has a name type of POSIX, the name is
672 * case sensitive and not otherwise. This has the effect of us
673 * not being able to access any POSIX file names which collate
674 * after the non-POSIX one when they only differ in case, but
675 * anyone doing screwy stuff like that deserves to burn in
676 * hell... Doing that kind of stuff on NT4 actually causes
677 * corruption on the partition even when using SP6a and Linux
678 * is not involved at all.
680 ic
= ie
->key
.file_name
.file_name_type
? IGNORE_CASE
:
683 * If the names match perfectly, we are done and return the
684 * mft reference of the inode (i.e. the inode number together
685 * with the sequence number for consistency checking. We
686 * convert it to cpu format before returning.
688 if (ntfs_are_names_equal(uname
, uname_len
,
689 (uchar_t
*)&ie
->key
.file_name
.file_name
,
690 ie
->key
.file_name
.file_name_length
, ic
,
691 vol
->upcase
, vol
->upcase_len
)) {
693 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
694 put_attr_search_ctx(ctx
);
695 unmap_mft_record(dir_ni
);
699 * Not a perfect match, need to do full blown collation so we
700 * know which way in the B+tree we have to go.
702 rc
= ntfs_collate_names(uname
, uname_len
,
703 (uchar_t
*)&ie
->key
.file_name
.file_name
,
704 ie
->key
.file_name
.file_name_length
, 1,
705 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
707 * If uname collates before the name of the current entry, there
708 * is definitely no such name in this index but we might need to
709 * descend into the B+tree so we just break out of the loop.
713 /* The names are not equal, continue the search. */
717 * Names match with case insensitive comparison, now try the
718 * case sensitive comparison, which is required for proper
721 rc
= ntfs_collate_names(uname
, uname_len
,
722 (uchar_t
*)&ie
->key
.file_name
.file_name
,
723 ie
->key
.file_name
.file_name_length
, 1,
724 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
730 * Perfect match, this will never happen as the
731 * ntfs_are_names_equal() call will have gotten a match but we
732 * still treat it correctly.
737 * We have finished with this index without success. Check for the
738 * presence of a child node.
740 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
741 /* No child node, return -ENOENT. */
744 } /* Child node present, descend into it. */
745 /* Consistency check: Verify that an index allocation exists. */
746 if (!NInoIndexAllocPresent(dir_ni
)) {
747 ntfs_error(sb
, "No index allocation attribute but index entry "
748 "requires one. Directory inode 0x%lx is "
749 "corrupt or driver bug.", dir_ni
->mft_no
);
753 /* Get the starting vcn of the index_block holding the child node. */
754 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
755 ia_mapping
= VFS_I(dir_ni
)->i_mapping
;
757 * We are done with the index root and the mft record. Release them,
758 * otherwise we deadlock with ntfs_map_page().
760 put_attr_search_ctx(ctx
);
761 unmap_mft_record(dir_ni
);
764 descend_into_child_node
:
766 * Convert vcn to index into the index allocation attribute in units
767 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
770 page
= ntfs_map_page(ia_mapping
, vcn
<<
771 dir_ni
->itype
.index
.vcn_size_bits
>> PAGE_CACHE_SHIFT
);
773 ntfs_error(sb
, "Failed to map directory index page, error %ld.",
778 kaddr
= (u8
*)page_address(page
);
779 fast_descend_into_child_node
:
780 /* Get to the index allocation block. */
781 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
782 dir_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_CACHE_MASK
));
784 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_CACHE_SIZE
) {
785 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
786 "inode 0x%lx or driver bug.", dir_ni
->mft_no
);
790 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
791 ntfs_error(sb
, "Actual VCN (0x%Lx) of index buffer is "
792 "different from expected VCN (0x%Lx). "
793 "Directory inode 0x%lx is corrupt or driver "
795 (long long)sle64_to_cpu(ia
->index_block_vcn
),
796 (long long)vcn
, dir_ni
->mft_no
);
800 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
801 dir_ni
->itype
.index
.block_size
) {
802 ntfs_error(sb
, "Index buffer (VCN 0x%Lx) of directory inode "
803 "0x%lx has a size (%u) differing from the "
804 "directory specified size (%u). Directory "
805 "inode is corrupt or driver bug.",
806 (long long)vcn
, dir_ni
->mft_no
,
807 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
808 dir_ni
->itype
.index
.block_size
);
812 index_end
= (u8
*)ia
+ dir_ni
->itype
.index
.block_size
;
813 if (index_end
> kaddr
+ PAGE_CACHE_SIZE
) {
814 ntfs_error(sb
, "Index buffer (VCN 0x%Lx) of directory inode "
815 "0x%lx crosses page boundary. Impossible! "
816 "Cannot access! This is probably a bug in the "
817 "driver.", (long long)vcn
, dir_ni
->mft_no
);
821 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
822 if (index_end
> (u8
*)ia
+ dir_ni
->itype
.index
.block_size
) {
823 ntfs_error(sb
, "Size of index buffer (VCN 0x%Lx) of directory "
824 "inode 0x%lx exceeds maximum size.",
825 (long long)vcn
, dir_ni
->mft_no
);
829 /* The first index entry. */
830 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
831 le32_to_cpu(ia
->index
.entries_offset
));
833 * Iterate similar to above big loop but applied to index buffer, thus
834 * loop until we exceed valid memory (corruption case) or until we
835 * reach the last entry.
837 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
839 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
840 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
841 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
843 ntfs_error(sb
, "Index entry out of bounds in "
844 "directory inode 0x%lx.",
850 * The last entry cannot contain a name. It can however contain
851 * a pointer to a child node in the B+tree so we just break out.
853 if (ie
->flags
& INDEX_ENTRY_END
)
856 * If the current entry has a name type of POSIX, the name is
857 * case sensitive and not otherwise. This has the effect of us
858 * not being able to access any POSIX file names which collate
859 * after the non-POSIX one when they only differ in case, but
860 * anyone doing screwy stuff like that deserves to burn in
861 * hell... Doing that kind of stuff on NT4 actually causes
862 * corruption on the partition even when using SP6a and Linux
863 * is not involved at all.
865 ic
= ie
->key
.file_name
.file_name_type
? IGNORE_CASE
:
868 * If the names match perfectly, we are done and return the
869 * mft reference of the inode (i.e. the inode number together
870 * with the sequence number for consistency checking. We
871 * convert it to cpu format before returning.
873 if (ntfs_are_names_equal(uname
, uname_len
,
874 (uchar_t
*)&ie
->key
.file_name
.file_name
,
875 ie
->key
.file_name
.file_name_length
, ic
,
876 vol
->upcase
, vol
->upcase_len
)) {
878 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
879 ntfs_unmap_page(page
);
883 * Not a perfect match, need to do full blown collation so we
884 * know which way in the B+tree we have to go.
886 rc
= ntfs_collate_names(uname
, uname_len
,
887 (uchar_t
*)&ie
->key
.file_name
.file_name
,
888 ie
->key
.file_name
.file_name_length
, 1,
889 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
891 * If uname collates before the name of the current entry, there
892 * is definitely no such name in this index but we might need to
893 * descend into the B+tree so we just break out of the loop.
897 /* The names are not equal, continue the search. */
901 * Names match with case insensitive comparison, now try the
902 * case sensitive comparison, which is required for proper
905 rc
= ntfs_collate_names(uname
, uname_len
,
906 (uchar_t
*)&ie
->key
.file_name
.file_name
,
907 ie
->key
.file_name
.file_name_length
, 1,
908 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
914 * Perfect match, this will never happen as the
915 * ntfs_are_names_equal() call will have gotten a match but we
916 * still treat it correctly.
921 * We have finished with this index buffer without success. Check for
922 * the presence of a child node.
924 if (ie
->flags
& INDEX_ENTRY_NODE
) {
925 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
926 ntfs_error(sb
, "Index entry with child node found in "
927 "a leaf node in directory inode 0x%lx.",
932 /* Child node present, descend into it. */
934 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
936 /* If vcn is in the same page cache page as old_vcn we
937 * recycle the mapped page. */
938 if (old_vcn
<< vol
->cluster_size_bits
>>
939 PAGE_CACHE_SHIFT
== vcn
<<
940 vol
->cluster_size_bits
>>
942 goto fast_descend_into_child_node
;
943 ntfs_unmap_page(page
);
944 goto descend_into_child_node
;
946 ntfs_error(sb
, "Negative child node vcn in directory inode "
947 "0x%lx.", dir_ni
->mft_no
);
951 /* No child node, return -ENOENT. */
952 ntfs_debug("Entry not found.");
955 ntfs_unmap_page(page
);
958 put_attr_search_ctx(ctx
);
960 unmap_mft_record(dir_ni
);
961 return ERR_MREF(err
);
963 ntfs_error(sb
, "Corrupt directory. Aborting lookup.");
972 INDEX_ALLOCATION
*ia
;
973 } index_union
__attribute__ ((__transparent_union__
));
976 INDEX_TYPE_ROOT
, /* index root */
977 INDEX_TYPE_ALLOCATION
, /* index allocation */
981 * ntfs_filldir - ntfs specific filldir method
982 * @vol: current ntfs volume
983 * @fpos: position in the directory
984 * @ndir: ntfs inode of current directory
985 * @index_type: specifies whether @iu is an index root or an index allocation
986 * @iu: index root or index allocation attribute to which @ie belongs
987 * @ie: current index entry
988 * @name: buffer to use for the converted name
989 * @dirent: vfs filldir callback context
990 * @filldir: vfs filldir callback
992 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
995 static inline int ntfs_filldir(ntfs_volume
*vol
, loff_t
*fpos
,
996 ntfs_inode
*ndir
, const INDEX_TYPE index_type
,
997 index_union iu
, INDEX_ENTRY
*ie
, u8
*name
,
998 void *dirent
, filldir_t filldir
)
1002 FILE_NAME_TYPE_FLAGS name_type
;
1004 /* Advance the position even if going to skip the entry. */
1005 if (index_type
== INDEX_TYPE_ALLOCATION
)
1006 *fpos
= (u8
*)ie
- (u8
*)iu
.ia
+
1007 (sle64_to_cpu(iu
.ia
->index_block_vcn
) <<
1008 ndir
->itype
.index
.vcn_size_bits
) +
1009 vol
->mft_record_size
;
1010 else /* if (index_type == INDEX_TYPE_ROOT) */
1011 *fpos
= (u8
*)ie
- (u8
*)iu
.ir
;
1012 name_type
= ie
->key
.file_name
.file_name_type
;
1013 if (name_type
== FILE_NAME_DOS
) {
1014 ntfs_debug("Skipping DOS name space entry.");
1017 if (MREF_LE(ie
->data
.dir
.indexed_file
) == FILE_root
) {
1018 ntfs_debug("Skipping root directory self reference entry.");
1021 if (MREF_LE(ie
->data
.dir
.indexed_file
) < FILE_first_user
&&
1022 !NVolShowSystemFiles(vol
)) {
1023 ntfs_debug("Skipping system file.");
1026 name_len
= ntfs_ucstonls(vol
, (uchar_t
*)&ie
->key
.file_name
.file_name
,
1027 ie
->key
.file_name
.file_name_length
, &name
,
1028 NTFS_MAX_NAME_LEN
* NLS_MAX_CHARSET_SIZE
+ 1);
1029 if (name_len
<= 0) {
1030 ntfs_debug("Skipping unrepresentable file.");
1033 if (ie
->key
.file_name
.file_attributes
&
1034 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT
)
1038 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%Lx, inode "
1039 "0x%lx, DT_%s.", name
, name_len
, *fpos
,
1040 MREF_LE(ie
->data
.dir
.indexed_file
),
1041 dt_type
== DT_DIR
? "DIR" : "REG");
1042 return filldir(dirent
, name
, name_len
, *fpos
,
1043 MREF_LE(ie
->data
.dir
.indexed_file
), dt_type
);
1047 * VFS calls readdir without BKL but with i_sem held. This protects the VFS
1048 * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1051 * We use the same basic approach as the old NTFS driver, i.e. we parse the
1052 * index root entries and then the index allocation entries that are marked
1053 * as in use in the index bitmap.
1054 * While this will return the names in random order this doesn't matter for
1055 * readdir but OTOH results in a faster readdir.
1057 static int ntfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1059 s64 ia_pos
, ia_start
, prev_ia_pos
, bmp_pos
;
1061 struct inode
*bmp_vi
, *vdir
= filp
->f_dentry
->d_inode
;
1062 struct super_block
*sb
= vdir
->i_sb
;
1063 ntfs_inode
*ndir
= NTFS_I(vdir
);
1064 ntfs_volume
*vol
= NTFS_SB(sb
);
1068 INDEX_ALLOCATION
*ia
;
1070 int rc
, err
, ir_pos
, cur_bmp_pos
;
1071 struct address_space
*ia_mapping
, *bmp_mapping
;
1072 struct page
*bmp_page
= NULL
, *ia_page
= NULL
;
1073 u8
*kaddr
, *bmp
, *index_end
;
1074 attr_search_context
*ctx
;
1077 ntfs_debug("Entering for inode 0x%lx, fpos 0x%Lx.",
1080 /* Are we at end of dir yet? */
1081 if (fpos
>= vdir
->i_size
+ vol
->mft_record_size
)
1083 /* Emulate . and .. for all directories. */
1085 ntfs_debug("Calling filldir for . with len 1, fpos 0x0, "
1086 "inode 0x%lx, DT_DIR.", vdir
->i_ino
);
1087 rc
= filldir(dirent
, ".", 1, fpos
, vdir
->i_ino
, DT_DIR
);
1093 ntfs_debug("Calling filldir for .. with len 2, fpos 0x1, "
1094 "inode 0x%lx, DT_DIR.",
1095 parent_ino(filp
->f_dentry
));
1096 rc
= filldir(dirent
, "..", 2, fpos
,
1097 parent_ino(filp
->f_dentry
), DT_DIR
);
1105 * Allocate a buffer to store the current name being processed
1106 * converted to format determined by current NLS.
1108 name
= (u8
*)kmalloc(NTFS_MAX_NAME_LEN
* NLS_MAX_CHARSET_SIZE
+ 1,
1110 if (unlikely(!name
)) {
1114 /* Are we jumping straight into the index allocation attribute? */
1115 if (fpos
>= vol
->mft_record_size
)
1116 goto skip_index_root
;
1117 /* Get hold of the mft record for the directory. */
1118 m
= map_mft_record(ndir
);
1119 if (unlikely(IS_ERR(m
))) {
1124 ctx
= get_attr_search_ctx(ndir
, m
);
1125 if (unlikely(!ctx
)) {
1129 /* Get the offset into the index root attribute. */
1131 /* Find the index root attribute in the mft record. */
1132 if (unlikely(!lookup_attr(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0,
1134 ntfs_error(sb
, "Index root attribute missing in directory "
1135 "inode 0x%lx.", vdir
->i_ino
);
1138 /* Get to the index root value (it's been verified in read_inode). */
1139 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
1140 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
1141 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
1142 /* The first index entry. */
1143 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
1144 le32_to_cpu(ir
->index
.entries_offset
));
1146 * Loop until we exceed valid memory (corruption case) or until we
1147 * reach the last entry or until filldir tells us it has had enough
1148 * or signals an error (both covered by the rc test).
1150 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
1151 ntfs_debug("In index root, offset 0x%x.", (u8
*)ie
- (u8
*)ir
);
1152 /* Bounds checks. */
1153 if (unlikely((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
1154 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
1155 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
1158 /* The last entry cannot contain a name. */
1159 if (ie
->flags
& INDEX_ENTRY_END
)
1161 /* Skip index root entry if continuing previous readdir. */
1162 if (ir_pos
> (u8
*)ie
- (u8
*)ir
)
1164 /* Submit the name to the filldir callback. */
1165 rc
= ntfs_filldir(vol
, &fpos
, ndir
, INDEX_TYPE_ROOT
, ir
, ie
,
1166 name
, dirent
, filldir
);
1168 put_attr_search_ctx(ctx
);
1169 unmap_mft_record(ndir
);
1174 * We are done with the index root and the mft record for that matter.
1175 * We need to release it, otherwise we deadlock on ntfs_attr_iget()
1176 * and/or ntfs_read_page().
1178 put_attr_search_ctx(ctx
);
1179 unmap_mft_record(ndir
);
1182 /* If there is no index allocation attribute we are finished. */
1183 if (!NInoIndexAllocPresent(ndir
))
1185 /* Advance fpos to the beginning of the index allocation. */
1186 fpos
= vol
->mft_record_size
;
1190 /* Get the offset into the index allocation attribute. */
1191 ia_pos
= (s64
)fpos
- vol
->mft_record_size
;
1192 ia_mapping
= vdir
->i_mapping
;
1193 bmp_vi
= ndir
->itype
.index
.bmp_ino
;
1194 if (unlikely(!bmp_vi
)) {
1195 ntfs_debug("Inode %lu, regetting index bitmap.", vdir
->i_ino
);
1196 bmp_vi
= ntfs_attr_iget(vdir
, AT_BITMAP
, I30
, 4);
1197 if (unlikely(IS_ERR(bmp_vi
))) {
1198 ntfs_error(sb
, "Failed to get bitmap attribute.");
1199 err
= PTR_ERR(bmp_vi
);
1202 ndir
->itype
.index
.bmp_ino
= bmp_vi
;
1204 bmp_mapping
= bmp_vi
->i_mapping
;
1205 /* Get the starting bitmap bit position and sanity check it. */
1206 bmp_pos
= ia_pos
>> ndir
->itype
.index
.block_size_bits
;
1207 if (unlikely(bmp_pos
>> 3 >= bmp_vi
->i_size
)) {
1208 ntfs_error(sb
, "Current index allocation position exceeds "
1209 "index bitmap size.");
1212 /* Get the starting bit position in the current bitmap page. */
1213 cur_bmp_pos
= bmp_pos
& ((PAGE_CACHE_SIZE
* 8) - 1);
1214 bmp_pos
&= ~(u64
)((PAGE_CACHE_SIZE
* 8) - 1);
1216 ntfs_debug("Reading bitmap with page index 0x%Lx, bit ofs 0x%Lx",
1217 (long long)bmp_pos
>> (3 + PAGE_CACHE_SHIFT
),
1218 (long long)bmp_pos
& ((PAGE_CACHE_SIZE
* 8) - 1));
1219 bmp_page
= ntfs_map_page(bmp_mapping
,
1220 bmp_pos
>> (3 + PAGE_CACHE_SHIFT
));
1221 if (unlikely(IS_ERR(bmp_page
))) {
1222 ntfs_error(sb
, "Reading index bitmap failed.");
1223 err
= PTR_ERR(bmp_page
);
1227 bmp
= (u8
*)page_address(bmp_page
);
1228 /* Find next index block in use. */
1229 while (!(bmp
[cur_bmp_pos
>> 3] & (1 << (cur_bmp_pos
& 7)))) {
1230 find_next_index_buffer
:
1233 * If we have reached the end of the bitmap page, get the next
1234 * page, and put away the old one.
1236 if (unlikely((cur_bmp_pos
>> 3) >= PAGE_CACHE_SIZE
)) {
1237 ntfs_unmap_page(bmp_page
);
1238 bmp_pos
+= PAGE_CACHE_SIZE
* 8;
1240 goto get_next_bmp_page
;
1242 /* If we have reached the end of the bitmap, we are done. */
1243 if (unlikely(((bmp_pos
+ cur_bmp_pos
) >> 3) >= vdir
->i_size
))
1245 ia_pos
= (bmp_pos
+ cur_bmp_pos
) <<
1246 ndir
->itype
.index
.block_size_bits
;
1248 ntfs_debug("Handling index buffer 0x%Lx.",
1249 (long long)bmp_pos
+ cur_bmp_pos
);
1250 /* If the current index buffer is in the same page we reuse the page. */
1251 if ((prev_ia_pos
& PAGE_CACHE_MASK
) != (ia_pos
& PAGE_CACHE_MASK
)) {
1252 prev_ia_pos
= ia_pos
;
1253 if (likely(ia_page
!= NULL
))
1254 ntfs_unmap_page(ia_page
);
1256 * Map the page cache page containing the current ia_pos,
1257 * reading it from disk if necessary.
1259 ia_page
= ntfs_map_page(ia_mapping
, ia_pos
>> PAGE_CACHE_SHIFT
);
1260 if (unlikely(IS_ERR(ia_page
))) {
1261 ntfs_error(sb
, "Reading index allocation data failed.");
1262 err
= PTR_ERR(ia_page
);
1266 kaddr
= (u8
*)page_address(ia_page
);
1268 /* Get the current index buffer. */
1269 ia
= (INDEX_ALLOCATION
*)(kaddr
+ (ia_pos
& ~PAGE_CACHE_MASK
&
1270 ~(s64
)(ndir
->itype
.index
.block_size
- 1)));
1271 /* Bounds checks. */
1272 if (unlikely((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_CACHE_SIZE
)) {
1273 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
1274 "inode 0x%lx or driver bug.", vdir
->i_ino
);
1277 if (unlikely(sle64_to_cpu(ia
->index_block_vcn
) != (ia_pos
&
1278 ~(s64
)(ndir
->itype
.index
.block_size
- 1)) >>
1279 ndir
->itype
.index
.vcn_size_bits
)) {
1280 ntfs_error(sb
, "Actual VCN (0x%Lx) of index buffer is "
1281 "different from expected VCN (0x%Lx). "
1282 "Directory inode 0x%lx is corrupt or driver "
1284 (long long)sle64_to_cpu(ia
->index_block_vcn
),
1285 (long long)ia_pos
>>
1286 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1289 if (unlikely(le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
1290 ndir
->itype
.index
.block_size
)) {
1291 ntfs_error(sb
, "Index buffer (VCN 0x%Lx) of directory inode "
1292 "0x%lx has a size (%u) differing from the "
1293 "directory specified size (%u). Directory "
1294 "inode is corrupt or driver bug.",
1295 (long long)ia_pos
>>
1296 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
,
1297 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
1298 ndir
->itype
.index
.block_size
);
1301 index_end
= (u8
*)ia
+ ndir
->itype
.index
.block_size
;
1302 if (unlikely(index_end
> kaddr
+ PAGE_CACHE_SIZE
)) {
1303 ntfs_error(sb
, "Index buffer (VCN 0x%Lx) of directory inode "
1304 "0x%lx crosses page boundary. Impossible! "
1305 "Cannot access! This is probably a bug in the "
1306 "driver.", (long long)ia_pos
>>
1307 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1310 ia_start
= ia_pos
& ~(s64
)(ndir
->itype
.index
.block_size
- 1);
1311 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
1312 if (unlikely(index_end
> (u8
*)ia
+ ndir
->itype
.index
.block_size
)) {
1313 ntfs_error(sb
, "Size of index buffer (VCN 0x%Lx) of directory "
1314 "inode 0x%lx exceeds maximum size.",
1315 (long long)ia_pos
>>
1316 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1319 /* The first index entry in this index buffer. */
1320 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
1321 le32_to_cpu(ia
->index
.entries_offset
));
1323 * Loop until we exceed valid memory (corruption case) or until we
1324 * reach the last entry or until filldir tells us it has had enough
1325 * or signals an error (both covered by the rc test).
1327 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
1328 ntfs_debug("In index allocation, offset 0x%Lx.",
1329 (long long)ia_start
+ ((u8
*)ie
- (u8
*)ia
));
1330 /* Bounds checks. */
1331 if (unlikely((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
1332 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
1333 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
1336 /* The last entry cannot contain a name. */
1337 if (ie
->flags
& INDEX_ENTRY_END
)
1339 /* Skip index block entry if continuing previous readdir. */
1340 if (ia_pos
- ia_start
> (u8
*)ie
- (u8
*)ia
)
1342 /* Submit the name to the filldir callback. */
1343 rc
= ntfs_filldir(vol
, &fpos
, ndir
, INDEX_TYPE_ALLOCATION
, ia
,
1344 ie
, name
, dirent
, filldir
);
1346 ntfs_unmap_page(ia_page
);
1347 ntfs_unmap_page(bmp_page
);
1351 goto find_next_index_buffer
;
1354 ntfs_unmap_page(ia_page
);
1355 ntfs_unmap_page(bmp_page
);
1357 /* We are finished, set fpos to EOD. */
1358 fpos
= vdir
->i_size
+ vol
->mft_record_size
;
1364 ntfs_debug("EOD, fpos 0x%Lx, returning 0.", fpos
);
1366 ntfs_debug("filldir returned %i, fpos 0x%Lx, returning 0.",
1373 ntfs_unmap_page(bmp_page
);
1375 ntfs_unmap_page(ia_page
);
1379 put_attr_search_ctx(ctx
);
1381 unmap_mft_record(ndir
);
1384 ntfs_debug("Failed. Returning error code %i.", -err
);
1390 * ntfs_dir_open - called when an inode is about to be opened
1391 * @vi: inode to be opened
1392 * @filp: file structure describing the inode
1394 * Limit directory size to the page cache limit on architectures where unsigned
1395 * long is 32-bits. This is the most we can do for now without overflowing the
1396 * page cache page index. Doing it this way means we don't run into problems
1397 * because of existing too large directories. It would be better to allow the
1398 * user to read the accessible part of the directory but I doubt very much
1399 * anyone is going to hit this check on a 32-bit architecture, so there is no
1400 * point in adding the extra complexity required to support this.
1402 * On 64-bit architectures, the check is hopefully optimized away by the
1405 static int ntfs_dir_open(struct inode
*vi
, struct file
*filp
)
1407 if (sizeof(unsigned long) < 8) {
1408 if (vi
->i_size
> MAX_LFS_FILESIZE
)
1414 struct file_operations ntfs_dir_ops
= {
1415 .llseek
= generic_file_llseek
, /* Seek inside directory. */
1416 .read
= generic_read_dir
, /* Return -EISDIR. */
1417 .readdir
= ntfs_readdir
, /* Read directory contents. */
1418 .open
= ntfs_dir_open
, /* Open directory. */