📄 dir.c
字号:
unmap_mft_record(dir_ni); m = NULL; ctx = NULL;descend_into_child_node: /* * Convert vcn to index into the index allocation attribute in units * of PAGE_CACHE_SIZE and map the page cache page, reading it from * disk if necessary. */ page = ntfs_map_page(ia_mapping, vcn << dir_ni->itype.index.vcn_size_bits >> PAGE_CACHE_SHIFT); if (IS_ERR(page)) { ntfs_error(sb, "Failed to map directory index page, error %ld.", -PTR_ERR(page)); err = PTR_ERR(page); goto err_out; } lock_page(page); kaddr = (u8*)page_address(page);fast_descend_into_child_node: /* Get to the index allocation block. */ ia = (INDEX_ALLOCATION*)(kaddr + ((vcn << dir_ni->itype.index.vcn_size_bits) & ~PAGE_CACHE_MASK)); /* Bounds checks. */ if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_CACHE_SIZE) { ntfs_error(sb, "Out of bounds check failed. Corrupt directory " "inode 0x%lx or driver bug.", dir_ni->mft_no); goto unm_err_out; } /* Catch multi sector transfer fixup errors. */ if (unlikely(!ntfs_is_indx_record(ia->magic))) { ntfs_error(sb, "Directory index record with vcn 0x%llx is " "corrupt. Corrupt inode 0x%lx. Run chkdsk.", (unsigned long long)vcn, dir_ni->mft_no); goto unm_err_out; } if (sle64_to_cpu(ia->index_block_vcn) != vcn) { ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is " "different from expected VCN (0x%llx). " "Directory inode 0x%lx is corrupt or driver " "bug.", (unsigned long long) sle64_to_cpu(ia->index_block_vcn), (unsigned long long)vcn, dir_ni->mft_no); goto unm_err_out; } if (le32_to_cpu(ia->index.allocated_size) + 0x18 != dir_ni->itype.index.block_size) { ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " "0x%lx has a size (%u) differing from the " "directory specified size (%u). Directory " "inode is corrupt or driver bug.", (unsigned long long)vcn, dir_ni->mft_no, le32_to_cpu(ia->index.allocated_size) + 0x18, dir_ni->itype.index.block_size); goto unm_err_out; } index_end = (u8*)ia + dir_ni->itype.index.block_size; if (index_end > kaddr + PAGE_CACHE_SIZE) { ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode " "0x%lx crosses page boundary. Impossible! " "Cannot access! This is probably a bug in the " "driver.", (unsigned long long)vcn, dir_ni->mft_no); goto unm_err_out; } index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); if (index_end > (u8*)ia + dir_ni->itype.index.block_size) { ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory " "inode 0x%lx exceeds maximum size.", (unsigned long long)vcn, dir_ni->mft_no); goto unm_err_out; } /* The first index entry. */ ie = (INDEX_ENTRY*)((u8*)&ia->index + le32_to_cpu(ia->index.entries_offset)); /* * Iterate similar to above big loop but applied to index buffer, thus * loop until we exceed valid memory (corruption case) or until we * reach the last entry. */ for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { /* Bounds check. */ if ((u8*)ie < (u8*)ia || (u8*)ie + sizeof(INDEX_ENTRY_HEADER) > index_end || (u8*)ie + le16_to_cpu(ie->key_length) > index_end) { ntfs_error(sb, "Index entry out of bounds in " "directory inode 0x%lx.", dir_ni->mft_no); goto unm_err_out; } /* * The last entry cannot contain a name. It can however contain * a pointer to a child node in the B+tree so we just break out. */ if (ie->flags & INDEX_ENTRY_END) break; /* * If the current entry has a name type of POSIX, the name is * case sensitive and not otherwise. This has the effect of us * not being able to access any POSIX file names which collate * after the non-POSIX one when they only differ in case, but * anyone doing screwy stuff like that deserves to burn in * hell... Doing that kind of stuff on NT4 actually causes * corruption on the partition even when using SP6a and Linux * is not involved at all. */ ic = ie->key.file_name.file_name_type ? IGNORE_CASE : CASE_SENSITIVE; /* * If the names match perfectly, we are done and return the * mft reference of the inode (i.e. the inode number together * with the sequence number for consistency checking. We * convert it to cpu format before returning. */ if (ntfs_are_names_equal(uname, uname_len, (ntfschar*)&ie->key.file_name.file_name, ie->key.file_name.file_name_length, ic, vol->upcase, vol->upcase_len)) {found_it2: mref = le64_to_cpu(ie->data.dir.indexed_file); unlock_page(page); ntfs_unmap_page(page); return mref; } /* * Not a perfect match, need to do full blown collation so we * know which way in the B+tree we have to go. */ rc = ntfs_collate_names(uname, uname_len, (ntfschar*)&ie->key.file_name.file_name, ie->key.file_name.file_name_length, 1, IGNORE_CASE, vol->upcase, vol->upcase_len); /* * If uname collates before the name of the current entry, there * is definitely no such name in this index but we might need to * descend into the B+tree so we just break out of the loop. */ if (rc == -1) break; /* The names are not equal, continue the search. */ if (rc) continue; /* * Names match with case insensitive comparison, now try the * case sensitive comparison, which is required for proper * collation. */ rc = ntfs_collate_names(uname, uname_len, (ntfschar*)&ie->key.file_name.file_name, ie->key.file_name.file_name_length, 1, CASE_SENSITIVE, vol->upcase, vol->upcase_len); if (rc == -1) break; if (rc) continue; /* * Perfect match, this will never happen as the * ntfs_are_names_equal() call will have gotten a match but we * still treat it correctly. */ goto found_it2; } /* * We have finished with this index buffer without success. Check for * the presence of a child node. */ if (ie->flags & INDEX_ENTRY_NODE) { if ((ia->index.flags & NODE_MASK) == LEAF_NODE) { ntfs_error(sb, "Index entry with child node found in " "a leaf node in directory inode 0x%lx.", dir_ni->mft_no); goto unm_err_out; } /* Child node present, descend into it. */ old_vcn = vcn; vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8); if (vcn >= 0) { /* If vcn is in the same page cache page as old_vcn we * recycle the mapped page. */ if (old_vcn << vol->cluster_size_bits >> PAGE_CACHE_SHIFT == vcn << vol->cluster_size_bits >> PAGE_CACHE_SHIFT) goto fast_descend_into_child_node; unlock_page(page); ntfs_unmap_page(page); goto descend_into_child_node; } ntfs_error(sb, "Negative child node vcn in directory inode " "0x%lx.", dir_ni->mft_no); goto unm_err_out; } /* No child node, return -ENOENT. */ ntfs_debug("Entry not found."); err = -ENOENT;unm_err_out: unlock_page(page); ntfs_unmap_page(page);err_out: if (!err) err = -EIO; if (ctx) ntfs_attr_put_search_ctx(ctx); if (m) unmap_mft_record(dir_ni); return ERR_MREF(err);dir_err_out: ntfs_error(sb, "Corrupt directory. Aborting lookup."); goto err_out;}#endif/** * ntfs_filldir - ntfs specific filldir method * @vol: current ntfs volume * @fpos: position in the directory * @ndir: ntfs inode of current directory * @ia_page: page in which the index allocation buffer @ie is in resides * @ie: current index entry * @name: buffer to use for the converted name * @dirent: vfs filldir callback context * @filldir: vfs filldir callback * * Convert the Unicode @name to the loaded NLS and pass it to the @filldir * callback. * * If @ia_page is not NULL it is the locked page containing the index * allocation block containing the index entry @ie. * * Note, we drop (and then reacquire) the page lock on @ia_page across the * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup * since ntfs_lookup() will lock the same page. As an optimization, we do not * retake the lock if we are returning a non-zero value as ntfs_readdir() * would need to drop the lock immediately anyway. */static inline int ntfs_filldir(ntfs_volume *vol, loff_t fpos, ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie, u8 *name, void *dirent, filldir_t filldir){ unsigned long mref; int name_len, rc; unsigned dt_type; FILE_NAME_TYPE_FLAGS name_type; name_type = ie->key.file_name.file_name_type; if (name_type == FILE_NAME_DOS) { ntfs_debug("Skipping DOS name space entry."); return 0; } if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) { ntfs_debug("Skipping root directory self reference entry."); return 0; } if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user && !NVolShowSystemFiles(vol)) { ntfs_debug("Skipping system file."); return 0; } name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name, ie->key.file_name.file_name_length, &name, NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1); if (name_len <= 0) { ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.", (long long)MREF_LE(ie->data.dir.indexed_file)); return 0; } if (ie->key.file_name.file_attributes & FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT) dt_type = DT_DIR; else dt_type = DT_REG; mref = MREF_LE(ie->data.dir.indexed_file); /* * Drop the page lock otherwise we deadlock with NFS when it calls * ->lookup since ntfs_lookup() will lock the same page. */ if (ia_page) unlock_page(ia_page); ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode " "0x%lx, DT_%s.", name, name_len, fpos, mref, dt_type == DT_DIR ? "DIR" : "REG"); rc = filldir(dirent, name, name_len, fpos, mref, dt_type); /* Relock the page but not if we are aborting ->readdir. */ if (!rc && ia_page) lock_page(ia_page); return rc;}/* * We use the same basic approach as the old NTFS driver, i.e. we parse the * index root entries and then the index allocation entries that are marked * as in use in the index bitmap. * * While this will return the names in random order this doesn't matter for * ->readdir but OTOH results in a faster ->readdir. * * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS * parts (e.g. ->f_pos and ->i_size, and it also protects against directory * modifications). * * Locking: - Caller must hold i_mutex on the directory. * - Each page cache page in the index allocation mapping must be * locked whilst being accessed otherwise we may find a corrupt * page due to it being under ->writepage at the moment which * applies the mst protection fixups before writing out and then * removes them again after the write is complete after which it * unlocks the page. */static int ntfs_readdir(struct file *filp, void *dirent, filldir_t filldir){ s64 ia_pos, ia_start, prev_ia_pos, bmp_pos; loff_t fpos, i_size; struct inode *bmp_vi, *vdir = filp->f_path.dentry->d_inode; struct super_block *sb = vdir->i_sb; ntfs_inode *ndir = NTFS_I(vdir); ntfs_volume *vol = NTFS_SB(sb); MFT_RECORD *m; INDEX_ROOT *ir = NULL; INDEX_ENTRY *ie; INDEX_ALLOCATION *ia; u8 *name = NULL; int rc, err, ir_pos, cur_bmp_pos; struct address_space *ia_mapping, *bmp_mapping; struct page *bmp_page = NULL, *ia_page = NULL; u8 *kaddr, *bmp, *index_end; ntfs_attr_search_ctx *ctx; fpos = filp->f_pos; ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.", vdir->i_ino, fpos); rc = err = 0; /* Are we at end of dir yet? */ i_size = i_size_read(vdir); if (fpos >= i_size + vol->mft_record_size) goto done; /* Emulate . and .. for all directories. */ if (!fpos) { ntfs_debug("Calling filldir for . with len 1, fpos 0x0, " "inode 0x%lx, DT_DIR.", vdir->i_ino); rc = filldir(dirent, ".", 1, fpos, vdir->i_ino, DT_DIR); if (rc) goto done; fpos++; } if (fpos == 1) { ntfs_debug("Calling filldir for .. with len 2, fpos 0x1, " "inode 0x%lx, DT_DIR.", (unsigned long)parent_ino(filp->f_path.dentry)); rc = filldir(dirent, "..", 2, fpos, parent_ino(filp->f_path.dentry), DT_DIR); if (rc) goto done; fpos++; } m = NULL; ctx = NULL; /* * Allocate a buffer to store the current name being processed * converted to format determined by current NLS. */ name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS); if (unlikely(!name)) { err = -ENOMEM; goto err_out; } /* Are we jumping straight into the index allocation attribute? */ if (fpos >= vol->mft_record_size) goto skip_index_root; /* Get hold of the mft record for the directory. */ m = map_mft_record(ndir); if (IS_ERR(m)) { err = PTR_ERR(m); m = NULL; goto err_out; } ctx = ntfs_attr_get_search_ctx(ndir, m); if (unlikely(!ctx)) { err = -ENOMEM; goto err_out; } /* Get the offset into the index root attribute. */ ir_pos = (s64)fpos; /* Find the index root attribute in the mft record. */ err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL, 0, ctx); if (unlikely(err)) { ntfs_error(sb, "Index root attribute missing in directory " "inode 0x%lx.", vdir->i_ino); goto err_out; } /* * Copy the index root attribute value to a buffer so that we can put
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -