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>
11 #include <linux/blkdev.h>
21 * The little endian Unicode string $I30 as a global constant.
23 ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
24 cpu_to_le16('3'), cpu_to_le16('0'), 0 };
27 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
28 * @dir_ni: ntfs inode of the directory in which to search for the name
29 * @uname: Unicode name for which to search in the directory
30 * @uname_len: length of the name @uname in Unicode characters
31 * @res: return the found file name if necessary (see below)
33 * Look for an inode with name @uname in the directory with inode @dir_ni.
34 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
35 * the Unicode name. If the name is found in the directory, the corresponding
36 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
37 * is a 64-bit number containing the sequence number.
39 * On error, a negative value is returned corresponding to the error code. In
40 * particular if the inode is not found -ENOENT is returned. Note that you
41 * can't just check the return value for being negative, you have to check the
42 * inode number for being negative which you can extract using MREC(return
45 * Note, @uname_len does not include the (optional) terminating NULL character.
47 * Note, we look for a case sensitive match first but we also look for a case
48 * insensitive match at the same time. If we find a case insensitive match, we
49 * save that for the case that we don't find an exact match, where we return
50 * the case insensitive match and setup @res (which we allocate!) with the mft
51 * reference, the file name type, length and with a copy of the little endian
52 * Unicode file name itself. If we match a file name which is in the DOS name
53 * space, we only return the mft reference and file name type in @res.
54 * ntfs_lookup() then uses this to find the long file name in the inode itself.
55 * This is to avoid polluting the dcache with short file names. We want them to
56 * work but we don't care for how quickly one can access them. This also fixes
57 * the dcache aliasing issues.
59 * Locking: - Caller must hold i_mutex on the directory.
60 * - Each page cache page in the index allocation mapping must be
61 * locked whilst being accessed otherwise we may find a corrupt
62 * page due to it being under ->writepage at the moment which
63 * applies the mst protection fixups before writing out and then
64 * removes them again after the write is complete after which it
67 MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
68 const int uname_len, ntfs_name **res)
70 ntfs_volume *vol = dir_ni->vol;
71 struct super_block *sb = vol->sb;
78 ntfs_attr_search_ctx *ctx;
81 struct address_space *ia_mapping;
84 ntfs_name *name = NULL;
86 BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
87 BUG_ON(NInoAttr(dir_ni));
88 /* Get hold of the mft record for the directory. */
89 m = map_mft_record(dir_ni);
91 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
93 return ERR_MREF(PTR_ERR(m));
95 ctx = ntfs_attr_get_search_ctx(dir_ni, m);
100 /* Find the index root attribute in the mft record. */
101 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
104 if (err == -ENOENT) {
105 ntfs_error(sb, "Index root attribute missing in "
106 "directory inode 0x%lx.",
112 /* Get to the index root value (it's been verified in read_inode). */
113 ir = (INDEX_ROOT*)((u8*)ctx->attr +
114 le16_to_cpu(ctx->attr->data.resident.value_offset));
115 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
116 /* The first index entry. */
117 ie = (INDEX_ENTRY*)((u8*)&ir->index +
118 le32_to_cpu(ir->index.entries_offset));
120 * Loop until we exceed valid memory (corruption case) or until we
121 * reach the last entry.
123 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
125 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
126 sizeof(INDEX_ENTRY_HEADER) > index_end ||
127 (u8*)ie + le16_to_cpu(ie->key_length) >
131 * The last entry cannot contain a name. It can however contain
132 * a pointer to a child node in the B+tree so we just break out.
134 if (ie->flags & INDEX_ENTRY_END)
137 * We perform a case sensitive comparison and if that matches
138 * we are done and return the mft reference of the inode (i.e.
139 * the inode number together with the sequence number for
140 * consistency checking). We convert it to cpu format before
143 if (ntfs_are_names_equal(uname, uname_len,
144 (ntfschar*)&ie->key.file_name.file_name,
145 ie->key.file_name.file_name_length,
146 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
149 * We have a perfect match, so we don't need to care
150 * about having matched imperfectly before, so we can
151 * free name and set *res to NULL.
152 * However, if the perfect match is a short file name,
153 * we need to signal this through *res, so that
154 * ntfs_lookup() can fix dcache aliasing issues.
155 * As an optimization we just reuse an existing
156 * allocation of *res.
158 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
160 name = kmalloc(sizeof(ntfs_name),
167 name->mref = le64_to_cpu(
168 ie->data.dir.indexed_file);
169 name->type = FILE_NAME_DOS;
176 mref = le64_to_cpu(ie->data.dir.indexed_file);
177 ntfs_attr_put_search_ctx(ctx);
178 unmap_mft_record(dir_ni);
182 * For a case insensitive mount, we also perform a case
183 * insensitive comparison (provided the file name is not in the
184 * POSIX namespace). If the comparison matches, and the name is
185 * in the WIN32 namespace, we cache the filename in *res so
186 * that the caller, ntfs_lookup(), can work on it. If the
187 * comparison matches, and the name is in the DOS namespace, we
188 * only cache the mft reference and the file name type (we set
189 * the name length to zero for simplicity).
191 if (!NVolCaseSensitive(vol) &&
192 ie->key.file_name.file_name_type &&
193 ntfs_are_names_equal(uname, uname_len,
194 (ntfschar*)&ie->key.file_name.file_name,
195 ie->key.file_name.file_name_length,
196 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
197 int name_size = sizeof(ntfs_name);
198 u8 type = ie->key.file_name.file_name_type;
199 u8 len = ie->key.file_name.file_name_length;
201 /* Only one case insensitive matching name allowed. */
203 ntfs_error(sb, "Found already allocated name "
204 "in phase 1. Please run chkdsk "
205 "and if that doesn't find any "
206 "errors please report you saw "
208 "linux-ntfs-dev@lists."
213 if (type != FILE_NAME_DOS)
214 name_size += len * sizeof(ntfschar);
215 name = kmalloc(name_size, GFP_NOFS);
220 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
222 if (type != FILE_NAME_DOS) {
224 memcpy(name->name, ie->key.file_name.file_name,
225 len * sizeof(ntfschar));
231 * Not a perfect match, need to do full blown collation so we
232 * know which way in the B+tree we have to go.
234 rc = ntfs_collate_names(uname, uname_len,
235 (ntfschar*)&ie->key.file_name.file_name,
236 ie->key.file_name.file_name_length, 1,
237 IGNORE_CASE, vol->upcase, vol->upcase_len);
239 * If uname collates before the name of the current entry, there
240 * is definitely no such name in this index but we might need to
241 * descend into the B+tree so we just break out of the loop.
245 /* The names are not equal, continue the search. */
249 * Names match with case insensitive comparison, now try the
250 * case sensitive comparison, which is required for proper
253 rc = ntfs_collate_names(uname, uname_len,
254 (ntfschar*)&ie->key.file_name.file_name,
255 ie->key.file_name.file_name_length, 1,
256 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
262 * Perfect match, this will never happen as the
263 * ntfs_are_names_equal() call will have gotten a match but we
264 * still treat it correctly.
269 * We have finished with this index without success. Check for the
270 * presence of a child node and if not present return -ENOENT, unless
271 * we have got a matching name cached in name in which case return the
272 * mft reference associated with it.
274 if (!(ie->flags & INDEX_ENTRY_NODE)) {
276 ntfs_attr_put_search_ctx(ctx);
277 unmap_mft_record(dir_ni);
280 ntfs_debug("Entry not found.");
283 } /* Child node present, descend into it. */
284 /* Consistency check: Verify that an index allocation exists. */
285 if (!NInoIndexAllocPresent(dir_ni)) {
286 ntfs_error(sb, "No index allocation attribute but index entry "
287 "requires one. Directory inode 0x%lx is "
288 "corrupt or driver bug.", dir_ni->mft_no);
291 /* Get the starting vcn of the index_block holding the child node. */
292 vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
293 ia_mapping = VFS_I(dir_ni)->i_mapping;
295 * We are done with the index root and the mft record. Release them,
296 * otherwise we deadlock with ntfs_map_page().
298 ntfs_attr_put_search_ctx(ctx);
299 unmap_mft_record(dir_ni);
302 descend_into_child_node:
304 * Convert vcn to index into the index allocation attribute in units
305 * of PAGE_SIZE and map the page cache page, reading it from
308 page = ntfs_map_page(ia_mapping, vcn <<
309 dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
311 ntfs_error(sb, "Failed to map directory index page, error %ld.",
317 kaddr = (u8*)page_address(page);
318 fast_descend_into_child_node:
319 /* Get to the index allocation block. */
320 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
321 dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
323 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
324 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
325 "inode 0x%lx or driver bug.", dir_ni->mft_no);
328 /* Catch multi sector transfer fixup errors. */
329 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
330 ntfs_error(sb, "Directory index record with vcn 0x%llx is "
331 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
332 (unsigned long long)vcn, 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);
344 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
345 dir_ni->itype.index.block_size) {
346 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
347 "0x%lx has a size (%u) differing from the "
348 "directory specified size (%u). Directory "
349 "inode is corrupt or driver bug.",
350 (unsigned long long)vcn, dir_ni->mft_no,
351 le32_to_cpu(ia->index.allocated_size) + 0x18,
352 dir_ni->itype.index.block_size);
355 index_end = (u8*)ia + dir_ni->itype.index.block_size;
356 if (index_end > kaddr + PAGE_SIZE) {
357 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
358 "0x%lx crosses page boundary. Impossible! "
359 "Cannot access! This is probably a bug in the "
360 "driver.", (unsigned long long)vcn,
364 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
365 if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
366 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
367 "inode 0x%lx exceeds maximum size.",
368 (unsigned long long)vcn, dir_ni->mft_no);
371 /* The first index entry. */
372 ie = (INDEX_ENTRY*)((u8*)&ia->index +
373 le32_to_cpu(ia->index.entries_offset));
375 * Iterate similar to above big loop but applied to index buffer, thus
376 * loop until we exceed valid memory (corruption case) or until we
377 * reach the last entry.
379 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
381 if ((u8*)ie < (u8*)ia || (u8*)ie +
382 sizeof(INDEX_ENTRY_HEADER) > index_end ||
383 (u8*)ie + le16_to_cpu(ie->key_length) >
385 ntfs_error(sb, "Index entry out of bounds in "
386 "directory inode 0x%lx.",
391 * The last entry cannot contain a name. It can however contain
392 * a pointer to a child node in the B+tree so we just break out.
394 if (ie->flags & INDEX_ENTRY_END)
397 * We perform a case sensitive comparison and if that matches
398 * we are done and return the mft reference of the inode (i.e.
399 * the inode number together with the sequence number for
400 * consistency checking). We convert it to cpu format before
403 if (ntfs_are_names_equal(uname, uname_len,
404 (ntfschar*)&ie->key.file_name.file_name,
405 ie->key.file_name.file_name_length,
406 CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
409 * We have a perfect match, so we don't need to care
410 * about having matched imperfectly before, so we can
411 * free name and set *res to NULL.
412 * However, if the perfect match is a short file name,
413 * we need to signal this through *res, so that
414 * ntfs_lookup() can fix dcache aliasing issues.
415 * As an optimization we just reuse an existing
416 * allocation of *res.
418 if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
420 name = kmalloc(sizeof(ntfs_name),
427 name->mref = le64_to_cpu(
428 ie->data.dir.indexed_file);
429 name->type = FILE_NAME_DOS;
436 mref = le64_to_cpu(ie->data.dir.indexed_file);
438 ntfs_unmap_page(page);
442 * For a case insensitive mount, we also perform a case
443 * insensitive comparison (provided the file name is not in the
444 * POSIX namespace). If the comparison matches, and the name is
445 * in the WIN32 namespace, we cache the filename in *res so
446 * that the caller, ntfs_lookup(), can work on it. If the
447 * comparison matches, and the name is in the DOS namespace, we
448 * only cache the mft reference and the file name type (we set
449 * the name length to zero for simplicity).
451 if (!NVolCaseSensitive(vol) &&
452 ie->key.file_name.file_name_type &&
453 ntfs_are_names_equal(uname, uname_len,
454 (ntfschar*)&ie->key.file_name.file_name,
455 ie->key.file_name.file_name_length,
456 IGNORE_CASE, vol->upcase, vol->upcase_len)) {
457 int name_size = sizeof(ntfs_name);
458 u8 type = ie->key.file_name.file_name_type;
459 u8 len = ie->key.file_name.file_name_length;
461 /* Only one case insensitive matching name allowed. */
463 ntfs_error(sb, "Found already allocated name "
464 "in phase 2. Please run chkdsk "
465 "and if that doesn't find any "
466 "errors please report you saw "
468 "linux-ntfs-dev@lists."
471 ntfs_unmap_page(page);
475 if (type != FILE_NAME_DOS)
476 name_size += len * sizeof(ntfschar);
477 name = kmalloc(name_size, GFP_NOFS);
482 name->mref = le64_to_cpu(ie->data.dir.indexed_file);
484 if (type != FILE_NAME_DOS) {
486 memcpy(name->name, ie->key.file_name.file_name,
487 len * sizeof(ntfschar));
493 * Not a perfect match, need to do full blown collation so we
494 * know which way in the B+tree we have to go.
496 rc = ntfs_collate_names(uname, uname_len,
497 (ntfschar*)&ie->key.file_name.file_name,
498 ie->key.file_name.file_name_length, 1,
499 IGNORE_CASE, vol->upcase, vol->upcase_len);
501 * If uname collates before the name of the current entry, there
502 * is definitely no such name in this index but we might need to
503 * descend into the B+tree so we just break out of the loop.
507 /* The names are not equal, continue the search. */
511 * Names match with case insensitive comparison, now try the
512 * case sensitive comparison, which is required for proper
515 rc = ntfs_collate_names(uname, uname_len,
516 (ntfschar*)&ie->key.file_name.file_name,
517 ie->key.file_name.file_name_length, 1,
518 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
524 * Perfect match, this will never happen as the
525 * ntfs_are_names_equal() call will have gotten a match but we
526 * still treat it correctly.
531 * We have finished with this index buffer without success. Check for
532 * the presence of a child node.
534 if (ie->flags & INDEX_ENTRY_NODE) {
535 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
536 ntfs_error(sb, "Index entry with child node found in "
537 "a leaf node in directory inode 0x%lx.",
541 /* Child node present, descend into it. */
543 vcn = sle64_to_cpup((sle64*)((u8*)ie +
544 le16_to_cpu(ie->length) - 8));
546 /* If vcn is in the same page cache page as old_vcn we
547 * recycle the mapped page. */
548 if (old_vcn << vol->cluster_size_bits >>
550 vol->cluster_size_bits >>
552 goto fast_descend_into_child_node;
554 ntfs_unmap_page(page);
555 goto descend_into_child_node;
557 ntfs_error(sb, "Negative child node vcn in directory inode "
558 "0x%lx.", dir_ni->mft_no);
562 * No child node present, return -ENOENT, unless we have got a matching
563 * name cached in name in which case return the mft reference
564 * associated with it.
568 ntfs_unmap_page(page);
571 ntfs_debug("Entry not found.");
575 ntfs_unmap_page(page);
580 ntfs_attr_put_search_ctx(ctx);
582 unmap_mft_record(dir_ni);
587 return ERR_MREF(err);
589 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
596 // The algorithm embedded in this code will be required for the time when we
597 // want to support adding of entries to directories, where we require correct
598 // collation of file names in order not to cause corruption of the filesystem.
601 * ntfs_lookup_inode_by_name - find an inode in a directory given its name
602 * @dir_ni: ntfs inode of the directory in which to search for the name
603 * @uname: Unicode name for which to search in the directory
604 * @uname_len: length of the name @uname in Unicode characters
606 * Look for an inode with name @uname in the directory with inode @dir_ni.
607 * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
608 * the Unicode name. If the name is found in the directory, the corresponding
609 * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
610 * is a 64-bit number containing the sequence number.
612 * On error, a negative value is returned corresponding to the error code. In
613 * particular if the inode is not found -ENOENT is returned. Note that you
614 * can't just check the return value for being negative, you have to check the
615 * inode number for being negative which you can extract using MREC(return
618 * Note, @uname_len does not include the (optional) terminating NULL character.
620 u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
623 ntfs_volume *vol = dir_ni->vol;
624 struct super_block *sb = vol->sb;
628 INDEX_ALLOCATION *ia;
631 ntfs_attr_search_ctx *ctx;
635 struct address_space *ia_mapping;
639 /* Get hold of the mft record for the directory. */
640 m = map_mft_record(dir_ni);
642 ntfs_error(sb, "map_mft_record() failed with error code %ld.",
644 return ERR_MREF(PTR_ERR(m));
646 ctx = ntfs_attr_get_search_ctx(dir_ni, m);
651 /* Find the index root attribute in the mft record. */
652 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
655 if (err == -ENOENT) {
656 ntfs_error(sb, "Index root attribute missing in "
657 "directory inode 0x%lx.",
663 /* Get to the index root value (it's been verified in read_inode). */
664 ir = (INDEX_ROOT*)((u8*)ctx->attr +
665 le16_to_cpu(ctx->attr->data.resident.value_offset));
666 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
667 /* The first index entry. */
668 ie = (INDEX_ENTRY*)((u8*)&ir->index +
669 le32_to_cpu(ir->index.entries_offset));
671 * Loop until we exceed valid memory (corruption case) or until we
672 * reach the last entry.
674 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
676 if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
677 sizeof(INDEX_ENTRY_HEADER) > index_end ||
678 (u8*)ie + le16_to_cpu(ie->key_length) >
682 * The last entry cannot contain a name. It can however contain
683 * a pointer to a child node in the B+tree so we just break out.
685 if (ie->flags & INDEX_ENTRY_END)
688 * If the current entry has a name type of POSIX, the name is
689 * case sensitive and not otherwise. This has the effect of us
690 * not being able to access any POSIX file names which collate
691 * after the non-POSIX one when they only differ in case, but
692 * anyone doing screwy stuff like that deserves to burn in
693 * hell... Doing that kind of stuff on NT4 actually causes
694 * corruption on the partition even when using SP6a and Linux
695 * is not involved at all.
697 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
700 * If the names match perfectly, we are done and return the
701 * mft reference of the inode (i.e. the inode number together
702 * with the sequence number for consistency checking. We
703 * convert it to cpu format before returning.
705 if (ntfs_are_names_equal(uname, uname_len,
706 (ntfschar*)&ie->key.file_name.file_name,
707 ie->key.file_name.file_name_length, ic,
708 vol->upcase, vol->upcase_len)) {
710 mref = le64_to_cpu(ie->data.dir.indexed_file);
711 ntfs_attr_put_search_ctx(ctx);
712 unmap_mft_record(dir_ni);
716 * Not a perfect match, need to do full blown collation so we
717 * know which way in the B+tree we have to go.
719 rc = ntfs_collate_names(uname, uname_len,
720 (ntfschar*)&ie->key.file_name.file_name,
721 ie->key.file_name.file_name_length, 1,
722 IGNORE_CASE, vol->upcase, vol->upcase_len);
724 * If uname collates before the name of the current entry, there
725 * is definitely no such name in this index but we might need to
726 * descend into the B+tree so we just break out of the loop.
730 /* The names are not equal, continue the search. */
734 * Names match with case insensitive comparison, now try the
735 * case sensitive comparison, which is required for proper
738 rc = ntfs_collate_names(uname, uname_len,
739 (ntfschar*)&ie->key.file_name.file_name,
740 ie->key.file_name.file_name_length, 1,
741 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
747 * Perfect match, this will never happen as the
748 * ntfs_are_names_equal() call will have gotten a match but we
749 * still treat it correctly.
754 * We have finished with this index without success. Check for the
755 * presence of a child node.
757 if (!(ie->flags & INDEX_ENTRY_NODE)) {
758 /* No child node, return -ENOENT. */
761 } /* Child node present, descend into it. */
762 /* Consistency check: Verify that an index allocation exists. */
763 if (!NInoIndexAllocPresent(dir_ni)) {
764 ntfs_error(sb, "No index allocation attribute but index entry "
765 "requires one. Directory inode 0x%lx is "
766 "corrupt or driver bug.", dir_ni->mft_no);
769 /* Get the starting vcn of the index_block holding the child node. */
770 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
771 ia_mapping = VFS_I(dir_ni)->i_mapping;
773 * We are done with the index root and the mft record. Release them,
774 * otherwise we deadlock with ntfs_map_page().
776 ntfs_attr_put_search_ctx(ctx);
777 unmap_mft_record(dir_ni);
780 descend_into_child_node:
782 * Convert vcn to index into the index allocation attribute in units
783 * of PAGE_SIZE and map the page cache page, reading it from
786 page = ntfs_map_page(ia_mapping, vcn <<
787 dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
789 ntfs_error(sb, "Failed to map directory index page, error %ld.",
795 kaddr = (u8*)page_address(page);
796 fast_descend_into_child_node:
797 /* Get to the index allocation block. */
798 ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
799 dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
801 if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
802 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
803 "inode 0x%lx or driver bug.", dir_ni->mft_no);
806 /* Catch multi sector transfer fixup errors. */
807 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
808 ntfs_error(sb, "Directory index record with vcn 0x%llx is "
809 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
810 (unsigned long long)vcn, dir_ni->mft_no);
813 if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
814 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
815 "different from expected VCN (0x%llx). "
816 "Directory inode 0x%lx is corrupt or driver "
817 "bug.", (unsigned long long)
818 sle64_to_cpu(ia->index_block_vcn),
819 (unsigned long long)vcn, dir_ni->mft_no);
822 if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
823 dir_ni->itype.index.block_size) {
824 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
825 "0x%lx has a size (%u) differing from the "
826 "directory specified size (%u). Directory "
827 "inode is corrupt or driver bug.",
828 (unsigned long long)vcn, dir_ni->mft_no,
829 le32_to_cpu(ia->index.allocated_size) + 0x18,
830 dir_ni->itype.index.block_size);
833 index_end = (u8*)ia + dir_ni->itype.index.block_size;
834 if (index_end > kaddr + PAGE_SIZE) {
835 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
836 "0x%lx crosses page boundary. Impossible! "
837 "Cannot access! This is probably a bug in the "
838 "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);
849 /* The first index entry. */
850 ie = (INDEX_ENTRY*)((u8*)&ia->index +
851 le32_to_cpu(ia->index.entries_offset));
853 * Iterate similar to above big loop but applied to index buffer, thus
854 * loop until we exceed valid memory (corruption case) or until we
855 * reach the last entry.
857 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
859 if ((u8*)ie < (u8*)ia || (u8*)ie +
860 sizeof(INDEX_ENTRY_HEADER) > index_end ||
861 (u8*)ie + le16_to_cpu(ie->key_length) >
863 ntfs_error(sb, "Index entry out of bounds in "
864 "directory inode 0x%lx.",
869 * The last entry cannot contain a name. It can however contain
870 * a pointer to a child node in the B+tree so we just break out.
872 if (ie->flags & INDEX_ENTRY_END)
875 * If the current entry has a name type of POSIX, the name is
876 * case sensitive and not otherwise. This has the effect of us
877 * not being able to access any POSIX file names which collate
878 * after the non-POSIX one when they only differ in case, but
879 * anyone doing screwy stuff like that deserves to burn in
880 * hell... Doing that kind of stuff on NT4 actually causes
881 * corruption on the partition even when using SP6a and Linux
882 * is not involved at all.
884 ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
887 * If the names match perfectly, we are done and return the
888 * mft reference of the inode (i.e. the inode number together
889 * with the sequence number for consistency checking. We
890 * convert it to cpu format before returning.
892 if (ntfs_are_names_equal(uname, uname_len,
893 (ntfschar*)&ie->key.file_name.file_name,
894 ie->key.file_name.file_name_length, ic,
895 vol->upcase, vol->upcase_len)) {
897 mref = le64_to_cpu(ie->data.dir.indexed_file);
899 ntfs_unmap_page(page);
903 * Not a perfect match, need to do full blown collation so we
904 * know which way in the B+tree we have to go.
906 rc = ntfs_collate_names(uname, uname_len,
907 (ntfschar*)&ie->key.file_name.file_name,
908 ie->key.file_name.file_name_length, 1,
909 IGNORE_CASE, vol->upcase, vol->upcase_len);
911 * If uname collates before the name of the current entry, there
912 * is definitely no such name in this index but we might need to
913 * descend into the B+tree so we just break out of the loop.
917 /* The names are not equal, continue the search. */
921 * Names match with case insensitive comparison, now try the
922 * case sensitive comparison, which is required for proper
925 rc = ntfs_collate_names(uname, uname_len,
926 (ntfschar*)&ie->key.file_name.file_name,
927 ie->key.file_name.file_name_length, 1,
928 CASE_SENSITIVE, vol->upcase, vol->upcase_len);
934 * Perfect match, this will never happen as the
935 * ntfs_are_names_equal() call will have gotten a match but we
936 * still treat it correctly.
941 * We have finished with this index buffer without success. Check for
942 * the presence of a child node.
944 if (ie->flags & INDEX_ENTRY_NODE) {
945 if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
946 ntfs_error(sb, "Index entry with child node found in "
947 "a leaf node in directory inode 0x%lx.",
951 /* Child node present, descend into it. */
953 vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
955 /* If vcn is in the same page cache page as old_vcn we
956 * recycle the mapped page. */
957 if (old_vcn << vol->cluster_size_bits >>
959 vol->cluster_size_bits >>
961 goto fast_descend_into_child_node;
963 ntfs_unmap_page(page);
964 goto descend_into_child_node;
966 ntfs_error(sb, "Negative child node vcn in directory inode "
967 "0x%lx.", dir_ni->mft_no);
970 /* No child node, return -ENOENT. */
971 ntfs_debug("Entry not found.");
975 ntfs_unmap_page(page);
980 ntfs_attr_put_search_ctx(ctx);
982 unmap_mft_record(dir_ni);
983 return ERR_MREF(err);
985 ntfs_error(sb, "Corrupt directory. Aborting lookup.");
992 * ntfs_filldir - ntfs specific filldir method
993 * @vol: current ntfs volume
994 * @ndir: ntfs inode of current directory
995 * @ia_page: page in which the index allocation buffer @ie is in resides
996 * @ie: current index entry
997 * @name: buffer to use for the converted name
998 * @actor: what to feed the entries to
1000 * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
1003 * If @ia_page is not NULL it is the locked page containing the index
1004 * allocation block containing the index entry @ie.
1006 * Note, we drop (and then reacquire) the page lock on @ia_page across the
1007 * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
1008 * since ntfs_lookup() will lock the same page. As an optimization, we do not
1009 * retake the lock if we are returning a non-zero value as ntfs_readdir()
1010 * would need to drop the lock immediately anyway.
1012 static inline int ntfs_filldir(ntfs_volume *vol,
1013 ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie,
1014 u8 *name, struct dir_context *actor)
1019 FILE_NAME_TYPE_FLAGS name_type;
1021 name_type = ie->key.file_name.file_name_type;
1022 if (name_type == FILE_NAME_DOS) {
1023 ntfs_debug("Skipping DOS name space entry.");
1026 if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
1027 ntfs_debug("Skipping root directory self reference entry.");
1030 if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
1031 !NVolShowSystemFiles(vol)) {
1032 ntfs_debug("Skipping system file.");
1035 name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
1036 ie->key.file_name.file_name_length, &name,
1037 NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
1038 if (name_len <= 0) {
1039 ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
1040 (long long)MREF_LE(ie->data.dir.indexed_file));
1043 if (ie->key.file_name.file_attributes &
1044 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
1048 mref = MREF_LE(ie->data.dir.indexed_file);
1050 * Drop the page lock otherwise we deadlock with NFS when it calls
1051 * ->lookup since ntfs_lookup() will lock the same page.
1054 unlock_page(ia_page);
1055 ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
1056 "0x%lx, DT_%s.", name, name_len, actor->pos, mref,
1057 dt_type == DT_DIR ? "DIR" : "REG");
1058 if (!dir_emit(actor, name, name_len, mref, dt_type))
1060 /* Relock the page but not if we are aborting ->readdir. */
1067 * We use the same basic approach as the old NTFS driver, i.e. we parse the
1068 * index root entries and then the index allocation entries that are marked
1069 * as in use in the index bitmap.
1071 * While this will return the names in random order this doesn't matter for
1072 * ->readdir but OTOH results in a faster ->readdir.
1074 * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
1075 * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
1078 * Locking: - Caller must hold i_mutex on the directory.
1079 * - Each page cache page in the index allocation mapping must be
1080 * locked whilst being accessed otherwise we may find a corrupt
1081 * page due to it being under ->writepage at the moment which
1082 * applies the mst protection fixups before writing out and then
1083 * removes them again after the write is complete after which it
1086 static int ntfs_readdir(struct file *file, struct dir_context *actor)
1088 s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
1090 struct inode *bmp_vi, *vdir = file_inode(file);
1091 struct super_block *sb = vdir->i_sb;
1092 ntfs_inode *ndir = NTFS_I(vdir);
1093 ntfs_volume *vol = NTFS_SB(sb);
1095 INDEX_ROOT *ir = NULL;
1097 INDEX_ALLOCATION *ia;
1099 int rc, err, ir_pos, cur_bmp_pos;
1100 struct address_space *ia_mapping, *bmp_mapping;
1101 struct page *bmp_page = NULL, *ia_page = NULL;
1102 u8 *kaddr, *bmp, *index_end;
1103 ntfs_attr_search_ctx *ctx;
1105 ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
1106 vdir->i_ino, actor->pos);
1108 /* Are we at end of dir yet? */
1109 i_size = i_size_read(vdir);
1110 if (actor->pos >= i_size + vol->mft_record_size)
1112 /* Emulate . and .. for all directories. */
1113 if (!dir_emit_dots(file, actor))
1118 * Allocate a buffer to store the current name being processed
1119 * converted to format determined by current NLS.
1121 name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
1122 if (unlikely(!name)) {
1126 /* Are we jumping straight into the index allocation attribute? */
1127 if (actor->pos >= vol->mft_record_size)
1128 goto skip_index_root;
1129 /* Get hold of the mft record for the directory. */
1130 m = map_mft_record(ndir);
1136 ctx = ntfs_attr_get_search_ctx(ndir, m);
1137 if (unlikely(!ctx)) {
1141 /* Get the offset into the index root attribute. */
1142 ir_pos = (s64)actor->pos;
1143 /* Find the index root attribute in the mft record. */
1144 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
1146 if (unlikely(err)) {
1147 ntfs_error(sb, "Index root attribute missing in directory "
1148 "inode 0x%lx.", vdir->i_ino);
1152 * Copy the index root attribute value to a buffer so that we can put
1153 * the search context and unmap the mft record before calling the
1154 * filldir() callback. We need to do this because of NFSd which calls
1155 * ->lookup() from its filldir callback() and this causes NTFS to
1156 * deadlock as ntfs_lookup() maps the mft record of the directory and
1157 * we have got it mapped here already. The only solution is for us to
1158 * unmap the mft record here so that a call to ntfs_lookup() is able to
1159 * map the mft record without deadlocking.
1161 rc = le32_to_cpu(ctx->attr->data.resident.value_length);
1162 ir = kmalloc(rc, GFP_NOFS);
1163 if (unlikely(!ir)) {
1167 /* Copy the index root value (it has been verified in read_inode). */
1168 memcpy(ir, (u8*)ctx->attr +
1169 le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
1170 ntfs_attr_put_search_ctx(ctx);
1171 unmap_mft_record(ndir);
1174 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1175 /* The first index entry. */
1176 ie = (INDEX_ENTRY*)((u8*)&ir->index +
1177 le32_to_cpu(ir->index.entries_offset));
1179 * Loop until we exceed valid memory (corruption case) or until we
1180 * reach the last entry or until filldir tells us it has had enough
1181 * or signals an error (both covered by the rc test).
1183 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1184 ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir);
1185 /* Bounds checks. */
1186 if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
1187 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1188 (u8*)ie + le16_to_cpu(ie->key_length) >
1191 /* The last entry cannot contain a name. */
1192 if (ie->flags & INDEX_ENTRY_END)
1194 /* Skip index root entry if continuing previous readdir. */
1195 if (ir_pos > (u8*)ie - (u8*)ir)
1197 /* Advance the position even if going to skip the entry. */
1198 actor->pos = (u8*)ie - (u8*)ir;
1199 /* Submit the name to the filldir callback. */
1200 rc = ntfs_filldir(vol, ndir, NULL, ie, name, actor);
1206 /* We are done with the index root and can free the buffer. */
1209 /* If there is no index allocation attribute we are finished. */
1210 if (!NInoIndexAllocPresent(ndir))
1212 /* Advance fpos to the beginning of the index allocation. */
1213 actor->pos = vol->mft_record_size;
1217 /* Get the offset into the index allocation attribute. */
1218 ia_pos = (s64)actor->pos - vol->mft_record_size;
1219 ia_mapping = vdir->i_mapping;
1220 ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino);
1221 bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
1222 if (IS_ERR(bmp_vi)) {
1223 ntfs_error(sb, "Failed to get bitmap attribute.");
1224 err = PTR_ERR(bmp_vi);
1227 bmp_mapping = bmp_vi->i_mapping;
1228 /* Get the starting bitmap bit position and sanity check it. */
1229 bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
1230 if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
1231 ntfs_error(sb, "Current index allocation position exceeds "
1232 "index bitmap size.");
1235 /* Get the starting bit position in the current bitmap page. */
1236 cur_bmp_pos = bmp_pos & ((PAGE_SIZE * 8) - 1);
1237 bmp_pos &= ~(u64)((PAGE_SIZE * 8) - 1);
1239 ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
1240 (unsigned long long)bmp_pos >> (3 + PAGE_SHIFT),
1241 (unsigned long long)bmp_pos &
1242 (unsigned long long)((PAGE_SIZE * 8) - 1));
1243 bmp_page = ntfs_map_page(bmp_mapping,
1244 bmp_pos >> (3 + PAGE_SHIFT));
1245 if (IS_ERR(bmp_page)) {
1246 ntfs_error(sb, "Reading index bitmap failed.");
1247 err = PTR_ERR(bmp_page);
1251 bmp = (u8*)page_address(bmp_page);
1252 /* Find next index block in use. */
1253 while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
1254 find_next_index_buffer:
1257 * If we have reached the end of the bitmap page, get the next
1258 * page, and put away the old one.
1260 if (unlikely((cur_bmp_pos >> 3) >= PAGE_SIZE)) {
1261 ntfs_unmap_page(bmp_page);
1262 bmp_pos += PAGE_SIZE * 8;
1264 goto get_next_bmp_page;
1266 /* If we have reached the end of the bitmap, we are done. */
1267 if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
1269 ia_pos = (bmp_pos + cur_bmp_pos) <<
1270 ndir->itype.index.block_size_bits;
1272 ntfs_debug("Handling index buffer 0x%llx.",
1273 (unsigned long long)bmp_pos + cur_bmp_pos);
1274 /* If the current index buffer is in the same page we reuse the page. */
1275 if ((prev_ia_pos & (s64)PAGE_MASK) !=
1276 (ia_pos & (s64)PAGE_MASK)) {
1277 prev_ia_pos = ia_pos;
1278 if (likely(ia_page != NULL)) {
1279 unlock_page(ia_page);
1280 ntfs_unmap_page(ia_page);
1283 * Map the page cache page containing the current ia_pos,
1284 * reading it from disk if necessary.
1286 ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_SHIFT);
1287 if (IS_ERR(ia_page)) {
1288 ntfs_error(sb, "Reading index allocation data failed.");
1289 err = PTR_ERR(ia_page);
1294 kaddr = (u8*)page_address(ia_page);
1296 /* Get the current index buffer. */
1297 ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_MASK &
1298 ~(s64)(ndir->itype.index.block_size - 1)));
1299 /* Bounds checks. */
1300 if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE)) {
1301 ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
1302 "inode 0x%lx or driver bug.", vdir->i_ino);
1305 /* Catch multi sector transfer fixup errors. */
1306 if (unlikely(!ntfs_is_indx_record(ia->magic))) {
1307 ntfs_error(sb, "Directory index record with vcn 0x%llx is "
1308 "corrupt. Corrupt inode 0x%lx. Run chkdsk.",
1309 (unsigned long long)ia_pos >>
1310 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1313 if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
1314 ~(s64)(ndir->itype.index.block_size - 1)) >>
1315 ndir->itype.index.vcn_size_bits)) {
1316 ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
1317 "different from expected VCN (0x%llx). "
1318 "Directory inode 0x%lx is corrupt or driver "
1319 "bug. ", (unsigned long long)
1320 sle64_to_cpu(ia->index_block_vcn),
1321 (unsigned long long)ia_pos >>
1322 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1325 if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
1326 ndir->itype.index.block_size)) {
1327 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1328 "0x%lx has a size (%u) differing from the "
1329 "directory specified size (%u). Directory "
1330 "inode is corrupt or driver bug.",
1331 (unsigned long long)ia_pos >>
1332 ndir->itype.index.vcn_size_bits, vdir->i_ino,
1333 le32_to_cpu(ia->index.allocated_size) + 0x18,
1334 ndir->itype.index.block_size);
1337 index_end = (u8*)ia + ndir->itype.index.block_size;
1338 if (unlikely(index_end > kaddr + PAGE_SIZE)) {
1339 ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
1340 "0x%lx crosses page boundary. Impossible! "
1341 "Cannot access! This is probably a bug in the "
1342 "driver.", (unsigned long long)ia_pos >>
1343 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1346 ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
1347 index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
1348 if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
1349 ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
1350 "inode 0x%lx exceeds maximum size.",
1351 (unsigned long long)ia_pos >>
1352 ndir->itype.index.vcn_size_bits, vdir->i_ino);
1355 /* The first index entry in this index buffer. */
1356 ie = (INDEX_ENTRY*)((u8*)&ia->index +
1357 le32_to_cpu(ia->index.entries_offset));
1359 * Loop until we exceed valid memory (corruption case) or until we
1360 * reach the last entry or until filldir tells us it has had enough
1361 * or signals an error (both covered by the rc test).
1363 for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
1364 ntfs_debug("In index allocation, offset 0x%llx.",
1365 (unsigned long long)ia_start +
1366 (unsigned long long)((u8*)ie - (u8*)ia));
1367 /* Bounds checks. */
1368 if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
1369 sizeof(INDEX_ENTRY_HEADER) > index_end ||
1370 (u8*)ie + le16_to_cpu(ie->key_length) >
1373 /* The last entry cannot contain a name. */
1374 if (ie->flags & INDEX_ENTRY_END)
1376 /* Skip index block entry if continuing previous readdir. */
1377 if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
1379 /* Advance the position even if going to skip the entry. */
1380 actor->pos = (u8*)ie - (u8*)ia +
1381 (sle64_to_cpu(ia->index_block_vcn) <<
1382 ndir->itype.index.vcn_size_bits) +
1383 vol->mft_record_size;
1385 * Submit the name to the @filldir callback. Note,
1386 * ntfs_filldir() drops the lock on @ia_page but it retakes it
1387 * before returning, unless a non-zero value is returned in
1388 * which case the page is left unlocked.
1390 rc = ntfs_filldir(vol, ndir, ia_page, ie, name, actor);
1392 /* @ia_page is already unlocked in this case. */
1393 ntfs_unmap_page(ia_page);
1394 ntfs_unmap_page(bmp_page);
1399 goto find_next_index_buffer;
1402 unlock_page(ia_page);
1403 ntfs_unmap_page(ia_page);
1405 ntfs_unmap_page(bmp_page);
1408 /* We are finished, set fpos to EOD. */
1409 actor->pos = i_size + vol->mft_record_size;
1415 ntfs_unmap_page(bmp_page);
1420 unlock_page(ia_page);
1421 ntfs_unmap_page(ia_page);
1426 ntfs_attr_put_search_ctx(ctx);
1428 unmap_mft_record(ndir);
1431 ntfs_debug("Failed. Returning error code %i.", -err);
1436 * ntfs_dir_open - called when an inode is about to be opened
1437 * @vi: inode to be opened
1438 * @filp: file structure describing the inode
1440 * Limit directory size to the page cache limit on architectures where unsigned
1441 * long is 32-bits. This is the most we can do for now without overflowing the
1442 * page cache page index. Doing it this way means we don't run into problems
1443 * because of existing too large directories. It would be better to allow the
1444 * user to read the accessible part of the directory but I doubt very much
1445 * anyone is going to hit this check on a 32-bit architecture, so there is no
1446 * point in adding the extra complexity required to support this.
1448 * On 64-bit architectures, the check is hopefully optimized away by the
1451 static int ntfs_dir_open(struct inode *vi, struct file *filp)
1453 if (sizeof(unsigned long) < 8) {
1454 if (i_size_read(vi) > MAX_LFS_FILESIZE)
1463 * ntfs_dir_fsync - sync a directory to disk
1464 * @filp: directory to be synced
1465 * @dentry: dentry describing the directory to sync
1466 * @datasync: if non-zero only flush user data and not metadata
1468 * Data integrity sync of a directory to disk. Used for fsync, fdatasync, and
1469 * msync system calls. This function is based on file.c::ntfs_file_fsync().
1471 * Write the mft record and all associated extent mft records as well as the
1472 * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
1474 * If @datasync is true, we do not wait on the inode(s) to be written out
1475 * but we always wait on the page cache pages to be written out.
1477 * Note: In the past @filp could be NULL so we ignore it as we don't need it
1480 * Locking: Caller must hold i_mutex on the inode.
1482 * TODO: We should probably also write all attribute/index inodes associated
1483 * with this inode but since we have no simple way of getting to them we ignore
1484 * this problem for now. We do write the $BITMAP attribute if it is present
1485 * which is the important one for a directory so things are not too bad.
1487 static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
1490 struct inode *bmp_vi, *vi = filp->f_mapping->host;
1494 ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
1496 err = file_write_and_wait_range(filp, start, end);
1501 BUG_ON(!S_ISDIR(vi->i_mode));
1502 /* If the bitmap attribute inode is in memory sync it, too. */
1503 na.mft_no = vi->i_ino;
1504 na.type = AT_BITMAP;
1507 bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na);
1509 write_inode_now(bmp_vi, !datasync);
1512 ret = __ntfs_write_inode(vi, 1);
1513 write_inode_now(vi, !datasync);
1514 err = sync_blockdev(vi->i_sb->s_bdev);
1515 if (unlikely(err && !ret))
1518 ntfs_debug("Done.");
1520 ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
1521 "%u.", datasync ? "data" : "", vi->i_ino, -ret);
1526 #endif /* NTFS_RW */
1528 WRAP_DIR_ITER(ntfs_readdir) // FIXME!
1529 const struct file_operations ntfs_dir_ops = {
1530 .llseek = generic_file_llseek, /* Seek inside directory. */
1531 .read = generic_read_dir, /* Return -EISDIR. */
1532 .iterate_shared = shared_ntfs_readdir, /* Read directory contents. */
1534 .fsync = ntfs_dir_fsync, /* Sync a directory to disk. */
1535 #endif /* NTFS_RW */
1536 /*.ioctl = ,*/ /* Perform function on the
1537 mounted filesystem. */
1538 .open = ntfs_dir_open, /* Open directory. */