⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mft.c

📁 上一个上传的有问题,这个是好的。visopsys包括系统内核和GUI的全部SOURCE code ,还包括一些基本的docs文档。里面src子目录对应所有SOURCE code.对于想研究操作系统的朋
💻 C
📖 第 1 页 / 共 4 页
字号:
			size = ll << 3;			bit = data_pos & 7;			data_pos &= ~7ull;			ntfs_log_debug("Before inner for loop: size 0x%x, "					"data_pos 0x%llx, bit 0x%llx, "					"*byte 0x%hhx, b %u.\n", size,					(long long)data_pos, (long long)bit,					byte ? *byte : -1, b);			for (; bit < size && data_pos + bit < pass_end;					bit &= ~7ull, bit += 8) {				byte = buf + (bit >> 3);				if (*byte == 0xff)					continue;				/* Note: ffz() result must be zero based. */				b = ntfs_ffz((unsigned long)*byte);				if (b < 8 && b >= (bit & 7)) {					free(buf);					return data_pos + (bit & ~7ull) + b;				}			}			ntfs_log_debug("After inner for loop: size 0x%x, "					"data_pos 0x%llx, bit 0x%llx, "					"*byte 0x%hhx, b %u.\n", size,					(long long)data_pos, (long long)bit,					byte ? *byte : -1, b);			data_pos += size;			/*			 * If the end of the pass has not been reached yet,			 * continue searching the mft bitmap for a zero bit.			 */			if (data_pos < pass_end)				continue;		}		/* Do the next pass. */		pass++;		if (pass == 2) {			/*			 * Starting the second pass, in which we scan the first			 * part of the zone which we omitted earlier.			 */			pass_end = pass_start;			data_pos = pass_start = 24;			ntfs_log_debug("pass %i, pass_start 0x%llx, pass_end "					"0x%llx.\n", pass, (long long)pass_start,					(long long)pass_end);			if (data_pos >= pass_end)				break;		}	}	/* No free mft records in currently initialized mft bitmap. */	free(buf);	errno = ENOSPC;	return -1;}/** * ntfs_mft_bitmap_extend_allocation - extend mft bitmap attribute by a cluster * @vol:	volume on which to extend the mft bitmap attribute * * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster. * * Note:  Only changes allocated_size, i.e. does not touch initialized_size or * data_size. * * Return 0 on success and -1 on error with errno set to the error code. */static int ntfs_mft_bitmap_extend_allocation(ntfs_volume *vol){	LCN lcn;	s64 ll = 0; /* silence compiler warning */	ntfs_attr *mftbmp_na, *lcnbmp_na;	runlist_element *rl, *rl2 = NULL; /* silence compiler warning */	ntfs_attr_search_ctx *ctx;	MFT_RECORD *m = NULL; /* silence compiler warning */	ATTR_RECORD *a = NULL; /* silence compiler warning */	int ret, mp_size;	u32 old_alen = 0; /* silence compiler warning */	u8 b, tb;	struct {		u8 added_cluster:1;		u8 added_run:1;		u8 mp_rebuilt:1;	} status = { 0, 0, 0 };	mftbmp_na = vol->mftbmp_na;	lcnbmp_na = vol->lcnbmp_na;	/*	 * Determine the last lcn of the mft bitmap.  The allocated size of the	 * mft bitmap cannot be zero so we are ok to do this.	 */	rl = ntfs_attr_find_vcn(mftbmp_na, (mftbmp_na->allocated_size - 1) >>			vol->cluster_size_bits);	if (!rl || !rl->length || rl->lcn < 0) {		ntfs_log_error("Failed to determine last allocated "				"cluster of mft bitmap attribute.\n");		if (rl)			errno = EIO;		return -1;	}	lcn = rl->lcn + rl->length;	/*	 * Attempt to get the cluster following the last allocated cluster by	 * hand as it may be in the MFT zone so the allocator would not give it	 * to us.	 */	ret = (int)ntfs_attr_pread(lcnbmp_na, lcn >> 3, 1, &b);	if (ret < 0) {		ntfs_log_error("Failed to read from lcn bitmap.\n");		return -1;	}	ntfs_log_debug("Read %i byte%s.\n", ret, ret == 1 ? "" : "s");	tb = 1 << (lcn & 7ull);	if (ret == 1 && b != 0xff && !(b & tb)) {		/* Next cluster is free, allocate it. */		b |= tb;		ret = (int)ntfs_attr_pwrite(lcnbmp_na, lcn >> 3, 1, &b);		if (ret < 1) {			ntfs_log_error("Failed to write to lcn "					"bitmap.\n");			if (!ret)				errno = EIO;			return -1;		}		/* Update the mft bitmap runlist. */		rl->length++;		rl[1].vcn++;		status.added_cluster = 1;		ntfs_log_debug("Appending one cluster to mft bitmap.\n");	} else {		/* Allocate a cluster from the DATA_ZONE. */		rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE);		if (!rl2) {			ntfs_log_error("Failed to allocate a cluster for "					"the mft bitmap.\n");			return -1;		}		rl = ntfs_runlists_merge(mftbmp_na->rl, rl2);		if (!rl) {			ret = errno;			ntfs_log_error("Failed to merge runlists for mft "					"bitmap.\n");			if (ntfs_cluster_free_from_rl(vol, rl2))				ntfs_log_error("Failed to deallocate "						"cluster.%s\n", es);			free(rl2);			errno = ret;			return -1;		}		mftbmp_na->rl = rl;		status.added_run = 1;		ntfs_log_debug("Adding one run to mft bitmap.\n");		/* Find the last run in the new runlist. */		for (; rl[1].length; rl++)			;	}	/*	 * Update the attribute record as well.  Note: @rl is the last	 * (non-terminator) runlist element of mft bitmap.	 */	ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL);	if (!ctx) {		ntfs_log_error("Failed to get search context.\n");		goto undo_alloc;	}	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,			mftbmp_na->name_len, 0, rl[1].vcn, NULL, 0, ctx)) {		ntfs_log_error("Failed to find last attribute extent of "				"mft bitmap attribute.\n");		goto undo_alloc;	}	m = ctx->mrec;	a = ctx->attr;	ll = sle64_to_cpu(a->lowest_vcn);	rl2 = ntfs_attr_find_vcn(mftbmp_na, ll);	if (!rl2 || !rl2->length) {		ntfs_log_error("Failed to determine previous last "				"allocated cluster of mft bitmap attribute.\n");		if (rl2)			errno = EIO;		goto undo_alloc;	}	/* Get the size for the new mapping pairs array for this extent. */	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll);	if (mp_size <= 0) {		ntfs_log_error("Get size for mapping pairs failed for "				"mft bitmap attribute extent.\n");		goto undo_alloc;	}	/* Expand the attribute record if necessary. */	old_alen = le32_to_cpu(a->length);	if (ntfs_attr_record_resize(m, a, mp_size +			le16_to_cpu(a->mapping_pairs_offset))) {		if (errno != ENOSPC) {			ntfs_log_error("Failed to resize attribute "					"record for mft bitmap attribute.\n");			goto undo_alloc;		}		// TODO: Deal with this by moving this extent to a new mft		// record or by starting a new extent in a new mft record.		ntfs_log_error("Not enough space in this mft record to "				"accommodate extended mft bitmap attribute "				"extent.  Cannot handle this yet.\n");		errno = EOPNOTSUPP;		goto undo_alloc;	}	status.mp_rebuilt = 1;	/* Generate the mapping pairs array directly into the attr record. */	if (ntfs_mapping_pairs_build(vol, (u8*)a +			le16_to_cpu(a->mapping_pairs_offset), mp_size, rl2, ll,			NULL)) {		ntfs_log_error("Failed to build mapping pairs array for "				"mft bitmap attribute.\n");		errno = EIO;		goto undo_alloc;	}	/* Update the highest_vcn. */	a->highest_vcn = cpu_to_sle64(rl[1].vcn - 1);	/*	 * We now have extended the mft bitmap allocated_size by one cluster.	 * Reflect this in the ntfs_attr structure and the attribute record.	 */	if (a->lowest_vcn) {		/*		 * We are not in the first attribute extent, switch to it, but		 * first ensure the changes will make it to disk later.		 */		ntfs_inode_mark_dirty(ctx->ntfs_ino);		ntfs_attr_reinit_search_ctx(ctx);		if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,				mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) {			ntfs_log_error("Failed to find first attribute "					"extent of mft bitmap attribute.\n");			goto restore_undo_alloc;		}		a = ctx->attr;	}	mftbmp_na->allocated_size += vol->cluster_size;	a->allocated_size = cpu_to_sle64(mftbmp_na->allocated_size);	/* Ensure the changes make it to disk. */	ntfs_inode_mark_dirty(ctx->ntfs_ino);	ntfs_attr_put_search_ctx(ctx);	return 0;restore_undo_alloc:	ret = errno;	ntfs_attr_reinit_search_ctx(ctx);	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,			mftbmp_na->name_len, 0, rl[1].vcn, NULL, 0, ctx)) {		ntfs_log_error("Failed to find last attribute extent of "				"mft bitmap attribute.%s\n", es);		ntfs_attr_put_search_ctx(ctx);		mftbmp_na->allocated_size += vol->cluster_size;		/*		 * The only thing that is now wrong is ->allocated_size of the		 * base attribute extent which chkdsk should be able to fix.		 */		errno = ret;		return -1;	}	m = ctx->mrec;	a = ctx->attr;	a->highest_vcn = cpu_to_sle64(rl[1].vcn - 2);	errno = ret;undo_alloc:	ret = errno;	if (status.added_cluster) {		/* Truncate the last run in the runlist by one cluster. */		rl->length--;		rl[1].vcn--;	} else if (status.added_run) {		lcn = rl->lcn;		/* Remove the last run from the runlist. */		rl->lcn = rl[1].lcn;		rl->length = 0;	}	/* Deallocate the cluster. */	if (ntfs_bitmap_clear_bit(lcnbmp_na, lcn))		ntfs_log_error("Failed to free cluster.%s\n", es);	if (status.mp_rebuilt) {		if (ntfs_mapping_pairs_build(vol, (u8*)a +				le16_to_cpu(a->mapping_pairs_offset),				old_alen - le16_to_cpu(a->mapping_pairs_offset),				rl2, ll, NULL))			ntfs_log_error("Failed to restore mapping "					"pairs array.%s\n", es);		if (ntfs_attr_record_resize(m, a, old_alen))			ntfs_log_error("Failed to restore attribute "					"record.%s\n", es);		ntfs_inode_mark_dirty(ctx->ntfs_ino);	}	if (ctx)		ntfs_attr_put_search_ctx(ctx);	errno = ret;	return -1;}/** * ntfs_mft_bitmap_extend_initialized - extend mft bitmap initialized data * @vol:	volume on which to extend the mft bitmap attribute * * Extend the initialized portion of the mft bitmap attribute on the ntfs * volume @vol by 8 bytes. * * Note:  Only changes initialized_size and data_size, i.e. requires that * allocated_size is big enough to fit the new initialized_size. * * Return 0 on success and -1 on error with errno set to the error code. */static int ntfs_mft_bitmap_extend_initialized(ntfs_volume *vol){	s64 old_data_size, old_initialized_size, ll;	ntfs_attr *mftbmp_na;	ntfs_attr_search_ctx *ctx;	ATTR_RECORD *a;	int err;	mftbmp_na = vol->mftbmp_na;	ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL);	if (!ctx) {		ntfs_log_error("Failed to get search context.\n");		return -1;	}	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,			mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) {		ntfs_log_error("Failed to find first attribute extent of "				"mft bitmap attribute.\n");		err = errno;		goto put_err_out;	}	a = ctx->attr;	old_data_size = mftbmp_na->data_size;	old_initialized_size = mftbmp_na->initialized_size;	mftbmp_na->initialized_size += 8;	a->initialized_size = cpu_to_sle64(mftbmp_na->initialized_size);	if (mftbmp_na->initialized_size > mftbmp_na->data_size) {		mftbmp_na->data_size = mftbmp_na->initialized_size;		a->data_size = cpu_to_sle64(mftbmp_na->data_size);	}	/* Ensure the changes make it to disk. */	ntfs_inode_mark_dirty(ctx->ntfs_ino);	ntfs_attr_put_search_ctx(ctx);	/* Initialize the mft bitmap attribute value with zeroes. */	ll = 0;	ll = ntfs_attr_pwrite(mftbmp_na, old_initialized_size, 8, &ll);	if (ll == 8) {		ntfs_log_debug("Wrote eight initialized bytes to mft bitmap.\n");		return 0;	}	ntfs_log_error("Failed to write to mft bitmap.\n");	err = errno;	if (ll >= 0)		err = EIO;	/* Try to recover from the error. */	ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL);	if (!ctx) {		ntfs_log_error("Failed to get search context.%s\n", es);		goto err_out;	}	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,			mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) {		ntfs_log_error("Failed to find first attribute extent of "				"mft bitmap attribute.%s\n", es);put_err_out:		ntfs_attr_put_search_ctx(ctx);		goto err_out;	}	a = ctx->attr;	mftbmp_na->initialized_size = old_initialized_size;	a->initialized_size = cpu_to_sle64(old_initialized_size);	if (mftbmp_na->data_size != old_data_size) {		mftbmp_na->data_size = old_data_size;		a->data_size = cpu_to_sle64(old_data_size);	}	ntfs_inode_mark_dirty(ctx->ntfs_ino);	ntfs_attr_put_search_ctx(ctx);	ntfs_log_debug("Restored status of mftbmp: 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);err_out:	errno = err;	return -1;}/** * ntfs_mft_data_extend_allocation - extend mft data attribute * @vol:	volume on which to extend the mft data attribute * * Extend the mft data attribute on the ntfs volume @vol by 16 mft records * worth of clusters or if not enough space for this by one mft record worth * of clusters. * * Note:  Only changes allocated_size, i.e. does not touch initialized_size or * data_size. * * Return 0 on success and -1 on error with errno set to the error code. */static int ntfs_mft_data_extend_allocation(ntfs_volume *vol){	LCN lcn;	VCN old_last_vcn;	s64 min_nr, nr, ll = 0; /* silence compiler warning */	ntfs_attr *mft_na;	runlist_element *rl, *rl2;	ntfs_attr_search_ctx *ctx;	MFT_RECORD *m = NULL; /* silence compiler warning */	ATTR_RECORD *a = NULL; /* silence compiler warning */	int err, mp_size;	u32 old_alen = 0; /* silence compiler warning */	BOOL mp_rebuilt = FALSE;	ntfs_log_debug("Extending mft data allocation.\n");	mft_na = vol->mft_na;	/*	 * Determine the preferred allocation location, i.e. the last lcn of	 * the mft data attribute.  The allocated size of the mft data	 * attribute cannot be zero so we are ok to do this.	 */	rl = ntfs_attr_find_vcn(mft_na,			(mft_na->allocated_size - 1) >> vol->cluster_size_bits);	if (!rl || !rl->length || rl->lcn < 0) {		ntfs_log_error("Failed to determine last allocated "				"cluster of mft data attribute.\n");		if (rl)			errno = EIO;		return -1;	}	lcn = rl->lcn + rl->length;	ntfs_log_debug("Last lcn of mft data attribute is 0x%llx.\n", (long long)lcn);	/* Minimum allocation is one mft record worth of clusters. */	min_nr = vol->mft_record_size >> vol->cluster_size_bits;	if (!min_nr)		min_nr = 1;	/* Want to allocate 16 mft records worth of clusters. */	nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;	if (!nr)		nr = min_nr;	ntfs_log_debug("Trying mft data allocation with default cluster count "			"%lli.\n", (long long)nr);	old_last_vcn = rl[1].vcn;	do {		rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE);		if (rl2)			break;		if (errno != ENOSPC || nr == min_nr) {			ntfs_log_error("Failed to allocate the minimal "					"number of clusters (%lli) for the "					"mft data attribute.\n", (long long)nr);			return -1;		}		/*		 * There is not enough space to do the allocation, but there		 * might be enough space to do a minimal allocation so try that		 * before failing.		 */		nr = min_nr;		ntfs_log_debug("Retrying mft data allocation with minimal cluster "				"count %lli.\n", (long long)nr);	} while (1);	rl = ntfs_runlists_merge(mft_na->rl, rl2);	if (!rl) {		err = errno;		ntfs_log_error("Failed to merge runlists for mft data "				"attribute.\n");		if (ntfs_cluster_free_from_rl(vol, rl2))			ntfs_log_error("Failed to deallocate clusters "					"from the mft data attribute.%s\n", es);		free(rl2);		errno = err;		return -1;	}	mft_na->rl = rl;	ntfs_log_debug("Allocated %lli clusters.\n", nr);	/* Find the last run in the new runlist. */	for (; rl[1].length; rl++)		;	/* Update the attribute record as well. */	ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL);	if (!ctx) {		ntfs_log_error("Failed to get search context.\n");		goto undo_alloc;	}	if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0,			rl[1].vcn, NULL, 0, ctx)) {		ntfs_log_error("Failed to find last attribute extent of "				"mft data attribute.\n");		goto undo_alloc;	}	m = ctx->mrec;	a = ctx->attr;	ll = sle64_to_cpu(a->lowest_vcn);	rl2 = ntfs_attr_find_vcn(mft_na, ll);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -