1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2007 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
9 #include <linux/buffer_head.h>
10 #include <linux/slab.h>
20 * The little endian Unicode string $I30 as a global constant.
22 ntfschar I30
[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
23 cpu_to_le16('3'), cpu_to_le16('0'), 0 };
26 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
27 * @dir_ni: ntfs inode of the directory in which to search for the name
28 * @uname: Unicode name for which to search in the directory
29 * @uname_len: length of the name @uname in Unicode characters
30 * @res: return the found file name if necessary (see below)
32 * Look for an inode with name @uname in the directory with inode @dir_ni.
33 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
34 * the Unicode name. If the name is found in the directory, the corresponding
35 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
36 * is a 64-bit number containing the sequence number.
38 * On error, a negative value is returned corresponding to the error code. In
39 * particular if the inode is not found -ENOENT is returned. Note that you
40 * can't just check the return value for being negative, you have to check the
41 * inode number for being negative which you can extract using MREC(return
44 * Note, @uname_len does not include the (optional) terminating NULL character.
46 * Note, we look for a case sensitive match first but we also look for a case
47 * insensitive match at the same time. If we find a case insensitive match, we
48 * save that for the case that we don't find an exact match, where we return
49 * the case insensitive match and setup @res (which we allocate!) with the mft
50 * reference, the file name type, length and with a copy of the little endian
51 * Unicode file name itself. If we match a file name which is in the DOS name
52 * space, we only return the mft reference and file name type in @res.
53 * ntfs_lookup() then uses this to find the long file name in the inode itself.
54 * This is to avoid polluting the dcache with short file names. We want them to
55 * work but we don't care for how quickly one can access them. This also fixes
56 * the dcache aliasing issues.
58 * Locking: - Caller must hold i_mutex on the directory.
59 * - Each page cache page in the index allocation mapping must be
60 * locked whilst being accessed otherwise we may find a corrupt
61 * page due to it being under ->writepage at the moment which
62 * applies the mst protection fixups before writing out and then
63 * removes them again after the write is complete after which it
66 MFT_REF
ntfs_lookup_inode_by_name(ntfs_inode
*dir_ni
, const ntfschar
*uname
,
67 const int uname_len
, ntfs_name
**res
)
69 ntfs_volume
*vol
= dir_ni
->vol
;
70 struct super_block
*sb
= vol
->sb
;
77 ntfs_attr_search_ctx
*ctx
;
80 struct address_space
*ia_mapping
;
83 ntfs_name
*name
= NULL
;
85 BUG_ON(!S_ISDIR(VFS_I(dir_ni
)->i_mode
));
86 BUG_ON(NInoAttr(dir_ni
));
87 /* Get hold of the mft record for the directory. */
88 m
= map_mft_record(dir_ni
);
90 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
92 return ERR_MREF(PTR_ERR(m
));
94 ctx
= ntfs_attr_get_search_ctx(dir_ni
, m
);
99 /* Find the index root attribute in the mft record. */
100 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
,
103 if (err
== -ENOENT
) {
104 ntfs_error(sb
, "Index root attribute missing in "
105 "directory inode 0x%lx.",
111 /* Get to the index root value (it's been verified in read_inode). */
112 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
113 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
114 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
115 /* The first index entry. */
116 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
117 le32_to_cpu(ir
->index
.entries_offset
));
119 * Loop until we exceed valid memory (corruption case) or until we
120 * reach the last entry.
122 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
124 if ((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
125 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
126 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
130 * The last entry cannot contain a name. It can however contain
131 * a pointer to a child node in the B+tree so we just break out.
133 if (ie
->flags
& INDEX_ENTRY_END
)
136 * We perform a case sensitive comparison and if that matches
137 * we are done and return the mft reference of the inode (i.e.
138 * the inode number together with the sequence number for
139 * consistency checking). We convert it to cpu format before
142 if (ntfs_are_names_equal(uname
, uname_len
,
143 (ntfschar
*)&ie
->key
.file_name
.file_name
,
144 ie
->key
.file_name
.file_name_length
,
145 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
)) {
148 * We have a perfect match, so we don't need to care
149 * about having matched imperfectly before, so we can
150 * free name and set *res to NULL.
151 * However, if the perfect match is a short file name,
152 * we need to signal this through *res, so that
153 * ntfs_lookup() can fix dcache aliasing issues.
154 * As an optimization we just reuse an existing
155 * allocation of *res.
157 if (ie
->key
.file_name
.file_name_type
== FILE_NAME_DOS
) {
159 name
= kmalloc(sizeof(ntfs_name
),
166 name
->mref
= le64_to_cpu(
167 ie
->data
.dir
.indexed_file
);
168 name
->type
= FILE_NAME_DOS
;
175 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
176 ntfs_attr_put_search_ctx(ctx
);
177 unmap_mft_record(dir_ni
);
181 * For a case insensitive mount, we also perform a case
182 * insensitive comparison (provided the file name is not in the
183 * POSIX namespace). If the comparison matches, and the name is
184 * in the WIN32 namespace, we cache the filename in *res so
185 * that the caller, ntfs_lookup(), can work on it. If the
186 * comparison matches, and the name is in the DOS namespace, we
187 * only cache the mft reference and the file name type (we set
188 * the name length to zero for simplicity).
190 if (!NVolCaseSensitive(vol
) &&
191 ie
->key
.file_name
.file_name_type
&&
192 ntfs_are_names_equal(uname
, uname_len
,
193 (ntfschar
*)&ie
->key
.file_name
.file_name
,
194 ie
->key
.file_name
.file_name_length
,
195 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
)) {
196 int name_size
= sizeof(ntfs_name
);
197 u8 type
= ie
->key
.file_name
.file_name_type
;
198 u8 len
= ie
->key
.file_name
.file_name_length
;
200 /* Only one case insensitive matching name allowed. */
202 ntfs_error(sb
, "Found already allocated name "
203 "in phase 1. Please run chkdsk "
204 "and if that doesn't find any "
205 "errors please report you saw "
207 "linux-ntfs-dev@lists."
212 if (type
!= FILE_NAME_DOS
)
213 name_size
+= len
* sizeof(ntfschar
);
214 name
= kmalloc(name_size
, GFP_NOFS
);
219 name
->mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
221 if (type
!= FILE_NAME_DOS
) {
223 memcpy(name
->name
, ie
->key
.file_name
.file_name
,
224 len
* sizeof(ntfschar
));
230 * Not a perfect match, need to do full blown collation so we
231 * know which way in the B+tree we have to go.
233 rc
= ntfs_collate_names(uname
, uname_len
,
234 (ntfschar
*)&ie
->key
.file_name
.file_name
,
235 ie
->key
.file_name
.file_name_length
, 1,
236 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
238 * If uname collates before the name of the current entry, there
239 * is definitely no such name in this index but we might need to
240 * descend into the B+tree so we just break out of the loop.
244 /* The names are not equal, continue the search. */
248 * Names match with case insensitive comparison, now try the
249 * case sensitive comparison, which is required for proper
252 rc
= ntfs_collate_names(uname
, uname_len
,
253 (ntfschar
*)&ie
->key
.file_name
.file_name
,
254 ie
->key
.file_name
.file_name_length
, 1,
255 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
261 * Perfect match, this will never happen as the
262 * ntfs_are_names_equal() call will have gotten a match but we
263 * still treat it correctly.
268 * We have finished with this index without success. Check for the
269 * presence of a child node and if not present return -ENOENT, unless
270 * we have got a matching name cached in name in which case return the
271 * mft reference associated with it.
273 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
275 ntfs_attr_put_search_ctx(ctx
);
276 unmap_mft_record(dir_ni
);
279 ntfs_debug("Entry not found.");
282 } /* Child node present, descend into it. */
283 /* Consistency check: Verify that an index allocation exists. */
284 if (!NInoIndexAllocPresent(dir_ni
)) {
285 ntfs_error(sb
, "No index allocation attribute but index entry "
286 "requires one. Directory inode 0x%lx is "
287 "corrupt or driver bug.", dir_ni
->mft_no
);
290 /* Get the starting vcn of the index_block holding the child node. */
291 vcn
= sle64_to_cpup((sle64
*)((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8));
292 ia_mapping
= VFS_I(dir_ni
)->i_mapping
;
294 * We are done with the index root and the mft record. Release them,
295 * otherwise we deadlock with ntfs_map_page().
297 ntfs_attr_put_search_ctx(ctx
);
298 unmap_mft_record(dir_ni
);
301 descend_into_child_node
:
303 * Convert vcn to index into the index allocation attribute in units
304 * of PAGE_SIZE and map the page cache page, reading it from
307 page
= ntfs_map_page(ia_mapping
, vcn
<<
308 dir_ni
->itype
.index
.vcn_size_bits
>> PAGE_SHIFT
);
310 ntfs_error(sb
, "Failed to map directory index page, error %ld.",
316 kaddr
= (u8
*)page_address(page
);
317 fast_descend_into_child_node
:
318 /* Get to the index allocation block. */
319 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
320 dir_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_MASK
));
322 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_SIZE
) {
323 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
324 "inode 0x%lx or driver bug.", dir_ni
->mft_no
);
327 /* Catch multi sector transfer fixup errors. */
328 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
329 ntfs_error(sb
, "Directory index record with vcn 0x%llx is "
330 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
331 (unsigned long long)vcn
, dir_ni
->mft_no
);
334 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
335 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
336 "different from expected VCN (0x%llx). "
337 "Directory inode 0x%lx is corrupt or driver "
338 "bug.", (unsigned long long)
339 sle64_to_cpu(ia
->index_block_vcn
),
340 (unsigned long long)vcn
, dir_ni
->mft_no
);
343 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
344 dir_ni
->itype
.index
.block_size
) {
345 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
346 "0x%lx has a size (%u) differing from the "
347 "directory specified size (%u). Directory "
348 "inode is corrupt or driver bug.",
349 (unsigned long long)vcn
, dir_ni
->mft_no
,
350 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
351 dir_ni
->itype
.index
.block_size
);
354 index_end
= (u8
*)ia
+ dir_ni
->itype
.index
.block_size
;
355 if (index_end
> kaddr
+ PAGE_SIZE
) {
356 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
357 "0x%lx crosses page boundary. Impossible! "
358 "Cannot access! This is probably a bug in the "
359 "driver.", (unsigned long long)vcn
,
363 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
364 if (index_end
> (u8
*)ia
+ dir_ni
->itype
.index
.block_size
) {
365 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of directory "
366 "inode 0x%lx exceeds maximum size.",
367 (unsigned long long)vcn
, dir_ni
->mft_no
);
370 /* The first index entry. */
371 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
372 le32_to_cpu(ia
->index
.entries_offset
));
374 * Iterate similar to above big loop but applied to index buffer, thus
375 * loop until we exceed valid memory (corruption case) or until we
376 * reach the last entry.
378 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
380 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
381 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
382 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
384 ntfs_error(sb
, "Index entry out of bounds in "
385 "directory inode 0x%lx.",
390 * The last entry cannot contain a name. It can however contain
391 * a pointer to a child node in the B+tree so we just break out.
393 if (ie
->flags
& INDEX_ENTRY_END
)
396 * We perform a case sensitive comparison and if that matches
397 * we are done and return the mft reference of the inode (i.e.
398 * the inode number together with the sequence number for
399 * consistency checking). We convert it to cpu format before
402 if (ntfs_are_names_equal(uname
, uname_len
,
403 (ntfschar
*)&ie
->key
.file_name
.file_name
,
404 ie
->key
.file_name
.file_name_length
,
405 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
)) {
408 * We have a perfect match, so we don't need to care
409 * about having matched imperfectly before, so we can
410 * free name and set *res to NULL.
411 * However, if the perfect match is a short file name,
412 * we need to signal this through *res, so that
413 * ntfs_lookup() can fix dcache aliasing issues.
414 * As an optimization we just reuse an existing
415 * allocation of *res.
417 if (ie
->key
.file_name
.file_name_type
== FILE_NAME_DOS
) {
419 name
= kmalloc(sizeof(ntfs_name
),
426 name
->mref
= le64_to_cpu(
427 ie
->data
.dir
.indexed_file
);
428 name
->type
= FILE_NAME_DOS
;
435 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
437 ntfs_unmap_page(page
);
441 * For a case insensitive mount, we also perform a case
442 * insensitive comparison (provided the file name is not in the
443 * POSIX namespace). If the comparison matches, and the name is
444 * in the WIN32 namespace, we cache the filename in *res so
445 * that the caller, ntfs_lookup(), can work on it. If the
446 * comparison matches, and the name is in the DOS namespace, we
447 * only cache the mft reference and the file name type (we set
448 * the name length to zero for simplicity).
450 if (!NVolCaseSensitive(vol
) &&
451 ie
->key
.file_name
.file_name_type
&&
452 ntfs_are_names_equal(uname
, uname_len
,
453 (ntfschar
*)&ie
->key
.file_name
.file_name
,
454 ie
->key
.file_name
.file_name_length
,
455 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
)) {
456 int name_size
= sizeof(ntfs_name
);
457 u8 type
= ie
->key
.file_name
.file_name_type
;
458 u8 len
= ie
->key
.file_name
.file_name_length
;
460 /* Only one case insensitive matching name allowed. */
462 ntfs_error(sb
, "Found already allocated name "
463 "in phase 2. Please run chkdsk "
464 "and if that doesn't find any "
465 "errors please report you saw "
467 "linux-ntfs-dev@lists."
470 ntfs_unmap_page(page
);
474 if (type
!= FILE_NAME_DOS
)
475 name_size
+= len
* sizeof(ntfschar
);
476 name
= kmalloc(name_size
, GFP_NOFS
);
481 name
->mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
483 if (type
!= FILE_NAME_DOS
) {
485 memcpy(name
->name
, ie
->key
.file_name
.file_name
,
486 len
* sizeof(ntfschar
));
492 * Not a perfect match, need to do full blown collation so we
493 * know which way in the B+tree we have to go.
495 rc
= ntfs_collate_names(uname
, uname_len
,
496 (ntfschar
*)&ie
->key
.file_name
.file_name
,
497 ie
->key
.file_name
.file_name_length
, 1,
498 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
500 * If uname collates before the name of the current entry, there
501 * is definitely no such name in this index but we might need to
502 * descend into the B+tree so we just break out of the loop.
506 /* The names are not equal, continue the search. */
510 * Names match with case insensitive comparison, now try the
511 * case sensitive comparison, which is required for proper
514 rc
= ntfs_collate_names(uname
, uname_len
,
515 (ntfschar
*)&ie
->key
.file_name
.file_name
,
516 ie
->key
.file_name
.file_name_length
, 1,
517 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
523 * Perfect match, this will never happen as the
524 * ntfs_are_names_equal() call will have gotten a match but we
525 * still treat it correctly.
530 * We have finished with this index buffer without success. Check for
531 * the presence of a child node.
533 if (ie
->flags
& INDEX_ENTRY_NODE
) {
534 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
535 ntfs_error(sb
, "Index entry with child node found in "
536 "a leaf node in directory inode 0x%lx.",
540 /* Child node present, descend into it. */
542 vcn
= sle64_to_cpup((sle64
*)((u8
*)ie
+
543 le16_to_cpu(ie
->length
) - 8));
545 /* If vcn is in the same page cache page as old_vcn we
546 * recycle the mapped page. */
547 if (old_vcn
<< vol
->cluster_size_bits
>>
549 vol
->cluster_size_bits
>>
551 goto fast_descend_into_child_node
;
553 ntfs_unmap_page(page
);
554 goto descend_into_child_node
;
556 ntfs_error(sb
, "Negative child node vcn in directory inode "
557 "0x%lx.", dir_ni
->mft_no
);
561 * No child node present, return -ENOENT, unless we have got a matching
562 * name cached in name in which case return the mft reference
563 * associated with it.
567 ntfs_unmap_page(page
);
570 ntfs_debug("Entry not found.");
574 ntfs_unmap_page(page
);
579 ntfs_attr_put_search_ctx(ctx
);
581 unmap_mft_record(dir_ni
);
586 return ERR_MREF(err
);
588 ntfs_error(sb
, "Corrupt directory. Aborting lookup.");
595 // The algorithm embedded in this code will be required for the time when we
596 // want to support adding of entries to directories, where we require correct
597 // collation of file names in order not to cause corruption of the filesystem.
600 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
601 * @dir_ni: ntfs inode of the directory in which to search for the name
602 * @uname: Unicode name for which to search in the directory
603 * @uname_len: length of the name @uname in Unicode characters
605 * Look for an inode with name @uname in the directory with inode @dir_ni.
606 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
607 * the Unicode name. If the name is found in the directory, the corresponding
608 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
609 * is a 64-bit number containing the sequence number.
611 * On error, a negative value is returned corresponding to the error code. In
612 * particular if the inode is not found -ENOENT is returned. Note that you
613 * can't just check the return value for being negative, you have to check the
614 * inode number for being negative which you can extract using MREC(return
617 * Note, @uname_len does not include the (optional) terminating NULL character.
619 u64
ntfs_lookup_inode_by_name(ntfs_inode
*dir_ni
, const ntfschar
*uname
,
622 ntfs_volume
*vol
= dir_ni
->vol
;
623 struct super_block
*sb
= vol
->sb
;
627 INDEX_ALLOCATION
*ia
;
630 ntfs_attr_search_ctx
*ctx
;
634 struct address_space
*ia_mapping
;
638 /* Get hold of the mft record for the directory. */
639 m
= map_mft_record(dir_ni
);
641 ntfs_error(sb
, "map_mft_record() failed with error code %ld.",
643 return ERR_MREF(PTR_ERR(m
));
645 ctx
= ntfs_attr_get_search_ctx(dir_ni
, m
);
650 /* Find the index root attribute in the mft record. */
651 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
,
654 if (err
== -ENOENT
) {
655 ntfs_error(sb
, "Index root attribute missing in "
656 "directory inode 0x%lx.",
662 /* Get to the index root value (it's been verified in read_inode). */
663 ir
= (INDEX_ROOT
*)((u8
*)ctx
->attr
+
664 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
));
665 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
666 /* The first index entry. */
667 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
668 le32_to_cpu(ir
->index
.entries_offset
));
670 * Loop until we exceed valid memory (corruption case) or until we
671 * reach the last entry.
673 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
675 if ((u8
*)ie
< (u8
*)ctx
->mrec
|| (u8
*)ie
+
676 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
677 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
681 * The last entry cannot contain a name. It can however contain
682 * a pointer to a child node in the B+tree so we just break out.
684 if (ie
->flags
& INDEX_ENTRY_END
)
687 * If the current entry has a name type of POSIX, the name is
688 * case sensitive and not otherwise. This has the effect of us
689 * not being able to access any POSIX file names which collate
690 * after the non-POSIX one when they only differ in case, but
691 * anyone doing screwy stuff like that deserves to burn in
692 * hell... Doing that kind of stuff on NT4 actually causes
693 * corruption on the partition even when using SP6a and Linux
694 * is not involved at all.
696 ic
= ie
->key
.file_name
.file_name_type
? IGNORE_CASE
:
699 * If the names match perfectly, we are done and return the
700 * mft reference of the inode (i.e. the inode number together
701 * with the sequence number for consistency checking. We
702 * convert it to cpu format before returning.
704 if (ntfs_are_names_equal(uname
, uname_len
,
705 (ntfschar
*)&ie
->key
.file_name
.file_name
,
706 ie
->key
.file_name
.file_name_length
, ic
,
707 vol
->upcase
, vol
->upcase_len
)) {
709 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
710 ntfs_attr_put_search_ctx(ctx
);
711 unmap_mft_record(dir_ni
);
715 * Not a perfect match, need to do full blown collation so we
716 * know which way in the B+tree we have to go.
718 rc
= ntfs_collate_names(uname
, uname_len
,
719 (ntfschar
*)&ie
->key
.file_name
.file_name
,
720 ie
->key
.file_name
.file_name_length
, 1,
721 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
723 * If uname collates before the name of the current entry, there
724 * is definitely no such name in this index but we might need to
725 * descend into the B+tree so we just break out of the loop.
729 /* The names are not equal, continue the search. */
733 * Names match with case insensitive comparison, now try the
734 * case sensitive comparison, which is required for proper
737 rc
= ntfs_collate_names(uname
, uname_len
,
738 (ntfschar
*)&ie
->key
.file_name
.file_name
,
739 ie
->key
.file_name
.file_name_length
, 1,
740 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
746 * Perfect match, this will never happen as the
747 * ntfs_are_names_equal() call will have gotten a match but we
748 * still treat it correctly.
753 * We have finished with this index without success. Check for the
754 * presence of a child node.
756 if (!(ie
->flags
& INDEX_ENTRY_NODE
)) {
757 /* No child node, return -ENOENT. */
760 } /* Child node present, descend into it. */
761 /* Consistency check: Verify that an index allocation exists. */
762 if (!NInoIndexAllocPresent(dir_ni
)) {
763 ntfs_error(sb
, "No index allocation attribute but index entry "
764 "requires one. Directory inode 0x%lx is "
765 "corrupt or driver bug.", dir_ni
->mft_no
);
768 /* Get the starting vcn of the index_block holding the child node. */
769 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
770 ia_mapping
= VFS_I(dir_ni
)->i_mapping
;
772 * We are done with the index root and the mft record. Release them,
773 * otherwise we deadlock with ntfs_map_page().
775 ntfs_attr_put_search_ctx(ctx
);
776 unmap_mft_record(dir_ni
);
779 descend_into_child_node
:
781 * Convert vcn to index into the index allocation attribute in units
782 * of PAGE_SIZE and map the page cache page, reading it from
785 page
= ntfs_map_page(ia_mapping
, vcn
<<
786 dir_ni
->itype
.index
.vcn_size_bits
>> PAGE_SHIFT
);
788 ntfs_error(sb
, "Failed to map directory index page, error %ld.",
794 kaddr
= (u8
*)page_address(page
);
795 fast_descend_into_child_node
:
796 /* Get to the index allocation block. */
797 ia
= (INDEX_ALLOCATION
*)(kaddr
+ ((vcn
<<
798 dir_ni
->itype
.index
.vcn_size_bits
) & ~PAGE_MASK
));
800 if ((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_SIZE
) {
801 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
802 "inode 0x%lx or driver bug.", dir_ni
->mft_no
);
805 /* Catch multi sector transfer fixup errors. */
806 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
807 ntfs_error(sb
, "Directory index record with vcn 0x%llx is "
808 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
809 (unsigned long long)vcn
, dir_ni
->mft_no
);
812 if (sle64_to_cpu(ia
->index_block_vcn
) != vcn
) {
813 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
814 "different from expected VCN (0x%llx). "
815 "Directory inode 0x%lx is corrupt or driver "
816 "bug.", (unsigned long long)
817 sle64_to_cpu(ia
->index_block_vcn
),
818 (unsigned long long)vcn
, dir_ni
->mft_no
);
821 if (le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
822 dir_ni
->itype
.index
.block_size
) {
823 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
824 "0x%lx has a size (%u) differing from the "
825 "directory specified size (%u). Directory "
826 "inode is corrupt or driver bug.",
827 (unsigned long long)vcn
, dir_ni
->mft_no
,
828 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
829 dir_ni
->itype
.index
.block_size
);
832 index_end
= (u8
*)ia
+ dir_ni
->itype
.index
.block_size
;
833 if (index_end
> kaddr
+ PAGE_SIZE
) {
834 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
835 "0x%lx crosses page boundary. Impossible! "
836 "Cannot access! This is probably a bug in the "
837 "driver.", (unsigned long long)vcn
,
841 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
842 if (index_end
> (u8
*)ia
+ dir_ni
->itype
.index
.block_size
) {
843 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of directory "
844 "inode 0x%lx exceeds maximum size.",
845 (unsigned long long)vcn
, dir_ni
->mft_no
);
848 /* The first index entry. */
849 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
850 le32_to_cpu(ia
->index
.entries_offset
));
852 * Iterate similar to above big loop but applied to index buffer, thus
853 * loop until we exceed valid memory (corruption case) or until we
854 * reach the last entry.
856 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
858 if ((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
859 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
860 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
862 ntfs_error(sb
, "Index entry out of bounds in "
863 "directory inode 0x%lx.",
868 * The last entry cannot contain a name. It can however contain
869 * a pointer to a child node in the B+tree so we just break out.
871 if (ie
->flags
& INDEX_ENTRY_END
)
874 * If the current entry has a name type of POSIX, the name is
875 * case sensitive and not otherwise. This has the effect of us
876 * not being able to access any POSIX file names which collate
877 * after the non-POSIX one when they only differ in case, but
878 * anyone doing screwy stuff like that deserves to burn in
879 * hell... Doing that kind of stuff on NT4 actually causes
880 * corruption on the partition even when using SP6a and Linux
881 * is not involved at all.
883 ic
= ie
->key
.file_name
.file_name_type
? IGNORE_CASE
:
886 * If the names match perfectly, we are done and return the
887 * mft reference of the inode (i.e. the inode number together
888 * with the sequence number for consistency checking. We
889 * convert it to cpu format before returning.
891 if (ntfs_are_names_equal(uname
, uname_len
,
892 (ntfschar
*)&ie
->key
.file_name
.file_name
,
893 ie
->key
.file_name
.file_name_length
, ic
,
894 vol
->upcase
, vol
->upcase_len
)) {
896 mref
= le64_to_cpu(ie
->data
.dir
.indexed_file
);
898 ntfs_unmap_page(page
);
902 * Not a perfect match, need to do full blown collation so we
903 * know which way in the B+tree we have to go.
905 rc
= ntfs_collate_names(uname
, uname_len
,
906 (ntfschar
*)&ie
->key
.file_name
.file_name
,
907 ie
->key
.file_name
.file_name_length
, 1,
908 IGNORE_CASE
, vol
->upcase
, vol
->upcase_len
);
910 * If uname collates before the name of the current entry, there
911 * is definitely no such name in this index but we might need to
912 * descend into the B+tree so we just break out of the loop.
916 /* The names are not equal, continue the search. */
920 * Names match with case insensitive comparison, now try the
921 * case sensitive comparison, which is required for proper
924 rc
= ntfs_collate_names(uname
, uname_len
,
925 (ntfschar
*)&ie
->key
.file_name
.file_name
,
926 ie
->key
.file_name
.file_name_length
, 1,
927 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
933 * Perfect match, this will never happen as the
934 * ntfs_are_names_equal() call will have gotten a match but we
935 * still treat it correctly.
940 * We have finished with this index buffer without success. Check for
941 * the presence of a child node.
943 if (ie
->flags
& INDEX_ENTRY_NODE
) {
944 if ((ia
->index
.flags
& NODE_MASK
) == LEAF_NODE
) {
945 ntfs_error(sb
, "Index entry with child node found in "
946 "a leaf node in directory inode 0x%lx.",
950 /* Child node present, descend into it. */
952 vcn
= sle64_to_cpup((u8
*)ie
+ le16_to_cpu(ie
->length
) - 8);
954 /* If vcn is in the same page cache page as old_vcn we
955 * recycle the mapped page. */
956 if (old_vcn
<< vol
->cluster_size_bits
>>
958 vol
->cluster_size_bits
>>
960 goto fast_descend_into_child_node
;
962 ntfs_unmap_page(page
);
963 goto descend_into_child_node
;
965 ntfs_error(sb
, "Negative child node vcn in directory inode "
966 "0x%lx.", dir_ni
->mft_no
);
969 /* No child node, return -ENOENT. */
970 ntfs_debug("Entry not found.");
974 ntfs_unmap_page(page
);
979 ntfs_attr_put_search_ctx(ctx
);
981 unmap_mft_record(dir_ni
);
982 return ERR_MREF(err
);
984 ntfs_error(sb
, "Corrupt directory. Aborting lookup.");
991 * ntfs_filldir - ntfs specific filldir method
992 * @vol: current ntfs volume
993 * @ndir: ntfs inode of current directory
994 * @ia_page: page in which the index allocation buffer @ie is in resides
995 * @ie: current index entry
996 * @name: buffer to use for the converted name
997 * @actor: what to feed the entries to
999 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
1002 * If @ia_page is not NULL it is the locked page containing the index
1003 * allocation block containing the index entry @ie.
1005 * Note, we drop (and then reacquire) the page lock on @ia_page across the
1006 * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
1007 * since ntfs_lookup() will lock the same page. As an optimization, we do not
1008 * retake the lock if we are returning a non-zero value as ntfs_readdir()
1009 * would need to drop the lock immediately anyway.
1011 static inline int ntfs_filldir(ntfs_volume
*vol
,
1012 ntfs_inode
*ndir
, struct page
*ia_page
, INDEX_ENTRY
*ie
,
1013 u8
*name
, struct dir_context
*actor
)
1018 FILE_NAME_TYPE_FLAGS name_type
;
1020 name_type
= ie
->key
.file_name
.file_name_type
;
1021 if (name_type
== FILE_NAME_DOS
) {
1022 ntfs_debug("Skipping DOS name space entry.");
1025 if (MREF_LE(ie
->data
.dir
.indexed_file
) == FILE_root
) {
1026 ntfs_debug("Skipping root directory self reference entry.");
1029 if (MREF_LE(ie
->data
.dir
.indexed_file
) < FILE_first_user
&&
1030 !NVolShowSystemFiles(vol
)) {
1031 ntfs_debug("Skipping system file.");
1034 name_len
= ntfs_ucstonls(vol
, (ntfschar
*)&ie
->key
.file_name
.file_name
,
1035 ie
->key
.file_name
.file_name_length
, &name
,
1036 NTFS_MAX_NAME_LEN
* NLS_MAX_CHARSET_SIZE
+ 1);
1037 if (name_len
<= 0) {
1038 ntfs_warning(vol
->sb
, "Skipping unrepresentable inode 0x%llx.",
1039 (long long)MREF_LE(ie
->data
.dir
.indexed_file
));
1042 if (ie
->key
.file_name
.file_attributes
&
1043 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT
)
1047 mref
= MREF_LE(ie
->data
.dir
.indexed_file
);
1049 * Drop the page lock otherwise we deadlock with NFS when it calls
1050 * ->lookup since ntfs_lookup() will lock the same page.
1053 unlock_page(ia_page
);
1054 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
1055 "0x%lx, DT_%s.", name
, name_len
, actor
->pos
, mref
,
1056 dt_type
== DT_DIR
? "DIR" : "REG");
1057 if (!dir_emit(actor
, name
, name_len
, mref
, dt_type
))
1059 /* Relock the page but not if we are aborting ->readdir. */
1066 * We use the same basic approach as the old NTFS driver, i.e. we parse the
1067 * index root entries and then the index allocation entries that are marked
1068 * as in use in the index bitmap.
1070 * While this will return the names in random order this doesn't matter for
1071 * ->readdir but OTOH results in a faster ->readdir.
1073 * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
1074 * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1077 * Locking: - Caller must hold i_mutex on the directory.
1078 * - Each page cache page in the index allocation mapping must be
1079 * locked whilst being accessed otherwise we may find a corrupt
1080 * page due to it being under ->writepage at the moment which
1081 * applies the mst protection fixups before writing out and then
1082 * removes them again after the write is complete after which it
1085 static int ntfs_readdir(struct file
*file
, struct dir_context
*actor
)
1087 s64 ia_pos
, ia_start
, prev_ia_pos
, bmp_pos
;
1089 struct inode
*bmp_vi
, *vdir
= file_inode(file
);
1090 struct super_block
*sb
= vdir
->i_sb
;
1091 ntfs_inode
*ndir
= NTFS_I(vdir
);
1092 ntfs_volume
*vol
= NTFS_SB(sb
);
1094 INDEX_ROOT
*ir
= NULL
;
1096 INDEX_ALLOCATION
*ia
;
1098 int rc
, err
, ir_pos
, cur_bmp_pos
;
1099 struct address_space
*ia_mapping
, *bmp_mapping
;
1100 struct page
*bmp_page
= NULL
, *ia_page
= NULL
;
1101 u8
*kaddr
, *bmp
, *index_end
;
1102 ntfs_attr_search_ctx
*ctx
;
1104 ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
1105 vdir
->i_ino
, actor
->pos
);
1107 /* Are we at end of dir yet? */
1108 i_size
= i_size_read(vdir
);
1109 if (actor
->pos
>= i_size
+ vol
->mft_record_size
)
1111 /* Emulate . and .. for all directories. */
1112 if (!dir_emit_dots(file
, actor
))
1117 * Allocate a buffer to store the current name being processed
1118 * converted to format determined by current NLS.
1120 name
= kmalloc(NTFS_MAX_NAME_LEN
* NLS_MAX_CHARSET_SIZE
+ 1, GFP_NOFS
);
1121 if (unlikely(!name
)) {
1125 /* Are we jumping straight into the index allocation attribute? */
1126 if (actor
->pos
>= vol
->mft_record_size
)
1127 goto skip_index_root
;
1128 /* Get hold of the mft record for the directory. */
1129 m
= map_mft_record(ndir
);
1135 ctx
= ntfs_attr_get_search_ctx(ndir
, m
);
1136 if (unlikely(!ctx
)) {
1140 /* Get the offset into the index root attribute. */
1141 ir_pos
= (s64
)actor
->pos
;
1142 /* Find the index root attribute in the mft record. */
1143 err
= ntfs_attr_lookup(AT_INDEX_ROOT
, I30
, 4, CASE_SENSITIVE
, 0, NULL
,
1145 if (unlikely(err
)) {
1146 ntfs_error(sb
, "Index root attribute missing in directory "
1147 "inode 0x%lx.", vdir
->i_ino
);
1151 * Copy the index root attribute value to a buffer so that we can put
1152 * the search context and unmap the mft record before calling the
1153 * filldir() callback. We need to do this because of NFSd which calls
1154 * ->lookup() from its filldir callback() and this causes NTFS to
1155 * deadlock as ntfs_lookup() maps the mft record of the directory and
1156 * we have got it mapped here already. The only solution is for us to
1157 * unmap the mft record here so that a call to ntfs_lookup() is able to
1158 * map the mft record without deadlocking.
1160 rc
= le32_to_cpu(ctx
->attr
->data
.resident
.value_length
);
1161 ir
= kmalloc(rc
, GFP_NOFS
);
1162 if (unlikely(!ir
)) {
1166 /* Copy the index root value (it has been verified in read_inode). */
1167 memcpy(ir
, (u8
*)ctx
->attr
+
1168 le16_to_cpu(ctx
->attr
->data
.resident
.value_offset
), rc
);
1169 ntfs_attr_put_search_ctx(ctx
);
1170 unmap_mft_record(ndir
);
1173 index_end
= (u8
*)&ir
->index
+ le32_to_cpu(ir
->index
.index_length
);
1174 /* The first index entry. */
1175 ie
= (INDEX_ENTRY
*)((u8
*)&ir
->index
+
1176 le32_to_cpu(ir
->index
.entries_offset
));
1178 * Loop until we exceed valid memory (corruption case) or until we
1179 * reach the last entry or until filldir tells us it has had enough
1180 * or signals an error (both covered by the rc test).
1182 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
1183 ntfs_debug("In index root, offset 0x%zx.", (u8
*)ie
- (u8
*)ir
);
1184 /* Bounds checks. */
1185 if (unlikely((u8
*)ie
< (u8
*)ir
|| (u8
*)ie
+
1186 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
1187 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
1190 /* The last entry cannot contain a name. */
1191 if (ie
->flags
& INDEX_ENTRY_END
)
1193 /* Skip index root entry if continuing previous readdir. */
1194 if (ir_pos
> (u8
*)ie
- (u8
*)ir
)
1196 /* Advance the position even if going to skip the entry. */
1197 actor
->pos
= (u8
*)ie
- (u8
*)ir
;
1198 /* Submit the name to the filldir callback. */
1199 rc
= ntfs_filldir(vol
, ndir
, NULL
, ie
, name
, actor
);
1205 /* We are done with the index root and can free the buffer. */
1208 /* If there is no index allocation attribute we are finished. */
1209 if (!NInoIndexAllocPresent(ndir
))
1211 /* Advance fpos to the beginning of the index allocation. */
1212 actor
->pos
= vol
->mft_record_size
;
1216 /* Get the offset into the index allocation attribute. */
1217 ia_pos
= (s64
)actor
->pos
- vol
->mft_record_size
;
1218 ia_mapping
= vdir
->i_mapping
;
1219 ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir
->i_ino
);
1220 bmp_vi
= ntfs_attr_iget(vdir
, AT_BITMAP
, I30
, 4);
1221 if (IS_ERR(bmp_vi
)) {
1222 ntfs_error(sb
, "Failed to get bitmap attribute.");
1223 err
= PTR_ERR(bmp_vi
);
1226 bmp_mapping
= bmp_vi
->i_mapping
;
1227 /* Get the starting bitmap bit position and sanity check it. */
1228 bmp_pos
= ia_pos
>> ndir
->itype
.index
.block_size_bits
;
1229 if (unlikely(bmp_pos
>> 3 >= i_size_read(bmp_vi
))) {
1230 ntfs_error(sb
, "Current index allocation position exceeds "
1231 "index bitmap size.");
1234 /* Get the starting bit position in the current bitmap page. */
1235 cur_bmp_pos
= bmp_pos
& ((PAGE_SIZE
* 8) - 1);
1236 bmp_pos
&= ~(u64
)((PAGE_SIZE
* 8) - 1);
1238 ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1239 (unsigned long long)bmp_pos
>> (3 + PAGE_SHIFT
),
1240 (unsigned long long)bmp_pos
&
1241 (unsigned long long)((PAGE_SIZE
* 8) - 1));
1242 bmp_page
= ntfs_map_page(bmp_mapping
,
1243 bmp_pos
>> (3 + PAGE_SHIFT
));
1244 if (IS_ERR(bmp_page
)) {
1245 ntfs_error(sb
, "Reading index bitmap failed.");
1246 err
= PTR_ERR(bmp_page
);
1250 bmp
= (u8
*)page_address(bmp_page
);
1251 /* Find next index block in use. */
1252 while (!(bmp
[cur_bmp_pos
>> 3] & (1 << (cur_bmp_pos
& 7)))) {
1253 find_next_index_buffer
:
1256 * If we have reached the end of the bitmap page, get the next
1257 * page, and put away the old one.
1259 if (unlikely((cur_bmp_pos
>> 3) >= PAGE_SIZE
)) {
1260 ntfs_unmap_page(bmp_page
);
1261 bmp_pos
+= PAGE_SIZE
* 8;
1263 goto get_next_bmp_page
;
1265 /* If we have reached the end of the bitmap, we are done. */
1266 if (unlikely(((bmp_pos
+ cur_bmp_pos
) >> 3) >= i_size
))
1268 ia_pos
= (bmp_pos
+ cur_bmp_pos
) <<
1269 ndir
->itype
.index
.block_size_bits
;
1271 ntfs_debug("Handling index buffer 0x%llx.",
1272 (unsigned long long)bmp_pos
+ cur_bmp_pos
);
1273 /* If the current index buffer is in the same page we reuse the page. */
1274 if ((prev_ia_pos
& (s64
)PAGE_MASK
) !=
1275 (ia_pos
& (s64
)PAGE_MASK
)) {
1276 prev_ia_pos
= ia_pos
;
1277 if (likely(ia_page
!= NULL
)) {
1278 unlock_page(ia_page
);
1279 ntfs_unmap_page(ia_page
);
1282 * Map the page cache page containing the current ia_pos,
1283 * reading it from disk if necessary.
1285 ia_page
= ntfs_map_page(ia_mapping
, ia_pos
>> PAGE_SHIFT
);
1286 if (IS_ERR(ia_page
)) {
1287 ntfs_error(sb
, "Reading index allocation data failed.");
1288 err
= PTR_ERR(ia_page
);
1293 kaddr
= (u8
*)page_address(ia_page
);
1295 /* Get the current index buffer. */
1296 ia
= (INDEX_ALLOCATION
*)(kaddr
+ (ia_pos
& ~PAGE_MASK
&
1297 ~(s64
)(ndir
->itype
.index
.block_size
- 1)));
1298 /* Bounds checks. */
1299 if (unlikely((u8
*)ia
< kaddr
|| (u8
*)ia
> kaddr
+ PAGE_SIZE
)) {
1300 ntfs_error(sb
, "Out of bounds check failed. Corrupt directory "
1301 "inode 0x%lx or driver bug.", vdir
->i_ino
);
1304 /* Catch multi sector transfer fixup errors. */
1305 if (unlikely(!ntfs_is_indx_record(ia
->magic
))) {
1306 ntfs_error(sb
, "Directory index record with vcn 0x%llx is "
1307 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
1308 (unsigned long long)ia_pos
>>
1309 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1312 if (unlikely(sle64_to_cpu(ia
->index_block_vcn
) != (ia_pos
&
1313 ~(s64
)(ndir
->itype
.index
.block_size
- 1)) >>
1314 ndir
->itype
.index
.vcn_size_bits
)) {
1315 ntfs_error(sb
, "Actual VCN (0x%llx) of index buffer is "
1316 "different from expected VCN (0x%llx). "
1317 "Directory inode 0x%lx is corrupt or driver "
1318 "bug. ", (unsigned long long)
1319 sle64_to_cpu(ia
->index_block_vcn
),
1320 (unsigned long long)ia_pos
>>
1321 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1324 if (unlikely(le32_to_cpu(ia
->index
.allocated_size
) + 0x18 !=
1325 ndir
->itype
.index
.block_size
)) {
1326 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
1327 "0x%lx has a size (%u) differing from the "
1328 "directory specified size (%u). Directory "
1329 "inode is corrupt or driver bug.",
1330 (unsigned long long)ia_pos
>>
1331 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
,
1332 le32_to_cpu(ia
->index
.allocated_size
) + 0x18,
1333 ndir
->itype
.index
.block_size
);
1336 index_end
= (u8
*)ia
+ ndir
->itype
.index
.block_size
;
1337 if (unlikely(index_end
> kaddr
+ PAGE_SIZE
)) {
1338 ntfs_error(sb
, "Index buffer (VCN 0x%llx) of directory inode "
1339 "0x%lx crosses page boundary. Impossible! "
1340 "Cannot access! This is probably a bug in the "
1341 "driver.", (unsigned long long)ia_pos
>>
1342 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1345 ia_start
= ia_pos
& ~(s64
)(ndir
->itype
.index
.block_size
- 1);
1346 index_end
= (u8
*)&ia
->index
+ le32_to_cpu(ia
->index
.index_length
);
1347 if (unlikely(index_end
> (u8
*)ia
+ ndir
->itype
.index
.block_size
)) {
1348 ntfs_error(sb
, "Size of index buffer (VCN 0x%llx) of directory "
1349 "inode 0x%lx exceeds maximum size.",
1350 (unsigned long long)ia_pos
>>
1351 ndir
->itype
.index
.vcn_size_bits
, vdir
->i_ino
);
1354 /* The first index entry in this index buffer. */
1355 ie
= (INDEX_ENTRY
*)((u8
*)&ia
->index
+
1356 le32_to_cpu(ia
->index
.entries_offset
));
1358 * Loop until we exceed valid memory (corruption case) or until we
1359 * reach the last entry or until filldir tells us it has had enough
1360 * or signals an error (both covered by the rc test).
1362 for (;; ie
= (INDEX_ENTRY
*)((u8
*)ie
+ le16_to_cpu(ie
->length
))) {
1363 ntfs_debug("In index allocation, offset 0x%llx.",
1364 (unsigned long long)ia_start
+
1365 (unsigned long long)((u8
*)ie
- (u8
*)ia
));
1366 /* Bounds checks. */
1367 if (unlikely((u8
*)ie
< (u8
*)ia
|| (u8
*)ie
+
1368 sizeof(INDEX_ENTRY_HEADER
) > index_end
||
1369 (u8
*)ie
+ le16_to_cpu(ie
->key_length
) >
1372 /* The last entry cannot contain a name. */
1373 if (ie
->flags
& INDEX_ENTRY_END
)
1375 /* Skip index block entry if continuing previous readdir. */
1376 if (ia_pos
- ia_start
> (u8
*)ie
- (u8
*)ia
)
1378 /* Advance the position even if going to skip the entry. */
1379 actor
->pos
= (u8
*)ie
- (u8
*)ia
+
1380 (sle64_to_cpu(ia
->index_block_vcn
) <<
1381 ndir
->itype
.index
.vcn_size_bits
) +
1382 vol
->mft_record_size
;
1384 * Submit the name to the @filldir callback. Note,
1385 * ntfs_filldir() drops the lock on @ia_page but it retakes it
1386 * before returning, unless a non-zero value is returned in
1387 * which case the page is left unlocked.
1389 rc
= ntfs_filldir(vol
, ndir
, ia_page
, ie
, name
, actor
);
1391 /* @ia_page is already unlocked in this case. */
1392 ntfs_unmap_page(ia_page
);
1393 ntfs_unmap_page(bmp_page
);
1398 goto find_next_index_buffer
;
1401 unlock_page(ia_page
);
1402 ntfs_unmap_page(ia_page
);
1404 ntfs_unmap_page(bmp_page
);
1407 /* We are finished, set fpos to EOD. */
1408 actor
->pos
= i_size
+ vol
->mft_record_size
;
1414 ntfs_unmap_page(bmp_page
);
1419 unlock_page(ia_page
);
1420 ntfs_unmap_page(ia_page
);
1425 ntfs_attr_put_search_ctx(ctx
);
1427 unmap_mft_record(ndir
);
1430 ntfs_debug("Failed. Returning error code %i.", -err
);
1435 * ntfs_dir_open - called when an inode is about to be opened
1436 * @vi: inode to be opened
1437 * @filp: file structure describing the inode
1439 * Limit directory size to the page cache limit on architectures where unsigned
1440 * long is 32-bits. This is the most we can do for now without overflowing the
1441 * page cache page index. Doing it this way means we don't run into problems
1442 * because of existing too large directories. It would be better to allow the
1443 * user to read the accessible part of the directory but I doubt very much
1444 * anyone is going to hit this check on a 32-bit architecture, so there is no
1445 * point in adding the extra complexity required to support this.
1447 * On 64-bit architectures, the check is hopefully optimized away by the
1450 static int ntfs_dir_open(struct inode
*vi
, struct file
*filp
)
1452 if (sizeof(unsigned long) < 8) {
1453 if (i_size_read(vi
) > MAX_LFS_FILESIZE
)
1462 * ntfs_dir_fsync - sync a directory to disk
1463 * @filp: directory to be synced
1464 * @dentry: dentry describing the directory to sync
1465 * @datasync: if non-zero only flush user data and not metadata
1467 * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and
1468 * msync system calls. This function is based on file.c::ntfs_file_fsync().
1470 * Write the mft record and all associated extent mft records as well as the
1471 * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
1473 * If @datasync is true, we do not wait on the inode(s) to be written out
1474 * but we always wait on the page cache pages to be written out.
1476 * Note: In the past @filp could be NULL so we ignore it as we don't need it
1479 * Locking: Caller must hold i_mutex on the inode.
1481 * TODO: We should probably also write all attribute/index inodes associated
1482 * with this inode but since we have no simple way of getting to them we ignore
1483 * this problem for now. We do write the $BITMAP attribute if it is present
1484 * which is the important one for a directory so things are not too bad.
1486 static int ntfs_dir_fsync(struct file
*filp
, loff_t start
, loff_t end
,
1489 struct inode
*bmp_vi
, *vi
= filp
->f_mapping
->host
;
1493 ntfs_debug("Entering for inode 0x%lx.", vi
->i_ino
);
1495 err
= file_write_and_wait_range(filp
, start
, end
);
1500 BUG_ON(!S_ISDIR(vi
->i_mode
));
1501 /* If the bitmap attribute inode is in memory sync it, too. */
1502 na
.mft_no
= vi
->i_ino
;
1503 na
.type
= AT_BITMAP
;
1506 bmp_vi
= ilookup5(vi
->i_sb
, vi
->i_ino
, (test_t
)ntfs_test_inode
, &na
);
1508 write_inode_now(bmp_vi
, !datasync
);
1511 ret
= __ntfs_write_inode(vi
, 1);
1512 write_inode_now(vi
, !datasync
);
1513 err
= sync_blockdev(vi
->i_sb
->s_bdev
);
1514 if (unlikely(err
&& !ret
))
1517 ntfs_debug("Done.");
1519 ntfs_warning(vi
->i_sb
, "Failed to f%ssync inode 0x%lx. Error "
1520 "%u.", datasync
? "data" : "", vi
->i_ino
, -ret
);
1525 #endif /* NTFS_RW */
1527 const struct file_operations ntfs_dir_ops
= {
1528 .llseek
= generic_file_llseek
, /* Seek inside directory. */
1529 .read
= generic_read_dir
, /* Return -EISDIR. */
1530 .iterate
= ntfs_readdir
, /* Read directory contents. */
1532 .fsync
= ntfs_dir_fsync
, /* Sync a directory to disk. */
1533 #endif /* NTFS_RW */
1534 /*.ioctl = ,*/ /* Perform function on the
1535 mounted filesystem. */
1536 .open
= ntfs_dir_open
, /* Open directory. */