📄 inode.c
字号:
if (a->flags & ATTR_IS_SPARSE) NInoSetSparse(ni); ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset)); ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length); if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) { ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is " "corrupt."); goto unm_err_out; } index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); if (index_end > ir_end) { ntfs_error(vi->i_sb, "Directory index is corrupt."); goto unm_err_out; } if (ir->type != AT_FILE_NAME) { ntfs_error(vi->i_sb, "Indexed attribute is not " "$FILE_NAME."); goto unm_err_out; } if (ir->collation_rule != COLLATION_FILE_NAME) { ntfs_error(vi->i_sb, "Index collation rule is not " "COLLATION_FILE_NAME."); goto unm_err_out; } ni->itype.index.collation_rule = ir->collation_rule; ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); if (ni->itype.index.block_size & (ni->itype.index.block_size - 1)) { ntfs_error(vi->i_sb, "Index block size (%u) is not a " "power of two.", ni->itype.index.block_size); goto unm_err_out; } if (ni->itype.index.block_size > PAGE_CACHE_SIZE) { ntfs_error(vi->i_sb, "Index block size (%u) > " "PAGE_CACHE_SIZE (%ld) is not " "supported. Sorry.", ni->itype.index.block_size, PAGE_CACHE_SIZE); err = -EOPNOTSUPP; goto unm_err_out; } if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { ntfs_error(vi->i_sb, "Index block size (%u) < " "NTFS_BLOCK_SIZE (%i) is not " "supported. Sorry.", ni->itype.index.block_size, NTFS_BLOCK_SIZE); err = -EOPNOTSUPP; goto unm_err_out; } ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1; /* Determine the size of a vcn in the directory index. */ if (vol->cluster_size <= ni->itype.index.block_size) { ni->itype.index.vcn_size = vol->cluster_size; ni->itype.index.vcn_size_bits = vol->cluster_size_bits; } else { ni->itype.index.vcn_size = vol->sector_size; ni->itype.index.vcn_size_bits = vol->sector_size_bits; } /* Setup the index allocation attribute, even if not present. */ NInoSetMstProtected(ni); ni->type = AT_INDEX_ALLOCATION; ni->name = I30; ni->name_len = 4; if (!(ir->index.flags & LARGE_INDEX)) { /* No index allocation. */ vi->i_size = ni->initialized_size = ni->allocated_size = 0; /* We are done with the mft record, so we release it. */ ntfs_attr_put_search_ctx(ctx); unmap_mft_record(ni); m = NULL; ctx = NULL; goto skip_large_dir_stuff; } /* LARGE_INDEX: Index allocation present. Setup state. */ NInoSetIndexAllocPresent(ni); /* Find index allocation attribute. */ ntfs_attr_reinit_search_ctx(ctx); err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4, CASE_SENSITIVE, 0, NULL, 0, ctx); if (unlikely(err)) { if (err == -ENOENT) ntfs_error(vi->i_sb, "$INDEX_ALLOCATION " "attribute is not present but " "$INDEX_ROOT indicated it is."); else ntfs_error(vi->i_sb, "Failed to lookup " "$INDEX_ALLOCATION " "attribute."); goto unm_err_out; } a = ctx->attr; if (!a->non_resident) { ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " "is resident."); goto unm_err_out; } /* * Ensure the attribute name is placed before the mapping pairs * array. */ if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= le16_to_cpu( a->data.non_resident.mapping_pairs_offset)))) { ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name " "is placed after the mapping pairs " "array."); goto unm_err_out; } if (a->flags & ATTR_IS_ENCRYPTED) { ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " "is encrypted."); goto unm_err_out; } if (a->flags & ATTR_IS_SPARSE) { ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " "is sparse."); goto unm_err_out; } if (a->flags & ATTR_COMPRESSION_MASK) { ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " "is compressed."); goto unm_err_out; } if (a->data.non_resident.lowest_vcn) { ntfs_error(vi->i_sb, "First extent of " "$INDEX_ALLOCATION attribute has non " "zero lowest_vcn."); goto unm_err_out; } vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); ni->initialized_size = sle64_to_cpu( a->data.non_resident.initialized_size); ni->allocated_size = sle64_to_cpu( a->data.non_resident.allocated_size); /* * We are done with the mft record, so we release it. Otherwise * we would deadlock in ntfs_attr_iget(). */ ntfs_attr_put_search_ctx(ctx); unmap_mft_record(ni); m = NULL; ctx = NULL; /* Get the index bitmap attribute inode. */ bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4); if (IS_ERR(bvi)) { ntfs_error(vi->i_sb, "Failed to get bitmap attribute."); err = PTR_ERR(bvi); goto unm_err_out; } bni = NTFS_I(bvi); if (NInoCompressed(bni) || NInoEncrypted(bni) || NInoSparse(bni)) { ntfs_error(vi->i_sb, "$BITMAP attribute is compressed " "and/or encrypted and/or sparse."); goto iput_unm_err_out; } /* Consistency check bitmap size vs. index allocation size. */ bvi_size = i_size_read(bvi); if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) { ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) " "for index allocation (0x%llx).", bvi_size << 3, vi->i_size); goto iput_unm_err_out; } /* No longer need the bitmap attribute inode. */ iput(bvi);skip_large_dir_stuff: /* Setup the operations for this inode. */ vi->i_op = &ntfs_dir_inode_ops; vi->i_fop = &ntfs_dir_ops; } else { /* It is a file. */ ntfs_attr_reinit_search_ctx(ctx); /* Setup the data attribute, even if not present. */ ni->type = AT_DATA; ni->name = NULL; ni->name_len = 0; /* Find first extent of the unnamed data attribute. */ err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx); if (unlikely(err)) { vi->i_size = ni->initialized_size = ni->allocated_size = 0; if (err != -ENOENT) { ntfs_error(vi->i_sb, "Failed to lookup $DATA " "attribute."); goto unm_err_out; } /* * FILE_Secure does not have an unnamed $DATA * attribute, so we special case it here. */ if (vi->i_ino == FILE_Secure) goto no_data_attr_special_case; /* * Most if not all the system files in the $Extend * system directory do not have unnamed data * attributes so we need to check if the parent * directory of the file is FILE_Extend and if it is * ignore this error. To do this we need to get the * name of this inode from the mft record as the name * contains the back reference to the parent directory. */ if (ntfs_is_extended_system_file(ctx) > 0) goto no_data_attr_special_case; // FIXME: File is corrupt! Hot-fix with empty data // attribute if recovery option is set. ntfs_error(vi->i_sb, "$DATA attribute is missing."); goto unm_err_out; } a = ctx->attr; /* Setup the state. */ if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { if (a->flags & ATTR_COMPRESSION_MASK) { NInoSetCompressed(ni); if (vol->cluster_size > 4096) { ntfs_error(vi->i_sb, "Found " "compressed data but " "compression is " "disabled due to " "cluster size (%i) > " "4kiB.", vol->cluster_size); goto unm_err_out; } if ((a->flags & ATTR_COMPRESSION_MASK) != ATTR_IS_COMPRESSED) { ntfs_error(vi->i_sb, "Found unknown " "compression method " "or corrupt file."); goto unm_err_out; } } if (a->flags & ATTR_IS_SPARSE) NInoSetSparse(ni); } if (a->flags & ATTR_IS_ENCRYPTED) { if (NInoCompressed(ni)) { ntfs_error(vi->i_sb, "Found encrypted and " "compressed data."); goto unm_err_out; } NInoSetEncrypted(ni); } if (a->non_resident) { NInoSetNonResident(ni); if (NInoCompressed(ni) || NInoSparse(ni)) { if (NInoCompressed(ni) && a->data.non_resident. compression_unit != 4) { ntfs_error(vi->i_sb, "Found " "non-standard " "compression unit (%u " "instead of 4). " "Cannot handle this.", a->data.non_resident. compression_unit); err = -EOPNOTSUPP; goto unm_err_out; } if (a->data.non_resident.compression_unit) { ni->itype.compressed.block_size = 1U << (a->data.non_resident. compression_unit + vol->cluster_size_bits); ni->itype.compressed.block_size_bits = ffs(ni->itype. compressed. block_size) - 1; ni->itype.compressed.block_clusters = 1U << a->data. non_resident. compression_unit; } else { ni->itype.compressed.block_size = 0; ni->itype.compressed.block_size_bits = 0; ni->itype.compressed.block_clusters = 0; } ni->itype.compressed.size = sle64_to_cpu( a->data.non_resident. compressed_size); } if (a->data.non_resident.lowest_vcn) { ntfs_error(vi->i_sb, "First extent of $DATA " "attribute has non zero " "lowest_vcn."); goto unm_err_out; } vi->i_size = sle64_to_cpu( a->data.non_resident.data_size); ni->initialized_size = sle64_to_cpu( a->data.non_resident.initialized_size); ni->allocated_size = sle64_to_cpu( a->data.non_resident.allocated_size); } else { /* Resident attribute. */ vi->i_size = ni->initialized_size = le32_to_cpu( a->data.resident.value_length); ni->allocated_size = le32_to_cpu(a->length) - le16_to_cpu( a->data.resident.value_offset); if (vi->i_size > ni->allocated_size) { ntfs_error(vi->i_sb, "Resident data attribute " "is corrupt (size exceeds " "allocation)."); goto unm_err_out; } }no_data_attr_special_case: /* We are done with the mft record, so we release it. */ ntfs_attr_put_search_ctx(ctx); unmap_mft_record(ni); m = NULL; ctx = NULL; /* Setup the operations for this inode. */ vi->i_op = &ntfs_file_inode_ops; vi->i_fop = &ntfs_file_ops; } if (NInoMstProtected(ni)) vi->i_mapping->a_ops = &ntfs_mst_aops; else vi->i_mapping->a_ops = &ntfs_aops; /* * The number of 512-byte blocks used on disk (for stat). This is in so * far inaccurate as it doesn't account for any named streams or other * special non-resident attributes, but that is how Windows works, too, * so we are at least consistent with Windows, if not entirely * consistent with the Linux Way. Doing it the Linux Way would cause a * significant slowdown as it would involve iterating over all * attributes in the mft record and adding the allocated/compressed * sizes of all non-resident attributes present to give us the Linux * correct size that should go into i_blocks (after division by 512). */ if (S_ISREG(vi->i_mode) && (NInoCompressed(ni) || NInoSparse(ni))) vi->i_blocks = ni->itype.compressed.size >> 9; else vi->i_blocks = ni->allocated_size >> 9; ntfs_debug("Done."); return 0;iput_unm_err_out: iput(bvi);unm_err_out: if (!err) err = -EIO; if (ctx) ntfs_attr_put_search_ctx(ctx); if (m) unmap_mft_record(ni);err_out: ntfs_error(vol->sb, "Failed with error code %i. Marking corrupt " "inode 0x%lx as bad. Run chkdsk.", err, vi->i_ino); make_bad_inode(vi); if (err != -EOPNOTSUPP && err != -ENOMEM) NVolSetErrors(vol); return err;}/** * ntfs_read_locked_attr_inode - read an attribute inode from its base inode * @base_vi: base inode * @vi: attribute inode to read * * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the * attribute inode described by @vi into memory from the base mft record * described by @base_ni. * * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for * reading and looks up the attribute described by @vi before setting up the * necessary fields in @vi as well as initializing the ntfs inode. * * Q: What locks are held when the function is called? * A: i_state has I_LOCK set, hence the inode is locked, also * i_count is set to 1, so it is not going to go away * * Return 0 on success and -errno on error. In the error case, the inode will * have had make_bad_inode() executed on it. * * Note this cannot be called for AT_INDEX_ALLOCATION. */static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi){ ntfs_volume *vol = NTFS_SB(vi->i_sb); ntfs_inode *ni, *base_ni; MFT_RECORD *m; ATTR_RECORD *a; ntfs_attr_search_ctx *ctx; int err = 0; ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); ntfs_init_big_inode(vi); ni = NTFS_I(vi); base_ni = NTFS_I(base_vi); /* Just mirror the values from the base inode. */ vi->i_version = base_vi->i_version; vi->i_uid = base_vi->i_uid; vi->i_gid = base_vi->i_gid; vi->i_nlink = base_vi->i_nlink; vi->i_mtime = base_vi->i_mtime; vi->i_ctime = base_vi->i_ctime; vi->i_atime = base_vi->i_atime; vi->i_generation = ni->seq_no = base_ni->seq_no;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -