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

📄 bitmap.c

📁 上一个上传的有问题,这个是好的。visopsys包括系统内核和GUI的全部SOURCE code ,还包括一些基本的docs文档。里面src子目录对应所有SOURCE code.对于想研究操作系统的朋
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * bitmap.c - Bitmap handling code. Part of the Linux-NTFS project. * * Copyright (c) 2002-2006 Anton Altaparmakov * Copyright (c) 2004-2005 Richard Russon * * This program/include file is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program/include file is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (in the main directory of the Linux-NTFS * distribution in the file COPYING); if not, write to the Free Software * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * Modified 01/2007 by Andy McLaughlin for Visopsys port. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_STDIO_H#include <stdio.h>#endif#ifdef HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_ERRNO_H#include <errno.h>#endif#include "types.h"#include "attrib.h"#include "bitmap.h"#include "debug.h"#include "logging.h"/** * ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value * @na:		attribute containing the bitmap * @start_bit:	first bit to set * @count:	number of bits to set * @value:	value to set the bits to (i.e. 0 or 1) * * Set @count bits starting at bit @start_bit in the bitmap described by the * attribute @na to @value, where @value is either 0 or 1. * * On success return 0 and on error return -1 with errno set to the error code. */static __inline__ int ntfs_bitmap_set_bits_in_run(ntfs_attr *na, s64 start_bit,		s64 count, int value){	s64 bufsize, br;	u8 *buf, *lastbyte_buf;	int bit, firstbyte, lastbyte, lastbyte_pos, tmp, err;	if (!na || start_bit < 0 || count < 0) {		errno = EINVAL;		return -1;	}	bit = start_bit & 7;	if (bit)		firstbyte = 1;	else		firstbyte = 0;	/* Calculate the required buffer size in bytes, capping it at 8kiB. */	bufsize = ((count - (bit ? 8 - bit : 0) + 7) >> 3) + firstbyte;	if (bufsize > 8192)		bufsize = 8192;	/* Allocate memory. */	buf = (u8*)malloc(bufsize);	if (!buf)		return -1;	/* Depending on @value, zero or set all bits in the allocated buffer. */	memset(buf, value ? 0xff : 0, bufsize);	/* If there is a first partial byte... */	if (bit) {		/* read it in... */		br = ntfs_attr_pread(na, start_bit >> 3, 1, buf);		if (br != 1) {			free(buf);			errno = EIO;			return -1;		}		/* and set or clear the appropriate bits in it. */		while ((bit & 7) && count--) {			if (value)				*buf |= 1 << bit++;			else				*buf &= ~(1 << bit++);		}		/* Update @start_bit to the new position. */		start_bit = (start_bit + 7) & ~7;	}	/* Loop until @count reaches zero. */	lastbyte = 0;	lastbyte_buf = NULL;	bit = count & 7;	do {		/* If there is a last partial byte... */		if (count > 0 && bit) {			lastbyte_pos = ((count + 7) >> 3) + firstbyte;			if (!lastbyte_pos) {				// FIXME: Eeek! BUG!				ntfs_log_trace("Eeek! lastbyte is zero. Leaving "						"inconsistent metadata.\n");				err = EIO;				goto free_err_out;			}			/* and it is in the currently loaded bitmap window... */			if (lastbyte_pos <= bufsize) {				lastbyte_buf = buf + lastbyte_pos - 1;				/* read the byte in... */				br = ntfs_attr_pread(na, (start_bit + count) >>						3, 1, lastbyte_buf);				if (br != 1) {					// FIXME: Eeek! We need rollback! (AIA)					ntfs_log_trace("Eeek! Read of last byte "							"failed. Leaving "							"inconsistent metadata.\n");					err = EIO;					goto free_err_out;				}				/* and set/clear the appropriate bits in it. */				while (bit && count--) {					if (value)						*lastbyte_buf |= 1 << --bit;					else						*lastbyte_buf &= ~(1 << --bit);				}				/* We don't want to come back here... */				bit = 0;				/* We have a last byte that we have handled. */				lastbyte = 1;			}		}		/* Write the prepared buffer to disk. */		tmp = (start_bit >> 3) - firstbyte;		br = ntfs_attr_pwrite(na, tmp, bufsize, buf);		if (br != bufsize) {			// FIXME: Eeek! We need rollback! (AIA)			ntfs_log_trace("Eeek! Failed to write buffer to bitmap. "					"Leaving inconsistent metadata.\n");			err = EIO;			goto free_err_out;		}		/* Update counters. */		tmp = (bufsize - firstbyte - lastbyte) << 3;		if (firstbyte) {			firstbyte = 0;			/*			 * Re-set the partial first byte so a subsequent write			 * of the buffer does not have stale, incorrect bits.			 */			*buf = value ? 0xff : 0;		}		start_bit += tmp;		count -= tmp;		if (bufsize > (tmp = (count + 7) >> 3))			bufsize = tmp;		if (lastbyte && count != 0) {			// FIXME: Eeek! BUG!			ntfs_log_trace("Eeek! Last buffer but count is not zero (= "					"%lli). Leaving inconsistent metadata.\n",					(long long)count);			err = EIO;			goto free_err_out;		}	} while (count > 0);	/* Done! */	free(buf);	return 0;free_err_out:	free(buf);	errno = err;	return -1;}/** * ntfs_bitmap_set_run - set a run of bits in a bitmap * @na:		attribute containing the bitmap * @start_bit:	first bit to set * @count:	number of bits to set * * Set @count bits starting at bit @start_bit in the bitmap described by the * attribute @na. * * On success return 0 and on error return -1 with errno set to the error code. */int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count){	return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 1);}/** * ntfs_bitmap_clear_run - clear a run of bits in a bitmap * @na:		attribute containing the bitmap * @start_bit:	first bit to clear * @count:	number of bits to clear * * Clear @count bits starting at bit @start_bit in the bitmap described by the * attribute @na. * * On success return 0 and on error return -1 with errno set to the error code. */int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count){	ntfs_log_trace("Dealloc from bit 0x%llx, count 0x%llx.\n",		       (long long)start_bit, (long long)count);	return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 0);}#ifndef __VISOPSYS__#ifdef NTFS_RICH#include "layout.h"#include "volume.h"#include "rich.h"/** * ntfs_bmp_rollback - Discard the in-memory bitmap changes * @bmp: * * Description... * * Returns: */int ntfs_bmp_rollback(struct ntfs_bmp *bmp){	int i;	if ((!bmp) || (bmp->count == 0))		return 0;	ntfs_log_trace ("bmp %p, records %d, attr %lld/%02X\n", bmp, bmp->count, MREF(bmp->attr->ni->mft_no), bmp->attr->type);	for (i = 0; i < bmp->count; i++)		free(bmp->data[i]);	free(bmp->data);	free(bmp->data_vcn);	bmp->data = NULL;	bmp->data_vcn = NULL;	bmp->count = 0;	return 0;}/** * ntfs_bmp_commit - Write the cached bitmap data to disk * @bmp: * * Description... * * Returns: */int ntfs_bmp_commit(struct ntfs_bmp *bmp){	int i;	u32 cs;	u32 ws; // write size	if (!bmp)		return 0;	if (bmp->count == 0)		return 0;	ntfs_log_trace ("bmp %p, records %d, attr %lld/%02X\n", bmp, bmp->count, MREF(bmp->attr->ni->mft_no), bmp->attr->type);#if 0	ntfs_log_debug("attr = 0x%02X\n", bmp->attr->type);	ntfs_log_debug("resident = %d\n", !NAttrNonResident(bmp->attr));	ntfs_log_debug("\ta size = %lld\n", bmp->attr->allocated_size);	ntfs_log_debug("\td size = %lld\n", bmp->attr->data_size);	ntfs_log_debug("\ti size = %lld\n", bmp->attr->initialized_size);#endif	ntfs_log_debug("commit bmp inode %lld, 0x%02X (%sresident)\n", bmp->attr->ni->mft_no, bmp->attr->type, NAttrNonResident(bmp->attr) ? "non-" : "");	if (NAttrNonResident(bmp->attr)) {		cs = bmp->vol->cluster_size;		// non-resident		for (i = 0; i < bmp->count; i++) {			if (((bmp->data_vcn[i]+1) * cs) < bmp->attr->data_size)				ws = cs;			else				ws = bmp->attr->data_size & (cs - 1);			//ntfs_log_debug("writing %d bytes\n", ws);			ntfs_attr_pwrite(bmp->attr, bmp->data_vcn[i] * cs, ws, bmp->data[i]); // XXX retval			ntfs_log_warning("\tntfs_attr_pwrite(vcn %lld)\n", bmp->data_vcn[i]);		}	} else {		// resident		ntfs_attr_pwrite(bmp->attr, bmp->data_vcn[0], bmp->attr->data_size, bmp->data[0]); // XXX retval		ntfs_log_warning("\tntfs_attr_pwrite resident (%lld)\n", bmp->attr->data_size);	}	ntfs_bmp_rollback(bmp);	return 0;}/** * ntfs_bmp_free - Destroy a bitmap object * @bmp: * * Description... * * Returns: */void ntfs_bmp_free(struct ntfs_bmp *bmp){	if (!bmp)		return;	ntfs_log_trace ("bmp %p, records %d, attr %lld/%02X\n", bmp, bmp->count, MREF(bmp->attr->ni->mft_no), bmp->attr->type);

⌨️ 快捷键说明

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