2 * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2004 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/smp_lock.h>
28 * The little endian Unicode string $I30 as a global constant.
30 ntfschar I30[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('I'),
31 const_cpu_to_le16('3'), const_cpu_to_le16('0'),
32 const_cpu_to_le16(0) };
35 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
36 * @dir_ni: ntfs inode of the directory in which to search for the name
37 * @uname: Unicode name for which to search in the directory
38 * @uname_len: length of the name @uname in Unicode characters
39 * @res: return the found file name if necessary (see below)
41 * Look for an inode with name @uname in the directory with inode @dir_ni.
42 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
43 * the Unicode name. If the name is found in the directory, the corresponding
44 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
45 * is a 64-bit number containing the sequence number.
47 * On error, a negative value is returned corresponding to the error code. In
48 * particular if the inode is not found -ENOENT is returned. Note that you
49 * can't just check the return value for being negative, you have to check the
50 * inode number for being negative which you can extract using MREC(return
53 * Note, @uname_len does not include the (optional) terminating NULL character.
55 * Note, we look for a case sensitive match first but we also look for a case
56 * insensitive match at the same time. If we find a case insensitive match, we
57 * save that for the case that we don't find an exact match, where we return
58 * the case insensitive match and setup @res (which we allocate!) with the mft
59 * reference, the file name type, length and with a copy of the little endian
60 * Unicode file name itself. If we match a file name which is in the DOS name
61 * space, we only return the mft reference and file name type in @res.
62 * ntfs_lookup() then uses this to find the long file name in the inode itself.
63 * This is to avoid polluting the dcache with short file names. We want them to
64 * work but we don't care for how quickly one can access them. This also fixes
65 * the dcache aliasing issues.
67 * Locking: - Caller must hold i_sem on the directory.
68 * - Each page cache page in the index allocation mapping must be
69 * locked whilst being accessed otherwise we may find a corrupt
70 * page due to it being under ->writepage at the moment which
71 * applies the mst protection fixups before writing out and then
72 * removes them again after the write is complete after which it
75 MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
76 const int uname_len, ntfs_name **res)
78 ntfs_volume *vol = dir_ni->vol;
79 struct super_block *sb = vol->sb;
86 attr_search_context *ctx;
89 struct address_space *ia_mapping;
92 ntfs_name *name = NULL;
94 BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
95 BUG_ON(NInoAttr(dir_ni));
96 /* Get hold of the mft record for the directory. */
97 m = map_mft_record(dir_ni);
98 if (unlikely(IS_ERR(m))) {
99 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
101 return ERR_MREF(PTR_ERR(m));
103 ctx = get_attr_search_ctx(dir_ni, m);
104 if (unlikely(!ctx)) {
108 /* Find the index root attribute in the mft record. */
109 if (!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0,
111 ntfs_error(sb, "Index root attribute missing in directory "
112 "inode 0x%lx.", dir_ni->mft_no);
116 /* Get to the index root value (it's been verified in read_inode). */
117 ir = (INDEX_ROOT*)((u8*)ctx->attr +
118 le16_to_cpu(ctx->attr->data.resident.value_offset));
119 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
120 /* The first index entry. */
121 ie = (INDEX_ENTRY*)((u8*)&ir->index +
122 le32_to_cpu(ir->index.entries_offset));
124 * Loop until we exceed valid memory (corruption case) or until we
125 * reach the last entry.
127 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
129 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
130 sizeof(INDEX_ENTRY_HEADER) > index_end ||
131 (u8*)ie + le16_to_cpu(ie->key_length) >
135 * The last entry cannot contain a name. It can however contain
136 * a pointer to a child node in the B+tree so we just break out.
138 if (ie->flags & INDEX_ENTRY_END)
141 * We perform a case sensitive comparison and if that matches
142 * we are done and return the mft reference of the inode (i.e.
143 * the inode number together with the sequence number for
144 * consistency checking). We convert it to cpu format before
147 if (ntfs_are_names_equal(uname, uname_len,
148 (ntfschar*)&ie->key.file_name.file_name,
149 ie->key.file_name.file_name_length,
150 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
153 * We have a perfect match, so we don't need to care
154 * about having matched imperfectly before, so we can
155 * free name and set *res to NULL.
156 * However, if the perfect match is a short file name,
157 * we need to signal this through *res, so that
158 * ntfs_lookup() can fix dcache aliasing issues.
159 * As an optimization we just reuse an existing
160 * allocation of *res.
162 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
164 name = kmalloc(sizeof(ntfs_name),
171 name->mref = le64_to_cpu(
172 ie->data.dir.indexed_file);
173 name->type = FILE_NAME_DOS;
181 mref = le64_to_cpu(ie->data.dir.indexed_file);
182 put_attr_search_ctx(ctx);
183 unmap_mft_record(dir_ni);
187 * For a case insensitive mount, we also perform a case
188 * insensitive comparison (provided the file name is not in the
189 * POSIX namespace). If the comparison matches, and the name is
190 * in the WIN32 namespace, we cache the filename in *res so
191 * that the caller, ntfs_lookup(), can work on it. If the
192 * comparison matches, and the name is in the DOS namespace, we
193 * only cache the mft reference and the file name type (we set
194 * the name length to zero for simplicity).
196 if (!NVolCaseSensitive(vol) &&
197 ie->key.file_name.file_name_type &&
198 ntfs_are_names_equal(uname, uname_len,
199 (ntfschar*)&ie->key.file_name.file_name,
200 ie->key.file_name.file_name_length,
201 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
202 int name_size = sizeof(ntfs_name);
203 u8 type = ie->key.file_name.file_name_type;
204 u8 len = ie->key.file_name.file_name_length;
206 /* Only one case insensitive matching name allowed. */
208 ntfs_error(sb, "Found already allocated name "
209 "in phase 1. Please run chkdsk "
210 "and if that doesn't find any "
211 "errors please report you saw "
213 "linux-ntfs-dev@lists."
218 if (type != FILE_NAME_DOS)
219 name_size += len * sizeof(ntfschar);
220 name = kmalloc(name_size, GFP_NOFS);
225 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
227 if (type != FILE_NAME_DOS) {
229 memcpy(name->name, ie->key.file_name.file_name,
230 len * sizeof(ntfschar));
236 * Not a perfect match, need to do full blown collation so we
237 * know which way in the B+tree we have to go.
239 rc = ntfs_collate_names(uname, uname_len,
240 (ntfschar*)&ie->key.file_name.file_name,
241 ie->key.file_name.file_name_length, 1,
242 IGNORE_CASE, vol->upcase, vol->upcase_len);
244 * If uname collates before the name of the current entry, there
245 * is definitely no such name in this index but we might need to
246 * descend into the B+tree so we just break out of the loop.
250 /* The names are not equal, continue the search. */
254 * Names match with case insensitive comparison, now try the
255 * case sensitive comparison, which is required for proper
258 rc = ntfs_collate_names(uname, uname_len,
259 (ntfschar*)&ie->key.file_name.file_name,
260 ie->key.file_name.file_name_length, 1,
261 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
267 * Perfect match, this will never happen as the
268 * ntfs_are_names_equal() call will have gotten a match but we
269 * still treat it correctly.
274 * We have finished with this index without success. Check for the
275 * presence of a child node and if not present return -ENOENT, unless
276 * we have got a matching name cached in name in which case return the
277 * mft reference associated with it.
279 if (!(ie->flags & INDEX_ENTRY_NODE)) {
281 put_attr_search_ctx(ctx);
282 unmap_mft_record(dir_ni);
285 ntfs_debug("Entry not found.");
288 } /* Child node present, descend into it. */
289 /* Consistency check: Verify that an index allocation exists. */
290 if (!NInoIndexAllocPresent(dir_ni)) {
291 ntfs_error(sb, "No index allocation attribute but index entry "
292 "requires one. Directory inode 0x%lx is "
293 "corrupt or driver bug.", dir_ni->mft_no);
297 /* Get the starting vcn of the index_block holding the child node. */
298 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
299 ia_mapping = VFS_I(dir_ni)->i_mapping;
301 * We are done with the index root and the mft record. Release them,
302 * otherwise we deadlock with ntfs_map_page().
304 put_attr_search_ctx(ctx);
305 unmap_mft_record(dir_ni);
308 descend_into_child_node:
310 * Convert vcn to index into the index allocation attribute in units
311 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
314 page = ntfs_map_page(ia_mapping, vcn <<
315 dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
317 ntfs_error(sb, "Failed to map directory index page, error %ld.",
323 kaddr = (u8*)page_address(page);
324 fast_descend_into_child_node:
325 /* Get to the index allocation block. */
326 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
327 dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
329 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
330 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
331 "inode 0x%lx or driver bug.", dir_ni->mft_no);
335 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
336 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
337 "different from expected VCN (0x%llx). "
338 "Directory inode 0x%lx is corrupt or driver "
339 "bug.", (unsigned long long)
340 sle64_to_cpu(ia->index_block_vcn),
341 (unsigned long long)vcn, dir_ni->mft_no);
345 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
346 dir_ni->itype.index.block_size) {
347 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
348 "0x%lx has a size (%u) differing from the "
349 "directory specified size (%u). Directory "
350 "inode is corrupt or driver bug.",
351 (unsigned long long)vcn, dir_ni->mft_no,
352 le32_to_cpu(ia->index.allocated_size) + 0x18,
353 dir_ni->itype.index.block_size);
357 index_end = (u8*)ia + dir_ni->itype.index.block_size;
358 if (index_end > kaddr + PAGE_CACHE_SIZE) {
359 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
360 "0x%lx crosses page boundary. Impossible! "
361 "Cannot access! This is probably a bug in the "
362 "driver.", (unsigned long long)vcn,
367 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
368 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
369 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
370 "inode 0x%lx exceeds maximum size.",
371 (unsigned long long)vcn, dir_ni->mft_no);
375 /* The first index entry. */
376 ie = (INDEX_ENTRY*)((u8*)&ia->index +
377 le32_to_cpu(ia->index.entries_offset));
379 * Iterate similar to above big loop but applied to index buffer, thus
380 * loop until we exceed valid memory (corruption case) or until we
381 * reach the last entry.
383 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
385 if ((u8*)ie < (u8*)ia || (u8*)ie +
386 sizeof(INDEX_ENTRY_HEADER) > index_end ||
387 (u8*)ie + le16_to_cpu(ie->key_length) >
389 ntfs_error(sb, "Index entry out of bounds in "
390 "directory inode 0x%lx.",
396 * The last entry cannot contain a name. It can however contain
397 * a pointer to a child node in the B+tree so we just break out.
399 if (ie->flags & INDEX_ENTRY_END)
402 * We perform a case sensitive comparison and if that matches
403 * we are done and return the mft reference of the inode (i.e.
404 * the inode number together with the sequence number for
405 * consistency checking). We convert it to cpu format before
408 if (ntfs_are_names_equal(uname, uname_len,
409 (ntfschar*)&ie->key.file_name.file_name,
410 ie->key.file_name.file_name_length,
411 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
414 * We have a perfect match, so we don't need to care
415 * about having matched imperfectly before, so we can
416 * free name and set *res to NULL.
417 * However, if the perfect match is a short file name,
418 * we need to signal this through *res, so that
419 * ntfs_lookup() can fix dcache aliasing issues.
420 * As an optimization we just reuse an existing
421 * allocation of *res.
423 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
425 name = kmalloc(sizeof(ntfs_name),
432 name->mref = le64_to_cpu(
433 ie->data.dir.indexed_file);
434 name->type = FILE_NAME_DOS;
442 mref = le64_to_cpu(ie->data.dir.indexed_file);
444 ntfs_unmap_page(page);
448 * For a case insensitive mount, we also perform a case
449 * insensitive comparison (provided the file name is not in the
450 * POSIX namespace). If the comparison matches, and the name is
451 * in the WIN32 namespace, we cache the filename in *res so
452 * that the caller, ntfs_lookup(), can work on it. If the
453 * comparison matches, and the name is in the DOS namespace, we
454 * only cache the mft reference and the file name type (we set
455 * the name length to zero for simplicity).
457 if (!NVolCaseSensitive(vol) &&
458 ie->key.file_name.file_name_type &&
459 ntfs_are_names_equal(uname, uname_len,
460 (ntfschar*)&ie->key.file_name.file_name,
461 ie->key.file_name.file_name_length,
462 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
463 int name_size = sizeof(ntfs_name);
464 u8 type = ie->key.file_name.file_name_type;
465 u8 len = ie->key.file_name.file_name_length;
467 /* Only one case insensitive matching name allowed. */
469 ntfs_error(sb, "Found already allocated name "
470 "in phase 2. Please run chkdsk "
471 "and if that doesn't find any "
472 "errors please report you saw "
474 "linux-ntfs-dev@lists."
477 ntfs_unmap_page(page);
481 if (type != FILE_NAME_DOS)
482 name_size += len * sizeof(ntfschar);
483 name = kmalloc(name_size, GFP_NOFS);
488 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
490 if (type != FILE_NAME_DOS) {
492 memcpy(name->name, ie->key.file_name.file_name,
493 len * sizeof(ntfschar));
499 * Not a perfect match, need to do full blown collation so we
500 * know which way in the B+tree we have to go.
502 rc = ntfs_collate_names(uname, uname_len,
503 (ntfschar*)&ie->key.file_name.file_name,
504 ie->key.file_name.file_name_length, 1,
505 IGNORE_CASE, vol->upcase, vol->upcase_len);
507 * If uname collates before the name of the current entry, there
508 * is definitely no such name in this index but we might need to
509 * descend into the B+tree so we just break out of the loop.
513 /* The names are not equal, continue the search. */
517 * Names match with case insensitive comparison, now try the
518 * case sensitive comparison, which is required for proper
521 rc = ntfs_collate_names(uname, uname_len,
522 (ntfschar*)&ie->key.file_name.file_name,
523 ie->key.file_name.file_name_length, 1,
524 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
530 * Perfect match, this will never happen as the
531 * ntfs_are_names_equal() call will have gotten a match but we
532 * still treat it correctly.
537 * We have finished with this index buffer without success. Check for
538 * the presence of a child node.
540 if (ie->flags & INDEX_ENTRY_NODE) {
541 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
542 ntfs_error(sb, "Index entry with child node found in "
543 "a leaf node in directory inode 0x%lx.",
548 /* Child node present, descend into it. */
550 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
552 /* If vcn is in the same page cache page as old_vcn we
553 * recycle the mapped page. */
554 if (old_vcn << vol->cluster_size_bits >>
555 PAGE_CACHE_SHIFT == vcn <<
556 vol->cluster_size_bits >>
558 goto fast_descend_into_child_node;
560 ntfs_unmap_page(page);
561 goto descend_into_child_node;
563 ntfs_error(sb, "Negative child node vcn in directory inode "
564 "0x%lx.", dir_ni->mft_no);
569 * No child node present, return -ENOENT, unless we have got a matching
570 * name cached in name in which case return the mft reference
571 * associated with it.
575 ntfs_unmap_page(page);
578 ntfs_debug("Entry not found.");
582 ntfs_unmap_page(page);
585 put_attr_search_ctx(ctx);
587 unmap_mft_record(dir_ni);
592 return ERR_MREF(err);
594 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
602 // The algorithm embedded in this code will be required for the time when we
603 // want to support adding of entries to directories, where we require correct
604 // collation of file names in order not to cause corruption of the file system.
607 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
608 * @dir_ni: ntfs inode of the directory in which to search for the name
609 * @uname: Unicode name for which to search in the directory
610 * @uname_len: length of the name @uname in Unicode characters
612 * Look for an inode with name @uname in the directory with inode @dir_ni.
613 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
614 * the Unicode name. If the name is found in the directory, the corresponding
615 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
616 * is a 64-bit number containing the sequence number.
618 * On error, a negative value is returned corresponding to the error code. In
619 * particular if the inode is not found -ENOENT is returned. Note that you
620 * can't just check the return value for being negative, you have to check the
621 * inode number for being negative which you can extract using MREC(return
624 * Note, @uname_len does not include the (optional) terminating NULL character.
626 u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
629 ntfs_volume *vol = dir_ni->vol;
630 struct super_block *sb = vol->sb;
634 INDEX_ALLOCATION *ia;
637 attr_search_context *ctx;
641 struct address_space *ia_mapping;
645 /* Get hold of the mft record for the directory. */
646 m = map_mft_record(dir_ni);
648 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
650 return ERR_MREF(PTR_ERR(m));
652 ctx = get_attr_search_ctx(dir_ni, m);
657 /* Find the index root attribute in the mft record. */
658 if (!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0,
660 ntfs_error(sb, "Index root attribute missing in directory "
661 "inode 0x%lx.", dir_ni->mft_no);
665 /* Get to the index root value (it's been verified in read_inode). */
666 ir = (INDEX_ROOT*)((u8*)ctx->attr +
667 le16_to_cpu(ctx->attr->data.resident.value_offset));
668 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
669 /* The first index entry. */
670 ie = (INDEX_ENTRY*)((u8*)&ir->index +
671 le32_to_cpu(ir->index.entries_offset));
673 * Loop until we exceed valid memory (corruption case) or until we
674 * reach the last entry.
676 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
678 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
679 sizeof(INDEX_ENTRY_HEADER) > index_end ||
680 (u8*)ie + le16_to_cpu(ie->key_length) >
684 * The last entry cannot contain a name. It can however contain
685 * a pointer to a child node in the B+tree so we just break out.
687 if (ie->flags & INDEX_ENTRY_END)
690 * If the current entry has a name type of POSIX, the name is
691 * case sensitive and not otherwise. This has the effect of us
692 * not being able to access any POSIX file names which collate
693 * after the non-POSIX one when they only differ in case, but
694 * anyone doing screwy stuff like that deserves to burn in
695 * hell... Doing that kind of stuff on NT4 actually causes
696 * corruption on the partition even when using SP6a and Linux
697 * is not involved at all.
699 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
702 * If the names match perfectly, we are done and return the
703 * mft reference of the inode (i.e. the inode number together
704 * with the sequence number for consistency checking. We
705 * convert it to cpu format before returning.
707 if (ntfs_are_names_equal(uname, uname_len,
708 (ntfschar*)&ie->key.file_name.file_name,
709 ie->key.file_name.file_name_length, ic,
710 vol->upcase, vol->upcase_len)) {
712 mref = le64_to_cpu(ie->data.dir.indexed_file);
713 put_attr_search_ctx(ctx);
714 unmap_mft_record(dir_ni);
718 * Not a perfect match, need to do full blown collation so we
719 * know which way in the B+tree we have to go.
721 rc = ntfs_collate_names(uname, uname_len,
722 (ntfschar*)&ie->key.file_name.file_name,
723 ie->key.file_name.file_name_length, 1,
724 IGNORE_CASE, vol->upcase, vol->upcase_len);
726 * If uname collates before the name of the current entry, there
727 * is definitely no such name in this index but we might need to
728 * descend into the B+tree so we just break out of the loop.
732 /* The names are not equal, continue the search. */
736 * Names match with case insensitive comparison, now try the
737 * case sensitive comparison, which is required for proper
740 rc = ntfs_collate_names(uname, uname_len,
741 (ntfschar*)&ie->key.file_name.file_name,
742 ie->key.file_name.file_name_length, 1,
743 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
749 * Perfect match, this will never happen as the
750 * ntfs_are_names_equal() call will have gotten a match but we
751 * still treat it correctly.
756 * We have finished with this index without success. Check for the
757 * presence of a child node.
759 if (!(ie->flags & INDEX_ENTRY_NODE)) {
760 /* No child node, return -ENOENT. */
763 } /* Child node present, descend into it. */
764 /* Consistency check: Verify that an index allocation exists. */
765 if (!NInoIndexAllocPresent(dir_ni)) {
766 ntfs_error(sb, "No index allocation attribute but index entry "
767 "requires one. Directory inode 0x%lx is "
768 "corrupt or driver bug.", dir_ni->mft_no);
772 /* Get the starting vcn of the index_block holding the child node. */
773 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
774 ia_mapping = VFS_I(dir_ni)->i_mapping;
776 * We are done with the index root and the mft record. Release them,
777 * otherwise we deadlock with ntfs_map_page().
779 put_attr_search_ctx(ctx);
780 unmap_mft_record(dir_ni);
783 descend_into_child_node:
785 * Convert vcn to index into the index allocation attribute in units
786 * of PAGE_CACHE_SIZE and map the page cache page, reading it from
789 page = ntfs_map_page(ia_mapping, vcn <<
790 dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT);
792 ntfs_error(sb, "Failed to map directory index page, error %ld.",
798 kaddr = (u8*)page_address(page);
799 fast_descend_into_child_node:
800 /* Get to the index allocation block. */
801 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
802 dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK));
804 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) {
805 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
806 "inode 0x%lx or driver bug.", dir_ni->mft_no);
810 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
811 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
812 "different from expected VCN (0x%llx). "
813 "Directory inode 0x%lx is corrupt or driver "
814 "bug.", (unsigned long long)
815 sle64_to_cpu(ia->index_block_vcn),
816 (unsigned long long)vcn, dir_ni->mft_no);
820 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
821 dir_ni->itype.index.block_size) {
822 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
823 "0x%lx has a size (%u) differing from the "
824 "directory specified size (%u). Directory "
825 "inode is corrupt or driver bug.",
826 (unsigned long long)vcn, dir_ni->mft_no,
827 le32_to_cpu(ia->index.allocated_size) + 0x18,
828 dir_ni->itype.index.block_size);
832 index_end = (u8*)ia + dir_ni->itype.index.block_size;
833 if (index_end > kaddr + PAGE_CACHE_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,
842 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
843 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
844 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
845 "inode 0x%lx exceeds maximum size.",
846 (unsigned long long)vcn, dir_ni->mft_no);
850 /* The first index entry. */
851 ie = (INDEX_ENTRY*)((u8*)&ia->index +
852 le32_to_cpu(ia->index.entries_offset));
854 * Iterate similar to above big loop but applied to index buffer, thus
855 * loop until we exceed valid memory (corruption case) or until we
856 * reach the last entry.
858 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
860 if ((u8*)ie < (u8*)ia || (u8*)ie +
861 sizeof(INDEX_ENTRY_HEADER) > index_end ||
862 (u8*)ie + le16_to_cpu(ie->key_length) >
864 ntfs_error(sb, "Index entry out of bounds in "
865 "directory inode 0x%lx.",
871 * The last entry cannot contain a name. It can however contain
872 * a pointer to a child node in the B+tree so we just break out.
874 if (ie->flags & INDEX_ENTRY_END)
877 * If the current entry has a name type of POSIX, the name is
878 * case sensitive and not otherwise. This has the effect of us
879 * not being able to access any POSIX file names which collate
880 * after the non-POSIX one when they only differ in case, but
881 * anyone doing screwy stuff like that deserves to burn in
882 * hell... Doing that kind of stuff on NT4 actually causes
883 * corruption on the partition even when using SP6a and Linux
884 * is not involved at all.
886 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
889 * If the names match perfectly, we are done and return the
890 * mft reference of the inode (i.e. the inode number together
891 * with the sequence number for consistency checking. We
892 * convert it to cpu format before returning.
894 if (ntfs_are_names_equal(uname, uname_len,
895 (ntfschar*)&ie->key.file_name.file_name,
896 ie->key.file_name.file_name_length, ic,
897 vol->upcase, vol->upcase_len)) {
899 mref = le64_to_cpu(ie->data.dir.indexed_file);
901 ntfs_unmap_page(page);
905 * Not a perfect match, need to do full blown collation so we
906 * know which way in the B+tree we have to go.
908 rc = ntfs_collate_names(uname, uname_len,
909 (ntfschar*)&ie->key.file_name.file_name,
910 ie->key.file_name.file_name_length, 1,
911 IGNORE_CASE, vol->upcase, vol->upcase_len);
913 * If uname collates before the name of the current entry, there
914 * is definitely no such name in this index but we might need to
915 * descend into the B+tree so we just break out of the loop.
919 /* The names are not equal, continue the search. */
923 * Names match with case insensitive comparison, now try the
924 * case sensitive comparison, which is required for proper
927 rc = ntfs_collate_names(uname, uname_len,
928 (ntfschar*)&ie->key.file_name.file_name,
929 ie->key.file_name.file_name_length, 1,
930 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
936 * Perfect match, this will never happen as the
937 * ntfs_are_names_equal() call will have gotten a match but we
938 * still treat it correctly.
943 * We have finished with this index buffer without success. Check for
944 * the presence of a child node.
946 if (ie->flags & INDEX_ENTRY_NODE) {
947 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
948 ntfs_error(sb, "Index entry with child node found in "
949 "a leaf node in directory inode 0x%lx.",
954 /* Child node present, descend into it. */
956 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
958 /* If vcn is in the same page cache page as old_vcn we
959 * recycle the mapped page. */
960 if (old_vcn << vol->cluster_size_bits >>
961 PAGE_CACHE_SHIFT == vcn <<
962 vol->cluster_size_bits >>
964 goto fast_descend_into_child_node;
966 ntfs_unmap_page(page);
967 goto descend_into_child_node;
969 ntfs_error(sb, "Negative child node vcn in directory inode "
970 "0x%lx.", dir_ni->mft_no);
974 /* No child node, return -ENOENT. */
975 ntfs_debug("Entry not found.");
979 ntfs_unmap_page(page);
982 put_attr_search_ctx(ctx);
984 unmap_mft_record(dir_ni);
985 return ERR_MREF(err);
987 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
996 INDEX_ALLOCATION *ia;
997 } index_union __attribute__ ((__transparent_union__));
1000 INDEX_TYPE_ROOT, /* index root */
1001 INDEX_TYPE_ALLOCATION, /* index allocation */
1005 * ntfs_filldir - ntfs specific filldir method
1006 * @vol: current ntfs volume
1007 * @fpos: position in the directory
1008 * @ndir: ntfs inode of current directory
1009 * @index_type: specifies whether @iu is an index root or an index allocation
1010 * @iu: index root or index allocation attribute to which @ie belongs
1011 * @ia_page: page in which the index allocation buffer @ie is in resides
1012 * @ie: current index entry
1013 * @name: buffer to use for the converted name
1014 * @dirent: vfs filldir callback context
1015 * @filldir: vfs filldir callback
1017 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
1020 * If @index_type is INDEX_TYPE_ALLOCATION, @ia_page is the locked page
1021 * containing the index allocation block containing the index entry @ie.
1022 * Otherwise, @ia_page is NULL.
1024 * Note, we drop (and then reacquire) the page lock on @ia_page across the
1025 * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
1026 * since ntfs_lookup() will lock the same page. As an optimization, we do not
1027 * retake the lock if we are returning a non-zero value as ntfs_readdir()
1028 * would need to drop the lock immediately anyway.
1030 static inline int ntfs_filldir(ntfs_volume *vol, loff_t *fpos,
1031 ntfs_inode *ndir, const INDEX_TYPE index_type,
1032 index_union iu, struct page *ia_page, INDEX_ENTRY *ie,
1033 u8 *name, void *dirent, filldir_t filldir)
1038 FILE_NAME_TYPE_FLAGS name_type;
1040 /* Advance the position even if going to skip the entry. */
1041 if (index_type == INDEX_TYPE_ALLOCATION)
1042 *fpos = (u8*)ie - (u8*)iu.ia +
1043 (sle64_to_cpu(iu.ia->index_block_vcn) <<
1044 ndir->itype.index.vcn_size_bits) +
1045 vol->mft_record_size;
1046 else /* if (index_type == INDEX_TYPE_ROOT) */
1047 *fpos = (u8*)ie - (u8*)iu.ir;
1048 name_type = ie->key.file_name.file_name_type;
1049 if (name_type == FILE_NAME_DOS) {
1050 ntfs_debug("Skipping DOS name space entry.");
1053 if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
1054 ntfs_debug("Skipping root directory self reference entry.");
1057 if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
1058 !NVolShowSystemFiles(vol)) {
1059 ntfs_debug("Skipping system file.");
1062 name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
1063 ie->key.file_name.file_name_length, &name,
1064 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
1065 if (name_len <= 0) {
1066 ntfs_debug("Skipping unrepresentable file.");
1069 if (ie->key.file_name.file_attributes &
1070 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
1074 mref = MREF_LE(ie->data.dir.indexed_file);
1076 * Drop the page lock otherwise we deadlock with NFS when it calls
1077 * ->lookup since ntfs_lookup() will lock the same page.
1079 if (index_type == INDEX_TYPE_ALLOCATION)
1080 unlock_page(ia_page);
1081 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
1082 "0x%lx, DT_%s.", name, name_len, *fpos, mref,
1083 dt_type == DT_DIR ? "DIR" : "REG");
1084 rc = filldir(dirent, name, name_len, *fpos, mref, dt_type);
1085 /* Relock the page but not if we are aborting ->readdir. */
1086 if (!rc && index_type == INDEX_TYPE_ALLOCATION)
1092 * We use the same basic approach as the old NTFS driver, i.e. we parse the
1093 * index root entries and then the index allocation entries that are marked
1094 * as in use in the index bitmap.
1096 * While this will return the names in random order this doesn't matter for
1097 * ->readdir but OTOH results in a faster ->readdir.
1099 * VFS calls ->readdir without BKL but with i_sem held. This protects the VFS
1100 * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1103 * Locking: - Caller must hold i_sem on the directory.
1104 * - Each page cache page in the index allocation mapping must be
1105 * locked whilst being accessed otherwise we may find a corrupt
1106 * page due to it being under ->writepage at the moment which
1107 * applies the mst protection fixups before writing out and then
1108 * removes them again after the write is complete after which it
1111 static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1113 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1115 struct inode *bmp_vi, *vdir = filp->f_dentry->d_inode;
1116 struct super_block *sb = vdir->i_sb;
1117 ntfs_inode *ndir = NTFS_I(vdir);
1118 ntfs_volume *vol = NTFS_SB(sb);
1120 INDEX_ROOT *ir = NULL;
1122 INDEX_ALLOCATION *ia;
1124 int rc, err, ir_pos, cur_bmp_pos;
1125 struct address_space *ia_mapping, *bmp_mapping;
1126 struct page *bmp_page = NULL, *ia_page = NULL;
1127 u8 *kaddr, *bmp, *index_end;
1128 attr_search_context *ctx;
1131 ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
1134 /* Are we at end of dir yet? */
1135 if (fpos >= vdir->i_size + vol->mft_record_size)
1137 /* Emulate . and .. for all directories. */
1139 ntfs_debug("Calling filldir for . with len 1, fpos 0x0, "
1140 "inode 0x%lx, DT_DIR.", vdir->i_ino);
1141 rc = filldir(dirent, ".", 1, fpos, vdir->i_ino, DT_DIR);
1147 ntfs_debug("Calling filldir for .. with len 2, fpos 0x1, "
1148 "inode 0x%lx, DT_DIR.",
1149 parent_ino(filp->f_dentry));
1150 rc = filldir(dirent, "..", 2, fpos,
1151 parent_ino(filp->f_dentry), DT_DIR);
1159 * Allocate a buffer to store the current name being processed
1160 * converted to format determined by current NLS.
1162 name = (u8*)kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1,
1164 if (unlikely(!name)) {
1168 /* Are we jumping straight into the index allocation attribute? */
1169 if (fpos >= vol->mft_record_size)
1170 goto skip_index_root;
1171 /* Get hold of the mft record for the directory. */
1172 m = map_mft_record(ndir);
1173 if (unlikely(IS_ERR(m))) {
1178 ctx = get_attr_search_ctx(ndir, m);
1179 if (unlikely(!ctx)) {
1183 /* Get the offset into the index root attribute. */
1185 /* Find the index root attribute in the mft record. */
1186 if (unlikely(!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0,
1188 ntfs_error(sb, "Index root attribute missing in directory "
1189 "inode 0x%lx.", vdir->i_ino);
1193 * Copy the index root attribute value to a buffer so that we can put
1194 * the search context and unmap the mft record before calling the
1195 * filldir() callback. We need to do this because of NFSd which calls
1196 * ->lookup() from its filldir callback() and this causes NTFS to
1197 * deadlock as ntfs_lookup() maps the mft record of the directory and
1198 * we have got it mapped here already. The only solution is for us to
1199 * unmap the mft record here so that a call to ntfs_lookup() is able to
1200 * map the mft record without deadlocking.
1202 rc = le32_to_cpu(ctx->attr->data.resident.value_length);
1203 ir = (INDEX_ROOT*)kmalloc(rc, GFP_NOFS);
1204 if (unlikely(!ir)) {
1208 /* Copy the index root value (it has been verified in read_inode). */
1209 memcpy(ir, (u8*)ctx->attr +
1210 le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
1211 put_attr_search_ctx(ctx);
1212 unmap_mft_record(ndir);
1215 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1216 /* The first index entry. */
1217 ie = (INDEX_ENTRY*)((u8*)&ir->index +
1218 le32_to_cpu(ir->index.entries_offset));
1220 * Loop until we exceed valid memory (corruption case) or until we
1221 * reach the last entry or until filldir tells us it has had enough
1222 * or signals an error (both covered by the rc test).
1224 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1225 ntfs_debug("In index root, offset 0x%x.", (u8*)ie - (u8*)ir);
1226 /* Bounds checks. */
1227 if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
1228 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1229 (u8*)ie + le16_to_cpu(ie->key_length) >
1232 /* The last entry cannot contain a name. */
1233 if (ie->flags & INDEX_ENTRY_END)
1235 /* Skip index root entry if continuing previous readdir. */
1236 if (ir_pos > (u8*)ie - (u8*)ir)
1238 /* Submit the name to the filldir callback. */
1239 rc = ntfs_filldir(vol, &fpos, ndir, INDEX_TYPE_ROOT, ir, NULL,
1240 ie, name, dirent, filldir);
1246 /* We are done with the index root and can free the buffer. */
1249 /* If there is no index allocation attribute we are finished. */
1250 if (!NInoIndexAllocPresent(ndir))
1252 /* Advance fpos to the beginning of the index allocation. */
1253 fpos = vol->mft_record_size;
1257 /* Get the offset into the index allocation attribute. */
1258 ia_pos = (s64)fpos - vol->mft_record_size;
1259 ia_mapping = vdir->i_mapping;
1260 bmp_vi = ndir->itype.index.bmp_ino;
1261 if (unlikely(!bmp_vi)) {
1262 ntfs_debug("Inode 0x%lx, regetting index bitmap.", vdir->i_ino);
1263 bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
1264 if (unlikely(IS_ERR(bmp_vi))) {
1265 ntfs_error(sb, "Failed to get bitmap attribute.");
1266 err = PTR_ERR(bmp_vi);
1269 ndir->itype.index.bmp_ino = bmp_vi;
1271 bmp_mapping = bmp_vi->i_mapping;
1272 /* Get the starting bitmap bit position and sanity check it. */
1273 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1274 if (unlikely(bmp_pos >> 3 >= bmp_vi->i_size)) {
1275 ntfs_error(sb, "Current index allocation position exceeds "
1276 "index bitmap size.");
1279 /* Get the starting bit position in the current bitmap page. */
1280 cur_bmp_pos = bmp_pos & ((PAGE_CACHE_SIZE * 8) - 1);
1281 bmp_pos &= ~(u64)((PAGE_CACHE_SIZE * 8) - 1);
1283 ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1284 (unsigned long long)bmp_pos >> (3 + PAGE_CACHE_SHIFT),
1285 (unsigned long long)bmp_pos &
1286 ((PAGE_CACHE_SIZE * 8) - 1));
1287 bmp_page = ntfs_map_page(bmp_mapping,
1288 bmp_pos >> (3 + PAGE_CACHE_SHIFT));
1289 if (unlikely(IS_ERR(bmp_page))) {
1290 ntfs_error(sb, "Reading index bitmap failed.");
1291 err = PTR_ERR(bmp_page);
1295 bmp = (u8*)page_address(bmp_page);
1296 /* Find next index block in use. */
1297 while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
1298 find_next_index_buffer:
1301 * If we have reached the end of the bitmap page, get the next
1302 * page, and put away the old one.
1304 if (unlikely((cur_bmp_pos >> 3) >= PAGE_CACHE_SIZE)) {
1305 ntfs_unmap_page(bmp_page);
1306 bmp_pos += PAGE_CACHE_SIZE * 8;
1308 goto get_next_bmp_page;
1310 /* If we have reached the end of the bitmap, we are done. */
1311 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= vdir->i_size))
1313 ia_pos = (bmp_pos + cur_bmp_pos) <<
1314 ndir->itype.index.block_size_bits;
1316 ntfs_debug("Handling index buffer 0x%llx.",
1317 (unsigned long long)bmp_pos + cur_bmp_pos);
1318 /* If the current index buffer is in the same page we reuse the page. */
1319 if ((prev_ia_pos & PAGE_CACHE_MASK) != (ia_pos & PAGE_CACHE_MASK)) {
1320 prev_ia_pos = ia_pos;
1321 if (likely(ia_page != NULL)) {
1322 unlock_page(ia_page);
1323 ntfs_unmap_page(ia_page);
1326 * Map the page cache page containing the current ia_pos,
1327 * reading it from disk if necessary.
1329 ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_CACHE_SHIFT);
1330 if (unlikely(IS_ERR(ia_page))) {
1331 ntfs_error(sb, "Reading index allocation data failed.");
1332 err = PTR_ERR(ia_page);
1337 kaddr = (u8*)page_address(ia_page);
1339 /* Get the current index buffer. */
1340 ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_CACHE_MASK &
1341 ~(s64)(ndir->itype.index.block_size - 1)));
1342 /* Bounds checks. */
1343 if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE)) {
1344 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
1345 "inode 0x%lx or driver bug.", vdir->i_ino);
1348 if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
1349 ~(s64)(ndir->itype.index.block_size - 1)) >>
1350 ndir->itype.index.vcn_size_bits)) {
1351 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
1352 "different from expected VCN (0x%llx). "
1353 "Directory inode 0x%lx is corrupt or driver "
1354 "bug. ", (unsigned long long)
1355 sle64_to_cpu(ia->index_block_vcn),
1356 (unsigned long long)ia_pos >>
1357 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1360 if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
1361 ndir->itype.index.block_size)) {
1362 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1363 "0x%lx has a size (%u) differing from the "
1364 "directory specified size (%u). Directory "
1365 "inode is corrupt or driver bug.",
1366 (unsigned long long)ia_pos >>
1367 ndir->itype.index.vcn_size_bits, vdir->i_ino,
1368 le32_to_cpu(ia->index.allocated_size) + 0x18,
1369 ndir->itype.index.block_size);
1372 index_end = (u8*)ia + ndir->itype.index.block_size;
1373 if (unlikely(index_end > kaddr + PAGE_CACHE_SIZE)) {
1374 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1375 "0x%lx crosses page boundary. Impossible! "
1376 "Cannot access! This is probably a bug in the "
1377 "driver.", (unsigned long long)ia_pos >>
1378 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1381 ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
1382 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
1383 if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
1384 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
1385 "inode 0x%lx exceeds maximum size.",
1386 (unsigned long long)ia_pos >>
1387 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1390 /* The first index entry in this index buffer. */
1391 ie = (INDEX_ENTRY*)((u8*)&ia->index +
1392 le32_to_cpu(ia->index.entries_offset));
1394 * Loop until we exceed valid memory (corruption case) or until we
1395 * reach the last entry or until filldir tells us it has had enough
1396 * or signals an error (both covered by the rc test).
1398 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1399 ntfs_debug("In index allocation, offset 0x%llx.",
1400 (unsigned long long)ia_start + ((u8*)ie -
1402 /* Bounds checks. */
1403 if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
1404 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1405 (u8*)ie + le16_to_cpu(ie->key_length) >
1408 /* The last entry cannot contain a name. */
1409 if (ie->flags & INDEX_ENTRY_END)
1411 /* Skip index block entry if continuing previous readdir. */
1412 if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
1415 * Submit the name to the @filldir callback. Note,
1416 * ntfs_filldir() drops the lock on @ia_page but it retakes it
1417 * before returning, unless a non-zero value is returned in
1418 * which case the page is left unlocked.
1420 rc = ntfs_filldir(vol, &fpos, ndir, INDEX_TYPE_ALLOCATION, ia,
1421 ia_page, ie, name, dirent, filldir);
1423 /* @ia_page is already unlocked in this case. */
1424 ntfs_unmap_page(ia_page);
1425 ntfs_unmap_page(bmp_page);
1429 goto find_next_index_buffer;
1432 unlock_page(ia_page);
1433 ntfs_unmap_page(ia_page);
1435 ntfs_unmap_page(bmp_page);
1437 /* We are finished, set fpos to EOD. */
1438 fpos = vdir->i_size + vol->mft_record_size;
1444 ntfs_debug("EOD, fpos 0x%llx, returning 0.", fpos);
1446 ntfs_debug("filldir returned %i, fpos 0x%llx, returning 0.",
1453 ntfs_unmap_page(bmp_page);
1455 unlock_page(ia_page);
1456 ntfs_unmap_page(ia_page);
1463 put_attr_search_ctx(ctx);
1465 unmap_mft_record(ndir);
1468 ntfs_debug("Failed. Returning error code %i.", -err);
1474 * ntfs_dir_open - called when an inode is about to be opened
1475 * @vi: inode to be opened
1476 * @filp: file structure describing the inode
1478 * Limit directory size to the page cache limit on architectures where unsigned
1479 * long is 32-bits. This is the most we can do for now without overflowing the
1480 * page cache page index. Doing it this way means we don't run into problems
1481 * because of existing too large directories. It would be better to allow the
1482 * user to read the accessible part of the directory but I doubt very much
1483 * anyone is going to hit this check on a 32-bit architecture, so there is no
1484 * point in adding the extra complexity required to support this.
1486 * On 64-bit architectures, the check is hopefully optimized away by the
1489 static int ntfs_dir_open(struct inode *vi, struct file *filp)
1491 if (sizeof(unsigned long) < 8) {
1492 if (vi->i_size > MAX_LFS_FILESIZE)
1498 struct file_operations ntfs_dir_ops = {
1499 .llseek = generic_file_llseek, /* Seek inside directory. */
1500 .read = generic_read_dir, /* Return -EISDIR. */
1501 .readdir = ntfs_readdir, /* Read directory contents. */
1502 .open = ntfs_dir_open, /* Open directory. */