📄 mft.c
字号:
/* Sync MFT to minimize data loss if there won't be clean unmount. */ if (ntfs_inode_sync(mft_na->ni)) goto undo_data_init; ret = 0;out: ntfs_log_leave("\n"); return ret; undo_data_init: mft_na->initialized_size = old_data_initialized; mft_na->data_size = old_data_size; goto out;}/** * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume * @vol: volume on which to allocate the mft record * @base_ni: open base inode if allocating an extent mft record or NULL * * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol. * * If @base_ni is NULL make the mft record a base mft record and allocate it at * the default allocator position. * * If @base_ni is not NULL make the allocated mft record an extent record, * allocate it starting at the mft record after the base mft record and attach * the allocated and opened ntfs inode to the base inode @base_ni. * * On success return the now opened ntfs (extent) inode of the mft record. * * On error return NULL with errno set to the error code. * * To find a free mft record, we scan the mft bitmap for a zero bit. To * optimize this we start scanning at the place specified by @base_ni or if * @base_ni is NULL we start where we last stopped and we perform wrap around * when we reach the end. Note, we do not try to allocate mft records below * number 24 because numbers 0 to 15 are the defined system files anyway and 16 * to 24 are special in that they are used for storing extension mft records * for the $DATA attribute of $MFT. This is required to avoid the possibility * of creating a run list with a circular dependence which once written to disk * can never be read in again. Windows will only use records 16 to 24 for * normal files if the volume is completely out of space. We never use them * which means that when the volume is really out of space we cannot create any * more files while Windows can still create up to 8 small files. We can start * doing this at some later time, it does not matter much for now. * * When scanning the mft bitmap, we only search up to the last allocated mft * record. If there are no free records left in the range 24 to number of * allocated mft records, then we extend the $MFT/$DATA attribute in order to * create free mft records. We extend the allocated size of $MFT/$DATA by 16 * records at a time or one cluster, if cluster size is above 16kiB. If there * is not sufficient space to do this, we try to extend by a single mft record * or one cluster, if cluster size is above the mft record size, but we only do * this if there is enough free space, which we know from the values returned * by the failed cluster allocation function when we tried to do the first * allocation. * * No matter how many mft records we allocate, we initialize only the first * allocated mft record, incrementing mft data size and initialized size * accordingly, open an ntfs_inode for it and return it to the caller, unless * there are less than 24 mft records, in which case we allocate and initialize * mft records until we reach record 24 which we consider as the first free mft * record for use by normal files. * * If during any stage we overflow the initialized data in the mft bitmap, we * extend the initialized size (and data size) by 8 bytes, allocating another * cluster if required. The bitmap data size has to be at least equal to the * number of mft records in the mft, but it can be bigger, in which case the * superfluous bits are padded with zeroes. * * Thus, when we return successfully (return value non-zero), we will have: * - initialized / extended the mft bitmap if necessary, * - initialized / extended the mft data if necessary, * - set the bit corresponding to the mft record being allocated in the * mft bitmap, * - open an ntfs_inode for the allocated mft record, and we will * - return the ntfs_inode. * * On error (return value zero), nothing will have changed. If we had changed * anything before the error occurred, we will have reverted back to the * starting state before returning to the caller. Thus, except for bugs, we * should always leave the volume in a consistent state when returning from * this function. * * Note, this function cannot make use of most of the normal functions, like * for example for attribute resizing, etc, because when the run list overflows * the base mft record and an attribute list is used, it is very important that * the extension mft records used to store the $DATA attribute of $MFT can be * reached without having to read the information contained inside them, as * this would make it impossible to find them in the first place after the * volume is dismounted. $MFT/$BITMAP probably does not need to follow this * rule because the bitmap is not essential for finding the mft records, but on * the other hand, handling the bitmap in this special way would make life * easier because otherwise there might be circular invocations of functions * when reading the bitmap but if we are careful, we should be able to avoid * all problems. */ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, ntfs_inode *base_ni){ s64 ll, bit; ntfs_attr *mft_na, *mftbmp_na; MFT_RECORD *m; ntfs_inode *ni = NULL; int err; u16 seq_no, usn; if (base_ni) ntfs_log_enter("Entering (allocating an extent mft record for " "base mft record %lld).\n", (long long)base_ni->mft_no); else ntfs_log_enter("Entering (allocating a base mft record)\n"); if (!vol || !vol->mft_na || !vol->mftbmp_na) { errno = EINVAL; goto out; } mft_na = vol->mft_na; mftbmp_na = vol->mftbmp_na;retry: bit = ntfs_mft_bitmap_find_free_rec(vol, base_ni); if (bit >= 0) { ntfs_log_debug("found free record (#1) at %lld\n", (long long)bit); goto found_free_rec; } if (errno != ENOSPC) goto out; /* * No free mft records left. If the mft bitmap already covers more * than the currently used mft records, the next records are all free, * so we can simply allocate the first unused mft record. * Note: We also have to make sure that the mft bitmap at least covers * the first 24 mft records as they are special and whilst they may not * be in use, we do not allocate from them. */ ll = mft_na->initialized_size >> vol->mft_record_size_bits; if (mftbmp_na->initialized_size << 3 > ll && mftbmp_na->initialized_size > 3) { bit = ll; if (bit < 24) bit = 24; ntfs_log_debug("found free record (#2) at %lld\n", (long long)bit); goto found_free_rec; } /* * The mft bitmap needs to be expanded until it covers the first unused * mft record that we can allocate. * Note: The smallest mft record we allocate is mft record 24. */ ntfs_log_debug("Status of mftbmp before extension: allocated_size 0x%llx, " "data_size 0x%llx, initialized_size 0x%llx.\n", (long long)mftbmp_na->allocated_size, (long long)mftbmp_na->data_size, (long long)mftbmp_na->initialized_size); if (mftbmp_na->initialized_size + 8 > mftbmp_na->allocated_size) { /* Need to extend bitmap by one more cluster. */ ntfs_log_debug("mftbmp: initialized_size + 8 > allocated_size.\n"); if (ntfs_mft_bitmap_extend_allocation(vol)) goto err_out; ntfs_log_debug("Status of mftbmp after allocation extension: " "allocated_size 0x%llx, data_size 0x%llx, " "initialized_size 0x%llx.\n", (long long)mftbmp_na->allocated_size, (long long)mftbmp_na->data_size, (long long)mftbmp_na->initialized_size); } /* * We now have sufficient allocated space, extend the initialized_size * as well as the data_size if necessary and fill the new space with * zeroes. */ bit = mftbmp_na->initialized_size << 3; if (ntfs_mft_bitmap_extend_initialized(vol)) goto err_out; ntfs_log_debug("Status of mftbmp after initialized extension: " "allocated_size 0x%llx, data_size 0x%llx, " "initialized_size 0x%llx.\n", (long long)mftbmp_na->allocated_size, (long long)mftbmp_na->data_size, (long long)mftbmp_na->initialized_size); ntfs_log_debug("found free record (#3) at %lld\n", (long long)bit);found_free_rec: /* @bit is the found free mft record, allocate it in the mft bitmap. */ if (ntfs_bitmap_set_bit(mftbmp_na, bit)) { ntfs_log_error("Failed to allocate bit in mft bitmap.\n"); goto err_out; } /* The mft bitmap is now uptodate. Deal with mft data attribute now. */ ll = (bit + 1) << vol->mft_record_size_bits; if (ll > mft_na->initialized_size) if (ntfs_mft_record_init(vol, ll) < 0) goto undo_mftbmp_alloc; /* * We now have allocated and initialized the mft record. Need to read * it from disk and re-format it, preserving the sequence number if it * is not zero as well as the update sequence number if it is not zero * or -1 (0xffff). */ m = ntfs_malloc(vol->mft_record_size); if (!m) goto undo_mftbmp_alloc; if (ntfs_mft_record_read(vol, bit, m)) { ntfs_log_perror("Error reading mft %lld", (long long)bit); free(m); goto undo_mftbmp_alloc; } /* Sanity check that the mft record is really not in use. */ if (ntfs_is_file_record(m->magic) && (m->flags & MFT_RECORD_IN_USE)) { ntfs_log_error("Inode %lld is used but it wasn't marked in " "$MFT bitmap. Fixed.\n", (long long)bit); free(m); goto retry; } seq_no = m->sequence_number; usn = *(u16*)((u8*)m + le16_to_cpu(m->usa_ofs)); if (ntfs_mft_record_layout(vol, bit, m)) { ntfs_log_error("Failed to re-format mft record.\n"); free(m); goto undo_mftbmp_alloc; } if (le16_to_cpu(seq_no)) m->sequence_number = seq_no; seq_no = le16_to_cpu(usn); if (seq_no && seq_no != 0xffff) *(u16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn; /* Set the mft record itself in use. */ m->flags |= MFT_RECORD_IN_USE; /* Now need to open an ntfs inode for the mft record. */ ni = ntfs_inode_allocate(vol); if (!ni) { ntfs_log_error("Failed to allocate buffer for inode.\n"); free(m); goto undo_mftbmp_alloc; } ni->mft_no = bit; ni->mrec = m; /* * If we are allocating an extent mft record, make the opened inode an * extent inode and attach it to the base inode. Also, set the base * mft record reference in the extent inode. */ if (base_ni) { ni->nr_extents = -1; ni->base_ni = base_ni; m->base_mft_record = MK_LE_MREF(base_ni->mft_no, le16_to_cpu(base_ni->mrec->sequence_number)); /* * Attach the extent inode to the base inode, reallocating * memory if needed. */ if (!(base_ni->nr_extents & 3)) { ntfs_inode **extent_nis; int i; i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); extent_nis = ntfs_malloc(i); if (!extent_nis) { free(m); free(ni); goto undo_mftbmp_alloc; } if (base_ni->extent_nis) { memcpy(extent_nis, base_ni->extent_nis, i - 4 * sizeof(ntfs_inode *)); free(base_ni->extent_nis); } base_ni->extent_nis = extent_nis; } base_ni->extent_nis[base_ni->nr_extents++] = ni; } /* Make sure the allocated inode is written out to disk later. */ ntfs_inode_mark_dirty(ni); /* Initialize time, allocated and data size in ntfs_inode struct. */ ni->data_size = ni->allocated_size = 0; ni->flags = 0; ni->creation_time = ni->last_data_change_time = ni->last_mft_change_time = ni->last_access_time = time(NULL); /* Update the default mft allocation position if it was used. */ if (!base_ni) vol->mft_data_pos = bit + 1; /* Return the opened, allocated inode of the allocated mft record. */ ntfs_log_debug("allocated %sinode 0x%llx.\n", base_ni ? "extent " : "", (long long)bit); vol->free_mft_records--; out: ntfs_log_leave("\n"); return ni;undo_mftbmp_alloc: err = errno; if (ntfs_bitmap_clear_bit(mftbmp_na, bit)) ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es); errno = err;err_out: if (!errno) errno = EIO; ni = NULL; goto out; }/** * ntfs_mft_record_free - free an mft record on an ntfs volume * @vol: volume on which to free the mft record * @ni: open ntfs inode of the mft record to free * * Free the mft record of the open inode @ni on the mounted ntfs volume @vol. * Note that this function calls ntfs_inode_close() internally and hence you * cannot use the pointer @ni any more after this function returns success. * * On success return 0 and on error return -1 with errno set to the error code. */int ntfs_mft_record_free(ntfs_volume *vol, ntfs_inode *ni){ u64 mft_no; int err; u16 seq_no, old_seq_no; ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni->mft_no); if (!vol || !vol->mftbmp_na || !ni) { errno = EINVAL; return -1; } /* Cache the mft reference for later. */ mft_no = ni->mft_no; /* Mark the mft record as not in use. */ ni->mrec->flags &= ~MFT_RECORD_IN_USE; /* Increment the sequence number, skipping zero, if it is not zero. */ old_seq_no = ni->mrec->sequence_number; seq_no = le16_to_cpu(old_seq_no); if (seq_no == 0xffff) seq_no = 1; else if (seq_no) seq_no++; ni->mrec->sequence_number = cpu_to_le16(seq_no); /* Set the inode dirty and write it out. */ ntfs_inode_mark_dirty(ni); if (ntfs_inode_sync(ni)) { err = errno; goto sync_rollback; } /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */ if (ntfs_bitmap_clear_bit(vol->mftbmp_na, mft_no)) { err = errno; // FIXME: If ntfs_bitmap_clear_run() guarantees rollback on // error, this could be changed to goto sync_rollback; goto bitmap_rollback; } /* Throw away the now freed inode. */ if (!ntfs_inode_close(ni)) { vol->free_mft_records++; return 0; } err = errno; /* Rollback what we did... */bitmap_rollback: if (ntfs_bitmap_set_bit(vol->mftbmp_na, mft_no)) ntfs_log_debug("Eeek! Rollback failed in ntfs_mft_record_free(). " "Leaving inconsistent metadata!\n");sync_rollback: ni->mrec->flags |= MFT_RECORD_IN_USE; ni->mrec->sequence_number = old_seq_no; ntfs_inode_mark_dirty(ni); errno = err; return -1;}/** * ntfs_mft_usn_dec - Decrement USN by one * @mrec: pointer to an mft record * * On success return 0 and on error return -1 with errno set. */int ntfs_mft_usn_dec(MFT_RECORD *mrec){ u16 usn, *usnp; if (!mrec) { errno = EINVAL; return -1; } usnp = (u16 *)((char *)mrec + le16_to_cpu(mrec->usa_ofs)); usn = le16_to_cpup(usnp); if (usn-- <= 1) usn = 0xfffe; *usnp = cpu_to_le16(usn); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -