2 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2007 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/buffer_head.h>
24 #include <linux/slab.h>
34 * The little endian Unicode string $I30 as a global constant.
36 ntfschar I30
[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
37 cpu_to_le16('3'), cpu_to_le16('0'), 0 };
40 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
41 * @dir_ni: ntfs inode of the directory in which to search for the name
42 * @uname: Unicode name for which to search in the directory
43 * @uname_len: length of the name @uname in Unicode characters
44 * @res: return the found file name if necessary (see below)
46 * Look for an inode with name @uname in the directory with inode @dir_ni.
47 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
48 * the Unicode name. If the name is found in the directory, the corresponding
49 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
50 * is a 64-bit number containing the sequence number.
52 * On error, a negative value is returned corresponding to the error code. In
53 * particular if the inode is not found -ENOENT is returned. Note that you
54 * can't just check the return value for being negative, you have to check the
55 * inode number for being negative which you can extract using MREC(return
58 * Note, @uname_len does not include the (optional) terminating NULL character.
60 * Note, we look for a case sensitive match first but we also look for a case
61 * insensitive match at the same time. If we find a case insensitive match, we
62 * save that for the case that we don't find an exact match, where we return
63 * the case insensitive match and setup @res (which we allocate!) with the mft
64 * reference, the file name type, length and with a copy of the little endian
65 * Unicode file name itself. If we match a file name which is in the DOS name
66 * space, we only return the mft reference and file name type in @res.
67 * ntfs_lookup() then uses this to find the long file name in the inode itself.
68 * This is to avoid polluting the dcache with short file names. We want them to
69 * work but we don't care for how quickly one can access them. This also fixes
70 * the dcache aliasing issues.
72 * Locking: - Caller must hold i_mutex on the directory.
73 * - Each page cache page in the index allocation mapping must be
74 * locked whilst being accessed otherwise we may find a corrupt
75 * page due to it being under ->writepage at the moment which
76 * applies the mst protection fixups before writing out and then
77 * removes them again after the write is complete after which it
80 MFT_REF
ntfs_lookup_inode_by_name(ntfs_inode
*dir_ni
, const ntfschar
*uname
,
81 const int uname_len
, ntfs_name
**res
)
83 ntfs_volume
*vol
= dir_ni
->vol
;
84 struct super_block
*sb
= vol
->sb
;
91 ntfs_attr_search_ctx
*ctx
;
94 struct address_space
*ia_mapping
;
97 ntfs_name
*name
= NULL
;
99 BUG_ON(!S_ISDIR(VFS_I(dir_ni
)->i_mode
));
100 BUG_ON(NInoAttr(dir_ni
));
101 /* Get hold of the mft record for the directory. */
102 m
= map_mft_record(dir_ni
);
104 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
106 return ERR_MREF(PTR_ERR(m
));
108 ctx
= ntfs_attr_get_search_ctx(dir_ni
, m
);
109 if (unlikely(!ctx
)) {
113 /* Find the index root attribute in the mft record. */
114 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
,
117 if (err
== -ENOENT
) {
118 ntfs_error(sb
, "Index root attribute missing in "
119 "directory inode 0x%lx.",
125 /* Get to the index root value (it's been verified in read_inode). */
126 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
127 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
128 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
129 /* The first index entry. */
130 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
131 le32_to_cpu(ir
->index
.entries_offset
));
133 * Loop until we exceed valid memory (corruption case) or until we
134 * reach the last entry.
136 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
138 if ((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
139 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
140 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
144 * The last entry cannot contain a name. It can however contain
145 * a pointer to a child node in the B+tree so we just break out.
147 if (ie
->flags
& INDEX_ENTRY_END
)
150 * We perform a case sensitive comparison and if that matches
151 * we are done and return the mft reference of the inode (i.e.
152 * the inode number together with the sequence number for
153 * consistency checking). We convert it to cpu format before
156 if (ntfs_are_names_equal(uname
, uname_len
,
157 (ntfschar
*)&ie
->key
.file_name
.file_name
,
158 ie
->key
.file_name
.file_name_length
,
159 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
)) {
162 * We have a perfect match, so we don't need to care
163 * about having matched imperfectly before, so we can
164 * free name and set *res to NULL.
165 * However, if the perfect match is a short file name,
166 * we need to signal this through *res, so that
167 * ntfs_lookup() can fix dcache aliasing issues.
168 * As an optimization we just reuse an existing
169 * allocation of *res.
171 if (ie
->key
.file_name
.file_name_type
== FILE_NAME_DOS
) {
173 name
= kmalloc(sizeof(ntfs_name
),
180 name
->mref
= le64_to_cpu(
181 ie
->data
.dir
.indexed_file
);
182 name
->type
= FILE_NAME_DOS
;
189 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
190 ntfs_attr_put_search_ctx(ctx
);
191 unmap_mft_record(dir_ni
);
195 * For a case insensitive mount, we also perform a case
196 * insensitive comparison (provided the file name is not in the
197 * POSIX namespace). If the comparison matches, and the name is
198 * in the WIN32 namespace, we cache the filename in *res so
199 * that the caller, ntfs_lookup(), can work on it. If the
200 * comparison matches, and the name is in the DOS namespace, we
201 * only cache the mft reference and the file name type (we set
202 * the name length to zero for simplicity).
204 if (!NVolCaseSensitive(vol
) &&
205 ie
->key
.file_name
.file_name_type
&&
206 ntfs_are_names_equal(uname
, uname_len
,
207 (ntfschar
*)&ie
->key
.file_name
.file_name
,
208 ie
->key
.file_name
.file_name_length
,
209 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
)) {
210 int name_size
= sizeof(ntfs_name
);
211 u8 type
= ie
->key
.file_name
.file_name_type
;
212 u8 len
= ie
->key
.file_name
.file_name_length
;
214 /* Only one case insensitive matching name allowed. */
216 ntfs_error(sb
, "Found already allocated name "
217 "in phase 1. Please run chkdsk "
218 "and if that doesn't find any "
219 "errors please report you saw "
221 "linux-ntfs-dev@lists."
226 if (type
!= FILE_NAME_DOS
)
227 name_size
+= len
* sizeof(ntfschar
);
228 name
= kmalloc(name_size
, GFP_NOFS
);
233 name
->mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
235 if (type
!= FILE_NAME_DOS
) {
237 memcpy(name
->name
, ie
->key
.file_name
.file_name
,
238 len
* sizeof(ntfschar
));
244 * Not a perfect match, need to do full blown collation so we
245 * know which way in the B+tree we have to go.
247 rc
= ntfs_collate_names(uname
, uname_len
,
248 (ntfschar
*)&ie
->key
.file_name
.file_name
,
249 ie
->key
.file_name
.file_name_length
, 1,
250 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
252 * If uname collates before the name of the current entry, there
253 * is definitely no such name in this index but we might need to
254 * descend into the B+tree so we just break out of the loop.
258 /* The names are not equal, continue the search. */
262 * Names match with case insensitive comparison, now try the
263 * case sensitive comparison, which is required for proper
266 rc
= ntfs_collate_names(uname
, uname_len
,
267 (ntfschar
*)&ie
->key
.file_name
.file_name
,
268 ie
->key
.file_name
.file_name_length
, 1,
269 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
275 * Perfect match, this will never happen as the
276 * ntfs_are_names_equal() call will have gotten a match but we
277 * still treat it correctly.
282 * We have finished with this index without success. Check for the
283 * presence of a child node and if not present return -ENOENT, unless
284 * we have got a matching name cached in name in which case return the
285 * mft reference associated with it.
287 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
289 ntfs_attr_put_search_ctx(ctx
);
290 unmap_mft_record(dir_ni
);
293 ntfs_debug("Entry not found.");
296 } /* Child node present, descend into it. */
297 /* Consistency check: Verify that an index allocation exists. */
298 if (!NInoIndexAllocPresent(dir_ni
)) {
299 ntfs_error(sb
, "No index allocation attribute but index entry "
300 "requires one. Directory inode 0x%lx is "
301 "corrupt or driver bug.", dir_ni
->mft_no
);
304 /* Get the starting vcn of the index_block holding the child node. */
305 vcn
= sle64_to_cpup((sle64
*)((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8));
306 ia_mapping
= VFS_I(dir_ni
)->i_mapping
;
308 * We are done with the index root and the mft record. Release them,
309 * otherwise we deadlock with ntfs_map_page().
311 ntfs_attr_put_search_ctx(ctx
);
312 unmap_mft_record(dir_ni
);
315 descend_into_child_node
:
317 * Convert vcn to index into the index allocation attribute in units
318 * of PAGE_SIZE and map the page cache page, reading it from
321 page
= ntfs_map_page(ia_mapping
, vcn
<<
322 dir_ni
->itype
.index
.vcn_size_bits
>> PAGE_SHIFT
);
324 ntfs_error(sb
, "Failed to map directory index page, error %ld.",
330 kaddr
= (u8
*)page_address(page
);
331 fast_descend_into_child_node
:
332 /* Get to the index allocation block. */
333 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
334 dir_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_MASK
));
336 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_SIZE
) {
337 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
338 "inode 0x%lx or driver bug.", dir_ni
->mft_no
);
341 /* Catch multi sector transfer fixup errors. */
342 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
343 ntfs_error(sb
, "Directory index record with vcn 0x%llx is "
344 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
345 (unsigned long long)vcn
, dir_ni
->mft_no
);
348 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
349 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
350 "different from expected VCN (0x%llx). "
351 "Directory inode 0x%lx is corrupt or driver "
352 "bug.", (unsigned long long)
353 sle64_to_cpu(ia
->index_block_vcn
),
354 (unsigned long long)vcn
, dir_ni
->mft_no
);
357 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
358 dir_ni
->itype
.index
.block_size
) {
359 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
360 "0x%lx has a size (%u) differing from the "
361 "directory specified size (%u). Directory "
362 "inode is corrupt or driver bug.",
363 (unsigned long long)vcn
, dir_ni
->mft_no
,
364 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
365 dir_ni
->itype
.index
.block_size
);
368 index_end
= (u8
*)ia
+ dir_ni
->itype
.index
.block_size
;
369 if (index_end
> kaddr
+ PAGE_SIZE
) {
370 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
371 "0x%lx crosses page boundary. Impossible! "
372 "Cannot access! This is probably a bug in the "
373 "driver.", (unsigned long long)vcn
,
377 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
378 if (index_end
> (u8
*)ia
+ dir_ni
->itype
.index
.block_size
) {
379 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of directory "
380 "inode 0x%lx exceeds maximum size.",
381 (unsigned long long)vcn
, dir_ni
->mft_no
);
384 /* The first index entry. */
385 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
386 le32_to_cpu(ia
->index
.entries_offset
));
388 * Iterate similar to above big loop but applied to index buffer, thus
389 * loop until we exceed valid memory (corruption case) or until we
390 * reach the last entry.
392 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
394 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
395 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
396 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
398 ntfs_error(sb
, "Index entry out of bounds in "
399 "directory inode 0x%lx.",
404 * The last entry cannot contain a name. It can however contain
405 * a pointer to a child node in the B+tree so we just break out.
407 if (ie
->flags
& INDEX_ENTRY_END
)
410 * We perform a case sensitive comparison and if that matches
411 * we are done and return the mft reference of the inode (i.e.
412 * the inode number together with the sequence number for
413 * consistency checking). We convert it to cpu format before
416 if (ntfs_are_names_equal(uname
, uname_len
,
417 (ntfschar
*)&ie
->key
.file_name
.file_name
,
418 ie
->key
.file_name
.file_name_length
,
419 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
)) {
422 * We have a perfect match, so we don't need to care
423 * about having matched imperfectly before, so we can
424 * free name and set *res to NULL.
425 * However, if the perfect match is a short file name,
426 * we need to signal this through *res, so that
427 * ntfs_lookup() can fix dcache aliasing issues.
428 * As an optimization we just reuse an existing
429 * allocation of *res.
431 if (ie
->key
.file_name
.file_name_type
== FILE_NAME_DOS
) {
433 name
= kmalloc(sizeof(ntfs_name
),
440 name
->mref
= le64_to_cpu(
441 ie
->data
.dir
.indexed_file
);
442 name
->type
= FILE_NAME_DOS
;
449 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
451 ntfs_unmap_page(page
);
455 * For a case insensitive mount, we also perform a case
456 * insensitive comparison (provided the file name is not in the
457 * POSIX namespace). If the comparison matches, and the name is
458 * in the WIN32 namespace, we cache the filename in *res so
459 * that the caller, ntfs_lookup(), can work on it. If the
460 * comparison matches, and the name is in the DOS namespace, we
461 * only cache the mft reference and the file name type (we set
462 * the name length to zero for simplicity).
464 if (!NVolCaseSensitive(vol
) &&
465 ie
->key
.file_name
.file_name_type
&&
466 ntfs_are_names_equal(uname
, uname_len
,
467 (ntfschar
*)&ie
->key
.file_name
.file_name
,
468 ie
->key
.file_name
.file_name_length
,
469 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
)) {
470 int name_size
= sizeof(ntfs_name
);
471 u8 type
= ie
->key
.file_name
.file_name_type
;
472 u8 len
= ie
->key
.file_name
.file_name_length
;
474 /* Only one case insensitive matching name allowed. */
476 ntfs_error(sb
, "Found already allocated name "
477 "in phase 2. Please run chkdsk "
478 "and if that doesn't find any "
479 "errors please report you saw "
481 "linux-ntfs-dev@lists."
484 ntfs_unmap_page(page
);
488 if (type
!= FILE_NAME_DOS
)
489 name_size
+= len
* sizeof(ntfschar
);
490 name
= kmalloc(name_size
, GFP_NOFS
);
495 name
->mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
497 if (type
!= FILE_NAME_DOS
) {
499 memcpy(name
->name
, ie
->key
.file_name
.file_name
,
500 len
* sizeof(ntfschar
));
506 * Not a perfect match, need to do full blown collation so we
507 * know which way in the B+tree we have to go.
509 rc
= ntfs_collate_names(uname
, uname_len
,
510 (ntfschar
*)&ie
->key
.file_name
.file_name
,
511 ie
->key
.file_name
.file_name_length
, 1,
512 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
514 * If uname collates before the name of the current entry, there
515 * is definitely no such name in this index but we might need to
516 * descend into the B+tree so we just break out of the loop.
520 /* The names are not equal, continue the search. */
524 * Names match with case insensitive comparison, now try the
525 * case sensitive comparison, which is required for proper
528 rc
= ntfs_collate_names(uname
, uname_len
,
529 (ntfschar
*)&ie
->key
.file_name
.file_name
,
530 ie
->key
.file_name
.file_name_length
, 1,
531 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
537 * Perfect match, this will never happen as the
538 * ntfs_are_names_equal() call will have gotten a match but we
539 * still treat it correctly.
544 * We have finished with this index buffer without success. Check for
545 * the presence of a child node.
547 if (ie
->flags
& INDEX_ENTRY_NODE
) {
548 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
549 ntfs_error(sb
, "Index entry with child node found in "
550 "a leaf node in directory inode 0x%lx.",
554 /* Child node present, descend into it. */
556 vcn
= sle64_to_cpup((sle64
*)((u8
*)ie
+
557 le16_to_cpu(ie
->length
) - 8));
559 /* If vcn is in the same page cache page as old_vcn we
560 * recycle the mapped page. */
561 if (old_vcn
<< vol
->cluster_size_bits
>>
563 vol
->cluster_size_bits
>>
565 goto fast_descend_into_child_node
;
567 ntfs_unmap_page(page
);
568 goto descend_into_child_node
;
570 ntfs_error(sb
, "Negative child node vcn in directory inode "
571 "0x%lx.", dir_ni
->mft_no
);
575 * No child node present, return -ENOENT, unless we have got a matching
576 * name cached in name in which case return the mft reference
577 * associated with it.
581 ntfs_unmap_page(page
);
584 ntfs_debug("Entry not found.");
588 ntfs_unmap_page(page
);
593 ntfs_attr_put_search_ctx(ctx
);
595 unmap_mft_record(dir_ni
);
600 return ERR_MREF(err
);
602 ntfs_error(sb
, "Corrupt directory. Aborting lookup.");
609 // The algorithm embedded in this code will be required for the time when we
610 // want to support adding of entries to directories, where we require correct
611 // collation of file names in order not to cause corruption of the filesystem.
614 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
615 * @dir_ni: ntfs inode of the directory in which to search for the name
616 * @uname: Unicode name for which to search in the directory
617 * @uname_len: length of the name @uname in Unicode characters
619 * Look for an inode with name @uname in the directory with inode @dir_ni.
620 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
621 * the Unicode name. If the name is found in the directory, the corresponding
622 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
623 * is a 64-bit number containing the sequence number.
625 * On error, a negative value is returned corresponding to the error code. In
626 * particular if the inode is not found -ENOENT is returned. Note that you
627 * can't just check the return value for being negative, you have to check the
628 * inode number for being negative which you can extract using MREC(return
631 * Note, @uname_len does not include the (optional) terminating NULL character.
633 u64
ntfs_lookup_inode_by_name(ntfs_inode
*dir_ni
, const ntfschar
*uname
,
636 ntfs_volume
*vol
= dir_ni
->vol
;
637 struct super_block
*sb
= vol
->sb
;
641 INDEX_ALLOCATION
*ia
;
644 ntfs_attr_search_ctx
*ctx
;
648 struct address_space
*ia_mapping
;
652 /* Get hold of the mft record for the directory. */
653 m
= map_mft_record(dir_ni
);
655 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
657 return ERR_MREF(PTR_ERR(m
));
659 ctx
= ntfs_attr_get_search_ctx(dir_ni
, m
);
664 /* Find the index root attribute in the mft record. */
665 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
,
668 if (err
== -ENOENT
) {
669 ntfs_error(sb
, "Index root attribute missing in "
670 "directory inode 0x%lx.",
676 /* Get to the index root value (it's been verified in read_inode). */
677 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
678 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
679 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
680 /* The first index entry. */
681 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
682 le32_to_cpu(ir
->index
.entries_offset
));
684 * Loop until we exceed valid memory (corruption case) or until we
685 * reach the last entry.
687 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
689 if ((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
690 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
691 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
695 * The last entry cannot contain a name. It can however contain
696 * a pointer to a child node in the B+tree so we just break out.
698 if (ie
->flags
& INDEX_ENTRY_END
)
701 * If the current entry has a name type of POSIX, the name is
702 * case sensitive and not otherwise. This has the effect of us
703 * not being able to access any POSIX file names which collate
704 * after the non-POSIX one when they only differ in case, but
705 * anyone doing screwy stuff like that deserves to burn in
706 * hell... Doing that kind of stuff on NT4 actually causes
707 * corruption on the partition even when using SP6a and Linux
708 * is not involved at all.
710 ic
= ie
->key
.file_name
.file_name_type
? IGNORE_CASE
:
713 * If the names match perfectly, we are done and return the
714 * mft reference of the inode (i.e. the inode number together
715 * with the sequence number for consistency checking. We
716 * convert it to cpu format before returning.
718 if (ntfs_are_names_equal(uname
, uname_len
,
719 (ntfschar
*)&ie
->key
.file_name
.file_name
,
720 ie
->key
.file_name
.file_name_length
, ic
,
721 vol
->upcase
, vol
->upcase_len
)) {
723 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
724 ntfs_attr_put_search_ctx(ctx
);
725 unmap_mft_record(dir_ni
);
729 * Not a perfect match, need to do full blown collation so we
730 * know which way in the B+tree we have to go.
732 rc
= ntfs_collate_names(uname
, uname_len
,
733 (ntfschar
*)&ie
->key
.file_name
.file_name
,
734 ie
->key
.file_name
.file_name_length
, 1,
735 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
737 * If uname collates before the name of the current entry, there
738 * is definitely no such name in this index but we might need to
739 * descend into the B+tree so we just break out of the loop.
743 /* The names are not equal, continue the search. */
747 * Names match with case insensitive comparison, now try the
748 * case sensitive comparison, which is required for proper
751 rc
= ntfs_collate_names(uname
, uname_len
,
752 (ntfschar
*)&ie
->key
.file_name
.file_name
,
753 ie
->key
.file_name
.file_name_length
, 1,
754 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
760 * Perfect match, this will never happen as the
761 * ntfs_are_names_equal() call will have gotten a match but we
762 * still treat it correctly.
767 * We have finished with this index without success. Check for the
768 * presence of a child node.
770 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
771 /* No child node, return -ENOENT. */
774 } /* Child node present, descend into it. */
775 /* Consistency check: Verify that an index allocation exists. */
776 if (!NInoIndexAllocPresent(dir_ni
)) {
777 ntfs_error(sb
, "No index allocation attribute but index entry "
778 "requires one. Directory inode 0x%lx is "
779 "corrupt or driver bug.", dir_ni
->mft_no
);
782 /* Get the starting vcn of the index_block holding the child node. */
783 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
784 ia_mapping
= VFS_I(dir_ni
)->i_mapping
;
786 * We are done with the index root and the mft record. Release them,
787 * otherwise we deadlock with ntfs_map_page().
789 ntfs_attr_put_search_ctx(ctx
);
790 unmap_mft_record(dir_ni
);
793 descend_into_child_node
:
795 * Convert vcn to index into the index allocation attribute in units
796 * of PAGE_SIZE and map the page cache page, reading it from
799 page
= ntfs_map_page(ia_mapping
, vcn
<<
800 dir_ni
->itype
.index
.vcn_size_bits
>> PAGE_SHIFT
);
802 ntfs_error(sb
, "Failed to map directory index page, error %ld.",
808 kaddr
= (u8
*)page_address(page
);
809 fast_descend_into_child_node
:
810 /* Get to the index allocation block. */
811 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
812 dir_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_MASK
));
814 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_SIZE
) {
815 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
816 "inode 0x%lx or driver bug.", dir_ni
->mft_no
);
819 /* Catch multi sector transfer fixup errors. */
820 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
821 ntfs_error(sb
, "Directory index record with vcn 0x%llx is "
822 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
823 (unsigned long long)vcn
, dir_ni
->mft_no
);
826 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
827 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
828 "different from expected VCN (0x%llx). "
829 "Directory inode 0x%lx is corrupt or driver "
830 "bug.", (unsigned long long)
831 sle64_to_cpu(ia
->index_block_vcn
),
832 (unsigned long long)vcn
, dir_ni
->mft_no
);
835 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
836 dir_ni
->itype
.index
.block_size
) {
837 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
838 "0x%lx has a size (%u) differing from the "
839 "directory specified size (%u). Directory "
840 "inode is corrupt or driver bug.",
841 (unsigned long long)vcn
, dir_ni
->mft_no
,
842 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
843 dir_ni
->itype
.index
.block_size
);
846 index_end
= (u8
*)ia
+ dir_ni
->itype
.index
.block_size
;
847 if (index_end
> kaddr
+ PAGE_SIZE
) {
848 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
849 "0x%lx crosses page boundary. Impossible! "
850 "Cannot access! This is probably a bug in the "
851 "driver.", (unsigned long long)vcn
,
855 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
856 if (index_end
> (u8
*)ia
+ dir_ni
->itype
.index
.block_size
) {
857 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of directory "
858 "inode 0x%lx exceeds maximum size.",
859 (unsigned long long)vcn
, dir_ni
->mft_no
);
862 /* The first index entry. */
863 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
864 le32_to_cpu(ia
->index
.entries_offset
));
866 * Iterate similar to above big loop but applied to index buffer, thus
867 * loop until we exceed valid memory (corruption case) or until we
868 * reach the last entry.
870 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
872 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
873 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
874 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
876 ntfs_error(sb
, "Index entry out of bounds in "
877 "directory inode 0x%lx.",
882 * The last entry cannot contain a name. It can however contain
883 * a pointer to a child node in the B+tree so we just break out.
885 if (ie
->flags
& INDEX_ENTRY_END
)
888 * If the current entry has a name type of POSIX, the name is
889 * case sensitive and not otherwise. This has the effect of us
890 * not being able to access any POSIX file names which collate
891 * after the non-POSIX one when they only differ in case, but
892 * anyone doing screwy stuff like that deserves to burn in
893 * hell... Doing that kind of stuff on NT4 actually causes
894 * corruption on the partition even when using SP6a and Linux
895 * is not involved at all.
897 ic
= ie
->key
.file_name
.file_name_type
? IGNORE_CASE
:
900 * If the names match perfectly, we are done and return the
901 * mft reference of the inode (i.e. the inode number together
902 * with the sequence number for consistency checking. We
903 * convert it to cpu format before returning.
905 if (ntfs_are_names_equal(uname
, uname_len
,
906 (ntfschar
*)&ie
->key
.file_name
.file_name
,
907 ie
->key
.file_name
.file_name_length
, ic
,
908 vol
->upcase
, vol
->upcase_len
)) {
910 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
912 ntfs_unmap_page(page
);
916 * Not a perfect match, need to do full blown collation so we
917 * know which way in the B+tree we have to go.
919 rc
= ntfs_collate_names(uname
, uname_len
,
920 (ntfschar
*)&ie
->key
.file_name
.file_name
,
921 ie
->key
.file_name
.file_name_length
, 1,
922 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
924 * If uname collates before the name of the current entry, there
925 * is definitely no such name in this index but we might need to
926 * descend into the B+tree so we just break out of the loop.
930 /* The names are not equal, continue the search. */
934 * Names match with case insensitive comparison, now try the
935 * case sensitive comparison, which is required for proper
938 rc
= ntfs_collate_names(uname
, uname_len
,
939 (ntfschar
*)&ie
->key
.file_name
.file_name
,
940 ie
->key
.file_name
.file_name_length
, 1,
941 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
947 * Perfect match, this will never happen as the
948 * ntfs_are_names_equal() call will have gotten a match but we
949 * still treat it correctly.
954 * We have finished with this index buffer without success. Check for
955 * the presence of a child node.
957 if (ie
->flags
& INDEX_ENTRY_NODE
) {
958 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
959 ntfs_error(sb
, "Index entry with child node found in "
960 "a leaf node in directory inode 0x%lx.",
964 /* Child node present, descend into it. */
966 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
968 /* If vcn is in the same page cache page as old_vcn we
969 * recycle the mapped page. */
970 if (old_vcn
<< vol
->cluster_size_bits
>>
972 vol
->cluster_size_bits
>>
974 goto fast_descend_into_child_node
;
976 ntfs_unmap_page(page
);
977 goto descend_into_child_node
;
979 ntfs_error(sb
, "Negative child node vcn in directory inode "
980 "0x%lx.", dir_ni
->mft_no
);
983 /* No child node, return -ENOENT. */
984 ntfs_debug("Entry not found.");
988 ntfs_unmap_page(page
);
993 ntfs_attr_put_search_ctx(ctx
);
995 unmap_mft_record(dir_ni
);
996 return ERR_MREF(err
);
998 ntfs_error(sb
, "Corrupt directory. Aborting lookup.");
1005 * ntfs_filldir - ntfs specific filldir method
1006 * @vol: current ntfs volume
1007 * @ndir: ntfs inode of current directory
1008 * @ia_page: page in which the index allocation buffer @ie is in resides
1009 * @ie: current index entry
1010 * @name: buffer to use for the converted name
1011 * @actor: what to feed the entries to
1013 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
1016 * If @ia_page is not NULL it is the locked page containing the index
1017 * allocation block containing the index entry @ie.
1019 * Note, we drop (and then reacquire) the page lock on @ia_page across the
1020 * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
1021 * since ntfs_lookup() will lock the same page. As an optimization, we do not
1022 * retake the lock if we are returning a non-zero value as ntfs_readdir()
1023 * would need to drop the lock immediately anyway.
1025 static inline int ntfs_filldir(ntfs_volume
*vol
,
1026 ntfs_inode
*ndir
, struct page
*ia_page
, INDEX_ENTRY
*ie
,
1027 u8
*name
, struct dir_context
*actor
)
1032 FILE_NAME_TYPE_FLAGS name_type
;
1034 name_type
= ie
->key
.file_name
.file_name_type
;
1035 if (name_type
== FILE_NAME_DOS
) {
1036 ntfs_debug("Skipping DOS name space entry.");
1039 if (MREF_LE(ie
->data
.dir
.indexed_file
) == FILE_root
) {
1040 ntfs_debug("Skipping root directory self reference entry.");
1043 if (MREF_LE(ie
->data
.dir
.indexed_file
) < FILE_first_user
&&
1044 !NVolShowSystemFiles(vol
)) {
1045 ntfs_debug("Skipping system file.");
1048 name_len
= ntfs_ucstonls(vol
, (ntfschar
*)&ie
->key
.file_name
.file_name
,
1049 ie
->key
.file_name
.file_name_length
, &name
,
1050 NTFS_MAX_NAME_LEN
* NLS_MAX_CHARSET_SIZE
+ 1);
1051 if (name_len
<= 0) {
1052 ntfs_warning(vol
->sb
, "Skipping unrepresentable inode 0x%llx.",
1053 (long long)MREF_LE(ie
->data
.dir
.indexed_file
));
1056 if (ie
->key
.file_name
.file_attributes
&
1057 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT
)
1061 mref
= MREF_LE(ie
->data
.dir
.indexed_file
);
1063 * Drop the page lock otherwise we deadlock with NFS when it calls
1064 * ->lookup since ntfs_lookup() will lock the same page.
1067 unlock_page(ia_page
);
1068 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
1069 "0x%lx, DT_%s.", name
, name_len
, actor
->pos
, mref
,
1070 dt_type
== DT_DIR
? "DIR" : "REG");
1071 if (!dir_emit(actor
, name
, name_len
, mref
, dt_type
))
1073 /* Relock the page but not if we are aborting ->readdir. */
1080 * We use the same basic approach as the old NTFS driver, i.e. we parse the
1081 * index root entries and then the index allocation entries that are marked
1082 * as in use in the index bitmap.
1084 * While this will return the names in random order this doesn't matter for
1085 * ->readdir but OTOH results in a faster ->readdir.
1087 * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
1088 * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1091 * Locking: - Caller must hold i_mutex on the directory.
1092 * - Each page cache page in the index allocation mapping must be
1093 * locked whilst being accessed otherwise we may find a corrupt
1094 * page due to it being under ->writepage at the moment which
1095 * applies the mst protection fixups before writing out and then
1096 * removes them again after the write is complete after which it
1099 static int ntfs_readdir(struct file
*file
, struct dir_context
*actor
)
1101 s64 ia_pos
, ia_start
, prev_ia_pos
, bmp_pos
;
1103 struct inode
*bmp_vi
, *vdir
= file_inode(file
);
1104 struct super_block
*sb
= vdir
->i_sb
;
1105 ntfs_inode
*ndir
= NTFS_I(vdir
);
1106 ntfs_volume
*vol
= NTFS_SB(sb
);
1108 INDEX_ROOT
*ir
= NULL
;
1110 INDEX_ALLOCATION
*ia
;
1112 int rc
, err
, ir_pos
, cur_bmp_pos
;
1113 struct address_space
*ia_mapping
, *bmp_mapping
;
1114 struct page
*bmp_page
= NULL
, *ia_page
= NULL
;
1115 u8
*kaddr
, *bmp
, *index_end
;
1116 ntfs_attr_search_ctx
*ctx
;
1118 ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
1119 vdir
->i_ino
, actor
->pos
);
1121 /* Are we at end of dir yet? */
1122 i_size
= i_size_read(vdir
);
1123 if (actor
->pos
>= i_size
+ vol
->mft_record_size
)
1125 /* Emulate . and .. for all directories. */
1126 if (!dir_emit_dots(file
, actor
))
1131 * Allocate a buffer to store the current name being processed
1132 * converted to format determined by current NLS.
1134 name
= kmalloc(NTFS_MAX_NAME_LEN
* NLS_MAX_CHARSET_SIZE
+ 1, GFP_NOFS
);
1135 if (unlikely(!name
)) {
1139 /* Are we jumping straight into the index allocation attribute? */
1140 if (actor
->pos
>= vol
->mft_record_size
)
1141 goto skip_index_root
;
1142 /* Get hold of the mft record for the directory. */
1143 m
= map_mft_record(ndir
);
1149 ctx
= ntfs_attr_get_search_ctx(ndir
, m
);
1150 if (unlikely(!ctx
)) {
1154 /* Get the offset into the index root attribute. */
1155 ir_pos
= (s64
)actor
->pos
;
1156 /* Find the index root attribute in the mft record. */
1157 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
,
1159 if (unlikely(err
)) {
1160 ntfs_error(sb
, "Index root attribute missing in directory "
1161 "inode 0x%lx.", vdir
->i_ino
);
1165 * Copy the index root attribute value to a buffer so that we can put
1166 * the search context and unmap the mft record before calling the
1167 * filldir() callback. We need to do this because of NFSd which calls
1168 * ->lookup() from its filldir callback() and this causes NTFS to
1169 * deadlock as ntfs_lookup() maps the mft record of the directory and
1170 * we have got it mapped here already. The only solution is for us to
1171 * unmap the mft record here so that a call to ntfs_lookup() is able to
1172 * map the mft record without deadlocking.
1174 rc
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
1175 ir
= kmalloc(rc
, GFP_NOFS
);
1176 if (unlikely(!ir
)) {
1180 /* Copy the index root value (it has been verified in read_inode). */
1181 memcpy(ir
, (u8
*)ctx
->attr
+
1182 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
), rc
);
1183 ntfs_attr_put_search_ctx(ctx
);
1184 unmap_mft_record(ndir
);
1187 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
1188 /* The first index entry. */
1189 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
1190 le32_to_cpu(ir
->index
.entries_offset
));
1192 * Loop until we exceed valid memory (corruption case) or until we
1193 * reach the last entry or until filldir tells us it has had enough
1194 * or signals an error (both covered by the rc test).
1196 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
1197 ntfs_debug("In index root, offset 0x%zx.", (u8
*)ie
- (u8
*)ir
);
1198 /* Bounds checks. */
1199 if (unlikely((u8
*)ie
< (u8
*)ir
|| (u8
*)ie
+
1200 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
1201 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
1204 /* The last entry cannot contain a name. */
1205 if (ie
->flags
& INDEX_ENTRY_END
)
1207 /* Skip index root entry if continuing previous readdir. */
1208 if (ir_pos
> (u8
*)ie
- (u8
*)ir
)
1210 /* Advance the position even if going to skip the entry. */
1211 actor
->pos
= (u8
*)ie
- (u8
*)ir
;
1212 /* Submit the name to the filldir callback. */
1213 rc
= ntfs_filldir(vol
, ndir
, NULL
, ie
, name
, actor
);
1219 /* We are done with the index root and can free the buffer. */
1222 /* If there is no index allocation attribute we are finished. */
1223 if (!NInoIndexAllocPresent(ndir
))
1225 /* Advance fpos to the beginning of the index allocation. */
1226 actor
->pos
= vol
->mft_record_size
;
1230 /* Get the offset into the index allocation attribute. */
1231 ia_pos
= (s64
)actor
->pos
- vol
->mft_record_size
;
1232 ia_mapping
= vdir
->i_mapping
;
1233 ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir
->i_ino
);
1234 bmp_vi
= ntfs_attr_iget(vdir
, AT_BITMAP
, I30
, 4);
1235 if (IS_ERR(bmp_vi
)) {
1236 ntfs_error(sb
, "Failed to get bitmap attribute.");
1237 err
= PTR_ERR(bmp_vi
);
1240 bmp_mapping
= bmp_vi
->i_mapping
;
1241 /* Get the starting bitmap bit position and sanity check it. */
1242 bmp_pos
= ia_pos
>> ndir
->itype
.index
.block_size_bits
;
1243 if (unlikely(bmp_pos
>> 3 >= i_size_read(bmp_vi
))) {
1244 ntfs_error(sb
, "Current index allocation position exceeds "
1245 "index bitmap size.");
1248 /* Get the starting bit position in the current bitmap page. */
1249 cur_bmp_pos
= bmp_pos
& ((PAGE_SIZE
* 8) - 1);
1250 bmp_pos
&= ~(u64
)((PAGE_SIZE
* 8) - 1);
1252 ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1253 (unsigned long long)bmp_pos
>> (3 + PAGE_SHIFT
),
1254 (unsigned long long)bmp_pos
&
1255 (unsigned long long)((PAGE_SIZE
* 8) - 1));
1256 bmp_page
= ntfs_map_page(bmp_mapping
,
1257 bmp_pos
>> (3 + PAGE_SHIFT
));
1258 if (IS_ERR(bmp_page
)) {
1259 ntfs_error(sb
, "Reading index bitmap failed.");
1260 err
= PTR_ERR(bmp_page
);
1264 bmp
= (u8
*)page_address(bmp_page
);
1265 /* Find next index block in use. */
1266 while (!(bmp
[cur_bmp_pos
>> 3] & (1 << (cur_bmp_pos
& 7)))) {
1267 find_next_index_buffer
:
1270 * If we have reached the end of the bitmap page, get the next
1271 * page, and put away the old one.
1273 if (unlikely((cur_bmp_pos
>> 3) >= PAGE_SIZE
)) {
1274 ntfs_unmap_page(bmp_page
);
1275 bmp_pos
+= PAGE_SIZE
* 8;
1277 goto get_next_bmp_page
;
1279 /* If we have reached the end of the bitmap, we are done. */
1280 if (unlikely(((bmp_pos
+ cur_bmp_pos
) >> 3) >= i_size
))
1282 ia_pos
= (bmp_pos
+ cur_bmp_pos
) <<
1283 ndir
->itype
.index
.block_size_bits
;
1285 ntfs_debug("Handling index buffer 0x%llx.",
1286 (unsigned long long)bmp_pos
+ cur_bmp_pos
);
1287 /* If the current index buffer is in the same page we reuse the page. */
1288 if ((prev_ia_pos
& (s64
)PAGE_MASK
) !=
1289 (ia_pos
& (s64
)PAGE_MASK
)) {
1290 prev_ia_pos
= ia_pos
;
1291 if (likely(ia_page
!= NULL
)) {
1292 unlock_page(ia_page
);
1293 ntfs_unmap_page(ia_page
);
1296 * Map the page cache page containing the current ia_pos,
1297 * reading it from disk if necessary.
1299 ia_page
= ntfs_map_page(ia_mapping
, ia_pos
>> PAGE_SHIFT
);
1300 if (IS_ERR(ia_page
)) {
1301 ntfs_error(sb
, "Reading index allocation data failed.");
1302 err
= PTR_ERR(ia_page
);
1307 kaddr
= (u8
*)page_address(ia_page
);
1309 /* Get the current index buffer. */
1310 ia
= (INDEX_ALLOCATION
*)(kaddr
+ (ia_pos
& ~PAGE_MASK
&
1311 ~(s64
)(ndir
->itype
.index
.block_size
- 1)));
1312 /* Bounds checks. */
1313 if (unlikely((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_SIZE
)) {
1314 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
1315 "inode 0x%lx or driver bug.", vdir
->i_ino
);
1318 /* Catch multi sector transfer fixup errors. */
1319 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
1320 ntfs_error(sb
, "Directory index record with vcn 0x%llx is "
1321 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
1322 (unsigned long long)ia_pos
>>
1323 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1326 if (unlikely(sle64_to_cpu(ia
->index_block_vcn
) != (ia_pos
&
1327 ~(s64
)(ndir
->itype
.index
.block_size
- 1)) >>
1328 ndir
->itype
.index
.vcn_size_bits
)) {
1329 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
1330 "different from expected VCN (0x%llx). "
1331 "Directory inode 0x%lx is corrupt or driver "
1332 "bug. ", (unsigned long long)
1333 sle64_to_cpu(ia
->index_block_vcn
),
1334 (unsigned long long)ia_pos
>>
1335 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1338 if (unlikely(le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
1339 ndir
->itype
.index
.block_size
)) {
1340 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
1341 "0x%lx has a size (%u) differing from the "
1342 "directory specified size (%u). Directory "
1343 "inode is corrupt or driver bug.",
1344 (unsigned long long)ia_pos
>>
1345 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
,
1346 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
1347 ndir
->itype
.index
.block_size
);
1350 index_end
= (u8
*)ia
+ ndir
->itype
.index
.block_size
;
1351 if (unlikely(index_end
> kaddr
+ PAGE_SIZE
)) {
1352 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
1353 "0x%lx crosses page boundary. Impossible! "
1354 "Cannot access! This is probably a bug in the "
1355 "driver.", (unsigned long long)ia_pos
>>
1356 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1359 ia_start
= ia_pos
& ~(s64
)(ndir
->itype
.index
.block_size
- 1);
1360 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
1361 if (unlikely(index_end
> (u8
*)ia
+ ndir
->itype
.index
.block_size
)) {
1362 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of directory "
1363 "inode 0x%lx exceeds maximum size.",
1364 (unsigned long long)ia_pos
>>
1365 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1368 /* The first index entry in this index buffer. */
1369 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
1370 le32_to_cpu(ia
->index
.entries_offset
));
1372 * Loop until we exceed valid memory (corruption case) or until we
1373 * reach the last entry or until filldir tells us it has had enough
1374 * or signals an error (both covered by the rc test).
1376 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
1377 ntfs_debug("In index allocation, offset 0x%llx.",
1378 (unsigned long long)ia_start
+
1379 (unsigned long long)((u8
*)ie
- (u8
*)ia
));
1380 /* Bounds checks. */
1381 if (unlikely((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
1382 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
1383 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
1386 /* The last entry cannot contain a name. */
1387 if (ie
->flags
& INDEX_ENTRY_END
)
1389 /* Skip index block entry if continuing previous readdir. */
1390 if (ia_pos
- ia_start
> (u8
*)ie
- (u8
*)ia
)
1392 /* Advance the position even if going to skip the entry. */
1393 actor
->pos
= (u8
*)ie
- (u8
*)ia
+
1394 (sle64_to_cpu(ia
->index_block_vcn
) <<
1395 ndir
->itype
.index
.vcn_size_bits
) +
1396 vol
->mft_record_size
;
1398 * Submit the name to the @filldir callback. Note,
1399 * ntfs_filldir() drops the lock on @ia_page but it retakes it
1400 * before returning, unless a non-zero value is returned in
1401 * which case the page is left unlocked.
1403 rc
= ntfs_filldir(vol
, ndir
, ia_page
, ie
, name
, actor
);
1405 /* @ia_page is already unlocked in this case. */
1406 ntfs_unmap_page(ia_page
);
1407 ntfs_unmap_page(bmp_page
);
1412 goto find_next_index_buffer
;
1415 unlock_page(ia_page
);
1416 ntfs_unmap_page(ia_page
);
1418 ntfs_unmap_page(bmp_page
);
1421 /* We are finished, set fpos to EOD. */
1422 actor
->pos
= i_size
+ vol
->mft_record_size
;
1428 ntfs_unmap_page(bmp_page
);
1433 unlock_page(ia_page
);
1434 ntfs_unmap_page(ia_page
);
1439 ntfs_attr_put_search_ctx(ctx
);
1441 unmap_mft_record(ndir
);
1444 ntfs_debug("Failed. Returning error code %i.", -err
);
1449 * ntfs_dir_open - called when an inode is about to be opened
1450 * @vi: inode to be opened
1451 * @filp: file structure describing the inode
1453 * Limit directory size to the page cache limit on architectures where unsigned
1454 * long is 32-bits. This is the most we can do for now without overflowing the
1455 * page cache page index. Doing it this way means we don't run into problems
1456 * because of existing too large directories. It would be better to allow the
1457 * user to read the accessible part of the directory but I doubt very much
1458 * anyone is going to hit this check on a 32-bit architecture, so there is no
1459 * point in adding the extra complexity required to support this.
1461 * On 64-bit architectures, the check is hopefully optimized away by the
1464 static int ntfs_dir_open(struct inode
*vi
, struct file
*filp
)
1466 if (sizeof(unsigned long) < 8) {
1467 if (i_size_read(vi
) > MAX_LFS_FILESIZE
)
1476 * ntfs_dir_fsync - sync a directory to disk
1477 * @filp: directory to be synced
1478 * @dentry: dentry describing the directory to sync
1479 * @datasync: if non-zero only flush user data and not metadata
1481 * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and
1482 * msync system calls. This function is based on file.c::ntfs_file_fsync().
1484 * Write the mft record and all associated extent mft records as well as the
1485 * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
1487 * If @datasync is true, we do not wait on the inode(s) to be written out
1488 * but we always wait on the page cache pages to be written out.
1490 * Note: In the past @filp could be NULL so we ignore it as we don't need it
1493 * Locking: Caller must hold i_mutex on the inode.
1495 * TODO: We should probably also write all attribute/index inodes associated
1496 * with this inode but since we have no simple way of getting to them we ignore
1497 * this problem for now. We do write the $BITMAP attribute if it is present
1498 * which is the important one for a directory so things are not too bad.
1500 static int ntfs_dir_fsync(struct file
*filp
, loff_t start
, loff_t end
,
1503 struct inode
*bmp_vi
, *vi
= filp
->f_mapping
->host
;
1507 ntfs_debug("Entering for inode 0x%lx.", vi
->i_ino
);
1509 err
= filemap_write_and_wait_range(vi
->i_mapping
, start
, end
);
1514 BUG_ON(!S_ISDIR(vi
->i_mode
));
1515 /* If the bitmap attribute inode is in memory sync it, too. */
1516 na
.mft_no
= vi
->i_ino
;
1517 na
.type
= AT_BITMAP
;
1520 bmp_vi
= ilookup5(vi
->i_sb
, vi
->i_ino
, (test_t
)ntfs_test_inode
, &na
);
1522 write_inode_now(bmp_vi
, !datasync
);
1525 ret
= __ntfs_write_inode(vi
, 1);
1526 write_inode_now(vi
, !datasync
);
1527 err
= sync_blockdev(vi
->i_sb
->s_bdev
);
1528 if (unlikely(err
&& !ret
))
1531 ntfs_debug("Done.");
1533 ntfs_warning(vi
->i_sb
, "Failed to f%ssync inode 0x%lx. Error "
1534 "%u.", datasync
? "data" : "", vi
->i_ino
, -ret
);
1539 #endif /* NTFS_RW */
1541 const struct file_operations ntfs_dir_ops
= {
1542 .llseek
= generic_file_llseek
, /* Seek inside directory. */
1543 .read
= generic_read_dir
, /* Return -EISDIR. */
1544 .iterate
= ntfs_readdir
, /* Read directory contents. */
1546 .fsync
= ntfs_dir_fsync
, /* Sync a directory to disk. */
1547 /*.aio_fsync = ,*/ /* Sync all outstanding async
1548 i/o operations on a kiocb. */
1549 #endif /* NTFS_RW */
1550 /*.ioctl = ,*/ /* Perform function on the
1551 mounted filesystem. */
1552 .open
= ntfs_dir_open
, /* Open directory. */